aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlengine
diff options
context:
space:
mode:
authorRichard Weickelt <richard@weickelt.de>2018-05-31 21:41:47 +0200
committerRichard Weickelt <richard@weickelt.de>2018-06-21 18:00:11 +0000
commit73fdeb9c1c70079e54104c93811b5d7ff9e4ee0b (patch)
treec6c170c7a27fa8fa1715c035515c3ba849c097c9 /tests/auto/qml/qqmlengine
parentf44782d0cdbdb800d9c31d5aff712fbf29d52edc (diff)
Provide API to access singletons associated with a QQmlEngine
This patch adds allows C++ code to retrieve the instance of a registered singleton type. Until now this required a deturn via QML expression. Two methods are added to QQmlEngine: A generic one that encapsulates all singleton objects in a QJSValue and a template function for QObject-derived singleton types. An additional convenience function is added to query the QML type id. This function may also be used for other purposes in the future. [ChangeLog][QtQml][QQmlEngine] Added API to access singletons associated with a QQmlEngine. Task-number: QTBUG-39970 Change-Id: I67c132ede35f80b9aaf1c5e5456715cf4f1b0848 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmlengine')
-rw-r--r--tests/auto/qml/qqmlengine/tst_qqmlengine.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp
index f179093a3d..f58ae38264 100644
--- a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp
+++ b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp
@@ -79,6 +79,7 @@ private slots:
void componentFromEval();
void qrcUrls();
void cppSignalAndEval();
+ void singletonInstance();
public slots:
QObject *createAQObjectForOwnershipTest ()
@@ -952,6 +953,96 @@ void tst_qqmlengine::cppSignalAndEval()
QCOMPARE(object->property("r"), 1.1234);
}
+class CppSingleton : public QObject {
+ Q_OBJECT
+public:
+ CppSingleton() {}
+
+ static QObject *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
+ {
+ Q_UNUSED(qmlEngine);
+ Q_UNUSED(jsEngine);
+ return new CppSingleton();
+ }
+};
+
+class JsSingleton : public QObject {
+ Q_OBJECT
+public:
+ JsSingleton() {}
+
+ static QJSValue create(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
+ {
+ Q_UNUSED(qmlEngine);
+ QJSValue value = jsEngine->newQObject(new JsSingleton());
+ return value;
+ }
+};
+
+class SomeQObjectClass : public QObject {
+ Q_OBJECT
+public:
+ SomeQObjectClass() : QObject(nullptr){}
+};
+
+void tst_qqmlengine::singletonInstance()
+{
+ QQmlEngine engine;
+
+ int cppSingletonTypeId = qmlRegisterSingletonType<CppSingleton>("Test", 1, 0, "CppSingleton", &CppSingleton::create);
+ int jsValueSingletonTypeId = qmlRegisterSingletonType("Test", 1, 0, "JsSingleton", &JsSingleton::create);
+
+ {
+ // Cpp QObject singleton type
+ QJSValue value = engine.singletonInstance<QJSValue>(cppSingletonTypeId);
+ QVERIFY(!value.isUndefined());
+ QVERIFY(value.isQObject());
+ QObject *instance = value.toQObject();
+ QVERIFY(instance);
+ QCOMPARE(instance->metaObject()->className(), "CppSingleton");
+ }
+
+ {
+ // QJSValue QObject singleton type
+ QJSValue value = engine.singletonInstance<QJSValue>(jsValueSingletonTypeId);
+ QVERIFY(!value.isUndefined());
+ QVERIFY(value.isQObject());
+ QObject *instance = value.toQObject();
+ QVERIFY(instance);
+ QCOMPARE(instance->metaObject()->className(), "JsSingleton");
+ }
+
+ {
+ // Invalid types
+ QJSValue value;
+ value = engine.singletonInstance<QJSValue>(-4711);
+ QVERIFY(value.isUndefined());
+ value = engine.singletonInstance<QJSValue>(1701);
+ QVERIFY(value.isUndefined());
+ }
+
+ {
+ // Valid, but non-singleton type
+ int typeId = qmlRegisterType<CppSingleton>("Test", 1, 0, "NotASingleton");
+ QJSValue value = engine.singletonInstance<QJSValue>(typeId);
+ QVERIFY(value.isUndefined());
+ }
+
+ {
+ // Cpp QObject singleton type
+ CppSingleton *instance = engine.singletonInstance<CppSingleton*>(cppSingletonTypeId);
+ QVERIFY(instance);
+ QCOMPARE(instance->metaObject()->className(), "CppSingleton");
+ QCOMPARE(instance, engine.singletonInstance<QJSValue>(cppSingletonTypeId).toQObject());
+ }
+
+ {
+ // Wrong destination type
+ SomeQObjectClass * instance = engine.singletonInstance<SomeQObjectClass*>(cppSingletonTypeId);
+ QVERIFY(!instance);
+ }
+}
+
QTEST_MAIN(tst_qqmlengine)
#include "tst_qqmlengine.moc"