~mhr3/unity8/fix-1297246

« back to all changes in this revision

Viewing changes to tests/qmltests/Components/tst_FilterGrid.qml

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
import QtQuick 2.0
 
18
import QtTest 1.0
 
19
import "../../../Components"
 
20
import Ubuntu.Components.ListItems 0.1 as ListItem
 
21
import Ubuntu.Components 0.1
 
22
import Unity.Test 0.1 as UT
 
23
 
 
24
/*
 
25
  You should see 6 green squares (from "A" to "F") and a button "View all (12)".
 
26
  Once you press that button other 6 green squares (from "G" to "L") should show up.
 
27
*/
 
28
Rectangle {
 
29
    width: gridRect.width + controls.width
 
30
    height: units.gu(50)
 
31
    color: "white"
 
32
 
 
33
    Column {
 
34
        id: controls
 
35
        width: units.gu(30)
 
36
        height: parent.height
 
37
        anchors.top: parent.top
 
38
        anchors.right: parent.right
 
39
        ListItem.ValueSelector {
 
40
            id: collapsedRowCountSelector
 
41
            text: "collapsedRowCount"
 
42
            values: [1,2,3,4]
 
43
            selectedIndex: 1
 
44
        }
 
45
        Row {
 
46
            spacing: units.gu(1)
 
47
            Label { anchors.verticalCenter: parent.verticalCenter
 
48
                    text: "Expandable" }
 
49
            CheckBox {
 
50
                id: expandableCheckBox
 
51
                checked: true
 
52
            }
 
53
        }
 
54
        Row {
 
55
            spacing: units.gu(1)
 
56
            Label { anchors.verticalCenter: parent.verticalCenter
 
57
                    text: "Filter" }
 
58
            CheckBox {
 
59
                id: filterCheckBox
 
60
                checked: true
 
61
            }
 
62
        }
 
63
    }
 
64
 
 
65
    ListModel {
 
66
        id: fakeModel
 
67
        ListElement { name: "A" }
 
68
        ListElement { name: "B" }
 
69
        ListElement { name: "C" }
 
70
        ListElement { name: "D" }
 
71
        ListElement { name: "E" }
 
72
        ListElement { name: "F" }
 
73
        ListElement { name: "G" }
 
74
        ListElement { name: "H" }
 
75
        ListElement { name: "I" }
 
76
        ListElement { name: "J" }
 
77
        ListElement { name: "K" }
 
78
        ListElement { name: "L" }
 
79
    }
 
80
 
 
81
    Rectangle {
 
82
        id: gridRect
 
83
        width: units.gu(30)
 
84
        height: parent.height
 
85
        color: "grey"
 
86
        anchors.top: parent.top
 
87
        anchors.left: parent.left
 
88
 
 
89
        FilterGrid {
 
90
            id: filterGrid
 
91
            anchors.fill: parent
 
92
            model: fakeModel
 
93
            maximumNumberOfColumns: 3
 
94
            collapsedRowCount:
 
95
                collapsedRowCountSelector.values[collapsedRowCountSelector.selectedIndex]
 
96
            filter: filterCheckBox.checked
 
97
            expandable: expandableCheckBox.checked
 
98
            minimumHorizontalSpacing: units.gu(1)
 
99
            delegateWidth: units.gu(6)
 
100
            delegateHeight: units.gu(6)
 
101
            verticalSpacing: units.gu(1)
 
102
 
 
103
            delegate: Rectangle {
 
104
                // So that it can be identified by countVisibleDelegates()
 
105
                property bool isGridDelegate: true
 
106
 
 
107
                color: "green"
 
108
                width: units.gu(6)
 
109
                height: units.gu(6)
 
110
                Text {
 
111
                    anchors.centerIn: parent
 
112
                    text: name
 
113
                }
 
114
            }
 
115
        }
 
116
    }
 
117
 
 
118
    UT.UnityTestCase {
 
119
        name: "FilterGrid"
 
120
        when: windowShown
 
121
 
 
122
        function test_turningFilterOffShowsAllElements() {
 
123
            tryCompareFunction(countVisibleDelegates, 6)
 
124
 
 
125
            filterCheckBox.checked = false
 
126
 
 
127
            tryCompareFunction(countVisibleDelegates, 12)
 
128
 
 
129
            // back to initial state
 
130
            filterCheckBox.checked = true
 
131
        }
 
132
 
 
133
        function test_collapsedRowCount() {
 
134
            for (var i = 0; i < 4; ++i) {
 
135
                collapsedRowCountSelector.selectedIndex = i
 
136
                // We have 3 elements per row.
 
137
                // row count == index + 1
 
138
                tryCompareFunction(countVisibleDelegates, 3*(i+1))
 
139
            }
 
140
 
 
141
            // back to initial state
 
142
            collapsedRowCountSelector.selectedIndex = 1
 
143
        }
 
144
 
 
145
        // Checks that the filter toggle button, the one that says "View All (xy)",
 
146
        // shows up only when it's possible for the grid to be expanded.
 
147
        function test_filterToggleButton() {
 
148
            var filterToggleButton = findChild(filterGrid, "filterToggleButton");
 
149
 
 
150
            for (var i = 0; i < 4; ++i) {
 
151
                collapsedRowCountSelector.selectedIndex = i
 
152
                // row count == index + 1
 
153
                // The total number of rows is 4.
 
154
 
 
155
                expandableCheckBox.checked = false
 
156
                compare(filterToggleButton.visible, false)
 
157
                expandableCheckBox.checked = true
 
158
 
 
159
                if ((i+1) < 4)
 
160
                    compare(filterToggleButton.visible, true)
 
161
                else
 
162
                    compare(filterToggleButton.visible, false)
 
163
            }
 
164
 
 
165
            // back to initial state
 
166
            collapsedRowCountSelector.selectedIndex = 1
 
167
            expandableCheckBox.checked = true
 
168
        }
 
169
 
 
170
        function countVisibleDelegates() {
 
171
            return __countVisibleDelegates(filterGrid.visibleChildren, 0)
 
172
        }
 
173
 
 
174
        function __countVisibleDelegates(objList, total) {
 
175
            for (var i = 0; i < objList.length; ++i) {
 
176
                var child = objList[i];
 
177
                if (child.isGridDelegate !== undefined) {
 
178
                    ++total;
 
179
                } else {
 
180
                    total = __countVisibleDelegates(child.visibleChildren, total)
 
181
                }
 
182
            }
 
183
            return total
 
184
        }
 
185
    }
 
186
}