2020-08-13 21:08:54 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: LGPL-2.0-or-later
|
|
|
|
*/
|
2016-04-01 15:06:31 +02:00
|
|
|
|
|
|
|
#include "themetest.h"
|
2021-01-07 16:11:16 +01:00
|
|
|
#include "../src/plasma/private/svg_p.h"
|
2016-04-01 15:06:31 +02:00
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QApplication>
|
2019-12-14 16:58:05 +01:00
|
|
|
#include <QSignalSpy>
|
2016-04-01 15:06:31 +02:00
|
|
|
|
|
|
|
#include <KIconLoader>
|
|
|
|
#include <KIconTheme>
|
2016-08-02 10:08:26 +02:00
|
|
|
#include <KWindowSystem>
|
|
|
|
|
|
|
|
#include <config-plasma.h>
|
|
|
|
#if HAVE_X11
|
|
|
|
#include <KSelectionOwner>
|
|
|
|
#endif
|
2021-01-08 13:58:05 +01:00
|
|
|
#include <array>
|
2016-04-01 15:06:31 +02:00
|
|
|
|
2021-01-07 16:11:16 +01:00
|
|
|
QString cacheIdHash(const Plasma::SvgPrivate::CacheId &id)
|
|
|
|
{
|
|
|
|
static const uint seed = 0x9e3779b9;
|
|
|
|
std::array<uint, 10> parts = {
|
|
|
|
::qHash(id.width),
|
|
|
|
::qHash(id.height),
|
|
|
|
::qHash(id.elementName),
|
|
|
|
::qHash(id.filePath),
|
|
|
|
::qHash(id.status),
|
|
|
|
::qHash(id.devicePixelRatio),
|
|
|
|
::qHash(id.scaleFactor),
|
|
|
|
::qHash(id.colorGroup),
|
|
|
|
::qHash(id.extraFlags),
|
|
|
|
::qHash(id.lastModified)
|
|
|
|
};
|
|
|
|
return QString::number(qHashRange(parts.begin(), parts.end(), seed));
|
|
|
|
}
|
2016-04-01 15:06:31 +02:00
|
|
|
|
|
|
|
void ThemeTest::initTestCase()
|
|
|
|
{
|
|
|
|
// make our theme in search path
|
|
|
|
qputenv("XDG_DATA_DIRS",
|
|
|
|
qgetenv("XDG_DATA_DIRS") + ":" + QFINDTESTDATA("data").toLocal8Bit());
|
|
|
|
|
|
|
|
// set default icon theme to test-theme
|
|
|
|
QStandardPaths::setTestModeEnabled(true);
|
|
|
|
|
2016-04-13 10:43:27 +02:00
|
|
|
m_theme = new Plasma::Theme("testtheme", this);
|
2016-04-01 15:06:31 +02:00
|
|
|
QString configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
|
|
|
|
|
|
|
|
if(!QDir(configPath).mkpath(QStringLiteral("."))) {
|
|
|
|
qFatal("Failed to create test configuration directory.");
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile::remove(configPath);
|
|
|
|
|
|
|
|
QIcon::setThemeSearchPaths(QStringList()<<QFINDTESTDATA("data/icons"));
|
|
|
|
|
|
|
|
KConfigGroup plasmaConfig(KSharedConfig::openConfig("plasmarc"), "Theme");
|
|
|
|
plasmaConfig.writeEntry("name", "default");
|
|
|
|
m_svg = new Plasma::Svg();
|
2016-04-27 13:25:59 +02:00
|
|
|
|
2016-05-18 13:50:11 +02:00
|
|
|
KIconTheme::forceThemeForTests("test-theme");
|
2016-04-27 13:25:59 +02:00
|
|
|
KSharedConfig::openConfig()->reparseConfiguration();
|
|
|
|
KIconTheme::reconfigure();
|
|
|
|
KIconLoader::global()->reconfigure(QString());
|
2016-04-01 15:06:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThemeTest::cleanupTestCase()
|
|
|
|
{
|
|
|
|
// m_testIconsDir.removeRecursively();
|
|
|
|
delete m_svg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThemeTest::loadSvgIcon()
|
|
|
|
{
|
|
|
|
const auto *iconTheme = KIconLoader::global()->theme();
|
|
|
|
QString iconPath;
|
|
|
|
if (iconTheme) {
|
|
|
|
iconPath = iconTheme->iconPath(QLatin1String("konversation.svg"), 48, KIconLoader::MatchBest);
|
|
|
|
}
|
|
|
|
|
2016-05-16 12:45:23 +02:00
|
|
|
QVERIFY(iconTheme);
|
|
|
|
QVERIFY(iconTheme->isValid());
|
|
|
|
QVERIFY2(QFile::exists(iconPath), qPrintable(iconPath));
|
2016-04-01 15:06:31 +02:00
|
|
|
m_svg->setImagePath(iconPath);
|
|
|
|
QVERIFY(m_svg->isValid());
|
|
|
|
|
|
|
|
m_svg->pixmap(); //trigger the SVG being loaded
|
|
|
|
|
2020-09-30 13:10:56 +02:00
|
|
|
QFileInfo info(iconPath);
|
|
|
|
|
2021-01-08 22:13:06 +01:00
|
|
|
QString cacheId = cacheIdHash(Plasma::SvgPrivate::CacheId{48, 48, iconPath, QString(), m_svg->status(), m_svg->devicePixelRatio(), m_svg->scaleFactor(), m_svg->colorGroup(), 0, static_cast<uint>(info.lastModified().toSecsSinceEpoch())});
|
2021-01-07 16:11:16 +01:00
|
|
|
|
2016-04-01 15:06:31 +02:00
|
|
|
QPixmap result;
|
2020-09-30 13:10:56 +02:00
|
|
|
QVERIFY(m_svg->theme()->findInCache(cacheId, result, info.lastModified().toSecsSinceEpoch()));
|
2016-04-01 15:06:31 +02:00
|
|
|
|
|
|
|
QSignalSpy spy(m_svg, SIGNAL(repaintNeeded()));
|
|
|
|
QVERIFY(spy.isValid());
|
|
|
|
|
2016-05-18 13:50:11 +02:00
|
|
|
KIconTheme::forceThemeForTests("test-theme-two");
|
2016-04-01 15:06:31 +02:00
|
|
|
// KIconloader needs changesto be emitted manually, ouch.
|
|
|
|
for (int i=0; i < KIconLoader::LastGroup; i++) {
|
|
|
|
KIconLoader::emitChange(KIconLoader::Group(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
spy.wait();
|
|
|
|
//Svg emitting repaintNeeded when something changes in KIconLoader means the svg loaded an icon
|
|
|
|
QVERIFY(spy.count() == 1);
|
|
|
|
|
|
|
|
QVERIFY(!m_svg->theme()->findInCache(cacheId, result));
|
|
|
|
}
|
|
|
|
|
2016-04-13 10:43:27 +02:00
|
|
|
void ThemeTest::testColors()
|
|
|
|
{
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::TextColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(49,54,59));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::BackgroundColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(239,240,241));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HighlightColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HoverColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::FocusColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(30,146,255));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::LinkColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::VisitedLinkColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HighlightedTextColor,
|
2016-05-05 12:21:03 +02:00
|
|
|
Plasma::Theme::NormalColorGroup), QColor(252,252,252));
|
2016-04-20 13:05:24 +02:00
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::PositiveTextColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(17,209,22));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::NeutralTextColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(201,206,59));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::NegativeTextColor,
|
|
|
|
Plasma::Theme::NormalColorGroup), QColor(237,21,21));
|
2016-04-13 10:43:27 +02:00
|
|
|
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::TextColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(49,54,59));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::BackgroundColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(239,240,241));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HighlightColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HoverColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::FocusColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(30,146,255));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::LinkColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::VisitedLinkColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HighlightedTextColor,
|
2016-05-05 12:21:03 +02:00
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(252,252,252));
|
2016-04-20 13:05:24 +02:00
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::PositiveTextColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(17,209,23));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::NeutralTextColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(201,206,60));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::NegativeTextColor,
|
|
|
|
Plasma::Theme::ButtonColorGroup), QColor(237,21,22));
|
2016-04-13 10:43:27 +02:00
|
|
|
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::TextColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(49,54,59));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::BackgroundColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(252,252,252));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HighlightColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HoverColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::FocusColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(30,146,255));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::LinkColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::VisitedLinkColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(61,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HighlightedTextColor,
|
2016-05-05 12:21:03 +02:00
|
|
|
Plasma::Theme::ViewColorGroup), QColor(252,252,252));
|
2016-04-20 13:05:24 +02:00
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::PositiveTextColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(17,209,24));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::NeutralTextColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(201,206,61));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::NegativeTextColor,
|
|
|
|
Plasma::Theme::ViewColorGroup), QColor(237,21,23));
|
2016-04-13 10:43:27 +02:00
|
|
|
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::TextColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(239,240,241));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::BackgroundColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(49,54,59));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HighlightColor,
|
2016-05-05 12:21:03 +02:00
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(61,174,230));
|
2016-04-13 10:43:27 +02:00
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HoverColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(71,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::FocusColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(40,146,255));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::LinkColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(71,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::VisitedLinkColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(71,174,230));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::HighlightedTextColor,
|
2016-05-05 12:21:03 +02:00
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(252,252,252));
|
2016-04-20 13:05:24 +02:00
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::PositiveTextColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(17,209,25));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::NeutralTextColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(201,206,62));
|
|
|
|
QCOMPARE(m_theme->color(Plasma::Theme::NegativeTextColor,
|
|
|
|
Plasma::Theme::ComplementaryColorGroup), QColor(237,21,24));
|
2016-04-13 10:43:27 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 10:08:26 +02:00
|
|
|
void ThemeTest::testCompositingChange()
|
|
|
|
{
|
|
|
|
// this test simulates the compositing change on X11
|
|
|
|
#if HAVE_X11
|
|
|
|
if (!KWindowSystem::isPlatformX11()) {
|
|
|
|
QSKIP("Test is only for X11");
|
|
|
|
}
|
|
|
|
QVERIFY(!KWindowSystem::compositingActive());
|
|
|
|
|
|
|
|
// image path should give us an opaque variant
|
|
|
|
QVERIFY(m_theme->imagePath(QStringLiteral("element")).endsWith(QLatin1String("/desktoptheme/testtheme/opaque/element.svg")));
|
|
|
|
|
|
|
|
QSignalSpy themeChangedSpy(m_theme, &Plasma::Theme::themeChanged);
|
|
|
|
QVERIFY(themeChangedSpy.isValid());
|
|
|
|
|
|
|
|
// fake the compositor
|
|
|
|
QSignalSpy compositingChangedSpy(KWindowSystem::self(), &KWindowSystem::compositingChanged);
|
|
|
|
QVERIFY(compositingChangedSpy.isValid());
|
|
|
|
QScopedPointer<KSelectionOwner> compositorSelection(new KSelectionOwner("_NET_WM_CM_S0"));
|
|
|
|
QSignalSpy claimedSpy(compositorSelection.data(), &KSelectionOwner::claimedOwnership);
|
|
|
|
QVERIFY(claimedSpy.isValid());
|
|
|
|
compositorSelection->claim(true);
|
|
|
|
QVERIFY(claimedSpy.wait());
|
|
|
|
|
|
|
|
QCOMPARE(compositingChangedSpy.count(), 1);
|
|
|
|
QVERIFY(KWindowSystem::compositingActive());
|
|
|
|
QVERIFY(themeChangedSpy.wait());
|
|
|
|
QCOMPARE(themeChangedSpy.count(), 1);
|
|
|
|
QVERIFY(m_theme->imagePath(QStringLiteral("element")).endsWith(QLatin1String("/desktoptheme/testtheme/element.svg")));
|
|
|
|
|
|
|
|
// remove compositor again
|
|
|
|
compositorSelection.reset();
|
|
|
|
QVERIFY(compositingChangedSpy.wait());
|
|
|
|
QCOMPARE(compositingChangedSpy.count(), 2);
|
|
|
|
QVERIFY(!KWindowSystem::compositingActive());
|
|
|
|
QVERIFY(themeChangedSpy.wait());
|
|
|
|
QCOMPARE(themeChangedSpy.count(), 2);
|
|
|
|
QVERIFY(m_theme->imagePath(QStringLiteral("element")).endsWith(QLatin1String("/desktoptheme/testtheme/opaque/element.svg")));
|
|
|
|
#endif
|
|
|
|
}
|
2016-04-13 10:43:27 +02:00
|
|
|
|
2016-04-01 15:06:31 +02:00
|
|
|
QTEST_MAIN(ThemeTest)
|
|
|
|
|