From 48738dd945f8eb3112ce295c37a67632121020e7 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Mon, 16 Nov 2009 11:53:49 +1000 Subject: QmlEngine tests --- src/declarative/qml/qmlengine.cpp | 16 +- tests/auto/declarative/declarative.pro | 1 + tests/auto/declarative/qmlengine/qmlengine.pro | 8 + tests/auto/declarative/qmlengine/tst_qmlengine.cpp | 176 +++++++++++++++++++++ 4 files changed, 190 insertions(+), 11 deletions(-) create mode 100644 tests/auto/declarative/qmlengine/qmlengine.pro create mode 100644 tests/auto/declarative/qmlengine/tst_qmlengine.cpp diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index b6f3bde..66d4990 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -427,22 +427,16 @@ QmlContext *QmlEngine::contextForObject(const QObject *object) */ void QmlEngine::setContextForObject(QObject *object, QmlContext *context) { - QObjectPrivate *priv = QObjectPrivate::get(object); - - QmlDeclarativeData *data = - static_cast(priv->declarativeData); + if (!object || !context) + return; - if (data && data->context) { + QmlDeclarativeData *data = QmlDeclarativeData::get(object, true); + if (data->context) { qWarning("QmlEngine::setContextForObject(): Object already has a QmlContext"); return; } - if (!data) { - priv->declarativeData = new QmlDeclarativeData(context); - } else { - data->context = context; - } - + data->context = context; context->d_func()->contextObjects.append(object); } diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro index bcf908d..ec2c7d0 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/declarative/declarative.pro @@ -18,6 +18,7 @@ SUBDIRS += \ qmldom \ # Cover qmleasefollow \ # Cover qmlecmascript \ # Cover + qmlengine \ # Cover qmlerror \ # Cover qmlfontloader \ # Cover qmlgraphicsborderimage \ # Cover diff --git a/tests/auto/declarative/qmlengine/qmlengine.pro b/tests/auto/declarative/qmlengine/qmlengine.pro new file mode 100644 index 0000000..21d55a4 --- /dev/null +++ b/tests/auto/declarative/qmlengine/qmlengine.pro @@ -0,0 +1,8 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative webkit network +macx:CONFIG -= app_bundle + +SOURCES += tst_qmlengine.cpp + +# Define SRCDIR equal to test's source directory +DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/qmlengine/tst_qmlengine.cpp b/tests/auto/declarative/qmlengine/tst_qmlengine.cpp new file mode 100644 index 0000000..ef1eee5 --- /dev/null +++ b/tests/auto/declarative/qmlengine/tst_qmlengine.cpp @@ -0,0 +1,176 @@ +/**************************************************************************** +** +** 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 +#include +#include +#include + +class tst_qmlengine : public QObject +{ + Q_OBJECT +public: + tst_qmlengine() {} + +private slots: + void rootContext(); + void networkAccessManager(); + void baseUrl(); + void contextForObject(); + void offlineStoragePath(); +}; + +void tst_qmlengine::rootContext() +{ + QmlEngine engine; + + QVERIFY(engine.rootContext()); + + QCOMPARE(engine.rootContext()->engine(), &engine); + QVERIFY(engine.rootContext()->parentContext() == 0); +} + +void tst_qmlengine::networkAccessManager() +{ + QmlEngine *engine = new QmlEngine; + + // Test QmlEngine created manager + QPointer manager = engine->networkAccessManager(); + QVERIFY(manager != 0); + + // Test non-QmlEngine owner manager + QNetworkAccessManager localManager; + engine->setNetworkAccessManager(&localManager); + QVERIFY(manager == 0); + QVERIFY(engine->networkAccessManager() == &localManager); + + // Test QmlEngine owned manager + QPointer ownedManager = new QNetworkAccessManager(engine); + QVERIFY(ownedManager != 0); + engine->setNetworkAccessManager(ownedManager); + QVERIFY(engine->networkAccessManager() == ownedManager); + engine->setNetworkAccessManager(&localManager); + QVERIFY(ownedManager == 0); + QVERIFY(engine->networkAccessManager() == &localManager); + + // Test setting a null manager + engine->setNetworkAccessManager(0); + QVERIFY(engine->networkAccessManager() != 0); +} + +void tst_qmlengine::baseUrl() +{ + QmlEngine engine; + + QUrl cwd = QUrl::fromLocalFile(QDir::currentPath() + QDir::separator()); + + QCOMPARE(engine.baseUrl(), cwd); + QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml"))); + + QDir dir = QDir::current(); + dir.cdUp(); + QVERIFY(dir != QDir::current()); + QDir::setCurrent(dir.path()); + QVERIFY(QDir::current() == dir); + + QUrl cwd2 = QUrl::fromLocalFile(QDir::currentPath() + QDir::separator()); + QCOMPARE(engine.baseUrl(), cwd2); + QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd2.resolved(QUrl("main.qml"))); + + engine.setBaseUrl(cwd); + QCOMPARE(engine.baseUrl(), cwd); + QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml"))); +} + +void tst_qmlengine::contextForObject() +{ + QmlEngine *engine = new QmlEngine; + + // Test null-object + QVERIFY(QmlEngine::contextForObject(0) == 0); + + // Test an object with no context + QObject object; + QVERIFY(QmlEngine::contextForObject(&object) == 0); + + // Test setting null-object + QmlEngine::setContextForObject(0, engine->rootContext()); + + // Test setting null-context + QmlEngine::setContextForObject(&object, 0); + + // Test setting context + QmlEngine::setContextForObject(&object, engine->rootContext()); + QVERIFY(QmlEngine::contextForObject(&object) == engine->rootContext()); + + QmlContext context(engine->rootContext()); + + // Try changing context + QTest::ignoreMessage(QtWarningMsg, "QmlEngine::setContextForObject(): Object already has a QmlContext"); + QmlEngine::setContextForObject(&object, &context); + QVERIFY(QmlEngine::contextForObject(&object) == engine->rootContext()); + + // Delete context + delete engine; engine = 0; + QVERIFY(QmlEngine::contextForObject(&object) == 0); +} + +void tst_qmlengine::offlineStoragePath() +{ + QmlEngine engine; + + QDir dir(QDesktopServices::storageLocation(QDesktopServices::DataLocation)); + dir.cd("QML"); + dir.cd("OfflineStorage"); + + QCOMPARE(engine.offlineStoragePath(), dir.path()); + + engine.setOfflineStoragePath(QDir::homePath()); + QCOMPARE(engine.offlineStoragePath(), QDir::homePath()); +} + +QTEST_MAIN(tst_qmlengine) + +#include "tst_qmlengine.moc" -- cgit v0.12