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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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();
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();
287 QTEST_MAIN(tst_QDeclarativeWorkerScript)
289 #include "tst_qdeclarativeworkerscript.moc"