From d558ebf79b76e1a428a350d28950813a4db0755d Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Mon, 19 Apr 2021 16:52:09 +0200 Subject: 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 Reviewed-by: Ivan Solovev Reviewed-by: Lars Knoll --- .../corelib/kernel/qproperty/tst_qproperty.cpp | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'tests/auto') 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; -- cgit v1.2.3