Merge the QJSEngine and QJSValue development branch into master.
[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 <QtDeclarative/qjsengine.h>
47
48 #include <QtDeclarative/qdeclarativecomponent.h>
49 #include <QtDeclarative/qdeclarativeengine.h>
50
51 #include <private/qdeclarativeworkerscript_p.h>
52 #include <private/qdeclarativeengine_p.h>
53 #include "../../../shared/util.h"
54
55 #ifdef Q_OS_SYMBIAN
56 // In Symbian OS test data is located in applications private dir
57 #define SRCDIR "."
58 #endif
59
60 inline QUrl TEST_FILE(const QString &filename)
61 {
62     QFileInfo fileInfo(__FILE__);
63     return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
64 }
65
66
67 class tst_QDeclarativeWorkerScript : public QObject
68 {
69     Q_OBJECT
70 public:
71     tst_QDeclarativeWorkerScript() {}
72 private slots:
73     void source();
74     void messaging();
75     void messaging_data();
76     void messaging_sendQObjectList();
77     void messaging_sendJsObject();
78     void script_with_pragma();
79     void script_included();
80     void scriptError_onLoad();
81     void scriptError_onCall();
82     void stressDispose();
83
84 private:
85     void waitForEchoMessage(QDeclarativeWorkerScript *worker) {
86         QEventLoop loop;
87         QVERIFY(connect(worker, SIGNAL(done()), &loop, SLOT(quit())));
88         QTimer timer;
89         timer.setSingleShot(true);
90         connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
91         timer.start(10000);
92         loop.exec();
93         QVERIFY(timer.isActive());
94     }
95
96     QDeclarativeEngine m_engine;
97 };
98
99 void tst_QDeclarativeWorkerScript::source()
100 {
101     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
102     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
103     QVERIFY(worker != 0);
104     const QMetaObject *mo = worker->metaObject();
105
106     QVariant value(100);
107     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
108     waitForEchoMessage(worker);
109     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
110
111     QUrl source = QUrl::fromLocalFile(SRCDIR "/data/script_fixed_return.js");
112     worker->setSource(source);
113     QCOMPARE(worker->source(), source);
114     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
115     waitForEchoMessage(worker);
116     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), qVariantFromValue(QString("Hello_World")));
117
118     qApp->processEvents();
119     delete worker;
120 }
121
122 void tst_QDeclarativeWorkerScript::messaging()
123 {
124     QFETCH(QVariant, value);
125
126     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
127     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
128     QVERIFY(worker != 0);
129
130     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
131     waitForEchoMessage(worker);
132
133     const QMetaObject *mo = worker->metaObject();
134     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
135
136     qApp->processEvents();
137     delete worker;
138 }
139
140 void tst_QDeclarativeWorkerScript::messaging_data()
141 {
142     QTest::addColumn<QVariant>("value");
143
144     QTest::newRow("invalid") << QVariant();
145     QTest::newRow("bool") << qVariantFromValue(true);
146     QTest::newRow("int") << qVariantFromValue(1001);
147     QTest::newRow("real") << qVariantFromValue(10334.375);
148     QTest::newRow("string") << qVariantFromValue(QString("More cheeeese, Gromit!"));
149     QTest::newRow("variant list") << qVariantFromValue((QVariantList() << "a" << "b" << "c"));
150     QTest::newRow("date time") << qVariantFromValue(QDateTime::currentDateTime());
151 #ifndef QT_NO_REGEXP
152     // QtScript's QScriptValue -> QRegExp uses RegExp2 pattern syntax
153     QTest::newRow("regexp") << qVariantFromValue(QRegExp("^\\d\\d?$", Qt::CaseInsensitive, QRegExp::RegExp2));
154 #endif
155 }
156
157 void tst_QDeclarativeWorkerScript::messaging_sendQObjectList()
158 {
159     // Not allowed to send QObjects other than QDeclarativeWorkerListModelAgent
160     // instances. If objects are sent in a list, they will be sent as 'undefined'
161     // js values.
162
163     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
164     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
165     QVERIFY(worker != 0);
166
167     QVariantList objects;
168     for (int i=0; i<3; i++)
169         objects << qVariantFromValue(new QObject(this));
170
171     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(objects))));
172     waitForEchoMessage(worker);
173
174     const QMetaObject *mo = worker->metaObject();
175     QVariantList result = mo->property(mo->indexOfProperty("response")).read(worker).value<QVariantList>();
176     QCOMPARE(result, (QVariantList() << QVariant() << QVariant() << QVariant()));
177
178     qApp->processEvents();
179     delete worker;
180 }
181
182 void tst_QDeclarativeWorkerScript::messaging_sendJsObject()
183 {
184     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
185     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
186     QVERIFY(worker != 0);
187
188     // Properties are in alphabetical order to enable string-based comparison after
189     // QVariant roundtrip, since the properties will be stored in a QVariantMap.
190     QString jsObject = "{'haste': 1125, 'name': 'zyz', 'spell power': 3101}";
191
192     QVariantMap map;
193     map.insert("haste", 1125);
194     map.insert("name", "zyz");
195     map.insert("spell power", 3101);
196
197     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(map))));
198     waitForEchoMessage(worker);
199
200     QVariant result = qVariantFromValue(false);
201     QVERIFY(QMetaObject::invokeMethod(worker, "compareLiteralResponse", Qt::DirectConnection, 
202             Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, jsObject)));
203     QVERIFY(result.toBool());
204
205     qApp->processEvents();
206     delete worker;
207 }
208
209 void tst_QDeclarativeWorkerScript::script_with_pragma()
210 {
211     QVariant value(100);
212
213     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_pragma.qml");
214     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
215     QVERIFY(worker != 0);
216
217     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
218     waitForEchoMessage(worker);
219
220     const QMetaObject *mo = worker->metaObject();
221     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
222
223     qApp->processEvents();
224     delete worker;
225 }
226
227 void tst_QDeclarativeWorkerScript::script_included()
228 {
229     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_include.qml");
230     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
231     QVERIFY(worker != 0);
232
233     QString value("Hello");
234
235     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
236     waitForEchoMessage(worker);
237
238     const QMetaObject *mo = worker->metaObject();
239     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
240
241     qApp->processEvents();
242     delete worker;
243 }
244
245 static QString qdeclarativeworkerscript_lastWarning;
246 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
247 {
248     if (type == QtWarningMsg)
249          qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
250 }
251
252 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
253 {
254     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onLoad.qml");
255
256     QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
257     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
258     QVERIFY(worker != 0);
259
260     QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
261             TEST_FILE("data/script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Unexpected identifier"));
262
263     qInstallMsgHandler(previousMsgHandler);
264     qApp->processEvents();
265     delete worker;
266 }
267
268 void tst_QDeclarativeWorkerScript::scriptError_onCall()
269 {
270     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onCall.qml");
271     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
272     QVERIFY(worker != 0);
273
274     QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
275     QVariant value;
276     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
277
278     QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
279             TEST_FILE("data/script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
280
281     qInstallMsgHandler(previousMsgHandler);
282     qApp->processEvents();
283     delete worker;
284 }
285
286 // Rapidly create and destroy worker scripts to test resources are being disposed
287 // in the correct isolate
288 void tst_QDeclarativeWorkerScript::stressDispose()
289 {
290     for (int ii = 0; ii < 100; ++ii) {
291         QDeclarativeEngine engine;
292         QDeclarativeComponent component(&engine, SRCDIR "/data/stressDispose.qml");
293         QObject *o = component.create();
294         QVERIFY(o);
295         delete o;
296     }
297 }
298
299 QTEST_MAIN(tst_QDeclarativeWorkerScript)
300
301 #include "tst_qdeclarativeworkerscript.moc"