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