1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeContext>
45 #include <QNetworkAccessManager>
48 #include <QDesktopServices>
50 #include <QDeclarativeComponent>
51 #include <QDeclarativeNetworkAccessManagerFactory>
54 // In Symbian OS test data is located in applications private dir
58 class tst_qdeclarativeengine : public QObject
62 tst_qdeclarativeengine() {}
66 void networkAccessManager();
68 void contextForObject();
69 void offlineStoragePath();
70 void clearComponentCache();
71 void outputWarningsToStandardError();
72 void objectOwnership();
75 void tst_qdeclarativeengine::rootContext()
77 QDeclarativeEngine engine;
79 QVERIFY(engine.rootContext());
81 QCOMPARE(engine.rootContext()->engine(), &engine);
82 QVERIFY(engine.rootContext()->parentContext() == 0);
85 class NetworkAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory
88 NetworkAccessManagerFactory() : manager(0) {}
90 QNetworkAccessManager *create(QObject *parent) {
91 manager = new QNetworkAccessManager(parent);
95 QNetworkAccessManager *manager;
98 void tst_qdeclarativeengine::networkAccessManager()
100 QDeclarativeEngine *engine = new QDeclarativeEngine;
102 // Test QDeclarativeEngine created manager
103 QPointer<QNetworkAccessManager> manager = engine->networkAccessManager();
104 QVERIFY(manager != 0);
107 // Test factory created manager
108 engine = new QDeclarativeEngine;
109 NetworkAccessManagerFactory factory;
110 engine->setNetworkAccessManagerFactory(&factory);
111 QVERIFY(engine->networkAccessManagerFactory() == &factory);
112 QVERIFY(engine->networkAccessManager() == factory.manager);
116 void tst_qdeclarativeengine::baseUrl()
118 QDeclarativeEngine engine;
120 QUrl cwd = QUrl::fromLocalFile(QDir::currentPath() + QDir::separator());
122 QCOMPARE(engine.baseUrl(), cwd);
123 QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml")));
125 QDir dir = QDir::current();
127 QVERIFY(dir != QDir::current());
128 QDir::setCurrent(dir.path());
129 QVERIFY(QDir::current() == dir);
131 QUrl cwd2 = QUrl::fromLocalFile(QDir::currentPath() + QDir::separator());
132 QCOMPARE(engine.baseUrl(), cwd2);
133 QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd2.resolved(QUrl("main.qml")));
135 engine.setBaseUrl(cwd);
136 QCOMPARE(engine.baseUrl(), cwd);
137 QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml")));
140 void tst_qdeclarativeengine::contextForObject()
142 QDeclarativeEngine *engine = new QDeclarativeEngine;
145 QVERIFY(QDeclarativeEngine::contextForObject(0) == 0);
147 // Test an object with no context
149 QVERIFY(QDeclarativeEngine::contextForObject(&object) == 0);
151 // Test setting null-object
152 QDeclarativeEngine::setContextForObject(0, engine->rootContext());
154 // Test setting null-context
155 QDeclarativeEngine::setContextForObject(&object, 0);
157 // Test setting context
158 QDeclarativeEngine::setContextForObject(&object, engine->rootContext());
159 QVERIFY(QDeclarativeEngine::contextForObject(&object) == engine->rootContext());
161 QDeclarativeContext context(engine->rootContext());
163 // Try changing context
164 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeEngine::setContextForObject(): Object already has a QDeclarativeContext");
165 QDeclarativeEngine::setContextForObject(&object, &context);
166 QVERIFY(QDeclarativeEngine::contextForObject(&object) == engine->rootContext());
169 delete engine; engine = 0;
170 QVERIFY(QDeclarativeEngine::contextForObject(&object) == 0);
173 void tst_qdeclarativeengine::offlineStoragePath()
175 // Without these set, QDesktopServices::storageLocation returns
176 // strings with extra "//" at the end. We set them to ignore this problem.
177 qApp->setApplicationName("tst_qdeclarativeengine");
178 qApp->setOrganizationName("Nokia");
179 qApp->setOrganizationDomain("nokia.com");
181 QDeclarativeEngine engine;
183 QDir dir(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
186 dir.mkpath("OfflineStorage");
187 dir.cd("OfflineStorage");
189 QCOMPARE(QDir::fromNativeSeparators(engine.offlineStoragePath()), dir.path());
191 engine.setOfflineStoragePath(QDir::homePath());
192 QCOMPARE(engine.offlineStoragePath(), QDir::homePath());
195 void tst_qdeclarativeengine::clearComponentCache()
197 QDeclarativeEngine engine;
199 // Create original qml file
201 QFile file("temp.qml");
202 QVERIFY(file.open(QIODevice::WriteOnly));
203 file.write("import QtQuick 1.0\nQtObject {\nproperty int test: 10\n}\n");
207 // Test "test" property
209 QDeclarativeComponent component(&engine, "temp.qml");
210 QObject *obj = component.create();
212 QCOMPARE(obj->property("test").toInt(), 10);
218 QFile file("temp.qml");
219 QVERIFY(file.open(QIODevice::WriteOnly));
220 file.write("import QtQuick 1.0\nQtObject {\nproperty int test: 11\n}\n");
226 QDeclarativeComponent component(&engine, "temp.qml");
227 QObject *obj = component.create();
229 QCOMPARE(obj->property("test").toInt(), 10);
234 engine.clearComponentCache();
236 // Test cache refresh
238 QDeclarativeComponent component(&engine, "temp.qml");
239 QObject *obj = component.create();
241 QCOMPARE(obj->property("test").toInt(), 11);
246 static QStringList warnings;
247 static void msgHandler(QtMsgType, const char *warning)
249 warnings << QString::fromUtf8(warning);
252 void tst_qdeclarativeengine::outputWarningsToStandardError()
254 QDeclarativeEngine engine;
256 QCOMPARE(engine.outputWarningsToStandardError(), true);
258 QDeclarativeComponent c(&engine);
259 c.setData("import QtQuick 1.0; QtObject { property int a: undefined }", QUrl());
261 QVERIFY(c.isReady() == true);
264 QtMsgHandler old = qInstallMsgHandler(msgHandler);
266 QObject *o = c.create();
268 qInstallMsgHandler(old);
273 QCOMPARE(warnings.count(), 1);
274 QCOMPARE(warnings.at(0), QLatin1String("<Unknown File>:1: Unable to assign [undefined] to int a"));
278 engine.setOutputWarningsToStandardError(false);
279 QCOMPARE(engine.outputWarningsToStandardError(), false);
282 old = qInstallMsgHandler(msgHandler);
286 qInstallMsgHandler(old);
291 QCOMPARE(warnings.count(), 0);
294 void tst_qdeclarativeengine::objectOwnership()
297 QCOMPARE(QDeclarativeEngine::objectOwnership(0), QDeclarativeEngine::CppOwnership);
298 QDeclarativeEngine::setObjectOwnership(0, QDeclarativeEngine::JavaScriptOwnership);
299 QCOMPARE(QDeclarativeEngine::objectOwnership(0), QDeclarativeEngine::CppOwnership);
304 QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
305 QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::CppOwnership);
306 QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
307 QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::JavaScriptOwnership);
308 QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::JavaScriptOwnership);
309 QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::CppOwnership);
310 QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
314 QDeclarativeEngine engine;
315 QDeclarativeComponent c(&engine);
316 c.setData("import QtQuick 1.0; QtObject { property QtObject object: QtObject {} }", QUrl());
318 QObject *o = c.create();
321 QCOMPARE(QDeclarativeEngine::objectOwnership(o), QDeclarativeEngine::CppOwnership);
323 QObject *o2 = qvariant_cast<QObject *>(o->property("object"));
324 QCOMPARE(QDeclarativeEngine::objectOwnership(o2), QDeclarativeEngine::JavaScriptOwnership);
331 QTEST_MAIN(tst_qdeclarativeengine)
333 #include "tst_qdeclarativeengine.moc"