1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
48 #include <QtDeclarative/qdeclarativecomponent.h>
49 #include <QtDeclarative/qdeclarativeengine.h>
51 #include <private/qdeclarativeworkerscript_p.h>
52 #include <private/qdeclarativeengine_p.h>
53 #include "../shared/util.h"
55 inline QUrl TEST_FILE(const QString &filename)
57 return QUrl::fromLocalFile(TESTDATA(filename));
61 class tst_QDeclarativeWorkerScript : public QObject
65 tst_QDeclarativeWorkerScript() {}
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();
80 void waitForEchoMessage(QDeclarativeWorkerScript *worker) {
82 QVERIFY(connect(worker, SIGNAL(done()), &loop, SLOT(quit())));
84 timer.setSingleShot(true);
85 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
88 QVERIFY(timer.isActive());
91 QDeclarativeEngine m_engine;
94 void tst_QDeclarativeWorkerScript::source()
96 QDeclarativeComponent component(&m_engine, TESTDATA("worker.qml"));
97 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
99 const QMetaObject *mo = worker->metaObject();
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);
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")));
113 qApp->processEvents();
117 void tst_QDeclarativeWorkerScript::messaging()
119 QFETCH(QVariant, value);
121 QDeclarativeComponent component(&m_engine, TESTDATA("worker.qml"));
122 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
123 QVERIFY(worker != 0);
125 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
126 waitForEchoMessage(worker);
128 const QMetaObject *mo = worker->metaObject();
129 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
131 qApp->processEvents();
135 void tst_QDeclarativeWorkerScript::messaging_data()
137 QTest::addColumn<QVariant>("value");
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());
147 // QtScript's QScriptValue -> QRegExp uses RegExp2 pattern syntax
148 QTest::newRow("regexp") << qVariantFromValue(QRegExp("^\\d\\d?$", Qt::CaseInsensitive, QRegExp::RegExp2));
152 void tst_QDeclarativeWorkerScript::messaging_sendQObjectList()
154 // Not allowed to send QObjects other than QDeclarativeWorkerListModelAgent
155 // instances. If objects are sent in a list, they will be sent as 'undefined'
158 QDeclarativeComponent component(&m_engine, TESTDATA("worker.qml"));
159 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
160 QVERIFY(worker != 0);
162 QVariantList objects;
163 for (int i=0; i<3; i++)
164 objects << qVariantFromValue(new QObject(this));
166 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(objects))));
167 waitForEchoMessage(worker);
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()));
173 qApp->processEvents();
177 void tst_QDeclarativeWorkerScript::messaging_sendJsObject()
179 QDeclarativeComponent component(&m_engine, TESTDATA("worker.qml"));
180 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
181 QVERIFY(worker != 0);
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}";
188 map.insert("haste", 1125);
189 map.insert("name", "zyz");
190 map.insert("spell power", 3101);
192 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(map))));
193 waitForEchoMessage(worker);
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());
200 qApp->processEvents();
204 void tst_QDeclarativeWorkerScript::messaging_sendExternalObject()
206 QDeclarativeComponent component(&m_engine, TESTDATA("externalObjectWorker.qml"));
207 QObject *obj = component.create();
209 QMetaObject::invokeMethod(obj, "testExternalObject");
210 QTest::qWait(100); // shouldn't crash.
214 void tst_QDeclarativeWorkerScript::script_with_pragma()
218 QDeclarativeComponent component(&m_engine, TESTDATA("worker_pragma.qml"));
219 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
220 QVERIFY(worker != 0);
222 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
223 waitForEchoMessage(worker);
225 const QMetaObject *mo = worker->metaObject();
226 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
228 qApp->processEvents();
232 void tst_QDeclarativeWorkerScript::script_included()
234 QDeclarativeComponent component(&m_engine, TESTDATA("worker_include.qml"));
235 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
236 QVERIFY(worker != 0);
238 QString value("Hello");
240 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
241 waitForEchoMessage(worker);
243 const QMetaObject *mo = worker->metaObject();
244 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
246 qApp->processEvents();
250 static QString qdeclarativeworkerscript_lastWarning;
251 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
253 if (type == QtWarningMsg)
254 qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
257 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
259 QDeclarativeComponent component(&m_engine, TESTDATA("worker_error_onLoad.qml"));
261 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
262 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
263 QVERIFY(worker != 0);
265 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
266 TEST_FILE("script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Unexpected identifier"));
268 qInstallMsgHandler(previousMsgHandler);
269 qApp->processEvents();
273 void tst_QDeclarativeWorkerScript::scriptError_onCall()
275 QDeclarativeComponent component(&m_engine, TESTDATA("worker_error_onCall.qml"));
276 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
277 QVERIFY(worker != 0);
279 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
281 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
283 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
284 TEST_FILE("script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
286 qInstallMsgHandler(previousMsgHandler);
287 qApp->processEvents();
291 // Rapidly create and destroy worker scripts to test resources are being disposed
292 // in the correct isolate
293 void tst_QDeclarativeWorkerScript::stressDispose()
295 for (int ii = 0; ii < 100; ++ii) {
296 QDeclarativeEngine engine;
297 QDeclarativeComponent component(&engine, TESTDATA("stressDispose.qml"));
298 QObject *o = component.create();
304 QTEST_MAIN(tst_QDeclarativeWorkerScript)
306 #include "tst_qdeclarativeworkerscript.moc"