summaryrefslogtreecommitdiffstats
path: root/tests/auto/tools
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2019-11-08 16:20:44 +0100
committerSimon Hausmann <simon.hausmann@qt.io>2020-03-18 15:42:58 +0100
commitd4f044533111fcfb34fe3a785eeb7af7fdbefbdd (patch)
tree1b2ef0c8695655e6ee4c1ae5f13a731738d1da96 /tests/auto/tools
parentb5f6a85d2745ab6ac97f593664d255906923e737 (diff)
Add support for exposing public QProperty members in the meta-object system
At the moment this makes the type as well as the setter/getter available through the meta-call as well as the ability to register observers and bindings. Only QProperty members that are annotated with Q_PROPERTY(type name) are made public through the meta-object. Change-Id: I16b98fd318122c722b85ce61e39975284e0c2404 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/tools')
-rw-r--r--tests/auto/tools/moc/allmocs_baseline_in.json16
-rw-r--r--tests/auto/tools/moc/tst_moc.cpp107
2 files changed, 120 insertions, 3 deletions
diff --git a/tests/auto/tools/moc/allmocs_baseline_in.json b/tests/auto/tools/moc/allmocs_baseline_in.json
index 9cc06dc7bc..77647329c9 100644
--- a/tests/auto/tools/moc/allmocs_baseline_in.json
+++ b/tests/auto/tools/moc/allmocs_baseline_in.json
@@ -1046,6 +1046,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "prop1",
"read": "getProp1",
"required": false,
@@ -1059,6 +1060,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "prop2",
"read": "getProp2",
"required": false,
@@ -1072,6 +1074,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "prop3",
"read": "getProp3",
"required": false,
@@ -1184,6 +1187,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "flags",
"read": "flags",
"required": false,
@@ -1210,6 +1214,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "flags",
"read": "flags",
"required": false,
@@ -1223,6 +1228,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "flagsList",
"read": "flagsList",
"required": false,
@@ -1747,6 +1753,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "blah",
"read": "blah",
"required": false,
@@ -1799,6 +1806,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "blah",
"read": "blah",
"required": false,
@@ -1974,6 +1982,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "gadgetPoperty",
"read": "gadgetPoperty",
"required": false,
@@ -1986,6 +1995,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "objectPoperty",
"read": "objectPoperty",
"required": false,
@@ -2011,6 +2021,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "nestedGadgetPoperty",
"read": "nestedGadgetPoperty",
"required": false,
@@ -2036,6 +2047,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "nestedObjectPoperty",
"read": "nestedObjectPoperty",
"required": false,
@@ -2169,6 +2181,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "gadgetPoperty",
"read": "gadgetPoperty",
"required": false,
@@ -2181,6 +2194,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "objectPoperty",
"read": "objectPoperty",
"required": false,
@@ -2206,6 +2220,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "nestedGadgetPoperty",
"read": "nestedGadgetPoperty",
"required": false,
@@ -2231,6 +2246,7 @@
"constant": false,
"designable": true,
"final": false,
+ "isQProperty": false,
"name": "nestedObjectPoperty",
"read": "nestedObjectPoperty",
"required": false,
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp
index 31fe68a64e..221e741647 100644
--- a/tests/auto/tools/moc/tst_moc.cpp
+++ b/tests/auto/tools/moc/tst_moc.cpp
@@ -723,6 +723,9 @@ private slots:
void mocJsonOutput();
void mocInclude();
void requiredProperties();
+ void qpropertyMembers();
+ void observerMetaCall();
+ void setQPRopertyBinding();
signals:
void sigWithUnsignedArg(unsigned foo);
@@ -1603,7 +1606,7 @@ void tst_Moc::warnOnPropertyWithoutREAD()
QVERIFY(!mocOut.isEmpty());
QString mocWarning = QString::fromLocal8Bit(proc.readAllStandardError());
QCOMPARE(mocWarning, header +
- QString(":36: Warning: Property declaration foo has no READ accessor function or associated MEMBER variable. The property will be invalid.\n"));
+ QString(":36: Warning: Property declaration foo has neither an associated QProperty<> member, nor a READ accessor function nor an associated MEMBER variable. The property will be invalid.\n"));
#else
QSKIP("Only tested on linux/gcc");
#endif
@@ -2087,7 +2090,7 @@ void tst_Moc::warnings_data()
<< QStringList()
<< 0
<< QString("IGNORE_ALL_STDOUT")
- << QString("standard input:1: Warning: Property declaration x has no READ accessor function or associated MEMBER variable. The property will be invalid.");
+ << QString("standard input:1: Warning: Property declaration x has neither an associated QProperty<> member, nor a READ accessor function nor an associated MEMBER variable. The property will be invalid.");
// This should output a warning
QTest::newRow("Duplicate property warning")
@@ -2103,7 +2106,7 @@ void tst_Moc::warnings_data()
<< (QStringList() << "-nn")
<< 0
<< QString("IGNORE_ALL_STDOUT")
- << QString("standard input:1: Warning: Property declaration x has no READ accessor function or associated MEMBER variable. The property will be invalid.");
+ << QString("standard input:1: Warning: Property declaration x has neither an associated QProperty<> member, nor a READ accessor function nor an associated MEMBER variable. The property will be invalid.");
// Passing "-nw" should suppress the warning
QTest::newRow("Invalid property warning with -nw")
@@ -4100,6 +4103,104 @@ void tst_Moc::requiredProperties()
QVERIFY(!notRequired.isRequired());
}
+class ClassWithQPropertyMembers : public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(int publicProperty)
+ Q_PROPERTY(int privateExposedProperty)
+public:
+
+ QProperty<int> publicProperty;
+ QProperty<int> notExposed;
+
+protected:
+ QProperty<int> protectedProperty;
+
+private:
+ QProperty<int> privateProperty;
+ QProperty<int> privateExposedProperty;
+};
+
+void tst_Moc::qpropertyMembers()
+{
+ const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject;
+
+ QCOMPARE(metaObject->propertyCount() - metaObject->superClass()->propertyCount(), 2);
+
+ QCOMPARE(metaObject->indexOfProperty("notExposed"), -1);
+
+ QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty"));
+ QVERIFY(prop.isValid());
+
+ QVERIFY(metaObject->property(metaObject->indexOfProperty("privateExposedProperty")).isValid());
+
+ ClassWithQPropertyMembers instance;
+
+ prop.write(&instance, 42);
+ QCOMPARE(instance.publicProperty.value(), 42);
+
+ instance.publicProperty.setValue(100);
+ QCOMPARE(prop.read(&instance).toInt(), 100);
+
+ QCOMPARE(prop.metaType(), QMetaType(QMetaType::Int));
+
+ QVERIFY(!prop.notifySignal().isValid());
+}
+
+
+
+void tst_Moc::observerMetaCall()
+{
+ const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject;
+ QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty"));
+ QVERIFY(prop.isValid());
+
+ ClassWithQPropertyMembers instance;
+
+ int observerCallCount = 0;
+
+ QProperty<int> dummy;
+ auto handler = dummy.onValueChanged([&observerCallCount]() {
+ ++observerCallCount;
+ });
+
+ {
+ void *argv[] = { &handler };
+ instance.qt_metacall(QMetaObject::RegisterQPropertyObserver, prop.propertyIndex(), argv);
+ }
+
+ instance.publicProperty.setValue(100);
+ QCOMPARE(observerCallCount, 1);
+ instance.publicProperty.setValue(101);
+ QCOMPARE(observerCallCount, 2);
+}
+
+
+
+void tst_Moc::setQPRopertyBinding()
+{
+ const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject;
+ QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty"));
+ QVERIFY(prop.isValid());
+
+ ClassWithQPropertyMembers instance;
+
+ bool bindingCalled = false;
+ auto binding = Qt::makePropertyBinding([&bindingCalled]() {
+ bindingCalled = true;
+ return 42;
+ });
+
+ {
+ void *argv[] = { &binding };
+ instance.qt_metacall(QMetaObject::SetQPropertyBinding, prop.propertyIndex(), argv);
+ }
+ QVERIFY(!bindingCalled); // not yet!
+
+ QCOMPARE(instance.publicProperty.value(), 42);
+ QVERIFY(bindingCalled); // but now it should've been called :)
+}
+
QTEST_MAIN(tst_Moc)
// the generated code must compile with QT_NO_KEYWORDS