diff options
author | Richard Weickelt <richard@weickelt.de> | 2018-05-19 15:30:35 +0200 |
---|---|---|
committer | Richard Weickelt <richard@weickelt.de> | 2018-06-08 20:24:49 +0000 |
commit | 6e16cd5bd8843c6dae219c5fe1493969a51198d3 (patch) | |
tree | 742ad3b945f81da34e275974eff974cc12153617 /tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | |
parent | af46cc19937dd8d33538e62ad5ebfed8e958a8f5 (diff) |
Ensure identical behavior for singleton types defined in C++ and QML
Singleton types defined in C++ have no QML context. Therefore, both
qmlContext(obj) and qmlEngine(obj) return zero. Although documented,
this behavior is surprising and inconsistent with singleton types defined
in QML. The current behavior was decided upon in QTBUG-23116.
This patch puts C++ singleton types into a sub-context of the root context
by default, just like it is the case for QML-defined singleton types. This
doesn't cause any harm, but avoids surprises. It also fixes a bug in
QmlTypeWrapper that returned an invalid QVariant for QJSValue singletons.
Task-number: QTBUG-38583
Change-Id: Id1d48ecdc49f0e22714857a1b49b457885889e5e
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp')
-rw-r--r-- | tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 400c28928a..39f973f3fd 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -246,6 +246,8 @@ private slots: void compositeSingletonRegistered(); void compositeSingletonCircular(); + void singletonsHaveContextAndEngine(); + void customParserBindingScopes(); void customParserEvaluateEnum(); void customParserProperties(); @@ -3923,11 +3925,13 @@ void tst_qqmllanguage::getSingletonInstance(QObject* o, const char* propertyName return; QVariant variant = o->property(propertyName); - QVERIFY(variant.userType() == qMetaTypeId<QObject *>()); + QVERIFY(variant.isValid()); QObject *singleton = nullptr; - if (variant.canConvert<QObject*>()) + if (variant.userType() == qMetaTypeId<QObject *>()) singleton = variant.value<QObject*>(); + else if (variant.userType() == qMetaTypeId<QJSValue>()) + singleton = variant.value<QJSValue>().toQObject(); QVERIFY(singleton != nullptr); *result = singleton; @@ -4232,6 +4236,24 @@ void tst_qqmllanguage::compositeSingletonCircular() QCOMPARE(o->property("value").toInt(), 2); } +void tst_qqmllanguage::singletonsHaveContextAndEngine() +{ + QObject *qmlSingleton = nullptr; + getSingletonInstance(engine, "singletonTest18.qml", "qmlSingleton", &qmlSingleton); + QVERIFY(qmlContext(qmlSingleton)); + QCOMPARE(qmlEngine(qmlSingleton), &engine); + + QObject *jsSingleton = nullptr; + getSingletonInstance(engine, "singletonTest18.qml", "jsSingleton", &jsSingleton); + QVERIFY(qmlContext(jsSingleton)); + QCOMPARE(qmlEngine(jsSingleton), &engine); + + QObject *cppSingleton = nullptr; + getSingletonInstance(engine, "singletonTest18.qml", "cppSingleton", &cppSingleton); + QVERIFY(qmlContext(cppSingleton)); + QCOMPARE(qmlEngine(cppSingleton), &engine); +} + void tst_qqmllanguage::customParserBindingScopes() { QQmlComponent component(&engine, testFile("customParserBindingScopes.qml")); |