File indexing completed on 2024-04-21 16:19:57

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2013 Alejandro Fiestas Olivares <afiestas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "../src/platformtheme/kfontsettingsdata.h"
0008 #include "kdeplatformtheme_config.h"
0009 
0010 #include <QApplication>
0011 #include <QDir>
0012 #include <QFile>
0013 #include <QString>
0014 #include <QTest>
0015 
0016 #include <QDBusConnection>
0017 #include <QDBusMessage>
0018 
0019 static void prepareEnvironment()
0020 {
0021     qputenv("KDEHOME", QFile::encodeName(QDir::homePath() + QStringLiteral("/.kde5-unit-test-platformtheme")));
0022     qputenv("XDG_DATA_HOME", QFile::encodeName(QDir::homePath() + QStringLiteral("/.kde5-unit-test-platformtheme/xdg/local")));
0023     QByteArray configPath = QFile::encodeName(QDir::homePath() + QStringLiteral("/.kde5-unit-test-platformtheme/xdg/config"));
0024     qputenv("XDG_CONFIG_HOME", configPath);
0025     qputenv("KDE_SKIP_KDERC", "1");
0026     qunsetenv("KDE_COLOR_DEBUG");
0027 
0028     QDir().mkpath(configPath);
0029     configPath.append("/kdeglobals");
0030     QFile::remove(configPath);
0031     QFile::copy(CONFIGFILE, configPath);
0032 }
0033 
0034 // Run prepareEnvironment before qApp is created! slightly incorrect, QFile::encodeName can be wrong then.
0035 // But we can't use Q_COREAPP_STARTUP_FUNCTION because the platform theme ends up being created
0036 // first, with the wrong environment.
0037 Q_CONSTRUCTOR_FUNCTION(prepareEnvironment)
0038 
0039 class KFontSettingsData_UnitTest : public QWidget
0040 {
0041     Q_OBJECT
0042 public:
0043     bool event(QEvent *e) override
0044     {
0045         if (e->type() == QEvent::ApplicationFontChange) {
0046             m_appChangedFont = true;
0047         }
0048         return QWidget::event(e);
0049     }
0050 
0051 private:
0052     bool m_appChangedFont;
0053     KFontSettingsData *m_fonts;
0054 private Q_SLOTS:
0055     void initTestCase()
0056     {
0057         m_appChangedFont = false;
0058         m_fonts = new KFontSettingsData;
0059         qApp->processEvents(); // give time to delayed dbus connect
0060     }
0061 
0062     void testFonts()
0063     {
0064         QCOMPARE(m_fonts->font(KFontSettingsData::GeneralFont)->family(), QStringLiteral("OxyFontTest"));
0065         QCOMPARE(m_fonts->font(KFontSettingsData::FixedFont)->family(), QStringLiteral("OxyFixedTest Mono"));
0066         QCOMPARE(m_fonts->font(KFontSettingsData::ToolbarFont)->family(), QStringLiteral("OxyToolbarTest"));
0067         QCOMPARE(m_fonts->font(KFontSettingsData::MenuFont)->family(), QStringLiteral("OxyMenuTest"));
0068         QCOMPARE(m_fonts->font(KFontSettingsData::WindowTitleFont)->family(), QStringLiteral("OxyActiveTest"));
0069         QCOMPARE(m_fonts->font(KFontSettingsData::TaskbarFont)->family(), QStringLiteral("OxyTaskbarTest"));
0070         QCOMPARE(m_fonts->font(KFontSettingsData::SmallestReadableFont)->family(), QStringLiteral("OxySmallestReadableTest"));
0071     }
0072 
0073     void testFontsChanged()
0074     {
0075         QByteArray configPath = qgetenv("XDG_CONFIG_HOME");
0076         configPath.append("/kdeglobals");
0077         QFile::remove(configPath);
0078         QVERIFY(QFile::copy(CHANGED_CONFIGFILE, configPath));
0079 
0080         QEventLoop loop;
0081         QDBusConnection::sessionBus().connect(QString(),
0082                                               QStringLiteral("/KDEPlatformTheme"),
0083                                               QStringLiteral("org.kde.KDEPlatformTheme"),
0084                                               QStringLiteral("refreshFonts"),
0085                                               &loop,
0086                                               SLOT(quit()));
0087 
0088         QDBusMessage message =
0089             QDBusMessage::createSignal(QStringLiteral("/KDEPlatformTheme"), QStringLiteral("org.kde.KDEPlatformTheme"), QStringLiteral("refreshFonts"));
0090         QDBusConnection::sessionBus().send(message);
0091         loop.exec();
0092 
0093         QVERIFY(m_appChangedFont);
0094         QCOMPARE(m_fonts->font(KFontSettingsData::GeneralFont)->family(), QStringLiteral("ChangedFontTest"));
0095         QCOMPARE(m_fonts->font(KFontSettingsData::FixedFont)->family(), QStringLiteral("ChangedFixedTest Mono"));
0096         QCOMPARE(m_fonts->font(KFontSettingsData::ToolbarFont)->family(), QStringLiteral("ChangedToolbarTest"));
0097         QCOMPARE(m_fonts->font(KFontSettingsData::MenuFont)->family(), QStringLiteral("ChangedMenuTest"));
0098         QCOMPARE(m_fonts->font(KFontSettingsData::WindowTitleFont)->family(), QStringLiteral("ChangedActiveTest"));
0099         QCOMPARE(m_fonts->font(KFontSettingsData::TaskbarFont)->family(), QStringLiteral("ChangedTaskbarTest"));
0100         QCOMPARE(m_fonts->font(KFontSettingsData::SmallestReadableFont)->family(), QStringLiteral("ChangedSmallestReadableTest"));
0101     }
0102 };
0103 
0104 QTEST_MAIN(KFontSettingsData_UnitTest)
0105 
0106 #include "kfontsettingsdata_unittest.moc"