Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / extensions / bookmark_app_helper_unittest.cc
blobbd03fd47aafc70d29a34f5bed25cc0839aef2f1b
1 // Copyright 2014 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/extensions/bookmark_app_helper.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_service_test_base.h"
10 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/test/test_web_contents_factory.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/common/constants.h"
17 #include "extensions/common/extension_icon_set.h"
18 #include "extensions/common/manifest_handlers/icons_handler.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "ui/gfx/skia_util.h"
23 namespace extensions {
25 namespace {
27 const char kAppUrl[] = "http://www.chromium.org";
28 const char kAlternativeAppUrl[] = "http://www.notchromium.org";
29 const char kAppTitle[] = "Test title";
30 const char kAppShortName[] = "Test short name";
31 const char kAlternativeAppTitle[] = "Different test title";
32 const char kAppDescription[] = "Test description";
33 const char kAppIcon1[] = "fav1.png";
34 const char kAppIcon2[] = "fav2.png";
35 const char kAppIcon3[] = "fav3.png";
36 const char kAppIconURL1[] = "http://foo.com/1.png";
37 const char kAppIconURL2[] = "http://foo.com/2.png";
38 const char kAppIconURL3[] = "http://foo.com/3.png";
39 const char kAppIconURL4[] = "http://foo.com/4.png";
41 const int kIconSizeTiny = extension_misc::EXTENSION_ICON_BITTY;
42 const int kIconSizeSmall = extension_misc::EXTENSION_ICON_SMALL;
43 const int kIconSizeMedium = extension_misc::EXTENSION_ICON_MEDIUM;
44 const int kIconSizeLarge = extension_misc::EXTENSION_ICON_LARGE;
45 const int kIconSizeGigantor = extension_misc::EXTENSION_ICON_GIGANTOR;
46 const int kIconSizeUnsupported = 123;
48 const int kIconSizeSmallBetweenMediumAndLarge = 63;
49 const int kIconSizeLargeBetweenMediumAndLarge = 96;
51 class BookmarkAppHelperTest : public testing::Test {
52 public:
53 BookmarkAppHelperTest() {}
54 ~BookmarkAppHelperTest() override {}
56 private:
57 DISALLOW_COPY_AND_ASSIGN(BookmarkAppHelperTest);
60 class BookmarkAppHelperExtensionServiceTest
61 : public extensions::ExtensionServiceTestBase {
62 public:
63 BookmarkAppHelperExtensionServiceTest() {}
64 ~BookmarkAppHelperExtensionServiceTest() override {}
66 void SetUp() override {
67 extensions::ExtensionServiceTestBase::SetUp();
68 InitializeEmptyExtensionService();
69 service_->Init();
70 EXPECT_EQ(0u, registry()->enabled_extensions().size());
73 void TearDown() override {
74 ExtensionServiceTestBase::TearDown();
75 for (content::RenderProcessHost::iterator i(
76 content::RenderProcessHost::AllHostsIterator());
77 !i.IsAtEnd();
78 i.Advance()) {
79 content::RenderProcessHost* host = i.GetCurrentValue();
80 if (Profile::FromBrowserContext(host->GetBrowserContext()) ==
81 profile_.get())
82 host->Cleanup();
86 private:
87 DISALLOW_COPY_AND_ASSIGN(BookmarkAppHelperExtensionServiceTest);
90 SkBitmap CreateSquareBitmapWithColor(int size, SkColor color) {
91 SkBitmap bitmap;
92 bitmap.allocN32Pixels(size, size);
93 bitmap.eraseColor(color);
94 return bitmap;
97 BookmarkAppHelper::BitmapAndSource CreateSquareBitmapAndSourceWithColor(
98 int size,
99 SkColor color) {
100 return BookmarkAppHelper::BitmapAndSource(
101 GURL(), CreateSquareBitmapWithColor(size, color));
104 void ValidateBitmapSizeAndColor(SkBitmap bitmap, int size, SkColor color) {
105 // Obtain pixel lock to access pixels.
106 SkAutoLockPixels lock(bitmap);
107 EXPECT_EQ(color, bitmap.getColor(0, 0));
108 EXPECT_EQ(size, bitmap.width());
109 EXPECT_EQ(size, bitmap.height());
112 WebApplicationInfo::IconInfo CreateIconInfoWithBitmap(int size, SkColor color) {
113 WebApplicationInfo::IconInfo icon_info;
114 icon_info.width = size;
115 icon_info.height = size;
116 icon_info.data = CreateSquareBitmapWithColor(size, color);
117 return icon_info;
120 std::set<int> TestSizesToGenerate() {
121 const int kIconSizesToGenerate[] = {
122 extension_misc::EXTENSION_ICON_SMALL,
123 extension_misc::EXTENSION_ICON_MEDIUM,
124 extension_misc::EXTENSION_ICON_LARGE,
126 return std::set<int>(kIconSizesToGenerate,
127 kIconSizesToGenerate + arraysize(kIconSizesToGenerate));
130 void ValidateAllIconsWithURLsArePresent(const WebApplicationInfo& info_to_check,
131 const WebApplicationInfo& other_info) {
132 for (const auto& icon : info_to_check.icons) {
133 if (!icon.url.is_empty()) {
134 bool found = false;
135 for (const auto& other_icon : info_to_check.icons) {
136 if (other_icon.url == icon.url && other_icon.width == icon.width) {
137 found = true;
138 break;
141 EXPECT_TRUE(found);
146 std::vector<BookmarkAppHelper::BitmapAndSource>::const_iterator
147 FindMatchingBitmapAndSourceVector(
148 const std::vector<BookmarkAppHelper::BitmapAndSource>& bitmap_vector,
149 int size) {
150 for (std::vector<BookmarkAppHelper::BitmapAndSource>::const_iterator it =
151 bitmap_vector.begin();
152 it != bitmap_vector.end(); ++it) {
153 if (it->bitmap.width() == size) {
154 return it;
157 return bitmap_vector.end();
160 std::vector<BookmarkAppHelper::BitmapAndSource>::const_iterator
161 FindEqualOrLargerBitmapAndSourceVector(
162 const std::vector<BookmarkAppHelper::BitmapAndSource>& bitmap_vector,
163 int size) {
164 for (std::vector<BookmarkAppHelper::BitmapAndSource>::const_iterator it =
165 bitmap_vector.begin();
166 it != bitmap_vector.end(); ++it) {
167 if (it->bitmap.width() >= size) {
168 return it;
171 return bitmap_vector.end();
174 void ValidateWebApplicationInfo(base::Closure callback,
175 const WebApplicationInfo& original,
176 const WebApplicationInfo& newly_made) {
177 EXPECT_EQ(original.title, newly_made.title);
178 EXPECT_EQ(original.description, newly_made.description);
179 EXPECT_EQ(original.app_url, newly_made.app_url);
180 // There should be 6 icons, as there are three sizes which need to be
181 // generated, and each will generate a 1x and 2x icon.
182 EXPECT_EQ(6u, newly_made.icons.size());
183 callback.Run();
186 void ValidateOnlyGenerateIconsWhenNoLargerExists(
187 std::vector<BookmarkAppHelper::BitmapAndSource> downloaded,
188 std::map<int, BookmarkAppHelper::BitmapAndSource> size_map,
189 std::set<int> sizes_to_generate, int expected_generated) {
190 GURL empty_url("");
191 int number_generated = 0;
193 for (const auto& size : sizes_to_generate) {
194 auto icon_downloaded = FindMatchingBitmapAndSourceVector(downloaded, size);
195 auto icon_larger = FindEqualOrLargerBitmapAndSourceVector(downloaded, size);
196 if (icon_downloaded == downloaded.end()) {
197 auto icon_resized = size_map.find(size);
198 if (icon_larger == downloaded.end()) {
199 // There is no larger downloaded icon. Expect an icon to be generated.
200 EXPECT_NE(icon_resized, size_map.end());
201 EXPECT_EQ(icon_resized->second.bitmap.width(), size);
202 EXPECT_EQ(icon_resized->second.bitmap.height(), size);
203 EXPECT_EQ(icon_resized->second.bitmap.height(), size);
204 EXPECT_EQ(icon_resized->second.source_url, empty_url);
205 ++number_generated;
206 } else {
207 // There is a larger downloaded icon. Expect no icon to be generated.
208 // However, an existing icon may be resized down to fit this size.
209 // If this is the case, the |source_url| will be non-empty.
210 if (icon_resized != size_map.end()) {
211 EXPECT_EQ(icon_resized->second.bitmap.width(), size);
212 EXPECT_EQ(icon_resized->second.bitmap.height(), size);
213 EXPECT_EQ(icon_resized->second.bitmap.height(), size);
214 EXPECT_EQ(icon_resized->second.source_url, icon_larger->source_url);
217 } else {
218 // There is an icon of exactly this size downloaded. Expect no icon to be
219 // generated, and the existing downloaded icon to be used.
220 auto icon_resized = size_map.find(size);
221 EXPECT_NE(icon_resized, size_map.end());
222 EXPECT_EQ(icon_resized->second.bitmap.width(), size);
223 EXPECT_EQ(icon_resized->second.bitmap.height(), size);
224 EXPECT_EQ(icon_downloaded->bitmap.width(), size);
225 EXPECT_EQ(icon_downloaded->bitmap.height(), size);
226 EXPECT_EQ(icon_resized->second.source_url, icon_downloaded->source_url);
229 EXPECT_EQ(number_generated, expected_generated);
232 void TestIconGeneration(int icon_size, int expected_generated) {
233 WebApplicationInfo web_app_info;
234 std::vector<BookmarkAppHelper::BitmapAndSource> downloaded;
236 // Add an icon with a URL and bitmap. 'Download' it.
237 WebApplicationInfo::IconInfo icon_info =
238 CreateIconInfoWithBitmap(icon_size, SK_ColorRED);
239 icon_info.url = GURL(kAppIconURL1);
240 web_app_info.icons.push_back(icon_info);
241 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
242 icon_info.url, icon_info.data));
244 // Now run the resizing and generation.
245 WebApplicationInfo new_web_app_info;
246 std::map<int, BookmarkAppHelper::BitmapAndSource> size_map =
247 BookmarkAppHelper::ResizeIconsAndGenerateMissing(downloaded,
248 TestSizesToGenerate(),
249 &new_web_app_info);
251 // Test that we end up with the expected number of generated icons.
252 ValidateOnlyGenerateIconsWhenNoLargerExists(downloaded, size_map,
253 TestSizesToGenerate(),
254 expected_generated);
257 } // namespace
259 class TestBookmarkAppHelper : public BookmarkAppHelper {
260 public:
261 TestBookmarkAppHelper(ExtensionService* service,
262 WebApplicationInfo web_app_info,
263 content::WebContents* contents)
264 : BookmarkAppHelper(service->profile(), web_app_info, contents) {}
266 ~TestBookmarkAppHelper() override {}
268 void CreationComplete(const extensions::Extension* extension,
269 const WebApplicationInfo& web_app_info) {
270 extension_ = extension;
273 void CompleteGetManifest(const content::Manifest& manifest) {
274 BookmarkAppHelper::OnDidGetManifest(manifest);
277 void CompleteIconDownload(
278 bool success,
279 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) {
280 BookmarkAppHelper::OnIconsDownloaded(success, bitmaps);
283 const Extension* extension() { return extension_; }
285 private:
286 const Extension* extension_;
288 DISALLOW_COPY_AND_ASSIGN(TestBookmarkAppHelper);
291 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkApp) {
292 WebApplicationInfo web_app_info;
293 web_app_info.app_url = GURL(kAppUrl);
294 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
295 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
297 content::TestWebContentsFactory web_contents_factory;
298 content::WebContents* contents =
299 web_contents_factory.CreateWebContents(profile());
300 TestBookmarkAppHelper helper(service_, web_app_info, contents);
301 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete,
302 base::Unretained(&helper)));
304 std::map<GURL, std::vector<SkBitmap> > icon_map;
305 icon_map[GURL(kAppUrl)].push_back(
306 CreateSquareBitmapWithColor(kIconSizeSmall, SK_ColorRED));
307 helper.CompleteIconDownload(true, icon_map);
309 base::RunLoop().RunUntilIdle();
310 EXPECT_TRUE(helper.extension());
311 const Extension* extension =
312 service_->GetInstalledExtension(helper.extension()->id());
313 EXPECT_TRUE(extension);
314 EXPECT_EQ(1u, registry()->enabled_extensions().size());
315 EXPECT_TRUE(extension->from_bookmark());
316 EXPECT_EQ(kAppTitle, extension->name());
317 EXPECT_EQ(kAppDescription, extension->description());
318 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
319 EXPECT_FALSE(
320 IconsInfo::GetIconResource(
321 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY).empty());
324 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkAppWithManifest) {
325 WebApplicationInfo web_app_info;
327 content::TestWebContentsFactory web_contents_factory;
328 content::WebContents* contents =
329 web_contents_factory.CreateWebContents(profile());
330 TestBookmarkAppHelper helper(service_, web_app_info, contents);
331 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete,
332 base::Unretained(&helper)));
334 content::Manifest manifest;
335 manifest.start_url = GURL(kAppUrl);
336 manifest.name = base::NullableString16(base::UTF8ToUTF16(kAppTitle), false);
337 helper.CompleteGetManifest(manifest);
339 std::map<GURL, std::vector<SkBitmap> > icon_map;
340 helper.CompleteIconDownload(true, icon_map);
342 base::RunLoop().RunUntilIdle();
343 EXPECT_TRUE(helper.extension());
344 const Extension* extension =
345 service_->GetInstalledExtension(helper.extension()->id());
346 EXPECT_TRUE(extension);
347 EXPECT_EQ(1u, registry()->enabled_extensions().size());
348 EXPECT_TRUE(extension->from_bookmark());
349 EXPECT_EQ(kAppTitle, extension->name());
350 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
353 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkAppNoContents) {
354 WebApplicationInfo web_app_info;
355 web_app_info.app_url = GURL(kAppUrl);
356 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
357 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
358 web_app_info.icons.push_back(
359 CreateIconInfoWithBitmap(kIconSizeTiny, SK_ColorRED));
361 TestBookmarkAppHelper helper(service_, web_app_info, NULL);
362 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete,
363 base::Unretained(&helper)));
365 base::RunLoop().RunUntilIdle();
366 EXPECT_TRUE(helper.extension());
367 const Extension* extension =
368 service_->GetInstalledExtension(helper.extension()->id());
369 EXPECT_TRUE(extension);
370 EXPECT_EQ(1u, registry()->enabled_extensions().size());
371 EXPECT_TRUE(extension->from_bookmark());
372 EXPECT_EQ(kAppTitle, extension->name());
373 EXPECT_EQ(kAppDescription, extension->description());
374 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
375 EXPECT_FALSE(
376 IconsInfo::GetIconResource(extension, kIconSizeTiny,
377 ExtensionIconSet::MATCH_EXACTLY).empty());
378 EXPECT_FALSE(
379 IconsInfo::GetIconResource(
380 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY).empty());
381 EXPECT_FALSE(
382 IconsInfo::GetIconResource(extension,
383 kIconSizeSmall * 2,
384 ExtensionIconSet::MATCH_EXACTLY).empty());
385 EXPECT_FALSE(
386 IconsInfo::GetIconResource(
387 extension, kIconSizeMedium, ExtensionIconSet::MATCH_EXACTLY).empty());
388 EXPECT_FALSE(
389 IconsInfo::GetIconResource(extension,
390 kIconSizeMedium * 2,
391 ExtensionIconSet::MATCH_EXACTLY).empty());
394 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateAndUpdateBookmarkApp) {
395 EXPECT_EQ(0u, registry()->enabled_extensions().size());
396 WebApplicationInfo web_app_info;
397 web_app_info.app_url = GURL(kAppUrl);
398 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
399 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
400 web_app_info.icons.push_back(
401 CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED));
403 extensions::CreateOrUpdateBookmarkApp(service_, &web_app_info);
404 base::RunLoop().RunUntilIdle();
407 EXPECT_EQ(1u, registry()->enabled_extensions().size());
408 const Extension* extension =
409 registry()->enabled_extensions().begin()->get();
410 EXPECT_TRUE(extension->from_bookmark());
411 EXPECT_EQ(kAppTitle, extension->name());
412 EXPECT_EQ(kAppDescription, extension->description());
413 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
414 EXPECT_FALSE(extensions::IconsInfo::GetIconResource(
415 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY)
416 .empty());
419 web_app_info.title = base::UTF8ToUTF16(kAlternativeAppTitle);
420 web_app_info.icons[0] = CreateIconInfoWithBitmap(kIconSizeLarge, SK_ColorRED);
422 extensions::CreateOrUpdateBookmarkApp(service_, &web_app_info);
423 base::RunLoop().RunUntilIdle();
426 EXPECT_EQ(1u, registry()->enabled_extensions().size());
427 const Extension* extension =
428 registry()->enabled_extensions().begin()->get();
429 EXPECT_TRUE(extension->from_bookmark());
430 EXPECT_EQ(kAlternativeAppTitle, extension->name());
431 EXPECT_EQ(kAppDescription, extension->description());
432 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
433 EXPECT_FALSE(extensions::IconsInfo::GetIconResource(
434 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY)
435 .empty());
436 EXPECT_FALSE(extensions::IconsInfo::GetIconResource(
437 extension, kIconSizeLarge, ExtensionIconSet::MATCH_EXACTLY)
438 .empty());
442 TEST_F(BookmarkAppHelperExtensionServiceTest, GetWebApplicationInfo) {
443 WebApplicationInfo web_app_info;
444 web_app_info.app_url = GURL(kAppUrl);
445 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
446 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
448 extensions::CreateOrUpdateBookmarkApp(service_, &web_app_info);
449 base::RunLoop().RunUntilIdle();
451 EXPECT_EQ(1u, registry()->enabled_extensions().size());
452 base::RunLoop run_loop;
453 extensions::GetWebApplicationInfoFromApp(
454 profile_.get(), registry()->enabled_extensions().begin()->get(),
455 base::Bind(&ValidateWebApplicationInfo, run_loop.QuitClosure(),
456 web_app_info));
457 run_loop.Run();
460 TEST_F(BookmarkAppHelperExtensionServiceTest, LinkedAppIconsAreNotChanged) {
461 WebApplicationInfo web_app_info;
463 // Add two icons with a URL and bitmap, two icons with just a bitmap, an
464 // icon with just URL and an icon in an unsupported size with just a URL.
465 WebApplicationInfo::IconInfo icon_info =
466 CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED);
467 icon_info.url = GURL(kAppIconURL1);
468 web_app_info.icons.push_back(icon_info);
470 icon_info = CreateIconInfoWithBitmap(kIconSizeMedium, SK_ColorRED);
471 icon_info.url = GURL(kAppIconURL2);
472 web_app_info.icons.push_back(icon_info);
474 icon_info.data = SkBitmap();
475 icon_info.url = GURL(kAppIconURL3);
476 icon_info.width = 0;
477 icon_info.height = 0;
478 web_app_info.icons.push_back(icon_info);
480 icon_info.url = GURL(kAppIconURL4);
481 web_app_info.icons.push_back(icon_info);
483 icon_info = CreateIconInfoWithBitmap(kIconSizeLarge, SK_ColorRED);
484 web_app_info.icons.push_back(icon_info);
486 icon_info = CreateIconInfoWithBitmap(kIconSizeUnsupported, SK_ColorRED);
487 web_app_info.icons.push_back(icon_info);
489 // 'Download' one of the icons without a size or bitmap.
490 std::vector<BookmarkAppHelper::BitmapAndSource> downloaded;
491 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
492 GURL(kAppIconURL3),
493 CreateSquareBitmapWithColor(kIconSizeLarge, SK_ColorBLACK)));
495 // Now run the resizing and generation into a new web app info.
496 WebApplicationInfo new_web_app_info;
497 std::map<int, BookmarkAppHelper::BitmapAndSource> size_map =
498 BookmarkAppHelper::ResizeIconsAndGenerateMissing(downloaded,
499 TestSizesToGenerate(),
500 &new_web_app_info);
502 // Now check that the linked app icons (i.e. those with URLs) are matching in
503 // both lists.
504 ValidateAllIconsWithURLsArePresent(web_app_info, new_web_app_info);
505 ValidateAllIconsWithURLsArePresent(new_web_app_info, web_app_info);
508 TEST_F(BookmarkAppHelperTest, UpdateWebAppInfoFromManifest) {
509 WebApplicationInfo web_app_info;
510 web_app_info.title = base::UTF8ToUTF16(kAlternativeAppTitle);
511 web_app_info.app_url = GURL(kAlternativeAppUrl);
512 WebApplicationInfo::IconInfo info;
513 info.url = GURL(kAppIcon1);
514 web_app_info.icons.push_back(info);
516 content::Manifest manifest;
517 manifest.start_url = GURL(kAppUrl);
518 manifest.short_name = base::NullableString16(base::UTF8ToUTF16(kAppShortName),
519 false);
521 BookmarkAppHelper::UpdateWebAppInfoFromManifest(manifest, &web_app_info);
522 EXPECT_EQ(base::UTF8ToUTF16(kAppShortName), web_app_info.title);
523 EXPECT_EQ(GURL(kAppUrl), web_app_info.app_url);
525 // The icon info from |web_app_info| should be left as is, since the manifest
526 // doesn't have any icon information.
527 EXPECT_EQ(1u, web_app_info.icons.size());
528 EXPECT_EQ(GURL(kAppIcon1), web_app_info.icons[0].url);
530 // Test that |manifest.name| takes priority over |manifest.short_name|, and
531 // that icons provided by the manifest replace icons in |web_app_info|.
532 manifest.name = base::NullableString16(base::UTF8ToUTF16(kAppTitle), false);
534 content::Manifest::Icon icon;
535 icon.src = GURL(kAppIcon2);
536 manifest.icons.push_back(icon);
537 icon.src = GURL(kAppIcon3);
538 manifest.icons.push_back(icon);
540 BookmarkAppHelper::UpdateWebAppInfoFromManifest(manifest, &web_app_info);
541 EXPECT_EQ(base::UTF8ToUTF16(kAppTitle), web_app_info.title);
543 EXPECT_EQ(2u, web_app_info.icons.size());
544 EXPECT_EQ(GURL(kAppIcon2), web_app_info.icons[0].url);
545 EXPECT_EQ(GURL(kAppIcon3), web_app_info.icons[1].url);
548 TEST_F(BookmarkAppHelperTest, ConstrainBitmapsToSizes) {
549 std::set<int> desired_sizes;
550 desired_sizes.insert(16);
551 desired_sizes.insert(32);
552 desired_sizes.insert(128);
553 desired_sizes.insert(256);
556 std::vector<BookmarkAppHelper::BitmapAndSource> bitmaps;
557 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(16, SK_ColorRED));
558 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(32, SK_ColorGREEN));
559 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(48, SK_ColorBLUE));
560 bitmaps.push_back(
561 CreateSquareBitmapAndSourceWithColor(144, SK_ColorYELLOW));
563 std::map<int, BookmarkAppHelper::BitmapAndSource> results(
564 BookmarkAppHelper::ConstrainBitmapsToSizes(bitmaps, desired_sizes));
566 EXPECT_EQ(3u, results.size());
567 ValidateBitmapSizeAndColor(results[16].bitmap, 16, SK_ColorRED);
568 ValidateBitmapSizeAndColor(results[32].bitmap, 32, SK_ColorGREEN);
569 ValidateBitmapSizeAndColor(results[128].bitmap, 128, SK_ColorYELLOW);
572 std::vector<BookmarkAppHelper::BitmapAndSource> bitmaps;
573 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(512, SK_ColorRED));
574 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(18, SK_ColorGREEN));
575 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(33, SK_ColorBLUE));
576 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(17, SK_ColorYELLOW));
578 std::map<int, BookmarkAppHelper::BitmapAndSource> results(
579 BookmarkAppHelper::ConstrainBitmapsToSizes(bitmaps, desired_sizes));
581 EXPECT_EQ(3u, results.size());
582 ValidateBitmapSizeAndColor(results[16].bitmap, 16, SK_ColorYELLOW);
583 ValidateBitmapSizeAndColor(results[32].bitmap, 32, SK_ColorBLUE);
584 ValidateBitmapSizeAndColor(results[256].bitmap, 256, SK_ColorRED);
588 TEST_F(BookmarkAppHelperTest, IsValidBookmarkAppUrl) {
589 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("https://www.chromium.org")));
590 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("http://www.chromium.org/path")));
591 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("ftp://www.chromium.org")));
592 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("chrome://flags")));
595 TEST_F(BookmarkAppHelperTest, IconsGeneratedOnlyWhenNoneLarger) {
596 WebApplicationInfo web_app_info;
597 std::vector<BookmarkAppHelper::BitmapAndSource> downloaded;
599 // Add three icons with a URL and bitmap. 'Download' each of them.
600 WebApplicationInfo::IconInfo icon_info =
601 CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED);
602 icon_info.url = GURL(kAppIconURL1);
603 web_app_info.icons.push_back(icon_info);
604 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
605 icon_info.url, icon_info.data));
607 icon_info = CreateIconInfoWithBitmap(kIconSizeSmallBetweenMediumAndLarge,
608 SK_ColorRED);
609 icon_info.url = GURL(kAppIconURL2);
610 web_app_info.icons.push_back(icon_info);
611 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
612 icon_info.url, icon_info.data));
614 icon_info = CreateIconInfoWithBitmap(kIconSizeLargeBetweenMediumAndLarge,
615 SK_ColorRED);
616 icon_info.url = GURL(kAppIconURL3);
617 web_app_info.icons.push_back(icon_info);
618 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
619 icon_info.url, icon_info.data));
621 // Now run the resizing and generation.
622 WebApplicationInfo new_web_app_info;
623 std::map<int, BookmarkAppHelper::BitmapAndSource> size_map =
624 BookmarkAppHelper::ResizeIconsAndGenerateMissing(downloaded,
625 TestSizesToGenerate(),
626 &new_web_app_info);
628 // Test that icons are only generated when necessary. The largest icon
629 // downloaded is smaller than EXTENSION_ICON_LARGE, so one icon should be
630 // generated.
631 ValidateOnlyGenerateIconsWhenNoLargerExists(downloaded, size_map,
632 TestSizesToGenerate(), 1);
635 TEST_F(BookmarkAppHelperTest, AllIconsGeneratedWhenOnlyASmallOneIsProvided) {
636 // When only a tiny icon is downloaded (smaller than the three desired
637 // sizes), 3 icons should be generated.
638 TestIconGeneration(kIconSizeTiny, 3);
641 TEST_F(BookmarkAppHelperTest, NoIconsGeneratedWhenAVeryLargeOneIsProvided) {
642 // When an enormous icon is provided, each desired icon size should fall back
643 // to it, and no icons should be generated.
644 TestIconGeneration(kIconSizeGigantor, 0);
647 } // namespace extensions