tst_proxyroles.qml
1 import QtQuick 2.0 2 import QtQml 2.2 3 import QtTest 1.1 4 import SortFilterProxyModel 0.2 5 import SortFilterProxyModel.Test 0.2 6 import QtQml 2.2 7 8 Item { 9 ListModel { 10 id: listModel 11 ListElement { test: "first"; keep: true } 12 ListElement { test: "second"; keep: true } 13 ListElement { test: "third"; keep: true } 14 } 15 16 SortFilterProxyModel { 17 id: testModel 18 sourceModel: listModel 19 filters: [ 20 ValueFilter { 21 roleName: "keep" 22 value: true 23 }, 24 ValueFilter { 25 inverted: true 26 roleName: "staticRole" 27 value: "filterMe" 28 } 29 ] 30 31 proxyRoles: [ 32 StaticRole { 33 id: staticRole 34 name: "staticRole" 35 value: "foo" 36 }, 37 StaticRole { 38 id: renameRole 39 name: "renameMe" 40 value: "test" 41 }, 42 SourceIndexRole { 43 name: "sourceIndexRole" 44 }, 45 MultiRole {} 46 ] 47 } 48 49 Instantiator { 50 id: instantiator 51 model: testModel 52 QtObject { 53 property string staticRole: model.staticRole 54 property int sourceIndexRole: model.sourceIndexRole 55 } 56 } 57 58 ListModel { 59 id: singleRowModel 60 ListElement { 61 changingRole: "Change me" 62 otherRole: "I don't change" 63 } 64 } 65 66 SortFilterProxyModel { 67 id: noProxyRolesProxyModel 68 sourceModel: singleRowModel 69 } 70 71 Instantiator { 72 id: outerInstantiator 73 model: noProxyRolesProxyModel 74 QtObject { 75 property var counter: ({ count : 0 }) 76 property string changingRole: model.changingRole 77 property string otherRole: { 78 ++counter.count; 79 return model.otherRole; 80 } 81 } 82 } 83 84 TestCase { 85 name: "ProxyRoles" 86 87 function test_resetAfterNameChange() { 88 var oldObject = instantiator.object; 89 renameRole.name = "foobarRole"; 90 var newObject = instantiator.object; 91 verify(newObject !== oldObject, "Instantiator object should have been reinstantiated"); 92 } 93 94 function test_proxyRoleInvalidation() { 95 compare(instantiator.object.staticRole, "foo"); 96 staticRole.value = "bar"; 97 compare(instantiator.object.staticRole, "bar"); 98 } 99 100 function test_proxyRoleGetDataFromSource() { 101 compare(instantiator.object.sourceIndexRole, 0); 102 compare(testModel.get(1, "sourceIndexRole"), 1); 103 listModel.setProperty(1, "keep", false); 104 compare(testModel.get(1, "sourceIndexRole"), 2); 105 } 106 107 function test_filterFromProxyRole() { 108 staticRole.value = "filterMe"; 109 compare(testModel.count, 0); 110 staticRole.value = "foo"; 111 compare(testModel.count, 3); 112 } 113 114 function test_multiRole() { 115 compare(testModel.get(0, "role1"), "data for role1"); 116 compare(testModel.get(0, "role2"), "data for role2"); 117 } 118 119 function test_ProxyRolesDataChanged() { 120 outerInstantiator.object.counter.count = 0; 121 singleRowModel.setProperty(0, "changingRole", "Changed") 122 compare(outerInstantiator.object.changingRole, "Changed"); 123 compare(outerInstantiator.object.counter.count, 0); 124 } 125 } 126 }