Changeset View
Changeset View
Standalone View
Standalone View
plugins/messageviewer/bodypartformatter/autotests/rendertest.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2010 Volker Krause <vkrause@kde.org> | ||||
3 | Copyright (c) 2016 Sandro Knauß <sknauss@kde.org> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or modify it | ||||
6 | under the terms of the GNU Library General Public License as published by | ||||
7 | the Free Software Foundation; either version 2 of the License, or (at your | ||||
8 | option) any later version. | ||||
9 | | ||||
10 | This library is distributed in the hope that it will be useful, but WITHOUT | ||||
11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||||
13 | License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to the | ||||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
18 | 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "rendertest.h" | ||||
22 | | ||||
23 | #include "testobjecttreesource.h" | ||||
24 | | ||||
25 | #include <MessageViewer/ObjectTreeParser> | ||||
26 | #include <MessageViewer/CSSHelperBase> | ||||
27 | #include <MessageViewer/FileHtmlWriter> | ||||
28 | | ||||
29 | #include <KMime/Message> | ||||
30 | #include <QTest> | ||||
31 | #include <QDir> | ||||
32 | #include <QProcess> | ||||
33 | | ||||
34 | void RenderTest::testRender_data() | ||||
35 | { | ||||
36 | QTest::addColumn<QString>("mailFileName"); | ||||
37 | QTest::addColumn<QString>("referenceFileName"); | ||||
38 | QTest::addColumn<QString>("outFileName"); | ||||
39 | | ||||
40 | QDir dir(QStringLiteral(MAIL_DATA_DIR)); | ||||
41 | foreach (const QString &file, dir.entryList(QStringList(QLatin1String("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks)) { | ||||
42 | if (!QFile::exists(dir.path() + QLatin1Char('/') + file + QLatin1String(".html"))) { | ||||
43 | continue; | ||||
44 | } | ||||
45 | QTest::newRow(file.toLatin1()) << QString(dir.path() + QLatin1Char('/') + file) << QString(dir.path() + QLatin1Char('/') + file + QLatin1String(".html")) << QString(file + QLatin1String(".out")); | ||||
46 | } | ||||
47 | } | ||||
48 | | ||||
49 | void RenderTest::testRender() | ||||
50 | { | ||||
51 | QFETCH(QString, mailFileName); | ||||
52 | QFETCH(QString, referenceFileName); | ||||
53 | QFETCH(QString, outFileName); | ||||
54 | | ||||
55 | const QString htmlFileName = outFileName + QLatin1String(".html"); | ||||
56 | | ||||
57 | // load input mail | ||||
58 | QFile mailFile(mailFileName); | ||||
59 | QVERIFY(mailFile.open(QIODevice::ReadOnly)); | ||||
60 | const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll()); | ||||
61 | QVERIFY(!mailData.isEmpty()); | ||||
62 | KMime::Message::Ptr msg(new KMime::Message); | ||||
63 | msg->setContent(mailData); | ||||
64 | msg->parse(); | ||||
65 | | ||||
66 | // render the mail | ||||
67 | MessageViewer::FileHtmlWriter fileWriter(outFileName); | ||||
68 | QImage paintDevice; | ||||
69 | MessageViewer::CSSHelperBase cssHelper(&paintDevice); | ||||
70 | MessageViewer::NodeHelper nodeHelper; | ||||
71 | TestObjectTreeSource testSource(&fileWriter, &cssHelper); | ||||
72 | MessageViewer::ObjectTreeParser otp(&testSource, &nodeHelper); | ||||
73 | | ||||
74 | fileWriter.begin(QString()); | ||||
75 | fileWriter.queue(QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" | ||||
76 | "<html>\n" | ||||
77 | "<body>\n")); | ||||
78 | | ||||
79 | otp.parseObjectTree(msg.data()); | ||||
80 | | ||||
81 | fileWriter.queue(QStringLiteral("</body></html>")); | ||||
82 | fileWriter.flush(); | ||||
83 | fileWriter.end(); | ||||
84 | | ||||
85 | QVERIFY(QFile::exists(outFileName)); | ||||
86 | | ||||
87 | // validate xml and pretty-print for comparisson | ||||
88 | // TODO add proper cmake check for xmllint and diff | ||||
89 | QStringList args = QStringList() | ||||
90 | << QStringLiteral("--format") | ||||
91 | << QStringLiteral("--encode") | ||||
92 | << QStringLiteral("UTF8") | ||||
93 | << QStringLiteral("--output") | ||||
94 | << htmlFileName | ||||
95 | << outFileName; | ||||
96 | QCOMPARE(QProcess::execute(QLatin1String("xmllint"), args), 0); | ||||
97 | | ||||
98 | // get rid of system dependent or random paths | ||||
99 | { | ||||
100 | QFile f(htmlFileName); | ||||
101 | QVERIFY(f.open(QIODevice::ReadOnly)); | ||||
102 | QString content = QString::fromUtf8(f.readAll()); | ||||
103 | f.close(); | ||||
104 | content.replace(QRegExp(QLatin1String("\"file:[^\"]*[/(?:%2F)]([^\"/(?:%2F)]*)\"")), QStringLiteral("\"file:\\1\"")); | ||||
105 | QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate)); | ||||
106 | f.write(content.toUtf8()); | ||||
107 | f.close(); | ||||
108 | } | ||||
109 | | ||||
110 | // compare to reference file | ||||
111 | args = QStringList() | ||||
112 | << QStringLiteral("-u") | ||||
113 | << referenceFileName | ||||
114 | << htmlFileName; | ||||
115 | QProcess proc; | ||||
116 | proc.setProcessChannelMode(QProcess::ForwardedChannels); | ||||
117 | proc.start(QStringLiteral("diff"), args); | ||||
118 | QVERIFY(proc.waitForFinished()); | ||||
119 | | ||||
120 | QCOMPARE(proc.exitCode(), 0); | ||||
121 | } | ||||
122 | | ||||
123 | QTEST_MAIN(RenderTest) | ||||
124 | No newline at end of file |