File indexing completed on 2024-06-09 04:00:32

0001 /*
0002     SPDX-FileCopyrightText: 2023 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include <QQmlApplicationEngine>
0008 #include <QQuickStyle>
0009 #include <QSignalSpy>
0010 #include <QStandardPaths>
0011 #include <QTest>
0012 
0013 #ifdef Q_OS_UNIX
0014 #include <KConfigGroup>
0015 #include <KSharedConfig>
0016 #endif
0017 
0018 #ifdef Q_OS_WIN
0019 #include <Windows.h>
0020 #endif
0021 
0022 using namespace Qt::StringLiterals;
0023 
0024 class IntegrationTest : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 private Q_SLOTS:
0029     void initTestCase();
0030     void cleanupTestCase();
0031 
0032 #ifdef Q_OS_UNIX
0033     void testAnimationSpeedModifier_kconfig();
0034 #endif
0035 #ifdef Q_OS_WIN
0036     void testAnimationSpeedModifier_windows();
0037 #endif
0038 
0039 private:
0040     QQmlApplicationEngine engine;
0041 };
0042 
0043 void IntegrationTest::initTestCase()
0044 {
0045     const QString libraryPath = QLatin1String(qgetenv("CMAKE_BINARY_DIR")) + QDir::separator() + "bin"_L1 + QDir::separator() + "org.kde.desktop"_L1
0046         + QLatin1String(qgetenv("CMAKE_SHARED_LIBRARY_SUFFIX"));
0047     QVERIFY2(QFileInfo::exists(libraryPath), qUtf8Printable(libraryPath));
0048     QDir libraryDir = QFileInfo(libraryPath).dir();
0049     const QString kirigamiFolder(libraryDir.absolutePath() + QDir::separator() + u"kf6"_s + QDir::separator() + u"kirigami" + QDir::separator()
0050                                  + u"platform"_s);
0051     QVERIFY2(libraryDir.mkpath(kirigamiFolder), qUtf8Printable(kirigamiFolder));
0052     const QString targetFilePath = kirigamiFolder + QDir::separator() + QFileInfo(libraryPath).fileName();
0053     QFile(targetFilePath).remove();
0054     QVERIFY(QFile(libraryPath).copy(targetFilePath));
0055     QCoreApplication::addLibraryPath(QString::fromLatin1(qgetenv("CMAKE_BINARY_DIR")));
0056     QQuickStyle::setStyle(u"org.kde.desktop"_s);
0057 
0058     QStandardPaths::setTestModeEnabled(true);
0059 
0060     QSignalSpy objectCreatedSpy(&engine, &QQmlApplicationEngine::objectCreated);
0061     engine.load(QFINDTESTDATA(u"animationspeedmodifiertest.qml"_s));
0062     if (objectCreatedSpy.empty()) {
0063         QVERIFY(objectCreatedSpy.wait());
0064     }
0065     QCOMPARE(engine.rootObjects().size(), 1);
0066 }
0067 
0068 void IntegrationTest::cleanupTestCase()
0069 {
0070 }
0071 
0072 #ifdef Q_OS_UNIX
0073 void IntegrationTest::testAnimationSpeedModifier_kconfig()
0074 {
0075     QObject *rootObject = engine.rootObjects().constFirst();
0076     const double defaultLongDuration = rootObject->property("longDuration").toDouble();
0077     QSignalSpy longDurationSpy(rootObject, SIGNAL(longDurationChanged()));
0078 
0079     KConfigGroup kdeGroup(KSharedConfig::openConfig(u"kdeglobals"_s, KConfig::SimpleConfig), u"KDE"_s);
0080     QVERIFY(kdeGroup.isValid());
0081     kdeGroup.writeEntry("AnimationDurationFactor", 0.5, KConfig::Notify);
0082     QVERIFY(kdeGroup.sync());
0083 
0084     if (longDurationSpy.empty()) {
0085         QVERIFY(longDurationSpy.wait());
0086     }
0087     QCOMPARE(rootObject->property("longDuration").toDouble(), defaultLongDuration * 0.5);
0088 
0089     kdeGroup.writeEntry("AnimationDurationFactor", 1);
0090 }
0091 #endif
0092 
0093 #ifdef Q_OS_WIN
0094 void IntegrationTest::testAnimationSpeedModifier_windows()
0095 {
0096     bool isAnimated = true;
0097     QVERIFY(SystemParametersInfoW(SPI_GETCLIENTAREAANIMATION, 0, &isAnimated, 0));
0098 
0099     QObject *rootObject = engine.rootObjects().constFirst();
0100     const double defaultLongDuration = rootObject->property("longDuration").toDouble();
0101     QSignalSpy longDurationSpy(rootObject, SIGNAL(longDurationChanged()));
0102 
0103     if (isAnimated) {
0104         QVERIFY(defaultLongDuration > 0);
0105     } else {
0106         QCOMPARE(defaultLongDuration, 0);
0107     }
0108     isAnimated = !isAnimated;
0109     SystemParametersInfoW(SPI_SETCLIENTAREAANIMATION, 0, &isAnimated, SPIF_SENDCHANGE);
0110     if (GetLastError() == 1459) {
0111         return;
0112     }
0113 
0114     if (longDurationSpy.empty()) {
0115         QVERIFY(longDurationSpy.wait());
0116     }
0117 
0118     if (isAnimated) {
0119         QVERIFY(defaultLongDuration > 0);
0120     } else {
0121         QCOMPARE(defaultLongDuration, 0);
0122     }
0123 }
0124 #endif
0125 
0126 QTEST_MAIN(IntegrationTest)
0127 
0128 #include "animationspeedmodifiertest.moc"