// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: BSD-2-Clause // // The themes the settings page offers: pictures in a folder read off the disk // rather than a list written in QML. Nothing opens a window; the one thing // asked of a picture is its tone. #include "core/theme/ThemeLibrary.h" #include "PictureFixture.h" #include #include #include #include #include #include namespace { bool makePicture(const QString &dir, const QString &fileName) { QFile file(QDir(dir).absoluteFilePath(fileName)); return file.open(QIODevice::WriteOnly) && file.write("x") > 0; } // The contrast ratio of two colours, which is the one number a surface colour // is really judged by: the text on it is the scheme's own text colour, so this // is what says whether the tone can be seen and the text still read at the same // time. WCAG 2.1's definition, and the 7.0 it calls AAA for body text. double contrastOf(const QColor &a, const QColor &b) { const auto luminance = [](const QColor &c) { const auto channel = [&c](int value) { const double scaled = value / 255.0; return scaled <= 0.04045 ? scaled / 12.92 : std::pow((scaled + 0.055) / 1.055, 2.4); }; return 0.2126 * channel(c.red()) + 0.7152 * channel(c.green()) + 0.0722 * channel(c.blue()); }; const double one = luminance(a); const double other = luminance(b); return (std::max(one, other) + 0.05) / (std::min(one, other) + 0.05); } } // namespace class ThemeLibraryTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase() { // Test mode moves the data directory out of the user's home, and the // search reads it, so the list cannot be left answering with whatever // pictures the machine running this happens to have. QCoreApplication::setOrganizationDomain(QStringLiteral("koutnet.test")); QStandardPaths::setTestModeEnabled(true); } // The pictures in the folder are the themes, named as the files are. This is // the whole difference between reading a folder and listing the themes in // QML: a picture put there is a theme without anything being edited. void aThemeIsAPictureNamedAfterItsFile() { QTemporaryDir dir; QVERIFY(dir.isValid()); QVERIFY(makePicture(dir.path(), QStringLiteral("cloud-gin.jpg"))); QVERIFY(makePicture(dir.path(), QStringLiteral("village.png"))); QCOMPARE(ThemeLibrary::themesIn(dir.path()), QStringList({QStringLiteral("cloud-gin"), QStringLiteral("village")})); } // Pictures are themes, and GIFs now count as wallpapers too. Non-image // files and folders named like pictures are still ignored. void onlyPicturesAreThemes() { QTemporaryDir dir; QVERIFY(dir.isValid()); QVERIFY(makePicture(dir.path(), QStringLiteral("rain-soon.jpg"))); QVERIFY(makePicture(dir.path(), QStringLiteral("notes.txt"))); QVERIFY(makePicture(dir.path(), QStringLiteral("theme.colors"))); QVERIFY(makePicture(dir.path(), QStringLiteral("folder-preview.gif"))); QVERIFY(QDir(dir.path()).mkdir(QStringLiteral("village.jpg"))); // a folder, named like one QVERIFY(makePicture(dir.path(), QStringLiteral("README"))); QCOMPARE(ThemeLibrary::themesIn(dir.path()), QStringList({QStringLiteral("folder-preview"), QStringLiteral("rain-soon")})); } // Every extension the wallpaper dialog offers is accepted, so a picture that // could have been chosen by hand is not turned away for being in the folder // already. void takesEveryExtensionTheWallpaperDialogTakes() { QTemporaryDir dir; QVERIFY(dir.isValid()); QVERIFY(makePicture(dir.path(), QStringLiteral("a.jpg"))); QVERIFY(makePicture(dir.path(), QStringLiteral("b.jpeg"))); QVERIFY(makePicture(dir.path(), QStringLiteral("c.png"))); QVERIFY(makePicture(dir.path(), QStringLiteral("d.webp"))); QVERIFY(makePicture(dir.path(), QStringLiteral("e.bmp"))); QVERIFY(makePicture(dir.path(), QStringLiteral("f.gif"))); QCOMPARE(ThemeLibrary::themesIn(dir.path()), QStringList({QStringLiteral("a"), QStringLiteral("b"), QStringLiteral("c"), QStringLiteral("d"), QStringLiteral("e"), QStringLiteral("f")})); } // A theme is called by the name of its picture, and a name with a dot in the // middle of it is one name: "a.b.jpg" is a theme called "a.b", not one called // "a" with a second theme lost off the end of it. void theExtensionIsNotPartOfTheName() { QTemporaryDir dir; QVERIFY(dir.isValid()); QVERIFY(makePicture(dir.path(), QStringLiteral("a.b.jpg"))); QCOMPARE(ThemeLibrary::themesIn(dir.path()), QStringList({QStringLiteral("a.b")})); } void aFolderThatIsNotThereHasNoThemesInIt() { QCOMPARE(ThemeLibrary::themesIn(QStringLiteral("/nonexistent/koutnet/themes")), QStringList()); } // The name on the tile is the file's name written the way a name is written: // the dashes and underscores a file has to be spelled with are word breaks to // a reader, and a name is written with a capital. void aNameIsDrawnTheWayANameIsWritten() { QCOMPARE(ThemeLibrary::labelFor(QStringLiteral("cloud-gin")), QStringLiteral("Cloud Gin")); QCOMPARE(ThemeLibrary::labelFor(QStringLiteral("long_road")), QStringLiteral("Long Road")); QCOMPARE(ThemeLibrary::labelFor(QStringLiteral("sepia")), QStringLiteral("Sepia")); // Nothing to capitalize and nothing to turn into words: a name that is one // word is drawn as that word, and an empty one is drawn as nothing rather // than as a space. QCOMPARE(ThemeLibrary::labelFor(QStringLiteral("village")), QStringLiteral("Village")); QCOMPARE(ThemeLibrary::labelFor(QString()), QString()); } // Two pictures whose names come out the same way are still two themes, and a // grid of tiles with two of them saying the same thing is a grid nobody can // choose from. The second one keeps the name its file spells, which is what // tells them apart, and both keep their own identity either way. void twoThemesThatWouldReadAlikeAreToldApart() { QTemporaryDir dir; QVERIFY(dir.isValid()); QVERIFY(makePicture(dir.path(), QStringLiteral("a-b.jpg"))); QVERIFY(makePicture(dir.path(), QStringLiteral("a_b.jpg"))); const QVariantList themes = ThemeLibrary::themesFrom(dir.path()); QCOMPARE(themes.size(), 2); // Which of the two is read first is a collation detail and is not what // this is about: what matters is that one of them is called "A B" and the // other is called what its file spells, so that the two tiles in the // grid are two different tiles. QStringList names; QStringList labels; QStringList paths; for (const QVariant &theme : themes) { names << theme.toMap().value(QStringLiteral("name")).toString(); labels << theme.toMap().value(QStringLiteral("label")).toString(); paths << theme.toMap().value(QStringLiteral("path")).toString(); } std::sort(names.begin(), names.end()); QCOMPARE(names, QStringList({QStringLiteral("a-b"), QStringLiteral("a_b")})); QVERIFY(labels != QStringList({QStringLiteral("A B"), QStringLiteral("A B")})); QCOMPARE(labels.count(QStringLiteral("A B")), 1); // The other one is a tile labelled with what its file spells, which is // the price of two tiles that can be told apart at all. const int other = labels.indexOf(QStringLiteral("A B")) == 0 ? 1 : 0; QVERIFY(QStringList({QStringLiteral("a-b"), QStringLiteral("a_b")}).contains(labels.at(other))); // The paths are still the files' own, which is the whole of a theme. std::sort(paths.begin(), paths.end()); QVERIFY(paths.at(0).endsWith(QStringLiteral("a-b.jpg"))); QVERIFY(paths.at(1).endsWith(QStringLiteral("a_b.jpg"))); } // What a tile is handed has to be enough to draw a picture and to store the // choice: a bare path in an Image inside a component loaded out of a qrc is // resolved as a resource name and comes back with no picture at all. void everyThemeCarriesAPathAndAUrlAnImageCanLoad() { QTemporaryDir dir; QVERIFY(dir.isValid()); QVERIFY(makePicture(dir.path(), QStringLiteral("cloud-gin.jpg"))); const QVariantList themes = ThemeLibrary::themesFrom(dir.path()); QCOMPARE(themes.size(), 1); const QVariantMap theme = themes.first().toMap(); QCOMPARE(theme.value(QStringLiteral("path")).toString(), QDir(dir.path()).absoluteFilePath(QStringLiteral("cloud-gin.jpg"))); QCOMPARE(theme.value(QStringLiteral("url")).toString(), QUrl::fromLocalFile(theme.value(QStringLiteral("path")).toString()).toString()); QVERIFY(theme.value(QStringLiteral("url")).toString().startsWith(QStringLiteral("file:///"))); } // The search takes the first folder that has pictures in it and not the first // folder on the list: the user's own is above the installed one, so a theme // of their own is a theme without a build. void theFirstFolderHoldingPicturesIsTheOneOffered() { const QString userDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/themes"); QVERIFY(QDir().mkpath(userDir)); QVERIFY(makePicture(userDir, QStringLiteral("mine.jpg"))); QCOMPARE(ThemeLibrary::libraryDir(), QDir(userDir).absolutePath()); QCOMPARE(ThemeLibrary::themesIn(ThemeLibrary::libraryDir()), QStringList({QStringLiteral("mine")})); } // A scrim over a picture is drawn in the picture's own tone, so that a // wallpaper is the colour of the window rather than a photograph under a wash // of grey. A blue picture gives a blue scrim. void aScrimOverAPictureIsDrawnInThePictureItsOwnTone() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QString path = dir.filePath(QStringLiteral("cloud-gin.bmp")); QVERIFY(writeTestPicture(path, QColor::fromRgb(0x30, 0x50, 0xa0))); const QColor base(0xfa, 0xfa, 0xfa); const QColor tone = library.toneFor(path, base); QVERIFY(tone.isValid()); // in the picture's hue, which is blue and not a grey of the same value const QColor washed = tone.toHsl(); const int pictureHue = QColor::fromRgb(0x30, 0x50, 0xa0).toHsl().hue(); QVERIFY(washed.hue() >= 200 && washed.hue() <= 260); QCOMPARE(washed.hue(), pictureHue); // and the lightness is untouched: what is washed is a hue and a // saturation, and not a colour put into the surface whole QCOMPARE(washed.lightnessF(), base.lightnessF()); } // A light scheme is where this has least to show, and that is arithmetic: a // background at that lightness is nearly white, and a nearly white surface // cannot hold a saturated colour however the tone is mixed into it. What it // has to keep is the reading - the text is the scheme's, and the text on this // surface has to be read as well as it is read on the scheme's own. Tinting // further means darkening, and darkening costs the text more than the tint // gains, so the lightness stays exactly where the scheme put it. void aLightSchemeKeepsItsOwnReadingAndTintsWhatItCan() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QString path = dir.filePath(QStringLiteral("cloud-gin.bmp")); QVERIFY(writeTestPicture(path, QColor::fromRgb(0x30, 0x50, 0xa0))); const QColor light(0xfa, 0xfa, 0xfa); const QColor surface = library.toneFor(path, light); QCOMPARE(surface.lightnessF(), light.lightnessF()); // the scheme's own text colour, read on the scheme's own background, and // on this one: not worse, which is the whole of what a surface owes it const QColor text(0x23, 0x26, 0x29); QVERIFY(contrastOf(text, surface) >= 7.0); // and it is a colour and not a rounding of the scheme's grey. A tint at a // lightness costs a fraction of a point of contrast, not a band of it, // which is the whole reason the lightness is held and the saturation is // the thing that is given. QVERIFY(surface != light); QCOMPARE(surface.toHsl().hue(), QColor::fromRgb(0x30, 0x50, 0xa0).toHsl().hue()); } // A dark scheme has the room for a tone at its own lightness, and takes it at // exactly that lightness: the surface is the scheme's, tinted, and not a // different shade of the scheme than the one that was asked for. void aDarkSchemeKeepsItsOwnLightness() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QString path = dir.filePath(QStringLiteral("evening-mist.bmp")); QVERIFY(writeTestPicture(path, QColor::fromRgb(0xf8, 0xf0, 0x60))); const QColor dark(0x31, 0x36, 0x3b); const QColor surface = library.toneFor(path, dark); QCOMPARE(surface.lightnessF(), dark.lightnessF()); QVERIFY(contrastOf(QColor(0xef, 0xf0, 0xf1), surface) >= 7.0); } // One picture, one tone, under either scheme: the tone is the picture's and // only the lightness of the surface it is painted on is the scheme's, so the // two schemes differ in how light a surface is and not in what colour it is. void theToneIsThePicturesUnderEitherScheme() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QString path = dir.filePath(QStringLiteral("sunny.bmp")); QVERIFY(writeTestPicture(path, QColor::fromRgb(0xf8, 0xf0, 0x60))); const QColor light(0xfa, 0xfa, 0xfa); const QColor dark(0x31, 0x36, 0x3b); const QColor overLight = library.toneFor(path, light); const QColor overDark = library.toneFor(path, dark); QCOMPARE(overLight.toHsl().hue(), overDark.toHsl().hue()); QVERIFY(overLight != light); // a dark surface has the room to carry the tone, and does QVERIFY(overDark.toHsl().saturation() > 90); } // Neither end of the saturation range: a scrim below a fifth reads as the // grey it replaced, and one above half is a slide rather than a wash over a // photograph. A picture in black and white is washed in the scheme's hue, // because a hue of -1 handed back to a colour is grey again, which is the one // thing this is not. void theWashIsNeitherTheGreyItReplacedNorAColourOfItsOwn() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; // a dark background with the faintest hue in it, the way a colour scheme // has one: a grey has none, and there is nothing to give a grey picture const QColor base(0x31, 0x36, 0x3b); const QString grey = dir.filePath(QStringLiteral("grey.bmp")); QVERIFY(writeTestPicture(grey, QColor(0x80, 0x80, 0x80))); const QColor washedGrey = library.toneFor(grey, base).toHsl(); QVERIFY(washedGrey.isValid()); QVERIFY(std::abs(washedGrey.hue() - base.toHsl().hue()) <= 5); // Enough to read as a colour and not as the grey, give or take what a // round trip through eight bits does to a colour's own saturation. QVERIFY(washedGrey.saturation() > 30); const QString lurid = dir.filePath(QStringLiteral("lurid.bmp")); QVERIFY(writeTestPicture(lurid, QColor::fromRgb(0xff, 0x00, 0xff))); const QColor washedLurid = library.toneFor(lurid, base).toHsl(); QVERIFY(washedLurid.saturation() > 90); QVERIFY(washedLurid.saturation() < 200); } // The accent is the one colour washed for being looked at rather than for // being read against - a selected row, a sent message, a focus ring - so a // picture's hue at a saturation a glance can pick up, at the lightness of the // scheme's own highlight, since an accent picked for its own sake can be // readable on neither the surface nor itself. void anAccentIsThePicturesColourLoudEnoughToBeAnAccent() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QString path = dir.filePath(QStringLiteral("cloud-gin.bmp")); QVERIFY(writeTestPicture(path, QColor::fromRgb(0x30, 0x50, 0xa0))); const QColor base(0x3d, 0xae, 0xe9); // the scheme's own highlight const QColor accent = library.accentFor(path, base); QCOMPARE(accent.toHsl().hue(), QColor::fromRgb(0x30, 0x50, 0xa0).toHsl().hue()); // at least half, which is as little as a colour still reads as one at a // glance: the surface's own twenty percent would be a tint, not an accent QVERIFY(accent.toHsl().saturation() >= 128); QCOMPARE(accent.lightnessF(), base.lightnessF()); } // A picture in black and white has no hue to take and a grey accent is not an // accent, so it takes the scheme's hue at a saturation it is still read at: a // greyscale wallpaper colours the interface like any other and a scheme is // never left without a highlight. void aPictureWithNoHueStillGetsAnAccentAndTheSchemesOwnHue() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QString path = dir.filePath(QStringLiteral("first-snow.bmp")); QVERIFY(writeTestPicture(path, QColor(0x80, 0x80, 0x80))); const QColor base(0x3d, 0xae, 0xe9); const QColor accent = library.accentFor(path, base); QVERIFY(accent != base); QCOMPARE(accent.toHsl().hue(), base.toHsl().hue()); QVERIFY(accent.toHsl().saturation() >= 128); } // The same one answer as a surface: no path, a path that is not there and a // file that is not a picture answer with the scheme's highlight, so an accent // is never an invalid colour to bind to a colour. void anythingThatIsNotAPictureGetsTheSchemesOwnAccent() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QColor base(0x3d, 0xae, 0xe9); const QString notAPicture = dir.filePath(QStringLiteral("notes.txt")); QVERIFY(makePicture(dir.path(), QStringLiteral("notes.txt"))); QCOMPARE(library.accentFor(QString(), base), base); QCOMPARE(library.accentFor(dir.filePath(QStringLiteral("missing.bmp")), base), base); QCOMPARE(library.accentFor(notAPicture, base), base); } // One answer to paint with, whatever the path is: no path, a path that is not // there, and a file that is not a picture all wash with the scheme's own // colour rather than handing back nothing to bind to a colour. void anythingThatIsNotAPictureWashesInTheSchemesOwnColour() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QColor base(0x2a, 0x2a, 0x2a); const QString notAPicture = dir.filePath(QStringLiteral("notes.txt")); QVERIFY(makePicture(dir.path(), QStringLiteral("notes.txt"))); QCOMPARE(library.toneFor(QString(), base), base); QCOMPARE(library.toneFor(dir.filePath(QStringLiteral("missing.bmp")), base), base); QCOMPARE(library.toneFor(notAPicture, base), base); } // A tone is read once and remembered: these are fifteen-megabyte photographs // and the scrim is asked for its colour on every window resize. Replacing a // picture in the folder and pressing Reload has to read the new one, though - // a tone read out of the old file is the tone of a picture that is no longer // there. void aToneIsReadOnceAndTheReloadReadsItAgain() { QTemporaryDir dir; QVERIFY(dir.isValid()); ThemeLibrary library; const QString path = dir.filePath(QStringLiteral("cloud-gin.bmp")); const QColor base(0x2a, 0x2a, 0x2a); QVERIFY(writeTestPicture(path, QColor::fromRgb(0x30, 0x50, 0xa0))); const QColor blue = library.toneFor(path, base); QCOMPARE(library.toneFor(path, base), blue); QVERIFY(writeTestPicture(path, QColor::fromRgb(0xa0, 0x30, 0x30))); QCOMPARE(library.toneFor(path, base), blue); library.reload(); const int hueAfterReload = library.toneFor(path, base).toHsl().hue(); QVERIFY(hueAfterReload >= 340 || hueAfterReload <= 20); } // The folder is shown in the settings and opened from there, so it is a path // without the ".." the candidates are spelled with - and a path even on a // machine with no pictures anywhere, because an empty gallery that says which // folder it would have read is what tells a person where to put the first // picture. void theFolderIsAPathThatCanBeOpenedAndIsOneEvenWithNoPictures() { const QString dir = ThemeLibrary::libraryDir(); QVERIFY(!dir.isEmpty()); QVERIFY(!dir.contains(QStringLiteral(".."))); QVERIFY(dir.startsWith(QLatin1Char('/'))); } }; QTEST_MAIN(ThemeLibraryTest) #include "ThemeLibraryTest.moc"