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 <QtScript/qscriptengine.h>
48 #include <QtDeclarative/qdeclarativecomponent.h>
49 #include <QtDeclarative/qdeclarativeengine.h>
50 #include <QtDeclarative/qdeclarativeitem.h>
52 #include <private/qdeclarativeworkerscript_p.h>
53 #include <private/qdeclarativeengine_p.h>
54 #include "../../../shared/util.h"
57 // In Symbian OS test data is located in applications private dir
61 inline QUrl TEST_FILE(const QString &filename)
63 QFileInfo fileInfo(__FILE__);
64 return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
68 class tst_QDeclarativeWorkerScript : public QObject
72 tst_QDeclarativeWorkerScript() {}
76 void messaging_data();
77 void messaging_sendQObjectList();
78 void messaging_sendJsObject();
79 void script_with_pragma();
80 void script_included();
81 void scriptError_onLoad();
82 void scriptError_onCall();
86 void waitForEchoMessage(QDeclarativeWorkerScript *worker) {
88 QVERIFY(connect(worker, SIGNAL(done()), &loop, SLOT(quit())));
90 timer.setSingleShot(true);
91 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
94 QVERIFY(timer.isActive());
97 QDeclarativeEngine m_engine;
100 void tst_QDeclarativeWorkerScript::source()
102 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
103 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
104 QVERIFY(worker != 0);
105 const QMetaObject *mo = worker->metaObject();
108 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
109 waitForEchoMessage(worker);
110 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
112 QUrl source = QUrl::fromLocalFile(SRCDIR "/data/script_fixed_return.js");
113 worker->setSource(source);
114 QCOMPARE(worker->source(), source);
115 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
116 waitForEchoMessage(worker);
117 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), qVariantFromValue(QString("Hello_World")));
119 qApp->processEvents();
123 void tst_QDeclarativeWorkerScript::messaging()
125 QFETCH(QVariant, value);
127 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
128 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
129 QVERIFY(worker != 0);
131 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
132 waitForEchoMessage(worker);
134 const QMetaObject *mo = worker->metaObject();
135 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
137 qApp->processEvents();
141 void tst_QDeclarativeWorkerScript::messaging_data()
143 QTest::addColumn<QVariant>("value");
145 QTest::newRow("invalid") << QVariant();
146 QTest::newRow("bool") << qVariantFromValue(true);
147 QTest::newRow("int") << qVariantFromValue(1001);
148 QTest::newRow("real") << qVariantFromValue(10334.375);
149 QTest::newRow("string") << qVariantFromValue(QString("More cheeeese, Gromit!"));
150 QTest::newRow("variant list") << qVariantFromValue((QVariantList() << "a" << "b" << "c"));
151 QTest::newRow("date time") << qVariantFromValue(QDateTime::currentDateTime());
153 // QtScript's QScriptValue -> QRegExp uses RegExp2 pattern syntax
154 QTest::newRow("regexp") << qVariantFromValue(QRegExp("^\\d\\d?$", Qt::CaseInsensitive, QRegExp::RegExp2));
158 void tst_QDeclarativeWorkerScript::messaging_sendQObjectList()
160 // Not allowed to send QObjects other than QDeclarativeWorkerListModelAgent
161 // instances. If objects are sent in a list, they will be sent as 'undefined'
164 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
165 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
166 QVERIFY(worker != 0);
168 QVariantList objects;
169 for (int i=0; i<3; i++)
170 objects << qVariantFromValue(new QObject(this));
172 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(objects))));
173 waitForEchoMessage(worker);
175 const QMetaObject *mo = worker->metaObject();
176 QVariantList result = mo->property(mo->indexOfProperty("response")).read(worker).value<QVariantList>();
177 QCOMPARE(result, (QVariantList() << QVariant() << QVariant() << QVariant()));
179 qApp->processEvents();
183 void tst_QDeclarativeWorkerScript::messaging_sendJsObject()
185 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
186 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
187 QVERIFY(worker != 0);
189 // Properties are in alphabetical order to enable string-based comparison after
190 // QVariant roundtrip, since the properties will be stored in a QVariantMap.
191 QString jsObject = "{'haste': 1125, 'name': 'zyz', 'spell power': 3101}";
194 map.insert("haste", 1125);
195 map.insert("name", "zyz");
196 map.insert("spell power", 3101);
198 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(map))));
199 waitForEchoMessage(worker);
201 QVariant result = qVariantFromValue(false);
202 QVERIFY(QMetaObject::invokeMethod(worker, "compareLiteralResponse", Qt::DirectConnection,
203 Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, jsObject)));
204 QVERIFY(result.toBool());
206 qApp->processEvents();
210 void tst_QDeclarativeWorkerScript::script_with_pragma()
214 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_pragma.qml");
215 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
216 QVERIFY(worker != 0);
218 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
219 waitForEchoMessage(worker);
221 const QMetaObject *mo = worker->metaObject();
222 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
224 qApp->processEvents();
228 void tst_QDeclarativeWorkerScript::script_included()
230 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_include.qml");
231 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
232 QVERIFY(worker != 0);
234 QString value("Hello");
236 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
237 waitForEchoMessage(worker);
239 const QMetaObject *mo = worker->metaObject();
240 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
242 qApp->processEvents();
246 static QString qdeclarativeworkerscript_lastWarning;
247 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
249 if (type == QtWarningMsg)
250 qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
253 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
255 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onLoad.qml");
257 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
258 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
259 QVERIFY(worker != 0);
261 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
262 TEST_FILE("data/script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Unexpected identifier"));
264 qInstallMsgHandler(previousMsgHandler);
265 qApp->processEvents();
269 void tst_QDeclarativeWorkerScript::scriptError_onCall()
271 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onCall.qml");
272 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
273 QVERIFY(worker != 0);
275 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
277 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
279 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
280 TEST_FILE("data/script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
282 qInstallMsgHandler(previousMsgHandler);
283 qApp->processEvents();
287 // Rapidly create and destroy worker scripts to test resources are being disposed
288 // in the correct isolate
289 void tst_QDeclarativeWorkerScript::stressDispose()
291 for (int ii = 0; ii < 100; ++ii) {
292 QDeclarativeEngine engine;
293 QDeclarativeComponent component(&engine, SRCDIR "/data/stressDispose.qml");
294 QObject *o = component.create();
300 QTEST_MAIN(tst_QDeclarativeWorkerScript)
302 #include "tst_qdeclarativeworkerscript.moc"