From cea8b5832c56e92aba2b028afc965ddcf384d37b Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Mon, 30 Nov 2020 10:40:00 +0100 Subject: [PATCH] QProperty: add test case for spurious dependency issue Task-number: QTBUG-88999 Pick-to: 6.0 Change-Id: Ifcbf23fedfb795771550762dfed8fc38bce65794 Reviewed-by: Lars Knoll --- .../kernel/qproperty/tst_qproperty.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index 31807bf1a7..d824d39941 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -87,6 +87,8 @@ private slots: void modifyObserverListWhileIterating(); void compatPropertyNoDobuleNotification(); + + void noFakeDependencies(); }; void tst_QProperty::functorBinding() @@ -1353,6 +1355,56 @@ void tst_QProperty::compatPropertyNoDobuleNotification() QCOMPARE(counter, 1); } +class FakeDependencyCreator : public QObject +{ + Q_OBJECT + + Q_PROPERTY(int prop1 READ prop1 WRITE setProp1 NOTIFY prop1Changed BINDABLE bindableProp1) + Q_PROPERTY(int prop2 READ prop2 WRITE setProp2 NOTIFY prop2Changed BINDABLE bindableProp2) + Q_PROPERTY(int prop3 READ prop3 WRITE setProp3 NOTIFY prop3Changed BINDABLE bindableProp3) + +signals: + void prop1Changed(); + void prop2Changed(); + void prop3Changed(); + +public: + void setProp1(int val) { prop1Data = val; emit prop1Changed();} + void setProp2(int val) { prop2Data = val; emit prop2Changed();} + void setProp3(int val) { prop3Data = val; emit prop3Changed();} + + int prop1() { return prop1Data; } + int prop2() { return prop2Data; } + int prop3() { return prop3Data; } + + QBindable bindableProp1() { return QBindable(&prop1Data); } + QBindable bindableProp2() { return QBindable(&prop2Data); } + QBindable bindableProp3() { return QBindable(&prop3Data); } + +private: + Q_OBJECT_COMPAT_PROPERTY(FakeDependencyCreator, int, prop1Data, &FakeDependencyCreator::setProp1); + Q_OBJECT_COMPAT_PROPERTY(FakeDependencyCreator, int, prop2Data, &FakeDependencyCreator::setProp2); + Q_OBJECT_COMPAT_PROPERTY(FakeDependencyCreator, int, prop3Data, &FakeDependencyCreator::setProp3); +}; + +void tst_QProperty::noFakeDependencies() +{ + FakeDependencyCreator fdc; + int bindingFunctionCalled = 0; + fdc.bindableProp1().setBinding([&]() -> int {++bindingFunctionCalled; return fdc.prop2();}); + fdc.setProp2(42); + QCOMPARE(fdc.prop1(), 42); // basic binding works + + int slotCounter = 0; + QObject::connect(&fdc, &FakeDependencyCreator::prop1Changed, &fdc, [&](){ (void) fdc.prop3(); ++slotCounter;}); + fdc.setProp2(13); + QCOMPARE(slotCounter, 1); // sanity check + int old = bindingFunctionCalled; + fdc.setProp3(100); + QEXPECT_FAIL("", "Known to create a spurious dependency", Continue); + QCOMPARE(old, bindingFunctionCalled); +} + QTEST_MAIN(tst_QProperty); #include "tst_qproperty.moc"