~kalikiana/ubuntu-ui-toolkit/appsettings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright 2014 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.0
import QtTest 1.0
import Ubuntu.Test 1.0
import Ubuntu.Components 1.0
import Ubuntu.Components.ListItems 1.0

Item {
    width: units.gu(40)
    height: units.gu(60)

    ExpandablesColumn {
        id: expandablesColumn
        anchors { left: parent.left; top: parent.top; right: parent.right }
        height: units.gu(60)
        clip: true

        Repeater {
            id: repeater
            model: 20
            delegate: Expandable {
                id: expandable
                objectName: "expandable" + index
                expandedHeight: contentColumn.height

                onClicked: expanded = !expanded

                Column {
                    id: contentColumn
                    anchors { left: parent.left; right: parent.right; top: parent.top }
                    Rectangle {
                        anchors { left: parent.left; right: parent.right}
                        id: collapsedRect
                        color: index % 2 == 0 ? "khaki" : "blue"
                        height: expandable.collapsedHeight
                    }
                    Rectangle {
                        anchors { left: parent.left; right: parent.right }
                        height: units.gu(40)
                        color: "orange"
                    }
                }
            }
        }
    }

    UbuntuTestCase {
        name: "ExpandablesColumn"
        when: windowShown

        function init() {
            waitForRendering(expandablesColumn);
        }

        function expandItem(item) {
            item.expanded = true;
            var targetHeight = Math.min(item.expandedHeight, expandablesColumn.height - item.collapsedHeight);
            tryCompare(item, "height", targetHeight);
        }

        function collapse() {
            var expandedItem = expandablesColumn.expandedItem;
            if (expandedItem != undefined) {
                expandedItem.expanded = false;
                tryCompare(expandedItem, "height", expandedItem.collapsedHeight);
            }
        }

        function test_expandedItem() {
            var item = findChild(expandablesColumn, "expandable1");
            expandItem(item);

            // expandedItem needs to point to the expanded item
            compare(expandablesColumn.expandedItem, item);

            collapse();

            // expandedItem must be unset after collapsing
            compare(expandablesColumn.expandedItem, undefined);
        }

        function test_noScrollingNeeded() {
            var item = findChild(expandablesColumn, "expandable1");
            compare(expandablesColumn.mapFromItem(item).y, item.collapsedHeight);

            expandItem(item);

            compare(expandablesColumn.mapFromItem(item).y, item.collapsedHeight);
        }

        function test_scrollToTop() {
            expandablesColumn.height = units.gu(30);

            var item = findChild(expandablesColumn, "expandable1");
            compare(expandablesColumn.mapFromItem(item).y, item.collapsedHeight);

            expandItem(item);

            compare(expandablesColumn.mapFromItem(item).y, 0);
        }

        function test_scrollIntoView() {
            var item = findChild(expandablesColumn, "expandable9");
            expandItem(item);

            // The item must be scrolled upwards, leaving space for one other item at the bottom
            compare(expandablesColumn.mapFromItem(item).y, expandablesColumn.height - item.collapsedHeight - item.expandedHeight);
        }

        function test_collapseByClickingOutside() {
            // expand item 0
            var item = findChild(expandablesColumn, "expandable0");
            expandItem(item);

            // click on item 1
            var item1 = findChild(expandablesColumn, "expandable1");
            mouseClick(item1, item1.width / 2, item1.height / 2);

            // make sure item1 is collapsed
            tryCompare(item, "expanded", false);
        }

        function test_dimOthers() {
            var item = findChild(expandablesColumn, "expandable1");
            expandItem(item);

            for (var i = 0; i < repeater.count; ++i) {
                var isCurrent = (repeater.itemAt(i) === expandablesColumn.expandedItem)
                compare(repeater.itemAt(i).opacity, isCurrent ? 1 : .5)
            }
        }

        function cleanup() {
            // Restore listview height
            expandablesColumn.height = units.gu(60);
            collapse();
            // scroll the column back to top
            expandablesColumn.flick(0, units.gu(500));
            tryCompare(expandablesColumn, "flicking", false);
        }
    }
}