1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/manifest/manifest_icon_selector.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gfx/screen.h"
13 #include "ui/gfx/screen_type_delegate.h"
14 #include "ui/gfx/test/test_screen.h"
18 const int kPreferredIconSize
= 48;
22 class ManifestIconSelectorTest
: public testing::Test
{
24 ManifestIconSelectorTest() {
25 test_screen_
.display()->set_id(0x1337);
26 test_screen_
.display()->set_bounds(gfx::Rect(0, 0, 2560, 1440));
29 ~ManifestIconSelectorTest() override
{}
31 GURL
FindBestMatchingIcon(const std::vector
<content::Manifest::Icon
>& icons
) {
32 return ManifestIconSelector::FindBestMatchingIcon(
33 icons
, GetPreferredIconSizeInDp(), &test_screen_
);
36 void SetDisplayDeviceScaleFactor(float device_scale_factor
) {
37 test_screen_
.display()->set_device_scale_factor(device_scale_factor
);
40 static int GetPreferredIconSizeInDp() {
41 return kPreferredIconSize
;
44 static content::Manifest::Icon
CreateIcon(
45 const std::string
& url
,
46 const std::string
& type
,
48 const std::vector
<gfx::Size
> sizes
) {
49 content::Manifest::Icon icon
;
52 icon
.type
= base::NullableString16(base::UTF8ToUTF16(type
), false);
53 icon
.density
= density
;
60 gfx::test::TestScreen test_screen_
;
62 DISALLOW_COPY_AND_ASSIGN(ManifestIconSelectorTest
);
65 TEST_F(ManifestIconSelectorTest
, NoIcons
) {
66 // No icons should return the empty URL.
67 std::vector
<content::Manifest::Icon
> icons
;
68 GURL url
= FindBestMatchingIcon(icons
);
69 EXPECT_TRUE(url
.is_empty());
72 TEST_F(ManifestIconSelectorTest
, NoSizes
) {
73 // Icon with no sizes are ignored.
74 std::vector
<content::Manifest::Icon
> icons
;
76 CreateIcon("http://foo.com/icon.png", "", 1.0, std::vector
<gfx::Size
>()));
78 GURL url
= FindBestMatchingIcon(icons
);
79 EXPECT_TRUE(url
.is_empty());
82 TEST_F(ManifestIconSelectorTest
, MIMETypeFiltering
) {
83 // Icons with type specified to a MIME type that isn't a valid image MIME type
85 std::vector
<gfx::Size
> sizes
;
86 sizes
.push_back(gfx::Size(10, 10));
88 std::vector
<content::Manifest::Icon
> icons
;
90 CreateIcon("http://foo.com/icon.png", "image/foo_bar", 1.0, sizes
));
91 icons
.push_back(CreateIcon("http://foo.com/icon.png", "image/", 1.0, sizes
));
92 icons
.push_back(CreateIcon("http://foo.com/icon.png", "image/", 1.0, sizes
));
94 CreateIcon("http://foo.com/icon.png", "video/mp4", 1.0, sizes
));
96 GURL url
= FindBestMatchingIcon(icons
);
97 EXPECT_TRUE(url
.is_empty());
101 CreateIcon("http://foo.com/icon.png", "image/png", 1.0, sizes
));
102 url
= FindBestMatchingIcon(icons
);
103 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
107 CreateIcon("http://foo.com/icon.png", "image/gif", 1.0, sizes
));
108 url
= FindBestMatchingIcon(icons
);
109 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
113 CreateIcon("http://foo.com/icon.png", "image/jpeg", 1.0, sizes
));
114 url
= FindBestMatchingIcon(icons
);
115 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
118 TEST_F(ManifestIconSelectorTest
, PreferredSizeOfCurrentDensityIsUsedFirst
) {
119 // This test has three icons each are marked with sizes set to the preferred
120 // icon size for the associated density.
121 std::vector
<gfx::Size
> sizes_1
;
122 sizes_1
.push_back(gfx::Size(GetPreferredIconSizeInDp(),
123 GetPreferredIconSizeInDp()));
125 std::vector
<gfx::Size
> sizes_2
;
126 sizes_2
.push_back(gfx::Size(GetPreferredIconSizeInDp() * 2,
127 GetPreferredIconSizeInDp() * 2));
129 std::vector
<gfx::Size
> sizes_3
;
130 sizes_3
.push_back(gfx::Size(GetPreferredIconSizeInDp() * 3,
131 GetPreferredIconSizeInDp() * 3));
133 std::vector
<content::Manifest::Icon
> icons
;
134 icons
.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1
));
135 icons
.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes_2
));
136 icons
.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3
));
138 SetDisplayDeviceScaleFactor(1.0f
);
139 GURL url
= FindBestMatchingIcon(icons
);
140 EXPECT_EQ("http://foo.com/icon_x1.png", url
.spec());
142 SetDisplayDeviceScaleFactor(2.0f
);
143 url
= FindBestMatchingIcon(icons
);
144 EXPECT_EQ("http://foo.com/icon_x2.png", url
.spec());
146 SetDisplayDeviceScaleFactor(3.0f
);
147 url
= FindBestMatchingIcon(icons
);
148 EXPECT_EQ("http://foo.com/icon_x3.png", url
.spec());
151 TEST_F(ManifestIconSelectorTest
, PreferredSizeOfDefaultDensityIsUsedSecond
) {
152 // This test has three icons. The first one is of density zero and is marked
153 // with three sizes which are the preferred icon size for density 1, 2 and 3.
154 // The icon for density 2 and 3 have a size set to 2x2 and 3x3.
155 // Regardless of the device scale factor, the icon of density 1 is going to be
156 // used because it matches the preferred size.
157 std::vector
<gfx::Size
> sizes_1
;
158 sizes_1
.push_back(gfx::Size(GetPreferredIconSizeInDp(),
159 GetPreferredIconSizeInDp()));
160 sizes_1
.push_back(gfx::Size(GetPreferredIconSizeInDp() * 2,
161 GetPreferredIconSizeInDp() * 2));
162 sizes_1
.push_back(gfx::Size(GetPreferredIconSizeInDp() * 3,
163 GetPreferredIconSizeInDp() * 3));
165 std::vector
<gfx::Size
> sizes_2
;
166 sizes_2
.push_back(gfx::Size(2, 2));
168 std::vector
<gfx::Size
> sizes_3
;
169 sizes_3
.push_back(gfx::Size(3, 3));
171 std::vector
<content::Manifest::Icon
> icons
;
172 icons
.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1
));
173 icons
.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes_2
));
174 icons
.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3
));
176 SetDisplayDeviceScaleFactor(1.0f
);
177 GURL url
= FindBestMatchingIcon(icons
);
178 EXPECT_EQ("http://foo.com/icon_x1.png", url
.spec());
180 SetDisplayDeviceScaleFactor(2.0f
);
181 url
= FindBestMatchingIcon(icons
);
182 EXPECT_EQ("http://foo.com/icon_x1.png", url
.spec());
184 SetDisplayDeviceScaleFactor(3.0f
);
185 url
= FindBestMatchingIcon(icons
);
186 EXPECT_EQ("http://foo.com/icon_x1.png", url
.spec());
189 TEST_F(ManifestIconSelectorTest
, DeviceDensityFirst
) {
190 // If there is no perfect icon but an icon of the current device density is
191 // present, it will be picked.
192 // This test has three icons each are marked with sizes set to the preferred
193 // icon size for the associated density.
194 std::vector
<gfx::Size
> sizes
;
195 sizes
.push_back(gfx::Size(2, 2));
197 std::vector
<content::Manifest::Icon
> icons
;
198 icons
.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes
));
199 icons
.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes
));
200 icons
.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes
));
202 SetDisplayDeviceScaleFactor(1.0f
);
203 GURL url
= FindBestMatchingIcon(icons
);
204 EXPECT_EQ("http://foo.com/icon_x1.png", url
.spec());
206 SetDisplayDeviceScaleFactor(2.0f
);
207 url
= FindBestMatchingIcon(icons
);
208 EXPECT_EQ("http://foo.com/icon_x2.png", url
.spec());
210 SetDisplayDeviceScaleFactor(3.0f
);
211 url
= FindBestMatchingIcon(icons
);
212 EXPECT_EQ("http://foo.com/icon_x3.png", url
.spec());
215 TEST_F(ManifestIconSelectorTest
, DeviceDensityFallback
) {
216 // If there is no perfect icon but and no icon of the current display density,
217 // an icon of density 1.0 will be used.
218 std::vector
<gfx::Size
> sizes
;
219 sizes
.push_back(gfx::Size(2, 2));
221 std::vector
<content::Manifest::Icon
> icons
;
222 icons
.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes
));
223 icons
.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes
));
225 SetDisplayDeviceScaleFactor(3.0f
);
226 GURL url
= FindBestMatchingIcon(icons
);
227 EXPECT_EQ("http://foo.com/icon_x1.png", url
.spec());
230 TEST_F(ManifestIconSelectorTest
, DoNotUseOtherDensities
) {
231 // If there are only icons of densities that are not the current display
232 // density or the default density, they are ignored.
233 std::vector
<gfx::Size
> sizes
;
234 sizes
.push_back(gfx::Size(2, 2));
236 std::vector
<content::Manifest::Icon
> icons
;
237 icons
.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes
));
239 SetDisplayDeviceScaleFactor(3.0f
);
240 GURL url
= FindBestMatchingIcon(icons
);
241 EXPECT_TRUE(url
.is_empty());
244 TEST_F(ManifestIconSelectorTest
, NotSquareIconsAreIgnored
) {
245 std::vector
<gfx::Size
> sizes
;
246 sizes
.push_back(gfx::Size(20, 2));
248 std::vector
<content::Manifest::Icon
> icons
;
249 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes
));
251 GURL url
= FindBestMatchingIcon(icons
);
252 EXPECT_TRUE(url
.is_empty());
255 TEST_F(ManifestIconSelectorTest
, ClosestIconToPreferred
) {
256 // This test verifies ManifestIconSelector::FindBestMatchingIcon by passing
257 // different icon sizes and checking which one is picked.
258 // The Device Scale Factor is 1.0 and the preferred icon size is returned by
259 // GetPreferredIconSizeInDp().
260 int very_small
= GetPreferredIconSizeInDp() / 4;
261 int small_size
= GetPreferredIconSizeInDp() / 2;
262 int bit_small
= GetPreferredIconSizeInDp() - 1;
263 int bit_big
= GetPreferredIconSizeInDp() + 1;
264 int big
= GetPreferredIconSizeInDp() * 2;
265 int very_big
= GetPreferredIconSizeInDp() * 4;
267 // (very_small, bit_small) => bit_small
269 std::vector
<gfx::Size
> sizes_1
;
270 sizes_1
.push_back(gfx::Size(very_small
, very_small
));
272 std::vector
<gfx::Size
> sizes_2
;
273 sizes_2
.push_back(gfx::Size(bit_small
, bit_small
));
275 std::vector
<content::Manifest::Icon
> icons
;
276 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1
));
277 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2
));
279 GURL url
= FindBestMatchingIcon(icons
);
280 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
283 // (very_small, bit_small, smaller) => bit_small
285 std::vector
<gfx::Size
> sizes_1
;
286 sizes_1
.push_back(gfx::Size(very_small
, very_small
));
288 std::vector
<gfx::Size
> sizes_2
;
289 sizes_2
.push_back(gfx::Size(bit_small
, bit_small
));
291 std::vector
<gfx::Size
> sizes_3
;
292 sizes_3
.push_back(gfx::Size(small_size
, small_size
));
294 std::vector
<content::Manifest::Icon
> icons
;
295 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1
));
296 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2
));
297 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_3
));
299 GURL url
= FindBestMatchingIcon(icons
);
300 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
303 // (very_big, big) => big
305 std::vector
<gfx::Size
> sizes_1
;
306 sizes_1
.push_back(gfx::Size(very_big
, very_big
));
308 std::vector
<gfx::Size
> sizes_2
;
309 sizes_2
.push_back(gfx::Size(big
, big
));
311 std::vector
<content::Manifest::Icon
> icons
;
312 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1
));
313 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2
));
315 GURL url
= FindBestMatchingIcon(icons
);
316 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
319 // (very_big, big, bit_big) => bit_big
321 std::vector
<gfx::Size
> sizes_1
;
322 sizes_1
.push_back(gfx::Size(very_big
, very_big
));
324 std::vector
<gfx::Size
> sizes_2
;
325 sizes_2
.push_back(gfx::Size(big
, big
));
327 std::vector
<gfx::Size
> sizes_3
;
328 sizes_3
.push_back(gfx::Size(bit_big
, bit_big
));
330 std::vector
<content::Manifest::Icon
> icons
;
331 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1
));
332 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2
));
333 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_3
));
335 GURL url
= FindBestMatchingIcon(icons
);
336 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
339 // (bit_small, very_big) => very_big
341 std::vector
<gfx::Size
> sizes_1
;
342 sizes_1
.push_back(gfx::Size(bit_small
, bit_small
));
344 std::vector
<gfx::Size
> sizes_2
;
345 sizes_2
.push_back(gfx::Size(very_big
, very_big
));
347 std::vector
<content::Manifest::Icon
> icons
;
348 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1
));
349 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2
));
351 GURL url
= FindBestMatchingIcon(icons
);
352 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
355 // (bit_small, bit_big) => bit_big
357 std::vector
<gfx::Size
> sizes_1
;
358 sizes_1
.push_back(gfx::Size(bit_small
, bit_small
));
360 std::vector
<gfx::Size
> sizes_2
;
361 sizes_2
.push_back(gfx::Size(bit_big
, bit_big
));
363 std::vector
<content::Manifest::Icon
> icons
;
364 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1
));
365 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2
));
367 GURL url
= FindBestMatchingIcon(icons
);
368 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
372 TEST_F(ManifestIconSelectorTest
, UseAnyIfNoPreferredSize
) {
373 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a
374 // preferred size. An icon with the current device scale factor is preferred
375 // over one with the default density.
377 // 'any' with preferred size => preferred size
379 std::vector
<gfx::Size
> sizes_1
;
380 sizes_1
.push_back(gfx::Size(GetPreferredIconSizeInDp(),
381 GetPreferredIconSizeInDp()));
382 std::vector
<gfx::Size
> sizes_2
;
383 sizes_2
.push_back(gfx::Size(0, 0));
385 std::vector
<content::Manifest::Icon
> icons
;
386 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_1
));
387 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2
));
389 GURL url
= FindBestMatchingIcon(icons
);
390 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
393 // 'any' with nearly preferred size => any
395 std::vector
<gfx::Size
> sizes_1
;
396 sizes_1
.push_back(gfx::Size(GetPreferredIconSizeInDp() + 1,
397 GetPreferredIconSizeInDp() + 1));
398 std::vector
<gfx::Size
> sizes_2
;
399 sizes_2
.push_back(gfx::Size(0, 0));
401 std::vector
<content::Manifest::Icon
> icons
;
402 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1
));
403 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2
));
405 GURL url
= FindBestMatchingIcon(icons
);
406 EXPECT_EQ("http://foo.com/icon.png", url
.spec());
409 // 'any' on default density and current density => current density.
411 std::vector
<gfx::Size
> sizes
;
412 sizes
.push_back(gfx::Size(0, 0));
414 std::vector
<content::Manifest::Icon
> icons
;
415 icons
.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes
));
416 icons
.push_back(CreateIcon("http://foo.com/icon.png", "", 3.0, sizes
));
418 SetDisplayDeviceScaleFactor(3.0f
);
419 GURL url
= FindBestMatchingIcon(icons
);
420 EXPECT_EQ("http://foo.com/icon.png", url
.spec());