File indexing completed on 2024-04-21 03:54:30

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2015 Christoph Cullmann <cullmann@kde.org>
0004     SPDX-FileCopyrightText: 2016 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include <kiconloader.h>
0010 #include <kicontheme.h>
0011 
0012 #include <QDir>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 
0016 #include <KSharedConfig>
0017 
0018 // Install icontheme.rcc where KIconThemes will find it.
0019 // This must be done before QCoreApplication is even created, given the Q_COREAPP_STARTUP_FUNCTION in kiconthemes
0020 void earlyInit()
0021 {
0022     QStandardPaths::setTestModeEnabled(true);
0023     qputenv("XDG_DATA_DIRS", "/doesnotexist"); // ensure hicolor/oxygen/breeze are not found
0024     QFile rcc(QStringLiteral("icontheme.rcc"));
0025     Q_ASSERT(rcc.exists());
0026     QCoreApplication::setApplicationName(QStringLiteral("myappname")); // for a fixed location on Unix (appname is empty here otherwise)
0027     const QString destDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
0028     QDir().mkpath(destDir);
0029     const QString dest = destDir + QStringLiteral("/icontheme.rcc");
0030     QFile::remove(dest);
0031     if (!rcc.copy(dest)) {
0032         qWarning() << "Error copying to" << dest;
0033     }
0034 }
0035 Q_CONSTRUCTOR_FUNCTION(earlyInit)
0036 
0037 class KIconLoader_RCCThemeTest : public QObject
0038 {
0039     Q_OBJECT
0040 
0041 private Q_SLOTS:
0042     void initTestCase()
0043     {
0044         // Remove icon cache
0045         const QString cacheFile = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/icon-cache.kcache");
0046         QFile::remove(cacheFile);
0047 
0048         // Clear SHM cache
0049         KIconLoader::global()->reconfigure(QString());
0050     }
0051 
0052     void testThemeName()
0053     {
0054         QCOMPARE(QIcon::themeName(), m_internalThemeName);
0055     }
0056 
0057     void testQIconFromTheme()
0058     {
0059         // load icon with Qt API
0060         QVERIFY(!QIcon::fromTheme(QStringLiteral("someiconintheme")).isNull());
0061     }
0062 
0063     void testKIconLoader()
0064     {
0065         // Check that direct usage of KIconLoader (e.g. from KToolBar) works
0066         QVERIFY(KIconLoader::global()->theme());
0067         QCOMPARE(KIconLoader::global()->theme()->internalName(), m_internalThemeName);
0068 
0069         // load icon with KIconLoader API (unlikely to happen in reality)
0070         QString path;
0071         KIconLoader::global()->loadIcon(QStringLiteral("someiconintheme"), KIconLoader::Desktop, 22, KIconLoader::DefaultState, QStringList(), &path);
0072         QCOMPARE(path, QString(QStringLiteral(":/icons/") + m_internalThemeName + QStringLiteral("/22x22/appsNoContext/someiconintheme.png")));
0073     }
0074 
0075 private:
0076     const QString m_internalThemeName = QStringLiteral("kf6_rcc_theme");
0077 };
0078 
0079 QTEST_MAIN(KIconLoader_RCCThemeTest)
0080 
0081 #include "kiconloader_rcctest.moc"