summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2020-06-18 16:55:48 +0200
committerFabian Kosmale <fabian.kosmale@qt.io>2020-06-25 14:11:56 +0200
commit6a24ac7c4e0a7737b0cd0738c4e6fe8b9ac589ca (patch)
tree52e1f7b48a57da297d5eef19396b5c30408a1062 /tests
parentc2fb27f054b228311fdba71f2a49cb09ae2a8fc8 (diff)
QNotifiedProperty: pass old value to callback if requested
Check at compile time whether the static callback takes an argument (which has to be of the same time as the type of the property). If so, retrieve the old value and pass it to the callback. Change-Id: Ib1c4c9e05b826b6be492b03f66fa72ad015963ee Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
index e606d121ec..7200bf176e 100644
--- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
+++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
@@ -72,6 +72,7 @@ private slots:
void multipleObservers();
void propertyAlias();
void notifiedProperty();
+ void notifiedPropertyWithOldValueCallback();
};
void tst_QProperty::functorBinding()
@@ -779,6 +780,15 @@ struct ClassWithNotifiedProperty
QNotifiedProperty<int, &ClassWithNotifiedProperty::callback> property;
};
+struct ClassWithNotifiedProperty2
+{
+ QVector<int> recordedValues;
+
+ void callback(int oldValue) { recordedValues << oldValue; }
+
+ QNotifiedProperty<int, &ClassWithNotifiedProperty2::callback> property;
+};
+
void tst_QProperty::notifiedProperty()
{
ClassWithNotifiedProperty instance;
@@ -854,6 +864,18 @@ void tst_QProperty::notifiedProperty()
QCOMPARE(subscribedCount, 10);
}
+void tst_QProperty::notifiedPropertyWithOldValueCallback()
+{
+ ClassWithNotifiedProperty2 instance;
+ instance.property.setValue(&instance, 1);
+ instance.property.setBinding(&instance, [](){return 2;});
+ instance.property.setBinding(&instance, [](){return 3;});
+ instance.property.setValue(&instance, 4);
+ QVector<int> expected {0, 1, 2, 3};
+ QCOMPARE(instance.recordedValues, expected);
+ QCOMPARE(instance.property.value(), 4);
+}
+
QTEST_MAIN(tst_QProperty);
#include "tst_qproperty.moc"