diff options
Diffstat (limited to 'tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp')
-rw-r--r-- | tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp | 186 |
1 files changed, 106 insertions, 80 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index 848226713c..0b05353bf0 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -95,7 +95,9 @@ private slots: void noFakeDependencies(); void bindablePropertyWithInitialization(); - void markDirty(); + void noDoubleNotification(); + void groupedNotifications(); + void groupedNotificationConsistency(); }; void tst_QProperty::functorBinding() @@ -129,8 +131,8 @@ void tst_QProperty::multipleDependencies() QProperty<int> sum; sum.setBinding([&]() { return firstDependency + secondDependency; }); - QCOMPARE(QPropertyBindingDataPointer::get(firstDependency).observerCount(), 0); - QCOMPARE(QPropertyBindingDataPointer::get(secondDependency).observerCount(), 0); + QCOMPARE(QPropertyBindingDataPointer::get(firstDependency).observerCount(), 1); + QCOMPARE(QPropertyBindingDataPointer::get(secondDependency).observerCount(), 1); QCOMPARE(sum.value(), int(3)); QCOMPARE(QPropertyBindingDataPointer::get(firstDependency).observerCount(), 1); @@ -260,12 +262,12 @@ void tst_QProperty::avoidDependencyAllocationAfterFirstEval() QCOMPARE(propWithBinding.value(), int(11)); - QVERIFY(QPropertyBindingDataPointer::get(propWithBinding).bindingPtr()); - QCOMPARE(QPropertyBindingDataPointer::get(propWithBinding).bindingPtr()->dependencyObserverCount, 2u); + QVERIFY(QPropertyBindingDataPointer::get(propWithBinding).binding()); + QCOMPARE(QPropertyBindingDataPointer::get(propWithBinding).binding()->dependencyObserverCount, 2u); firstDependency = 100; QCOMPARE(propWithBinding.value(), int(110)); - QCOMPARE(QPropertyBindingDataPointer::get(propWithBinding).bindingPtr()->dependencyObserverCount, 2u); + QCOMPARE(QPropertyBindingDataPointer::get(propWithBinding).binding()->dependencyObserverCount, 2u); } void tst_QProperty::boolProperty() @@ -484,23 +486,22 @@ class BindingLoopTester : public QObject void tst_QProperty::bindingLoop() { - QScopedPointer<QProperty<int>> firstProp; + QProperty<int> firstProp; QProperty<int> secondProp([&]() -> int { - return firstProp ? firstProp->value() : 0; + return firstProp.value(); }); QProperty<int> thirdProp([&]() -> int { return secondProp.value(); }); - firstProp.reset(new QProperty<int>()); - firstProp->setBinding([&]() -> int { - return secondProp.value(); + firstProp.setBinding([&]() -> int { + return secondProp.value() + thirdProp.value(); }); - QCOMPARE(thirdProp.value(), 0); - QCOMPARE(secondProp.binding().error().type(), QPropertyBindingError::BindingLoop); + thirdProp.setValue(10); + QCOMPARE(firstProp.binding().error().type(), QPropertyBindingError::BindingLoop); { @@ -1200,7 +1201,9 @@ void tst_QProperty::qobjectObservers() MyQObject object; int onValueChangedCalled = 0; { - auto handler = object.bindableFoo().onValueChanged([&onValueChangedCalled]() { ++onValueChangedCalled;}); + auto handler = object.bindableFoo().onValueChanged([&onValueChangedCalled]() { + ++onValueChangedCalled; + }); QCOMPARE(onValueChangedCalled, 0); object.setFoo(10); @@ -1606,80 +1609,103 @@ void tst_QProperty::bindablePropertyWithInitialization() QCOMPARE(tester.prop3().anotherValue, 20); } -class MarkDirtyTester : public QObject +void tst_QProperty::noDoubleNotification() { - Q_OBJECT -public: - Q_PROPERTY(int value1 READ value1 WRITE setValue1 BINDABLE bindableValue1) - Q_PROPERTY(int value2 READ value2 WRITE setValue1 BINDABLE bindableValue2) - Q_PROPERTY(int computed READ computed BINDABLE bindableComputed) - - inline static int staticValue = 0; + /* dependency graph for this test + x --> y means y depends on x + a-->b-->d + \ ^ + \->c--/ + */ + QProperty<int> a(0); + QProperty<int> b; + b.setBinding([&](){ return a.value(); }); + QProperty<int> c; + c.setBinding([&](){ return a.value(); }); + QProperty<int> d; + d.setBinding([&](){ return b.value() + c.value(); }); + int nNotifications = 0; + int expected = 0; + auto connection = d.subscribe([&](){ + ++nNotifications; + QCOMPARE(d.value(), expected); + }); + QCOMPARE(nNotifications, 1); + expected = 2; + a = 1; + QCOMPARE(nNotifications, 2); + expected = 4; + a = 2; + QCOMPARE(nNotifications, 3); +} - int value1() const {return m_value1;} - void setValue1(int val) {m_value1 = val;} - QBindable<int> bindableValue1() {return { &m_value1 };} +void tst_QProperty::groupedNotifications() +{ + QProperty<int> a(0); + QProperty<int> b; + b.setBinding([&](){ return a.value(); }); + QProperty<int> c; + c.setBinding([&](){ return a.value(); }); + QProperty<int> d; + QProperty<int> e; + e.setBinding([&](){ return b.value() + c.value() + d.value(); }); + int nNotifications = 0; + int expected = 0; + auto connection = e.subscribe([&](){ + ++nNotifications; + QCOMPARE(e.value(), expected); + }); + QCOMPARE(nNotifications, 1); + + expected = 2; + Qt::beginPropertyUpdateGroup(); + a = 1; + QCOMPARE(b.value(), 0); + QCOMPARE(c.value(), 0); + QCOMPARE(d.value(), 0); + QCOMPARE(nNotifications, 1); + Qt::endPropertyUpdateGroup(); + QCOMPARE(b.value(), 1); + QCOMPARE(c.value(), 1); + QCOMPARE(e.value(), 2); + QCOMPARE(nNotifications, 2); + + expected = 7; + Qt::beginPropertyUpdateGroup(); + a = 2; + d = 3; + QCOMPARE(b.value(), 1); + QCOMPARE(c.value(), 1); + QCOMPARE(d.value(), 3); + QCOMPARE(nNotifications, 2); + Qt::endPropertyUpdateGroup(); + QCOMPARE(b.value(), 2); + QCOMPARE(c.value(), 2); + QCOMPARE(e.value(), 7); + QCOMPARE(nNotifications, 3); - int value2() const {return m_value2;} - void setValue2(int val) { m_value2.setValue(val); } - QBindable<int> bindableValue2() {return { &m_value2 };} - int computed() const { return staticValue + m_value1; } - QBindable<int> bindableComputed() {return {&m_computed};} +} - void incrementStaticValue() { - ++staticValue; - m_computed.markDirty(); - } +void tst_QProperty::groupedNotificationConsistency() +{ + QProperty<int> i(0); + QProperty<int> j(0); + bool areEqual = true; - void markValue1Dirty() { - m_value1.markDirty(); - } + auto observer = i.onValueChanged([&](){ + areEqual = i == j; + }); - void markValue2Dirty() { - m_value2.markDirty(); - } -private: - Q_OBJECT_BINDABLE_PROPERTY(MarkDirtyTester, int, m_value1, nullptr) - Q_OBJECT_COMPAT_PROPERTY(MarkDirtyTester, int, m_value2, &MarkDirtyTester::setValue2) - Q_OBJECT_COMPUTED_PROPERTY(MarkDirtyTester, int, m_computed, &MarkDirtyTester::computed) -}; + i = 1; + j = 1; + QVERIFY(!areEqual); // value changed runs before j = 1 -void tst_QProperty::markDirty() -{ - { - QProperty<int> testProperty; - int changeCounter = 0; - auto handler = testProperty.onValueChanged([&](){++changeCounter;}); - testProperty.markDirty(); - QCOMPARE(changeCounter, 1); - } - { - MarkDirtyTester dirtyTester; - int computedChangeCounter = 0; - int value1ChangeCounter = 0; - auto handler = dirtyTester.bindableComputed().onValueChanged([&](){ - computedChangeCounter++; - }); - auto handler2 = dirtyTester.bindableValue1().onValueChanged([&](){ - value1ChangeCounter++; - }); - dirtyTester.incrementStaticValue(); - QCOMPARE(computedChangeCounter, 1); - QCOMPARE(dirtyTester.computed(), 1); - dirtyTester.markValue1Dirty(); - QCOMPARE(value1ChangeCounter, 1); - QCOMPARE(computedChangeCounter, 1); - } - { - MarkDirtyTester dirtyTester; - int changeCounter = 0; - auto handler = dirtyTester.bindableValue2().onValueChanged([&](){ - changeCounter++; - }); - dirtyTester.markValue2Dirty(); - QCOMPARE(changeCounter, 1); - } + Qt::beginPropertyUpdateGroup(); + i = 2; + j = 2; + Qt::endPropertyUpdateGroup(); + QVERIFY(areEqual); // value changed runs after everything has been evaluated } QTEST_MAIN(tst_QProperty); |