File indexing completed on 2024-05-19 05:34:39

0001 /*
0002     SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "themetest.h"
0008 #include <QApplication>
0009 #include <QSignalSpy>
0010 #include <QStandardPaths>
0011 
0012 #include <KConfigGroup>
0013 #include <KIconLoader>
0014 #include <KIconTheme>
0015 #include <KWindowSystem>
0016 #include <KX11Extras>
0017 
0018 #include <config-plasma.h>
0019 #if HAVE_X11
0020 #include <KSelectionOwner>
0021 #endif
0022 #include <array>
0023 
0024 void ThemeTest::initTestCase()
0025 {
0026     // make our theme in search path
0027     qputenv("XDG_DATA_DIRS", QByteArray(qgetenv("XDG_DATA_DIRS") + ":" + QFINDTESTDATA("data").toLocal8Bit()));
0028 
0029     // set default icon theme to test-theme
0030     QStandardPaths::setTestModeEnabled(true);
0031 
0032     m_theme = new Plasma::Theme("testtheme", this);
0033     QString configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
0034 
0035     if (!QDir(configPath).mkpath(QStringLiteral("."))) {
0036         qFatal("Failed to create test configuration directory.");
0037     }
0038 
0039     QFile::remove(configPath);
0040 
0041     QIcon::setThemeSearchPaths(QStringList() << QFINDTESTDATA("data/icons"));
0042 
0043     KConfigGroup plasmaConfig(KSharedConfig::openConfig("plasmarc"), "Theme");
0044     plasmaConfig.writeEntry("name", "default");
0045 
0046     KIconTheme::forceThemeForTests("test-theme");
0047     KSharedConfig::openConfig()->reparseConfiguration();
0048     KIconTheme::reconfigure();
0049     KIconLoader::global()->reconfigure(QString());
0050 }
0051 
0052 void ThemeTest::testThemeConfig_data()
0053 {
0054     QTest::addColumn<QString>("themeName");
0055 
0056     QTest::addRow("new metadata style theme") << QStringLiteral("testtheme");
0057 }
0058 
0059 void ThemeTest::testThemeConfig()
0060 {
0061     QFETCH(QString, themeName);
0062     auto theme = std::make_unique<Plasma::Theme>(themeName, this);
0063     QCOMPARE(theme->backgroundContrastEnabled(), true);
0064     QCOMPARE(theme->backgroundContrast(), 0.23);
0065 }
0066 
0067 void ThemeTest::testColors()
0068 {
0069     QCOMPARE(m_theme->color(Plasma::Theme::TextColor, Plasma::Theme::NormalColorGroup), QColor(49, 54, 59));
0070     QCOMPARE(m_theme->color(Plasma::Theme::BackgroundColor, Plasma::Theme::NormalColorGroup), QColor(239, 240, 241));
0071     QCOMPARE(m_theme->color(Plasma::Theme::HighlightColor, Plasma::Theme::NormalColorGroup), QColor(61, 174, 230));
0072     QCOMPARE(m_theme->color(Plasma::Theme::HoverColor, Plasma::Theme::NormalColorGroup), QColor(61, 174, 230));
0073     QCOMPARE(m_theme->color(Plasma::Theme::FocusColor, Plasma::Theme::NormalColorGroup), QColor(30, 146, 255));
0074     QCOMPARE(m_theme->color(Plasma::Theme::LinkColor, Plasma::Theme::NormalColorGroup), QColor(61, 174, 230));
0075     QCOMPARE(m_theme->color(Plasma::Theme::VisitedLinkColor, Plasma::Theme::NormalColorGroup), QColor(61, 174, 230));
0076     QCOMPARE(m_theme->color(Plasma::Theme::HighlightedTextColor, Plasma::Theme::NormalColorGroup), QColor(252, 252, 252));
0077     QCOMPARE(m_theme->color(Plasma::Theme::PositiveTextColor, Plasma::Theme::NormalColorGroup), QColor(17, 209, 22));
0078     QCOMPARE(m_theme->color(Plasma::Theme::NeutralTextColor, Plasma::Theme::NormalColorGroup), QColor(201, 206, 59));
0079     QCOMPARE(m_theme->color(Plasma::Theme::NegativeTextColor, Plasma::Theme::NormalColorGroup), QColor(237, 21, 21));
0080 
0081     QCOMPARE(m_theme->color(Plasma::Theme::TextColor, Plasma::Theme::ButtonColorGroup), QColor(49, 54, 59));
0082     QCOMPARE(m_theme->color(Plasma::Theme::BackgroundColor, Plasma::Theme::ButtonColorGroup), QColor(239, 240, 241));
0083     QCOMPARE(m_theme->color(Plasma::Theme::HighlightColor, Plasma::Theme::ButtonColorGroup), QColor(61, 174, 230));
0084     QCOMPARE(m_theme->color(Plasma::Theme::HoverColor, Plasma::Theme::ButtonColorGroup), QColor(61, 174, 230));
0085     QCOMPARE(m_theme->color(Plasma::Theme::FocusColor, Plasma::Theme::ButtonColorGroup), QColor(30, 146, 255));
0086     QCOMPARE(m_theme->color(Plasma::Theme::LinkColor, Plasma::Theme::ButtonColorGroup), QColor(61, 174, 230));
0087     QCOMPARE(m_theme->color(Plasma::Theme::VisitedLinkColor, Plasma::Theme::ButtonColorGroup), QColor(61, 174, 230));
0088     QCOMPARE(m_theme->color(Plasma::Theme::HighlightedTextColor, Plasma::Theme::ButtonColorGroup), QColor(252, 252, 252));
0089     QCOMPARE(m_theme->color(Plasma::Theme::PositiveTextColor, Plasma::Theme::ButtonColorGroup), QColor(17, 209, 23));
0090     QCOMPARE(m_theme->color(Plasma::Theme::NeutralTextColor, Plasma::Theme::ButtonColorGroup), QColor(201, 206, 60));
0091     QCOMPARE(m_theme->color(Plasma::Theme::NegativeTextColor, Plasma::Theme::ButtonColorGroup), QColor(237, 21, 22));
0092 
0093     QCOMPARE(m_theme->color(Plasma::Theme::TextColor, Plasma::Theme::ViewColorGroup), QColor(49, 54, 59));
0094     QCOMPARE(m_theme->color(Plasma::Theme::BackgroundColor, Plasma::Theme::ViewColorGroup), QColor(252, 252, 252));
0095     QCOMPARE(m_theme->color(Plasma::Theme::HighlightColor, Plasma::Theme::ViewColorGroup), QColor(61, 174, 230));
0096     QCOMPARE(m_theme->color(Plasma::Theme::HoverColor, Plasma::Theme::ViewColorGroup), QColor(61, 174, 230));
0097     QCOMPARE(m_theme->color(Plasma::Theme::FocusColor, Plasma::Theme::ViewColorGroup), QColor(30, 146, 255));
0098     QCOMPARE(m_theme->color(Plasma::Theme::LinkColor, Plasma::Theme::ViewColorGroup), QColor(61, 174, 230));
0099     QCOMPARE(m_theme->color(Plasma::Theme::VisitedLinkColor, Plasma::Theme::ViewColorGroup), QColor(61, 174, 230));
0100     QCOMPARE(m_theme->color(Plasma::Theme::HighlightedTextColor, Plasma::Theme::ViewColorGroup), QColor(252, 252, 252));
0101     QCOMPARE(m_theme->color(Plasma::Theme::PositiveTextColor, Plasma::Theme::ViewColorGroup), QColor(17, 209, 24));
0102     QCOMPARE(m_theme->color(Plasma::Theme::NeutralTextColor, Plasma::Theme::ViewColorGroup), QColor(201, 206, 61));
0103     QCOMPARE(m_theme->color(Plasma::Theme::NegativeTextColor, Plasma::Theme::ViewColorGroup), QColor(237, 21, 23));
0104 
0105     QCOMPARE(m_theme->color(Plasma::Theme::TextColor, Plasma::Theme::ComplementaryColorGroup), QColor(239, 240, 241));
0106     QCOMPARE(m_theme->color(Plasma::Theme::BackgroundColor, Plasma::Theme::ComplementaryColorGroup), QColor(49, 54, 59));
0107     QCOMPARE(m_theme->color(Plasma::Theme::HighlightColor, Plasma::Theme::ComplementaryColorGroup), QColor(61, 174, 230));
0108     QCOMPARE(m_theme->color(Plasma::Theme::HoverColor, Plasma::Theme::ComplementaryColorGroup), QColor(71, 174, 230));
0109     QCOMPARE(m_theme->color(Plasma::Theme::FocusColor, Plasma::Theme::ComplementaryColorGroup), QColor(40, 146, 255));
0110     QCOMPARE(m_theme->color(Plasma::Theme::LinkColor, Plasma::Theme::ComplementaryColorGroup), QColor(71, 174, 230));
0111     QCOMPARE(m_theme->color(Plasma::Theme::VisitedLinkColor, Plasma::Theme::ComplementaryColorGroup), QColor(71, 174, 230));
0112     QCOMPARE(m_theme->color(Plasma::Theme::HighlightedTextColor, Plasma::Theme::ComplementaryColorGroup), QColor(252, 252, 252));
0113     QCOMPARE(m_theme->color(Plasma::Theme::PositiveTextColor, Plasma::Theme::ComplementaryColorGroup), QColor(17, 209, 25));
0114     QCOMPARE(m_theme->color(Plasma::Theme::NeutralTextColor, Plasma::Theme::ComplementaryColorGroup), QColor(201, 206, 62));
0115     QCOMPARE(m_theme->color(Plasma::Theme::NegativeTextColor, Plasma::Theme::ComplementaryColorGroup), QColor(237, 21, 24));
0116 }
0117 
0118 void ThemeTest::testCompositingChange()
0119 {
0120     // this test simulates the compositing change on X11
0121 #if HAVE_X11
0122     if (!KWindowSystem::isPlatformX11()) {
0123         QSKIP("Test is only for X11");
0124     }
0125     QVERIFY(!KX11Extras::compositingActive());
0126 
0127     // image path should give us an opaque variant
0128     QVERIFY(m_theme->imagePath(QStringLiteral("element")).endsWith(QLatin1String("/desktoptheme/testtheme/opaque/element.svg")));
0129 
0130     QSignalSpy themeChangedSpy(m_theme, &Plasma::Theme::themeChanged);
0131     QVERIFY(themeChangedSpy.isValid());
0132 
0133     // fake the compositor
0134     QSignalSpy compositingChangedSpy(KX11Extras::self(), &KX11Extras::compositingChanged);
0135     QVERIFY(compositingChangedSpy.isValid());
0136     std::unique_ptr<KSelectionOwner> compositorSelection(new KSelectionOwner("_NET_WM_CM_S0"));
0137     QSignalSpy claimedSpy(compositorSelection.get(), &KSelectionOwner::claimedOwnership);
0138     QVERIFY(claimedSpy.isValid());
0139     compositorSelection->claim(true);
0140     QVERIFY(claimedSpy.wait());
0141 
0142     QCOMPARE(compositingChangedSpy.count(), 1);
0143     QVERIFY(KX11Extras::compositingActive());
0144     QVERIFY(themeChangedSpy.wait());
0145     QCOMPARE(themeChangedSpy.count(), 1);
0146     QVERIFY(m_theme->imagePath(QStringLiteral("element")).endsWith(QLatin1String("/desktoptheme/testtheme/element.svg")));
0147 
0148     // remove compositor again
0149     compositorSelection.reset();
0150     QVERIFY(compositingChangedSpy.wait());
0151     QCOMPARE(compositingChangedSpy.count(), 2);
0152     QVERIFY(!KX11Extras::compositingActive());
0153     QVERIFY(themeChangedSpy.wait());
0154     QCOMPARE(themeChangedSpy.count(), 2);
0155     QVERIFY(m_theme->imagePath(QStringLiteral("element")).endsWith(QLatin1String("/desktoptheme/testtheme/opaque/element.svg")));
0156 #endif
0157 }
0158 
0159 QTEST_MAIN(ThemeTest)
0160 
0161 #include "moc_themetest.cpp"