Fix crash in object serialization due to sequences
[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     return QUrl::fromLocalFile(TESTDATA(filename));
58 }
59
60
61 class tst_QDeclarativeWorkerScript : public QObject
62 {
63     Q_OBJECT
64 public:
65     tst_QDeclarativeWorkerScript() {}
66 private slots:
67     void source();
68     void messaging();
69     void messaging_data();
70     void messaging_sendQObjectList();
71     void messaging_sendJsObject();
72     void messaging_sendExternalObject();
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, TESTDATA("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(TESTDATA("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, TESTDATA("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, TESTDATA("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, TESTDATA("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::messaging_sendExternalObject()
205 {
206     QDeclarativeComponent component(&m_engine, TESTDATA("externalObjectWorker.qml"));
207     QObject *obj = component.create();
208     QVERIFY(obj);
209     QMetaObject::invokeMethod(obj, "testExternalObject");
210     QTest::qWait(100); // shouldn't crash.
211     delete obj;
212 }
213
214 void tst_QDeclarativeWorkerScript::script_with_pragma()
215 {
216     QVariant value(100);
217
218     QDeclarativeComponent component(&m_engine, TESTDATA("worker_pragma.qml"));
219     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
220     QVERIFY(worker != 0);
221
222     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
223     waitForEchoMessage(worker);
224
225     const QMetaObject *mo = worker->metaObject();
226     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
227
228     qApp->processEvents();
229     delete worker;
230 }
231
232 void tst_QDeclarativeWorkerScript::script_included()
233 {
234     QDeclarativeComponent component(&m_engine, TESTDATA("worker_include.qml"));
235     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
236     QVERIFY(worker != 0);
237
238     QString value("Hello");
239
240     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
241     waitForEchoMessage(worker);
242
243     const QMetaObject *mo = worker->metaObject();
244     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
245
246     qApp->processEvents();
247     delete worker;
248 }
249
250 static QString qdeclarativeworkerscript_lastWarning;
251 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
252 {
253     if (type == QtWarningMsg)
254          qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
255 }
256
257 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
258 {
259     QDeclarativeComponent component(&m_engine, TESTDATA("worker_error_onLoad.qml"));
260
261     QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
262     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
263     QVERIFY(worker != 0);
264
265     QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
266             TEST_FILE("script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Unexpected identifier"));
267
268     qInstallMsgHandler(previousMsgHandler);
269     qApp->processEvents();
270     delete worker;
271 }
272
273 void tst_QDeclarativeWorkerScript::scriptError_onCall()
274 {
275     QDeclarativeComponent component(&m_engine, TESTDATA("worker_error_onCall.qml"));
276     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
277     QVERIFY(worker != 0);
278
279     QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
280     QVariant value;
281     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
282
283     QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
284             TEST_FILE("script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
285
286     qInstallMsgHandler(previousMsgHandler);
287     qApp->processEvents();
288     delete worker;
289 }
290
291 // Rapidly create and destroy worker scripts to test resources are being disposed
292 // in the correct isolate
293 void tst_QDeclarativeWorkerScript::stressDispose()
294 {
295     for (int ii = 0; ii < 100; ++ii) {
296         QDeclarativeEngine engine;
297         QDeclarativeComponent component(&engine, TESTDATA("stressDispose.qml"));
298         QObject *o = component.create();
299         QVERIFY(o);
300         delete o;
301     }
302 }
303
304 QTEST_MAIN(tst_QDeclarativeWorkerScript)
305
306 #include "tst_qdeclarativeworkerscript.moc"