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 // 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()); |
| 252 |
| 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()); |
| 258 |
| 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()); |
| 264 } |
| 265 |
230 TEST_F(ManifestIconSelectorTest, DoNotUseOtherDensities) { | 266 TEST_F(ManifestIconSelectorTest, DoNotUseOtherDensities) { |
231 // If there are only icons of densities that are not the current display | 267 // If there are only icons of densities that are not the current display |
232 // density or the default density, they are ignored. | 268 // density or the default density, they are ignored. |
233 std::vector<gfx::Size> sizes; | 269 std::vector<gfx::Size> sizes; |
234 sizes.push_back(gfx::Size(2, 2)); | 270 sizes.push_back(gfx::Size(1024, 1024)); |
235 | 271 |
236 std::vector<content::Manifest::Icon> icons; | 272 std::vector<content::Manifest::Icon> icons; |
237 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes)); | 273 icons.push_back(CreateIcon("http://foo.com/icon_x2.png", "", 2.0, sizes)); |
238 | 274 |
239 SetDisplayDeviceScaleFactor(3.0f); | 275 SetDisplayDeviceScaleFactor(3.0f); |
240 GURL url = FindBestMatchingIcon(icons); | 276 GURL url = FindBestMatchingIcon(icons); |
241 EXPECT_TRUE(url.is_empty()); | 277 EXPECT_TRUE(url.is_empty()); |
242 } | 278 } |
243 | 279 |
244 TEST_F(ManifestIconSelectorTest, NotSquareIconsAreIgnored) { | 280 TEST_F(ManifestIconSelectorTest, NotSquareIconsAreIgnored) { |
245 std::vector<gfx::Size> sizes; | 281 std::vector<gfx::Size> sizes; |
246 sizes.push_back(gfx::Size(20, 2)); | 282 sizes.push_back(gfx::Size(1024, 1023)); |
247 | 283 |
248 std::vector<content::Manifest::Icon> icons; | 284 std::vector<content::Manifest::Icon> icons; |
249 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes)); | 285 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes)); |
250 | 286 |
251 GURL url = FindBestMatchingIcon(icons); | 287 GURL url = FindBestMatchingIcon(icons); |
252 EXPECT_TRUE(url.is_empty()); | 288 EXPECT_TRUE(url.is_empty()); |
253 } | 289 } |
254 | 290 |
255 TEST_F(ManifestIconSelectorTest, ClosestIconToPreferred) { | 291 TEST_F(ManifestIconSelectorTest, ClosestIconToPreferred) { |
256 // This test verifies ManifestIconSelector::FindBestMatchingIcon by passing | 292 // This test verifies ManifestIconSelector::FindBestMatchingIcon by passing |
257 // different icon sizes and checking which one is picked. | 293 // 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 | 294 // The Device Scale Factor is 1.0 and the preferred icon size is returned by |
259 // GetPreferredIconSizeInDp(). | 295 // GetPreferredIconSizeInDp(). |
260 int very_small = GetPreferredIconSizeInDp() / 4; | 296 int very_small = GetPreferredIconSizeInDp() / 4; |
261 int small_size = GetPreferredIconSizeInDp() / 2; | 297 int small_size = GetPreferredIconSizeInDp() / 2; |
262 int bit_small = GetPreferredIconSizeInDp() - 1; | 298 int bit_small = GetPreferredIconSizeInDp() - 1; |
263 int bit_big = GetPreferredIconSizeInDp() + 1; | 299 int bit_big = GetPreferredIconSizeInDp() + 1; |
264 int big = GetPreferredIconSizeInDp() * 2; | 300 int big = GetPreferredIconSizeInDp() * 2; |
265 int very_big = GetPreferredIconSizeInDp() * 4; | 301 int very_big = GetPreferredIconSizeInDp() * 4; |
266 | 302 |
267 // (very_small, bit_small) => bit_small | 303 // (very_small, bit_small) => empty (since both are too small) |
268 { | 304 { |
269 std::vector<gfx::Size> sizes_1; | 305 std::vector<gfx::Size> sizes_1; |
270 sizes_1.push_back(gfx::Size(very_small, very_small)); | 306 sizes_1.push_back(gfx::Size(very_small, very_small)); |
271 | 307 |
272 std::vector<gfx::Size> sizes_2; | 308 std::vector<gfx::Size> sizes_2; |
273 sizes_2.push_back(gfx::Size(bit_small, bit_small)); | 309 sizes_2.push_back(gfx::Size(bit_small, bit_small)); |
274 | 310 |
275 std::vector<content::Manifest::Icon> icons; | 311 std::vector<content::Manifest::Icon> icons; |
276 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1)); | 312 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)); | 313 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 1.0, sizes_2)); |
278 | 314 |
279 GURL url = FindBestMatchingIcon(icons); | 315 GURL url = FindBestMatchingIcon(icons); |
280 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 316 EXPECT_EQ("", url.spec()); |
281 } | 317 } |
282 | 318 |
283 // (very_small, bit_small, smaller) => bit_small | 319 // (very_small, bit_small, smaller) => empty (since both are too small) |
284 { | 320 { |
285 std::vector<gfx::Size> sizes_1; | 321 std::vector<gfx::Size> sizes_1; |
286 sizes_1.push_back(gfx::Size(very_small, very_small)); | 322 sizes_1.push_back(gfx::Size(very_small, very_small)); |
287 | 323 |
288 std::vector<gfx::Size> sizes_2; | 324 std::vector<gfx::Size> sizes_2; |
289 sizes_2.push_back(gfx::Size(bit_small, bit_small)); | 325 sizes_2.push_back(gfx::Size(bit_small, bit_small)); |
290 | 326 |
291 std::vector<gfx::Size> sizes_3; | 327 std::vector<gfx::Size> sizes_3; |
292 sizes_3.push_back(gfx::Size(small_size, small_size)); | 328 sizes_3.push_back(gfx::Size(small_size, small_size)); |
293 | 329 |
294 std::vector<content::Manifest::Icon> icons; | 330 std::vector<content::Manifest::Icon> icons; |
295 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_1)); | 331 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)); | 332 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)); | 333 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes_3)); |
298 | 334 |
299 GURL url = FindBestMatchingIcon(icons); | 335 GURL url = FindBestMatchingIcon(icons); |
300 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 336 EXPECT_EQ("", url.spec()); |
301 } | 337 } |
302 | 338 |
303 // (very_big, big) => big | 339 // (very_big, big) => big |
304 { | 340 { |
305 std::vector<gfx::Size> sizes_1; | 341 std::vector<gfx::Size> sizes_1; |
306 sizes_1.push_back(gfx::Size(very_big, very_big)); | 342 sizes_1.push_back(gfx::Size(very_big, very_big)); |
307 | 343 |
308 std::vector<gfx::Size> sizes_2; | 344 std::vector<gfx::Size> sizes_2; |
309 sizes_2.push_back(gfx::Size(big, big)); | 345 sizes_2.push_back(gfx::Size(big, big)); |
310 | 346 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 | 449 |
414 std::vector<content::Manifest::Icon> icons; | 450 std::vector<content::Manifest::Icon> icons; |
415 icons.push_back(CreateIcon("http://foo.com/icon_no.png", "", 1.0, sizes)); | 451 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)); | 452 icons.push_back(CreateIcon("http://foo.com/icon.png", "", 3.0, sizes)); |
417 | 453 |
418 SetDisplayDeviceScaleFactor(3.0f); | 454 SetDisplayDeviceScaleFactor(3.0f); |
419 GURL url = FindBestMatchingIcon(icons); | 455 GURL url = FindBestMatchingIcon(icons); |
420 EXPECT_EQ("http://foo.com/icon.png", url.spec()); | 456 EXPECT_EQ("http://foo.com/icon.png", url.spec()); |
421 } | 457 } |
422 } | 458 } |
OLD | NEW |