File indexing completed on 2025-01-05 04:49:43

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "grantleeheaderstyleplugintest.h"
0008 #include "../grantleeheaderstyleplugin/grantleeheaderstyleinterface.h"
0009 #include "../grantleeheaderstyleplugin/grantleeheaderstyleplugin.h"
0010 #include "utils.h"
0011 
0012 #include <GrantleeTheme/GrantleeThemeManager>
0013 #include <MessageViewer/HeaderStyle>
0014 #include <MimeTreeParser/NodeHelper>
0015 
0016 #include <QActionGroup>
0017 #include <QStandardPaths>
0018 #include <QTest>
0019 
0020 #include <KActionCollection>
0021 #include <KActionMenu>
0022 
0023 GrantleeHeaderStylePluginTest::GrantleeHeaderStylePluginTest(QObject *parent)
0024     : QObject(parent)
0025 {
0026 }
0027 
0028 GrantleeHeaderStylePluginTest::~GrantleeHeaderStylePluginTest() = default;
0029 
0030 void GrantleeHeaderStylePluginTest::initTestCase()
0031 {
0032     QStandardPaths::setTestModeEnabled(true);
0033     qputenv("LC_ALL", "C");
0034     expectedDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0035     expectedDataLocation += QStringLiteral("/messageviewer/themes");
0036     QDir targetDir(expectedDataLocation);
0037     QDir sourceDir(QStringLiteral(GRANTLEETHEME_DATA_DIR));
0038     const auto themeDirs = sourceDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
0039 
0040     if (targetDir.exists()) {
0041         QVERIFY(targetDir.removeRecursively()); // Start with a fresh copy
0042     }
0043 
0044     for (const auto &themeDir : themeDirs) {
0045         const QString &dirName = targetDir.filePath(themeDir.fileName());
0046         QVERIFY(targetDir.mkpath(themeDir.fileName()));
0047         const auto files = QDir(themeDir.absoluteFilePath()).entryInfoList(QDir::Files | QDir::Readable | QDir::NoSymLinks);
0048         qDebug() << dirName << files;
0049         for (const auto &file : files) {
0050             const QString &newPath = dirName + QLatin1Char('/') + file.fileName();
0051             qDebug() << file << newPath;
0052             QVERIFY(QFile(file.absoluteFilePath()).copy(newPath));
0053         }
0054     }
0055 }
0056 
0057 void GrantleeHeaderStylePluginTest::cleanupTestCase()
0058 {
0059     QDir targetDir(expectedDataLocation);
0060 
0061     if (targetDir.exists()) {
0062         QVERIFY(targetDir.removeRecursively()); // Start with a fresh copy
0063     }
0064 }
0065 
0066 void GrantleeHeaderStylePluginTest::shouldHaveDefaultValue()
0067 {
0068     MessageViewer::GrantleeHeaderStylePlugin plugin;
0069     QVERIFY(plugin.headerStyle());
0070     QVERIFY(plugin.headerStrategy());
0071 }
0072 
0073 void GrantleeHeaderStylePluginTest::shouldCreateInterface()
0074 {
0075     MessageViewer::GrantleeHeaderStylePlugin plugin;
0076     auto menu = new KActionMenu(this);
0077     auto act = new QActionGroup(this);
0078 
0079     MessageViewer::HeaderStyleInterface *interface = plugin.createView(menu, act, new KActionCollection(this));
0080     QVERIFY(interface);
0081     // QVERIFY(!interface->action().isEmpty());
0082 }
0083 
0084 void GrantleeHeaderStylePluginTest::testThemeActivation_data()
0085 {
0086     QTest::addColumn<QString>("themeName");
0087 
0088     QDir dir(QStringLiteral(GRANTLEETHEME_DATA_DIR));
0089     const auto themeDirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
0090 
0091     for (const QFileInfo &t : themeDirs) {
0092         QTest::newRow(t.fileName().toLatin1().constData()) << t.fileName();
0093     }
0094 }
0095 
0096 void GrantleeHeaderStylePluginTest::testThemeActivation()
0097 {
0098     QFETCH(QString, themeName);
0099 
0100     MessageViewer::GrantleeHeaderStylePlugin plugin;
0101     auto menu = new KActionMenu(this);
0102     auto act = new QActionGroup(this);
0103 
0104     MessageViewer::GrantleeHeaderStyleInterface *interface =
0105         static_cast<MessageViewer::GrantleeHeaderStyleInterface *>(plugin.createView(menu, act, new KActionCollection(this)));
0106     QVERIFY(interface);
0107 
0108     QVERIFY(interface->mThemeManager->themes().contains(themeName));
0109 
0110     const auto actions = act->actions();
0111     for (const auto &action : actions) {
0112         if (action->data() == themeName) {
0113             action->trigger();
0114             break;
0115         }
0116     }
0117 
0118     QCOMPARE(plugin.headerStyle()->theme().dirName(), themeName);
0119 }
0120 
0121 void GrantleeHeaderStylePluginTest::testThemeRender_data()
0122 {
0123     QTest::addColumn<QString>("themeName");
0124     QTest::addColumn<QString>("mailFileName");
0125 
0126     QDir dir(QStringLiteral(HEADER_DATA_DIR));
0127     const auto themeDirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
0128     const auto l = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0129 
0130     for (const QFileInfo &t : themeDirs) {
0131         const QDir themeDir(t.absoluteFilePath());
0132         for (const QString &file : l) {
0133             if (!themeDir.exists(file + QStringLiteral(".html"))) {
0134                 continue;
0135             }
0136             QTest::newRow(QString(themeDir.dirName() + QStringLiteral("-") + file).toLatin1().constData()) << themeDir.dirName() << file;
0137         }
0138     }
0139 }
0140 
0141 void GrantleeHeaderStylePluginTest::testThemeRender()
0142 {
0143     QFETCH(QString, themeName);
0144     QFETCH(QString, mailFileName);
0145 
0146     auto aMsg = readAndParseMail(mailFileName);
0147     MessageViewer::GrantleeHeaderStylePlugin plugin;
0148     auto style = plugin.headerStyle();
0149     MimeTreeParser::NodeHelper nodeHelper;
0150     style->setNodeHelper(&nodeHelper);
0151     auto menu = new KActionMenu(this);
0152     auto act = new QActionGroup(this);
0153 
0154     MessageViewer::GrantleeHeaderStyleInterface *interface =
0155         static_cast<MessageViewer::GrantleeHeaderStyleInterface *>(plugin.createView(menu, act, new KActionCollection(this)));
0156     QVERIFY(interface);
0157 
0158     QVERIFY(interface->mThemeManager->themes().contains(themeName));
0159 
0160     const auto themes = interface->mThemeManager->themes();
0161     for (const auto &theme : themes) {
0162         if (theme.dirName() != themeName) {
0163             continue;
0164         }
0165         if (theme.absolutePath().startsWith(expectedDataLocation)) {
0166             style->setTheme(theme);
0167             break;
0168         }
0169     }
0170 
0171     QCOMPARE(style->theme().dirName(), themeName);
0172     // Make sure, that we do not load the same file from a different location.
0173     QVERIFY(style->theme().absolutePath().startsWith(expectedDataLocation));
0174 
0175     const QString data = style->format(aMsg.data());
0176     testHeaderFile(data, mailFileName, themeName);
0177 }
0178 
0179 QTEST_MAIN(GrantleeHeaderStylePluginTest)
0180 
0181 #include "moc_grantleeheaderstyleplugintest.cpp"