Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeworkerscript / tst_qdeclarativeworkerscript.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
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>
47
48 #include <QtDeclarative/qdeclarativecomponent.h>
49 #include <QtDeclarative/qdeclarativeengine.h>
50 #include <QtDeclarative/qdeclarativeitem.h>
51
52 #include <private/qdeclarativeworkerscript_p.h>
53 #include <private/qdeclarativeengine_p.h>
54 #include "../../../shared/util.h"
55
56 Q_DECLARE_METATYPE(QScriptValue)
57
58 #ifdef Q_OS_SYMBIAN
59 // In Symbian OS test data is located in applications private dir
60 #define SRCDIR "."
61 #endif
62
63 inline QUrl TEST_FILE(const QString &filename)
64 {
65     QFileInfo fileInfo(__FILE__);
66     return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
67 }
68
69
70 class tst_QDeclarativeWorkerScript : public QObject
71 {
72     Q_OBJECT
73 public:
74     tst_QDeclarativeWorkerScript() {}
75 private slots:
76     void source();
77     void messaging();
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();
85
86 private:
87     void waitForEchoMessage(QDeclarativeWorkerScript *worker) {
88         QEventLoop loop;
89         QVERIFY(connect(worker, SIGNAL(done()), &loop, SLOT(quit())));
90         QTimer timer;
91         timer.setSingleShot(true);
92         connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
93         timer.start(10000);
94         loop.exec();
95         QVERIFY(timer.isActive());
96     }
97
98     QDeclarativeEngine m_engine;
99 };
100
101 void tst_QDeclarativeWorkerScript::source()
102 {
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();
107
108     QVariant value(100);
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);
112
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")));
119
120     qApp->processEvents();
121     delete worker;
122 }
123
124 void tst_QDeclarativeWorkerScript::messaging()
125 {
126     QFETCH(QVariant, value);
127
128     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
129     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
130     QVERIFY(worker != 0);
131
132     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
133     waitForEchoMessage(worker);
134
135     const QMetaObject *mo = worker->metaObject();
136     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
137
138     qApp->processEvents();
139     delete worker;
140 }
141
142 void tst_QDeclarativeWorkerScript::messaging_data()
143 {
144     QTest::addColumn<QVariant>("value");
145
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());
153 #ifndef QT_NO_REGEXP
154     // QtScript's QScriptValue -> QRegExp uses RegExp2 pattern syntax
155     QTest::newRow("regexp") << qVariantFromValue(QRegExp("^\\d\\d?$", Qt::CaseInsensitive, QRegExp::RegExp2));
156 #endif
157 }
158
159 void tst_QDeclarativeWorkerScript::messaging_sendQObjectList()
160 {
161     // Not allowed to send QObjects other than QDeclarativeWorkerListModelAgent
162     // instances. If objects are sent in a list, they will be sent as 'undefined'
163     // js values.
164
165     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
166     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
167     QVERIFY(worker != 0);
168
169     QVariantList objects;
170     for (int i=0; i<3; i++)
171         objects << qVariantFromValue(new QObject(this));
172
173     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(objects))));
174     waitForEchoMessage(worker);
175
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()));
179
180     qApp->processEvents();
181     delete worker;
182 }
183
184 void tst_QDeclarativeWorkerScript::messaging_sendJsObject()
185 {
186     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml");
187     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
188     QVERIFY(worker != 0);
189
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}";
193
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);
199
200     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, qVariantFromValue(sv))));
201     waitForEchoMessage(worker);
202
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());
207
208     qApp->processEvents();
209     delete worker;
210 }
211
212 void tst_QDeclarativeWorkerScript::script_with_pragma()
213 {
214     QVariant value(100);
215
216     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_pragma.qml");
217     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
218     QVERIFY(worker != 0);
219
220     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
221     waitForEchoMessage(worker);
222
223     const QMetaObject *mo = worker->metaObject();
224     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
225
226     qApp->processEvents();
227     delete worker;
228 }
229
230 void tst_QDeclarativeWorkerScript::script_included()
231 {
232     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_include.qml");
233     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
234     QVERIFY(worker != 0);
235
236     QString value("Hello");
237
238     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
239     waitForEchoMessage(worker);
240
241     const QMetaObject *mo = worker->metaObject();
242     QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).toString(), value + " World");
243
244     qApp->processEvents();
245     delete worker;
246 }
247
248 static QString qdeclarativeworkerscript_lastWarning;
249 static void qdeclarativeworkerscript_warningsHandler(QtMsgType type, const char *msg)
250 {
251     if (type == QtWarningMsg)
252          qdeclarativeworkerscript_lastWarning = QString::fromUtf8(msg);
253 }
254
255 void tst_QDeclarativeWorkerScript::scriptError_onLoad()
256 {
257     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onLoad.qml");
258
259     QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
260     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
261     QVERIFY(worker != 0);
262
263     QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
264             TEST_FILE("data/script_error_onLoad.js").toString() + QLatin1String(":3: SyntaxError: Parse error"));
265
266     qInstallMsgHandler(previousMsgHandler);
267     qApp->processEvents();
268     delete worker;
269 }
270
271 void tst_QDeclarativeWorkerScript::scriptError_onCall()
272 {
273     QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_error_onCall.qml");
274     QDeclarativeWorkerScript *worker = qobject_cast<QDeclarativeWorkerScript*>(component.create());
275     QVERIFY(worker != 0);
276
277     QtMsgHandler previousMsgHandler = qInstallMsgHandler(qdeclarativeworkerscript_warningsHandler);
278     QVariant value;
279     QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
280
281     QTRY_COMPARE(qdeclarativeworkerscript_lastWarning,
282             TEST_FILE("data/script_error_onCall.js").toString() + QLatin1String(":4: ReferenceError: Can't find variable: getData"));
283
284     qInstallMsgHandler(previousMsgHandler);
285     qApp->processEvents();
286     delete worker;
287 }
288
289
290 QTEST_MAIN(tst_QDeclarativeWorkerScript)
291
292 #include "tst_qdeclarativeworkerscript.moc"