aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChris Adams <christopher.adams@nokia.com>2012-02-06 14:24:42 +1000
committerQt by Nokia <qt-info@nokia.com>2012-03-05 09:01:47 +0100
commitb6e78b38367a23f0b053bbd2abe4ef161e4053b9 (patch)
tree022bb82553af5b96a31f6e1ba56944d0e7060437 /tests
parent0284817d6cd7e17afa8da26ee6e9199100754446 (diff)
Improve support for var properties
This commit changes the semantics of function assignment in QML. Previously, function assignment was interpreted as binding assignment. Now, function assignment is interpreted as function assignment, and therefore fails for all property types other than "var" properties. To support imperative binding assignment, a new function was added to the Qt object: Qt.binding(function) which takes a single function parameter and returns a function object which will be interpreted as an assignable binding expression by the QML engine. Finally, this commit also slightly changes the semantics of var properties in that the "special" JavaScript values of null and undefined may be assigned to var properties, rather than being interpreted as reset requests. Task-number: QTBUG-21842 Change-Id: Iee99a878b9badf0fb76e983da7ebfa493f55ceb5 Reviewed-by: Matthew Vogt <matthew.vogt@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qqmlcomponent/data/createObjectWithScript.qml4
-rw-r--r--tests/auto/qml/qqmlecmascript/data/functionAssignment.1.qml2
-rw-r--r--tests/auto/qml/qqmlecmascript/data/functionAssignment.2.qml14
-rw-r--r--tests/auto/qml/qqmlecmascript/data/functionAssignment.3.qml18
-rw-r--r--tests/auto/qml/qqmlecmascript/data/functionAssignment.js4
-rw-r--r--tests/auto/qml/qqmlecmascript/data/propertyVar.11.qml21
-rw-r--r--tests/auto/qml/qqmlecmascript/data/propertyVar.12.qml19
-rw-r--r--tests/auto/qml/qqmlecmascript/data/propertyVar.13.qml19
-rw-r--r--tests/auto/qml/qqmlecmascript/data/propertyVar.14.qml21
-rw-r--r--tests/auto/qml/qqmlecmascript/data/propertyVar.15.qml20
-rw-r--r--tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp30
-rw-r--r--tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.2.qml12
-rw-r--r--tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.qml5
-rw-r--r--tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp20
-rw-r--r--tests/auto/quick/qquickloader/data/initialPropertyValues.binding.qml2
15 files changed, 197 insertions, 14 deletions
diff --git a/tests/auto/qml/qqmlcomponent/data/createObjectWithScript.qml b/tests/auto/qml/qqmlcomponent/data/createObjectWithScript.qml
index 122c6a87c8..989b295cb5 100644
--- a/tests/auto/qml/qqmlcomponent/data/createObjectWithScript.qml
+++ b/tests/auto/qml/qqmlcomponent/data/createObjectWithScript.qml
@@ -37,7 +37,7 @@ Item{
root.declarativerectangle = a.createObject(root, {"x":17,"y":17, "color":"white", "border.width":3, "innerRect.border.width": 20});
root.declarativeitem = b.createObject(root, {"x":17,"y":17,"testBool":true,"testInt":17,"testObject":root});
- root.bindingTestObject = c.createObject(root, {'testValue': (function(){return width * 3}) }) // use root.width
- root.bindingThisTestObject = c.createObject(root, {'testValue': (function(){return this.width * 3}) }) // use width of Item within 'c'
+ root.bindingTestObject = c.createObject(root, {'testValue': Qt.binding(function(){return width * 3}) }) // use root.width
+ root.bindingThisTestObject = c.createObject(root, {'testValue': Qt.binding(function(){return this.width * 3}) }) // use width of Item within 'c'
}
}
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/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
index 676557a81c..9c8d1e750b 100644
--- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
+++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
@@ -195,6 +195,7 @@ private slots:
void functionAssignment_fromJS();
void functionAssignment_fromJS_data();
void functionAssignmentfromJS_invalid();
+ void functionAssignment_afterBinding();
void eval();
void function();
void functionException();
@@ -3768,6 +3769,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()
@@ -5025,8 +5031,12 @@ void tst_qqmlecmascript::functionAssignment_fromBinding()
QQmlComponent component(&engine, testFileUrl("functionAssignment.1.qml"));
QString url = component.url().toString();
- QString warning = url + ":4: Unable to assign a function to a property.";
- QTest::ignoreMessage(QtWarningMsg, warning.toLatin1().constData());
+ QString w1 = url + ":4: Unable to assign a function to a property of any type other than var.";
+ QString w2 = url + ":5: Invalid use of Qt.binding() in a binding declaration.";
+ QString w3 = url + ":6: 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);
@@ -5094,6 +5104,22 @@ 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";
+ 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
+
+ delete o;
+}
+
void tst_qqmlecmascript::eval()
{
QQmlComponent component(&engine, testFileUrl("eval.qml"));
diff --git a/tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.2.qml b/tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.2.qml
new file mode 100644
index 0000000000..0da717ba5c
--- /dev/null
+++ b/tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.2.qml
@@ -0,0 +1,12 @@
+import QtQuick 2.0
+import Test 1.0
+
+MyTypeObject {
+ property int value: 10
+ rect.y: Qt.binding(function() { return value; }); // error.
+
+ Component.onCompleted: {
+ rect.x = 5;
+ rect.x = (function() { return value; }); // error.
+ }
+}
diff --git a/tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.qml b/tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.qml
index a65218669b..9b10803649 100644
--- a/tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.qml
+++ b/tests/auto/qml/qqmlvaluetypes/data/bindingAssignment.qml
@@ -1,7 +1,12 @@
+import QtQuick 2.0
import Test 1.0
MyTypeObject {
property int value: 10
rect.x: value
+
+ Component.onCompleted: {
+ rect.y = Qt.binding(function() { return value + 5; });
+ }
}
diff --git a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
index 0aa223e733..c37a42fee5 100644
--- a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
+++ b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp
@@ -871,17 +871,37 @@ void tst_qqmlvaluetypes::color()
// Test bindings can write to value types
void tst_qqmlvaluetypes::bindingAssignment()
{
+ // binding declaration
+ {
QQmlComponent component(&engine, testFileUrl("bindingAssignment.qml"));
MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
QVERIFY(object != 0);
QCOMPARE(object->rect().x(), 10);
+ QCOMPARE(object->rect().y(), 15);
object->setProperty("value", QVariant(92));
QCOMPARE(object->rect().x(), 92);
+ QCOMPARE(object->rect().y(), 97);
delete object;
+ }
+
+ // function assignment should fail without crashing
+ {
+ QString warning1 = testFileUrl("bindingAssignment.2.qml").toString() + QLatin1String(":6: Invalid use of Qt.binding() in a binding declaration.");
+ QString warning2 = testFileUrl("bindingAssignment.2.qml").toString() + QLatin1String(":10: Error: Cannot assign JavaScript function to value-type property");
+ QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1));
+ QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2));
+ QQmlComponent component(&engine, testFileUrl("bindingAssignment.2.qml"));
+ MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create());
+ QVERIFY(object != 0);
+ QCOMPARE(object->rect().x(), 5);
+ object->setProperty("value", QVariant(92));
+ QCOMPARE(object->rect().x(), 5);
+ delete object;
+ }
}
// Test bindings can read from value types
diff --git a/tests/auto/quick/qquickloader/data/initialPropertyValues.binding.qml b/tests/auto/quick/qquickloader/data/initialPropertyValues.binding.qml
index e0df50a74a..2ee60b0b50 100644
--- a/tests/auto/quick/qquickloader/data/initialPropertyValues.binding.qml
+++ b/tests/auto/quick/qquickloader/data/initialPropertyValues.binding.qml
@@ -16,6 +16,6 @@ Item {
}
Component.onCompleted: {
- loader.setSource("InitialPropertyValuesComponent.qml", {"canary": (function() { return root.bindable })});
+ loader.setSource("InitialPropertyValuesComponent.qml", {"canary": Qt.binding(function() { return root.bindable })});
}
}