1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qtest.h>
#include <QDeclarativeEngine>
#include <QDeclarativeContext>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QPointer>
#include <QDir>
#include <QDesktopServices>
#include <QDebug>
#include <QDeclarativeComponent>
#include <QDeclarativeNetworkAccessManagerFactory>
#ifdef Q_OS_SYMBIAN
// In Symbian OS test data is located in applications private dir
#define SRCDIR "."
#endif
class tst_qdeclarativeengine : public QObject
{
Q_OBJECT
public:
tst_qdeclarativeengine() {}
private slots:
void rootContext();
void networkAccessManager();
void baseUrl();
void contextForObject();
void offlineStoragePath();
void clearComponentCache();
void outputWarningsToStandardError();
void objectOwnership();
void synchronousNetworkReply();
};
void tst_qdeclarativeengine::rootContext()
{
QDeclarativeEngine engine;
QVERIFY(engine.rootContext());
QCOMPARE(engine.rootContext()->engine(), &engine);
QVERIFY(engine.rootContext()->parentContext() == 0);
}
class NetworkAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory
{
public:
NetworkAccessManagerFactory() : manager(0) {}
QNetworkAccessManager *create(QObject *parent) {
manager = new QNetworkAccessManager(parent);
return manager;
}
QNetworkAccessManager *manager;
};
void tst_qdeclarativeengine::networkAccessManager()
{
QDeclarativeEngine *engine = new QDeclarativeEngine;
// Test QDeclarativeEngine created manager
QPointer<QNetworkAccessManager> manager = engine->networkAccessManager();
QVERIFY(manager != 0);
delete engine;
// Test factory created manager
engine = new QDeclarativeEngine;
NetworkAccessManagerFactory factory;
engine->setNetworkAccessManagerFactory(&factory);
QVERIFY(engine->networkAccessManagerFactory() == &factory);
QVERIFY(engine->networkAccessManager() == factory.manager);
delete engine;
}
void tst_qdeclarativeengine::baseUrl()
{
QDeclarativeEngine 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_qdeclarativeengine::contextForObject()
{
QDeclarativeEngine *engine = new QDeclarativeEngine;
// Test null-object
QVERIFY(QDeclarativeEngine::contextForObject(0) == 0);
// Test an object with no context
QObject object;
QVERIFY(QDeclarativeEngine::contextForObject(&object) == 0);
// Test setting null-object
QDeclarativeEngine::setContextForObject(0, engine->rootContext());
// Test setting null-context
QDeclarativeEngine::setContextForObject(&object, 0);
// Test setting context
QDeclarativeEngine::setContextForObject(&object, engine->rootContext());
QVERIFY(QDeclarativeEngine::contextForObject(&object) == engine->rootContext());
QDeclarativeContext context(engine->rootContext());
// Try changing context
QTest::ignoreMessage(QtWarningMsg, "QDeclarativeEngine::setContextForObject(): Object already has a QDeclarativeContext");
QDeclarativeEngine::setContextForObject(&object, &context);
QVERIFY(QDeclarativeEngine::contextForObject(&object) == engine->rootContext());
// Delete context
delete engine; engine = 0;
QVERIFY(QDeclarativeEngine::contextForObject(&object) == 0);
}
void tst_qdeclarativeengine::offlineStoragePath()
{
// Without these set, QDesktopServices::storageLocation returns
// strings with extra "//" at the end. We set them to ignore this problem.
qApp->setApplicationName("tst_qdeclarativeengine");
qApp->setOrganizationName("Digia");
qApp->setOrganizationDomain("digia.com");
QDeclarativeEngine engine;
QDir dir(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
dir.mkpath("QML");
dir.cd("QML");
dir.mkpath("OfflineStorage");
dir.cd("OfflineStorage");
QCOMPARE(QDir::fromNativeSeparators(engine.offlineStoragePath()), dir.path());
engine.setOfflineStoragePath(QDir::homePath());
QCOMPARE(engine.offlineStoragePath(), QDir::homePath());
}
void tst_qdeclarativeengine::clearComponentCache()
{
QDeclarativeEngine engine;
// Create original qml file
{
QFile file("temp.qml");
QVERIFY(file.open(QIODevice::WriteOnly));
file.write("import QtQuick 1.0\nQtObject {\nproperty int test: 10\n}\n");
file.close();
}
// Test "test" property
{
QDeclarativeComponent component(&engine, "temp.qml");
QObject *obj = component.create();
QVERIFY(obj != 0);
QCOMPARE(obj->property("test").toInt(), 10);
delete obj;
}
// Modify qml file
{
QFile file("temp.qml");
QVERIFY(file.open(QIODevice::WriteOnly));
file.write("import QtQuick 1.0\nQtObject {\nproperty int test: 11\n}\n");
file.close();
}
// Test cache hit
{
QDeclarativeComponent component(&engine, "temp.qml");
QObject *obj = component.create();
QVERIFY(obj != 0);
QCOMPARE(obj->property("test").toInt(), 10);
delete obj;
}
// Clear cache
engine.clearComponentCache();
// Test cache refresh
{
QDeclarativeComponent component(&engine, "temp.qml");
QObject *obj = component.create();
QVERIFY(obj != 0);
QCOMPARE(obj->property("test").toInt(), 11);
delete obj;
}
}
static QStringList warnings;
static void msgHandler(QtMsgType, const char *warning)
{
warnings << QString::fromUtf8(warning);
}
void tst_qdeclarativeengine::outputWarningsToStandardError()
{
QDeclarativeEngine engine;
QCOMPARE(engine.outputWarningsToStandardError(), true);
QDeclarativeComponent c(&engine);
c.setData("import QtQuick 1.0; QtObject { property int a: undefined }", QUrl());
QVERIFY(c.isReady() == true);
warnings.clear();
QtMsgHandler old = qInstallMsgHandler(msgHandler);
QObject *o = c.create();
qInstallMsgHandler(old);
QVERIFY(o != 0);
delete o;
QCOMPARE(warnings.count(), 1);
QCOMPARE(warnings.at(0), QLatin1String("<Unknown File>:1: Unable to assign [undefined] to int a"));
warnings.clear();
engine.setOutputWarningsToStandardError(false);
QCOMPARE(engine.outputWarningsToStandardError(), false);
old = qInstallMsgHandler(msgHandler);
o = c.create();
qInstallMsgHandler(old);
QVERIFY(o != 0);
delete o;
QCOMPARE(warnings.count(), 0);
}
void tst_qdeclarativeengine::objectOwnership()
{
{
QCOMPARE(QDeclarativeEngine::objectOwnership(0), QDeclarativeEngine::CppOwnership);
QDeclarativeEngine::setObjectOwnership(0, QDeclarativeEngine::JavaScriptOwnership);
QCOMPARE(QDeclarativeEngine::objectOwnership(0), QDeclarativeEngine::CppOwnership);
}
{
QObject o;
QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::CppOwnership);
QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::JavaScriptOwnership);
QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::JavaScriptOwnership);
QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::CppOwnership);
QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
}
{
QDeclarativeEngine engine;
QDeclarativeComponent c(&engine);
c.setData("import QtQuick 1.0; QtObject { property QtObject object: QtObject {} }", QUrl());
QObject *o = c.create();
QVERIFY(o != 0);
QCOMPARE(QDeclarativeEngine::objectOwnership(o), QDeclarativeEngine::CppOwnership);
QObject *o2 = qvariant_cast<QObject *>(o->property("object"));
QCOMPARE(QDeclarativeEngine::objectOwnership(o2), QDeclarativeEngine::JavaScriptOwnership);
delete o;
}
}
class MyReply : public QNetworkReply {
Q_OBJECT
public:
MyReply() {
setFinished(true);
}
virtual qint64 readData(char* buffer, qint64 number) {
return 0;
}
virtual void abort() { }
};
class MyManager : public QNetworkAccessManager {
Q_OBJECT
public:
MyManager(QObject *parent = 0) : QNetworkAccessManager(parent) {
}
QNetworkReply *createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0) {
return new MyReply;
}
};
class MyFactory : public QDeclarativeNetworkAccessManagerFactory {
public:
QNetworkAccessManager *create(QObject *parent) {
return new MyManager;
}
};
void tst_qdeclarativeengine::synchronousNetworkReply()
{
MyFactory factory;
QDeclarativeEngine engine;
engine.setNetworkAccessManagerFactory(&factory);
QDeclarativeComponent c(&engine, QUrl("myScheme://test.qml"));
// we get an error, but we only care about whether we are finished or not in this test
QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
c.create();
// reply is finished, so should not be in loading state.
QVERIFY(!c.isLoading());
}
QTEST_MAIN(tst_qdeclarativeengine)
#include "tst_qdeclarativeengine.moc"
|