File indexing completed on 2024-04-21 14:55:14

0001 /* This file is part of the KDE libraries
0002     Copyright (c) 2006 David Faure <faure@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include <QSignalSpy>
0021 #include <QTest>
0022 #include "kglobalsettingstest.h"
0023 
0024 QTEST_MAIN(KGlobalSettingsTest)
0025 
0026 #include <kglobalsettings.h>
0027 #include <QProcess>
0028 #include <QEventLoop>
0029 #include <QDBusConnection>
0030 
0031 /**
0032  * The strategy of this test is:
0033  * We install QSignalSpy instances on many signals from KGlobalSettings::self(),
0034  * and then we get another process (kglobalsettingsclient) to call emitChange(),
0035  * and we check that the corresponding signals are emitted, i.e. that our process
0036  * got the dbus signal.
0037  *
0038  * As a nice side-effect we automatically test a bit of KProcess as well :)
0039  */
0040 
0041 void KGlobalSettingsTest::initTestCase()
0042 {
0043     // Some signals are only emitted when we are running a full KDE session. If
0044     // we are not then KDE applications follow the platform palette and font
0045     // settings.
0046     qputenv("KDE_FULL_SESSION", "1");
0047 
0048     // change to the bin dir
0049     QDir::setCurrent(QCoreApplication::applicationDirPath());
0050 
0051     QDBusConnectionInterface *bus = nullptr;
0052     if (!QDBusConnection::sessionBus().isConnected() || !(bus = QDBusConnection::sessionBus().interface())) {
0053         QFAIL("Session bus not found");
0054     }
0055 
0056     QVERIFY(!KGlobalSettings::showFilePreview(QUrl("audiocd:/")));
0057 }
0058 
0059 #define CREATE_ALL_SPYS \
0060     KGlobalSettings* settings = KGlobalSettings::self(); \
0061     settings->activate();                                                 \
0062     QSignalSpy palette_spy( settings, SIGNAL(kdisplayPaletteChanged()) ); \
0063     QSignalSpy font_spy( settings, SIGNAL(kdisplayFontChanged()) ); \
0064     QSignalSpy style_spy( settings, SIGNAL(kdisplayStyleChanged()) ); \
0065     QSignalSpy settings_spy( settings, SIGNAL(settingsChanged(int)) ); \
0066     QSignalSpy appearance_spy( settings, SIGNAL(appearanceChanged()) )
0067 
0068 static void callClient(const QString &opt, const char *signalToWaitFor)
0069 {
0070     QProcess proc;
0071     QString processName;
0072 #ifdef Q_OS_WIN
0073     processName = "kglobalsettingsclient.exe";
0074 #else
0075     if (QFile::exists("./kglobalsettingsclient.shell")) {
0076         processName = "./kglobalsettingsclient.shell";
0077     } else {
0078         QVERIFY(QFile::exists("./kglobalsettingsclient"));
0079         processName = "./kglobalsettingsclient";
0080     }
0081 #endif
0082 //     qDebug() << proc.args();
0083     proc.start(processName, QStringList() << opt);
0084     bool ok = proc.waitForFinished();
0085     QVERIFY(ok);
0086 
0087     QSignalSpy spy(KGlobalSettings::self(), signalToWaitFor);
0088     QVERIFY(spy.wait(5000));
0089 }
0090 
0091 void KGlobalSettingsTest::testPaletteChange()
0092 {
0093     CREATE_ALL_SPYS;
0094     callClient("-p", SIGNAL(kdisplayPaletteChanged()));
0095     QCOMPARE(palette_spy.size(), 1);
0096     QCOMPARE(font_spy.size(), 0);
0097     QCOMPARE(style_spy.size(), 0);
0098     QCOMPARE(settings_spy.size(), 0);
0099     QCOMPARE(appearance_spy.size(), 1);
0100 }
0101 
0102 void KGlobalSettingsTest::testFontChange()
0103 {
0104     CREATE_ALL_SPYS;
0105     callClient("-f", SIGNAL(kdisplayFontChanged()));
0106     QCOMPARE(palette_spy.size(), 0);
0107     QCOMPARE(font_spy.size(), 1);
0108     QCOMPARE(style_spy.size(), 0);
0109     QCOMPARE(settings_spy.size(), 0);
0110     QCOMPARE(appearance_spy.size(), 1);
0111 }
0112 
0113 void KGlobalSettingsTest::testSettingsChange()
0114 {
0115     CREATE_ALL_SPYS;
0116     callClient("--ps", SIGNAL(settingsChanged(int)));
0117     QCOMPARE(palette_spy.size(), 0);
0118     QCOMPARE(font_spy.size(), 0);
0119     QCOMPARE(style_spy.size(), 0);
0120     QCOMPARE(settings_spy.size(), 1);
0121     QCOMPARE(settings_spy.at(0).at(0).toInt(), (int)KGlobalSettings::SETTINGS_PATHS);
0122     QCOMPARE(appearance_spy.size(), 0);
0123 }
0124 
0125 #include "moc_kglobalsettingstest.cpp"