~zsombi/ubuntu-ui-toolkit/platform_menu

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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright 2015 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/>.
 *
 * Author: Andrea Bernabei <andrea.bernabei@canonical.com>
 */

import QtQuick 2.4
import QtTest 1.0
import Ubuntu.Test 1.0
import Ubuntu.Components 1.3
import Ubuntu.Components.Styles 1.3
import QtQml.Models 2.1

Item {
    id: main
    width: units.gu(50)
    height: units.gu(100)


    Component {
        id: flickableComp
        Item {
            width: units.gu(20)
            height: units.gu(30)
            property alias flickable: freshFlickable
            property alias scrollbar: freshScrollbar
            property alias content: content
            Flickable {
                id: freshFlickable
                anchors.fill: parent
                contentHeight: content.height
                contentWidth: content.width
                clip: true
                Rectangle {
                    id: content
                    width: units.gu(40)
                    //make this much taller than the flickable to avoid timing issues when
                    //detecting if the flickable is flicking in the tests
                    height: units.gu(200)
                    color: "blue"
                }
            }
            Scrollbar {
                id: freshScrollbar
                flickableItem: parent.flickable
            }
        }
    }

    SignalSpy {
        id: signalSpy
    }
    SignalSpy {
        id: anotherSignalSpy
    }

    Flickable {
        anchors.fill: parent
        Column {
            id: column
            Repeater {
                model: layoutsModel
            }
        }
    }

    VisualItemModel {
        id: layoutsModel
        Flickable {
            id: flickable_bottomAlign_anchors
        }
        Scrollbar {
            id: scrollbar_bottomAlign_anchors
            flickableItem: flickable_bottomAlign_anchors
            align: Qt.AlignBottom
        }

        Flickable {
            id: flickable_topAlign_anchors
        }
        Scrollbar {
            id: scrollbar_topAlign_anchors
            flickableItem: flickable_topAlign_anchors
            align: Qt.AlignBottom
        }

        Flickable {
            id: flickable_leadingAlign_anchors
        }
        Scrollbar {
            id: scrollbar_leadingAlign_anchors
            flickableItem: flickable_leadingAlign_anchors
            align: Qt.AlignLeading
        }

        Flickable {
            id: flickable_trailingAlign_anchors
        }
        Scrollbar {
            id: scrollbar_trailingAlign_anchors
            flickableItem: flickable_trailingAlign_anchors
            align: Qt.AlignTrailing
        }
    }

    UbuntuTestCase {
        name: "Scrollbar"
        when: windowShown

        function getFreshFlickable() {
            var flickable = flickableComp.createObject(column)
            if (!flickable) {
                console.log("Error: dynamic item creation failed.")
            }
            return flickable
        }
        function setVeryLongContentItem(flickable) {
            flickable.contentHeight = flickable.height * 10 + 1
        }
        function setupSignalSpy(spy, target, signalName) {
            spy.clear()
            //reset signalName otherwise it will look for the old signalName in the new target
            spy.signalName = ""
            spy.target = target
            spy.signalName = signalName
        }

        function warningMsg(msg) {
            return testUtil.callerFile() + ": " + msg
        }

        function test_bottomAlign_anchors() {
            compare(scrollbar_bottomAlign_anchors.flickableItem,
                    flickable_bottomAlign_anchors, "wrong flickableItem")
            compare(scrollbar_bottomAlign_anchors.anchors.left,
                    scrollbar_bottomAlign_anchors.flickableItem.left, "left anchor")
            compare(scrollbar_bottomAlign_anchors.anchors.right,
                    scrollbar_bottomAlign_anchors.flickableItem.right, "right anchor")
            compare(scrollbar_bottomAlign_anchors.anchors.bottom,
                    scrollbar_bottomAlign_anchors.flickableItem.bottom, "bottom anchor")
            //we can't check that the top anchor is not anchored
        }
        function test_topAlign_anchors() {
            compare(scrollbar_topAlign_anchors.flickableItem,
                    flickable_topAlign_anchors, "wrong flickableItem")
            compare(scrollbar_topAlign_anchors.anchors.left,
                    scrollbar_topAlign_anchors.flickableItem.left, "left anchor")
            compare(scrollbar_topAlign_anchors.anchors.right,
                    scrollbar_topAlign_anchors.flickableItem.right, "right anchor")
            compare(scrollbar_topAlign_anchors.anchors.top,
                    scrollbar_topAlign_anchors.flickableItem.top, "top anchor")
        }
        function test_leadingAlign_anchors() {
            compare(scrollbar_leadingAlign_anchors.flickableItem,
                    flickable_leadingAlign_anchors, "wrong flickableItem")
            compare(scrollbar_leadingAlign_anchors.anchors.left,
                    scrollbar_leadingAlign_anchors.flickableItem.left, "left anchor")
            compare(scrollbar_leadingAlign_anchors.anchors.bottom,
                    scrollbar_leadingAlign_anchors.flickableItem.bottom, "bottom anchor")
            compare(scrollbar_leadingAlign_anchors.anchors.top,
                    scrollbar_leadingAlign_anchors.flickableItem.top, "top anchor")
        }
        function test_trailingAlign_anchors() {
            compare(scrollbar_trailingAlign_anchors.flickableItem,
                    flickable_trailingAlign_anchors, "wrong flickableItem")
            compare(scrollbar_trailingAlign_anchors.anchors.right,
                    scrollbar_trailingAlign_anchors.flickableItem.right, "right anchor")
            compare(scrollbar_trailingAlign_anchors.anchors.bottom,
                    scrollbar_trailingAlign_anchors.flickableItem.bottom, "bottom anchor")
            compare(scrollbar_trailingAlign_anchors.anchors.top,
                    scrollbar_trailingAlign_anchors.flickableItem.top, "top anchor")
        }

        function test_indicatorStyleWhileFlicking_shortContent() {
            var freshTestItem = getFreshFlickable()
            if (!freshTestItem) return

            var flickable = freshTestItem.flickable
            var scrollbar = freshTestItem.scrollbar

            compare(scrollbar.__styleInstance.veryLongContentItem, false, "Scrollable item should be short.")

            setupSignalSpy(signalSpy, flickable, "movingChanged")

            flick(flickable, 1, 2, units.gu(2), -units.gu(10))

            signalSpy.wait()
            compare(signalSpy.count, 1, "No movingChanged signal after simulating a flick.")
            compare(flickable.moving, true, "Flickable not moving after simulating a flick.")
            compare(scrollbar.__styleInstance.state, "indicator", "Wrong style while flicking.")

            //we don't set it up before because the hinting feature already changes the style to thumb
            //at the beginning
            setupSignalSpy(anotherSignalSpy, scrollbar.__styleInstance, "stateChanged")

            //make sure the flickable stops
            mousePress(flickable, 0, 0)
            mouseMove(flickable, 0, 0)
            mouseRelease(flickable, 0, 0)

            signalSpy.wait()
            compare(flickable.moving, false, "Flickable still moving after simulating mouse click.")
            compare(signalSpy.count, 2, "No movingChanged signal after Flickable stopped moving.")

            anotherSignalSpy.wait()
            compare(anotherSignalSpy.count, 1, "State unchanged after Flickable stopped moving.")
            compare(scrollbar.__styleInstance.state, "hidden", "Wrong style after the item stopped moving.")

            freshTestItem.destroy()
        }

        function test_thumbStyleWhileFlicking_veryLongContent() {
            var freshTestItem = getFreshFlickable()
            if (!freshTestItem) return

            var flickable = freshTestItem.flickable
            var scrollbar = freshTestItem.scrollbar

            setupSignalSpy(signalSpy, scrollbar.__styleInstance, "veryLongContentItemChanged")
            setVeryLongContentItem(flickable)
            signalSpy.wait()

            compare(scrollbar.__styleInstance.veryLongContentItem, true, "Very long content item not detected")

            setupSignalSpy(signalSpy, flickable, "movingChanged")
            flick(flickable, 1, 2, units.gu(2), -units.gu(10))

            signalSpy.wait()
            compare(signalSpy.count, 1, "No movingChanged signal after simulating a flick.")
            compare(flickable.moving, true, "Flickable not moving after simulating a flick.")
            compare(scrollbar.__styleInstance.state, "thumb", "Wrong style while flicking a very long item")

            //we don't set it up before because the hinting feature already changes the style to thumb
            //at the beginning
            setupSignalSpy(anotherSignalSpy, scrollbar.__styleInstance, "stateChanged")

            //make sure the flickable stops
            mousePress(flickable, 0, 0)
            mouseMove(flickable, 0, 0)
            mouseRelease(flickable, 0, 0)

            signalSpy.wait()
            compare(flickable.moving, false, "Flickable still moving after simulating mouse click.")
            compare(signalSpy.count, 2, "No movingChanged signal after Flickable stopped moving.")

            anotherSignalSpy.wait()
            compare(anotherSignalSpy.count, 1, "State unchanged after Flickable stopped moving.")
            compare(scrollbar.__styleInstance.state, "hidden", "Wrong style after a the item stopped moving.")
            freshTestItem.destroy()
        }
    }
}