File: tst_header.13.qml

package info (click to toggle)
lomiri-ui-toolkit 1.3.5010%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,900 kB
  • sloc: cpp: 85,772; python: 5,528; sh: 1,364; javascript: 919; ansic: 573; makefile: 204
file content (483 lines) | stat: -rw-r--r-- 18,401 bytes parent folder | download
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * 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 Lomiri.Components 1.3
import Lomiri.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 ? parent.width : 234
        height: root.initialHeaderHeight

        Rectangle {
            // to visualize the header
            anchors.fill: parent
            color: LomiriColors.red
            opacity: 0.5
            border {
                color: "black"
                width: 2
            }
        }

        onMovingChanged: {
            // The value of movingLabel.text is used in:
            // - test_dont_move_when_flickable_shortens_bug1514143()
            // - test_dont_move_exposed_header_when_scrolling_down_bug1514143()
            // - test_dont_move_hidden_header_when_scrolling_up()
            movingLabel.text = "Moving changed to " + moving;
            movingLabel.color = moving ? "purple" : "red";
        }
    }

    Flickable {
        id: flickable
        anchors {
            top: header.flickable ? parent.top : header.bottom
            left: parent.left
            right: parent.right
            bottom: parent.bottom
        }
        contentHeight: root.height * 2

        Grid {
            id: switchGrid
            columns: 2
            spacing: units.gu(1)
            anchors {
                top: parent.top
                left: parent.left
                leftMargin: units.gu(5)
                topMargin: 2*root.initialHeaderHeight
            }
            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 {
            id: flickLabel
            anchors {
                top: contentYButton.bottom
                horizontalCenter: parent.horizontalCenter
                topMargin: units.gu(8)
            }
            text: "Flick me"
        }
        Label {
            id: movingLabel
            anchors {
                top: flickLabel.bottom
                horizontalCenter: parent.horizontalCenter
            }
            text: "hmm"
        }
    }

    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
    }

    Header {
        id: hiddenHeader
        exposed: false
        height: root.initialHeaderHeight
    }

    LomiriTestCase {
        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() {
            header.flickable = flickable;
            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_0_initially_hidden() {
            // Don't show an animation if the header is hidden initially.
            compare(hiddenHeader.y, -header.height,
                    "Hidden header has wrong initial y-value.");
        }

        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_exposed() {
            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 + otherFlickable.initialTopMargin,
                    "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_margins_invisible_header_bug1560458_bug1560419_data() {
            return [
                        { tag: "Inititial topMargin = 0",
                            flickable: flickable,
                            initialTopMargin: 0
                        },
                        { tag: "Initial topMargin > 0",
                            flickable: otherFlickable,
                            initialTopMargin: otherFlickable.initialTopMargin
                        }
                    ];
        }

        function test_flickable_margins_invisible_header_bug1560458_bug1560419(data) {
            var h = header.height;
            var fl = data.flickable;
            // the original margin before the flickable is connected to the header
            var fmargin = data.initialTopMargin;
            if (header.flickable !== fl) {
                compare(fl.topMargin, fmargin,
                        "Incorrect initial topMargin for flickable.");
                header.flickable = fl;
            }

            compare(fl.topMargin, h + fmargin,
                    "Flickable top margin does not match header height.");
            header.visible = false;
            compare(fl.topMargin, fmargin,
                    "Invisible header sets flickable.topMargin.");
            header.visible = true;
            compare(fl.topMargin, h + fmargin,
                    "Making flickable visible does not set flickable.topMargin.");
            header.height = 0;
            compare(fl.topMargin, fmargin,
                    "Header with height = 0 sets flickable.topMargin.");
            header.height = h;
            compare(fl.topMargin, h + fmargin,
                    "Setting header height does not set flickable.topMargin.");

            // Setting opacity to 0 should not change flickable.topMargin.
            // This allows opacity animations.
            header.opacity = 0.0;
            compare(fl.topMargin, h + fmargin,
                    "Setting header opacity to 0 changes flickable.topMargin.");
            header.opacity = 1.0;
            compare(fl.topMargin, h + fmargin,
                    "Setting header opacity to 1 changes flickable.topMargin.");

            header.parent = null;
            compare(fl.topMargin, fmargin,
                    "Header with no parent sets flickable.topMargin.");
            header.parent = root;
            compare(fl.topMargin, h + fmargin,
                    "Header with parent does not set flickable.topMargin.");
        }

        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;
        }

        function test_dont_move_when_flickable_shortens_bug1514143() {
            var flickableContentHeight = flickable.contentHeight;
            movingLabel.text = "HEADER DID NOT MOVE";
            flickable.contentHeight = 200;
            compare(movingLabel.text, "HEADER DID NOT MOVE",
                    "Reducing flickable contents height unneccessary sets header.moving.");
            flickable.contentHeight = flickableContentHeight;
            compare(movingLabel.text, "HEADER DID NOT MOVE",
                    "Increasing flickable contents height unneccessary sets header.moving.");
        }

        function test_dont_move_exposed_header_when_scrolling_down_bug1514143() {
            scroll_down(); scroll_down();
            wait_for_exposed(false, "Header doesn't hide when scrolling down.");
            header.exposed = true;
            wait_for_exposed(true, "Cannot expose header after scrolling down.");
            movingLabel.text = "HEADER DID NOT MOVE";
            scroll_up();
            wait(100);
            compare(movingLabel.text, "HEADER DID NOT MOVE",
                    "Header moved when scrolling up while header was already exposed.");
        }

        function test_dont_move_hidden_header_when_scrolling_up() {
            // flickable is at the top.
            header.exposed = false;
            wait_for_exposed(false, "Cannot hide header.");
            movingLabel.text = "HEADER DID NOT MOVE";
            scroll_down();
            wait(100);
            compare(movingLabel.text, "HEADER DID NOT MOVE",
                    "Header moved when scrolling down while header was already hidden.");
        }
    }
}