Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeengine / tst_qdeclarativeengine.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeContext>
45 #include <QNetworkAccessManager>
46 #include <QPointer>
47 #include <QDir>
48 #include <QDesktopServices>
49 #include <QDebug>
50 #include <QDeclarativeComponent>
51 #include <QDeclarativeNetworkAccessManagerFactory>
52
53 #ifdef Q_OS_SYMBIAN
54 // In Symbian OS test data is located in applications private dir
55 #define SRCDIR "."
56 #endif
57
58 class tst_qdeclarativeengine : public QObject
59 {
60     Q_OBJECT
61 public:
62     tst_qdeclarativeengine() {}
63
64 private slots:
65     void rootContext();
66     void networkAccessManager();
67     void baseUrl();
68     void contextForObject();
69     void offlineStoragePath();
70     void clearComponentCache();
71     void outputWarningsToStandardError();
72     void objectOwnership();
73 };
74
75 void tst_qdeclarativeengine::rootContext()
76 {
77     QDeclarativeEngine engine;
78
79     QVERIFY(engine.rootContext());
80
81     QCOMPARE(engine.rootContext()->engine(), &engine);
82     QVERIFY(engine.rootContext()->parentContext() == 0);
83 }
84
85 class NetworkAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory
86 {
87 public:
88     NetworkAccessManagerFactory() : manager(0) {}
89
90     QNetworkAccessManager *create(QObject *parent) {
91         manager = new QNetworkAccessManager(parent);
92         return manager;
93     }
94
95     QNetworkAccessManager *manager;
96 };
97
98 void tst_qdeclarativeengine::networkAccessManager()
99 {
100     QDeclarativeEngine *engine = new QDeclarativeEngine;
101
102     // Test QDeclarativeEngine created manager
103     QPointer<QNetworkAccessManager> manager = engine->networkAccessManager();
104     QVERIFY(manager != 0);
105     delete engine;
106
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);
113     delete engine;
114 }
115
116 void tst_qdeclarativeengine::baseUrl()
117 {
118     QDeclarativeEngine engine;
119
120     QUrl cwd = QUrl::fromLocalFile(QDir::currentPath() + QDir::separator());
121
122     QCOMPARE(engine.baseUrl(), cwd);
123     QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml")));
124
125     QDir dir = QDir::current();
126     dir.cdUp();
127     QVERIFY(dir != QDir::current());
128     QDir::setCurrent(dir.path());
129     QVERIFY(QDir::current() == dir);
130
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")));
134
135     engine.setBaseUrl(cwd);
136     QCOMPARE(engine.baseUrl(), cwd);
137     QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml")));
138 }
139
140 void tst_qdeclarativeengine::contextForObject()
141 {
142     QDeclarativeEngine *engine = new QDeclarativeEngine;
143
144     // Test null-object
145     QVERIFY(QDeclarativeEngine::contextForObject(0) == 0);
146
147     // Test an object with no context
148     QObject object;
149     QVERIFY(QDeclarativeEngine::contextForObject(&object) == 0);
150
151     // Test setting null-object
152     QDeclarativeEngine::setContextForObject(0, engine->rootContext());
153
154     // Test setting null-context
155     QDeclarativeEngine::setContextForObject(&object, 0);
156
157     // Test setting context
158     QDeclarativeEngine::setContextForObject(&object, engine->rootContext());
159     QVERIFY(QDeclarativeEngine::contextForObject(&object) == engine->rootContext());
160
161     QDeclarativeContext context(engine->rootContext());
162
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());
167
168     // Delete context
169     delete engine; engine = 0;
170     QVERIFY(QDeclarativeEngine::contextForObject(&object) == 0);
171 }
172
173 void tst_qdeclarativeengine::offlineStoragePath()
174 {
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");
180
181     QDeclarativeEngine engine;
182
183     QDir dir(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
184     dir.mkpath("QML");
185     dir.cd("QML");
186     dir.mkpath("OfflineStorage");
187     dir.cd("OfflineStorage");
188
189     QCOMPARE(QDir::fromNativeSeparators(engine.offlineStoragePath()), dir.path());
190
191     engine.setOfflineStoragePath(QDir::homePath());
192     QCOMPARE(engine.offlineStoragePath(), QDir::homePath());
193 }
194
195 void tst_qdeclarativeengine::clearComponentCache()
196 {
197     QDeclarativeEngine engine;
198
199     // Create original qml file
200     {
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");
204         file.close();
205     }
206
207     // Test "test" property
208     {
209         QDeclarativeComponent component(&engine, "temp.qml");
210         QObject *obj = component.create();
211         QVERIFY(obj != 0);
212         QCOMPARE(obj->property("test").toInt(), 10);
213         delete obj;
214     }
215
216     // Modify qml file
217     {
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");
221         file.close();
222     }
223
224     // Test cache hit
225     {
226         QDeclarativeComponent component(&engine, "temp.qml");
227         QObject *obj = component.create();
228         QVERIFY(obj != 0);
229         QCOMPARE(obj->property("test").toInt(), 10);
230         delete obj;
231     }
232
233     // Clear cache
234     engine.clearComponentCache();
235
236     // Test cache refresh
237     {
238         QDeclarativeComponent component(&engine, "temp.qml");
239         QObject *obj = component.create();
240         QVERIFY(obj != 0);
241         QCOMPARE(obj->property("test").toInt(), 11);
242         delete obj;
243     }
244 }
245
246 static QStringList warnings;
247 static void msgHandler(QtMsgType, const char *warning)
248 {
249     warnings << QString::fromUtf8(warning);
250 }
251
252 void tst_qdeclarativeengine::outputWarningsToStandardError()
253 {
254     QDeclarativeEngine engine;
255
256     QCOMPARE(engine.outputWarningsToStandardError(), true);
257
258     QDeclarativeComponent c(&engine);
259     c.setData("import QtQuick 1.0; QtObject { property int a: undefined }", QUrl());
260
261     QVERIFY(c.isReady() == true);
262
263     warnings.clear();
264     QtMsgHandler old = qInstallMsgHandler(msgHandler);
265
266     QObject *o = c.create();
267
268     qInstallMsgHandler(old);
269
270     QVERIFY(o != 0);
271     delete o;
272
273     QCOMPARE(warnings.count(), 1);
274     QCOMPARE(warnings.at(0), QLatin1String("<Unknown File>:1: Unable to assign [undefined] to int a"));
275     warnings.clear();
276
277
278     engine.setOutputWarningsToStandardError(false);
279     QCOMPARE(engine.outputWarningsToStandardError(), false);
280
281
282     old = qInstallMsgHandler(msgHandler);
283
284     o = c.create();
285
286     qInstallMsgHandler(old);
287
288     QVERIFY(o != 0);
289     delete o;
290
291     QCOMPARE(warnings.count(), 0);
292 }
293
294 void tst_qdeclarativeengine::objectOwnership()
295 {
296     {
297     QCOMPARE(QDeclarativeEngine::objectOwnership(0), QDeclarativeEngine::CppOwnership);
298     QDeclarativeEngine::setObjectOwnership(0, QDeclarativeEngine::JavaScriptOwnership);
299     QCOMPARE(QDeclarativeEngine::objectOwnership(0), QDeclarativeEngine::CppOwnership);
300     }
301
302     {
303     QObject o;
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);
311     }
312
313     {
314     QDeclarativeEngine engine;
315     QDeclarativeComponent c(&engine);
316     c.setData("import QtQuick 1.0; QtObject { property QtObject object: QtObject {} }", QUrl());
317
318     QObject *o = c.create();
319     QVERIFY(o != 0);
320
321     QCOMPARE(QDeclarativeEngine::objectOwnership(o), QDeclarativeEngine::CppOwnership);
322
323     QObject *o2 = qvariant_cast<QObject *>(o->property("object"));
324     QCOMPARE(QDeclarativeEngine::objectOwnership(o2), QDeclarativeEngine::JavaScriptOwnership);
325
326     delete o;
327     }
328
329 }
330
331 QTEST_MAIN(tst_qdeclarativeengine)
332
333 #include "tst_qdeclarativeengine.moc"