2
* Copyright (C) 2015 Canonical Ltd.
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.
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.
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/>.
18
import Ubuntu.Components 1.3
19
import Ubuntu.Test 1.0
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.
33
property real initialHeaderHeight: units.gu(6)
35
property list<Action> sectionActions: [
36
Action { text: "first" },
37
Action { text: "second" },
38
Action { text: "third" }
42
id: alternativeContents
43
visible: header.contents === alternativeContents
44
objectName: "alternative_contents"
47
color: UbuntuColors.red
50
anchors.centerIn: parent
51
text: "Custom header contents"
56
property list<Action> actionList: [
60
onTriggered: print("Trigger first action")
66
onTriggered: print("Trigger second action")
71
onTriggered: print("Trigger third action")
74
iconName: "appointment"
76
onTriggered: print("Trigger fourth action")
85
title: "Default title"
86
contents: contentsSwitch.checked ? alternativeContents : null
87
sections.actions: sectionsSwitch.checked ? root.sectionActions : []
88
trailingActionBar.actions: trailingActionsSwitch.checked ?
90
navigationActions: leadingActionsSwitch.checked ?
97
top: header.flickable ? parent.top : header.bottom
100
bottom: parent.bottom
102
contentHeight: root.height * 2
111
leftMargin: units.gu(5)
112
topMargin: root.initialHeaderHeight
117
checked: null === header.flickable
119
if (header.flickable) {
120
header.flickable = null;
122
header.flickable = flickable;
127
text: "header locked"
132
checked: header.exposed
134
header.exposed = !header.exposed;
138
text: "header exposed"
142
id: leadingActionsSwitch
146
text: "leading actions"
150
id: trailingActionsSwitch
154
text: "trailing actions"
162
text: "replace title by contents"
170
text: "show sections"
186
function initTestCase() {
187
style = header.__styleInstance;
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);
197
function scroll_down() {
198
scroll(-2.0*header.height);
201
function scroll_up() {
202
scroll(header.height);
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?");
210
compare(header.y, 0, errorMessage +
211
" y-value/exposed mismatch for exposed header!");
213
compare(header.y, -header.height, errorMessage +
214
" y-value/exposed mismatch for hidden header!");
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);
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;
229
var sections = header.sections;
230
compare(header.sections.height, 0,
231
"Empty sections has non-0 height.");
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.");
239
sections.actions = [];
240
compare(header.height, initialHeight,
241
"Unsetting sections does not revert the header height.");
244
function test_background_color() {
245
var background = findChild(style, "header_background");
246
compare(background.color, style.backgroundColor,
247
"Incorrect initial background color.");
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.");
255
style.backgroundColor = initialColor;
256
compare(Qt.colorEqual(background.color, initialColor), true,
257
"Reverting the background color failed.");
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.");
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.");
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.");
279
style.foregroundColor = color1;
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.");
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.");
294
var color2 = "#ACDC12"; // a random color.
295
style.dividerColor = color2;
296
compare(Qt.colorEqual(divider.color, color2), true,
297
"Incorrect updated divider color.");
299
style.dividerColor = color1;
300
compare(Qt.colorEqual(divider.color, color1), true,
301
"Incorrect reverted divider color.");
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.");
310
var newTitle = "Updated title text";
311
header.title = newTitle;
312
compare(titleLabel.text, newTitle, "Incorrect updated title text.");
314
header.title = oldTitle;
315
compare(titleLabel.text, oldTitle, "Incorrect reverted title text.");
318
function test_contents() {
319
compare(defaultHeader.contents, null, "Default header contents is not null.");
320
compare(header.contents, null, "Header has contents initially.");
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.");
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.");
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.");
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.");
365
// The properties of header.sections, header.leadingActionBar and
366
// header.trailingActionBar are tested in tst_sections.qml and tst_actionbar.qml.