Rename vector icon header files.
[chromium-blink-merge.git] / chrome / browser / manifest / manifest_icon_selector_unittest.cc
blob36bd1d44812848de8d1b7ddefcca4a83eeb4d36b
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 namespace {
18 const int kPreferredIconSize = 48;
22 class ManifestIconSelectorTest : public testing::Test {
23 protected:
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,
47 double density,
48 const std::vector<gfx::Size> sizes) {
49 content::Manifest::Icon icon;
50 icon.src = GURL(url);
51 if (!type.empty())
52 icon.type = base::NullableString16(base::UTF8ToUTF16(type), false);
53 icon.density = density;
54 icon.sizes = sizes;
56 return icon;
59 private:
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;
75 icons.push_back(
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
84 // are ignored.
85 std::vector<gfx::Size> sizes;
86 sizes.push_back(gfx::Size(1024, 1024));
88 std::vector<content::Manifest::Icon> icons;
89 icons.push_back(
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));
93 icons.push_back(
94 CreateIcon("http://foo.com/icon.png", "video/mp4", 1.0, sizes));
96 GURL url = FindBestMatchingIcon(icons);
97 EXPECT_TRUE(url.is_empty());
99 icons.clear();
100 icons.push_back(
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());
105 icons.clear();
106 icons.push_back(
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());
111 icons.clear();
112 icons.push_back(
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 1024x1024.
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(1024, 1024));
168 std::vector<gfx::Size> sizes_3;
169 sizes_3.push_back(gfx::Size(1024, 1024));
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(1024, 1024));
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(1024, 1024));
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, DeviceDensityMatchRejectsTooSmall) {
231 // If we have to resort to density matching to find icons, then an icon of
232 // the correct size has not been found. Make sure that an icon which is just
233 // slightly smaller than one density bucket below the device is not chosen
234 // even if the density matches.
235 std::vector<gfx::Size> sizes_1_2;
236 std::vector<gfx::Size> sizes_3;
238 sizes_1_2.push_back(gfx::Size(47, 47));
239 sizes_3.push_back(gfx::Size(95, 95));
241 std::vector<content::Manifest::Icon> icons;
242 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1_2));
243 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes_1_2));
244 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3));
246 // Nothing matches here because there is a density scale factor lower bound of
247 // of 1.0 which since there is no density bucket smaller than the one
248 // associated with this scale factor.
249 SetDisplayDeviceScaleFactor(1.0f);
250 GURL url = FindBestMatchingIcon(icons);
251 EXPECT_TRUE(url.is_empty());
253 // Nothing matches here as the icon is just smaller than the icon size
254 // one density bucket below (i.e. 96 is expected and 48 is the minimum).
255 SetDisplayDeviceScaleFactor(2.0f);
256 url = FindBestMatchingIcon(icons);
257 EXPECT_TRUE(url.is_empty());
259 // Nothing matches here as the icon is just smaller than the icon size
260 // one density bucket below (i.e. 144 is expected and 96 is the minimum).
261 SetDisplayDeviceScaleFactor(3.0f);
262 url = FindBestMatchingIcon(icons);
263 EXPECT_TRUE(url.is_empty());
266 TEST_F(ManifestIconSelectorTest, DoNotUseOtherDensities) {
267 // If there are only icons of densities that are not the current display
268 // density or the default density, they are ignored.
269 std::vector<gfx::Size> sizes;
270 sizes.push_back(gfx::Size(1024, 1024));
272 std::vector<content::Manifest::Icon> icons;
273 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes));
275 SetDisplayDeviceScaleFactor(3.0f);
276 GURL url = FindBestMatchingIcon(icons);
277 EXPECT_TRUE(url.is_empty());
280 TEST_F(ManifestIconSelectorTest, NotSquareIconsAreIgnored) {
281 std::vector<gfx::Size> sizes;
282 sizes.push_back(gfx::Size(1024, 1023));
284 std::vector<content::Manifest::Icon> icons;
285 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes));
287 GURL url = FindBestMatchingIcon(icons);
288 EXPECT_TRUE(url.is_empty());
291 TEST_F(ManifestIconSelectorTest, ClosestIconToPreferred) {
292 // This test verifies ManifestIconSelector::FindBestMatchingIcon by passing
293 // different icon sizes and checking which one is picked.
294 // The Device Scale Factor is 1.0 and the preferred icon size is returned by
295 // GetPreferredIconSizeInDp().
296 int very_small = GetPreferredIconSizeInDp() / 4;
297 int small_size = GetPreferredIconSizeInDp() / 2;
298 int bit_small = GetPreferredIconSizeInDp() - 1;
299 int bit_big = GetPreferredIconSizeInDp() + 1;
300 int big = GetPreferredIconSizeInDp() * 2;
301 int very_big = GetPreferredIconSizeInDp() * 4;
303 // (very_small, bit_small) => empty (since both are too small)
305 std::vector<gfx::Size> sizes_1;
306 sizes_1.push_back(gfx::Size(very_small, very_small));
308 std::vector<gfx::Size> sizes_2;
309 sizes_2.push_back(gfx::Size(bit_small, bit_small));
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("", url.spec());
319 // (very_small, bit_small, smaller) => empty (since both are too small)
321 std::vector<gfx::Size> sizes_1;
322 sizes_1.push_back(gfx::Size(very_small, very_small));
324 std::vector<gfx::Size> sizes_2;
325 sizes_2.push_back(gfx::Size(bit_small, bit_small));
327 std::vector<gfx::Size> sizes_3;
328 sizes_3.push_back(gfx::Size(small_size, small_size));
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.png", "", 1.0, sizes_2));
333 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_3));
335 GURL url = FindBestMatchingIcon(icons);
336 EXPECT_EQ("", url.spec());
339 // (very_big, big) => big
341 std::vector<gfx::Size> sizes_1;
342 sizes_1.push_back(gfx::Size(very_big, very_big));
344 std::vector<gfx::Size> sizes_2;
345 sizes_2.push_back(gfx::Size(big, 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 // (very_big, big, bit_big) => bit_big
357 std::vector<gfx::Size> sizes_1;
358 sizes_1.push_back(gfx::Size(very_big, very_big));
360 std::vector<gfx::Size> sizes_2;
361 sizes_2.push_back(gfx::Size(big, big));
363 std::vector<gfx::Size> sizes_3;
364 sizes_3.push_back(gfx::Size(bit_big, bit_big));
366 std::vector<content::Manifest::Icon> icons;
367 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
368 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2));
369 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_3));
371 GURL url = FindBestMatchingIcon(icons);
372 EXPECT_EQ("http://foo.com/icon.png", url.spec());
375 // (bit_small, very_big) => very_big
377 std::vector<gfx::Size> sizes_1;
378 sizes_1.push_back(gfx::Size(bit_small, bit_small));
380 std::vector<gfx::Size> sizes_2;
381 sizes_2.push_back(gfx::Size(very_big, very_big));
383 std::vector<content::Manifest::Icon> icons;
384 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
385 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
387 GURL url = FindBestMatchingIcon(icons);
388 EXPECT_EQ("http://foo.com/icon.png", url.spec());
391 // (bit_small, bit_big) => bit_big
393 std::vector<gfx::Size> sizes_1;
394 sizes_1.push_back(gfx::Size(bit_small, bit_small));
396 std::vector<gfx::Size> sizes_2;
397 sizes_2.push_back(gfx::Size(bit_big, bit_big));
399 std::vector<content::Manifest::Icon> icons;
400 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
401 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
403 GURL url = FindBestMatchingIcon(icons);
404 EXPECT_EQ("http://foo.com/icon.png", url.spec());
408 TEST_F(ManifestIconSelectorTest, UseAnyIfNoPreferredSize) {
409 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a
410 // preferred size. An icon with the current device scale factor is preferred
411 // over one with the default density.
413 // 'any' with preferred size => preferred size
415 std::vector<gfx::Size> sizes_1;
416 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(),
417 GetPreferredIconSizeInDp()));
418 std::vector<gfx::Size> sizes_2;
419 sizes_2.push_back(gfx::Size(0, 0));
421 std::vector<content::Manifest::Icon> icons;
422 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_1));
423 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_2));
425 GURL url = FindBestMatchingIcon(icons);
426 EXPECT_EQ("http://foo.com/icon.png", url.spec());
429 // 'any' with nearly preferred size => any
431 std::vector<gfx::Size> sizes_1;
432 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() + 1,
433 GetPreferredIconSizeInDp() + 1));
434 std::vector<gfx::Size> sizes_2;
435 sizes_2.push_back(gfx::Size(0, 0));
437 std::vector<content::Manifest::Icon> icons;
438 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1));
439 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2));
441 GURL url = FindBestMatchingIcon(icons);
442 EXPECT_EQ("http://foo.com/icon.png", url.spec());
445 // 'any' on default density and current density => current density.
447 std::vector<gfx::Size> sizes;
448 sizes.push_back(gfx::Size(0, 0));
450 std::vector<content::Manifest::Icon> icons;
451 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes));
452 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 3.0, sizes));
454 SetDisplayDeviceScaleFactor(3.0f);
455 GURL url = FindBestMatchingIcon(icons);
456 EXPECT_EQ("http://foo.com/icon.png", url.spec());