File: grantleeheaderstyleplugintest.cpp

package info (click to toggle)
kdepim-addons 25.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,776 kB
  • sloc: cpp: 47,568; xml: 282; sh: 114; makefile: 19
file content (191 lines) | stat: -rw-r--r-- 6,247 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
   SPDX-FileCopyrightText: 2015-2025 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "grantleeheaderstyleplugintest.h"
#include "../grantleeheaderstyleplugin/grantleeheaderstyleinterface.h"
#include "../grantleeheaderstyleplugin/grantleeheaderstyleplugin.h"
#include "utils.h"

#include <GrantleeTheme/GrantleeThemeManager>
#include <MessageViewer/HeaderStyle>
#include <MimeTreeParser/NodeHelper>

#include <QActionGroup>
#include <QStandardPaths>
#include <QTest>

#include <KActionCollection>
#include <KActionMenu>

#ifndef Q_OS_WIN
void initLocale()
{
    setenv("LC_ALL", "C", 1);
    setenv("TZ", "UTC", 1);
    QLocale::setDefault(QLocale::c());
}

Q_CONSTRUCTOR_FUNCTION(initLocale)
#endif

GrantleeHeaderStylePluginTest::GrantleeHeaderStylePluginTest(QObject *parent)
    : QObject(parent)
{
}

GrantleeHeaderStylePluginTest::~GrantleeHeaderStylePluginTest() = default;

void GrantleeHeaderStylePluginTest::initTestCase()
{
    QStandardPaths::setTestModeEnabled(true);
    expectedDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
    expectedDataLocation += QStringLiteral("/messageviewer/themes");
    QDir targetDir(expectedDataLocation);
    QDir sourceDir(QStringLiteral(GRANTLEETHEME_DATA_DIR));
    const auto themeDirs = sourceDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);

    if (targetDir.exists()) {
        QVERIFY(targetDir.removeRecursively()); // Start with a fresh copy
    }

    for (const auto &themeDir : themeDirs) {
        const QString &dirName = targetDir.filePath(themeDir.fileName());
        QVERIFY(targetDir.mkpath(themeDir.fileName()));
        const auto files = QDir(themeDir.absoluteFilePath()).entryInfoList(QDir::Files | QDir::Readable | QDir::NoSymLinks);
        qDebug() << dirName << files;
        for (const auto &file : files) {
            const QString &newPath = dirName + QLatin1Char('/') + file.fileName();
            qDebug() << file << newPath;
            QVERIFY(QFile(file.absoluteFilePath()).copy(newPath));
        }
    }
}

void GrantleeHeaderStylePluginTest::cleanupTestCase()
{
    QDir targetDir(expectedDataLocation);

    if (targetDir.exists()) {
        QVERIFY(targetDir.removeRecursively()); // Start with a fresh copy
    }
}

void GrantleeHeaderStylePluginTest::shouldHaveDefaultValue()
{
    MessageViewer::GrantleeHeaderStylePlugin plugin;
    QVERIFY(plugin.headerStyle());
    QVERIFY(plugin.headerStrategy());
}

void GrantleeHeaderStylePluginTest::shouldCreateInterface()
{
    MessageViewer::GrantleeHeaderStylePlugin plugin;
    auto menu = new KActionMenu(this);
    auto act = new QActionGroup(this);

    MessageViewer::HeaderStyleInterface *interface = plugin.createView(menu, act, new KActionCollection(this));
    QVERIFY(interface);
    // QVERIFY(!interface->action().isEmpty());
}

void GrantleeHeaderStylePluginTest::testThemeActivation_data()
{
    QTest::addColumn<QString>("themeName");

    QDir dir(QStringLiteral(GRANTLEETHEME_DATA_DIR));
    const auto themeDirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);

    for (const QFileInfo &t : themeDirs) {
        QTest::newRow(t.fileName().toLatin1().constData()) << t.fileName();
    }
}

void GrantleeHeaderStylePluginTest::testThemeActivation()
{
    QFETCH(QString, themeName);

    MessageViewer::GrantleeHeaderStylePlugin plugin;
    auto menu = new KActionMenu(this);
    auto act = new QActionGroup(this);

    MessageViewer::GrantleeHeaderStyleInterface *interface =
        static_cast<MessageViewer::GrantleeHeaderStyleInterface *>(plugin.createView(menu, act, new KActionCollection(this)));
    QVERIFY(interface);

    QVERIFY(interface->mThemeManager->themes().contains(themeName));

    const auto actions = act->actions();
    for (const auto &action : actions) {
        if (action->data() == themeName) {
            action->trigger();
            break;
        }
    }

    QCOMPARE(plugin.headerStyle()->theme().dirName(), themeName);
}

void GrantleeHeaderStylePluginTest::testThemeRender_data()
{
    QTest::addColumn<QString>("themeName");
    QTest::addColumn<QString>("mailFileName");

    QDir dir(QStringLiteral(HEADER_DATA_DIR));
    const auto themeDirs = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
    const auto l = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);

    for (const QFileInfo &t : themeDirs) {
        const QDir themeDir(t.absoluteFilePath());
        for (const QString &file : l) {
            if (!themeDir.exists(file + QStringLiteral(".html"))) {
                continue;
            }
            QTest::newRow(QString(themeDir.dirName() + QStringLiteral("-") + file).toLatin1().constData()) << themeDir.dirName() << file;
        }
    }
}

void GrantleeHeaderStylePluginTest::testThemeRender()
{
    QFETCH(QString, themeName);
    QFETCH(QString, mailFileName);

    auto aMsg = readAndParseMail(mailFileName);
    MessageViewer::GrantleeHeaderStylePlugin plugin;
    auto style = plugin.headerStyle();
    MimeTreeParser::NodeHelper nodeHelper;
    style->setNodeHelper(&nodeHelper);
    auto menu = new KActionMenu(this);
    auto act = new QActionGroup(this);

    MessageViewer::GrantleeHeaderStyleInterface *interface =
        static_cast<MessageViewer::GrantleeHeaderStyleInterface *>(plugin.createView(menu, act, new KActionCollection(this)));
    QVERIFY(interface);

    QVERIFY(interface->mThemeManager->themes().contains(themeName));

    const auto themes = interface->mThemeManager->themes();
    for (const auto &theme : themes) {
        if (theme.dirName() != themeName) {
            continue;
        }
        if (theme.absolutePath().startsWith(expectedDataLocation)) {
            style->setTheme(theme);
            break;
        }
    }

    QCOMPARE(style->theme().dirName(), themeName);
    // Make sure, that we do not load the same file from a different location.
    QVERIFY(style->theme().absolutePath().startsWith(expectedDataLocation));

    const QString data = style->format(aMsg.data());
    testHeaderFile(data, mailFileName, themeName);
}

QTEST_MAIN(GrantleeHeaderStylePluginTest)

#include "moc_grantleeheaderstyleplugintest.cpp"