File indexing completed on 2024-12-22 05:05:18

0001 // SPDX-FileCopyrightText: 2017 Christian Mollekopf <mollekopf@kolabsys.com>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include <KMime/Message>
0005 #include <QAbstractItemModelTester>
0006 #include <QDebug>
0007 #include <QTest>
0008 #include <QTextDocument>
0009 
0010 #include "messageparser.h"
0011 #include "partmodel.h"
0012 
0013 KMime::Message::Ptr readMailFromFile(const QString &mailFile)
0014 {
0015     QFile file(QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + mailFile);
0016     file.open(QIODevice::ReadOnly);
0017     Q_ASSERT(file.isOpen());
0018     auto mailData = KMime::CRLFtoLF(file.readAll());
0019     KMime::Message::Ptr message(new KMime::Message);
0020     message->setContent(mailData);
0021     message->parse();
0022     return message;
0023 }
0024 
0025 class PartModelTest : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 private Q_SLOTS:
0030 
0031     void initTestCase()
0032     {
0033     }
0034 
0035     void testTrim()
0036     {
0037         auto result = PartModel::trim(QLatin1StringView("<p>This is some funky test.</p>\n<p>-- <br>\nChristian Mollekopf<br>\nSenior Software"));
0038         QCOMPARE(result.second, true);
0039         QCOMPARE(result.first, QLatin1StringView("<p>This is some funky test.</p>\n"));
0040     }
0041 
0042     void testTrimFromPlain()
0043     {
0044         // Qt::convertFromPlainText inserts non-breaking spaces
0045         auto result = PartModel::trim(Qt::convertFromPlainText(QLatin1StringView("This is some funky text.\n\n-- \nChristian Mollekopf\nSenior Software")));
0046         QCOMPARE(result.second, true);
0047         //\u00A0 is a on-breaking space
0048         const auto expected = QStringLiteral("<p>This is some funky text.</p>\n").replace(QLatin1Char(' '), QChar(0x00a0));
0049         QCOMPARE(result.first, expected);
0050     }
0051 
0052     void testModel()
0053     {
0054         MessageParser messageParser;
0055         messageParser.setMessage(readMailFromFile(QLatin1StringView("html.mbox")));
0056 
0057         QFont font{};
0058         font.setFamily(QStringLiteral("Noto Sans"));
0059         qGuiApp->setFont(font);
0060 
0061         auto partModel = messageParser.parts();
0062         new QAbstractItemModelTester(partModel);
0063         QCOMPARE(partModel->rowCount(), 1);
0064         QCOMPARE(partModel->data(partModel->index(0, 0), PartModel::TypeRole).value<PartModel::Types>(), PartModel::Types::Plain);
0065         QCOMPARE(partModel->data(partModel->index(0, 0), PartModel::IsEmbeddedRole).toBool(), false);
0066         QCOMPARE(partModel->data(partModel->index(0, 0), PartModel::IsErrorRole).toBool(), false);
0067         QCOMPARE(
0068             partModel->data(partModel->index(0, 0), PartModel::ContentRole).toString(),
0069             QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
0070                            "\"http://www.w3.org/TR/html4/loose.dtd\">\n<html><head><title></title><style>\nbody {\n  overflow:hidden;\n  font-family: \"Noto "
0071                            "Sans\" ! important;\n  color: #31363b ! important;\n  background-color: #fcfcfc ! important\n}\nblockquote { \n  border-left: 2px "
0072                            "solid #bdc3c7 ! important;\n}\n</style></head>\n<body>\n<html><body><p><span>HTML</span> text</p></body></html></body></html>"));
0073     }
0074 };
0075 
0076 QTEST_MAIN(PartModelTest)
0077 #include "partmodeltest.moc"