aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qquickloader/tst_qquickloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/quick/qquickloader/tst_qquickloader.cpp')
-rw-r--r--tests/auto/quick/qquickloader/tst_qquickloader.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/auto/quick/qquickloader/tst_qquickloader.cpp b/tests/auto/quick/qquickloader/tst_qquickloader.cpp
index 30fc52f95a..7a176661e8 100644
--- a/tests/auto/quick/qquickloader/tst_qquickloader.cpp
+++ b/tests/auto/quick/qquickloader/tst_qquickloader.cpp
@@ -29,6 +29,7 @@
#include <QSignalSpy>
+#include <QtQml/QQmlContext>
#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlcomponent.h>
#include <QtQml/qqmlincubator.h>
@@ -123,6 +124,8 @@ private slots:
void bindings();
void parentErrors();
+
+ void rootContext();
};
Q_DECLARE_METATYPE(QList<QQmlError>)
@@ -1327,6 +1330,64 @@ void tst_QQuickLoader::parentErrors()
QVERIFY2(warningsSpy.isEmpty(), qPrintable(failureMessage));
}
+class ObjectInRootContext: public QObject
+{
+ Q_OBJECT
+
+public:
+ int didIt = 0;
+
+public slots:
+ void doIt() {
+ didIt += 1;
+ }
+};
+
+void tst_QQuickLoader::rootContext()
+{
+ QQmlEngine engine;
+ ObjectInRootContext objectInRootContext;
+ engine.rootContext()->setContextProperty("objectInRootContext", &objectInRootContext);
+
+ QQmlComponent component(&engine, testFileUrl("rootContext.qml"));
+ QScopedPointer<QObject> object(component.create());
+ QVERIFY(!object.isNull());
+
+ QQuickLoader *loader = object->property("loader").value<QQuickLoader*>();
+ QVERIFY(loader);
+
+ QSignalSpy warningsSpy(&engine, SIGNAL(warnings(QList<QQmlError>)));
+
+ // Give the loader a component
+ loader->setSourceComponent(object->property("component").value<QQmlComponent*>());
+ QTRY_VERIFY(loader->active());
+ QTRY_VERIFY(loader->item());
+
+ QString failureMessage;
+ if (!warningsSpy.isEmpty()) {
+ QDebug stream(&failureMessage);
+ stream << warningsSpy.first().first().value<QList<QQmlError>>();
+ }
+ QVERIFY2(warningsSpy.isEmpty(), qPrintable(failureMessage));
+ QCOMPARE(objectInRootContext.didIt, 0);
+
+ // Deactivate the loader, which deletes the item.
+ // Check that a) there are no errors, and b) the objectInRootContext can still be resolved even
+ // after deactivating the loader. If it cannot, a ReferenceError for objectInRootContext is
+ // generated (and the 'doIt' counter in objectInRootContext will be 1 for the call before
+ // the deactivation).
+ loader->item()->setProperty("trigger", true);
+ QTRY_VERIFY(!loader->active());
+ QTRY_VERIFY(!loader->item());
+
+ if (!warningsSpy.isEmpty()) {
+ QDebug stream(&failureMessage);
+ stream << warningsSpy.first().first().value<QList<QQmlError>>();
+ }
+ QVERIFY2(warningsSpy.isEmpty(), qPrintable(failureMessage));
+ QCOMPARE(objectInRootContext.didIt, 2);
+}
+
QTEST_MAIN(tst_QQuickLoader)
#include "tst_qquickloader.moc"