File indexing completed on 2025-01-19 04:46:48

0001 /*
0002   SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
0003   SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "rendertest.h"
0009 #include "rendertest-common.cpp"
0010 
0011 #include "testobjecttreesource.h"
0012 
0013 #include <MessageViewer/CSSHelperBase>
0014 #include <MessageViewer/FileHtmlWriter>
0015 #include <MimeTreeParser/ObjectTreeParser>
0016 
0017 #include <KMime/Message>
0018 #include <QDir>
0019 #include <QIcon>
0020 #include <QPalette>
0021 #include <QStandardPaths>
0022 #include <QStyle>
0023 #include <QStyleFactory>
0024 #include <QTest>
0025 
0026 #ifndef Q_OS_WIN
0027 void initLocale()
0028 {
0029     setenv("KDEHOME", QFile::encodeName(QDir::homePath() + QLatin1StringView("/.qttest")).constData(), 1);
0030     setenv("LC_ALL", "en_US.utf-8", 1);
0031     setenv("TZ", "UTC", 1);
0032     setenv("BPF_ITINERARY_TESTMODE", "1", 1); // avoid itinerary plugin doing calendar lookups or D-Bus calls etc
0033     QStandardPaths::setTestModeEnabled(true);
0034     QLocale::setDefault(QLocale(QStringLiteral("en_US")));
0035 }
0036 
0037 Q_CONSTRUCTOR_FUNCTION(initLocale)
0038 #endif
0039 
0040 void RenderTest::initTestCase()
0041 {
0042     QIcon::setThemeName(QStringLiteral("breeze"));
0043     QApplication::setStyle(QStyleFactory::create(QStringLiteral("Fusion")));
0044     QPalette p(QApplication::style()->standardPalette());
0045     p.setCurrentColorGroup(QPalette::Normal);
0046     p.setColor(QPalette::Button, QColor::fromRgb(0xef, 0xeb, 0xe7));
0047     p.setColor(QPalette::ButtonText, QColor::fromRgb(0, 0, 0));
0048     p.setColor(QPalette::Shadow, QColor::fromRgb(0x76, 0x74, 0x72));
0049     QApplication::setPalette(p);
0050 }
0051 
0052 void RenderTest::testRender_data()
0053 {
0054     QTest::addColumn<QString>("mailFileName");
0055     QTest::addColumn<QString>("referenceFileName");
0056     QTest::addColumn<QString>("outFileName");
0057 
0058     QDir dir(QStringLiteral(MAIL_DATA_DIR));
0059     const QStringList lst = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
0060     for (const QString &file : lst) {
0061         if (!QFile::exists(dir.path() + QLatin1Char('/') + file + QStringLiteral(".html"))) {
0062             continue;
0063         }
0064         QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file)
0065                                                    << QString(dir.path() + QLatin1Char('/') + file + QStringLiteral(".html"))
0066                                                    << QString(file + QStringLiteral(".out"));
0067     }
0068 }
0069 
0070 void RenderTest::testRender()
0071 {
0072     QFETCH(QString, mailFileName);
0073     QFETCH(QString, referenceFileName);
0074     QFETCH(QString, outFileName);
0075 
0076     // load input mail
0077     QFile mailFile(mailFileName);
0078     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0079     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0080     QVERIFY(!mailData.isEmpty());
0081     KMime::Message::Ptr msg(new KMime::Message);
0082     msg->setContent(mailData);
0083     msg->parse();
0084 
0085     // render the mail
0086     MessageViewer::FileHtmlWriter fileWriter(outFileName);
0087     QImage paintDevice;
0088     MessageViewer::CSSHelperBase cssHelper(&paintDevice);
0089     MimeTreeParser::NodeHelper nodeHelper;
0090     TestObjectTreeSource testSource(&fileWriter, &cssHelper);
0091     MimeTreeParser::ObjectTreeParser otp(&testSource, &nodeHelper);
0092 
0093     otp.parseObjectTree(msg.data());
0094 
0095     fileWriter.begin();
0096     fileWriter.write(
0097         QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
0098                        "<html>\n"
0099                        "<body>\n"));
0100     testSource.render(otp.parsedPart(), false);
0101     fileWriter.write(QStringLiteral("</body></html>"));
0102     fileWriter.end();
0103 
0104     compareFile(outFileName, referenceFileName);
0105 }
0106 
0107 void RenderTest::testRenderKeyDetails_data()
0108 {
0109     QTest::addColumn<QString>("basename");
0110     QTest::newRow("message-with-openpgp-key.mbox") << QStringLiteral("message-with-openpgp-key.mbox");
0111     QTest::newRow("message-with-two-openpgp-key.mbox") << QStringLiteral("message-with-two-openpgp-key.mbox");
0112 }
0113 
0114 void RenderTest::testRenderKeyDetails()
0115 {
0116     QFETCH(QString, basename);
0117     QString mailFileName = QStringLiteral(MAIL_DATA_DIR) + QLatin1Char('/') + basename;
0118     QString referenceFileName = QStringLiteral(MAIL_DATA_DIR) + QLatin1Char('/') + basename + QStringLiteral(".html");
0119     QString outFileName = basename + QStringLiteral(".out");
0120 
0121     // load input mail
0122     QFile mailFile(mailFileName);
0123     QVERIFY(mailFile.open(QIODevice::ReadOnly));
0124     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0125     QVERIFY(!mailData.isEmpty());
0126     KMime::Message::Ptr msg(new KMime::Message);
0127     msg->setContent(mailData);
0128     msg->parse();
0129 
0130     // render the mail
0131     QEventLoop loop;
0132     MessageViewer::FileHtmlWriter fileWriter(outFileName);
0133     QImage paintDevice;
0134     MessageViewer::CSSHelperBase cssHelper(&paintDevice);
0135     MimeTreeParser::NodeHelper nodeHelper;
0136     TestObjectTreeSource testSource(&fileWriter, &cssHelper);
0137     testSource.setShowSignatureDetails(true);
0138     MimeTreeParser::ObjectTreeParser otp(&testSource, &nodeHelper);
0139 
0140     connect(&nodeHelper, &MimeTreeParser::NodeHelper::update, &loop, &QEventLoop::quit);
0141     otp.parseObjectTree(msg.data());
0142 
0143     fileWriter.begin();
0144     fileWriter.write(
0145         QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
0146                        "<html>\n"
0147                        "<body>\n"));
0148     testSource.render(otp.parsedPart(), false);
0149     fileWriter.write(QStringLiteral("</body></html>"));
0150     fileWriter.end();
0151 
0152     compareFile(outFileName, referenceFileName + QStringLiteral(".running"));
0153     loop.exec();
0154 
0155     {
0156         MimeTreeParser::ObjectTreeParser otp(&testSource, &nodeHelper);
0157         otp.parseObjectTree(msg.data());
0158 
0159         fileWriter.begin();
0160         fileWriter.write(
0161             QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
0162                            "<html>\n"
0163                            "<body>\n"));
0164         testSource.render(otp.parsedPart(), false);
0165         fileWriter.write(QStringLiteral("</body></html>"));
0166         fileWriter.end();
0167 
0168         compareFile(outFileName, referenceFileName + QStringLiteral(".details"));
0169     }
0170 }
0171 
0172 QTEST_MAIN(RenderTest)
0173 
0174 #include "moc_rendertest.cpp"