/ tests / tst_filtercontainers.qml
tst_filtercontainers.qml
  1  import QtQuick 2.0
  2  import SortFilterProxyModel 0.2
  3  import QtQml.Models 2.2
  4  import QtTest 1.1
  5  
  6  Item {
  7      property list<Filter> filters: [
  8          AllOf {
  9              property string tag: "allOf"
 10              property var expectedValues: [{a: 0, b: false}]
 11              ValueFilter {
 12                  roleName: "a"
 13                  value: "0"
 14              }
 15              ValueFilter {
 16                  roleName: "b"
 17                  value: false
 18              }
 19          },
 20          AllOf {
 21              property string tag: "allOfOneDisabled"
 22              property var expectedValues: [{a: 0, b: true}, {a: 0, b: false}]
 23              ValueFilter {
 24                  roleName: "a"
 25                  value: "0"
 26              }
 27              ValueFilter {
 28                  enabled: false
 29                  roleName: "b"
 30                  value: false
 31              }
 32          },
 33          AnyOf {
 34              property string tag: "anyOf"
 35              property var expectedValues: [{a: 0, b: true}, {a: 0, b: false}, {a: 1, b: false}]
 36              ValueFilter {
 37                  roleName: "a"
 38                  value: "0"
 39              }
 40              ValueFilter {
 41                  roleName: "b"
 42                  value: false
 43              }
 44          }
 45      ]
 46  
 47      AllOf {
 48          id: outerFilter
 49          ValueFilter {
 50              roleName: "a"
 51              value: "0"
 52          }
 53          ValueFilter {
 54              id: innerFilter
 55              roleName: "b"
 56              value: false
 57          }
 58      }
 59  
 60      ListModel {
 61          id: dataModel
 62          ListElement { a: 0; b: true }
 63          ListElement { a: 0; b: false }
 64          ListElement { a: 1; b: true }
 65          ListElement { a: 1; b: false }
 66      }
 67  
 68      SortFilterProxyModel {
 69          id: testModel
 70          sourceModel: dataModel
 71      }
 72  
 73      TestCase {
 74          name:"RangeFilterTests"
 75  
 76          function modelValues() {
 77              var modelValues = [];
 78  
 79              for (var i = 0; i < testModel.count; i++)
 80                  modelValues.push(testModel.get(i));
 81  
 82              return modelValues;
 83          }
 84  
 85          function test_filterContainers_data() {
 86              return filters;
 87          }
 88  
 89          function test_filterContainers(filter) {
 90              testModel.filters = filter;
 91              compare(JSON.stringify(modelValues()), JSON.stringify(filter.expectedValues));
 92          }
 93  
 94          function test_changeInnerFilter() {
 95              testModel.filters = outerFilter;
 96              compare(JSON.stringify(modelValues()), JSON.stringify([{a: 0, b: false}]));
 97              innerFilter.value = true;
 98              compare(JSON.stringify(modelValues()), JSON.stringify([{a: 0, b: true}]));
 99              innerFilter.enabled = false;
100              compare(JSON.stringify(modelValues()), JSON.stringify([{a: 0, b: true}, {a: 0, b: false}]));
101          }
102      }
103  }