rendertest.cpp 4.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
  Copyright (c) 2010 Volker Krause <vkrause@kde.org>
  Copyright (c) 2016 Sandro Knauß <sknauss@kde.org>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library 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 Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include "rendertest.h"

#include "testobjecttreesource.h"

25
#include <MimeTreeParser/ObjectTreeParser>
Sandro Knauß's avatar
Sandro Knauß committed
26
#include <MessageViewer/CSSHelperBase>
27
#include <MimeTreeParser/FileHtmlWriter>
28 29 30 31

#include <KMime/Message>
#include <QTest>
#include <QDir>
32
#include <QIcon>
33 34
#include <QProcess>

Laurent Montel's avatar
Laurent Montel committed
35 36 37 38 39 40 41 42 43
#ifndef Q_OS_WIN
void initLocale()
{
    setenv("LC_ALL", "en_US.utf-8", 1);
}

Q_CONSTRUCTOR_FUNCTION(initLocale)
#endif

44 45 46 47 48 49 50
void RenderTest::testRender_data()
{
    QTest::addColumn<QString>("mailFileName");
    QTest::addColumn<QString>("referenceFileName");
    QTest::addColumn<QString>("outFileName");

    QDir dir(QStringLiteral(MAIL_DATA_DIR));
Laurent Montel's avatar
Laurent Montel committed
51 52
    const QStringList lst = dir.entryList(QStringList(QLatin1String("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
    for (const QString &file : lst) {
53 54 55
        if (!QFile::exists(dir.path() + QLatin1Char('/') + file + QLatin1String(".html"))) {
            continue;
        }
Laurent Montel's avatar
Laurent Montel committed
56 57
        QTest::newRow(file.toLatin1()) << QString(dir.path() + QLatin1Char('/') +  file) << QString(dir.path() + QLatin1Char('/') + file + QLatin1String(".html"))
                                       << QString(file + QLatin1String(".out"));
58 59 60 61 62 63 64 65 66 67 68
    }
}

void RenderTest::testRender()
{
    QFETCH(QString, mailFileName);
    QFETCH(QString, referenceFileName);
    QFETCH(QString, outFileName);

    const QString htmlFileName = outFileName + QLatin1String(".html");

69 70
    QIcon::setThemeName(QStringLiteral("oxygen"));

71 72 73 74 75 76 77 78 79 80
    // load input mail
    QFile mailFile(mailFileName);
    QVERIFY(mailFile.open(QIODevice::ReadOnly));
    const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
    QVERIFY(!mailData.isEmpty());
    KMime::Message::Ptr msg(new KMime::Message);
    msg->setContent(mailData);
    msg->parse();

    // render the mail
81
    MimeTreeParser::FileHtmlWriter fileWriter(outFileName);
82
    QImage paintDevice;
Sandro Knauß's avatar
Sandro Knauß committed
83
    MessageViewer::CSSHelperBase cssHelper(&paintDevice);
84
    MimeTreeParser::NodeHelper nodeHelper;
85
    TestObjectTreeSource testSource(&fileWriter, &cssHelper);
86
    MimeTreeParser::ObjectTreeParser otp(&testSource, &nodeHelper);
87 88 89

    fileWriter.begin(QString());
    fileWriter.queue(QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
Laurent Montel's avatar
Laurent Montel committed
90 91
                                    "<html>\n"
                                    "<body>\n"));
92 93 94 95 96 97 98 99 100 101 102 103

    otp.parseObjectTree(msg.data());

    fileWriter.queue(QStringLiteral("</body></html>"));
    fileWriter.flush();
    fileWriter.end();

    QVERIFY(QFile::exists(outFileName));

    // validate xml and pretty-print for comparisson
    // TODO add proper cmake check for xmllint and diff
    QStringList args = QStringList()
Laurent Montel's avatar
Laurent Montel committed
104 105 106 107 108 109
                       << QStringLiteral("--format")
                       << QStringLiteral("--encode")
                       << QStringLiteral("UTF8")
                       << QStringLiteral("--output")
                       << htmlFileName
                       << outFileName;
Laurent Montel's avatar
Laurent Montel committed
110
    QCOMPARE(QProcess::execute(QLatin1String("xmllint"), args), 0);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    // get rid of system dependent or random paths
    {
        QFile f(htmlFileName);
        QVERIFY(f.open(QIODevice::ReadOnly));
        QString content = QString::fromUtf8(f.readAll());
        f.close();
        content.replace(QRegExp(QLatin1String("\"file:[^\"]*[/(?:%2F)]([^\"/(?:%2F)]*)\"")), QStringLiteral("\"file:\\1\""));
        QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
        f.write(content.toUtf8());
        f.close();
    }

    // compare to reference file
    args = QStringList()
Laurent Montel's avatar
Laurent Montel committed
126 127 128
           << QStringLiteral("-u")
           << referenceFileName
           << htmlFileName;
129 130 131 132 133 134 135 136
    QProcess proc;
    proc.setProcessChannelMode(QProcess::ForwardedChannels);
    proc.start(QStringLiteral("diff"), args);
    QVERIFY(proc.waitForFinished());

    QCOMPARE(proc.exitCode(), 0);
}

137
QTEST_MAIN(RenderTest)