Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / manifest / manifest_icon_selector_unittest.cc
bloba6441c9c407a4fabab081cd1edc11f4c87e7187f
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"
7 #include <string>
8 #include <vector>
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"
16 class ManifestIconSelectorTest : public testing::Test {
17 protected:
18 ManifestIconSelectorTest() {
19 test_screen_.display()->set_id(0x1337);
20 test_screen_.display()->set_bounds(gfx::Rect(0, 0, 2560, 1440));
23 ~ManifestIconSelectorTest() override {}
25 GURL FindBestMatchingIconWithMinimum(
26 const std::vector<content::Manifest::Icon>& icons,
27 int minimum_icon_size_in_dp) {
28 return ManifestIconSelector::FindBestMatchingIcon(
29 icons, GetPreferredIconSizeInDp(),
30 minimum_icon_size_in_dp, &test_screen_);
33 GURL FindBestMatchingIcon(const std::vector<content::Manifest::Icon>& icons) {
34 return FindBestMatchingIconWithMinimum(icons, 0);
37 void SetDisplayDeviceScaleFactor(float device_scale_factor) {
38 test_screen_.display()->set_device_scale_factor(device_scale_factor);
41 int GetPreferredIconSizeInDp() {
42 return preferred_icon_size_;
45 void SetPreferredIconSizeInDp(int new_size) {
46 preferred_icon_size_ = new_size;
49 static content::Manifest::Icon CreateIcon(
50 const std::string& url,
51 const std::string& type,
52 double density,
53 const std::vector<gfx::Size> sizes) {
54 content::Manifest::Icon icon;
55 icon.src = GURL(url);
56 if (!type.empty())
57 icon.type = base::NullableString16(base::UTF8ToUTF16(type), false);
58 icon.density = density;
59 icon.sizes = sizes;
61 return icon;
64 private:
65 gfx::test::TestScreen test_screen_;
66 int preferred_icon_size_ = 48;
68 DISALLOW_COPY_AND_ASSIGN(ManifestIconSelectorTest);
71 TEST_F(ManifestIconSelectorTest, NoIcons) {
72 // No icons should return the empty URL.
73 std::vector<content::Manifest::Icon> icons;
74 GURL url = FindBestMatchingIcon(icons);
75 EXPECT_TRUE(url.is_empty());
78 TEST_F(ManifestIconSelectorTest, NoSizes) {
79 // Icon with no sizes are ignored.
80 std::vector<content::Manifest::Icon> icons;
81 icons.push_back(
82 CreateIcon("http://foo.com/icon.png", "", 1.0, std::vector<gfx::Size>()));
84 GURL url = FindBestMatchingIcon(icons);
85 EXPECT_TRUE(url.is_empty());
88 TEST_F(ManifestIconSelectorTest, MIMETypeFiltering) {
89 // Icons with type specified to a MIME type that isn't a valid image MIME type
90 // are ignored.
91 std::vector<gfx::Size> sizes;
92 sizes.push_back(gfx::Size(1024, 1024));
94 std::vector<content::Manifest::Icon> icons;
95 icons.push_back(
96 CreateIcon("http://foo.com/icon.png", "image/foo_bar", 1.0, sizes));
97 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", 1.0, sizes));
98 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", 1.0, sizes));
99 icons.push_back(
100 CreateIcon("http://foo.com/icon.png", "video/mp4", 1.0, sizes));
102 GURL url = FindBestMatchingIcon(icons);
103 EXPECT_TRUE(url.is_empty());
105 icons.clear();
106 icons.push_back(
107 CreateIcon("http://foo.com/icon.png", "image/png", 1.0, sizes));
108 url = FindBestMatchingIcon(icons);
109 EXPECT_EQ("http://foo.com/icon.png", url.spec());
111 icons.clear();
112 icons.push_back(
113 CreateIcon("http://foo.com/icon.png", "image/gif", 1.0, sizes));
114 url = FindBestMatchingIcon(icons);
115 EXPECT_EQ("http://foo.com/icon.png", url.spec());
117 icons.clear();
118 icons.push_back(
119 CreateIcon("http://foo.com/icon.png", "image/jpeg", 1.0, sizes));
120 url = FindBestMatchingIcon(icons);
121 EXPECT_EQ("http://foo.com/icon.png", url.spec());
124 TEST_F(ManifestIconSelectorTest, PreferredSizeOfCurrentDensityIsUsedFirst) {
125 // This test has three icons each are marked with sizes set to the preferred
126 // icon size for the associated density.
127 std::vector<gfx::Size> sizes_1;
128 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(),
129 GetPreferredIconSizeInDp()));
131 std::vector<gfx::Size> sizes_2;
132 sizes_2.push_back(gfx::Size(GetPreferredIconSizeInDp() * 2,
133 GetPreferredIconSizeInDp() * 2));
135 std::vector<gfx::Size> sizes_3;
136 sizes_3.push_back(gfx::Size(GetPreferredIconSizeInDp() * 3,
137 GetPreferredIconSizeInDp() * 3));
139 std::vector<content::Manifest::Icon> icons;
140 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1));
141 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes_2));
142 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3));
144 SetDisplayDeviceScaleFactor(1.0f);
145 GURL url = FindBestMatchingIcon(icons);
146 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
148 SetDisplayDeviceScaleFactor(2.0f);
149 url = FindBestMatchingIcon(icons);
150 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec());
152 SetDisplayDeviceScaleFactor(3.0f);
153 url = FindBestMatchingIcon(icons);
154 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec());
157 TEST_F(ManifestIconSelectorTest, PreferredSizeOfDefaultDensityIsUsedSecond) {
158 // This test has three icons. The first one is of density zero and is marked
159 // with three sizes which are the preferred icon size for density 1, 2 and 3.
160 // The icon for density 2 and 3 have a size set to 1024x1024.
161 // Regardless of the device scale factor, the icon of density 1 is going to be
162 // used because it matches the preferred size.
163 std::vector<gfx::Size> sizes_1;
164 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(),
165 GetPreferredIconSizeInDp()));
166 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() * 2,
167 GetPreferredIconSizeInDp() * 2));
168 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() * 3,
169 GetPreferredIconSizeInDp() * 3));
171 std::vector<gfx::Size> sizes_2;
172 sizes_2.push_back(gfx::Size(1024, 1024));
174 std::vector<gfx::Size> sizes_3;
175 sizes_3.push_back(gfx::Size(1024, 1024));
177 std::vector<content::Manifest::Icon> icons;
178 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1));
179 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes_2));
180 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3));
182 SetDisplayDeviceScaleFactor(1.0f);
183 GURL url = FindBestMatchingIcon(icons);
184 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
186 SetDisplayDeviceScaleFactor(2.0f);
187 url = FindBestMatchingIcon(icons);
188 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
190 SetDisplayDeviceScaleFactor(3.0f);
191 url = FindBestMatchingIcon(icons);
192 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
195 TEST_F(ManifestIconSelectorTest, DeviceDensityFirst) {
196 // If there is no perfect icon but an icon of the current device density is
197 // present, it will be picked.
198 // This test has three icons each are marked with sizes set to the preferred
199 // icon size for the associated density.
200 std::vector<gfx::Size> sizes;
201 sizes.push_back(gfx::Size(1024, 1024));
203 std::vector<content::Manifest::Icon> icons;
204 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes));
205 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes));
206 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes));
208 SetDisplayDeviceScaleFactor(1.0f);
209 GURL url = FindBestMatchingIcon(icons);
210 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
212 SetDisplayDeviceScaleFactor(2.0f);
213 url = FindBestMatchingIcon(icons);
214 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec());
216 SetDisplayDeviceScaleFactor(3.0f);
217 url = FindBestMatchingIcon(icons);
218 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec());
221 TEST_F(ManifestIconSelectorTest, DeviceDensityFallback) {
222 // If there is no perfect icon but and no icon of the current display density,
223 // an icon of density 1.0 will be used.
224 std::vector<gfx::Size> sizes;
225 sizes.push_back(gfx::Size(1024, 1024));
227 std::vector<content::Manifest::Icon> icons;
228 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes));
229 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes));
231 SetDisplayDeviceScaleFactor(3.0f);
232 GURL url = FindBestMatchingIcon(icons);
233 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
236 TEST_F(ManifestIconSelectorTest, DeviceDensityMatchRejectsTooSmall) {
237 // If we have to resort to density matching to find icons, then an icon of
238 // the correct size has not been found. Make sure that the minimum passed
239 // is enforced.
240 std::vector<gfx::Size> sizes_1_2;
241 std::vector<gfx::Size> sizes_3;
243 sizes_1_2.push_back(gfx::Size(47, 47));
244 sizes_3.push_back(gfx::Size(95, 95));
246 // Set to a value which will not affect the calculations.
247 SetPreferredIconSizeInDp(1024);
249 std::vector<content::Manifest::Icon> icons;
250 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1_2));
251 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes_1_2));
252 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3));
254 // Nothing matches here because the minimum is 48.
255 SetDisplayDeviceScaleFactor(1.0f);
256 GURL url = FindBestMatchingIconWithMinimum(icons, 48);
257 EXPECT_TRUE(url.is_empty());
259 // Nothing matches here because the minimum is 48 again.
260 SetDisplayDeviceScaleFactor(2.0f);
261 url = FindBestMatchingIconWithMinimum(icons, 48);
262 EXPECT_TRUE(url.is_empty());
264 // Nothing matches here as the minimum is 96.
265 SetDisplayDeviceScaleFactor(3.0f);
266 url = FindBestMatchingIconWithMinimum(icons, 96);
267 EXPECT_TRUE(url.is_empty());
270 TEST_F(ManifestIconSelectorTest, IdealVeryCloseToMinimumMatches) {
271 std::vector<gfx::Size> sizes;
272 sizes.push_back(gfx::Size(2, 2));
274 std::vector<content::Manifest::Icon> icons;
275 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes));
277 SetDisplayDeviceScaleFactor(1.0f);
278 GURL url = FindBestMatchingIconWithMinimum(icons, 1);
279 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
282 TEST_F(ManifestIconSelectorTest, SizeVeryCloseToMinimumMatches) {
283 std::vector<gfx::Size> sizes;
284 sizes.push_back(gfx::Size(2, 2));
286 SetPreferredIconSizeInDp(200);
288 std::vector<content::Manifest::Icon> icons;
289 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes));
291 SetDisplayDeviceScaleFactor(1.0f);
292 GURL url = FindBestMatchingIconWithMinimum(icons, 1);
293 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
296 TEST_F(ManifestIconSelectorTest, DoNotUseOtherDensities) {
297 // If there are only icons of densities that are not the current display
298 // density or the default density, they are ignored.
299 std::vector<gfx::Size> sizes;
300 sizes.push_back(gfx::Size(1024, 1024));
302 std::vector<content::Manifest::Icon> icons;
303 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes));
305 SetDisplayDeviceScaleFactor(3.0f);
306 GURL url = FindBestMatchingIcon(icons);
307 EXPECT_TRUE(url.is_empty());
310 TEST_F(ManifestIconSelectorTest, NotSquareIconsAreIgnored) {
311 std::vector<gfx::Size> sizes;
312 sizes.push_back(gfx::Size(1024, 1023));
314 std::vector<content::Manifest::Icon> icons;
315 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes));
317 GURL url = FindBestMatchingIcon(icons);
318 EXPECT_TRUE(url.is_empty());
321 TEST_F(ManifestIconSelectorTest, ClosestIconToPreferred) {
322 // This test verifies ManifestIconSelector::FindBestMatchingIcon by passing
323 // different icon sizes and checking which one is picked.
324 // The Device Scale Factor is 1.0 and the preferred icon size is returned by
325 // GetPreferredIconSizeInDp().
326 int very_small = GetPreferredIconSizeInDp() / 4;
327 int small_size = GetPreferredIconSizeInDp() / 2;
328 int bit_small = GetPreferredIconSizeInDp() - 1;
329 int bit_big = GetPreferredIconSizeInDp() + 1;
330 int big = GetPreferredIconSizeInDp() * 2;
331 int very_big = GetPreferredIconSizeInDp() * 4;
333 // (very_small, bit_small) => bit_small
335 std::vector<gfx::Size> sizes_1;
336 sizes_1.push_back(gfx::Size(very_small, very_small));
338 std::vector<gfx::Size> sizes_2;
339 sizes_2.push_back(gfx::Size(bit_small, bit_small));
341 std::vector<content::Manifest::Icon> icons;
342 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
343 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
345 GURL url = FindBestMatchingIcon(icons);
346 EXPECT_EQ("http://foo.com/icon.png", url.spec());
349 // (very_small, bit_small, smaller) => bit_small
351 std::vector<gfx::Size> sizes_1;
352 sizes_1.push_back(gfx::Size(very_small, very_small));
354 std::vector<gfx::Size> sizes_2;
355 sizes_2.push_back(gfx::Size(bit_small, bit_small));
357 std::vector<gfx::Size> sizes_3;
358 sizes_3.push_back(gfx::Size(small_size, small_size));
360 std::vector<content::Manifest::Icon> icons;
361 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
362 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
363 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_3));
365 GURL url = FindBestMatchingIcon(icons);
366 EXPECT_EQ("http://foo.com/icon.png", url.spec());
369 // (very_big, big) => big
371 std::vector<gfx::Size> sizes_1;
372 sizes_1.push_back(gfx::Size(very_big, very_big));
374 std::vector<gfx::Size> sizes_2;
375 sizes_2.push_back(gfx::Size(big, big));
377 std::vector<content::Manifest::Icon> icons;
378 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
379 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
381 GURL url = FindBestMatchingIcon(icons);
382 EXPECT_EQ("http://foo.com/icon.png", url.spec());
385 // (very_big, big, bit_big) => bit_big
387 std::vector<gfx::Size> sizes_1;
388 sizes_1.push_back(gfx::Size(very_big, very_big));
390 std::vector<gfx::Size> sizes_2;
391 sizes_2.push_back(gfx::Size(big, big));
393 std::vector<gfx::Size> sizes_3;
394 sizes_3.push_back(gfx::Size(bit_big, bit_big));
396 std::vector<content::Manifest::Icon> icons;
397 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
398 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2));
399 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_3));
401 GURL url = FindBestMatchingIcon(icons);
402 EXPECT_EQ("http://foo.com/icon.png", url.spec());
405 // (bit_small, very_big) => very_big
407 std::vector<gfx::Size> sizes_1;
408 sizes_1.push_back(gfx::Size(bit_small, bit_small));
410 std::vector<gfx::Size> sizes_2;
411 sizes_2.push_back(gfx::Size(very_big, very_big));
413 std::vector<content::Manifest::Icon> icons;
414 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
415 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
417 GURL url = FindBestMatchingIcon(icons);
418 EXPECT_EQ("http://foo.com/icon.png", url.spec());
421 // (bit_small, bit_big) => bit_big
423 std::vector<gfx::Size> sizes_1;
424 sizes_1.push_back(gfx::Size(bit_small, bit_small));
426 std::vector<gfx::Size> sizes_2;
427 sizes_2.push_back(gfx::Size(bit_big, bit_big));
429 std::vector<content::Manifest::Icon> icons;
430 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
431 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
433 GURL url = FindBestMatchingIcon(icons);
434 EXPECT_EQ("http://foo.com/icon.png", url.spec());
438 TEST_F(ManifestIconSelectorTest, UseAnyIfNoPreferredSize) {
439 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a
440 // preferred size. An icon with the current device scale factor is preferred
441 // over one with the default density.
443 // 'any' with preferred size => preferred size
445 std::vector<gfx::Size> sizes_1;
446 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(),
447 GetPreferredIconSizeInDp()));
448 std::vector<gfx::Size> sizes_2;
449 sizes_2.push_back(gfx::Size(0, 0));
451 std::vector<content::Manifest::Icon> icons;
452 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_1));
453 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2));
455 GURL url = FindBestMatchingIcon(icons);
456 EXPECT_EQ("http://foo.com/icon.png", url.spec());
459 // 'any' with nearly preferred size => any
461 std::vector<gfx::Size> sizes_1;
462 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() + 1,
463 GetPreferredIconSizeInDp() + 1));
464 std::vector<gfx::Size> sizes_2;
465 sizes_2.push_back(gfx::Size(0, 0));
467 std::vector<content::Manifest::Icon> icons;
468 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
469 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
471 GURL url = FindBestMatchingIcon(icons);
472 EXPECT_EQ("http://foo.com/icon.png", url.spec());
475 // 'any' on default density and current density => current density.
477 std::vector<gfx::Size> sizes;
478 sizes.push_back(gfx::Size(0, 0));
480 std::vector<content::Manifest::Icon> icons;
481 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes));
482 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 3.0, sizes));
484 SetDisplayDeviceScaleFactor(3.0f);
485 GURL url = FindBestMatchingIcon(icons);
486 EXPECT_EQ("http://foo.com/icon.png", url.spec());