~zsombi/ubuntu-ui-toolkit/listitemSelectModeBugs

« back to all changes in this revision

Viewing changes to tests/unit_x11/tst_components/tst_pageheader.qml

  • Committer: Zsombor Egri
  • Date: 2015-11-16 06:35:05 UTC
  • mfrom: (1664.1.1 listitemSelectModeBugs)
  • Revision ID: zsombor.egri@canonical.com-20151116063505-cwn2qfks7qzk10g9
re-sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
import QtQuick 2.4
 
18
import Ubuntu.Components 1.3
 
19
import Ubuntu.Test 1.0
 
20
 
 
21
Item {
 
22
    // Wrap the root Item to work around bug #1504755 which
 
23
    //  causes the OverflowPanel to open behind the PageHeader
 
24
    //  without this wrapper Item.
 
25
    id: wrapper
 
26
    width: units.gu(50)
 
27
    height: units.gu(70)
 
28
 
 
29
    Item {
 
30
        anchors.fill: parent
 
31
        id: root
 
32
 
 
33
        property real initialHeaderHeight: units.gu(6)
 
34
 
 
35
        property list<Action> sectionActions: [
 
36
            Action { text: "first" },
 
37
            Action { text: "second" },
 
38
            Action { text: "third" }
 
39
        ]
 
40
 
 
41
        Rectangle {
 
42
            id: alternativeContents
 
43
            visible: header.contents === alternativeContents
 
44
            objectName: "alternative_contents"
 
45
 
 
46
            anchors.fill: parent
 
47
            color: UbuntuColors.red
 
48
 
 
49
            Label {
 
50
                anchors.centerIn: parent
 
51
                text: "Custom header contents"
 
52
                color: "white"
 
53
            }
 
54
        }
 
55
 
 
56
        property list<Action> actionList: [
 
57
            Action {
 
58
                iconName: "settings"
 
59
                text: "first"
 
60
                onTriggered: print("Trigger first action")
 
61
                objectName: "action1"
 
62
            },
 
63
            Action {
 
64
                iconName: "info"
 
65
                text: "second"
 
66
                onTriggered: print("Trigger second action")
 
67
            },
 
68
            Action {
 
69
                iconName: "search"
 
70
                text: "third"
 
71
                onTriggered: print("Trigger third action")
 
72
            },
 
73
            Action {
 
74
                iconName: "appointment"
 
75
                text: "fourth"
 
76
                onTriggered: print("Trigger fourth action")
 
77
            }
 
78
        ]
 
79
 
 
80
        PageHeader {
 
81
            id: header
 
82
            flickable: flickable
 
83
            z:1
 
84
 
 
85
            title: "Default title"
 
86
            contents: contentsSwitch.checked ? alternativeContents : null
 
87
            sections.actions: sectionsSwitch.checked ? root.sectionActions : []
 
88
            trailingActionBar.actions: trailingActionsSwitch.checked ?
 
89
                                           root.actionList : []
 
90
            navigationActions: leadingActionsSwitch.checked ?
 
91
                                          root.actionList : []
 
92
        }
 
93
 
 
94
        Flickable {
 
95
            id: flickable
 
96
            anchors {
 
97
                top: header.flickable ? parent.top : header.bottom
 
98
                left: parent.left
 
99
                right: parent.right
 
100
                bottom: parent.bottom
 
101
            }
 
102
            contentHeight: root.height * 2
 
103
 
 
104
            Grid {
 
105
                id: switchGrid
 
106
                columns: 2
 
107
                spacing: units.gu(1)
 
108
                anchors {
 
109
                    top: parent.top
 
110
                    left: parent.left
 
111
                    leftMargin: units.gu(5)
 
112
                    topMargin: root.initialHeaderHeight
 
113
                }
 
114
 
 
115
                Switch {
 
116
                    id: lockedSwitch
 
117
                    checked: null === header.flickable
 
118
                    function trigger() {
 
119
                        if (header.flickable) {
 
120
                            header.flickable = null;
 
121
                        } else {
 
122
                            header.flickable = flickable;
 
123
                        }
 
124
                    }
 
125
                }
 
126
                Label {
 
127
                    text: "header locked"
 
128
                }
 
129
 
 
130
                Switch {
 
131
                    id: hiddenSwitch
 
132
                    checked: header.exposed
 
133
                    function trigger() {
 
134
                        header.exposed = !header.exposed;
 
135
                    }
 
136
                }
 
137
                Label {
 
138
                    text: "header exposed"
 
139
                }
 
140
 
 
141
                Switch {
 
142
                    id: leadingActionsSwitch
 
143
                    checked: false
 
144
                }
 
145
                Label {
 
146
                    text: "leading actions"
 
147
                }
 
148
 
 
149
                Switch {
 
150
                    id: trailingActionsSwitch
 
151
                    checked: true
 
152
                }
 
153
                Label {
 
154
                    text: "trailing actions"
 
155
                }
 
156
 
 
157
                Switch {
 
158
                    id: contentsSwitch
 
159
                    checked: false
 
160
                }
 
161
                Label {
 
162
                    text: "replace title by contents"
 
163
                }
 
164
 
 
165
                Switch {
 
166
                    id: sectionsSwitch
 
167
                    checked: false
 
168
                }
 
169
                Label {
 
170
                    text: "show sections"
 
171
                }
 
172
            }
 
173
 
 
174
            PageHeader {
 
175
                id: defaultHeader
 
176
                visible: false
 
177
            }
 
178
        }
 
179
 
 
180
        UbuntuTestCase {
 
181
            name: "PageHeader"
 
182
            when: windowShown
 
183
            id: testCase
 
184
 
 
185
            property var style;
 
186
            function initTestCase() {
 
187
                style = header.__styleInstance;
 
188
            }
 
189
 
 
190
            function scroll(dy) {
 
191
                var p = centerOf(flickable);
 
192
                // Use mouseWheel to scroll because mouseDrag is very unreliable
 
193
                // and does not properly handle negative values for dy.
 
194
                mouseWheel(flickable, p.x, p.y, 0, dy);
 
195
            }
 
196
 
 
197
            function scroll_down() {
 
198
                scroll(-2.0*header.height);
 
199
            }
 
200
 
 
201
            function scroll_up() {
 
202
                scroll(header.height);
 
203
            }
 
204
 
 
205
            function wait_for_exposed(exposed, errorMessage) {
 
206
                tryCompare(header, "exposed", exposed, 5000, errorMessage);
 
207
                // wait for the animation to finish:
 
208
                tryCompare(header, "moving", false, 5000, "Header still moving?");
 
209
                if (exposed) {
 
210
                    compare(header.y, 0, errorMessage +
 
211
                            " y-value/exposed mismatch for exposed header!");
 
212
                } else {
 
213
                    compare(header.y, -header.height, errorMessage +
 
214
                            " y-value/exposed mismatch for hidden header!");
 
215
                }
 
216
            }
 
217
 
 
218
            // Use this function to prevent copying the color by reference.
 
219
            function color_by_value(color) {
 
220
                return Qt.rgba(color.r, color.g, color.b, color.a);
 
221
            }
 
222
 
 
223
            function test_height() {
 
224
                var divider = findChild(style, "header_divider");
 
225
                compare(header.height, style.contentHeight + divider.height,
 
226
                        "Incorrect initial header height.");
 
227
                var initialHeight = header.height;
 
228
 
 
229
                var sections = header.sections;
 
230
                compare(header.sections.height, 0,
 
231
                        "Empty sections has non-0 height.");
 
232
 
 
233
                sections.actions = root.sectionActions;
 
234
                compare(sections.height > 0, true,
 
235
                        "Sections with actions has non-positive height.");
 
236
                compare(header.height, style.contentHeight + divider.height + sections.height,
 
237
                        "Header with sections has incorrect total height.");
 
238
 
 
239
                sections.actions = [];
 
240
                compare(header.height, initialHeight,
 
241
                        "Unsetting sections does not revert the header height.");
 
242
            }
 
243
 
 
244
            function test_background_color() {
 
245
                var background = findChild(style, "header_background");
 
246
                compare(background.color, style.backgroundColor,
 
247
                        "Incorrect initial background color.");
 
248
 
 
249
                var initialColor = color_by_value(style.backgroundColor);
 
250
                var otherColor = "#CCFDAA"; // a random color.
 
251
                style.backgroundColor = otherColor;
 
252
                compare(Qt.colorEqual(background.color, otherColor), true,
 
253
                        "Updating style background color does not change the background color.");
 
254
 
 
255
                style.backgroundColor = initialColor;
 
256
                compare(Qt.colorEqual(background.color, initialColor), true,
 
257
                        "Reverting the background color failed.");
 
258
            }
 
259
 
 
260
            function test_foreground_color() {
 
261
                var color1 = color_by_value(style.foregroundColor);
 
262
                var bar = header.trailingActionBar;
 
263
                var iconButton = findChild(bar, "action1_button");
 
264
                var buttonStyle = iconButton.__styleInstance;
 
265
                compare(Qt.colorEqual(buttonStyle.foregroundColor, color1), true,
 
266
                        "Button foreground color does not match header foreground color.");
 
267
 
 
268
                var label = findChild(header, "header_title_label");
 
269
                compare(Qt.colorEqual(label.color, color1), true,
 
270
                        "Title color does not match header foreground color.");
 
271
 
 
272
                var color2 = "#FF1ABC"; // a random color.
 
273
                style.foregroundColor = color2;
 
274
                compare(Qt.colorEqual(buttonStyle.foregroundColor, color2), true,
 
275
                        "Button foreground color does not match updated header foreground color.");
 
276
                compare(Qt.colorEqual(label.color, color2), true,
 
277
                        "Title color does not match updated header foreground color.");
 
278
 
 
279
                style.foregroundColor = color1;
 
280
 
 
281
                // revert to the original color.
 
282
                compare(Qt.colorEqual(buttonStyle.foregroundColor, color1), true,
 
283
                        "Button foreground color does not match reverted header foreground color.");
 
284
                compare(Qt.colorEqual(label.color, color1), true,
 
285
                        "Title color does not match reverted header foreground color.");
 
286
            }
 
287
 
 
288
            function test_divider_color() {
 
289
                var color1 = color_by_value(style.dividerColor);
 
290
                var divider = findChild(style, "header_divider");
 
291
                compare(Qt.colorEqual(divider.color, color1), true,
 
292
                        "Incorrect divider color.");
 
293
 
 
294
                var color2 = "#ACDC12"; // a random color.
 
295
                style.dividerColor = color2;
 
296
                compare(Qt.colorEqual(divider.color, color2), true,
 
297
                        "Incorrect updated divider color.");
 
298
 
 
299
                style.dividerColor = color1;
 
300
                compare(Qt.colorEqual(divider.color, color1), true,
 
301
                        "Incorrect reverted divider color.");
 
302
            }
 
303
 
 
304
            function test_title() {
 
305
                compare(defaultHeader.title, "", "Header has a title by default.");
 
306
                var oldTitle = header.title;
 
307
                var titleLabel = findChild(header, "header_title_label");
 
308
                compare(titleLabel.text, oldTitle, "Incorrect title text.");
 
309
 
 
310
                var newTitle = "Updated title text";
 
311
                header.title = newTitle;
 
312
                compare(titleLabel.text, newTitle, "Incorrect updated title text.");
 
313
 
 
314
                header.title = oldTitle;
 
315
                compare(titleLabel.text, oldTitle, "Incorrect reverted title text.");
 
316
            }
 
317
 
 
318
            function test_contents() {
 
319
                compare(defaultHeader.contents, null, "Default header contents is not null.");
 
320
                compare(header.contents, null, "Header has contents initially.");
 
321
 
 
322
                var titleLabel = findChild(header, "header_title_label");
 
323
                compare(titleLabel !== null, true, "No title component loaded.");
 
324
                compare(titleLabel.visible, true, "Title is not visible.");
 
325
 
 
326
                var altParent = alternativeContents.parent;
 
327
                header.contents = alternativeContents;
 
328
                titleLabel = findChild(header, "header_title_label");
 
329
                compare(titleLabel, null, "Setting contents does not unload title.");
 
330
                var headerContents = findChild(header, "alternative_contents");
 
331
                compare(headerContents, alternativeContents,
 
332
                        "New contents was not re-parented to the header.");
 
333
                compare(altParent !== headerContents.parent, true,
 
334
                        "Contents parent was not changed.");
 
335
 
 
336
                header.contents = null;
 
337
                titleLabel = findChild(header, "header_title_label");
 
338
                compare(titleLabel !== null, true, "No title component loaded after unsetting contents.");
 
339
                compare(titleLabel.visible, true, "Title label invisible after unsetting contents.");
 
340
                headerContents = findChild(header, "alternative_contents");
 
341
                compare(headerContents, null,
 
342
                        "Previous header contents is not removed as a child of header.");
 
343
                compare(alternativeContents.parent, altParent,
 
344
                        "Contents parent was not reverted.");
 
345
            }
 
346
 
 
347
            function test_navigationActions() {
 
348
                header.navigationActions = [];
 
349
                compare(header.leadingActionBar.actions, header.navigationActions,
 
350
                        "Leading action bar actions does not equal navigationActions initially.");
 
351
                header.navigationActions = root.actionList;
 
352
                compare(header.leadingActionBar.actions, header.navigationActions,
 
353
                        "Updating navigationActions does not update leading actions.");
 
354
                header.navigationActions = [];
 
355
                compare(header.leadingActionBar.actions, header.navigationActions,
 
356
                        "Reverting navigationActions does not revert leading actions.");
 
357
                header.leadingActionBar.actions = root.actionList;
 
358
                compare(header.navigationActions.length, 0,
 
359
                        "Setting leading actions changes navigationActions.");
 
360
                header.leadingActionBar.actions = [];
 
361
                compare(header.navigationActions.length, 0,
 
362
                        "Reverting leading actions changes navigationActions.");
 
363
            }
 
364
 
 
365
            // The properties of header.sections, header.leadingActionBar and
 
366
            //  header.trailingActionBar are tested in tst_sections.qml and tst_actionbar.qml.
 
367
        }
 
368
    }
 
369
}