From 1b8bb2933c2809ee9032656da65052a7d5339bfd Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Sun, 29 Jul 2018 11:21:22 +0200 Subject: [PATCH] added binding tests --- src/mvvmcore/binding.cpp | 5 + src/mvvmcore/binding.h | 1 + tests/auto/mvvmcore/binding/binding.pro | 11 ++ tests/auto/mvvmcore/binding/tst_binding.cpp | 147 ++++++++++++++++++ tests/auto/mvvmcore/mvvmcore.pro | 3 +- .../serviceregistry/serviceregistry.pro | 10 +- 6 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 tests/auto/mvvmcore/binding/binding.pro create mode 100644 tests/auto/mvvmcore/binding/tst_binding.cpp diff --git a/src/mvvmcore/binding.cpp b/src/mvvmcore/binding.cpp index b0ef260..3819f33 100644 --- a/src/mvvmcore/binding.cpp +++ b/src/mvvmcore/binding.cpp @@ -100,6 +100,11 @@ bool Binding::isValid() const return d; } +Binding::operator bool() const +{ + return d; +} + void Binding::unbind() { if(d) { diff --git a/src/mvvmcore/binding.h b/src/mvvmcore/binding.h index 9f2513f..13ac327 100644 --- a/src/mvvmcore/binding.h +++ b/src/mvvmcore/binding.h @@ -33,6 +33,7 @@ public: //! Specifies whether the binding is a valid and active binding bool isValid() const; + operator bool() const; //! Remove the binding by disconnecting all change signals Q_INVOKABLE void unbind(); diff --git a/tests/auto/mvvmcore/binding/binding.pro b/tests/auto/mvvmcore/binding/binding.pro new file mode 100644 index 0000000..37b1f04 --- /dev/null +++ b/tests/auto/mvvmcore/binding/binding.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +QT += testlib mvvmcore +QT -= gui +CONFIG += console +CONFIG -= app_bundle + +TARGET = tst_binding + +SOURCES += \ + tst_binding.cpp diff --git a/tests/auto/mvvmcore/binding/tst_binding.cpp b/tests/auto/mvvmcore/binding/tst_binding.cpp new file mode 100644 index 0000000..113898f --- /dev/null +++ b/tests/auto/mvvmcore/binding/tst_binding.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +using namespace QtMvvm; + +class BindingTest : public QObject +{ + Q_OBJECT + + Q_PROPERTY(int vmProp MEMBER vmProp NOTIFY vmPropChanged) + Q_PROPERTY(int vProp MEMBER vProp NOTIFY vPropChanged) + +Q_SIGNALS: + void vmPropChanged(int vmProp); + void vPropChanged(int vProp); + + void triggerVm(); + void triggerV(); + +private Q_SLOTS: + void testSingleInit(); + void testOneWayToView(); + void testOneWayToViewModel(); + void testTwoWay(); + void testCustomChangeSignals(); + +private: + int vmProp = 0; + int vProp = 0; +}; + +void BindingTest::testSingleInit() +{ + vmProp = 42; + vProp = 0; + auto binding = bind(this, "vmProp", this, "vProp", Binding::SingleInit); + QVERIFY(binding); + QCOMPARE(vmProp, 42); + QCOMPARE(vProp, vmProp); + + vmProp = 33; + emit vmPropChanged(vmProp); + QCOMPARE(vProp, 42); + + emit vPropChanged(vProp); + QCOMPARE(vmProp, 33); + + binding.unbind(); + QVERIFY(!binding.isValid()); +} + +void BindingTest::testOneWayToView() +{ + vmProp = 13; + vProp = 0; + auto binding = bind(this, "vmProp", this, "vProp", Binding::OneWayToView); + QVERIFY(binding); + QCOMPARE(vmProp, 13); + QCOMPARE(vProp, vmProp); + + vmProp = 33; + emit vmPropChanged(vmProp); + QCOMPARE(vProp, vmProp); + + vProp = 13; + emit vPropChanged(vProp); + QCOMPARE(vmProp, 33); + + binding.unbind(); + QVERIFY(!binding.isValid()); +} + +void BindingTest::testOneWayToViewModel() +{ + vmProp = 0; + vProp = 11; + auto binding = bind(this, "vmProp", this, "vProp", Binding::OneWayToViewModel); + QVERIFY(binding); + QCOMPARE(vmProp, 0); + QCOMPARE(vProp, 11); + + vmProp = 55; + emit vmPropChanged(vmProp); + QCOMPARE(vProp, 11); + + vProp = 5; + emit vPropChanged(vProp); + QCOMPARE(vmProp, vProp); + + binding.unbind(); + QVERIFY(!binding.isValid()); +} + +void BindingTest::testTwoWay() +{ + vmProp = 77; + vProp = 0; + auto binding = bind(this, "vmProp", this, "vProp", Binding::TwoWay); + QVERIFY(binding); + QCOMPARE(vmProp, 77); + QCOMPARE(vProp, vmProp); + + vmProp = 12; + emit vmPropChanged(vmProp); + QCOMPARE(vProp, 12); + QCOMPARE(vProp, vmProp); + + vProp = 9; + emit vPropChanged(vProp); + QCOMPARE(vmProp, 9); + QCOMPARE(vmProp, vProp); + + binding.unbind(); + QVERIFY(!binding.isValid()); +} + +void BindingTest::testCustomChangeSignals() +{ + vmProp = 16; + vProp = 0; + auto binding = bind(this, "vmProp", this, "vProp", Binding::TwoWay, "triggerVm()", "triggerV()"); + QVERIFY(binding); + QCOMPARE(vmProp, 16); + QCOMPARE(vProp, vmProp); + + vmProp = 8; + emit vmPropChanged(vmProp); + QCOMPARE(vProp, 16); + emit triggerVm(); + QCOMPARE(vProp, 8); + QCOMPARE(vProp, vmProp); + + vProp = 99; + emit vPropChanged(vProp); + QCOMPARE(vmProp, 8); + emit triggerV(); + QCOMPARE(vmProp, 99); + QCOMPARE(vmProp, vProp); + + binding.unbind(); + QVERIFY(!binding.isValid()); +} + +QTEST_MAIN(BindingTest) + +#include "tst_binding.moc" diff --git a/tests/auto/mvvmcore/mvvmcore.pro b/tests/auto/mvvmcore/mvvmcore.pro index 7121cc0..776a671 100644 --- a/tests/auto/mvvmcore/mvvmcore.pro +++ b/tests/auto/mvvmcore/mvvmcore.pro @@ -3,7 +3,8 @@ TEMPLATE = subdirs SUBDIRS += \ settingsgenerator \ serviceregistry \ - serviceregistrytestplugin + serviceregistrytestplugin \ + binding serviceregistry.depends += serviceregistrytestplugin diff --git a/tests/auto/mvvmcore/serviceregistry/serviceregistry.pro b/tests/auto/mvvmcore/serviceregistry/serviceregistry.pro index 58178e7..df58df2 100644 --- a/tests/auto/mvvmcore/serviceregistry/serviceregistry.pro +++ b/tests/auto/mvvmcore/serviceregistry/serviceregistry.pro @@ -7,16 +7,16 @@ CONFIG -= app_bundle TARGET = tst_serviceregistry -SOURCES += \ - tst_serviceregistry.cpp \ - testservice.cpp \ - testobject.cpp - HEADERS += \ testinterface.h \ testservice.h \ testobject.h \ plugintestinterface.h +SOURCES += \ + tst_serviceregistry.cpp \ + testservice.cpp \ + testobject.cpp + load(qt_build_paths) DEFINES += PLUGIN_TESTDIR=\\\"$$MODULE_BASE_OUTDIR/tests/plugins\\\"