@@ -1,5 +1,6 @@
11/*
22 * Copyright (C) 2015 Canonical, Ltd.
3+ * Copyright (C) 2021 UBports Foundation
34 *
45 * This program is free software; you can redistribute it and/or modify
56 * it under the terms of the GNU General Public License as published by
@@ -16,20 +17,34 @@
1617
1718import QtQuick 2.4
1819import QtTest 1.0
19−import Ubuntu.Components 1.3
2020import Unity.Test 0.1
2121
2222import "../../../qml/Components"
2323
2424
2525Image {
26− width: units.gu(70)
27− height: units.gu(70)
26+ id: root
27+ width: 70
28+ height: 70
2829
2930 source: wallpaperResolver.background
3031
32+ readonly property url blue: Qt.resolvedUrl("../../data/unity/backgrounds/blue.png")
33+ readonly property url red: Qt.resolvedUrl("../../data/unity/backgrounds/red.png")
34+ readonly property url big: Qt.resolvedUrl("../../graphics/applicationIcons/dash.png")
35+
3136 WallpaperResolver {
3237 id: wallpaperResolver
38+
39+ property var pastBackgrounds: []
40+
41+ onBackgroundChanged: pastBackgrounds.push(background)
42+ }
43+
44+ SignalSpy {
45+ id: spy
46+ signalName: "backgroundChanged"
47+ target: wallpaperResolver
3348 }
3449
3550 UnityTestCase {
@@ -39,37 +54,108 @@ Image {
3954
4055 function test_background_data() {
4156 return [
57+ {tag: "empty-candidates",
58+ list: [],
59+ output: ""},
60+
61+ {tag: "blank-candidate",
62+ list: [""],
63+ output: ""},
64+
65+ {tag: "blank-urls",
66+ list: [Qt.resolvedUrl(""), Qt.resolvedUrl(""), root.blue],
67+ output: root.blue},
68+
69+ {tag: "invalid-urls",
70+ list: [Qt.resolvedUrl("/first"), Qt.resolvedUrl("/middle"), root.blue],
71+ output: root.blue},
72+
73+ {tag: "valid-after-blanks",
74+ list: ["", "", root.red],
75+ output: root.red},
76+
77+ // Ensure that the WallpaperResolver doesn't get stuck if it
78+ // sees the same invalid wallpaper multiple times in a row
79+ {tag: "valid-after-the-same-invalid",
80+ list: ["/first", "/first", "/first", root.red],
81+ output: root.red},
82+
83+ {tag: "naughty",
84+ list: [null, undefined, "", NaN, 1.0, 1, root.red, null],
85+ output: root.red},
86+
4287 {tag: "none-valid",
4388 list: ["/first", "/middle", "/last"],
44− output: "file:///last"},
89+ output: Qt.resolvedUrl("/last")},
4590
4691 {tag: "first-valid",
47− list: [Qt.resolvedUrl("../../data/unity/backgrounds/blue.png"),
48− "/middle", "/last"],
49− output: Qt.resolvedUrl("../../data/unity/backgrounds/blue.png")},
92+ list: [root.blue, "/middle", "/last"],
93+ output: root.blue},
5094
5195 {tag: "middle-valid",
52− list: ["/first",
53− Qt.resolvedUrl("../../data/unity/backgrounds/red.png"),
54− "/last"],
55− output: Qt.resolvedUrl("../../data/unity/backgrounds/red.png")},
96+ list: ["/first", root.red, "/last"],
97+ output: root.red},
5698
5799 {tag: "last-valid",
58− list: ["/first",
59− "/middle",
60− Qt.resolvedUrl("../../data/unity/backgrounds/red.png")],
61− output: Qt.resolvedUrl("../../data/unity/backgrounds/red.png")},
100+ list: ["/first", "/middle", root.red],
101+ output: root.red},
62102
63103 {tag: "multiple-valid",
64− list: [Qt.resolvedUrl("../../data/unity/backgrounds/blue.png"),
65− Qt.resolvedUrl("../../data/unity/backgrounds/red.png")],
66− output: Qt.resolvedUrl("../../data/unity/backgrounds/blue.png")},
104+ list: [root.blue, root.red],
105+ output: root.blue},
106+
107+ {tag: "multiple-valid-after-multiple-invalid",
108+ list: ["/first", "/middle", "/last", root.blue, root.red],
109+ output: root.blue},
110+
67111 ]
68112 }
69113
114+ function init() {
115+ // Make sure we don't have our next test compare() to the results
116+ // of the last test by exercising the resolver
117+ wallpaperResolver.cache = true;
118+ wallpaperResolver.candidates = [];
119+ tryCompare(wallpaperResolver, "background", "");
120+ wallpaperResolver.pastBackgrounds = [];
121+ wallpaperResolver.candidates = [root.blue, root.blue];
122+ tryCompare(wallpaperResolver, "background", root.blue);
123+ wallpaperResolver.candidates = [];
124+ tryCompare(wallpaperResolver, "background", "");
125+ tryCompare(wallpaperResolver, "pastBackgrounds", [root.blue, Qt.resolvedUrl("")])
126+ wallpaperResolver.pastBackgrounds = [];
127+ }
128+
70129 function test_background(data) {
71130 wallpaperResolver.candidates = data.list;
72131 tryCompare(wallpaperResolver, "background", data.output);
73132 }
133+
134+ function test_reload_with_blanks() {
135+ wallpaperResolver.candidates = ["", "", root.red];
136+ tryCompare(wallpaperResolver, "background", root.red);
137+ wallpaperResolver.candidates = ["", "", root.blue];
138+ tryCompare(wallpaperResolver, "background", root.blue);
139+ }
140+
141+ // WallpaperResolver loads images asynchronously. It's important to make
142+ // sure that it never returns the wrong image just because they loaded
143+ // in the wrong order. So we set its images 100 times.
144+ function test_images_changing() {
145+ wallpaperResolver.cache = false;
146+ for (var i = 0; i < 50; i++) {
147+ // We don't spy on the first one because the wallpaper doesn't
148+ // transition from a set url to "" the first time.
149+ wallpaperResolver.candidates = [root.red, root.big, root.blue];
150+ tryCompare(wallpaperResolver, "background", root.red);
151+
152+ spy.clear();
153+ wallpaperResolver.pastBackgrounds = [];
154+ wallpaperResolver.candidates = [root.big, root.blue, root.red];
155+ tryCompare(wallpaperResolver, "background", root.big);
156+ compare(wallpaperResolver.pastBackgrounds, ["", root.big])
157+ compare(spy.count, 4);
158+ }
159+ }
74160 }
75161}