~mterry/unity8/power-button-on-lock

« back to all changes in this revision

Viewing changes to tests/qmltests/Panel/tst_IndicatorsBar.qml

  • Committer: Michael Terry
  • Date: 2014-11-24 15:25:42 UTC
  • mfrom: (1368.1.82 unity8)
  • Revision ID: michael.terry@canonical.com-20141124152542-5cysva8ds3qfula6
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013-2014 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.1
 
18
import QtQuick.Layouts 1.1
 
19
import QtTest 1.0
 
20
import "../../../qml/Panel"
 
21
import Ubuntu.Components 0.1
 
22
import Unity.Test 0.1 as UT
 
23
import Unity.Indicators 0.1 as Indicators
 
24
 
 
25
IndicatorTest {
 
26
    id: root
 
27
    width: units.gu(100)
 
28
    height: units.gu(40)
 
29
 
 
30
 
 
31
    RowLayout {
 
32
        anchors.fill: parent
 
33
        anchors.margins: units.gu(1)
 
34
 
 
35
        Rectangle {
 
36
            id: itemArea
 
37
            color: "blue"
 
38
            Layout.fillWidth: true
 
39
            Layout.fillHeight: true
 
40
 
 
41
            Rectangle {
 
42
                color: "black"
 
43
                anchors.fill: indicatorsBar
 
44
            }
 
45
 
 
46
            IndicatorsBar {
 
47
                id: indicatorsBar
 
48
                height: expanded ? units.gu(7) : units.gu(3)
 
49
                width: units.gu(30)
 
50
                anchors.centerIn: parent
 
51
                indicatorsModel: root.indicatorsModel
 
52
                interactive: expanded && height === units.gu(7)
 
53
 
 
54
                Behavior on height {
 
55
                    NumberAnimation {
 
56
                        id: heightAnimation
 
57
                        duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing
 
58
                    }
 
59
                }
 
60
 
 
61
                MouseArea {
 
62
                    anchors.fill: parent
 
63
                    enabled: !indicatorsBar.expanded
 
64
                    onPressed: {
 
65
                        indicatorsBar.selectItemAt(mouse.x);
 
66
                        indicatorsBar.expanded = true
 
67
                    }
 
68
                }
 
69
            }
 
70
        }
 
71
 
 
72
        ColumnLayout {
 
73
            Layout.alignment: Qt.AlignTop
 
74
            Layout.fillWidth: false
 
75
 
 
76
            Button {
 
77
                Layout.fillWidth: true
 
78
                text: indicatorsBar.expanded ? "Collapse" : "Expand"
 
79
                onClicked: indicatorsBar.expanded = !indicatorsBar.expanded
 
80
            }
 
81
 
 
82
            Rectangle {
 
83
                Layout.preferredHeight: units.dp(1);
 
84
                Layout.fillWidth: true;
 
85
                color: "black"
 
86
            }
 
87
 
 
88
            Repeater {
 
89
                model: root.originalModelData
 
90
                RowLayout {
 
91
                    CheckBox {
 
92
                        checked: true
 
93
                        onCheckedChanged: checked ? insertIndicator(index) : removeIndicator(index);
 
94
                    }
 
95
                    Label {
 
96
                        Layout.fillWidth: true
 
97
                        text: modelData["identifier"]
 
98
                    }
 
99
 
 
100
                    CheckBox {
 
101
                        checked: true
 
102
                        onCheckedChanged: setIndicatorVisible(index, checked);
 
103
                    }
 
104
                    Label {
 
105
                        text: "visible"
 
106
                    }
 
107
                }
 
108
            }
 
109
        }
 
110
    }
 
111
 
 
112
    UT.UnityTestCase {
 
113
        name: "IndicatorsBar"
 
114
        when: windowShown
 
115
 
 
116
        function init() {
 
117
            indicatorsBar.expanded = false;
 
118
            wait_for_expansion_to_settle();
 
119
        }
 
120
 
 
121
        function test_expandSelectedItem_data() {
 
122
            return [
 
123
                { index: 0 },
 
124
                { index: 2 },
 
125
                { index: 4 }
 
126
            ];
 
127
        }
 
128
 
 
129
        function wait_for_expansion_to_settle() {
 
130
            tryCompare(heightAnimation, "running", false);
 
131
            wait(UbuntuAnimation.SnapDuration); // put a little extra wait in for things to settle
 
132
        }
 
133
 
 
134
        // Rough check that expanding a selected item keeps it within the area of the original item.
 
135
        function test_expandSelectedItem(data) {
 
136
            var dataItem = findChild(indicatorsBar, root.originalModelData[data.index]["identifier"] + "-panelItem");
 
137
            verify(dataItem !== null);
 
138
 
 
139
            var mappedPosition = indicatorsBar.mapFromItem(dataItem, dataItem.width/2, dataItem.height/2);
 
140
 
 
141
            indicatorsBar.selectItemAt(mappedPosition.x);
 
142
            indicatorsBar.expanded = true;
 
143
            wait_for_expansion_to_settle();
 
144
 
 
145
 
 
146
            // mappedPosition contained within mappedRect
 
147
            tryCompareFunction(function() {
 
148
                var mappedRect = indicatorsBar.mapFromItem(dataItem, 0, 0, dataItem.width, dataItem.height);
 
149
                return mappedRect.x <= mappedPosition.x; },
 
150
            true);
 
151
            tryCompareFunction(function() {
 
152
                var mappedRect = indicatorsBar.mapFromItem(dataItem, 0, 0, dataItem.width, dataItem.height);
 
153
                return mappedRect.x + mappedRect.width >= mappedPosition.x;
 
154
            }, true);
 
155
        }
 
156
 
 
157
        function test_scrollOffset() {
 
158
            indicatorsBar.expanded = true;
 
159
            wait_for_expansion_to_settle();
 
160
 
 
161
            var lastItemIndex = root.originalModelData.length-1;
 
162
            var dataItem = findChild(indicatorsBar, root.originalModelData[lastItemIndex]["identifier"] + "-panelItem");
 
163
            verify(dataItem !== null);
 
164
 
 
165
            var row = findChild(indicatorsBar, "indicatorItemRow");
 
166
            // test will not work without these conditions
 
167
            verify(row.width >= indicatorsBar.width + dataItem.width);
 
168
 
 
169
            var mappedPosition = indicatorsBar.mapFromItem(dataItem, dataItem.width/2, dataItem.height/2);
 
170
            indicatorsBar.addScrollOffset(-dataItem.width);
 
171
            var newMappedPosition = indicatorsBar.mapFromItem(dataItem, dataItem.width/2, dataItem.height/2);
 
172
 
 
173
            compare(mappedPosition.x, newMappedPosition.x - dataItem.width);
 
174
        }
 
175
 
 
176
        function test_selectItemWhenExpanded_data() {
 
177
            return [
 
178
                { index: 3 },
 
179
                { index: 4 }
 
180
            ];
 
181
        }
 
182
 
 
183
        function test_selectItemWhenExpanded(data) {
 
184
            indicatorsBar.expanded = true;
 
185
            wait_for_expansion_to_settle();
 
186
 
 
187
            var dataItem = findChild(indicatorsBar, root.originalModelData[data.index]["identifier"] + "-panelItem");
 
188
            if (indicatorsBar.mapFromItem(dataItem, dataItem.width/2, dataItem.height/2).x < 0) {
 
189
                skip("Out of bounds");
 
190
            }
 
191
            mouseClick(dataItem, dataItem.width/2, dataItem.height/2);
 
192
            verify(dataItem.selected === true);
 
193
        }
 
194
 
 
195
        function test_visibleIndicators_data() {
 
196
            return [
 
197
                { visible: [true, false, true, false, true, true] },
 
198
                { visible: [false, false, false, false, false, false] }
 
199
            ];
 
200
        }
 
201
 
 
202
        function test_visibleIndicators(data) {
 
203
            for (var i = 0; i < data.visible.length; i++) {
 
204
                var visible = data.visible[i];
 
205
                root.setIndicatorVisible(i, visible);
 
206
 
 
207
                var dataItem = findChild(indicatorsBar, root.originalModelData[i]["identifier"] + "-panelItem");
 
208
                tryCompare(dataItem, "opacity", visible ? 1.0 : 0.0);
 
209
                tryCompareFunction(function() { return dataItem.width > 0.0; }, visible);
 
210
            }
 
211
 
 
212
            indicatorsBar.expanded = true;
 
213
            wait_for_expansion_to_settle();
 
214
 
 
215
            for (var i = 0; i < data.visible.length; i++) {
 
216
                root.setIndicatorVisible(i, data.visible[i]);
 
217
 
 
218
                var dataItem = findChild(indicatorsBar, root.originalModelData[i]["identifier"] + "-panelItem");
 
219
                tryCompareFunction(function() { return dataItem.opacity > 0.0; }, true);
 
220
                tryCompareFunction(function() { return dataItem.width > 0.0; }, true);
 
221
            }
 
222
        }
 
223
    }
 
224
}