diff options
Diffstat (limited to 'tests/auto/qml/qqmlecmascript')
11 files changed, 162 insertions, 13 deletions
diff --git a/tests/auto/qml/qqmlecmascript/data/functionAssignment.1.qml b/tests/auto/qml/qqmlecmascript/data/functionAssignment.1.qml index 09540f1f6e..0b1b45b41b 100644 --- a/tests/auto/qml/qqmlecmascript/data/functionAssignment.1.qml +++ b/tests/auto/qml/qqmlecmascript/data/functionAssignment.1.qml @@ -2,4 +2,6 @@ import Qt.test 1.0 MyQmlObject { property variant a: function myFunction() { return 2; } + property variant b: Qt.binding(function() { return 2; }) + property var c: Qt.binding(function() { return 2; }) } diff --git a/tests/auto/qml/qqmlecmascript/data/functionAssignment.2.qml b/tests/auto/qml/qqmlecmascript/data/functionAssignment.2.qml index 0f78eaf1dc..3d8fd85a88 100644 --- a/tests/auto/qml/qqmlecmascript/data/functionAssignment.2.qml +++ b/tests/auto/qml/qqmlecmascript/data/functionAssignment.2.qml @@ -24,7 +24,7 @@ MyQmlObject { function myFunction() { return aNumber * 10; } - a = myFunction; + a = Qt.binding(myFunction); } property QtObject obj: QtObject { @@ -34,7 +34,7 @@ MyQmlObject { } } onAssignWithThisChanged: { - a = obj.myFunction; + a = Qt.binding(obj.myFunction); } onAssignToPropertyFromJsFileChanged: { @@ -47,8 +47,8 @@ MyQmlObject { property Text text: Text { } onAssignToValueTypeChanged: { - text.font.pixelSize = (function() { return aNumber * 10; }) - a = (function() { return text.font.pixelSize; }) + text.font.pixelSize = Qt.binding(function() { return aNumber * 10; }) + a = Qt.binding(function() { return text.font.pixelSize; }) } @@ -57,17 +57,17 @@ MyQmlObject { onAssignFuncWithoutReturnChanged: { function myFunction() { } - a = myFunction; + a = Qt.binding(myFunction); } onAssignWrongTypeChanged: { function myFunction() { return 'a string'; } - aNumber = myFunction; + aNumber = Qt.binding(myFunction); } onAssignWrongTypeToValueTypeChanged: { - text.font.pixelSize = (function() { return 'a string'; }) + text.font.pixelSize = Qt.binding(function() { return 'a string'; }) } } diff --git a/tests/auto/qml/qqmlecmascript/data/functionAssignment.3.qml b/tests/auto/qml/qqmlecmascript/data/functionAssignment.3.qml new file mode 100644 index 0000000000..c34a868949 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/functionAssignment.3.qml @@ -0,0 +1,18 @@ +import QtQuick 2.0 + +Item { + property int t1: 1 + property int t2: 2 + + function randomNumber() { + return 4; + } + + Component.onCompleted: { + // shouldn't "convert" the randomNumber function into a binding permanently + t1 = Qt.binding(randomNumber) + + // therefore, the following assignment should fail. + t2 = randomNumber + } +} diff --git a/tests/auto/qml/qqmlecmascript/data/functionAssignment.js b/tests/auto/qml/qqmlecmascript/data/functionAssignment.js index 14daa7629f..3ba4e193e6 100644 --- a/tests/auto/qml/qqmlecmascript/data/functionAssignment.js +++ b/tests/auto/qml/qqmlecmascript/data/functionAssignment.js @@ -1,6 +1,6 @@ function bindProperty() { - a = (function(){ return aNumber * 10 }) + a = Qt.binding(function(){ return aNumber * 10 }) } @@ -13,5 +13,5 @@ var testObj = new TestObject() function bindPropertyWithThis() { - a = testObj.bindFunction + a = Qt.binding(testObj.bindFunction) } diff --git a/tests/auto/qml/qqmlecmascript/data/propertyVar.11.qml b/tests/auto/qml/qqmlecmascript/data/propertyVar.11.qml new file mode 100644 index 0000000000..63be26717e --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/propertyVar.11.qml @@ -0,0 +1,21 @@ +import QtQuick 2.0 + +Item { + property bool test: false + property var fnResult: testFunction(5) + property var f1: testFunction + property var f2 + + function testFunction(x) { + return x; + } + + Component.onCompleted: { + f2 = testFunction; + if (fnResult != 5) return; + if (f1(6) != 6) return; + if (f2(7) != 7) return; + if (f1 != f2) return; + test = true; + } +} diff --git a/tests/auto/qml/qqmlecmascript/data/propertyVar.12.qml b/tests/auto/qml/qqmlecmascript/data/propertyVar.12.qml new file mode 100644 index 0000000000..3510bd2350 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/propertyVar.12.qml @@ -0,0 +1,19 @@ +import QtQuick 2.0 + +Item { + property bool test: false + property var nullOne: null + property var nullTwo + property var undefOne: undefined + property var undefTwo + + Component.onCompleted: { + nullTwo = null; + undefTwo = undefined; + if (nullOne != null) return; + if (nullOne != nullTwo) return; + if (undefOne != undefined) return; + if (undefOne != undefTwo) return; + test = true; + } +} diff --git a/tests/auto/qml/qqmlecmascript/data/propertyVar.13.qml b/tests/auto/qml/qqmlecmascript/data/propertyVar.13.qml new file mode 100644 index 0000000000..14c7c677ae --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/propertyVar.13.qml @@ -0,0 +1,19 @@ +import QtQuick 2.0 + +Item { + property bool test: false + property var f: b + 12 + property int a: 100 + property int b: testFunction() + + function testFunction() { + return a * 3; + } + + Component.onCompleted: { + if (f != 312) return; + a = 120; + if (f != 372) return; + test = true; + } +} diff --git a/tests/auto/qml/qqmlecmascript/data/propertyVar.14.qml b/tests/auto/qml/qqmlecmascript/data/propertyVar.14.qml new file mode 100644 index 0000000000..a1e26661bb --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/propertyVar.14.qml @@ -0,0 +1,21 @@ +import QtQuick 2.0 + +Item { + property bool test: false + property var f + property int a: 100 + property int b + + function testFunction() { + return a * 3; + } + + Component.onCompleted: { + b = Qt.binding(testFunction); + f = Qt.binding(function() { return b + 12; }); + if (f != 312) return; + a = 120; + if (f != 372) return; + test = true; + } +} diff --git a/tests/auto/qml/qqmlecmascript/data/propertyVar.15.qml b/tests/auto/qml/qqmlecmascript/data/propertyVar.15.qml new file mode 100644 index 0000000000..5e5071fc2d --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/propertyVar.15.qml @@ -0,0 +1,20 @@ +import QtQuick 2.0 + +Item { + property bool test: false + property var storedBinding: [ Qt.binding(function() { return testFunction() + 12; }) ] + property int a: 100 + property int b + + function testFunction() { + return a * 3; + } + + Component.onCompleted: { + b = storedBinding[0]; + if (b != 312) return; + a = 120; + if (b != 372) return; + test = true; + } +} diff --git a/tests/auto/qml/qqmlecmascript/testtypes.h b/tests/auto/qml/qqmlecmascript/testtypes.h index 519f9e479a..167e944aa6 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.h +++ b/tests/auto/qml/qqmlecmascript/testtypes.h @@ -51,6 +51,7 @@ #include <QtGui/qmatrix.h> #include <QtGui/qcolor.h> #include <QtGui/qvector3d.h> +#include <QtGui/QFont> #include <QtGui/QPixmap> #include <QtCore/qdatetime.h> #include <QtQml/qjsvalue.h> diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 273cd07986..c2d490e3c5 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -202,6 +202,7 @@ private slots: void functionAssignment_fromJS(); void functionAssignment_fromJS_data(); void functionAssignmentfromJS_invalid(); + void functionAssignment_afterBinding(); void eval(); void function(); void functionException(); @@ -4053,6 +4054,11 @@ void tst_qqmlecmascript::propertyVar_data() QTest::newRow("literal property assignment") << testFileUrl("propertyVar.8.qml"); QTest::newRow("qobject property assignment") << testFileUrl("propertyVar.9.qml"); QTest::newRow("base class var property assignment") << testFileUrl("propertyVar.10.qml"); + QTest::newRow("javascript function assignment") << testFileUrl("propertyVar.11.qml"); + QTest::newRow("javascript special assignment") << testFileUrl("propertyVar.12.qml"); + QTest::newRow("declarative binding assignment") << testFileUrl("propertyVar.13.qml"); + QTest::newRow("imperative binding assignment") << testFileUrl("propertyVar.14.qml"); + QTest::newRow("stored binding assignment") << testFileUrl("propertyVar.15.qml"); } void tst_qqmlecmascript::propertyVar() @@ -4900,7 +4906,7 @@ void tst_qqmlecmascript::sequenceConversionWrite() QVERIFY(seq != 0); // we haven't registered QList<QPoint> as a sequence type, so writing shouldn't work. - QString warningOne = qmlFile.toString() + QLatin1String(":16: Error: Cannot assign QVariantList to void"); + QString warningOne = qmlFile.toString() + QLatin1String(":16: Error: Cannot assign QVariantList to an unregistered type"); QTest::ignoreMessage(QtWarningMsg, warningOne.toAscii().constData()); QMetaObject::invokeMethod(object, "performTest"); @@ -5315,9 +5321,13 @@ void tst_qqmlecmascript::functionAssignment_fromBinding() QQmlComponent component(&engine, testFileUrl("functionAssignment.1.qml")); QString url = component.url().toString(); - QString warning = url + ":4:25: Unable to assign a function to a property."; - QTest::ignoreMessage(QtWarningMsg, warning.toLatin1().constData()); - + QString w1 = url + ":4:25: Unable to assign a function to a property of any type other than var."; + QString w2 = url + ":5:25: Invalid use of Qt.binding() in a binding declaration."; + QString w3 = url + ":6:21: Invalid use of Qt.binding() in a binding declaration."; + QTest::ignoreMessage(QtWarningMsg, w1.toLatin1().constData()); + QTest::ignoreMessage(QtWarningMsg, w2.toLatin1().constData()); + QTest::ignoreMessage(QtWarningMsg, w3.toLatin1().constData()); + MyQmlObject *o = qobject_cast<MyQmlObject *>(component.create()); QVERIFY(o != 0); @@ -5384,6 +5394,24 @@ void tst_qqmlecmascript::functionAssignmentfromJS_invalid() delete o; } +void tst_qqmlecmascript::functionAssignment_afterBinding() +{ + QQmlComponent component(&engine, testFileUrl("functionAssignment.3.qml")); + + QString url = component.url().toString(); + //QString w1 = url + ":16: Error: Cannot assign JavaScript function to int"; // for now, function assignment = binding assignment + QString w1 = QLatin1String("WARNING: function assignment is DEPRECATED and will be removed! Wrap RHS in Qt.binding(): ") + url + QLatin1String(":16"); + QTest::ignoreMessage(QtWarningMsg, w1.toLatin1().constData()); + + QObject *o = component.create(); + QVERIFY(o != 0); + QCOMPARE(o->property("t1"), QVariant::fromValue<int>(4)); // should have bound + //QCOMPARE(o->property("t2"), QVariant::fromValue<int>(2)); // should not have changed + QCOMPARE(o->property("t2"), QVariant::fromValue<int>(4)); // for now, function assignment = binding assignment + + delete o; +} + void tst_qqmlecmascript::eval() { QQmlComponent component(&engine, testFileUrl("eval.qml")); |