OLD | NEW |
---|---|
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/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 CreateIcon("http://foo.com/icon.png", "", 1.0, std::vector<gfx::Size>())); | 76 CreateIcon("http://foo.com/icon.png", "", 1.0, std::vector<gfx::Size>())); |
77 | 77 |
78 GURL url = FindBestMatchingIcon(icons); | 78 GURL url = FindBestMatchingIcon(icons); |
79 EXPECT_TRUE(url.is_empty()); | 79 EXPECT_TRUE(url.is_empty()); |
80 } | 80 } |
81 | 81 |
82 TEST_F(ManifestIconSelectorTest, MIMETypeFiltering) { | 82 TEST_F(ManifestIconSelectorTest, MIMETypeFiltering) { |
83 // Icons with type specified to a MIME type that isn't a valid image MIME type | 83 // Icons with type specified to a MIME type that isn't a valid image MIME type |
84 // are ignored. | 84 // are ignored. |
85 std::vector<gfx::Size> sizes; | 85 std::vector<gfx::Size> sizes; |
86 sizes.push_back(gfx::Size(10, 10)); | 86 sizes.push_back(gfx::Size(1024, 1024)); |
87 | 87 |
88 std::vector<content::Manifest::Icon> icons; | 88 std::vector<content::Manifest::Icon> icons; |
89 icons.push_back( | 89 icons.push_back( |
90 CreateIcon("http://foo.com/icon.png", "image/foo_bar", 1.0, sizes)); | 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)); | 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)); | 92 icons.push_back(CreateIcon("http://foo.com/icon.png", "image/", 1.0, sizes)); |
93 icons.push_back( | 93 icons.push_back( |
94 CreateIcon("http://foo.com/icon.png", "video/mp4", 1.0, sizes)); | 94 CreateIcon("http://foo.com/icon.png", "video/mp4", 1.0, sizes)); |
95 | 95 |
96 GURL url = FindBestMatchingIcon(icons); | 96 GURL url = FindBestMatchingIcon(icons); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec()); | 144 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec()); |
145 | 145 |
146 SetDisplayDeviceScaleFactor(3.0f); | 146 SetDisplayDeviceScaleFactor(3.0f); |
147 url = FindBestMatchingIcon(icons); | 147 url = FindBestMatchingIcon(icons); |
148 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); | 148 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); |
149 } | 149 } |
150 | 150 |
151 TEST_F(ManifestIconSelectorTest, PreferredSizeOfDefaultDensityIsUsedSecond) { | 151 TEST_F(ManifestIconSelectorTest, PreferredSizeOfDefaultDensityIsUsedSecond) { |
152 // This test has three icons. The first one is of density zero and is marked | 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. | 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. | 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 | 155 // Regardless of the device scale factor, the icon of density 1 is going to be |
156 // used because it matches the preferred size. | 156 // used because it matches the preferred size. |
157 std::vector<gfx::Size> sizes_1; | 157 std::vector<gfx::Size> sizes_1; |
158 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(), | 158 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp(), |
159 GetPreferredIconSizeInDp())); | 159 GetPreferredIconSizeInDp())); |
160 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() * 2, | 160 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() * 2, |
161 GetPreferredIconSizeInDp() * 2)); | 161 GetPreferredIconSizeInDp() * 2)); |
162 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() * 3, | 162 sizes_1.push_back(gfx::Size(GetPreferredIconSizeInDp() * 3, |
163 GetPreferredIconSizeInDp() * 3)); | 163 GetPreferredIconSizeInDp() * 3)); |
164 | 164 |
165 std::vector<gfx::Size> sizes_2; | 165 std::vector<gfx::Size> sizes_2; |
166 sizes_2.push_back(gfx::Size(2, 2)); | 166 sizes_2.push_back(gfx::Size(1024, 1024)); |
167 | 167 |
168 std::vector<gfx::Size> sizes_3; | 168 std::vector<gfx::Size> sizes_3; |
169 sizes_3.push_back(gfx::Size(3, 3)); | 169 sizes_3.push_back(gfx::Size(1024, 1024)); |
170 | 170 |
171 std::vector<content::Manifest::Icon> icons; | 171 std::vector<content::Manifest::Icon> icons; |
172 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes_1)); | 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)); | 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)); | 174 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes_3)); |
175 | 175 |
176 SetDisplayDeviceScaleFactor(1.0f); | 176 SetDisplayDeviceScaleFactor(1.0f); |
177 GURL url = FindBestMatchingIcon(icons); | 177 GURL url = FindBestMatchingIcon(icons); |
178 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 178 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
179 | 179 |
180 SetDisplayDeviceScaleFactor(2.0f); | 180 SetDisplayDeviceScaleFactor(2.0f); |
181 url = FindBestMatchingIcon(icons); | 181 url = FindBestMatchingIcon(icons); |
182 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 182 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
183 | 183 |
184 SetDisplayDeviceScaleFactor(3.0f); | 184 SetDisplayDeviceScaleFactor(3.0f); |
185 url = FindBestMatchingIcon(icons); | 185 url = FindBestMatchingIcon(icons); |
186 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 186 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
187 } | 187 } |
188 | 188 |
189 TEST_F(ManifestIconSelectorTest, DeviceDensityFirst) { | 189 TEST_F(ManifestIconSelectorTest, DeviceDensityFirst) { |
190 // If there is no perfect icon but an icon of the current device density is | 190 // If there is no perfect icon but an icon of the current device density is |
191 // present, it will be picked. | 191 // present, it will be picked. |
192 // This test has three icons each are marked with sizes set to the preferred | 192 // This test has three icons each are marked with sizes set to the preferred |
193 // icon size for the associated density. | 193 // icon size for the associated density. |
194 std::vector<gfx::Size> sizes; | 194 std::vector<gfx::Size> sizes; |
195 sizes.push_back(gfx::Size(2, 2)); | 195 sizes.push_back(gfx::Size(1024, 1024)); |
196 | 196 |
197 std::vector<content::Manifest::Icon> icons; | 197 std::vector<content::Manifest::Icon> icons; |
198 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes)); | 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)); | 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)); | 200 icons.push_back(CreateIcon("http://foo.com/icon_x3.png", "", 3.0, sizes)); |
201 | 201 |
202 SetDisplayDeviceScaleFactor(1.0f); | 202 SetDisplayDeviceScaleFactor(1.0f); |
203 GURL url = FindBestMatchingIcon(icons); | 203 GURL url = FindBestMatchingIcon(icons); |
204 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 204 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
205 | 205 |
206 SetDisplayDeviceScaleFactor(2.0f); | 206 SetDisplayDeviceScaleFactor(2.0f); |
207 url = FindBestMatchingIcon(icons); | 207 url = FindBestMatchingIcon(icons); |
208 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec()); | 208 EXPECT_EQ("http://foo.com/icon_x2.png", url.spec()); |
209 | 209 |
210 SetDisplayDeviceScaleFactor(3.0f); | 210 SetDisplayDeviceScaleFactor(3.0f); |
211 url = FindBestMatchingIcon(icons); | 211 url = FindBestMatchingIcon(icons); |
212 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); | 212 EXPECT_EQ("http://foo.com/icon_x3.png", url.spec()); |
213 } | 213 } |
214 | 214 |
215 TEST_F(ManifestIconSelectorTest, DeviceDensityFallback) { | 215 TEST_F(ManifestIconSelectorTest, DeviceDensityFallback) { |
216 // If there is no perfect icon but and no icon of the current display density, | 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. | 217 // an icon of density 1.0 will be used. |
218 std::vector<gfx::Size> sizes; | 218 std::vector<gfx::Size> sizes; |
219 sizes.push_back(gfx::Size(2, 2)); | 219 sizes.push_back(gfx::Size(1024, 1024)); |
220 | 220 |
221 std::vector<content::Manifest::Icon> icons; | 221 std::vector<content::Manifest::Icon> icons; |
222 icons.push_back(CreateIcon("http://foo.com/icon_x1.png", "", 1.0, sizes)); | 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)); | 223 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes)); |
224 | 224 |
225 SetDisplayDeviceScaleFactor(3.0f); | 225 SetDisplayDeviceScaleFactor(3.0f); |
226 GURL url = FindBestMatchingIcon(icons); | 226 GURL url = FindBestMatchingIcon(icons); |
227 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); | 227 EXPECT_EQ("http://foo.com/icon_x1.png", url.spec()); |
228 } | 228 } |
229 | 229 |
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; | |
237 | |
238 sizes_1_2.push_back(gfx::Size(47, 47)); | |
239 sizes_3.push_back(gfx::Size(95, 95)); | |
240 | |
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)); | |
245 | |
246 SetDisplayDeviceScaleFactor(1.0f); | |
mlamouri (slow - plz ping)
2015/08/20 22:18:18
nit: add comments explaining why nothing matches.
Lalit Maganti
2015/08/21 10:46:37
Done.
| |
247 GURL url = FindBestMatchingIcon(icons); | |
248 EXPECT_TRUE(url.is_empty()); | |
249 | |
250 SetDisplayDeviceScaleFactor(2.0f); | |
mlamouri (slow - plz ping)
2015/08/20 22:18:18
ditto
Lalit Maganti
2015/08/21 10:46:37
Done.
| |
251 url = FindBestMatchingIcon(icons); | |
252 EXPECT_TRUE(url.is_empty()); | |
253 | |
254 SetDisplayDeviceScaleFactor(3.0f); | |
mlamouri (slow - plz ping)
2015/08/20 22:18:18
ditto
Lalit Maganti
2015/08/21 10:46:37
Done.
| |
255 url = FindBestMatchingIcon(icons); | |
256 EXPECT_TRUE(url.is_empty()); | |
257 } | |
258 | |
230 TEST_F(ManifestIconSelectorTest, DoNotUseOtherDensities) { | 259 TEST_F(ManifestIconSelectorTest, DoNotUseOtherDensities) { |
231 // If there are only icons of densities that are not the current display | 260 // If there are only icons of densities that are not the current display |
232 // density or the default density, they are ignored. | 261 // density or the default density, they are ignored. |
233 std::vector<gfx::Size> sizes; | 262 std::vector<gfx::Size> sizes; |
234 sizes.push_back(gfx::Size(2, 2)); | 263 sizes.push_back(gfx::Size(1024, 1024)); |
235 | 264 |
236 std::vector<content::Manifest::Icon> icons; | 265 std::vector<content::Manifest::Icon> icons; |
237 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes)); | 266 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes)); |
238 | 267 |
239 SetDisplayDeviceScaleFactor(3.0f); | 268 SetDisplayDeviceScaleFactor(3.0f); |
240 GURL url = FindBestMatchingIcon(icons); | 269 GURL url = FindBestMatchingIcon(icons); |
241 EXPECT_TRUE(url.is_empty()); | 270 EXPECT_TRUE(url.is_empty()); |
242 } | 271 } |
243 | 272 |
244 TEST_F(ManifestIconSelectorTest, NotSquareIconsAreIgnored) { | 273 TEST_F(ManifestIconSelectorTest, NotSquareIconsAreIgnored) { |
245 std::vector<gfx::Size> sizes; | 274 std::vector<gfx::Size> sizes; |
246 sizes.push_back(gfx::Size(20, 2)); | 275 sizes.push_back(gfx::Size(1024, 1023)); |
247 | 276 |
248 std::vector<content::Manifest::Icon> icons; | 277 std::vector<content::Manifest::Icon> icons; |
249 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes)); | 278 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes)); |
250 | 279 |
251 GURL url = FindBestMatchingIcon(icons); | 280 GURL url = FindBestMatchingIcon(icons); |
252 EXPECT_TRUE(url.is_empty()); | 281 EXPECT_TRUE(url.is_empty()); |
253 } | 282 } |
254 | 283 |
255 TEST_F(ManifestIconSelectorTest, ClosestIconToPreferred) { | 284 TEST_F(ManifestIconSelectorTest, ClosestIconToPreferred) { |
256 // This test verifies ManifestIconSelector::FindBestMatchingIcon by passing | 285 // This test verifies ManifestIconSelector::FindBestMatchingIcon by passing |
257 // different icon sizes and checking which one is picked. | 286 // 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 | 287 // The Device Scale Factor is 1.0 and the preferred icon size is returned by |
259 // GetPreferredIconSizeInDp(). | 288 // GetPreferredIconSizeInDp(). |
260 int very_small = GetPreferredIconSizeInDp() / 4; | 289 int very_small = GetPreferredIconSizeInDp() / 4; |
261 int small_size = GetPreferredIconSizeInDp() / 2; | 290 int small_size = GetPreferredIconSizeInDp() / 2; |
262 int bit_small = GetPreferredIconSizeInDp() - 1; | 291 int bit_small = GetPreferredIconSizeInDp() - 1; |
263 int bit_big = GetPreferredIconSizeInDp() + 1; | 292 int bit_big = GetPreferredIconSizeInDp() + 1; |
264 int big = GetPreferredIconSizeInDp() * 2; | 293 int big = GetPreferredIconSizeInDp() * 2; |
265 int very_big = GetPreferredIconSizeInDp() * 4; | 294 int very_big = GetPreferredIconSizeInDp() * 4; |
266 | 295 |
267 // (very_small, bit_small) => bit_small | 296 // (very_small, bit_small) => empty (since both are too small) |
268 { | 297 { |
269 std::vector<gfx::Size> sizes_1; | 298 std::vector<gfx::Size> sizes_1; |
270 sizes_1.push_back(gfx::Size(very_small, very_small)); | 299 sizes_1.push_back(gfx::Size(very_small, very_small)); |
271 | 300 |
272 std::vector<gfx::Size> sizes_2; | 301 std::vector<gfx::Size> sizes_2; |
273 sizes_2.push_back(gfx::Size(bit_small, bit_small)); | 302 sizes_2.push_back(gfx::Size(bit_small, bit_small)); |
274 | 303 |
275 std::vector<content::Manifest::Icon> icons; | 304 std::vector<content::Manifest::Icon> icons; |
276 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1)); | 305 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)); | 306 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2)); |
278 | 307 |
279 GURL url = FindBestMatchingIcon(icons); | 308 GURL url = FindBestMatchingIcon(icons); |
280 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 309 EXPECT_EQ("", url.spec()); |
281 } | 310 } |
282 | 311 |
283 // (very_small, bit_small, smaller) => bit_small | 312 // (very_small, bit_small, smaller) => empty (since both are too small) |
284 { | 313 { |
285 std::vector<gfx::Size> sizes_1; | 314 std::vector<gfx::Size> sizes_1; |
286 sizes_1.push_back(gfx::Size(very_small, very_small)); | 315 sizes_1.push_back(gfx::Size(very_small, very_small)); |
287 | 316 |
288 std::vector<gfx::Size> sizes_2; | 317 std::vector<gfx::Size> sizes_2; |
289 sizes_2.push_back(gfx::Size(bit_small, bit_small)); | 318 sizes_2.push_back(gfx::Size(bit_small, bit_small)); |
290 | 319 |
291 std::vector<gfx::Size> sizes_3; | 320 std::vector<gfx::Size> sizes_3; |
292 sizes_3.push_back(gfx::Size(small_size, small_size)); | 321 sizes_3.push_back(gfx::Size(small_size, small_size)); |
293 | 322 |
294 std::vector<content::Manifest::Icon> icons; | 323 std::vector<content::Manifest::Icon> icons; |
295 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1)); | 324 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)); | 325 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)); | 326 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_3)); |
298 | 327 |
299 GURL url = FindBestMatchingIcon(icons); | 328 GURL url = FindBestMatchingIcon(icons); |
300 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 329 EXPECT_EQ("", url.spec()); |
301 } | 330 } |
302 | 331 |
303 // (very_big, big) => big | 332 // (very_big, big) => big |
304 { | 333 { |
305 std::vector<gfx::Size> sizes_1; | 334 std::vector<gfx::Size> sizes_1; |
306 sizes_1.push_back(gfx::Size(very_big, very_big)); | 335 sizes_1.push_back(gfx::Size(very_big, very_big)); |
307 | 336 |
308 std::vector<gfx::Size> sizes_2; | 337 std::vector<gfx::Size> sizes_2; |
309 sizes_2.push_back(gfx::Size(big, big)); | 338 sizes_2.push_back(gfx::Size(big, big)); |
310 | 339 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 | 442 |
414 std::vector<content::Manifest::Icon> icons; | 443 std::vector<content::Manifest::Icon> icons; |
415 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes)); | 444 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)); | 445 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 3.0, sizes)); |
417 | 446 |
418 SetDisplayDeviceScaleFactor(3.0f); | 447 SetDisplayDeviceScaleFactor(3.0f); |
419 GURL url = FindBestMatchingIcon(icons); | 448 GURL url = FindBestMatchingIcon(icons); |
420 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 449 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
421 } | 450 } |
422 } | 451 } |
OLD | NEW |