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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* Copyright (C) 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/>.
*/
import QtQuick 2.4
import Ubuntu.Components 1.3
import Ubuntu.Test 1.0
Item {
id: root
width: units.gu(50)
height: units.gu(70)
property real initialHeaderHeight: units.gu(6)
Header {
id: header
flickable: flickable
z:1
width: parent.width
height: root.initialHeaderHeight
Rectangle {
// to visualize the header
anchors.fill: parent
color: UbuntuColors.red
opacity: 0.5
border {
color: "black"
width: 2
}
}
}
Flickable {
id: flickable
anchors.fill: parent
contentHeight: height * 2
Grid {
id: switchGrid
columns: 2
spacing: units.gu(1)
anchors {
top: parent.top
left: parent.left
margins: units.gu(5)
}
Switch {
id: lockedSwitch
checked: null === header.flickable
function trigger() {
if (header.flickable) {
header.flickable = null;
} else {
header.flickable = flickable;
}
}
}
Label {
text: "header locked"
}
Switch {
id: hiddenSwitch
checked: header.exposed
function trigger() {
header.exposed = !header.exposed;
}
}
Label {
text: "header exposed"
}
Item {
width: 1
height: 1
}
}
Button {
id: contentYButton
anchors {
top: switchGrid.bottom
topMargin: units.gu(4)
horizontalCenter: parent.horizontalCenter
}
property real newY: flickable.contentY == 0 ? -header.height : 0
onClicked: flickable.contentY = newY;
text: "Set contentY to " + newY
}
Label {
anchors {
top : contentYButton.bottom
horizontalCenter: parent.horizontalCenter
topMargin: units.gu(8)
}
text: "Flick me"
}
}
Rectangle {
id: reparentTestItem
anchors {
right: parent.right
bottom: parent.bottom
}
height: units.gu(15)
width: parent.width / 2
color: "blue"
Label {
anchors {
horizontalCenter: parent.horizontalCenter
bottom: parent.bottom
}
text: "Click to reparent"
color: "white"
}
MouseArea {
anchors.fill: parent
onClicked: {
if (header.parent === root) {
header.parent = reparentTestItem;
} else {
header.parent = root;
}
}
}
}
Flickable {
id: otherFlickable
property real initialTopMargin: 123
topMargin: initialTopMargin
height: units.gu(10)
contentHeight: units.gu(20);
}
Header {
id: otherHeader
}
UbuntuTestCase {
name: "Header"
when: windowShown
id: testCase
function initTestCase() {
wait_for_exposed(true, "Header is not exposed initially.");
compare(otherHeader.flickable, null, "Flickable not null by default.");
compare(header.flickable, flickable, "Flickable not properly intialized.");
// note: moving may be true briefly due to header height changes, but
// it does not change in the initialization after wait_for_exposed() above.
compare(header.moving, false, "Header moving initially.");
}
function init() {
flickable.contentHeight = 2*flickable.height;
flickable.interactive = true;
flickable.contentY = -header.height;
header.exposed = true;
wait_for_exposed(true);
}
function scroll(dy) {
var p = centerOf(flickable);
// Use mouseWheel to scroll because mouseDrag is very unreliable
// and does not properly handle negative values for dy.
mouseWheel(flickable, p.x, p.y, 0,dy);
}
function scroll_down() {
scroll(-2.0*header.height);
}
function scroll_up() {
scroll(header.height);
}
function wait_for_exposed(exposed, errorMessage) {
tryCompare(header, "exposed", exposed, 5000, errorMessage);
// wait for the animation to finish:
tryCompare(header, "moving", false, 5000, "Header still moving?");
if (exposed) {
compare(header.y, 0, errorMessage +
" y-value/exposed mismatch for exposed header!");
} else {
compare(header.y, -header.height, errorMessage +
" y-value/exposed mismatch for hidden header!");
}
}
function test_reparent_width() {
// test initial header width:
compare(header.parent, root);
compare(header.width, root.width);
compare(header.y, 0);
// test width update when changing parent:
header.parent = reparentTestItem;
compare(header.parent, reparentTestItem);
compare(header.width, reparentTestItem.width);
compare(header.y, 0);
// test width update when changing width of parent:
var old_width = reparentTestItem.width;
reparentTestItem.width = units.gu(5);
compare(header.width, reparentTestItem.width);
compare(header.y, 0);
// revert to original parent:
header.parent = root;
compare(header.parent, root);
compare(header.width, root.width);
compare(header.y, 0);
reparentTestItem.width = old_width;
}
function test_height_change() {
// first scroll down for this test, the following test repeats
// the cases at the top of the flickable.
scroll_down();
header.height = units.gu(15);
wait_for_exposed(true, "Increasing header height hides header.");
scroll_down();
header.exposed = false;
wait_for_exposed(false, "Header with height set does not hide.");
header.height = units.gu(2);
wait_for_exposed(false, "Decreasing header height exposes it.");
header.exposed = true;
wait_for_exposed(true, "Header with decreased height does not expose.");
// revert to initial state
header.height = root.initialHeaderHeight;
flickable.contentY = -header.height;
wait_for_exposed(true, "Setting flickable.contentY hides the header.");
}
function test_height_change_at_top() {
// Near the top, changing the header height exposes the header
// to avoid the header becoming inaccessible because it cannot
// be pulled down.
header.exposed = false;
wait_for_exposed(false);
header.height = units.gu(15);
wait_for_exposed(true, "Increasing header height at top hides header.");
// making the header smaller does not need to expose it, because there is
// enough space to pull it down.
// revert to original state.
header.height = root.initialHeaderHeight;
flickable.contentY = -header.height;
header.exposed = true;
wait_for_exposed(true);
}
function test_set_exposed_to_hide_and_show() {
header.exposed = false;
wait_for_exposed(false, "Cannot hide header by setting visible to false.");
header.exposed = true;
wait_for_exposed(true, "Cannot show header by setting visible to true.");
// change the value of exposed twice quickly:
header.exposed = false;
header.exposed = true;
wait_for_exposed(true, "Quickly hiding and showing header does not result in exposed header.");
// and the other way around:
header.exposed = false;
wait_for_exposed(false);
header.exposed = true;
header.exposed = false;
wait_for_exposed(false, "Quickly showing and hiding header does not result in hidden header.");
header.exposed = true;
wait_for_exposed(true);
}
function test_scroll_updates_visible() {
scroll_down();
wait_for_exposed(false, "Scrolling down does not hide header.");
scroll_up();
wait_for_exposed(true, "Scrolling up does not show header.");
}
function test_flickable_margins() {
compare(flickable.topMargin, header.height, "Flickable top margin does not match header height.");
header.height = units.gu(15);
wait_for_exposed(true, "Increasing header height at top hides header.");
compare(flickable.topMargin, header.height, "Updating header height does not update flickable top margin.");
header.height = root.initialHeaderHeight; // revert
wait_for_exposed(true, "Reverting header height at top hides header.");
compare(flickable.topMargin, header.height, "Reverting header height does not revert flickable top margin.");
compare(otherFlickable.topMargin, otherFlickable.initialTopMargin, "Flickable top margin is not initialized properly.");
header.flickable = otherFlickable;
compare(otherFlickable.topMargin, header.height, "Setting flickable does not update flickable top margin.");
compare(flickable.topMargin, 0, "Changing the flickable does not reset the previous flickable top margin to 0.");
header.flickable = flickable;
compare(otherFlickable.topMargin, otherFlickable.initialTopMargin, "Reverting flickable does not restore the other flickable top margin.");
compare(flickable.topMargin, header.height, "Reverting flickable breaks flickable top margin.");
}
function test_flickable_contentHeight_bug1156573() {
var old_height = flickable.contentHeight;
header.exposed = false;
wait_for_exposed(false);
flickable.contentHeight = flickable.height / 2;
wait_for_exposed(true, "Small content height does not expose the header.");
// revert:
flickable.contentHeight = old_height;
compare(header.exposed, true, "Reverting flickable content height hides the header.");
}
function test_flickable_interactive() {
header.exposed = false;
wait_for_exposed(false);
flickable.interactive = false;
wait_for_exposed(true, "Making flickable not interactive does not expose the header.");
// revert:
flickable.interactive = true;
compare(header.exposed, true, "Reverting flickable exposed hides the header.");
}
function test_scroll_disconnected_flickable() {
var hy = header.y;
header.flickable = null;
scroll_down();
compare(header.y, hy, "Header scrolls when disconnected flickable scrolls down.");
wait_for_exposed(true, "Scrolling down disconnected flickable hides header.");
scroll_up();
compare(header.y, hy, "Header scrolls when disconnected flickable scrolls up.");
wait_for_exposed(true, "Scrolling up disconnected flickable hides header.");
header.flickable = flickable;
}
}
}
|