50 #include <QtCore/qnumeric.h> |
50 #include <QtCore/qnumeric.h> |
51 #include <private/qdeclarativeengine_p.h> |
51 #include <private/qdeclarativeengine_p.h> |
52 #include <private/qdeclarativeglobalscriptclass_p.h> |
52 #include <private/qdeclarativeglobalscriptclass_p.h> |
53 #include "testtypes.h" |
53 #include "testtypes.h" |
54 #include "testhttpserver.h" |
54 #include "testhttpserver.h" |
|
55 |
|
56 #ifdef Q_OS_SYMBIAN |
|
57 // In Symbian OS test data is located in applications private dir |
|
58 #define SRCDIR "." |
|
59 #endif |
55 |
60 |
56 /* |
61 /* |
57 This test covers evaluation of ECMAScript expressions and bindings from within |
62 This test covers evaluation of ECMAScript expressions and bindings from within |
58 QML. This does not include static QML language issues. |
63 QML. This does not include static QML language issues. |
59 |
64 |
170 QCOMPARE(object->enumProperty(), MyTypeObject::EnumVal2); |
177 QCOMPARE(object->enumProperty(), MyTypeObject::EnumVal2); |
171 QCOMPARE(object->stringProperty(), QString("Hello World!")); |
178 QCOMPARE(object->stringProperty(), QString("Hello World!")); |
172 QCOMPARE(object->uintProperty(), uint(10)); |
179 QCOMPARE(object->uintProperty(), uint(10)); |
173 QCOMPARE(object->intProperty(), -19); |
180 QCOMPARE(object->intProperty(), -19); |
174 QCOMPARE((float)object->realProperty(), float(23.2)); |
181 QCOMPARE((float)object->realProperty(), float(23.2)); |
175 QCOMPARE((float)object->doubleProperty(), float(-19.7)); |
182 QCOMPARE((float)object->doubleProperty(), float(-19.75)); |
176 QCOMPARE((float)object->floatProperty(), float(8.5)); |
183 QCOMPARE((float)object->floatProperty(), float(8.5)); |
177 QCOMPARE(object->colorProperty(), QColor("red")); |
184 QCOMPARE(object->colorProperty(), QColor("red")); |
178 QCOMPARE(object->dateProperty(), QDate(1982, 11, 25)); |
185 QCOMPARE(object->dateProperty(), QDate(1982, 11, 25)); |
179 QCOMPARE(object->timeProperty(), QTime(11, 11, 32)); |
186 QCOMPARE(object->timeProperty(), QTime(11, 11, 32)); |
180 QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1))); |
187 QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1))); |
198 QCOMPARE(object->enumProperty(), MyTypeObject::EnumVal2); |
205 QCOMPARE(object->enumProperty(), MyTypeObject::EnumVal2); |
199 QCOMPARE(object->stringProperty(), QString("Hello World!")); |
206 QCOMPARE(object->stringProperty(), QString("Hello World!")); |
200 QCOMPARE(object->uintProperty(), uint(10)); |
207 QCOMPARE(object->uintProperty(), uint(10)); |
201 QCOMPARE(object->intProperty(), -19); |
208 QCOMPARE(object->intProperty(), -19); |
202 QCOMPARE((float)object->realProperty(), float(23.2)); |
209 QCOMPARE((float)object->realProperty(), float(23.2)); |
203 QCOMPARE((float)object->doubleProperty(), float(-19.7)); |
210 QCOMPARE((float)object->doubleProperty(), float(-19.75)); |
204 QCOMPARE((float)object->floatProperty(), float(8.5)); |
211 QCOMPARE((float)object->floatProperty(), float(8.5)); |
205 QCOMPARE(object->colorProperty(), QColor("red")); |
212 QCOMPARE(object->colorProperty(), QColor("red")); |
206 QCOMPARE(object->dateProperty(), QDate(1982, 11, 25)); |
213 QCOMPARE(object->dateProperty(), QDate(1982, 11, 25)); |
207 QCOMPARE(object->timeProperty(), QTime(11, 11, 32)); |
214 QCOMPARE(object->timeProperty(), QTime(11, 11, 32)); |
208 QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1))); |
215 QCOMPARE(object->dateTimeProperty(), QDateTime(QDate(2009, 5, 12), QTime(13, 22, 1))); |
271 { |
278 { |
272 QDeclarativeComponent component(&engine, TEST_FILE("signalAssignment.2.qml")); |
279 QDeclarativeComponent component(&engine, TEST_FILE("signalAssignment.2.qml")); |
273 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
280 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
274 QVERIFY(object != 0); |
281 QVERIFY(object != 0); |
275 QCOMPARE(object->string(), QString()); |
282 QCOMPARE(object->string(), QString()); |
276 emit object->argumentSignal(19, "Hello world!", 10.3); |
283 emit object->argumentSignal(19, "Hello world!", 10.25); |
277 QCOMPARE(object->string(), QString("pass 19 Hello world! 10.3")); |
284 QCOMPARE(object->string(), QString("pass 19 Hello world! 10.25")); |
278 } |
285 } |
279 } |
286 } |
280 |
287 |
281 void tst_qdeclarativeecmascript::methods() |
288 void tst_qdeclarativeecmascript::methods() |
282 { |
289 { |
995 { |
1002 { |
996 QDeclarativeComponent component(&engine, TEST_FILE("scriptErrors.qml")); |
1003 QDeclarativeComponent component(&engine, TEST_FILE("scriptErrors.qml")); |
997 QString url = component.url().toString(); |
1004 QString url = component.url().toString(); |
998 |
1005 |
999 QString warning1 = url.left(url.length() - 3) + "js:2: Error: Invalid write to global property \"a\""; |
1006 QString warning1 = url.left(url.length() - 3) + "js:2: Error: Invalid write to global property \"a\""; |
1000 QString warning2 = url + ":5: TypeError: Result of expression 'a' [undefined] is not an object."; |
1007 QString warning2 = url + ":5: ReferenceError: Can't find variable: a"; |
1001 QString warning3 = url.left(url.length() - 3) + "js:4: Error: Invalid write to global property \"a\""; |
1008 QString warning3 = url.left(url.length() - 3) + "js:4: Error: Invalid write to global property \"a\""; |
1002 QString warning4 = url + ":10: TypeError: Result of expression 'a' [undefined] is not an object."; |
1009 QString warning4 = url + ":10: ReferenceError: Can't find variable: a"; |
1003 QString warning5 = url + ":8: TypeError: Result of expression 'a' [undefined] is not an object."; |
1010 QString warning5 = url + ":8: ReferenceError: Can't find variable: a"; |
1004 QString warning6 = url + ":7: Unable to assign [undefined] to int x"; |
1011 QString warning6 = url + ":7: Unable to assign [undefined] to int x"; |
1005 QString warning7 = url + ":12: Error: Cannot assign to read-only property \"trueProperty\""; |
1012 QString warning7 = url + ":12: Error: Cannot assign to read-only property \"trueProperty\""; |
1006 QString warning8 = url + ":13: Error: Cannot assign to non-existent property \"fakeProperty\""; |
1013 QString warning8 = url + ":13: Error: Cannot assign to non-existent property \"fakeProperty\""; |
1007 |
1014 |
1008 QTest::ignoreMessage(QtWarningMsg, warning1.toLatin1().constData()); |
1015 QTest::ignoreMessage(QtWarningMsg, warning1.toLatin1().constData()); |
1315 MyInvokableObject o; |
1322 MyInvokableObject o; |
1316 |
1323 |
1317 QDeclarativeEngine qmlengine; |
1324 QDeclarativeEngine qmlengine; |
1318 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(&qmlengine); |
1325 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(&qmlengine); |
1319 QScriptEngine *engine = &ep->scriptEngine; |
1326 QScriptEngine *engine = &ep->scriptEngine; |
1320 ep->globalClass->explicitSetProperty("object", ep->objectClass->newQObject(&o)); |
1327 |
|
1328 QStringList names; QList<QScriptValue> values; |
|
1329 names << QLatin1String("object"); values << ep->objectClass->newQObject(&o); |
|
1330 names << QLatin1String("undefined"); values << engine->undefinedValue(); |
|
1331 |
|
1332 ep->globalClass->explicitSetProperty(names, values); |
1321 |
1333 |
1322 // Non-existent methods |
1334 // Non-existent methods |
1323 o.reset(); |
1335 o.reset(); |
1324 QCOMPARE(engine->evaluate("object.method_nonexistent()").isError(), true); |
1336 QCOMPARE(engine->evaluate("object.method_nonexistent()").isError(), true); |
1325 QCOMPARE(o.error(), false); |
1337 QCOMPARE(o.error(), false); |
1373 QCOMPARE(o.error(), false); |
1385 QCOMPARE(o.error(), false); |
1374 QCOMPARE(o.invoked(), 1); |
1386 QCOMPARE(o.invoked(), 1); |
1375 QCOMPARE(o.actuals().count(), 0); |
1387 QCOMPARE(o.actuals().count(), 0); |
1376 |
1388 |
1377 o.reset(); |
1389 o.reset(); |
1378 QVERIFY(engine->evaluate("object.method_NoArgs_real()").strictlyEquals(QScriptValue(engine, 19.7))); |
1390 QVERIFY(engine->evaluate("object.method_NoArgs_real()").strictlyEquals(QScriptValue(engine, 19.75))); |
1379 QCOMPARE(o.error(), false); |
1391 QCOMPARE(o.error(), false); |
1380 QCOMPARE(o.invoked(), 2); |
1392 QCOMPARE(o.invoked(), 2); |
1381 QCOMPARE(o.actuals().count(), 0); |
1393 QCOMPARE(o.actuals().count(), 0); |
1382 |
1394 |
1383 o.reset(); |
1395 o.reset(); |
1707 QCOMPARE(o.error(), false); |
1719 QCOMPARE(o.error(), false); |
1708 QCOMPARE(o.invoked(), 17); |
1720 QCOMPARE(o.invoked(), 17); |
1709 QCOMPARE(o.actuals().count(), 2); |
1721 QCOMPARE(o.actuals().count(), 2); |
1710 QCOMPARE(o.actuals().at(0), QVariant(10)); |
1722 QCOMPARE(o.actuals().at(0), QVariant(10)); |
1711 QCOMPARE(o.actuals().at(1), QVariant(11)); |
1723 QCOMPARE(o.actuals().at(1), QVariant(11)); |
|
1724 |
|
1725 o.reset(); |
|
1726 QCOMPARE(engine->evaluate("object.method_with_enum(9)").isUndefined(), true); |
|
1727 QCOMPARE(o.error(), false); |
|
1728 QCOMPARE(o.invoked(), 18); |
|
1729 QCOMPARE(o.actuals().count(), 1); |
|
1730 QCOMPARE(o.actuals().at(0), QVariant(9)); |
1712 } |
1731 } |
1713 |
1732 |
1714 // QTBUG-5675 |
1733 // QTBUG-5675 |
1715 void tst_qdeclarativeecmascript::listToVariant() |
1734 void tst_qdeclarativeecmascript::listToVariant() |
1716 { |
1735 { |
1797 |
1816 |
1798 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1817 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1799 QVERIFY(object != 0); |
1818 QVERIFY(object != 0); |
1800 |
1819 |
1801 QCOMPARE(object->property("test").toBool(), false); |
1820 QCOMPARE(object->property("test").toBool(), false); |
1802 emit object->argumentSignal(19, "Hello world!", 10.3); |
1821 emit object->argumentSignal(19, "Hello world!", 10.25); |
1803 QCOMPARE(object->property("test").toBool(), true); |
1822 QCOMPARE(object->property("test").toBool(), true); |
1804 |
1823 |
1805 delete object; |
1824 delete object; |
1806 } |
1825 } |
1807 |
1826 |
1810 |
1829 |
1811 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1830 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1812 QVERIFY(object != 0); |
1831 QVERIFY(object != 0); |
1813 |
1832 |
1814 QCOMPARE(object->property("test").toBool(), false); |
1833 QCOMPARE(object->property("test").toBool(), false); |
1815 emit object->argumentSignal(19, "Hello world!", 10.3); |
1834 emit object->argumentSignal(19, "Hello world!", 10.25); |
1816 QCOMPARE(object->property("test").toBool(), true); |
1835 QCOMPARE(object->property("test").toBool(), true); |
1817 |
1836 |
1818 delete object; |
1837 delete object; |
1819 } |
1838 } |
1820 |
1839 |
1823 |
1842 |
1824 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1843 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1825 QVERIFY(object != 0); |
1844 QVERIFY(object != 0); |
1826 |
1845 |
1827 QCOMPARE(object->property("test").toBool(), false); |
1846 QCOMPARE(object->property("test").toBool(), false); |
1828 emit object->argumentSignal(19, "Hello world!", 10.3); |
1847 emit object->argumentSignal(19, "Hello world!", 10.25); |
1829 QCOMPARE(object->property("test").toBool(), true); |
1848 QCOMPARE(object->property("test").toBool(), true); |
1830 |
1849 |
1831 delete object; |
1850 delete object; |
1832 } |
1851 } |
1833 |
1852 |
1836 |
1855 |
1837 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1856 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1838 QVERIFY(object != 0); |
1857 QVERIFY(object != 0); |
1839 |
1858 |
1840 QCOMPARE(object->methodCalled(), false); |
1859 QCOMPARE(object->methodCalled(), false); |
1841 emit object->argumentSignal(19, "Hello world!", 10.3); |
1860 emit object->argumentSignal(19, "Hello world!", 10.25); |
1842 QCOMPARE(object->methodCalled(), true); |
1861 QCOMPARE(object->methodCalled(), true); |
1843 |
1862 |
1844 delete object; |
1863 delete object; |
1845 } |
1864 } |
1846 |
1865 |
1849 |
1868 |
1850 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1869 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1851 QVERIFY(object != 0); |
1870 QVERIFY(object != 0); |
1852 |
1871 |
1853 QCOMPARE(object->methodCalled(), false); |
1872 QCOMPARE(object->methodCalled(), false); |
1854 emit object->argumentSignal(19, "Hello world!", 10.3); |
1873 emit object->argumentSignal(19, "Hello world!", 10.25); |
1855 QCOMPARE(object->methodCalled(), true); |
1874 QCOMPARE(object->methodCalled(), true); |
1856 |
1875 |
1857 delete object; |
1876 delete object; |
1858 } |
1877 } |
1859 |
1878 |
1862 |
1881 |
1863 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1882 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1864 QVERIFY(object != 0); |
1883 QVERIFY(object != 0); |
1865 |
1884 |
1866 QCOMPARE(object->property("test").toInt(), 0); |
1885 QCOMPARE(object->property("test").toInt(), 0); |
1867 emit object->argumentSignal(19, "Hello world!", 10.3); |
1886 emit object->argumentSignal(19, "Hello world!", 10.25); |
1868 QCOMPARE(object->property("test").toInt(), 2); |
1887 QCOMPARE(object->property("test").toInt(), 2); |
1869 |
1888 |
1870 delete object; |
1889 delete object; |
1871 } |
1890 } |
1872 } |
1891 } |
1878 |
1897 |
1879 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1898 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1880 QVERIFY(object != 0); |
1899 QVERIFY(object != 0); |
1881 |
1900 |
1882 QCOMPARE(object->property("test").toInt(), 0); |
1901 QCOMPARE(object->property("test").toInt(), 0); |
1883 emit object->argumentSignal(19, "Hello world!", 10.3); |
1902 emit object->argumentSignal(19, "Hello world!", 10.25); |
1884 QCOMPARE(object->property("test").toInt(), 1); |
1903 QCOMPARE(object->property("test").toInt(), 1); |
1885 emit object->argumentSignal(19, "Hello world!", 10.3); |
1904 emit object->argumentSignal(19, "Hello world!", 10.25); |
1886 QCOMPARE(object->property("test").toInt(), 2); |
1905 QCOMPARE(object->property("test").toInt(), 2); |
1887 emit object->basicSignal(); |
1906 emit object->basicSignal(); |
1888 QCOMPARE(object->property("test").toInt(), 2); |
1907 QCOMPARE(object->property("test").toInt(), 2); |
1889 emit object->argumentSignal(19, "Hello world!", 10.3); |
1908 emit object->argumentSignal(19, "Hello world!", 10.25); |
1890 QCOMPARE(object->property("test").toInt(), 2); |
1909 QCOMPARE(object->property("test").toInt(), 2); |
1891 |
1910 |
1892 delete object; |
1911 delete object; |
1893 } |
1912 } |
1894 |
1913 |
1897 |
1916 |
1898 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1917 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1899 QVERIFY(object != 0); |
1918 QVERIFY(object != 0); |
1900 |
1919 |
1901 QCOMPARE(object->property("test").toInt(), 0); |
1920 QCOMPARE(object->property("test").toInt(), 0); |
1902 emit object->argumentSignal(19, "Hello world!", 10.3); |
1921 emit object->argumentSignal(19, "Hello world!", 10.25); |
1903 QCOMPARE(object->property("test").toInt(), 1); |
1922 QCOMPARE(object->property("test").toInt(), 1); |
1904 emit object->argumentSignal(19, "Hello world!", 10.3); |
1923 emit object->argumentSignal(19, "Hello world!", 10.25); |
1905 QCOMPARE(object->property("test").toInt(), 2); |
1924 QCOMPARE(object->property("test").toInt(), 2); |
1906 emit object->basicSignal(); |
1925 emit object->basicSignal(); |
1907 QCOMPARE(object->property("test").toInt(), 2); |
1926 QCOMPARE(object->property("test").toInt(), 2); |
1908 emit object->argumentSignal(19, "Hello world!", 10.3); |
1927 emit object->argumentSignal(19, "Hello world!", 10.25); |
1909 QCOMPARE(object->property("test").toInt(), 2); |
1928 QCOMPARE(object->property("test").toInt(), 2); |
1910 |
1929 |
1911 delete object; |
1930 delete object; |
1912 } |
1931 } |
1913 |
1932 |
1916 |
1935 |
1917 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1936 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1918 QVERIFY(object != 0); |
1937 QVERIFY(object != 0); |
1919 |
1938 |
1920 QCOMPARE(object->property("test").toInt(), 0); |
1939 QCOMPARE(object->property("test").toInt(), 0); |
1921 emit object->argumentSignal(19, "Hello world!", 10.3); |
1940 emit object->argumentSignal(19, "Hello world!", 10.25); |
1922 QCOMPARE(object->property("test").toInt(), 1); |
1941 QCOMPARE(object->property("test").toInt(), 1); |
1923 emit object->argumentSignal(19, "Hello world!", 10.3); |
1942 emit object->argumentSignal(19, "Hello world!", 10.25); |
1924 QCOMPARE(object->property("test").toInt(), 2); |
1943 QCOMPARE(object->property("test").toInt(), 2); |
1925 emit object->basicSignal(); |
1944 emit object->basicSignal(); |
1926 QCOMPARE(object->property("test").toInt(), 2); |
1945 QCOMPARE(object->property("test").toInt(), 2); |
1927 emit object->argumentSignal(19, "Hello world!", 10.3); |
1946 emit object->argumentSignal(19, "Hello world!", 10.25); |
1928 QCOMPARE(object->property("test").toInt(), 3); |
1947 QCOMPARE(object->property("test").toInt(), 3); |
1929 |
1948 |
1930 delete object; |
1949 delete object; |
1931 } |
1950 } |
1932 { |
1951 { |
1934 |
1953 |
1935 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1954 MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create()); |
1936 QVERIFY(object != 0); |
1955 QVERIFY(object != 0); |
1937 |
1956 |
1938 QCOMPARE(object->property("test").toInt(), 0); |
1957 QCOMPARE(object->property("test").toInt(), 0); |
1939 emit object->argumentSignal(19, "Hello world!", 10.3); |
1958 emit object->argumentSignal(19, "Hello world!", 10.25); |
1940 QCOMPARE(object->property("test").toInt(), 1); |
1959 QCOMPARE(object->property("test").toInt(), 1); |
1941 emit object->argumentSignal(19, "Hello world!", 10.3); |
1960 emit object->argumentSignal(19, "Hello world!", 10.25); |
1942 QCOMPARE(object->property("test").toInt(), 2); |
1961 QCOMPARE(object->property("test").toInt(), 2); |
1943 emit object->basicSignal(); |
1962 emit object->basicSignal(); |
1944 QCOMPARE(object->property("test").toInt(), 2); |
1963 QCOMPARE(object->property("test").toInt(), 2); |
1945 emit object->argumentSignal(19, "Hello world!", 10.3); |
1964 emit object->argumentSignal(19, "Hello world!", 10.25); |
1946 QCOMPARE(object->property("test").toInt(), 3); |
1965 QCOMPARE(object->property("test").toInt(), 3); |
1947 |
1966 |
1948 delete object; |
1967 delete object; |
1949 } |
1968 } |
1950 } |
1969 } |
2491 QObject *o = component.create(); |
2510 QObject *o = component.create(); |
2492 QVERIFY(o != 0); |
2511 QVERIFY(o != 0); |
2493 delete o; |
2512 delete o; |
2494 } |
2513 } |
2495 |
2514 |
|
2515 void tst_qdeclarativeecmascript::qtbug_11606() |
|
2516 { |
|
2517 QDeclarativeComponent component(&engine, TEST_FILE("qtbug_11606.qml")); |
|
2518 QObject *o = component.create(); |
|
2519 QVERIFY(o != 0); |
|
2520 QCOMPARE(o->property("test").toBool(), true); |
|
2521 delete o; |
|
2522 } |
|
2523 |
|
2524 void tst_qdeclarativeecmascript::qtbug_11600() |
|
2525 { |
|
2526 QDeclarativeComponent component(&engine, TEST_FILE("qtbug_11600.qml")); |
|
2527 QObject *o = component.create(); |
|
2528 QVERIFY(o != 0); |
|
2529 QCOMPARE(o->property("test").toBool(), true); |
|
2530 delete o; |
|
2531 } |
|
2532 |
|
2533 |
2496 QTEST_MAIN(tst_qdeclarativeecmascript) |
2534 QTEST_MAIN(tst_qdeclarativeecmascript) |
2497 |
2535 |
2498 #include "tst_qdeclarativeecmascript.moc" |
2536 #include "tst_qdeclarativeecmascript.moc" |