diff --git a/messageviewer/src/header/autotests/data/datetest.tmpl.html b/messageviewer/src/header/autotests/data/datetest.tmpl.html
index aa1e85df..e5298606 100644
--- a/messageviewer/src/header/autotests/data/datetest.tmpl.html
+++ b/messageviewer/src/header/autotests/data/datetest.tmpl.html
@@ -1,15 +1,15 @@
-
str: Yesterday 16:52:43
+
str: 21 May 2018 16:52:43
short: 21 May 2018 16:52:43
-long: Yesterday 16:52:43
-fancylong: Yesterday 16:52:43
-fancyshort: Yesterday 16:52:43
+long: Mon May 21 16:52:43 2018
+fancylong: 21 May 2018 16:52:43
+fancyshort: 21 May 2018 16:52:43
localelong: Mon May 21 16:52:43 2018
invalid:
diff --git a/messageviewer/src/header/autotests/data/datetest.tmpl.html b/messageviewer/src/header/autotests/data/fancydate.html
similarity index 54%
copy from messageviewer/src/header/autotests/data/datetest.tmpl.html
copy to messageviewer/src/header/autotests/data/fancydate.html
index aa1e85df..b4c71fe9 100644
--- a/messageviewer/src/header/autotests/data/datetest.tmpl.html
+++ b/messageviewer/src/header/autotests/data/fancydate.html
@@ -1,15 +1,11 @@
-
str: Yesterday 16:52:43
-short: 21 May 2018 16:52:43
-long: Yesterday 16:52:43
-fancylong: Yesterday 16:52:43
-fancyshort: Yesterday 16:52:43
-localelong: Mon May 21 16:52:43 2018
-invalid:
+
str: Yesterday 12:34:56
+fancyshort: Yesterday 12:34:56
+fancylong: Yesterday 12:34:56
diff --git a/messageviewer/src/header/autotests/data/fancydate.tmpl b/messageviewer/src/header/autotests/data/fancydate.tmpl
new file mode 100644
index 00000000..38cbf54f
--- /dev/null
+++ b/messageviewer/src/header/autotests/data/fancydate.tmpl
@@ -0,0 +1,3 @@
+str: {{ header.date.str|safe }}
+fancyshort: {{ header.date.fancyshort|safe }}
+fancylong: {{ header.date.fancylong|safe }}
diff --git a/messageviewer/src/header/autotests/grantleeheaderformattertest.cpp b/messageviewer/src/header/autotests/grantleeheaderformattertest.cpp
index c6c3a0a9..ad5b6249 100644
--- a/messageviewer/src/header/autotests/grantleeheaderformattertest.cpp
+++ b/messageviewer/src/header/autotests/grantleeheaderformattertest.cpp
@@ -1,161 +1,186 @@
/*
Copyright 2018 Sandro Knauß
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy
defined in Section 14 of version 3 of the license.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include "grantleeheaderformattertest.h"
#include
#include "../grantleeheaderformatter.h"
#include
#include
#include
#include
#include
#include
using namespace MessageViewer;
QTEST_MAIN(GrantleeHeaderFormatterTest)
void testHeaderFile(const QString &data, const QString &absolutePath, const QString &name)
{
QString header = QStringLiteral("\n"
"\n"
"\n");
header += data + QStringLiteral(" \n");
header += QStringLiteral("\n\n\n");
header.replace(QStringLiteral("file://") + absolutePath, QStringLiteral("file://PATHTOSTYLE"));
header.replace(QRegExp(QStringLiteral("[\t ]+")), QStringLiteral(" "));
header.replace(QRegExp(QStringLiteral("[\t ]*\n+[\t ]*")), QStringLiteral("\n"));
header.replace(QRegExp(QStringLiteral("([\n\t ])\\1+")), QStringLiteral("\\1"));
header.replace(QRegExp(QStringLiteral(">\n+[\t ]*")), QStringLiteral(">"));
header.replace(QRegExp(QStringLiteral("[\t ]*\n+[\t ]*<")), QStringLiteral("<"));
header.replace(QLatin1String(" "), QLatin1String("NBSP_ENTITY_PLACEHOLDER")); // xmlling chokes on
QString outName = name + QStringLiteral(".out.html");
QString fName = name + QStringLiteral(".html");
QVERIFY(QFile(QStringLiteral(HEADER_DATA_DIR "/") + fName).exists());
{
QFile f(outName);
f.open(QIODevice::WriteOnly);
f.write(header.toUtf8());
f.close();
}
// TODO add proper cmake check for xmllint and diff
{
const QStringList args = QStringList()
<< QStringLiteral("--format")
<< QStringLiteral("--encode") << QStringLiteral("UTF8")
<< QStringLiteral("--output") << fName
<< outName;
QCOMPARE(QProcess::execute(QStringLiteral("xmllint"), args), 0);
}
{
// compare to reference file
const QStringList args = QStringList()
<< QStringLiteral("-u")
<< fName
<< QStringLiteral(HEADER_DATA_DIR "/") + fName;
QProcess proc;
proc.setProcessChannelMode(QProcess::ForwardedChannels);
proc.start(QStringLiteral("diff"), args);
QVERIFY(proc.waitForFinished());
QCOMPARE(proc.exitCode(), 0);
}
}
KMime::Message::Ptr readAndParseMail(const QString &mailFile)
{
QFile file(QStringLiteral(HEADER_DATA_DIR) + QLatin1Char('/') + mailFile);
bool openFile = file.open(QIODevice::ReadOnly);
Q_ASSERT(openFile);
const QByteArray data = KMime::CRLFtoLF(file.readAll());
Q_ASSERT(!data.isEmpty());
KMime::Message::Ptr msg(new KMime::Message);
msg->setContent(data);
msg->parse();
return msg;
}
void GrantleeHeaderFormatterTest::testInvalid()
{
auto style = GrantleeHeaderStyle();
auto formatter = GrantleeHeaderFormatter();
auto aMsg = readAndParseMail(QStringLiteral("allheaders.mbox"));
QString filename = QStringLiteral("invalid");
QString absolutePath = QStringLiteral(HEADER_DATA_DIR) + QLatin1Char('/') + filename;
QString data = formatter.toHtml(QStringList(), absolutePath, filename, &style, aMsg.data(), false);
QCOMPARE(data, QStringLiteral("Template not found, invalid"));
}
void GrantleeHeaderFormatterTest::testPrint()
{
QString tmplName = QStringLiteral("printtest.tmpl");
auto style = GrantleeHeaderStyle();
auto formatter = GrantleeHeaderFormatter();
KMime::Message::Ptr aMsg(new KMime::Message);
const QString &absolutePath = QStringLiteral(HEADER_DATA_DIR) + QLatin1Char('/') + tmplName;
{
const QString &data = formatter.toHtml(QStringList(), QStringLiteral(HEADER_DATA_DIR), tmplName, &style, aMsg.data(), false);
testHeaderFile(QStringLiteral("")+data, absolutePath, QStringLiteral("printtest.off"));
}
{
const QString &data = formatter.toHtml(QStringList(), QStringLiteral(HEADER_DATA_DIR), tmplName, &style, aMsg.data(), true);
testHeaderFile(QStringLiteral("
")+data, absolutePath, QStringLiteral("printtest.on"));
}
}
+void GrantleeHeaderFormatterTest::testFancyDate()
+{
+ QString tmplName = QStringLiteral("fancydate.tmpl");
+
+ auto style = GrantleeHeaderStyle();
+ auto formatter = GrantleeHeaderFormatter();
+ KMime::Message::Ptr msg(new KMime::Message);
+
+ {
+ auto datetime(QDateTime::currentDateTime());
+ datetime.setTime(QTime(12,34,56));
+ datetime = datetime.addDays(-1);
+
+ const QByteArray data = "From: from@example.com\nDate: " + datetime.toString(Qt::RFC2822Date).toLocal8Bit() + "\nTo: to@example.com\n\ncontent";
+ msg->setContent(KMime::CRLFtoLF(data));
+ msg->parse();
+ }
+
+ const QString &absolutePath = QStringLiteral(HEADER_DATA_DIR) + QLatin1Char('/') + tmplName;
+
+ const QString &data = formatter.toHtml(QStringList(), QStringLiteral(HEADER_DATA_DIR), tmplName, &style, msg.data(), false);
+ testHeaderFile(QStringLiteral("
")+data, absolutePath, QStringLiteral("fancydate"));
+
+}
+
void GrantleeHeaderFormatterTest::testBlock_data()
{
QTest::addColumn
("tmplName");
QDir dir(QStringLiteral(HEADER_DATA_DIR));
const auto l = dir.entryList(QStringList(QStringLiteral("*.tmpl")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
for (const QString &file : l) {
if (!QFile::exists(dir.path() + QLatin1Char('/') + file + QStringLiteral(".html"))) {
continue;
}
QTest::newRow(file.toLatin1().constData()) << file;
}
}
void GrantleeHeaderFormatterTest::testBlock()
{
QFETCH(QString, tmplName);
auto style = GrantleeHeaderStyle();
auto formatter = GrantleeHeaderFormatter();
auto aMsg = readAndParseMail(QStringLiteral("headertest.mbox"));
QString absolutePath = QStringLiteral(HEADER_DATA_DIR) + QLatin1Char('/') + tmplName;
QString data = formatter.toHtml(QStringList(), QStringLiteral(HEADER_DATA_DIR), tmplName, &style, aMsg.data(), false);
testHeaderFile(QStringLiteral("")+data, absolutePath, tmplName);
}
diff --git a/messageviewer/src/header/autotests/grantleeheaderformattertest.h b/messageviewer/src/header/autotests/grantleeheaderformattertest.h
index 4dcaf08d..7325a6da 100644
--- a/messageviewer/src/header/autotests/grantleeheaderformattertest.h
+++ b/messageviewer/src/header/autotests/grantleeheaderformattertest.h
@@ -1,36 +1,37 @@
/*
Copyright 2018 Sandro Knauß
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy
defined in Section 14 of version 3 of the license.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#ifndef GRANTLEEHEADERFORMATTERTEST_H
#define GRANTLEEHEADERFORMATTERTEST_H
#include
class GrantleeHeaderFormatterTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testInvalid();
void testPrint();
+ void testFancyDate();
void testBlock_data();
void testBlock();
};
#endif