diff --git a/examples/messageviewergrantleetheme/themes/example/header.html b/examples/messageviewergrantleetheme/themes/example/header.html --- a/examples/messageviewergrantleetheme/themes/example/header.html +++ b/examples/messageviewergrantleetheme/themes/example/header.html @@ -6,9 +6,9 @@ {% endwith %} {% endwith %} {% endif %} - {% if header.from %} + {% if header.from.isSet %} {% with header.fromi18n as title %} - {% with header.from|safe as value %} + {% with header.from.fullAddress|safe as value %} {% include "header_row.html" %} {% endwith %} {% endwith %} @@ -34,13 +34,12 @@ {% endwith %} {% endwith %} {% endif %} - {% if header.date %} - {% with header.datei18n as title %} - {% with header.dateshort|safe as value %} - {% include "header_row.html" %} - {% endwith %} - {% endwith %} - {% endif %} + + {% with header.datei18n as title %} + {% with header.date.short as value %} + {% include "header_row.html" %} + {% endwith %} + {% endwith %} {% if header.spamHTML %} {{ spamHTML|safe }} diff --git a/examples/messageviewergrantleetheme/themes/example_fancy/header.html b/examples/messageviewergrantleetheme/themes/example_fancy/header.html --- a/examples/messageviewergrantleetheme/themes/example_fancy/header.html +++ b/examples/messageviewergrantleetheme/themes/example_fancy/header.html @@ -11,18 +11,18 @@ {{ header.fromi18n }} - {{ header.from|safe }} + {{ header.from.str}} {% if header.resentfrom %} -   {{ header.resentfromi18n }}: {{ header.resentfrom|safe }} +   {{ header.resentfromi18n }}: {{ header.resentfrom }} {% endif %} {% if vCardName %}   {{ header.vcardi18n }} {% endif %} {% if header.organization %} -   ("{{ header.organization|safe }}") +   ("{{ header.organization}}") {% endif %} @@ -50,7 +50,7 @@ {% if header.Sender %} Sender: - {{ header.Sender|safe }} + {{ header.Sender }} {% endif %} @@ -63,7 +63,7 @@ {{ header.datei18n }} - {{ header.dateshort }} + {{ header.date.short }} @@ -76,7 +76,7 @@ {% if header.photourl %}
- +
{% endif %} diff --git a/plugins/messageviewerheaderplugins/autotests/CMakeLists.txt b/plugins/messageviewerheaderplugins/autotests/CMakeLists.txt --- a/plugins/messageviewerheaderplugins/autotests/CMakeLists.txt +++ b/plugins/messageviewerheaderplugins/autotests/CMakeLists.txt @@ -1,5 +1,6 @@ # convenience macro to add qtest unit tests add_definitions( -DHEADER_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data" ) +add_definitions( -DGRANTLEETHEME_DATA_DIR="${CMAKE_SOURCE_DIR}/examples/messageviewergrantleetheme/themes" ) macro(add_messageviewer_header_unittest _source _additional) set( _test ${_source} utils.cpp ${_additional}) diff --git a/plugins/messageviewerheaderplugins/autotests/data/example/allheaders.mbox.html b/plugins/messageviewerheaderplugins/autotests/data/example/allheaders.mbox.html new file mode 100644 --- /dev/null +++ b/plugins/messageviewerheaderplugins/autotests/data/example/allheaders.mbox.html @@ -0,0 +1,29 @@ + + + + +
+
Subject:ssr/vpn超低价促销
+
+ + From: + + admin <admin@ssrr.link> +
+
To:spam1 <spam1@example.org>, spam2 <spam2@example.org>, spam3 <spam3@example.org>, spam4 <spam4@example.org>, spam5 <spam5@example.org>, spam6 <spam6@example.org>
+
+ + CC: + + foo@exampleorg +
+
+ + BCC: + + bcc@example.org +
+
Date:21 May 2018 16:52:43
+
+ + diff --git a/plugins/messageviewerheaderplugins/autotests/data/example/headertest.mbox.html b/plugins/messageviewerheaderplugins/autotests/data/example/headertest.mbox.html new file mode 100644 --- /dev/null +++ b/plugins/messageviewerheaderplugins/autotests/data/example/headertest.mbox.html @@ -0,0 +1,14 @@ + + + + +
+
Subject:No Subject
+
From:admin <admin@ssrr.link>, from2 <from2@example.org>, from3 <from3@example.org>, from4 <from4@example.org>, from5 <from5@example.org>, from6 <from6@example.org>
+
To:direct recipient1 <tospam1@example.org>, direct recipient2 <tospam2@example.org>, direct recipient3 <tospam3@example.org>, direct recipient4 <tospam4@example.org>, direct recipient5 <tospam5@example.org>, direct recipient6 <tospam6@example.org>
+
CC:copy recipient1 <ccspam1@example.org>, copy recipient2 <ccspam2@example.org>, copy recipient3 <ccspam3@example.org>, copy recipient4 <ccspam4@example.org>, copy recipient5 <ccspam5@example.org>, copy recipient6 <ccspam6@example.org>
+
BCC:blind copy recipient1 <bccspam1@example.org>, blind copy recipient2 <bccspam2@example.org>, blind copy recipient3 <bccspam3@example.org>, blind copy recipient4 <bccspam4@example.org>, blind copy recipient5 <bccspam5@example.org>, blind copy recipient6 <bccspam6@example.org>
+
Date:21 May 2018 16:52:43
+
+ + diff --git a/plugins/messageviewerheaderplugins/autotests/data/example_fancy/allheaders.mbox.html b/plugins/messageviewerheaderplugins/autotests/data/example_fancy/allheaders.mbox.html new file mode 100644 --- /dev/null +++ b/plugins/messageviewerheaderplugins/autotests/data/example_fancy/allheaders.mbox.html @@ -0,0 +1,77 @@ + + + + +
+
ssr/vpn超低价促销
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
From:admin <admin@ssrr.link> +NBSP_ENTITY_PLACEHOLDERNBSP_ENTITY_PLACEHOLDER("Universität KDE")
To: + + + spam1 <spam1@example.org>, spam2 <spam2@example.org>, spam3 <spam3@example.org>, spam4 <spam4@example.org> + spam1 <spam1@example.org>, spam2 <spam2@example.org>, spam3 <spam3@example.org>, spam4 <spam4@example.org>, spam5 <spam5@example.org>, spam6 <spam6@example.org> + +
CC: + foo@exampleorg +
BCC: + bcc@example.org +
Sender: sender <sender@example.org>
List-Id: testlist <testlist@listi.example.org>
Date: 21 May 2018 16:52:43
+
+
+
+
+ +
+
+ + + + + +
+
Spam Status:NBSP_ENTITY_PLACEHOLDERSpamassassin NBSP_ENTITY_PLACEHOLDER
+
+ + +
+ + diff --git a/plugins/messageviewerheaderplugins/autotests/data/example_fancy/headertest.mbox.html b/plugins/messageviewerheaderplugins/autotests/data/example_fancy/headertest.mbox.html new file mode 100644 --- /dev/null +++ b/plugins/messageviewerheaderplugins/autotests/data/example_fancy/headertest.mbox.html @@ -0,0 +1,62 @@ + + + + +
+
No Subject
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
From:admin <admin@ssrr.link>, from2 <from2@example.org>, from3 <from3@example.org>, from4 <from4@example.org>, from5 <from5@example.org>, from6 <from6@example.org>
To: + + + direct recipient1 <tospam1@example.org>, direct recipient2 <tospam2@example.org>, direct recipient3 <tospam3@example.org>, direct recipient4 <tospam4@example.org> + direct recipient1 <tospam1@example.org>, direct recipient2 <tospam2@example.org>, direct recipient3 <tospam3@example.org>, direct recipient4 <tospam4@example.org>, direct recipient5 <tospam5@example.org>, direct recipient6 <tospam6@example.org> + +
CC: + + + copy recipient1 <ccspam1@example.org>, copy recipient2 <ccspam2@example.org>, copy recipient3 <ccspam3@example.org>, copy recipient4 <ccspam4@example.org> + copy recipient1 <ccspam1@example.org>, copy recipient2 <ccspam2@example.org>, copy recipient3 <ccspam3@example.org>, copy recipient4 <ccspam4@example.org>, copy recipient5 <ccspam5@example.org>, copy recipient6 <ccspam6@example.org> + +
BCC: + + + blind copy recipient1 <bccspam1@example.org>, blind copy recipient2 <bccspam2@example.org>, blind copy recipient3 <bccspam3@example.org>, blind copy recipient4 <bccspam4@example.org> + blind copy recipient1 <bccspam1@example.org>, blind copy recipient2 <bccspam2@example.org>, blind copy recipient3 <bccspam3@example.org>, blind copy recipient4 <bccspam4@example.org>, blind copy recipient5 <bccspam5@example.org>, blind copy recipient6 <bccspam6@example.org> + +
Date: 21 May 2018 16:52:43
+
+
+
+
+ + diff --git a/plugins/messageviewerheaderplugins/autotests/grantleeheaderstyleplugintest.h b/plugins/messageviewerheaderplugins/autotests/grantleeheaderstyleplugintest.h --- a/plugins/messageviewerheaderplugins/autotests/grantleeheaderstyleplugintest.h +++ b/plugins/messageviewerheaderplugins/autotests/grantleeheaderstyleplugintest.h @@ -29,9 +29,20 @@ explicit GrantleeHeaderStylePluginTest(QObject *parent = nullptr); ~GrantleeHeaderStylePluginTest(); +public Q_SLOTS: + void initTestCase(); + void cleanupTestCase(); + private Q_SLOTS: void shouldHaveDefaultValue(); void shouldCreateInterface(); + void testThemeActivation_data(); + void testThemeActivation(); + void testThemeRender_data(); + void testThemeRender(); + +private: + QString expectedDataLocation; }; #endif // GRANTLEEHEADERSTYLEPLUGINTEST_H diff --git a/plugins/messageviewerheaderplugins/autotests/grantleeheaderstyleplugintest.cpp b/plugins/messageviewerheaderplugins/autotests/grantleeheaderstyleplugintest.cpp --- a/plugins/messageviewerheaderplugins/autotests/grantleeheaderstyleplugintest.cpp +++ b/plugins/messageviewerheaderplugins/autotests/grantleeheaderstyleplugintest.cpp @@ -18,11 +18,19 @@ */ #include "grantleeheaderstyleplugintest.h" +#include "utils.h" #include "../grantleeheaderstyleplugin/grantleeheaderstyleplugin.h" #include "../grantleeheaderstyleplugin/grantleeheaderstyleinterface.h" + +#include +#include + #include +#include + #include #include + GrantleeHeaderStylePluginTest::GrantleeHeaderStylePluginTest(QObject *parent) : QObject(parent) { @@ -32,6 +40,44 @@ { } +void GrantleeHeaderStylePluginTest::initTestCase() +{ + QStandardPaths::setTestModeEnabled(true); + qputenv("LC_ALL", "C"); + expectedDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); + expectedDataLocation += QDir::separator() + 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 + QDir::separator() + 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; @@ -50,4 +96,93 @@ //QVERIFY(!interface->action().isEmpty()); } +void GrantleeHeaderStylePluginTest::testThemeActivation_data() +{ + QTest::addColumn("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; + KActionMenu *menu = new KActionMenu(this); + QActionGroup *act = new QActionGroup(this); + + MessageViewer::GrantleeHeaderStyleInterface *interface = static_cast(plugin.createView(menu, act, new KActionCollection(this))); + QVERIFY(interface); + + QVERIFY(interface->mThemeManager->themes().contains(themeName)); + + for (const auto &action : act->actions()) { + if (action->data() == themeName) { + action->trigger(); + break; + } + } + + QCOMPARE(plugin.headerStyle()->theme().data(), themeName); +} + +void GrantleeHeaderStylePluginTest::testThemeRender_data() +{ + QTest::addColumn("themeName"); + QTest::addColumn("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(); + KActionMenu *menu = new KActionMenu(this); + QActionGroup *act = new QActionGroup(this); + + MessageViewer::GrantleeHeaderStyleInterface *interface = static_cast(plugin.createView(menu, act, new KActionCollection(this))); + QVERIFY(interface); + + QVERIFY(interface->mThemeManager->themes().contains(themeName)); + + for (const auto &theme : interface->mThemeManager->themes()) { + if (theme.data() != 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) diff --git a/plugins/messageviewerheaderplugins/autotests/utils.h b/plugins/messageviewerheaderplugins/autotests/utils.h --- a/plugins/messageviewerheaderplugins/autotests/utils.h +++ b/plugins/messageviewerheaderplugins/autotests/utils.h @@ -23,7 +23,7 @@ #include #include -void testHeaderFile(const QString &data, const QString &name); +void testHeaderFile(const QString &data, const QString &name, const QString &dir = QString()); KMime::Message::Ptr readAndParseMail(const QString &mailFile); #endif // HEADERSTYLEPLUGINTESTUTILS_H diff --git a/plugins/messageviewerheaderplugins/autotests/utils.cpp b/plugins/messageviewerheaderplugins/autotests/utils.cpp --- a/plugins/messageviewerheaderplugins/autotests/utils.cpp +++ b/plugins/messageviewerheaderplugins/autotests/utils.cpp @@ -26,7 +26,7 @@ #include #include -void testHeaderFile(const QString &data, const QString &name) +void testHeaderFile(const QString &data, const QString &name, const QString &dir) { QString header = QStringLiteral("\n" "\n" @@ -46,7 +46,13 @@ QString outName = name + QStringLiteral(".out.html"); QString fName = name + QStringLiteral(".html"); - QVERIFY(QFile(QStringLiteral(HEADER_DATA_DIR "/") + fName).exists()); + QString referenceFile = QStringLiteral(HEADER_DATA_DIR "/"); + if (!dir.isEmpty()) { + referenceFile += dir + QStringLiteral("/"); + } + referenceFile += fName; + + QVERIFY(QFile(referenceFile).exists()); { QFile f(outName); @@ -69,7 +75,7 @@ const QStringList args = QStringList() << QStringLiteral("-u") << fName - << QStringLiteral(HEADER_DATA_DIR "/") + fName; + << referenceFile; QProcess proc; proc.setProcessChannelMode(QProcess::ForwardedChannels); proc.start(QStringLiteral("diff"), args); diff --git a/plugins/messageviewerheaderplugins/grantleeheaderstyleplugin/grantleeheaderstyleinterface.h b/plugins/messageviewerheaderplugins/grantleeheaderstyleplugin/grantleeheaderstyleinterface.h --- a/plugins/messageviewerheaderplugins/grantleeheaderstyleplugin/grantleeheaderstyleinterface.h +++ b/plugins/messageviewerheaderplugins/grantleeheaderstyleplugin/grantleeheaderstyleinterface.h @@ -26,6 +26,8 @@ class ThemeManager; } +class GrantleeHeaderStylePluginTest; + namespace MessageViewer { class GrantleeHeaderStyleInterface : public MessageViewer::HeaderStyleInterface { @@ -38,6 +40,8 @@ void activateAction() override; private: void slotGrantleeHeaders(); + + friend ::GrantleeHeaderStylePluginTest; GrantleeTheme::ThemeManager *mThemeManager = nullptr; }; }