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"
56 Q_DECLARE_METATYPE(QScriptValue)
59 // In Symbian OS test data is located in applications private dir
63 inline QUrl TEST_FILE(const QString &filename)
65 QFileInfo fileInfo(__FILE__);
66 return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
70 class tst_QDeclarativeWorkerScript : public QObject
74 tst_QDeclarativeWorkerScript() {}
78 void messaging_data();
79 void messaging_sendQObjectList();
80 void messaging_sendJsObject();
81 void script_with_pragma();
82 void script_included();
83 void scriptError_onLoad();
84 void scriptError_onCall();
87 void waitForEchoMessage(QDeclarativeWorkerScript *worker) {
89 QVERIFY(connect(worker, SIGNAL(done()), &loop, SLOT(quit())));
91 timer.setSingleShot(true);
92 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
95 QVERIFY(timer.isActive());
98 QDeclarativeEngine m_engine;
101 void tst_QDeclarativeWorkerScript::source()
103 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
104 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
105 QVERIFY(worker != 0);
106 const QMetaObject *mo = worker->metaObject();
109 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
110 waitForEchoMessage(worker);
111 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
113 QUrl source = QUrl::fromLocalFile(SRCDIR "/data/script_fixed_return.js");
114 worker->setSource(source);
115 QCOMPARE(worker->source(), source);
116 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
117 waitForEchoMessage(worker);
118 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), qVariantFromValue(QString("Hello_World")));
120 qApp->processEvents();
124 void tst_QDeclarativeWorkerScript::messaging()
126 QFETCH(QVariant, value);
128 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
129 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
130 QVERIFY(worker != 0);
132 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
133 waitForEchoMessage(worker);
135 const QMetaObject *mo = worker->metaObject();
136 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
138 qApp->processEvents();
142 void tst_QDeclarativeWorkerScript::messaging_data()
144 QTest::addColumn<QVariant>("value");
146 QTest::newRow("invalid") << QVariant();
147 QTest::newRow("bool") << qVariantFromValue(true);
148 QTest::newRow("int") << qVariantFromValue(1001);
149 QTest::newRow("real") << qVariantFromValue(10334.375);
150 QTest::newRow("string") << qVariantFromValue(QString("More cheeeese, Gromit!"));
151 QTest::newRow("variant list") << qVariantFromValue((QVariantList() << "a" << "b" << "c"));
152 QTest::newRow("date time") << qVariantFromValue(QDateTime::currentDateTime());
154 // QtScript's QScriptValue -> QRegExp uses RegExp2 pattern syntax
155 QTest::newRow("regexp") << qVariantFromValue(QRegExp("^\\d\\d?$", Qt::CaseInsensitive, QRegExp::RegExp2));
159 void tst_QDeclarativeWorkerScript::messaging_sendQObjectList()
161 // Not allowed to send QObjects other than QDeclarativeWorkerListModelAgent
162 // instances. If objects are sent in a list, they will be sent as 'undefined'
165 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
166 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
167 QVERIFY(worker != 0);
169 QVariantList objects;
170 for (int i=0; i<3; i++)
171 objects << qVariantFromValue(new QObject(this));
173 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(objects))));
174 waitForEchoMessage(worker);
176 const QMetaObject *mo = worker->metaObject();
177 QVariantList result = mo->property(mo->indexOfProperty("response")).read(worker).value<QVariantList>();
178 QCOMPARE(result, (QVariantList() << QVariant() << QVariant() << QVariant()));
180 qApp->processEvents();
184 void tst_QDeclarativeWorkerScript::messaging_sendJsObject()
186 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
187 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
188 QVERIFY(worker != 0);
190 // Properties are in alphabetical order to enable string-based comparison after
191 // QVariant roundtrip, since the properties will be stored in a QVariantMap.
192 QString jsObject = "{'haste': 1125, 'name': 'zyz', 'spell power': 3101}";
194 QScriptEngine *engine = QDeclarativeEnginePrivate::getScriptEngine(qmlEngine(worker));
195 QScriptValue sv = engine->newObject();
196 sv.setProperty("haste", 1125);
197 sv.setProperty("name", "zyz");
198 sv.setProperty("spell power", 3101);
200 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(sv))));
201 waitForEchoMessage(worker);
203 QVariant result = qVariantFromValue(false);
204 QVERIFY(QMetaObject::invokeMethod(worker, "compareLiteralResponse", Qt::DirectConnection,
205 Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, jsObject)));
206 QVERIFY(result.toBool());
208 qApp->processEvents();
212 void tst_QDeclarativeWorkerScript::script_with_pragma()
216 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_pragma.qml");
217 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
218 QVERIFY(worker != 0);
220 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
221 waitForEchoMessage(worker);
223 const QMetaObject *mo = worker->metaObject();
224 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
226 qApp->processEvents();
230 void tst_QDeclarativeWorkerScript::script_included()
232 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_include.qml");
233 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
234 QVERIFY(worker != 0);
236 QString value("Hello");
238 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
239 waitForEchoMessage(worker);
241 const QMetaObject *mo = worker->metaObject();
242 QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
244 qApp->processEvents();
248 static QString qdeclarativeworkerscript_lastWarning;
249 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
251 if (type == QtWarningMsg)
252 qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
255 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
257 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onLoad.qml");
259 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
260 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
261 QVERIFY(worker != 0);
263 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
264 TEST_FILE("data/script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Parse error"));
266 qInstallMsgHandler(previousMsgHandler);
267 qApp->processEvents();
271 void tst_QDeclarativeWorkerScript::scriptError_onCall()
273 QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onCall.qml");
274 QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
275 QVERIFY(worker != 0);
277 QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
279 QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
281 QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
282 TEST_FILE("data/script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
284 qInstallMsgHandler(previousMsgHandler);
285 qApp->processEvents();
290 QTEST_MAIN(tst_QDeclarativeWorkerScript)
292 #include "tst_qdeclarativeworkerscript.moc"