~macslow/unity8/fix-1475678

« back to all changes in this revision

Viewing changes to tests/qmltests/Components/tst_ResponsiveGridView.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 ".."
 
20
import "../../../Components"
 
21
import Ubuntu.Components.ListItems 0.1 as ListItem
 
22
import Ubuntu.Components 0.1
 
23
import Unity.Test 0.1 as UT
 
24
 
 
25
Item {
 
26
    width: gridRect.width + controls.width
 
27
    height: units.gu(80)
 
28
 
 
29
    Column {
 
30
        id: controls
 
31
        width: units.gu(40)
 
32
        height: parent.height
 
33
        anchors.top: parent.top
 
34
        anchors.right: parent.right
 
35
        ListItem.ValueSelector {
 
36
            id: maxColumnsSelector
 
37
            text: "maximumNumberOfColumns"
 
38
            values: [2,4,8,13,1000]
 
39
            selectedIndex: 1
 
40
        }
 
41
        ListItem.ValueSelector {
 
42
            id: minHSpacingSelector
 
43
            text: "minHorizontalSpacing"
 
44
            values: [0,units.gu(2),units.gu(8),units.gu(25)]
 
45
            selectedIndex: 0
 
46
        }
 
47
    }
 
48
 
 
49
    ListModel {
 
50
        id: fakeModel
 
51
        ListElement { name: "A" }
 
52
        ListElement { name: "B" }
 
53
        ListElement { name: "C" }
 
54
        ListElement { name: "D" }
 
55
        ListElement { name: "E" }
 
56
        ListElement { name: "F" }
 
57
        ListElement { name: "G" }
 
58
        ListElement { name: "H" }
 
59
        ListElement { name: "I" }
 
60
        ListElement { name: "J" }
 
61
        ListElement { name: "K" }
 
62
        ListElement { name: "L" }
 
63
        ListElement { name: "M" }
 
64
        ListElement { name: "N" }
 
65
        ListElement { name: "O" }
 
66
        ListElement { name: "P" }
 
67
        ListElement { name: "Q" }
 
68
        ListElement { name: "R" }
 
69
        ListElement { name: "S" }
 
70
        ListElement { name: "T" }
 
71
        ListElement { name: "U" }
 
72
    }
 
73
 
 
74
    Rectangle {
 
75
        id: gridRect
 
76
        width: units.gu(80)
 
77
        height: parent.height
 
78
        color: "grey"
 
79
        anchors.top: parent.top
 
80
        anchors.left: parent.left
 
81
 
 
82
        ResponsiveGridView {
 
83
            id: grid
 
84
            anchors.fill: parent
 
85
            model: fakeModel
 
86
            minimumHorizontalSpacing:
 
87
                minHSpacingSelector.values[minHSpacingSelector.selectedIndex]
 
88
            verticalSpacing: units.gu(2)
 
89
            maximumNumberOfColumns:
 
90
                maxColumnsSelector.values[maxColumnsSelector.selectedIndex]
 
91
            delegateWidth: units.gu(6)
 
92
            delegateHeight: units.gu(6)
 
93
 
 
94
            delegate: Rectangle {
 
95
                // So that it can be identified by test code
 
96
                property bool isGridDelegate: true
 
97
                color: "grey"
 
98
                border.color: "red"
 
99
                border.width: 1
 
100
 
 
101
                // IMPORTANT: always use grid's cellWidth and cellHeight here to get
 
102
                // ResponsiveGridView's intended result
 
103
                width: grid.cellWidth
 
104
                height: grid.cellHeight
 
105
 
 
106
                Rectangle {
 
107
                    color: "green"
 
108
                    anchors.centerIn: parent
 
109
                    width: units.gu(6)
 
110
                    height: units.gu(6)
 
111
                    Text {
 
112
                        anchors.centerIn: parent
 
113
                        text: name
 
114
                    }
 
115
                }
 
116
 
 
117
                Text { x:0; y:0; text:"(" + parent.x + ", " + parent.y + ")"}
 
118
            }
 
119
        }
 
120
    }
 
121
 
 
122
    UT.UnityTestCase {
 
123
        name: "ResponsiveGridView"
 
124
        when: windowShown
 
125
 
 
126
        function test_maximumNumberOfColumns_data() {
 
127
            var data = new Array()
 
128
 
 
129
            data.push({selectedIndex: 0, maxColumnCount:2, columnCount: 2})
 
130
            data.push({selectedIndex: 1, maxColumnCount:4, columnCount: 4})
 
131
            data.push({selectedIndex: 2, maxColumnCount:8, columnCount: 8})
 
132
            data.push({selectedIndex: 4, maxColumnCount:1000, columnCount: 13})
 
133
 
 
134
            return data
 
135
        }
 
136
 
 
137
        /* Change ResponsiveGridView's maximumNumberOfColumns property and check
 
138
           that the resulting number of columns matches expectations */
 
139
        function test_maximumNumberOfColumns(data) {
 
140
            minHSpacingSelector.selectedIndex = 0
 
141
 
 
142
            // sanity checks
 
143
            compare(maxColumnsSelector.values[data.selectedIndex], data.maxColumnCount)
 
144
            compare(minHSpacingSelector.values[0], 0)
 
145
 
 
146
            maxColumnsSelector.selectedIndex = data.selectedIndex
 
147
            tryCompareFunction(countGridDelegatesOnFirstRow, data.columnCount);
 
148
            compare(grid.columns, data.columnCount)
 
149
        }
 
150
 
 
151
        function test_minimumHorizontalSpacing_data() {
 
152
            var data = new Array()
 
153
 
 
154
            data.push({selectedIndex: 0, minHSpacing:0, columnCount: 13})
 
155
            data.push({selectedIndex: 1, minHSpacing:units.gu(2), columnCount: 9})
 
156
            data.push({selectedIndex: 2, minHSpacing:units.gu(8), columnCount: 5})
 
157
            data.push({selectedIndex: 3, minHSpacing:units.gu(25), columnCount: 2})
 
158
 
 
159
            return data
 
160
        }
 
161
 
 
162
        /* Change ResponsiveGridView's minimumHorizontalSpacing property and check
 
163
           that the resulting number of columns matches expectations */
 
164
        function test_minimumHorizontalSpacing(data) {
 
165
            maxColumnsSelector.selectedIndex = 4
 
166
 
 
167
            // sanity checks
 
168
            compare(maxColumnsSelector.values[4], 1000)
 
169
            compare(minHSpacingSelector.values[data.selectedIndex], data.minHSpacing)
 
170
 
 
171
            minHSpacingSelector.selectedIndex = data.selectedIndex
 
172
            tryCompareFunction(countGridDelegatesOnFirstRow, data.columnCount);
 
173
            compare(grid.columns, data.columnCount)
 
174
        }
 
175
 
 
176
 
 
177
        function countGridDelegatesOnFirstRow() {
 
178
            return __countGridDelegatesOnFirstRow(grid.visibleChildren, 0)
 
179
        }
 
180
 
 
181
        function __countGridDelegatesOnFirstRow(objList, total) {
 
182
            for (var i = 0; i < objList.length; ++i) {
 
183
                var child = objList[i];
 
184
                if (child.isGridDelegate !== undefined && child.y === 0) {
 
185
                    ++total;
 
186
                } else {
 
187
                    total = __countGridDelegatesOnFirstRow(child.visibleChildren, total)
 
188
                }
 
189
            }
 
190
            return total
 
191
        }
 
192
    }
 
193
}