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"
56 // In Symbian OS test data is located in applications private dir
60 inline QUrl TEST_FILE(const QString &filename)
62 QFileInfo fileInfo(__FILE__);
63 return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
67 class tst_QDeclarativeWorkerScript : public QObject
71 tst_QDeclarativeWorkerScript() {}
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();
85 void waitForEchoMessage(QDeclarativeWorkerScript *worker) {
87 QVERIFY(connect(worker, SIGNAL(done()), &loop, SLOT(quit())));
89 timer.setSingleShot(true);
90 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
93 QVERIFY(timer.isActive());
96 QDeclarativeEngine m_engine;
99 void tst_QDeclarativeWorkerScript::source()
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();
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);
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")));
118 qApp->processEvents();
122 void tst_QDeclarativeWorkerScript::messaging()
124 QFETCH(QVariant, value);
126 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
127 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
128 QVERIFY(worker != 0);
130 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
131 waitForEchoMessage(worker);
133 const QMetaObject *mo = worker->metaObject();
134 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
136 qApp->processEvents();
140 void tst_QDeclarativeWorkerScript::messaging_data()
142 QTest::addColumn<QVariant>("value");
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());
152 // QtScript's QScriptValue -> QRegExp uses RegExp2 pattern syntax
153 QTest::newRow("regexp") << qVariantFromValue(QRegExp("^\\d\\d?$", Qt::CaseInsensitive, QRegExp::RegExp2));
157 void tst_QDeclarativeWorkerScript::messaging_sendQObjectList()
159 // Not allowed to send QObjects other than QDeclarativeWorkerListModelAgent
160 // instances. If objects are sent in a list, they will be sent as 'undefined'
163 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
164 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
165 QVERIFY(worker != 0);
167 QVariantList objects;
168 for (int i=0; i<3; i++)
169 objects << qVariantFromValue(new QObject(this));
171 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(objects))));
172 waitForEchoMessage(worker);
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()));
178 qApp->processEvents();
182 void tst_QDeclarativeWorkerScript::messaging_sendJsObject()
184 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
185 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
186 QVERIFY(worker != 0);
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}";
193 map.insert("haste", 1125);
194 map.insert("name", "zyz");
195 map.insert("spell power", 3101);
197 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(map))));
198 waitForEchoMessage(worker);
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());
205 qApp->processEvents();
209 void tst_QDeclarativeWorkerScript::script_with_pragma()
213 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_pragma.qml");
214 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
215 QVERIFY(worker != 0);
217 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
218 waitForEchoMessage(worker);
220 const QMetaObject *mo = worker->metaObject();
221 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
223 qApp->processEvents();
227 void tst_QDeclarativeWorkerScript::script_included()
229 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_include.qml");
230 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
231 QVERIFY(worker != 0);
233 QString value("Hello");
235 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
236 waitForEchoMessage(worker);
238 const QMetaObject *mo = worker->metaObject();
239 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
241 qApp->processEvents();
245 static QString qdeclarativeworkerscript_lastWarning;
246 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
248 if (type == QtWarningMsg)
249 qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
252 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
254 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onLoad.qml");
256 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
257 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
258 QVERIFY(worker != 0);
260 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
261 TEST_FILE("data/script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Unexpected identifier"));
263 qInstallMsgHandler(previousMsgHandler);
264 qApp->processEvents();
268 void tst_QDeclarativeWorkerScript::scriptError_onCall()
270 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onCall.qml");
271 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
272 QVERIFY(worker != 0);
274 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
276 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
278 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
279 TEST_FILE("data/script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
281 qInstallMsgHandler(previousMsgHandler);
282 qApp->processEvents();
286 // Rapidly create and destroy worker scripts to test resources are being disposed
287 // in the correct isolate
288 void tst_QDeclarativeWorkerScript::stressDispose()
290 for (int ii = 0; ii < 100; ++ii) {
291 QDeclarativeEngine engine;
292 QDeclarativeComponent component(&engine, SRCDIR "/data/stressDispose.qml");
293 QObject *o = component.create();
299 QTEST_MAIN(tst_QDeclarativeWorkerScript)
301 #include "tst_qdeclarativeworkerscript.moc"