From 36420993cb7572fa630ac88cdd6afefa2ab1c660 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Tue, 3 Nov 2009 15:43:56 +1000 Subject: More QmlContext tests --- src/declarative/qml/qmlcontext.cpp | 16 +------ src/declarative/qml/qmlcontext_p.h | 3 -- .../auto/declarative/qmlcontext/tst_qmlcontext.cpp | 49 +++++++++++++++++++++- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/declarative/qml/qmlcontext.cpp b/src/declarative/qml/qmlcontext.cpp index d37d959..f8e685a 100644 --- a/src/declarative/qml/qmlcontext.cpp +++ b/src/declarative/qml/qmlcontext.cpp @@ -88,18 +88,6 @@ void QmlContextPrivate::addScript(const QString &script, QObject *scopeObject, scripts.append(scope); } -void QmlContextPrivate::dump() -{ - dump(0); -} - -void QmlContextPrivate::dump(int depth) -{ - QByteArray ba(depth * 4, ' '); - if (parent) - parent->d_func()->dump(depth + 1); -} - void QmlContextPrivate::destroyed(ContextGuard *guard) { Q_Q(QmlContext); @@ -382,8 +370,8 @@ void QmlContext::setContextProperty(const QString &name, const QVariant &value) if (d->notifyIndex == -1) d->notifyIndex = this->metaObject()->methodCount(); - if (QmlMetaType::isObject(value.userType())) { - QObject *o = QmlMetaType::toQObject(value); + if (d->engine && QmlEnginePrivate::get(d->engine)->isObject(value.userType())) { + QObject *o = *(QObject **)value.constData(); setContextProperty(name, o); } else { diff --git a/src/declarative/qml/qmlcontext_p.h b/src/declarative/qml/qmlcontext_p.h index cc8fcc6..7f9be0f 100644 --- a/src/declarative/qml/qmlcontext_p.h +++ b/src/declarative/qml/qmlcontext_p.h @@ -101,9 +101,6 @@ public: void init(); - void dump(); - void dump(int depth); - void invalidateEngines(); void refreshExpressions(); QSet childContexts; diff --git a/tests/auto/declarative/qmlcontext/tst_qmlcontext.cpp b/tests/auto/declarative/qmlcontext/tst_qmlcontext.cpp index 69d9091..3c60b2b 100644 --- a/tests/auto/declarative/qmlcontext/tst_qmlcontext.cpp +++ b/tests/auto/declarative/qmlcontext/tst_qmlcontext.cpp @@ -44,6 +44,7 @@ #include #include #include +#include class tst_qmlcontext : public QObject { @@ -58,6 +59,7 @@ private slots: void parentContext(); void setContextProperty(); void addDefaultObject(); + void destruction(); private: QmlEngine engine; @@ -111,6 +113,14 @@ void tst_qmlcontext::resolvedUrl() QCOMPARE(ctxt2.resolvedUrl(QUrl("main2.qml")), QUrl()); } + + // Absolute + { + QmlContext ctxt(&engine); + + QCOMPARE(ctxt.resolvedUrl(QUrl("http://www.nokia.com/main2.qml")), QUrl("http://www.nokia.com/main2.qml")); + QCOMPARE(ctxt.resolvedUrl(QUrl("file:///main2.qml")), QUrl("file:///main2.qml")); + } } void tst_qmlcontext::engineMethod() @@ -256,10 +266,10 @@ void tst_qmlcontext::setContextProperty() // Static context properties ctxt.setContextProperty("a", QVariant(10)); ctxt.setContextProperty("b", QVariant(9)); + ctxt2.setContextProperty("d", &obj2); ctxt2.setContextProperty("b", QVariant(19)); ctxt2.setContextProperty("c", QVariant(QString("Hello World!"))); ctxt.setContextProperty("d", &obj1); - ctxt2.setContextProperty("d", &obj2); ctxt.setContextProperty("e", &obj1); TEST_CONTEXT_PROPERTY(&ctxt2, a, QVariant(10)); @@ -334,6 +344,26 @@ void tst_qmlcontext::setContextProperty() delete obj; } + + // Setting an object-variant context property + { + QmlComponent component(&engine); + component.setData("import Qt 4.6; Object { id: root; property int a: 10; property int test: ctxtProp.a; property var obj: root; }", QUrl()); + + QmlContext ctxt(engine.rootContext()); + ctxt.setContextProperty("ctxtProp", QVariant()); + + QTest::ignoreMessage(QtWarningMsg, ":1: TypeError: Result of expression 'ctxtProp' [undefined] is not an object."); + QObject *obj = component.create(&ctxt); + + QVariant v = obj->property("obj"); + + ctxt.setContextProperty("ctxtProp", v); + + QCOMPARE(obj->property("test"), QVariant(10)); + + delete obj; + } } void tst_qmlcontext::addDefaultObject() @@ -382,6 +412,23 @@ void tst_qmlcontext::addDefaultObject() } } +void tst_qmlcontext::destruction() +{ + QmlContext *ctxt = new QmlContext(&engine); + + QObject obj; + QmlEngine::setContextForObject(&obj, ctxt); + QmlExpression expr(ctxt, "a", 0); + + QCOMPARE(ctxt, QmlEngine::contextForObject(&obj)); + QCOMPARE(ctxt, expr.context()); + + delete ctxt; ctxt = 0; + + QCOMPARE(ctxt, QmlEngine::contextForObject(&obj)); + QCOMPARE(ctxt, expr.context()); +} + QTEST_MAIN(tst_qmlcontext) #include "tst_qmlcontext.moc" -- cgit v0.12