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-common.cpp" 0009 0010 #include "testobjecttreesource.h" 0011 0012 #include <MessageViewer/CSSHelperBase> 0013 #include <MessageViewer/FileHtmlWriter> 0014 #include <MimeTreeParser/ObjectTreeParser> 0015 0016 #include <KMime/Message> 0017 #include <QDir> 0018 #include <QIcon> 0019 #include <QPalette> 0020 #include <QStandardPaths> 0021 #include <QStyle> 0022 #include <QStyleFactory> 0023 #include <QTest> 0024 0025 #ifndef Q_OS_WIN 0026 void initLocale() 0027 { 0028 setenv("KDEHOME", QFile::encodeName(QDir::homePath() + QLatin1StringView("/.qttest")).constData(), 1); 0029 setenv("LC_ALL", "en_US.utf-8", 1); 0030 setenv("TZ", "UTC", 1); 0031 QStandardPaths::setTestModeEnabled(true); 0032 QLocale::setDefault(QLocale(QStringLiteral("en_US"))); 0033 } 0034 0035 Q_CONSTRUCTOR_FUNCTION(initLocale) 0036 #endif 0037 0038 class RenderTestAkonadi : public QObject 0039 { 0040 Q_OBJECT 0041 private Q_SLOTS: 0042 void initTestCase() 0043 { 0044 QIcon::setThemeName(QStringLiteral("breeze")); 0045 QApplication::setStyle(QStyleFactory::create(QStringLiteral("Fusion"))); 0046 QPalette p(QApplication::style()->standardPalette()); 0047 p.setCurrentColorGroup(QPalette::Normal); 0048 p.setColor(QPalette::Button, QColor::fromRgb(0xef, 0xeb, 0xe7)); 0049 p.setColor(QPalette::ButtonText, QColor::fromRgb(0, 0, 0)); 0050 p.setColor(QPalette::Shadow, QColor::fromRgb(0x76, 0x74, 0x72)); 0051 QApplication::setPalette(p); 0052 } 0053 0054 void testRender_data() 0055 { 0056 QTest::addColumn<QString>("mailFileName"); 0057 QTest::addColumn<QString>("referenceFileName"); 0058 QTest::addColumn<QString>("outFileName"); 0059 0060 QDir dir(QStringLiteral(DATA_DIR)); 0061 const QStringList lst = dir.entryList(QStringList(QStringLiteral("*.mbox")), QDir::Files | QDir::Readable | QDir::NoSymLinks); 0062 for (const QString &file : lst) { 0063 if (!QFile::exists(dir.path() + QLatin1Char('/') + file + QStringLiteral(".html"))) { 0064 continue; 0065 } 0066 QTest::newRow(file.toLatin1().constData()) 0067 << QString(dir.path() + QLatin1Char('/') + file) << QString(dir.path() + QLatin1Char('/') + file + QStringLiteral(".html")) 0068 << QString(file + QStringLiteral(".out")); 0069 } 0070 } 0071 0072 void testRender() 0073 { 0074 QFETCH(QString, mailFileName); 0075 QFETCH(QString, referenceFileName); 0076 QFETCH(QString, outFileName); 0077 0078 // load input mail 0079 QFile mailFile(mailFileName); 0080 QVERIFY(mailFile.open(QIODevice::ReadOnly)); 0081 const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll()); 0082 QVERIFY(!mailData.isEmpty()); 0083 KMime::Message::Ptr msg(new KMime::Message); 0084 msg->setContent(mailData); 0085 msg->parse(); 0086 0087 // render the mail 0088 MessageViewer::FileHtmlWriter fileWriter(outFileName); 0089 QImage paintDevice; 0090 MessageViewer::CSSHelperBase cssHelper(&paintDevice); 0091 MimeTreeParser::NodeHelper nodeHelper; 0092 TestObjectTreeSource testSource(&fileWriter, &cssHelper); 0093 MimeTreeParser::ObjectTreeParser otp(&testSource, &nodeHelper); 0094 0095 otp.parseObjectTree(msg.data()); 0096 0097 fileWriter.begin(); 0098 fileWriter.write( 0099 QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" 0100 "<html>\n" 0101 "<body>\n")); 0102 testSource.render(otp.parsedPart(), false); 0103 fileWriter.write(QStringLiteral("</body></html>")); 0104 fileWriter.end(); 0105 0106 compareFile(outFileName, referenceFileName); 0107 } 0108 }; 0109 0110 QTEST_MAIN(RenderTestAkonadi) 0111 0112 #include "rendertest-akonadi.moc"