@@ -1,5 +1,6 @@
2 * Copyright (C) 2015 Canonical, Ltd.
2 * Copyright (C) 2015 Canonical, Ltd.
3+ * Copyright (C) 2021 UBports Foundation
4 * This program is free software; you can redistribute it and/or modify
5 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * it under the terms of the GNU General Public License as published by
@@ -16,20 +17,34 @@
17import QtQuick 2.4
18import QtQuick 2.4
18import QtTest 1.0
19import QtTest 1.0
19−import Ubuntu.Components 1.3
20import Unity.Test 0.1
20import Unity.Test 0.1
22import "../../../qml/Components"
22import "../../../qml/Components"
26− width: units.gu(70)
27+ width: 70
27− height: units.gu(70)
28+ height: 70
29 source: wallpaperResolver.background
30 source: wallpaperResolver.background
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")
31 WallpaperResolver {
36 WallpaperResolver {
32 id: wallpaperResolver
37 id: wallpaperResolver
39+ property var pastBackgrounds: []
41+ onBackgroundChanged: pastBackgrounds.push(background)
46+ signalName: "backgroundChanged"
47+ target: wallpaperResolver
35 UnityTestCase {
50 UnityTestCase {
@@ -39,37 +54,108 @@ Image {
40 function test_background_data() {
55 function test_background_data() {
57+ {tag: "empty-candidates",
61+ {tag: "blank-candidate",
66+ list: [Qt.resolvedUrl(""), Qt.resolvedUrl(""), root.blue],
69+ {tag: "invalid-urls",
70+ list: [Qt.resolvedUrl("/first"), Qt.resolvedUrl("/middle"), root.blue],
73+ {tag: "valid-after-blanks",
74+ list: ["", "", root.red],
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],
84+ list: [null, undefined, "", NaN, 1.0, 1, root.red, null],
42 {tag: "none-valid",
87 {tag: "none-valid",
43 list: ["/first", "/middle", "/last"],
88 list: ["/first", "/middle", "/last"],
44− output: "file:///last"},
89+ output: Qt.resolvedUrl("/last")},
46 {tag: "first-valid",
91 {tag: "first-valid",
47− list: [Qt.resolvedUrl("../../data/unity/backgrounds/blue.png"),
48− "/middle", "/last"],
92+ list: [root.blue, "/middle", "/last"],
49− output: Qt.resolvedUrl("../../data/unity/backgrounds/blue.png")},
93+ output: root.blue},
51 {tag: "middle-valid",
95 {tag: "middle-valid",
52− list: ["/first",
96+ list: ["/first", root.red, "/last"],
53− Qt.resolvedUrl("../../data/unity/backgrounds/red.png"),
97+ output: root.red},
55− output: Qt.resolvedUrl("../../data/unity/backgrounds/red.png")},
57 {tag: "last-valid",
99 {tag: "last-valid",
58− list: ["/first",
100+ list: ["/first", "/middle", root.red],
59− "/middle",
101+ output: root.red},
60− Qt.resolvedUrl("../../data/unity/backgrounds/red.png")],
61− output: Qt.resolvedUrl("../../data/unity/backgrounds/red.png")},
63 {tag: "multiple-valid",
103 {tag: "multiple-valid",
64− list: [Qt.resolvedUrl("../../data/unity/backgrounds/blue.png"),
104+ list: [root.blue, root.red],
65− Qt.resolvedUrl("../../data/unity/backgrounds/red.png")],
105+ output: root.blue},
66− output: Qt.resolvedUrl("../../data/unity/backgrounds/blue.png")},
106+
107+ {tag: "multiple-valid-after-multiple-invalid",
108+ list: ["/first", "/middle", "/last", root.blue, root.red],
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 = [];
70 function test_background(data) {
129 function test_background(data) {
71 wallpaperResolver.candidates = data.list;
130 wallpaperResolver.candidates = data.list;
72 tryCompare(wallpaperResolver, "background", data.output);
131 tryCompare(wallpaperResolver, "background", data.output);
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);
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);
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);