/ tests / tst_delayed.qml
tst_delayed.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  
  7  Item {
  8      ListModel {
  9          id: testModel1
 10          ListElement{ role1: 1 }
 11      }
 12      SortFilterProxyModel {
 13          id: testFilterProxyModel
 14          sourceModel: testModel1
 15          property int foo: 1
 16          filters: [
 17              ExpressionFilter {
 18                  id: expressionFilter
 19                  property var w: ({count : 0}) // wrap count in a js object so modifying it doesn't bind it in the expression
 20                  expression: {
 21                      ++w.count;
 22                      testFilterProxyModel.foo;
 23                      return true;
 24                  }
 25              },
 26              ValueFilter {
 27                  roleName: "role1"
 28                  value: testFilterProxyModel.foo
 29              },
 30              ValueFilter {
 31                  roleName: "role1"
 32                  value: testFilterProxyModel.foo
 33              }
 34          ]
 35          sorters: RoleSorter {
 36              roleName: "role1"
 37              sortOrder: testFilterProxyModel.foo === 1 ? Qt.AscendingOrder : Qt.DescendingOrder
 38          }
 39      }
 40  
 41      ListModel {
 42          id: testModel2
 43          ListElement{ role1: 1 }
 44          ListElement{ role1: 2 }
 45      }
 46      SortFilterProxyModel {
 47          id: testSorterProxyModel
 48          sourceModel: testModel2
 49          property bool foo: true
 50          sorters: [
 51              ExpressionSorter {
 52                  id: expressionSorter
 53                  property var w: ({count : 0}) // wrap count in a js object so modifying it doesn't bind it in the expression
 54                  expression: {
 55                      ++w.count;
 56                      testSorterProxyModel.foo;
 57                      return false;
 58                  }
 59              },
 60              RoleSorter {
 61                  roleName: "role1"
 62                  sortOrder: testSorterProxyModel.foo ? Qt.AscendingOrder : Qt.DescendingOrder
 63              },
 64              RoleSorter {
 65                  roleName: "role1"
 66                  sortOrder: testSorterProxyModel.foo ? Qt.AscendingOrder : Qt.DescendingOrder
 67              }
 68          ]
 69      }
 70  
 71      SortFilterProxyModel {
 72          id: testRolesProxyModel
 73          sourceModel: testModel1
 74          property bool foo: true
 75          proxyRoles: [
 76              StaticRole {
 77                  name: "display"
 78                  value: 5
 79              },
 80              ExpressionRole {
 81                  id: expressionRole
 82                  name: "expressionRole"
 83                  property var w: ({count : 0}) // wrap count in a js object so modifying it doesn't bind it in the expression
 84                  expression: {
 85                      ++w.count;
 86                      return testRolesProxyModel.foo;
 87                  }
 88              },
 89              StaticRole {
 90                  name: "role1"
 91                  value: testRolesProxyModel.foo
 92              },
 93              StaticRole {
 94                  name: "role2"
 95                  value: testRolesProxyModel.foo
 96              }
 97          ]
 98      }
 99  
100      SignalSpy {
101          id: dataChangedSpy
102          target: testRolesProxyModel
103          signalName: "dataChanged"
104      }
105  
106      Instantiator {
107          id: instantiator
108          model: testRolesProxyModel
109          delegate: QtObject { property bool foo: model.expressionRole; property bool foo2: model.expressionRole }
110      }
111  
112      TestCase {
113          name: "DelayedTest"
114  
115          function test_directFilters() {
116              testFilterProxyModel.delayed = false;
117              expressionFilter.w.count = 0;
118              testFilterProxyModel.foo = 2;
119              compare(testFilterProxyModel.count, 0);
120              verify(expressionFilter.w.count > 1);
121              var lastEvaluationCount = expressionFilter.w.count;
122              wait(0);
123              compare(testFilterProxyModel.count, 0);
124              compare(expressionFilter.w.count, lastEvaluationCount);
125          }
126  
127          function test_delayedFilters() {
128              testFilterProxyModel.delayed = false;
129              testFilterProxyModel.foo = 2;
130              compare(testFilterProxyModel.count, 0);
131              testFilterProxyModel.delayed = true;
132              expressionFilter.w.count = 0;
133              testFilterProxyModel.foo = 0;
134              testFilterProxyModel.foo = 1;
135              compare(testFilterProxyModel.count, 0);
136              compare(expressionFilter.w.count, 0);
137              wait(0);
138              compare(testFilterProxyModel.count, 1);
139              compare(expressionFilter.w.count, 1);
140          }
141  
142          function test_directSorters() {
143              testSorterProxyModel.delayed = false;
144              testSorterProxyModel.foo = true;
145              compare(testSorterProxyModel.get(0).role1, 1);
146              expressionSorter.w.count = 0;
147              testSorterProxyModel.foo = false;
148              compare(testSorterProxyModel.get(0).role1, 2);
149              verify(expressionSorter.w.count > 1);
150              var lastEvaluationCount = expressionSorter.w.count
151              wait(0);
152              compare(testSorterProxyModel.get(0).role1, 2);
153              compare(expressionSorter.w.count, lastEvaluationCount);
154          }
155  
156          function test_delayedSorters() {
157              testSorterProxyModel.delayed = false;
158              testSorterProxyModel.foo = true;
159              compare(testSorterProxyModel.get(0).role1, 1);
160              testSorterProxyModel.delayed = true;
161              expressionSorter.w.count = 0;
162              testSorterProxyModel.foo = false;
163              testSorterProxyModel.foo = true;
164              testSorterProxyModel.foo = false;
165              compare(testSorterProxyModel.get(0).role1, 1);
166              compare(expressionSorter.w.count, 0);
167              wait(0);
168              compare(testSorterProxyModel.get(0).role1, 2);
169              compare(expressionSorter.w.count, 2);
170          }
171  
172          function test_proxyRoles() {
173              // init not delayed
174              testRolesProxyModel.delayed = false;
175              testRolesProxyModel.foo = true;
176              compare(instantiator.object.foo, true);
177              expressionRole.w.count = 0;
178              dataChangedSpy.clear();
179  
180              // test not delayed
181              testRolesProxyModel.foo = false;
182              compare(instantiator.object.foo, false);
183              compare(dataChangedSpy.count, 3);
184              var notDelayedCount = expressionRole.w.count; // why is it 12 and not just 3 ?
185              wait(0);
186              compare(instantiator.object.foo, false);
187              compare(dataChangedSpy.count, 3);
188              compare(expressionRole.w.count, notDelayedCount);
189  
190              // init delayed
191              testRolesProxyModel.delayed = true;
192              expressionRole.w.count = 0;
193              dataChangedSpy.clear();
194  
195              // test delayed
196              testRolesProxyModel.foo = true;
197              testRolesProxyModel.foo = false;
198              testRolesProxyModel.foo = true;
199              compare(instantiator.object.foo, false);
200              compare(dataChangedSpy.count, 0);
201              compare(expressionRole.w.count, 0);
202              wait(0);
203              compare(instantiator.object.foo, true);
204              compare(dataChangedSpy.count, 1);
205              var expectedDelayedCount = notDelayedCount / 3;
206              compare(expressionRole.w.count, expectedDelayedCount);
207          }
208      }
209  }