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 ****************************************************************************/
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeComponent>
45 #include <private/qdeclarativeengine_p.h>
46 #include <private/qdeclarativeobjectscriptclass_p.h>
47 #include <private/qdeclarativerectangle_p.h>
48 #include <QScriptEngine>
49 #include <QScriptValue>
52 // In Symbian OS test data is located in applications private dir
56 class tst_script : public QObject
66 void property_getter();
67 void property_getter_js();
68 void property_getter_qobject();
69 void property_getter_qmetaproperty();
70 void property_qobject();
71 void property_qmlobject();
75 void function_qobject();
76 void function_qmlobject();
78 void function_args_js();
79 void function_args_cpp();
80 void function_args_qobject();
81 void function_args_qmlobject();
83 void signal_unconnected();
86 void signal_unusedArgs();
89 void slot_simple_js();
91 void slot_complex_js();
96 void global_property_js();
97 void global_property_qml();
98 void global_property_qml_js();
100 void scriptfile_property();
103 inline QUrl TEST_FILE(const QString &filename)
105 return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
108 class TestObject : public QObject
111 Q_PROPERTY(int x READ x)
114 TestObject(QObject *parent = 0);
118 void emitMySignal() { emit mySignal(); }
119 void emitMySignalWithArgs(int n) { emit mySignalWithArgs(n); }
123 void mySignalWithArgs(int n);
130 int methodArgs(int val) {
137 QML_DECLARE_TYPE(TestObject);
139 TestObject::TestObject(QObject *parent)
140 : QObject(parent), m_x(0)
149 void tst_script::initTestCase()
151 qmlRegisterType<TestObject>("Qt.test", 1, 0, "TestObject");
155 #define PROPERTY_PROGRAM \
156 "(function(testObject) { return (function() { " \
158 " for (var ii = 0; ii < 10000; ++ii) { " \
159 " test += testObject.x; " \
164 void tst_script::property_js()
166 QScriptEngine engine;
168 QScriptValue v = engine.newObject();
169 v.setProperty(QLatin1String("x"), 10);
171 QScriptValueList args;
173 QScriptValue prog = engine.evaluate(PROPERTY_PROGRAM).call(engine.globalObject(), args);
177 prog.call().toNumber();
181 static QScriptValue property_getter_method(QScriptContext *, QScriptEngine *engine)
184 return QScriptValue(engine,x++);
187 void tst_script::property_getter()
189 QScriptEngine engine;
191 QScriptValue v = engine.newObject();
192 v.setProperty(QLatin1String("x"), engine.newFunction(property_getter_method),
193 QScriptValue::PropertyGetter);
195 QScriptValueList args;
197 QScriptValue prog = engine.evaluate(PROPERTY_PROGRAM).call(engine.globalObject(), args);
205 static TestObject *property_getter_qobject_object = 0;
206 static QScriptValue property_getter_qobject_method(QScriptContext *, QScriptEngine *)
210 idx = TestObject::staticMetaObject.indexOfProperty("x");
213 void *args[] = { &value, 0 };
214 QMetaObject::metacall(property_getter_qobject_object, QMetaObject::ReadProperty, idx, args);
216 return QScriptValue(value);
219 static QScriptValue property_getter_qmetaproperty_method(QScriptContext *, QScriptEngine *)
223 idx = TestObject::staticMetaObject.indexOfProperty("x");
226 value = property_getter_qobject_object->metaObject()->property(idx).read(property_getter_qobject_object).toInt();
228 return QScriptValue(value);
231 void tst_script::property_getter_qobject()
233 QScriptEngine engine;
236 property_getter_qobject_object = &to;
237 QScriptValue v = engine.newObject();
238 v.setProperty(QLatin1String("x"), engine.newFunction(property_getter_qobject_method),
239 QScriptValue::PropertyGetter);
241 QScriptValueList args;
243 QScriptValue prog = engine.evaluate(PROPERTY_PROGRAM).call(engine.globalObject(), args);
249 property_getter_qobject_object = 0;
252 void tst_script::property_getter_qmetaproperty()
254 QScriptEngine engine;
257 property_getter_qobject_object = &to;
258 QScriptValue v = engine.newObject();
259 v.setProperty(QLatin1String("x"), engine.newFunction(property_getter_qmetaproperty_method),
260 QScriptValue::PropertyGetter);
262 QScriptValueList args;
264 QScriptValue prog = engine.evaluate(PROPERTY_PROGRAM).call(engine.globalObject(), args);
270 property_getter_qobject_object = 0;
274 void tst_script::property_getter_js()
276 QScriptEngine engine;
278 QScriptValue v = engine.evaluate("(function() { var o = new Object; o._x = 0; o.__defineGetter__(\"x\", function() { return this._x++; }); return o; })").call();
280 QScriptValueList args;
282 QScriptValue prog = engine.evaluate(PROPERTY_PROGRAM).call(engine.globalObject(), args);
290 void tst_script::property_qobject()
292 QScriptEngine engine;
295 QScriptValue v = engine.newQObject(&to);
297 QScriptValueList args;
299 QScriptValue prog = engine.evaluate(PROPERTY_PROGRAM).call(engine.globalObject(), args);
307 void tst_script::property_qmlobject()
309 QDeclarativeEngine qmlengine;
311 QScriptEngine *engine = QDeclarativeEnginePrivate::getScriptEngine(&qmlengine);
314 QScriptValue v = QDeclarativeEnginePrivate::get(&qmlengine)->objectClass->newQObject(&to);
316 QScriptValueList args;
318 QScriptValue prog = engine->evaluate(PROPERTY_PROGRAM).call(engine->globalObject(), args);
326 #define FUNCTION_PROGRAM \
327 "(function(testObject) { return (function() { " \
329 " for (var ii = 0; ii < 10000; ++ii) { " \
330 " test += testObject.method(); " \
335 void tst_script::function_js()
337 QScriptEngine engine;
339 QScriptValue v = engine.evaluate("(function() { var o = new Object; o._x = 0; o.method = (function() { return this._x++; }); return o; })").call();
341 QScriptValueList args;
343 QScriptValue prog = engine.evaluate(FUNCTION_PROGRAM).call(engine.globalObject(), args);
351 static QScriptValue function_method(QScriptContext *, QScriptEngine *)
354 return QScriptValue(x++);
357 void tst_script::function_cpp()
359 QScriptEngine engine;
361 QScriptValue v = engine.newObject();
362 v.setProperty(QLatin1String("method"), engine.newFunction(function_method));
364 QScriptValueList args;
366 QScriptValue prog = engine.evaluate(FUNCTION_PROGRAM).call(engine.globalObject(), args);
374 void tst_script::function_qobject()
376 QScriptEngine engine;
379 QScriptValue v = engine.newQObject(&to);
381 QScriptValueList args;
383 QScriptValue prog = engine.evaluate(FUNCTION_PROGRAM).call(engine.globalObject(), args);
391 void tst_script::function_qmlobject()
393 QDeclarativeEngine qmlengine;
395 QScriptEngine *engine = QDeclarativeEnginePrivate::getScriptEngine(&qmlengine);
398 QScriptValue v = QDeclarativeEnginePrivate::get(&qmlengine)->objectClass->newQObject(&to);
400 QScriptValueList args;
402 QScriptValue prog = engine->evaluate(FUNCTION_PROGRAM).call(engine->globalObject(), args);
410 #define FUNCTION_ARGS_PROGRAM \
411 "(function(testObject) { return (function() { " \
413 " for (var ii = 0; ii < 10000; ++ii) { " \
414 " test += testObject.methodArgs(ii); " \
419 void tst_script::function_args_js()
421 QScriptEngine engine;
423 QScriptValue v = engine.evaluate("(function() { var o = new Object; o._x = 0; o.methodArgs = (function(a) { return a + this._x++; }); return o; })").call();
425 QScriptValueList args;
427 QScriptValue prog = engine.evaluate(FUNCTION_ARGS_PROGRAM).call(engine.globalObject(), args);
435 static QScriptValue function_args_method(QScriptContext *ctxt, QScriptEngine *)
438 return QScriptValue(ctxt->argument(0).toNumber() + x++);
441 void tst_script::function_args_cpp()
443 QScriptEngine engine;
445 QScriptValue v = engine.newObject();
446 v.setProperty(QLatin1String("methodArgs"), engine.newFunction(function_args_method));
448 QScriptValueList args;
450 QScriptValue prog = engine.evaluate(FUNCTION_ARGS_PROGRAM).call(engine.globalObject(), args);
458 void tst_script::function_args_qobject()
460 QScriptEngine engine;
463 QScriptValue v = engine.newQObject(&to);
465 QScriptValueList args;
467 QScriptValue prog = engine.evaluate(FUNCTION_ARGS_PROGRAM).call(engine.globalObject(), args);
475 void tst_script::function_args_qmlobject()
477 QDeclarativeEngine qmlengine;
479 QScriptEngine *engine = QDeclarativeEnginePrivate::getScriptEngine(&qmlengine);
482 QScriptValue v = QDeclarativeEnginePrivate::get(&qmlengine)->objectClass->newQObject(&to);
484 QScriptValueList args;
486 QScriptValue prog = engine->evaluate(FUNCTION_ARGS_PROGRAM).call(engine->globalObject(), args);
494 void tst_script::signal_unconnected()
496 QDeclarativeEngine engine;
497 QDeclarativeComponent component(&engine, TEST_FILE("signal_unconnected.qml"));
498 TestObject *object = qobject_cast<TestObject *>(component.create());
499 QVERIFY(object != 0);
502 object->emitMySignal();
508 void tst_script::signal_qml()
510 QDeclarativeEngine engine;
511 QDeclarativeComponent component(&engine, TEST_FILE("signal_qml.qml"));
512 TestObject *object = qobject_cast<TestObject *>(component.create());
513 QVERIFY(object != 0);
516 object->emitMySignal();
522 void tst_script::signal_args()
524 QDeclarativeEngine engine;
525 QDeclarativeComponent component(&engine, TEST_FILE("signal_args.qml"));
526 TestObject *object = qobject_cast<TestObject *>(component.create());
527 QVERIFY(object != 0);
530 object->emitMySignalWithArgs(11);
536 void tst_script::signal_unusedArgs()
538 QDeclarativeEngine engine;
539 QDeclarativeComponent component(&engine, TEST_FILE("signal_unusedArgs.qml"));
540 TestObject *object = qobject_cast<TestObject *>(component.create());
541 QVERIFY(object != 0);
544 object->emitMySignalWithArgs(11);
550 void tst_script::slot_simple()
552 QDeclarativeEngine engine;
553 QDeclarativeComponent component(&engine, TEST_FILE("slot_simple.qml"));
554 TestObject *object = qobject_cast<TestObject *>(component.create());
555 QVERIFY(object != 0);
558 object->emitMySignal();
564 void tst_script::slot_simple_js()
566 QDeclarativeEngine engine;
567 QDeclarativeComponent component(&engine, TEST_FILE("slot_simple_js.qml"));
568 TestObject *object = qobject_cast<TestObject *>(component.create());
569 QVERIFY(object != 0);
572 object->emitMySignal();
578 void tst_script::slot_complex()
580 QDeclarativeEngine engine;
581 QDeclarativeComponent component(&engine, TEST_FILE("slot_complex.qml"));
582 TestObject *object = qobject_cast<TestObject *>(component.create());
583 QVERIFY(object != 0);
586 object->emitMySignal();
592 void tst_script::slot_complex_js()
594 QDeclarativeEngine engine;
595 QDeclarativeComponent component(&engine, TEST_FILE("slot_complex_js.qml"));
596 TestObject *object = qobject_cast<TestObject *>(component.create());
597 QVERIFY(object != 0);
600 object->emitMySignal();
606 void tst_script::block_data()
608 QTest::addColumn<QString>("methodName");
609 QTest::newRow("direct") << "doSomethingDirect()";
610 QTest::newRow("localObj") << "doSomethingLocalObj()";
611 QTest::newRow("local") << "doSomethingLocal()";
614 void tst_script::block()
616 QFETCH(QString, methodName);
617 QDeclarativeEngine engine;
618 QDeclarativeComponent component(&engine, TEST_FILE("block.qml"));
619 QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle *>(component.create());
622 int index = rect->metaObject()->indexOfMethod(methodName.toUtf8());
623 QVERIFY(index != -1);
624 QMetaMethod method = rect->metaObject()->method(index);
627 method.invoke(rect, Qt::DirectConnection);
633 #define GLOBALPROPERTY_PROGRAM \
635 " for (var ii = 0; ii < 10000; ++ii) { " \
640 void tst_script::global_property_js()
642 QScriptEngine engine;
644 QScriptValue prog = engine.evaluate(GLOBALPROPERTY_PROGRAM);
652 void tst_script::global_property_qml()
654 QDeclarativeEngine qmlengine;
656 QScriptEngine *engine = QDeclarativeEnginePrivate::getScriptEngine(&qmlengine);
657 QScriptValue prog = engine->evaluate(GLOBALPROPERTY_PROGRAM);
665 void tst_script::global_property_qml_js()
667 QDeclarativeEngine engine;
668 QDeclarativeComponent component(&engine, TEST_FILE("global_prop.qml"));
669 QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle *>(component.create());
672 int index = rect->metaObject()->indexOfMethod("triggered()");
673 QVERIFY(index != -1);
674 QMetaMethod method = rect->metaObject()->method(index);
677 method.invoke(rect, Qt::DirectConnection);
683 void tst_script::scriptfile_property()
685 QDeclarativeEngine engine;
686 QDeclarativeComponent component(&engine, TEST_FILE("global_prop.qml"));
687 QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle *>(component.create());
690 int index = rect->metaObject()->indexOfMethod("incrementTriggered()");
691 QVERIFY(index != -1);
692 QMetaMethod method = rect->metaObject()->method(index);
695 method.invoke(rect, Qt::DirectConnection);
701 QTEST_MAIN(tst_script)
703 #include "tst_script.moc"