From 6434a87199b3259e9002570332beaa8caf5d2213 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 24 May 2016 09:20:02 +0200 Subject: A generic mailtest that can be applied to all resources that support mails. --- tests/CMakeLists.txt | 14 ++-- tests/mailtest.cpp | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/mailtest.h | 69 +++++++++++++++++++ 3 files changed, 262 insertions(+), 4 deletions(-) create mode 100644 tests/mailtest.cpp create mode 100644 tests/mailtest.h (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c9f7591..9de6ffa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,22 +10,28 @@ include_directories( add_definitions(-DTESTDATAPATH="${CMAKE_CURRENT_SOURCE_DIR}/data") +find_package(KF5 COMPONENTS REQUIRED Mime) + +add_library(sink_test SHARED testimplementations.cpp getrssusage.cpp mailtest.cpp) +qt5_use_modules(sink_test Core Test Concurrent) +target_link_libraries(sink_test sink libhawd KF5::Mime) + macro(manual_tests) foreach(_testname ${ARGN}) - add_executable(${_testname} ${_testname}.cpp testimplementations.cpp getrssusage.cpp) + add_executable(${_testname} ${_testname}.cpp) generate_flatbuffers(${_testname} calendar) qt5_use_modules(${_testname} Core Test Concurrent) - target_link_libraries(${_testname} sink libhawd) + target_link_libraries(${_testname} sink libhawd sink_test) endforeach(_testname) endmacro(manual_tests) macro(auto_tests) foreach(_testname ${ARGN}) - add_executable(${_testname} ${_testname}.cpp testimplementations.cpp getrssusage.cpp) + add_executable(${_testname} ${_testname}.cpp) generate_flatbuffers(${_testname} calendar) add_test(${_testname} ${_testname}) qt5_use_modules(${_testname} Core Test Concurrent) - target_link_libraries(${_testname} sink libhawd) + target_link_libraries(${_testname} sink libhawd sink_test) endforeach(_testname) endmacro(auto_tests) diff --git a/tests/mailtest.cpp b/tests/mailtest.cpp new file mode 100644 index 0000000..79a077c --- /dev/null +++ b/tests/mailtest.cpp @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include "mailtest.h" + +#include + +#include +#include + +#include "store.h" +#include "resourcecontrol.h" +#include "log.h" +#include "test.h" + +using namespace Sink; +using namespace Sink::ApplicationDomain; + +void MailTest::initTestCase() +{ + Test::initTest(); + Log::setDebugOutputLevel(Sink::Log::Trace); + resetTestEnvironment(); + auto resource = createResource(); + QVERIFY(!resource.identifier().isEmpty()); + + VERIFYEXEC(Store::create(resource)); + + mResourceInstanceIdentifier = resource.identifier(); +} + +void MailTest::cleanup() +{ + VERIFYEXEC(ResourceControl::shutdown(mResourceInstanceIdentifier)); + removeResourceFromDisk(mResourceInstanceIdentifier); +} + +void MailTest::init() +{ + qDebug(); + qDebug() << "-----------------------------------------"; + qDebug(); + VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier)); +} + +void MailTest::testCreateModifyDeleteFolder() +{ + QString name = "name"; + QByteArray icon = "icon"; + + auto folder = Folder::create(mResourceInstanceIdentifier); + folder.setName(name); + folder.setIcon(icon); + + VERIFYEXEC(Store::create(folder)); + VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); + { + auto job = Store::fetchAll(Query::RequestedProperties(QByteArrayList() << Folder::Name::name << Folder::Icon::name)) + .then>([=](const QList &folders) { + QCOMPARE(folders.size(), 1); + auto folder = *folders.first(); + QCOMPARE(folder.getName(), name); + QCOMPARE(folder.getIcon(), icon); + }); + VERIFYEXEC(job); + } + + QString name2 = "name2"; + QByteArray icon2 = "icon2"; + folder.setName(name2); + folder.setIcon(icon2); + + VERIFYEXEC(Store::modify(folder)); + VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); + { + auto job = Store::fetchAll(Query::RequestedProperties(QByteArrayList() << Folder::Name::name << Folder::Icon::name)) + .then>([=](const QList &folders) { + QCOMPARE(folders.size(), 1); + auto folder = *folders.first(); + QCOMPARE(folder.getName(), name2); + QCOMPARE(folder.getIcon(), icon2); + }); + VERIFYEXEC(job); + } + + VERIFYEXEC(Store::remove(folder)); + VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); + { + auto job = Store::fetchAll(Query::RequestedProperties(QByteArrayList() << Folder::Name::name << Folder::Icon::name)) + .then>([=](const QList &folders) { + QCOMPARE(folders.size(), 0); + }); + VERIFYEXEC(job); + } +} + +void MailTest::testCreateModifyDeleteMail() +{ + + const auto subject = QString::fromLatin1("Foobar"); + + Query query; + query.resources << mResourceInstanceIdentifier; + query.request().request(); + + auto folder = Folder::create(mResourceInstanceIdentifier); + folder.setName("folder"); + VERIFYEXEC(Store::create(folder)); + + auto message = KMime::Message::Ptr::create(); + message->subject(true)->fromUnicodeString(subject, "utf8"); + message->assemble(); + + auto mail = Mail::create(mResourceInstanceIdentifier); + mail.setMimeMessage(message->encodedContent()); + mail.setFolder(folder); + + VERIFYEXEC(Store::create(mail)); + VERIFYEXEC(ResourceControl::flushMessageQueue(query.resources)); + { + auto job = Store::fetchAll(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name)) + .then>([=](const QList &mails) { + QCOMPARE(mails.size(), 1); + auto mail = *mails.first(); + // QCOMPARE(mail.getSubject(), subject); + QCOMPARE(mail.getFolder(), folder.identifier()); + // TODO test access to mime message + + // return Store::remove(*mail) + // .then(ResourceControl::flushReplayQueue(query.resources)) // The change needs to be replayed already + // .then(ResourceControl::inspect(ResourceControl::Inspection::ExistenceInspection(*mail, false))); + }); + VERIFYEXEC(job); + } + + const auto subject2 = QString::fromLatin1("Foobar2"); + auto message2 = KMime::Message::Ptr::create(); + message2->subject(true)->fromUnicodeString(subject2, "utf8"); + message2->assemble(); + mail.setMimeMessage(message2->encodedContent()); + + VERIFYEXEC(Store::modify(mail)); + VERIFYEXEC(ResourceControl::flushMessageQueue(query.resources)); + { + auto job = Store::fetchAll(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name)) + .then>([=](const QList &mails) { + QCOMPARE(mails.size(), 1); + auto mail = *mails.first(); + QCOMPARE(mail.getFolder(), folder.identifier()); + // QCOMPARE(mail.getSubject(), subject); + // TODO test access to modified mime message + + }); + VERIFYEXEC(job); + } + + VERIFYEXEC(Store::remove(mail)); + VERIFYEXEC(ResourceControl::flushMessageQueue(query.resources)); + { + auto job = Store::fetchAll(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name)) + .then>([=](const QList &mails) { + QCOMPARE(mails.size(), 0); + }); + VERIFYEXEC(job); + } +} + +#include "mailtest.moc" diff --git a/tests/mailtest.h b/tests/mailtest.h new file mode 100644 index 0000000..0729a91 --- /dev/null +++ b/tests/mailtest.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#pragma once + +#include +#include + +#include + +#define ASYNCCOMPARE(actual, expected) \ +do {\ + if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\ + return KAsync::error(1, "Comparison failed.");\ +} while (0) + +#define ASYNCVERIFY(statement) \ +do {\ + if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\ + return KAsync::error(1, "Verify failed.");\ +} while (0) + +#define VERIFYEXEC(statement) \ +do {\ + auto result = statement.exec(); \ + result.waitForFinished(); \ + if (!QTest::qVerify(!result.errorCode(), #statement, "", __FILE__, __LINE__))\ + return;\ +} while (0) + +namespace Sink { + +class MailTest : public QObject +{ + Q_OBJECT + +protected: + QByteArray mResourceInstanceIdentifier; + + virtual void resetTestEnvironment() = 0; + virtual Sink::ApplicationDomain::SinkResource createResource() = 0; + virtual void removeResourceFromDisk(const QByteArray &mResourceInstanceIdentifier) = 0; + +private slots: + void initTestCase(); + void init(); + void cleanup(); + + void testCreateModifyDeleteFolder(); + void testCreateModifyDeleteMail(); +}; + +} + -- cgit v1.2.3