Merge branch 'qtquick2' of scm.dev.nokia.troll.no:qt/qtdeclarative-staging into v8
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeworkerscript / tst_qdeclarativeworkerscript.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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QtCore/qdebug.h>
43 #include <QtCore/qtimer.h>
44 #include <QtCore/qdir.h>
45 #include <QtCore/qfileinfo.h>
46 #include <QtScript/qscriptengine.h>
47
48 #include <QtDeclarative/qdeclarativecomponent.h>
49 #include <QtDeclarative/qdeclarativeengine.h>
50 #include <QtDeclarative/qdeclarativeitem.h>
51
52 #include <private/qdeclarativeworkerscript_p.h>
53 #include <private/qdeclarativeengine_p.h>
54 #include "../../../shared/util.h"
55
56 #ifdef Q_OS_SYMBIAN
57 // In Symbian OS test data is located in applications private dir
58 #define SRCDIR "."
59 #endif
60
61 inline QUrl TEST_FILE(const QString &filename)
62 {
63     QFileInfo fileInfo(__FILE__);
64     return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
65 }
66
67
68 class tst_QDeclarativeWorkerScript : public QObject
69 {
70     Q_OBJECT
71 public:
72     tst_QDeclarativeWorkerScript() {}
73 private slots:
74     void source();
75     void messaging();
76     void messaging_data();
77     void messaging_sendQObjectList();
78     void messaging_sendJsObject();
79     void script_with_pragma();
80     void script_included();
81     void scriptError_onLoad();
82     void scriptError_onCall();
83     void stressDispose();
84
85 private:
86     void waitForEchoMessage(QDeclarativeWorkerScript *worker) {
87         QEventLoop loop;
88         QVERIFY(connect(worker, SIGNAL(done()), &loop, SLOT(quit())));
89         QTimer timer;
90         timer.setSingleShot(true);
91         connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
92         timer.start(10000);
93         loop.exec();
94         QVERIFY(timer.isActive());
95     }
96
97     QDeclarativeEngine m_engine;
98 };
99
100 void tst_QDeclarativeWorkerScript::source()
101 {
102     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
103     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
104     QVERIFY(worker != 0);
105     const QMetaObject *mo = worker->metaObject();
106
107     QVariant value(100);
108     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
109     waitForEchoMessage(worker);
110     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
111
112     QUrl source = QUrl::fromLocalFile(SRCDIR "/data/script_fixed_return.js");
113     worker->setSource(source);
114     QCOMPARE(worker->source(), source);
115     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
116     waitForEchoMessage(worker);
117     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), qVariantFromValue(QString("Hello_World")));
118
119     qApp->processEvents();
120     delete worker;
121 }
122
123 void tst_QDeclarativeWorkerScript::messaging()
124 {
125     QFETCH(QVariant, value);
126
127     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
128     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
129     QVERIFY(worker != 0);
130
131     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
132     waitForEchoMessage(worker);
133
134     const QMetaObject *mo = worker->metaObject();
135     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
136
137     qApp->processEvents();
138     delete worker;
139 }
140
141 void tst_QDeclarativeWorkerScript::messaging_data()
142 {
143     QTest::addColumn<QVariant>("value");
144
145     QTest::newRow("invalid") << QVariant();
146     QTest::newRow("bool") << qVariantFromValue(true);
147     QTest::newRow("int") << qVariantFromValue(1001);
148     QTest::newRow("real") << qVariantFromValue(10334.375);
149     QTest::newRow("string") << qVariantFromValue(QString("More cheeeese, Gromit!"));
150     QTest::newRow("variant list") << qVariantFromValue((QVariantList() << "a" << "b" << "c"));
151     QTest::newRow("date time") << qVariantFromValue(QDateTime::currentDateTime());
152 #ifndef QT_NO_REGEXP
153     // QtScript's QScriptValue -> QRegExp uses RegExp2 pattern syntax
154     QTest::newRow("regexp") << qVariantFromValue(QRegExp("^\\d\\d?$", Qt::CaseInsensitive, QRegExp::RegExp2));
155 #endif
156 }
157
158 void tst_QDeclarativeWorkerScript::messaging_sendQObjectList()
159 {
160     // Not allowed to send QObjects other than QDeclarativeWorkerListModelAgent
161     // instances. If objects are sent in a list, they will be sent as 'undefined'
162     // js values.
163
164     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
165     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
166     QVERIFY(worker != 0);
167
168     QVariantList objects;
169     for (int i=0; i<3; i++)
170         objects << qVariantFromValue(new QObject(this));
171
172     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(objects))));
173     waitForEchoMessage(worker);
174
175     const QMetaObject *mo = worker->metaObject();
176     QVariantList result = mo->property(mo->indexOfProperty("response")).read(worker).value<QVariantList>();
177     QCOMPARE(result, (QVariantList() << QVariant() << QVariant() << QVariant()));
178
179     qApp->processEvents();
180     delete worker;
181 }
182
183 void tst_QDeclarativeWorkerScript::messaging_sendJsObject()
184 {
185     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
186     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
187     QVERIFY(worker != 0);
188
189     // Properties are in alphabetical order to enable string-based comparison after
190     // QVariant roundtrip, since the properties will be stored in a QVariantMap.
191     QString jsObject = "{'haste': 1125, 'name': 'zyz', 'spell power': 3101}";
192
193     QVariantMap map;
194     map.insert("haste", 1125);
195     map.insert("name", "zyz");
196     map.insert("spell power", 3101);
197
198     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(map))));
199     waitForEchoMessage(worker);
200
201     QVariant result = qVariantFromValue(false);
202     QVERIFY(QMetaObject::invokeMethod(worker, "compareLiteralResponse", Qt::DirectConnection, 
203             Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, jsObject)));
204     QVERIFY(result.toBool());
205
206     qApp->processEvents();
207     delete worker;
208 }
209
210 void tst_QDeclarativeWorkerScript::script_with_pragma()
211 {
212     QVariant value(100);
213
214     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_pragma.qml");
215     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
216     QVERIFY(worker != 0);
217
218     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
219     waitForEchoMessage(worker);
220
221     const QMetaObject *mo = worker->metaObject();
222     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
223
224     qApp->processEvents();
225     delete worker;
226 }
227
228 void tst_QDeclarativeWorkerScript::script_included()
229 {
230     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_include.qml");
231     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
232     QVERIFY(worker != 0);
233
234     QString value("Hello");
235
236     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
237     waitForEchoMessage(worker);
238
239     const QMetaObject *mo = worker->metaObject();
240     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
241
242     qApp->processEvents();
243     delete worker;
244 }
245
246 static QString qdeclarativeworkerscript_lastWarning;
247 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
248 {
249     if (type == QtWarningMsg)
250          qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
251 }
252
253 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
254 {
255     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onLoad.qml");
256
257     QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
258     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
259     QVERIFY(worker != 0);
260
261     QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
262             TEST_FILE("data/script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Unexpected identifier"));
263
264     qInstallMsgHandler(previousMsgHandler);
265     qApp->processEvents();
266     delete worker;
267 }
268
269 void tst_QDeclarativeWorkerScript::scriptError_onCall()
270 {
271     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onCall.qml");
272     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
273     QVERIFY(worker != 0);
274
275     QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
276     QVariant value;
277     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
278
279     QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
280             TEST_FILE("data/script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
281
282     qInstallMsgHandler(previousMsgHandler);
283     qApp->processEvents();
284     delete worker;
285 }
286
287 // Rapidly create and destroy worker scripts to test resources are being disposed
288 // in the correct isolate
289 void tst_QDeclarativeWorkerScript::stressDispose()
290 {
291     for (int ii = 0; ii < 100; ++ii) {
292         QDeclarativeEngine engine;
293         QDeclarativeComponent component(&engine, SRCDIR "/data/stressDispose.qml");
294         QObject *o = component.create();
295         QVERIFY(o);
296         delete o;
297     }
298 }
299
300 QTEST_MAIN(tst_QDeclarativeWorkerScript)
301
302 #include "tst_qdeclarativeworkerscript.moc"