diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-04-19 16:52:09 +0200 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-04-22 16:15:41 +0200 |
commit | d558ebf79b76e1a428a350d28950813a4db0755d (patch) | |
tree | 9e459a7c24fd167949b789141d29ac5f5480c7ff /tests/auto | |
parent | 6969496e0078f9f9df9bef817caad71cf5213e3a (diff) |
Add QObjectBindableProperyt::notify
This mirrors the functionality of QObjectCompatProperty::notify, and can
be useful to delay notifications until a class invariant has been
restored.
Change-Id: I1c16a0b1537a1b53d144c8abe48e546553edf877
Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index c403276464..5197be3912 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -82,6 +82,7 @@ private slots: void bindingValueReplacement(); void quntypedBindableApi(); void readonlyConstQBindable(); + void qobjectBindableManualNotify(); void testNewStuff(); void qobjectObservers(); @@ -1137,6 +1138,50 @@ public: Q_OBJECT_COMPAT_PROPERTY(MyQObject, int, compatData, &MyQObject::setCompat) }; + +void tst_QProperty::qobjectBindableManualNotify() +{ + // Given an object of type MyQObject, + MyQObject object; + // track its foo property's change count + auto bindable = object.bindableFoo(); + int fooChangeCount = 0; + auto changeHandler = bindable.onValueChanged([&](){++fooChangeCount;}); + // and how many changed signals it emits. + QSignalSpy fooChangedSpy(&object, &MyQObject::fooChanged); + + // If we bypass the bindings system, + object.fooData.setValueBypassingBindings(42); + // there is no change. + QCOMPARE(fooChangeCount, 0); + QCOMPARE(fooChangedSpy.count(), 0); + // Once we notify manually + object.fooData.notify(); + // observers are notified and the signal arrives. + QCOMPARE(fooChangeCount, 1); + QCOMPARE(fooChangedSpy.count(), 1); + + // If we set a binding + int i = 1; + object.fooData.setBinding([&](){return i;}); + // then the value changes + QCOMPARE(object.foo(), 1); + // and the change and signal count are incremented. + QCOMPARE(fooChangeCount, 2); + QCOMPARE(fooChangedSpy.count(), 2); + // Changing a non-property won't trigger any notification. + i = 2; + QCOMPARE(fooChangeCount, 2); + QCOMPARE(fooChangedSpy.count(), 2); + // Manually triggering the notification + object.fooData.notify(); + // increments the change count + QCOMPARE(fooChangeCount, 3); + QCOMPARE(fooChangedSpy.count(), 3); + // but doesn't actually cause a binding reevaluation. + QCOMPARE(object.foo(), 1); +} + void tst_QProperty::testNewStuff() { MyQObject testReadOnly; |