aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qmltest/listmodel/tst_listmodel.qml
diff options
context:
space:
mode:
authorMichael Brasser <mbrasser@ford.com>2017-09-21 16:29:39 -0500
committerMichael Brasser <michael.brasser@live.com>2017-10-24 14:35:53 +0000
commitecf6a2a3658fa17cd4c34f1dd5acc1904f8b4314 (patch)
treeca0d487899e9a38132260b0a27ee44f5421025c2 /tests/auto/qmltest/listmodel/tst_listmodel.qml
parentf23ee3368ea62930587358d2dff9ec2cb70668f1 (diff)
Allow assigning functions in ListElement
As an alternative to using eval with a string. This allows code such as: // model ListElement { action: function() { doSomething() } } // delegate MouseArea { onClicked: model.action() } [ChangeLog][ListModel] Support assignment of function declarations in ListElement, to allow for models with actions. Change-Id: I50d188ab30f43b2b8a47f48ceb4281d3ca55bd44 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests/auto/qmltest/listmodel/tst_listmodel.qml')
-rw-r--r--tests/auto/qmltest/listmodel/tst_listmodel.qml56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/auto/qmltest/listmodel/tst_listmodel.qml b/tests/auto/qmltest/listmodel/tst_listmodel.qml
index 2d9d533a9a..8628918b55 100644
--- a/tests/auto/qmltest/listmodel/tst_listmodel.qml
+++ b/tests/auto/qmltest/listmodel/tst_listmodel.qml
@@ -51,6 +51,16 @@ Item {
ListModel { id: secondmodel; ListElement { name: "SecondModelElement0" } ListElement { name: "SecondModelElement1" } }
ListModel { id: altermodel; ListElement { name: "AlterModelElement0" } ListElement { name: "AlterModelElement1" } }
+ property string funcResult
+ ListModel {
+ id: funcModel
+ property string modelProp
+ ListElement { friendlyText: "one"; action: function(obj) { funcResult = obj.friendlyText } }
+ ListElement { friendlyText: "two"; action: function() {} }
+ ListElement { friendlyText: "three"; action: function() { modelProp = "fail" } }
+ ListElement { friendlyText: "four"; action: function() { funcResult = friendlyText } }
+ }
+
TestCase {
name: "ListModel"
@@ -128,5 +138,51 @@ Item {
tryCompare(altermodel, 'count', 0)
compare(altermodel.get(0), undefined)
}
+
+ function test_functions() {
+ // test different ways of calling
+ funcModel.get(0).action(funcModel.get(0))
+ compare(funcResult, "one")
+ funcModel.get(0).action.call(this, { friendlyText: "seven" })
+ compare(funcResult, "seven")
+
+ // test different ways of setting
+ funcResult = ""
+ funcModel.get(1).action()
+ compare(funcResult, "")
+
+ funcModel.set(1, { friendlyText: "two", action: function() { funcResult = "set" } })
+ funcModel.get(1).action()
+ compare(funcResult, "set")
+
+ funcModel.setProperty(1, "action", function() { top.funcResult = "setProperty" })
+ funcModel.get(1).action()
+ compare(funcResult, "setProperty")
+
+ funcModel.get(1).action = function() { funcResult = "jsSet" }
+ funcModel.get(1).action()
+ compare(funcResult, "jsSet")
+
+ // test unsupported features
+ var didThrow = false
+ try {
+ funcModel.get(2).action()
+ } catch(ex) {
+ verify(ex.toString().includes("Error: Invalid write to global property"))
+ didThrow = true
+ }
+ verify(didThrow)
+
+ didThrow = false
+ try {
+ funcModel.get(3).action()
+ } catch(ex) {
+ verify(ex.toString().includes("ReferenceError: friendlyText is not defined"))
+ didThrow = true
+ }
+ verify(didThrow)
+
+
+ }
}
}