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 QFileInfo fileInfo(__FILE__);
58 return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
62 class tst_QDeclarativeWorkerScript : public QObject
66 tst_QDeclarativeWorkerScript() {}
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();
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, SRCDIR "/data/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(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")));
113 qApp->processEvents();
117 void tst_QDeclarativeWorkerScript::messaging()
119 QFETCH(QVariant, value);
121 QDeclarativeComponent component(&m_engine, SRCDIR "/data/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, SRCDIR "/data/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, SRCDIR "/data/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::script_with_pragma()
208 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_pragma.qml");
209 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
210 QVERIFY(worker != 0);
212 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
213 waitForEchoMessage(worker);
215 const QMetaObject *mo = worker->metaObject();
216 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
218 qApp->processEvents();
222 void tst_QDeclarativeWorkerScript::script_included()
224 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_include.qml");
225 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
226 QVERIFY(worker != 0);
228 QString value("Hello");
230 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
231 waitForEchoMessage(worker);
233 const QMetaObject *mo = worker->metaObject();
234 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
236 qApp->processEvents();
240 static QString qdeclarativeworkerscript_lastWarning;
241 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
243 if (type == QtWarningMsg)
244 qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
247 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
249 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onLoad.qml");
251 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
252 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
253 QVERIFY(worker != 0);
255 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
256 TEST_FILE("data/script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Unexpected identifier"));
258 qInstallMsgHandler(previousMsgHandler);
259 qApp->processEvents();
263 void tst_QDeclarativeWorkerScript::scriptError_onCall()
265 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onCall.qml");
266 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
267 QVERIFY(worker != 0);
269 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
271 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
273 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
274 TEST_FILE("data/script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
276 qInstallMsgHandler(previousMsgHandler);
277 qApp->processEvents();
281 // Rapidly create and destroy worker scripts to test resources are being disposed
282 // in the correct isolate
283 void tst_QDeclarativeWorkerScript::stressDispose()
285 for (int ii = 0; ii < 100; ++ii) {
286 QDeclarativeEngine engine;
287 QDeclarativeComponent component(&engine, SRCDIR "/data/stressDispose.qml");
288 QObject *o = component.create();
294 QTEST_MAIN(tst_QDeclarativeWorkerScript)
296 #include "tst_qdeclarativeworkerscript.moc"