aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-02-26 16:04:23 +0100
committerUlf Hermann <ulf.hermann@qt.io>2020-03-23 12:00:33 +0100
commitd51c007ecc8aa6256cb95cf3992e5ac34a70fa3f (patch)
tree80f90690d29ab48a128c075b6c982cea7256a150 /tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
parentc83a39fd460d1787d1c17391413543bf7ca4c330 (diff)
Encapsulate QQmlContextData
This class is not a private detail of QQmlContext. And it is incredibly hard to see who owns what in there. Let's add some civilization ... We enforce refcounting for QQmlContextData across the code base, with two exceptions: 1. QQmlContextPrivate may or may not own its QQmlContextData. 2. We may request a QQmlContextData owned by its parent QQmlContextData. For these two cases we keep flags in QQmlContextData and when the respective field (m_parent or m_publicContext) is reset, we release() once. Furthermore, QQmlContextData and QQmlGuardedContextData are moved to their own files, in order to de-spaghettify qqmlcontext_p.h and qqmlcontext.cpp. When the QQmlEngine is deleted, any QQmlComponents drop their object creators now, in order to release any context data held by those. Before, the context data would be deleted, but the object creators would retain the dangling pointer. [ChangeLog][QML][Important Behavior Changes] QQmlContext::baseUrl() does what the documentation says now: It prefers explicitly set baseUrls over compilation unit URLs. Only if no baseUrl is set, the CU's URL is returned. It used to prefer the CU's URL. Change-Id: Ieeb5dcb07b45d891526191321386d5443b8f5738 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp')
-rw-r--r--tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp58
1 files changed, 29 insertions, 29 deletions
diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
index cfbc329198..b3e4296a13 100644
--- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
+++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
@@ -386,7 +386,7 @@ private slots:
private:
// static void propertyVarWeakRefCallback(v8::Persistent<v8::Value> object, void* parameter);
- static void verifyContextLifetime(QQmlContextData *ctxt);
+ static void verifyContextLifetime(const QQmlRefPointer<QQmlContextData> &ctxt);
// When calling into JavaScript, the specific type of the return value can differ if that return
// value is a number. This is not only the case for non-integral numbers, or numbers that do not
@@ -4289,16 +4289,16 @@ void tst_qqmlecmascript::singletonTypeResolution()
delete object;
}
-void tst_qqmlecmascript::verifyContextLifetime(QQmlContextData *ctxt) {
- QQmlContextData *childCtxt = ctxt->childContexts;
+void tst_qqmlecmascript::verifyContextLifetime(const QQmlRefPointer<QQmlContextData> &ctxt) {
+ QQmlRefPointer<QQmlContextData> childCtxt = ctxt->childContexts();
- if (!ctxt->importedScripts.isNullOrUndefined()) {
- QV4::ExecutionEngine *v4 = ctxt->engine->handle();
+ if (!ctxt->importedScripts().isNullOrUndefined()) {
+ QV4::ExecutionEngine *v4 = ctxt->engine()->handle();
QV4::Scope scope(v4);
- QV4::ScopedArrayObject scripts(scope, ctxt->importedScripts.value());
+ QV4::ScopedArrayObject scripts(scope, ctxt->importedScripts().value());
QV4::Scoped<QV4::QQmlContextWrapper> qml(scope);
for (quint32 i = 0; i < scripts->getLength(); ++i) {
- QQmlContextData *scriptContext, *newContext;
+ QQmlRefPointer<QQmlContextData> scriptContext, newContext;
qml = scripts->get(i);
scriptContext = qml ? qml->getContext() : nullptr;
@@ -4310,17 +4310,16 @@ void tst_qqmlecmascript::verifyContextLifetime(QQmlContextData *ctxt) {
Q_UNUSED(temporaryScope)
}
- ctxt->engine->collectGarbage();
+ ctxt->engine()->collectGarbage();
qml = scripts->get(i);
newContext = qml ? qml->getContext() : nullptr;
- QCOMPARE(scriptContext, newContext);
+ QCOMPARE(scriptContext.data(), newContext.data());
}
}
while (childCtxt) {
verifyContextLifetime(childCtxt);
-
- childCtxt = childCtxt->nextChild;
+ childCtxt = childCtxt->nextChild();
}
}
@@ -4592,9 +4591,7 @@ void tst_qqmlecmascript::importScripts()
QVERIFY(!object);
} else {
QVERIFY(object != nullptr);
-
- QQmlContextData *ctxt = QQmlContextData::get(engine.rootContext());
- tst_qqmlecmascript::verifyContextLifetime(ctxt);
+ tst_qqmlecmascript::verifyContextLifetime(QQmlContextData::get(engine.rootContext()));
for (int i = 0; i < propertyNames.size(); ++i)
QCOMPARE(object->property(propertyNames.at(i).toLatin1().constData()), propertyValues.at(i));
@@ -6138,24 +6135,22 @@ void tst_qqmlecmascript::nullObjectInitializer()
// Test that bindings don't evaluate once the engine has been destroyed
void tst_qqmlecmascript::deletedEngine()
{
- QQmlEngine *engine = new QQmlEngine;
- QQmlComponent component(engine, testFileUrl("deletedEngine.qml"));
+ QScopedPointer<QQmlEngine> engine(new QQmlEngine);
+ QQmlComponent component(engine.data(), testFileUrl("deletedEngine.qml"));
+ QScopedPointer<QObject> object(component.create());
- QObject *object = component.create();
QVERIFY(object != nullptr);
QCOMPARE(object->property("a").toInt(), 39);
object->setProperty("b", QVariant(9));
QCOMPARE(object->property("a").toInt(), 117);
- delete engine;
+ engine.reset(); // This drops the engine's context hierarchy and the object gets notified.
QCOMPARE(object->property("a").toInt(), 0);
object->setProperty("b", QVariant(10));
object->setProperty("b", QVariant());
QCOMPARE(object->property("a").toInt(), 0);
-
- delete object;
}
// Test the crashing part of QTBUG-9705
@@ -6217,25 +6212,30 @@ void tst_qqmlecmascript::variants()
void tst_qqmlecmascript::qtbug_9792()
{
QQmlEngine engine;
- QQmlComponent component(&engine, testFileUrl("qtbug_9792.qml"));
- QQmlContext *context = new QQmlContext(engine.rootContext());
+ QScopedPointer<QQmlContext> context(new QQmlContext(engine.rootContext()));
+ QScopedPointer<QObject> object;
- MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create(context));
- QVERIFY(object != nullptr);
+ MyQmlObject *myQmlObject = nullptr;
+ {
+ // Drop the component after creation as that holds a strong reference
+ // to the root QQmlContextData. We want the context data to be destroyed.
+ QQmlComponent component(&engine, testFileUrl("qtbug_9792.qml"));
+ object.reset(component.create(context.data()));
+ myQmlObject = qobject_cast<MyQmlObject*>(object.data());
+ }
+ QVERIFY(myQmlObject != nullptr);
QTest::ignoreMessage(QtDebugMsg, "Hello world!");
- object->basicSignal();
+ myQmlObject->basicSignal();
- delete context;
+ context.reset();
QQmlTestMessageHandler messageHandler;
- object->basicSignal();
+ myQmlObject->basicSignal();
QVERIFY2(messageHandler.messages().isEmpty(), qPrintable(messageHandler.messageString()));
-
- delete object;
}
// Verifies that QPointer<>s used in the vmemetaobject are cleaned correctly