From 3ac6cf5ffae2247719730f328d1363c498e4ee83 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 8 Jan 2018 10:34:07 +0100 Subject: Dynamically setup initial test state --- components/kube/tests/tst_composerview.qml | 34 +++++++++++++++-- tests/CMakeLists.txt | 5 ++- tests/kubetestrunner.cpp | 22 +++++------ tests/teststore.cpp | 59 ++++++++++++++++++++++++++++++ tests/teststore.h | 31 ++++++++++++++++ 5 files changed, 134 insertions(+), 17 deletions(-) create mode 100644 tests/teststore.cpp create mode 100644 tests/teststore.h diff --git a/components/kube/tests/tst_composerview.qml b/components/kube/tests/tst_composerview.qml index b7b6b3df..d9cd2dfb 100644 --- a/components/kube/tests/tst_composerview.qml +++ b/components/kube/tests/tst_composerview.qml @@ -21,6 +21,7 @@ import QtQuick 2.7 import QtTest 1.0 import "../qml" import org.kube.framework 1.0 as Kube +import org.kube.test 1.0 TestCase { id: testCase @@ -29,16 +30,20 @@ TestCase { name: "ComposerView" when: windowShown - ComposerView { - id: composer - focus: true + Component { + id:composerComponent + ComposerView { + focus: true + } } function test_1start() { + var composer = createTemporaryObject(composerComponent, testCase, {}) verify(composer) } function test_2verifyInitialFocus() { + var composer = createTemporaryObject(composerComponent, testCase, {}) var newMailButton = findChild(composer, "newMailButton"); verify(newMailButton) verify(newMailButton.activeFocus) @@ -55,6 +60,29 @@ TestCase { } function test_3sendMessage() { + var initialState = { + accounts: [{ + id: "account1", + }], + identities: [{ + account: "account1", + name: "Test Identity", + address: "identity@example.org" + }], + resources: [{ + id: "resource1", + account: "account1", + type: "dummy" + }, + { + id: "resource2", + account: "account1", + type: "mailtransport" + }] + } + TestStore.setup(initialState) + var composer = createTemporaryObject(composerComponent, testCase, {}) + var domainObjectController = controllerComponent.createObject(null, {blocking: true}) var mail = { type: "mail", diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9ce7915c..ca394d9e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,9 +1,10 @@ -find_package(Qt5 REQUIRED NO_MODULE COMPONENTS QuickTest Network) +find_package(Qt5 REQUIRED NO_MODULE COMPONENTS QuickTest Network Quick) find_package(Sink CONFIG REQUIRED) find_package(KAsync CONFIG REQUIRED) -add_executable(kubetestrunner kubetestrunner.cpp) +add_executable(kubetestrunner kubetestrunner.cpp teststore.cpp) target_link_libraries(kubetestrunner Qt5::QuickTest + Qt5::Quick sink ) diff --git a/tests/kubetestrunner.cpp b/tests/kubetestrunner.cpp index 3bf06d44..dd022091 100644 --- a/tests/kubetestrunner.cpp +++ b/tests/kubetestrunner.cpp @@ -17,25 +17,23 @@ 02110-1301, USA. */ #include +#include #include -#include +#include "teststore.h" + +static QObject *teststore(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + return new Kube::TestStore; +} int main(int argc, char **argv) { QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true); Sink::Test::initTest(); - auto resource = Sink::ApplicationDomain::DummyResource::create("account1"); - Sink::Store::create(resource).exec().waitForFinished(); - - auto transportResource = Sink::ApplicationDomain::MailtransportResource::create("account1"); - Sink::Store::create(transportResource).exec().waitForFinished(); - - auto identity = Sink::ApplicationDomain::Identity{}; - identity.setAccount("account1"); - identity.setAddress("identity@example.org"); - identity.setName("Identity Name"); - Sink::Store::create(identity).exec().waitForFinished(); + qmlRegisterSingletonType("org.kube.test", 1, 0, "TestStore", teststore); QTEST_ADD_GPU_BLACKLIST_SUPPORT QTEST_SET_MAIN_SOURCE_PATH diff --git a/tests/teststore.cpp b/tests/teststore.cpp new file mode 100644 index 00000000..a8cd0b2d --- /dev/null +++ b/tests/teststore.cpp @@ -0,0 +1,59 @@ +/* + Copyright (c) 2018 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#include "teststore.h" + +#include + +#include +#include + +using namespace Kube; + +static void iterateOverObjects(const QVariantList &list, std::function callback) +{ + for (const auto &entry : list) { + auto object = entry.toMap(); + callback(object); + } +} + +void TestStore::setup(const QVariantMap &map) +{ + iterateOverObjects(map.value("resources").toList(), [] (const QVariantMap &object) { + auto resource = [&] { + if (object["type"] == "dummy") { + return Sink::ApplicationDomain::DummyResource::create(object["account"].toByteArray()); + } + if (object["type"] == "mailtransport") { + return Sink::ApplicationDomain::MailtransportResource::create(object["account"].toByteArray()); + } + Q_ASSERT(false); + return Sink::ApplicationDomain::SinkResource{}; + }(); + Sink::Store::create(resource).exec().waitForFinished(); + }); + + iterateOverObjects(map.value("identities").toList(), [] (const QVariantMap &object) { + auto identity = Sink::ApplicationDomain::Identity{}; + identity.setAccount(object["account"].toByteArray()); + identity.setAddress(object["address"].toString()); + identity.setName(object["name"].toString()); + Sink::Store::create(identity).exec().waitForFinished(); + }); +} diff --git a/tests/teststore.h b/tests/teststore.h new file mode 100644 index 00000000..f0578ca5 --- /dev/null +++ b/tests/teststore.h @@ -0,0 +1,31 @@ +/* + Copyright (c) 2018 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#pragma once + +#include + +namespace Kube { + +class TestStore : public QObject { + Q_OBJECT +public: + Q_INVOKABLE void setup(const QVariantMap &); +}; + +} -- cgit v1.2.3