aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-01-15 09:18:14 +0100
committerUlf Hermann <ulf.hermann@qt.io>2020-01-15 14:09:11 +0100
commitd9a02d5c834b57f0da781c7dcf8d74e3b4ce1314 (patch)
tree6e3a84437c6c10b9861603b2dc4b4e9bcbd0732a /tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
parentc5a8dc153b3d3218360184837b098ba09018d982 (diff)
Test that we can override an uncreatable singleton type with an instance
This is the most prominent use case for singleton instances. You register a type for which you cannot manage the life cycle, but you have one instance of it. That instance can now be registered at runtime to override the static type registration. Change-Id: I647b8df13f11f3385ead10ed7d3d617e536b8f87 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp')
-rw-r--r--tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp19
1 files changed, 14 insertions, 5 deletions
diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
index 7b4662a5cd..089daf3ed5 100644
--- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
+++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
@@ -5378,25 +5378,34 @@ void tst_qqmllanguage::listContainingDeletedObject()
void tst_qqmllanguage::overrideSingleton()
{
- auto check = [](const QString &name) {
+ auto check = [](const QString &name, const QByteArray &singletonElement) {
const QByteArray testQml = "import Test 1.0\n"
"import QtQml 2.0\n"
- "QtObject { objectName: BareSingleton.objectName }";
+ "QtObject { objectName: " + singletonElement + ".objectName }";
QQmlEngine engine;
QQmlComponent component(&engine, nullptr);
- component.setData(testQml, QUrl());
+ component.setData(testQml, QUrl("singleton.qml"));
QVERIFY(component.isReady());
QScopedPointer<QObject> obj(component.create());
QCOMPARE(obj->objectName(), name);
};
- check("statically registered");
+ check("statically registered", "BareSingleton");
BareSingleton singleton;
singleton.setObjectName("dynamically registered");
qmlRegisterSingletonInstance("Test", 1, 0, "BareSingleton", &singleton);
- check("dynamically registered");
+ check("dynamically registered", "BareSingleton");
+
+ QTest::ignoreMessage(
+ QtWarningMsg,
+ "singleton.qml:3: TypeError: Cannot read property 'objectName' of undefined");
+ check("", "UncreatableSingleton");
+
+ qmlRegisterSingletonInstance("Test", 1, 0, "UncreatableSingleton",
+ UncreatableSingleton::instance());
+ check("uncreatable", "UncreatableSingleton");
}
QTEST_MAIN(tst_qqmllanguage)