From c0a7cfc6d205caf93bc46693ef4aca15fbcc36f8 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Mon, 2 Nov 2009 13:53:11 +1000 Subject: QmlContext tests --- src/declarative/qml/qmlbinding.cpp | 8 + src/declarative/qml/qmlbinding_p.h | 1 + src/declarative/qml/qmlcontext.cpp | 35 +- src/declarative/qml/qmlcontext_p.h | 1 + src/declarative/qml/qmlexpression.cpp | 4 + src/declarative/qml/qmlexpression_p.h | 6 + tests/auto/declarative/declarative.pro | 1 + tests/auto/declarative/qmlcontext/qmlcontext.pro | 6 + .../auto/declarative/qmlcontext/tst_qmlcontext.cpp | 387 +++++++++++++++++++++ 9 files changed, 445 insertions(+), 4 deletions(-) create mode 100644 tests/auto/declarative/qmlcontext/qmlcontext.pro create mode 100644 tests/auto/declarative/qmlcontext/tst_qmlcontext.cpp diff --git a/src/declarative/qml/qmlbinding.cpp b/src/declarative/qml/qmlbinding.cpp index eb08b61..5f7330f 100644 --- a/src/declarative/qml/qmlbinding.cpp +++ b/src/declarative/qml/qmlbinding.cpp @@ -67,6 +67,14 @@ QmlBindingData::~QmlBindingData() removeError(); } +void QmlBindingData::refresh() +{ + if (enabled && !updating && q) { + QmlBinding *b = static_cast(QmlExpressionPrivate::get(q)); + b->update(); + } +} + void QmlBindingData::removeError() { if (!prevError) return; diff --git a/src/declarative/qml/qmlbinding_p.h b/src/declarative/qml/qmlbinding_p.h index 6977e5b..e4de239 100644 --- a/src/declarative/qml/qmlbinding_p.h +++ b/src/declarative/qml/qmlbinding_p.h @@ -70,6 +70,7 @@ public: QmlMetaProperty property; + virtual void refresh(); void removeError(); bool addError(); QmlBindingData *nextError; diff --git a/src/declarative/qml/qmlcontext.cpp b/src/declarative/qml/qmlcontext.cpp index fe0d9cb..d37d959 100644 --- a/src/declarative/qml/qmlcontext.cpp +++ b/src/declarative/qml/qmlcontext.cpp @@ -180,9 +180,9 @@ void QmlContextPrivate::init() component.create(&context); \endcode - Each context may have up to 32 default objects, and objects added first take - precedence over those added later. All properties added explicitly by - QmlContext::setContextProperty() take precedence over default object properties. + Default objects added first take precedence over those added later. All properties + added explicitly by QmlContext::setContextProperty() take precedence over default + object properties. Contexts are hierarchal, with the \l {QmlEngine::rootContext()}{root context} being created by the QmlEngine. A component instantiated in a given context @@ -323,6 +323,26 @@ void QmlContextPrivate::invalidateEngines() } } +/* +Refreshes all expressions that could possibly depend on this context. +Refreshing flushes all context-tree dependent caches in the expressions, and should occur every +time the context tree *structure* (not values) changes. +*/ +void QmlContextPrivate::refreshExpressions() +{ + for (QSet::ConstIterator iter = childContexts.begin(); + iter != childContexts.end(); + ++iter) { + (*iter)->d_func()->refreshExpressions(); + } + + QmlAbstractExpression *expression = expressions; + while (expression) { + expression->refresh(); + expression = expression->m_nextExpression; + } +} + /*! Return the context's QmlEngine, or 0 if the context has no QmlEngine or the QmlEngine was destroyed. @@ -373,6 +393,8 @@ void QmlContext::setContextProperty(const QString &name, const QVariant &value) if (idx == -1) { d->propertyNames->add(name, d->idValueCount + d->propertyValues.count()); d->propertyValues.append(value); + + d->refreshExpressions(); } else { d->propertyValues[idx] = value; QMetaObject::activate(this, idx + d->notifyIndex, 0); @@ -404,7 +426,7 @@ void QmlContextPrivate::setIdPropertyData(QmlIntegerCache *data) /*! Set a the \a value of the \a name property on this context. - QmlContext does \b not take ownership of \a value. + QmlContext does \bold not take ownership of \a value. */ void QmlContext::setContextProperty(const QString &name, QObject *value) { @@ -418,6 +440,8 @@ void QmlContext::setContextProperty(const QString &name, QObject *value) if (idx == -1) { d->propertyNames->add(name, d->idValueCount + d->propertyValues.count()); d->propertyValues.append(QVariant::fromValue(value)); + + d->refreshExpressions(); } else { d->propertyValues[idx] = QVariant::fromValue(value); QMetaObject::activate(this, idx + d->notifyIndex, 0); @@ -432,6 +456,7 @@ void QmlContext::setContextProperty(const QString &name, QObject *value) */ QUrl QmlContext::resolvedUrl(const QUrl &src) { + Q_D(QmlContext); QmlContext *ctxt = this; if (src.isRelative() && !src.isEmpty()) { if (ctxt) { @@ -444,6 +469,8 @@ QUrl QmlContext::resolvedUrl(const QUrl &src) if (ctxt) return ctxt->d_func()->url.resolved(src); + else if (d->engine) + return d->engine->baseUrl().resolved(src); } return QUrl(); } else { diff --git a/src/declarative/qml/qmlcontext_p.h b/src/declarative/qml/qmlcontext_p.h index 286b882..cc8fcc6 100644 --- a/src/declarative/qml/qmlcontext_p.h +++ b/src/declarative/qml/qmlcontext_p.h @@ -105,6 +105,7 @@ public: void dump(int depth); void invalidateEngines(); + void refreshExpressions(); QSet childContexts; QmlAbstractExpression *expressions; diff --git a/src/declarative/qml/qmlexpression.cpp b/src/declarative/qml/qmlexpression.cpp index 2470c1d..faf9f5a 100644 --- a/src/declarative/qml/qmlexpression.cpp +++ b/src/declarative/qml/qmlexpression.cpp @@ -728,6 +728,10 @@ void QmlAbstractExpression::setContext(QmlContext *context) } } +void QmlAbstractExpression::refresh() +{ +} + bool QmlAbstractExpression::isValid() const { return m_context != 0; diff --git a/src/declarative/qml/qmlexpression_p.h b/src/declarative/qml/qmlexpression_p.h index ea572c3..c6ba54d 100644 --- a/src/declarative/qml/qmlexpression_p.h +++ b/src/declarative/qml/qmlexpression_p.h @@ -72,8 +72,11 @@ public: QmlContext *context() const; void setContext(QmlContext *); + virtual void refresh(); + private: friend class QmlContext; + friend class QmlContextPrivate; QmlContext *m_context; QmlAbstractExpression **m_prevExpression; QmlAbstractExpression *m_nextExpression; @@ -152,6 +155,9 @@ public: static QmlExpressionPrivate *get(QmlExpression *expr) { return static_cast(QObjectPrivate::get(expr)); } + static QmlExpression *get(QmlExpressionPrivate *expr) { + return expr->q_func(); + } static void exceptionToError(QScriptEngine *, QmlError &); }; diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro index b9ab3ca..40cdb58 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/declarative/declarative.pro @@ -15,6 +15,7 @@ SUBDIRS += anchors \ qfxtextedit \ qfxtextinput \ qfxwebview \ + qmlcontext \ qmldom \ qmlecmascript \ qmllanguage \ diff --git a/tests/auto/declarative/qmlcontext/qmlcontext.pro b/tests/auto/declarative/qmlcontext/qmlcontext.pro new file mode 100644 index 0000000..9e66429 --- /dev/null +++ b/tests/auto/declarative/qmlcontext/qmlcontext.pro @@ -0,0 +1,6 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative +SOURCES += tst_qmlcontext.cpp +macx:CONFIG -= app_bundle + +DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/qmlcontext/tst_qmlcontext.cpp b/tests/auto/declarative/qmlcontext/tst_qmlcontext.cpp new file mode 100644 index 0000000..69d9091 --- /dev/null +++ b/tests/auto/declarative/qmlcontext/tst_qmlcontext.cpp @@ -0,0 +1,387 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include + +class tst_qmlcontext : public QObject +{ + Q_OBJECT +public: + tst_qmlcontext() {} + +private slots: + void baseUrl(); + void resolvedUrl(); + void engineMethod(); + void parentContext(); + void setContextProperty(); + void addDefaultObject(); + +private: + QmlEngine engine; +}; + +void tst_qmlcontext::baseUrl() +{ + QmlContext ctxt(&engine); + + QCOMPARE(ctxt.baseUrl(), QUrl()); + + ctxt.setBaseUrl(QUrl("http://www.nokia.com/")); + + QCOMPARE(ctxt.baseUrl(), QUrl("http://www.nokia.com/")); +} + +void tst_qmlcontext::resolvedUrl() +{ + // Relative to the component + { + QmlContext ctxt(&engine); + ctxt.setBaseUrl(QUrl("http://www.nokia.com/")); + + QCOMPARE(ctxt.resolvedUrl(QUrl("main.qml")), QUrl("http://www.nokia.com/main.qml")); + } + + // Relative to a parent + { + QmlContext ctxt(&engine); + ctxt.setBaseUrl(QUrl("http://www.nokia.com/")); + + QmlContext ctxt2(&ctxt); + QCOMPARE(ctxt2.resolvedUrl(QUrl("main2.qml")), QUrl("http://www.nokia.com/main2.qml")); + } + + // Relative to the engine + { + QmlContext ctxt(&engine); + QCOMPARE(ctxt.resolvedUrl(QUrl("main.qml")), engine.baseUrl().resolved(QUrl("main.qml"))); + } + + // Relative to a deleted parent + { + QmlContext *ctxt = new QmlContext(&engine); + ctxt->setBaseUrl(QUrl("http://www.nokia.com/")); + + QmlContext ctxt2(ctxt); + QCOMPARE(ctxt2.resolvedUrl(QUrl("main2.qml")), QUrl("http://www.nokia.com/main2.qml")); + + delete ctxt; ctxt = 0; + + QCOMPARE(ctxt2.resolvedUrl(QUrl("main2.qml")), QUrl()); + } +} + +void tst_qmlcontext::engineMethod() +{ + QmlEngine *engine = new QmlEngine; + + QmlContext ctxt(engine); + QmlContext ctxt2(&ctxt); + QmlContext ctxt3(&ctxt2); + QmlContext ctxt4(&ctxt2); + + QCOMPARE(ctxt.engine(), engine); + QCOMPARE(ctxt2.engine(), engine); + QCOMPARE(ctxt3.engine(), engine); + QCOMPARE(ctxt4.engine(), engine); + + delete engine; engine = 0; + + QCOMPARE(ctxt.engine(), engine); + QCOMPARE(ctxt2.engine(), engine); + QCOMPARE(ctxt3.engine(), engine); + QCOMPARE(ctxt4.engine(), engine); +} + +void tst_qmlcontext::parentContext() +{ + QmlEngine *engine = new QmlEngine; + + QCOMPARE(engine->rootContext()->parentContext(), (QmlContext *)0); + + QmlContext *ctxt = new QmlContext(engine); + QmlContext *ctxt2 = new QmlContext(ctxt); + QmlContext *ctxt3 = new QmlContext(ctxt2); + QmlContext *ctxt4 = new QmlContext(ctxt2); + QmlContext *ctxt5 = new QmlContext(ctxt); + QmlContext *ctxt6 = new QmlContext(engine); + QmlContext *ctxt7 = new QmlContext(engine->rootContext()); + + QCOMPARE(ctxt->parentContext(), engine->rootContext()); + QCOMPARE(ctxt2->parentContext(), ctxt); + QCOMPARE(ctxt3->parentContext(), ctxt2); + QCOMPARE(ctxt4->parentContext(), ctxt2); + QCOMPARE(ctxt5->parentContext(), ctxt); + QCOMPARE(ctxt6->parentContext(), engine->rootContext()); + QCOMPARE(ctxt7->parentContext(), engine->rootContext()); + + delete ctxt2; ctxt2 = 0; + + QCOMPARE(ctxt->parentContext(), engine->rootContext()); + QCOMPARE(ctxt3->parentContext(), ctxt2); + QCOMPARE(ctxt4->parentContext(), ctxt2); + QCOMPARE(ctxt5->parentContext(), ctxt); + QCOMPARE(ctxt6->parentContext(), engine->rootContext()); + QCOMPARE(ctxt7->parentContext(), engine->rootContext()); + + delete engine; engine = 0; + + QCOMPARE(ctxt->parentContext(), (QmlContext *)0); + QCOMPARE(ctxt3->parentContext(), ctxt2); + QCOMPARE(ctxt4->parentContext(), ctxt2); + QCOMPARE(ctxt5->parentContext(), ctxt); + QCOMPARE(ctxt6->parentContext(), (QmlContext *)0); + QCOMPARE(ctxt7->parentContext(), (QmlContext *)0); + + delete ctxt7; + delete ctxt6; + delete ctxt5; + delete ctxt4; + delete ctxt3; + delete ctxt; +} + +class TestObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(int a READ a NOTIFY aChanged) + Q_PROPERTY(int b READ b NOTIFY bChanged) + Q_PROPERTY(int c READ c NOTIFY cChanged) + +public: + TestObject() : _a(10), _b(10), _c(10) {} + + int a() const { return _a; } + void setA(int a) { _a = a; emit aChanged(); } + + int b() const { return _b; } + void setB(int b) { _b = b; emit bChanged(); } + + int c() const { return _c; } + void setC(int c) { _c = c; emit cChanged(); } + +signals: + void aChanged(); + void bChanged(); + void cChanged(); + +private: + int _a; + int _b; + int _c; +}; + +class TestObject2 : public QObject +{ + Q_OBJECT + Q_PROPERTY(int b READ b NOTIFY bChanged) + +public: + TestObject2() : _b(10) {} + + int b() const { return _b; } + void setB(int b) { _b = b; emit bChanged(); } + +signals: + void bChanged(); + +private: + int _b; +}; + +#define TEST_CONTEXT_PROPERTY(ctxt, name, value) \ +{ \ + QmlComponent component(&engine); \ + component.setData("import Qt 4.6; Object { property var test: " #name " }", QUrl()); \ +\ + QObject *obj = component.create(ctxt); \ +\ + QCOMPARE(obj->property("test"), value); \ +\ + delete obj; \ +} + +void tst_qmlcontext::setContextProperty() +{ + QmlContext ctxt(&engine); + QmlContext ctxt2(&ctxt); + + TestObject obj1; + obj1.setA(3345); + TestObject obj2; + obj2.setA(-19); + + // Static context properties + ctxt.setContextProperty("a", QVariant(10)); + ctxt.setContextProperty("b", QVariant(9)); + 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)); + TEST_CONTEXT_PROPERTY(&ctxt2, b, QVariant(19)); + TEST_CONTEXT_PROPERTY(&ctxt2, c, QVariant(QString("Hello World!"))); + TEST_CONTEXT_PROPERTY(&ctxt2, d.a, QVariant(-19)); + TEST_CONTEXT_PROPERTY(&ctxt2, e.a, QVariant(3345)); + + ctxt.setContextProperty("a", QVariant(13)); + ctxt.setContextProperty("b", QVariant(4)); + ctxt2.setContextProperty("b", QVariant(8)); + ctxt2.setContextProperty("c", QVariant(QString("Hi World!"))); + ctxt2.setContextProperty("d", &obj1); + obj1.setA(12); + + TEST_CONTEXT_PROPERTY(&ctxt2, a, QVariant(13)); + TEST_CONTEXT_PROPERTY(&ctxt2, b, QVariant(8)); + TEST_CONTEXT_PROPERTY(&ctxt2, c, QVariant(QString("Hi World!"))); + TEST_CONTEXT_PROPERTY(&ctxt2, d.a, QVariant(12)); + TEST_CONTEXT_PROPERTY(&ctxt2, e.a, QVariant(12)); + + // Changes in context properties + { + QmlComponent component(&engine); + component.setData("import Qt 4.6; Object { property var test: a }", QUrl()); + + QObject *obj = component.create(&ctxt2); + + QCOMPARE(obj->property("test"), QVariant(13)); + ctxt.setContextProperty("a", QVariant(19)); + QCOMPARE(obj->property("test"), QVariant(19)); + + delete obj; + } + { + QmlComponent component(&engine); + component.setData("import Qt 4.6; Object { property var test: b }", QUrl()); + + QObject *obj = component.create(&ctxt2); + + QCOMPARE(obj->property("test"), QVariant(8)); + ctxt.setContextProperty("b", QVariant(5)); + QCOMPARE(obj->property("test"), QVariant(8)); + ctxt2.setContextProperty("b", QVariant(1912)); + QCOMPARE(obj->property("test"), QVariant(1912)); + + delete obj; + } + { + QmlComponent component(&engine); + component.setData("import Qt 4.6; Object { property var test: e.a }", QUrl()); + + QObject *obj = component.create(&ctxt2); + + QCOMPARE(obj->property("test"), QVariant(12)); + obj1.setA(13); + QCOMPARE(obj->property("test"), QVariant(13)); + + delete obj; + } + + // New context properties + { + QmlComponent component(&engine); + component.setData("import Qt 4.6; Object { property var test: a }", QUrl()); + + QObject *obj = component.create(&ctxt2); + + QCOMPARE(obj->property("test"), QVariant(19)); + ctxt2.setContextProperty("a", QVariant(1945)); + QCOMPARE(obj->property("test"), QVariant(1945)); + + delete obj; + } +} + +void tst_qmlcontext::addDefaultObject() +{ + QmlContext ctxt(&engine); + + TestObject to; + TestObject2 to2; + + to.setA(2); + to.setB(192); + to.setC(18); + to2.setB(111999); + + ctxt.addDefaultObject(&to2); + ctxt.addDefaultObject(&to); + ctxt.setContextProperty("c", QVariant(9)); + + // Static context properties + TEST_CONTEXT_PROPERTY(&ctxt, a, QVariant(2)); + TEST_CONTEXT_PROPERTY(&ctxt, b, QVariant(111999)); + TEST_CONTEXT_PROPERTY(&ctxt, c, QVariant(9)); + + to.setA(12); + to.setB(100); + to.setC(7); + to2.setB(1612); + ctxt.setContextProperty("c", QVariant(3)); + + TEST_CONTEXT_PROPERTY(&ctxt, a, QVariant(12)); + TEST_CONTEXT_PROPERTY(&ctxt, b, QVariant(1612)); + TEST_CONTEXT_PROPERTY(&ctxt, c, QVariant(3)); + + // Changes in context properties + { + QmlComponent component(&engine); + component.setData("import Qt 4.6; Object { property var test: a }", QUrl()); + + QObject *obj = component.create(&ctxt); + + QCOMPARE(obj->property("test"), QVariant(12)); + to.setA(14); + QCOMPARE(obj->property("test"), QVariant(14)); + + delete obj; + } +} + +QTEST_MAIN(tst_qmlcontext) + +#include "tst_qmlcontext.moc" -- cgit v0.12