[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kube/develop] framework/src: Fixed html to plaintext conversion
From:       Christian Mollekopf <null () kde ! org>
Date:       2017-06-30 18:01:23
Message-ID: E1dR0EN-0000Z4-P6 () code ! kde ! org
[Download RAW message or body]

Git commit ab912af8158028206cf43b0a6ca0eac8eef040f4 by Christian Mollekopf.
Committed on 30/06/2017 at 14:23.
Pushed by cmollekopf into branch 'develop'.

Fixed html to plaintext conversion

M  +1    -0    framework/src/CMakeLists.txt
M  +6    -4    framework/src/domain/mime/mailtemplates.cpp
A  +15   -0    framework/src/domain/mime/tests/CMakeLists.txt
A  +99   -0    framework/src/domain/mime/tests/mailtemplatetest.cpp     [License: \
UNKNOWN]  *

The files marked with a * at the end have a non valid license. Please read: \
http://techbase.kde.org/Policies/Licensing_Policy and use the headers which are \
listed at that page.


https://commits.kde.org/kube/ab912af8158028206cf43b0a6ca0eac8eef040f4

diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt
index 1c53530..b935fbe 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 d41039d..c202af8 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 0000000..4fad00a
--- /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 0000000..18f315f
--- /dev/null
+++ b/framework/src/domain/mime/tests/mailtemplatetest.cpp
@@ -0,0 +1,99 @@
+#include <QTest>
+#include <QDebug>
+#include <QSignalSpy>
+#include <functional>
+#include <QStandardPaths>
+#include <QDir>
+#include <QtWebEngine>
+
+#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"


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic