From ab912af8158028206cf43b0a6ca0eac8eef040f4 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 30 Jun 2017 16:23:20 +0200 Subject: Fixed html to plaintext conversion --- framework/src/CMakeLists.txt | 1 + framework/src/domain/mime/mailtemplates.cpp | 10 ++- framework/src/domain/mime/tests/CMakeLists.txt | 15 ++++ .../src/domain/mime/tests/mailtemplatetest.cpp | 99 ++++++++++++++++++++++ 4 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 framework/src/domain/mime/tests/CMakeLists.txt create mode 100644 framework/src/domain/mime/tests/mailtemplatetest.cpp (limited to 'framework/src') diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt index 1c53530f..b935fbed 100644 --- a/framework/src/CMakeLists.txt +++ b/framework/src/CMakeLists.txt @@ -47,6 +47,7 @@ qt5_use_modules(frameworkplugin Core Quick Qml WebEngineWidgets Test) target_link_libraries(frameworkplugin sink kube_otp KF5::Codecs KF5::Package KF5::Contacts KAsync) install(TARGETS frameworkplugin DESTINATION ${FRAMEWORK_INSTALL_DIR}) +add_subdirectory(domain/mime/tests) add_subdirectory(domain/mime/mimetreeparser) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/framework/src/domain/mime/mailtemplates.cpp b/framework/src/domain/mime/mailtemplates.cpp index d41039db..c202af80 100644 --- a/framework/src/domain/mime/mailtemplates.cpp +++ b/framework/src/domain/mime/mailtemplates.cpp @@ -396,9 +396,11 @@ void plainMessageText(const QString &plainTextContent, const QString &htmlConten auto page = new QWebEnginePage; setupPage(page); page->setHtml(htmlContent); - page->toPlainText([=] (const QString &plaintext) { - page->deleteLater(); - callback(plaintext); + QObject::connect(page, &QWebEnginePage::loadFinished, [=] (bool ok) { + page->toPlainText([=] (const QString &plaintext) { + page->deleteLater(); + callback(plaintext); + }); }); return; } @@ -860,7 +862,7 @@ void MailTemplates::reply(const KMime::Message::Ptr &origMsg, const std::functio makeValidHtml(htmlBodyResult, headElement); } - //Assemble the message */ + //Assemble the message addProcessedBodyToMessage(msg, plainBodyResult, htmlBodyResult, false); applyCharset(msg, origMsg); msg->assemble(); diff --git a/framework/src/domain/mime/tests/CMakeLists.txt b/framework/src/domain/mime/tests/CMakeLists.txt new file mode 100644 index 00000000..4fad00a1 --- /dev/null +++ b/framework/src/domain/mime/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +add_definitions( -DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/../testdata" ) + +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/.. + ) + +include(ECMAddTests) +find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Test WebEngine) + +ecm_add_test(mailtemplatetest.cpp + TEST_NAME "mailtemplatetest" + NAME_PREFIX "" + LINK_LIBRARIES Qt5::Core Qt5::Test Qt5::WebEngine frameworkplugin +) diff --git a/framework/src/domain/mime/tests/mailtemplatetest.cpp b/framework/src/domain/mime/tests/mailtemplatetest.cpp new file mode 100644 index 00000000..18f315f1 --- /dev/null +++ b/framework/src/domain/mime/tests/mailtemplatetest.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "mailtemplates.h" + +static QByteArray readMailFromFile(const QString &mailFile) +{ + Q_ASSERT(!QString::fromLatin1(MAIL_DATA_DIR).isEmpty()); + QFile file(QLatin1String(MAIL_DATA_DIR) + QLatin1Char('/') + mailFile); + file.open(QIODevice::ReadOnly); + Q_ASSERT(file.isOpen()); + return file.readAll(); +} + + +static KMime::Message::Ptr readMail(const QString &mailFile) +{ + auto msg = KMime::Message::Ptr::create(); + msg->setContent(readMailFromFile(mailFile)); + msg->parse(); + return msg; +} + +static QString removeFirstLine(const QString &s) +{ + return s.mid(s.indexOf("\n") + 1); +} + +static QString normalize(const QString &s) +{ + auto text = s; + text.replace(">", ""); + text.replace("\n", ""); + text.replace(" ", ""); + return text; +} + +static QString unquote(const QString &s) +{ + auto text = s; + text.replace("> ", ""); + return text; +} + +class MailTemplateTest : public QObject +{ + Q_OBJECT +private slots: + + void initTestCase() + { + QtWebEngine::initialize(); + } + + void testPlain() + { + auto msg = readMail("plaintext.mbox"); + KMime::Message::Ptr result; + MailTemplates::reply(msg, [&] (const KMime::Message::Ptr &r) { + result = r; + }); + QTRY_VERIFY(result); + QCOMPARE(normalize(removeFirstLine(result->body())), normalize(msg->body())); + } + + void testHtml() + { + auto msg = readMail("html.mbox"); + KMime::Message::Ptr result; + MailTemplates::reply(msg, [&] (const KMime::Message::Ptr &r) { + result = r; + }); + QTRY_VERIFY(result); + QCOMPARE(unquote(removeFirstLine(result->body())), QLatin1String("HTML text")); + } + + void testMultipartSigned() + { + auto msg = readMail("openpgp-signed-mailinglist.mbox"); + KMime::Message::Ptr result; + MailTemplates::reply(msg, [&] (const KMime::Message::Ptr &r) { + result = r; + }); + QTRY_VERIFY(result); + auto content = normalize(removeFirstLine(result->body())); + QVERIFY(!content.isEmpty()); + QEXPECT_FAIL("", "Not implemented yet.", Continue); + QVERIFY(content.contains("i noticed a new branch")); + } + +}; + +QTEST_MAIN(MailTemplateTest) +#include "mailtemplatetest.moc" -- cgit v1.2.3