Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(904)

Side by Side Diff: chrome/browser/manifest/manifest_icon_selector_unittest.cc

Issue 2662103002: Refactor ManifestIconSelector and update it for Manifest.icon.purpose (Closed)
Patch Set: InstallableManager not updated Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/manifest/manifest_icon_selector.h" 5 #include "chrome/browser/manifest/manifest_icon_selector.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 using IconPurpose = content::Manifest::Icon::IconPurpose;
15
14 namespace { 16 namespace {
15 17
16 const int kDefaultIconSize = 144; 18 const int kIdealIconSize = 144;
17 19 const int kMinimumIconSize = 0;
18 GURL FindBestMatchingIconWithMinimum(
19 const std::vector<content::Manifest::Icon>& icons,
20 int ideal_icon_size_in_px,
21 int minimum_icon_size_in_px) {
22 return ManifestIconSelector::FindBestMatchingIcon(
23 icons, ideal_icon_size_in_px, minimum_icon_size_in_px);
24 }
25
26 GURL FindBestMatchingIcon(const std::vector<content::Manifest::Icon>& icons,
27 int ideal_icon_size_in_px) {
28 return FindBestMatchingIconWithMinimum(icons, ideal_icon_size_in_px, 0);
29 }
30 20
31 static content::Manifest::Icon CreateIcon(const std::string& url, 21 static content::Manifest::Icon CreateIcon(const std::string& url,
32 const std::string& type, 22 const std::string& type,
33 const std::vector<gfx::Size> sizes) { 23 const std::vector<gfx::Size> sizes,
24 IconPurpose purpose) {
34 content::Manifest::Icon icon; 25 content::Manifest::Icon icon;
35 icon.src = GURL(url); 26 icon.src = GURL(url);
36 icon.type = base::UTF8ToUTF16(type); 27 icon.type = base::UTF8ToUTF16(type);
37 icon.sizes = sizes; 28 icon.sizes = sizes;
29 icon.purpose.push_back(purpose);
38 30
39 return icon; 31 return icon;
40 } 32 }
41 33
42 } // anonymous namespace 34 } // anonymous namespace
43 35
44 TEST(ManifestIconSelector, NoIcons) { 36 TEST(ManifestIconSelector, NoIcons) {
45 // No icons should return the empty URL. 37 // No icons should return the empty URL.
46 std::vector<content::Manifest::Icon> icons; 38 std::vector<content::Manifest::Icon> icons;
47 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 39 GURL url = ManifestIconSelector::FindBestMatchingIcon(
40 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
48 EXPECT_TRUE(url.is_empty()); 41 EXPECT_TRUE(url.is_empty());
49 } 42 }
50 43
51 TEST(ManifestIconSelector, NoSizes) { 44 TEST(ManifestIconSelector, NoSizes) {
52 // Icon with no sizes are ignored. 45 // Icon with no sizes are ignored.
53 std::vector<content::Manifest::Icon> icons; 46 std::vector<content::Manifest::Icon> icons;
54 icons.push_back( 47 icons.push_back(CreateIcon("http://foo.com/icon.png", "",
55 CreateIcon("http://foo.com/icon.png", "", std::vector<gfx::Size>())); 48 std::vector<gfx::Size>(), IconPurpose::ANY));
56 49
57 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 50 GURL url = ManifestIconSelector::FindBestMatchingIcon(
51 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
58 EXPECT_TRUE(url.is_empty()); 52 EXPECT_TRUE(url.is_empty());
59 } 53 }
60 54
61 TEST(ManifestIconSelector, MIMETypeFiltering) { 55 TEST(ManifestIconSelector, MIMETypeFiltering) {
62 // Icons with type specified to a MIME type that isn't a valid image MIME type 56 // Icons with type specified to a MIME type that isn't a valid image MIME type
63 // are ignored. 57 // are ignored.
64 std::vector<gfx::Size> sizes; 58 std::vector<gfx::Size> sizes;
65 sizes.push_back(gfx::Size(1024, 1024)); 59 sizes.push_back(gfx::Size(1024, 1024));
66 60
67 std::vector<content::Manifest::Icon> icons; 61 std::vector<content::Manifest::Icon> icons;
62 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/foo_bar", sizes,
63 IconPurpose::ANY));
68 icons.push_back( 64 icons.push_back(
69 CreateIcon("http://foo.com/icon.png", "image/foo_bar", sizes)); 65 CreateIcon("http://foo.com/icon.png", "image/", sizes, IconPurpose::ANY));
70 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", sizes)); 66 icons.push_back(
71 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", sizes)); 67 CreateIcon("http://foo.com/icon.png", "image/", sizes, IconPurpose::ANY));
72 icons.push_back(CreateIcon("http://foo.com/icon.png", "video/mp4", sizes)); 68 icons.push_back(CreateIcon("http://foo.com/icon.png", "video/mp4", sizes,
69 IconPurpose::ANY));
73 70
74 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 71 GURL url = ManifestIconSelector::FindBestMatchingIcon(
72 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
75 EXPECT_TRUE(url.is_empty()); 73 EXPECT_TRUE(url.is_empty());
76 74
77 icons.clear(); 75 icons.clear();
78 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/png", sizes)); 76 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/png", sizes,
79 url = FindBestMatchingIcon(icons, kDefaultIconSize); 77 IconPurpose::ANY));
78 url = ManifestIconSelector::FindBestMatchingIcon(
79 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
80 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 80 EXPECT_EQ("http://foo.com/icon.png", url.spec());
81 81
82 icons.clear(); 82 icons.clear();
83 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/gif", sizes)); 83 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/gif", sizes,
84 url = FindBestMatchingIcon(icons, kDefaultIconSize); 84 IconPurpose::ANY));
85 url = ManifestIconSelector::FindBestMatchingIcon(
86 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
85 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 87 EXPECT_EQ("http://foo.com/icon.png", url.spec());
86 88
87 icons.clear(); 89 icons.clear();
88 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/jpeg", sizes)); 90 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/jpeg", sizes,
89 url = FindBestMatchingIcon(icons, kDefaultIconSize); 91 IconPurpose::ANY));
92 url = ManifestIconSelector::FindBestMatchingIcon(
93 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
90 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 94 EXPECT_EQ("http://foo.com/icon.png", url.spec());
91 } 95 }
92 96
93 TEST(ManifestIconSelector, PreferredSizeIsUsedFirst) { 97 TEST(ManifestIconSelector, PurposeFiltering) {
94 // Each icon is marked with sizes that match the preferred icon size. 98 // Icons with purpose specified to non-matching purpose are ignored.
95 std::vector<gfx::Size> sizes_48; 99 std::vector<gfx::Size> sizes_48;
96 sizes_48.push_back(gfx::Size(48, 48)); 100 sizes_48.push_back(gfx::Size(48, 48));
97 101
102 std::vector<gfx::Size> sizes_96;
103 sizes_96.push_back(gfx::Size(96, 96));
104
105 std::vector<gfx::Size> sizes_144;
106 sizes_144.push_back(gfx::Size(144, 144));
107
108 std::vector<content::Manifest::Icon> icons;
109 icons.push_back(
110 CreateIcon("http://foo.com/icon_48.png", "", sizes_48, IconPurpose::BADGE) );
111 icons.push_back(
112 CreateIcon("http://foo.com/icon_96.png", "", sizes_96, IconPurpose::ANY));
113 icons.push_back(CreateIcon("http://foo.com/icon_144.png", "", sizes_144,
114 IconPurpose::ANY));
115
116 GURL url = ManifestIconSelector::FindBestMatchingIcon(
117 icons, 48, kMinimumIconSize, IconPurpose::BADGE);
118 EXPECT_EQ("http://foo.com/icon_48.png", url.spec());
119
120 url = ManifestIconSelector::FindBestMatchingIcon(
121 icons, 48, kMinimumIconSize, IconPurpose::ANY);
122 EXPECT_EQ("http://foo.com/icon_96.png", url.spec());
123
124 url = ManifestIconSelector::FindBestMatchingIcon(icons, 96, kMinimumIconSize,
125 IconPurpose::BADGE);
126 EXPECT_TRUE(url.is_empty());
127
128 url = ManifestIconSelector::FindBestMatchingIcon(icons, 144, kMinimumIconSize,
129 IconPurpose::ANY);
130 EXPECT_EQ("http://foo.com/icon_144.png", url.spec());
131 }
132
133 TEST(ManifestIconSelector, IdealSizeIsUsedFirst) {
134 // Each icon is marked with sizes that match the ideal icon size.
135 std::vector<gfx::Size> sizes_48;
136 sizes_48.push_back(gfx::Size(48, 48));
137
98 std::vector<gfx::Size> sizes_96; 138 std::vector<gfx::Size> sizes_96;
99 sizes_96.push_back(gfx::Size(96, 96)); 139 sizes_96.push_back(gfx::Size(96, 96));
100 140
101 std::vector<gfx::Size> sizes_144; 141 std::vector<gfx::Size> sizes_144;
102 sizes_144.push_back(gfx::Size(144, 144)); 142 sizes_144.push_back(gfx::Size(144, 144));
103 143
104 std::vector<content::Manifest::Icon> icons; 144 std::vector<content::Manifest::Icon> icons;
105 icons.push_back(CreateIcon("http://foo.com/icon_48.png", "", sizes_48)); 145 icons.push_back(
106 icons.push_back(CreateIcon("http://foo.com/icon_96.png", "", sizes_96)); 146 CreateIcon("http://foo.com/icon_48.png", "", sizes_48, IconPurpose::ANY));
107 icons.push_back(CreateIcon("http://foo.com/icon_144.png", "", sizes_144)); 147 icons.push_back(
148 CreateIcon("http://foo.com/icon_96.png", "", sizes_96, IconPurpose::ANY));
149 icons.push_back(CreateIcon("http://foo.com/icon_144.png", "", sizes_144,
150 IconPurpose::ANY));
108 151
109 GURL url = FindBestMatchingIcon(icons, 48); 152 GURL url = ManifestIconSelector::FindBestMatchingIcon(
153 icons, 48, kMinimumIconSize, IconPurpose::ANY);
110 EXPECT_EQ("http://foo.com/icon_48.png", url.spec()); 154 EXPECT_EQ("http://foo.com/icon_48.png", url.spec());
111 155
112 url = FindBestMatchingIcon(icons, 96); 156 url = ManifestIconSelector::FindBestMatchingIcon(icons, 96, kMinimumIconSize,
157 IconPurpose::ANY);
113 EXPECT_EQ("http://foo.com/icon_96.png", url.spec()); 158 EXPECT_EQ("http://foo.com/icon_96.png", url.spec());
114 159
115 url = FindBestMatchingIcon(icons, 144); 160 url = ManifestIconSelector::FindBestMatchingIcon(icons, 144, kMinimumIconSize,
161 IconPurpose::ANY);
116 EXPECT_EQ("http://foo.com/icon_144.png", url.spec()); 162 EXPECT_EQ("http://foo.com/icon_144.png", url.spec());
117 } 163 }
118 164
119 TEST(ManifestIconSelector, FirstIconWithPreferredSizeIsUsedFirst) { 165 TEST(ManifestIconSelector, FirstIconWithIdealSizeIsUsedFirst) {
120 // This test has three icons. The first icon is going to be used because it 166 // This test has three icons. The first icon is going to be used because it
121 // contains the preferred size. 167 // contains the ideal size.
122 std::vector<gfx::Size> sizes_1; 168 std::vector<gfx::Size> sizes_1;
123 sizes_1.push_back(gfx::Size(kDefaultIconSize, kDefaultIconSize)); 169 sizes_1.push_back(gfx::Size(kIdealIconSize, kIdealIconSize));
124 sizes_1.push_back(gfx::Size(kDefaultIconSize * 2, kDefaultIconSize * 2)); 170 sizes_1.push_back(gfx::Size(kIdealIconSize * 2, kIdealIconSize * 2));
125 sizes_1.push_back(gfx::Size(kDefaultIconSize * 3, kDefaultIconSize * 3)); 171 sizes_1.push_back(gfx::Size(kIdealIconSize * 3, kIdealIconSize * 3));
126 172
127 std::vector<gfx::Size> sizes_2; 173 std::vector<gfx::Size> sizes_2;
128 sizes_2.push_back(gfx::Size(1024, 1024)); 174 sizes_2.push_back(gfx::Size(1024, 1024));
129 175
130 std::vector<gfx::Size> sizes_3; 176 std::vector<gfx::Size> sizes_3;
131 sizes_3.push_back(gfx::Size(1024, 1024)); 177 sizes_3.push_back(gfx::Size(1024, 1024));
132 178
133 std::vector<content::Manifest::Icon> icons; 179 std::vector<content::Manifest::Icon> icons;
134 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes_1)); 180 icons.push_back(
135 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", sizes_2)); 181 CreateIcon("http://foo.com/icon_x1.png", "", sizes_1, IconPurpose::ANY));
136 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", sizes_3)); 182 icons.push_back(
183 CreateIcon("http://foo.com/icon_x2.png", "", sizes_2, IconPurpose::ANY));
184 icons.push_back(
185 CreateIcon("http://foo.com/icon_x3.png", "", sizes_3, IconPurpose::ANY));
137 186
138 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 187 GURL url = ManifestIconSelector::FindBestMatchingIcon(
188 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
139 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); 189 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
140 190
141 url = FindBestMatchingIcon(icons, kDefaultIconSize * 2); 191 url = ManifestIconSelector::FindBestMatchingIcon(
192 icons, kIdealIconSize * 2, kMinimumIconSize, IconPurpose::ANY);
142 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); 193 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
143 194
144 url = FindBestMatchingIcon(icons, kDefaultIconSize * 3); 195 url = ManifestIconSelector::FindBestMatchingIcon(
196 icons, kIdealIconSize * 3, kMinimumIconSize, IconPurpose::ANY);
145 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); 197 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
146 } 198 }
147 199
148 TEST(ManifestIconSelector, FallbackToSmallestLargerIcon) { 200 TEST(ManifestIconSelector, FallbackToSmallestLargerIcon) {
149 // If there is no perfect icon, the smallest larger icon will be chosen. 201 // If there is no perfect icon, the smallest larger icon will be chosen.
150 std::vector<gfx::Size> sizes_1; 202 std::vector<gfx::Size> sizes_1;
151 sizes_1.push_back(gfx::Size(90, 90)); 203 sizes_1.push_back(gfx::Size(90, 90));
152 204
153 std::vector<gfx::Size> sizes_2; 205 std::vector<gfx::Size> sizes_2;
154 sizes_2.push_back(gfx::Size(128, 128)); 206 sizes_2.push_back(gfx::Size(128, 128));
155 207
156 std::vector<gfx::Size> sizes_3; 208 std::vector<gfx::Size> sizes_3;
157 sizes_3.push_back(gfx::Size(192, 192)); 209 sizes_3.push_back(gfx::Size(192, 192));
158 210
159 std::vector<content::Manifest::Icon> icons; 211 std::vector<content::Manifest::Icon> icons;
160 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes_1)); 212 icons.push_back(
161 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", sizes_2)); 213 CreateIcon("http://foo.com/icon_x1.png", "", sizes_1, IconPurpose::ANY));
162 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", sizes_3)); 214 icons.push_back(
215 CreateIcon("http://foo.com/icon_x2.png", "", sizes_2, IconPurpose::ANY));
216 icons.push_back(
217 CreateIcon("http://foo.com/icon_x3.png", "", sizes_3, IconPurpose::ANY));
163 218
164 GURL url = FindBestMatchingIcon(icons, 48); 219 GURL url = ManifestIconSelector::FindBestMatchingIcon(
220 icons, 48, kMinimumIconSize, IconPurpose::ANY);
165 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); 221 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
166 222
167 url = FindBestMatchingIcon(icons, 96); 223 url = ManifestIconSelector::FindBestMatchingIcon(icons, 96, kMinimumIconSize,
224 IconPurpose::ANY);
168 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec()); 225 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec());
169 226
170 url = FindBestMatchingIcon(icons, 144); 227 url = ManifestIconSelector::FindBestMatchingIcon(icons, 144, kMinimumIconSize,
228 IconPurpose::ANY);
171 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); 229 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec());
172 } 230 }
173 231
174 TEST(ManifestIconSelector, FallbackToLargestIconLargerThanMinimum) { 232 TEST(ManifestIconSelector, FallbackToLargestIconLargerThanMinimum) {
175 // When an icon of the correct size has not been found, we fall back to the 233 // When an icon of the correct size has not been found, we fall back to the
176 // closest non-matching sizes. Make sure that the minimum passed is enforced. 234 // closest non-matching sizes. Make sure that the minimum passed is enforced.
177 std::vector<gfx::Size> sizes_1_2; 235 std::vector<gfx::Size> sizes_1_2;
178 std::vector<gfx::Size> sizes_3; 236 std::vector<gfx::Size> sizes_3;
179 237
180 sizes_1_2.push_back(gfx::Size(47, 47)); 238 sizes_1_2.push_back(gfx::Size(47, 47));
181 sizes_3.push_back(gfx::Size(95, 95)); 239 sizes_3.push_back(gfx::Size(95, 95));
182 240
183 std::vector<content::Manifest::Icon> icons; 241 std::vector<content::Manifest::Icon> icons;
184 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes_1_2)); 242 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes_1_2,
185 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", sizes_1_2)); 243 IconPurpose::ANY));
186 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", sizes_3)); 244 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", sizes_1_2,
245 IconPurpose::ANY));
246 icons.push_back(
247 CreateIcon("http://foo.com/icon_x3.png", "", sizes_3, IconPurpose::ANY));
187 248
188 // Icon 3 should match. 249 // Icon 3 should match.
189 GURL url = FindBestMatchingIconWithMinimum(icons, 1024, 48); 250 GURL url = ManifestIconSelector::FindBestMatchingIcon(icons, 1024, 48,
251 IconPurpose::ANY);
190 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); 252 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec());
191 253
192 // Nothing matches here as the minimum is 96. 254 // Nothing matches here as the minimum is 96.
193 url = FindBestMatchingIconWithMinimum(icons, 1024, 96); 255 url = ManifestIconSelector::FindBestMatchingIcon(icons, 1024, 96,
256 IconPurpose::ANY);
194 EXPECT_TRUE(url.is_empty()); 257 EXPECT_TRUE(url.is_empty());
195 } 258 }
196 259
197 TEST(ManifestIconSelector, IdealVeryCloseToMinimumMatches) { 260 TEST(ManifestIconSelector, IdealVeryCloseToMinimumMatches) {
198 std::vector<gfx::Size> sizes; 261 std::vector<gfx::Size> sizes;
199 sizes.push_back(gfx::Size(2, 2)); 262 sizes.push_back(gfx::Size(2, 2));
200 263
201 std::vector<content::Manifest::Icon> icons; 264 std::vector<content::Manifest::Icon> icons;
202 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes)); 265 icons.push_back(
266 CreateIcon("http://foo.com/icon_x1.png", "", sizes, IconPurpose::ANY));
203 267
204 GURL url = FindBestMatchingIconWithMinimum(icons, 2, 1); 268 GURL url =
269 ManifestIconSelector::FindBestMatchingIcon(icons, 2, 1, IconPurpose::ANY);
205 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); 270 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
206 } 271 }
207 272
208 TEST(ManifestIconSelector, SizeVeryCloseToMinimumMatches) { 273 TEST(ManifestIconSelector, SizeVeryCloseToMinimumMatches) {
209 std::vector<gfx::Size> sizes; 274 std::vector<gfx::Size> sizes;
210 sizes.push_back(gfx::Size(2, 2)); 275 sizes.push_back(gfx::Size(2, 2));
211 276
212 std::vector<content::Manifest::Icon> icons; 277 std::vector<content::Manifest::Icon> icons;
213 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", sizes)); 278 icons.push_back(
279 CreateIcon("http://foo.com/icon_x1.png", "", sizes, IconPurpose::ANY));
214 280
215 GURL url = FindBestMatchingIconWithMinimum(icons, 200, 1); 281 GURL url = ManifestIconSelector::FindBestMatchingIcon(icons, 200, 1,
282 IconPurpose::ANY);
216 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); 283 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec());
217 } 284 }
218 285
219 TEST(ManifestIconSelector, NotSquareIconsAreIgnored) { 286 TEST(ManifestIconSelector, NotSquareIconsAreIgnored) {
220 std::vector<gfx::Size> sizes; 287 std::vector<gfx::Size> sizes;
221 sizes.push_back(gfx::Size(1024, 1023)); 288 sizes.push_back(gfx::Size(1024, 1023));
222 289
223 std::vector<content::Manifest::Icon> icons; 290 std::vector<content::Manifest::Icon> icons;
224 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes)); 291 icons.push_back(
292 CreateIcon("http://foo.com/icon.png", "", sizes, IconPurpose::ANY));
225 293
226 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 294 GURL url = ManifestIconSelector::FindBestMatchingIcon(
295 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
227 EXPECT_TRUE(url.is_empty()); 296 EXPECT_TRUE(url.is_empty());
228 } 297 }
229 298
230 TEST(ManifestIconSelector, ClosestIconToPreferred) { 299 TEST(ManifestIconSelector, ClosestIconToIdeal) {
231 // Ensure ManifestIconSelector::FindBestMatchingIcon selects the closest icon 300 // Ensure ManifestIconSelector::FindBestMatchingIcon selects the closest icon
232 // to the preferred size when presented with a number of options. 301 // to the ideal size when presented with a number of options.
233 int very_small = kDefaultIconSize / 4; 302 int very_small = kIdealIconSize / 4;
234 int small_size = kDefaultIconSize / 2; 303 int small_size = kIdealIconSize / 2;
235 int bit_small = kDefaultIconSize - 1; 304 int bit_small = kIdealIconSize - 1;
236 int bit_big = kDefaultIconSize + 1; 305 int bit_big = kIdealIconSize + 1;
237 int big = kDefaultIconSize * 2; 306 int big = kIdealIconSize * 2;
238 int very_big = kDefaultIconSize * 4; 307 int very_big = kIdealIconSize * 4;
239 308
240 // (very_small, bit_small) => bit_small 309 // (very_small, bit_small) => bit_small
241 { 310 {
242 std::vector<gfx::Size> sizes_1; 311 std::vector<gfx::Size> sizes_1;
243 sizes_1.push_back(gfx::Size(very_small, very_small)); 312 sizes_1.push_back(gfx::Size(very_small, very_small));
244 313
245 std::vector<gfx::Size> sizes_2; 314 std::vector<gfx::Size> sizes_2;
246 sizes_2.push_back(gfx::Size(bit_small, bit_small)); 315 sizes_2.push_back(gfx::Size(bit_small, bit_small));
247 316
248 std::vector<content::Manifest::Icon> icons; 317 std::vector<content::Manifest::Icon> icons;
249 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); 318 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1,
250 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); 319 IconPurpose::ANY));
320 icons.push_back(
321 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY));
251 322
252 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 323 GURL url = ManifestIconSelector::FindBestMatchingIcon(
324 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
253 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 325 EXPECT_EQ("http://foo.com/icon.png", url.spec());
254 } 326 }
255 327
256 // (very_small, bit_small, small_size) => bit_small 328 // (very_small, bit_small, small_size) => bit_small
257 { 329 {
258 std::vector<gfx::Size> sizes_1; 330 std::vector<gfx::Size> sizes_1;
259 sizes_1.push_back(gfx::Size(very_small, very_small)); 331 sizes_1.push_back(gfx::Size(very_small, very_small));
260 332
261 std::vector<gfx::Size> sizes_2; 333 std::vector<gfx::Size> sizes_2;
262 sizes_2.push_back(gfx::Size(bit_small, bit_small)); 334 sizes_2.push_back(gfx::Size(bit_small, bit_small));
263 335
264 std::vector<gfx::Size> sizes_3; 336 std::vector<gfx::Size> sizes_3;
265 sizes_3.push_back(gfx::Size(small_size, small_size)); 337 sizes_3.push_back(gfx::Size(small_size, small_size));
266 338
267 std::vector<content::Manifest::Icon> icons; 339 std::vector<content::Manifest::Icon> icons;
268 icons.push_back(CreateIcon("http://foo.com/icon_no_1.png", "", sizes_1)); 340 icons.push_back(CreateIcon("http://foo.com/icon_no_1.png", "", sizes_1,
269 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); 341 IconPurpose::ANY));
270 icons.push_back(CreateIcon("http://foo.com/icon_no_2.png", "", sizes_3)); 342 icons.push_back(
343 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY));
344 icons.push_back(CreateIcon("http://foo.com/icon_no_2.png", "", sizes_3,
345 IconPurpose::ANY));
271 346
272 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 347 GURL url = ManifestIconSelector::FindBestMatchingIcon(
348 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
273 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 349 EXPECT_EQ("http://foo.com/icon.png", url.spec());
274 } 350 }
275 351
276 // (very_big, big) => big 352 // (very_big, big) => big
277 { 353 {
278 std::vector<gfx::Size> sizes_1; 354 std::vector<gfx::Size> sizes_1;
279 sizes_1.push_back(gfx::Size(very_big, very_big)); 355 sizes_1.push_back(gfx::Size(very_big, very_big));
280 356
281 std::vector<gfx::Size> sizes_2; 357 std::vector<gfx::Size> sizes_2;
282 sizes_2.push_back(gfx::Size(big, big)); 358 sizes_2.push_back(gfx::Size(big, big));
283 359
284 std::vector<content::Manifest::Icon> icons; 360 std::vector<content::Manifest::Icon> icons;
285 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); 361 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1,
286 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); 362 IconPurpose::ANY));
363 icons.push_back(
364 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY));
287 365
288 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 366 GURL url = ManifestIconSelector::FindBestMatchingIcon(
367 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
289 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 368 EXPECT_EQ("http://foo.com/icon.png", url.spec());
290 } 369 }
291 370
292 // (very_big, big, bit_big) => bit_big 371 // (very_big, big, bit_big) => bit_big
293 { 372 {
294 std::vector<gfx::Size> sizes_1; 373 std::vector<gfx::Size> sizes_1;
295 sizes_1.push_back(gfx::Size(very_big, very_big)); 374 sizes_1.push_back(gfx::Size(very_big, very_big));
296 375
297 std::vector<gfx::Size> sizes_2; 376 std::vector<gfx::Size> sizes_2;
298 sizes_2.push_back(gfx::Size(big, big)); 377 sizes_2.push_back(gfx::Size(big, big));
299 378
300 std::vector<gfx::Size> sizes_3; 379 std::vector<gfx::Size> sizes_3;
301 sizes_3.push_back(gfx::Size(bit_big, bit_big)); 380 sizes_3.push_back(gfx::Size(bit_big, bit_big));
302 381
303 std::vector<content::Manifest::Icon> icons; 382 std::vector<content::Manifest::Icon> icons;
304 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); 383 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1,
305 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_2)); 384 IconPurpose::ANY));
306 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_3)); 385 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_2,
386 IconPurpose::ANY));
387 icons.push_back(
388 CreateIcon("http://foo.com/icon.png", "", sizes_3, IconPurpose::ANY));
307 389
308 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 390 GURL url = ManifestIconSelector::FindBestMatchingIcon(
391 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
309 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 392 EXPECT_EQ("http://foo.com/icon.png", url.spec());
310 } 393 }
311 394
312 // (bit_small, very_big) => very_big 395 // (bit_small, very_big) => very_big
313 { 396 {
314 std::vector<gfx::Size> sizes_1; 397 std::vector<gfx::Size> sizes_1;
315 sizes_1.push_back(gfx::Size(bit_small, bit_small)); 398 sizes_1.push_back(gfx::Size(bit_small, bit_small));
316 399
317 std::vector<gfx::Size> sizes_2; 400 std::vector<gfx::Size> sizes_2;
318 sizes_2.push_back(gfx::Size(very_big, very_big)); 401 sizes_2.push_back(gfx::Size(very_big, very_big));
319 402
320 std::vector<content::Manifest::Icon> icons; 403 std::vector<content::Manifest::Icon> icons;
321 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); 404 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1,
322 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); 405 IconPurpose::ANY));
406 icons.push_back(
407 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY));
323 408
324 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 409 GURL url = ManifestIconSelector::FindBestMatchingIcon(
410 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
325 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 411 EXPECT_EQ("http://foo.com/icon.png", url.spec());
326 } 412 }
327 413
328 // (bit_small, bit_big) => bit_big 414 // (bit_small, bit_big) => bit_big
329 { 415 {
330 std::vector<gfx::Size> sizes_1; 416 std::vector<gfx::Size> sizes_1;
331 sizes_1.push_back(gfx::Size(bit_small, bit_small)); 417 sizes_1.push_back(gfx::Size(bit_small, bit_small));
332 418
333 std::vector<gfx::Size> sizes_2; 419 std::vector<gfx::Size> sizes_2;
334 sizes_2.push_back(gfx::Size(bit_big, bit_big)); 420 sizes_2.push_back(gfx::Size(bit_big, bit_big));
335 421
336 std::vector<content::Manifest::Icon> icons; 422 std::vector<content::Manifest::Icon> icons;
337 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); 423 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1,
338 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); 424 IconPurpose::ANY));
425 icons.push_back(
426 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY));
339 427
340 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 428 GURL url = ManifestIconSelector::FindBestMatchingIcon(
429 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
341 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 430 EXPECT_EQ("http://foo.com/icon.png", url.spec());
342 } 431 }
343 } 432 }
344 433
345 TEST(ManifestIconSelector, UseAnyIfNoPreferredSize) { 434 TEST(ManifestIconSelector, UseAnyIfNoIdealSize) {
346 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a 435 // 'any' (ie. gfx::Size(0,0)) should be used if there is no icon of a
347 // preferred size. 436 // ideal size.
348 437
349 // Icon with 'any' and icon with preferred size => preferred size is chosen. 438 // Icon with 'any' and icon with ideal size => ideal size is chosen.
350 { 439 {
351 std::vector<gfx::Size> sizes_1; 440 std::vector<gfx::Size> sizes_1;
352 sizes_1.push_back(gfx::Size(kDefaultIconSize, kDefaultIconSize)); 441 sizes_1.push_back(gfx::Size(kIdealIconSize, kIdealIconSize));
353 std::vector<gfx::Size> sizes_2; 442 std::vector<gfx::Size> sizes_2;
354 sizes_2.push_back(gfx::Size(0, 0)); 443 sizes_2.push_back(gfx::Size(0, 0));
355 444
356 std::vector<content::Manifest::Icon> icons; 445 std::vector<content::Manifest::Icon> icons;
357 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_1)); 446 icons.push_back(
358 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_2)); 447 CreateIcon("http://foo.com/icon.png", "", sizes_1, IconPurpose::ANY));
448 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_2,
449 IconPurpose::ANY));
359 450
360 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 451 GURL url = ManifestIconSelector::FindBestMatchingIcon(
452 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
361 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 453 EXPECT_EQ("http://foo.com/icon.png", url.spec());
362 } 454 }
363 455
364 // Icon with 'any' and icon larger than preferred size => any is chosen. 456 // Icon with 'any' and icon larger than ideal size => any is chosen.
365 { 457 {
366 std::vector<gfx::Size> sizes_1; 458 std::vector<gfx::Size> sizes_1;
367 sizes_1.push_back(gfx::Size(kDefaultIconSize + 1, kDefaultIconSize + 1)); 459 sizes_1.push_back(gfx::Size(kIdealIconSize + 1, kIdealIconSize + 1));
368 std::vector<gfx::Size> sizes_2; 460 std::vector<gfx::Size> sizes_2;
369 sizes_2.push_back(gfx::Size(0, 0)); 461 sizes_2.push_back(gfx::Size(0, 0));
370 462
371 std::vector<content::Manifest::Icon> icons; 463 std::vector<content::Manifest::Icon> icons;
372 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1)); 464 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", sizes_1,
373 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes_2)); 465 IconPurpose::ANY));
466 icons.push_back(
467 CreateIcon("http://foo.com/icon.png", "", sizes_2, IconPurpose::ANY));
374 468
375 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize); 469 GURL url = ManifestIconSelector::FindBestMatchingIcon(
470 icons, kIdealIconSize, kMinimumIconSize, IconPurpose::ANY);
376 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 471 EXPECT_EQ("http://foo.com/icon.png", url.spec());
377 } 472 }
378 473
379 // Multiple icons with 'any' => the last one is chosen. 474 // Multiple icons with 'any' => the last one is chosen.
380 { 475 {
381 std::vector<gfx::Size> sizes; 476 std::vector<gfx::Size> sizes;
382 sizes.push_back(gfx::Size(0, 0)); 477 sizes.push_back(gfx::Size(0, 0));
383 478
384 std::vector<content::Manifest::Icon> icons; 479 std::vector<content::Manifest::Icon> icons;
385 icons.push_back(CreateIcon("http://foo.com/icon_no1.png", "", sizes)); 480 icons.push_back(
386 icons.push_back(CreateIcon("http://foo.com/icon_no2.png", "", sizes)); 481 CreateIcon("http://foo.com/icon_no1.png", "", sizes, IconPurpose::ANY));
387 icons.push_back(CreateIcon("http://foo.com/icon.png", "", sizes)); 482 icons.push_back(
483 CreateIcon("http://foo.com/icon_no2.png", "", sizes, IconPurpose::ANY));
484 icons.push_back(
485 CreateIcon("http://foo.com/icon.png", "", sizes, IconPurpose::ANY));
388 486
389 GURL url = FindBestMatchingIcon(icons, kDefaultIconSize * 3); 487 GURL url = ManifestIconSelector::FindBestMatchingIcon(
488 icons, kIdealIconSize * 3, kMinimumIconSize, IconPurpose::ANY);
390 EXPECT_EQ("http://foo.com/icon.png", url.spec()); 489 EXPECT_EQ("http://foo.com/icon.png", url.spec());
391 } 490 }
392 } 491 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698