Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / extensions / bookmark_app_helper_unittest.cc
blobbb3a2dd3eb393ccab233ef88bb0db7b3c2c496e5
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/web_contents_tester.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 scoped_ptr<content::WebContents> contents(
298 content::WebContentsTester::CreateTestWebContents(profile_.get(), NULL));
299 TestBookmarkAppHelper helper(service_, web_app_info, contents.get());
300 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete,
301 base::Unretained(&helper)));
303 std::map<GURL, std::vector<SkBitmap> > icon_map;
304 icon_map[GURL(kAppUrl)].push_back(
305 CreateSquareBitmapWithColor(kIconSizeSmall, SK_ColorRED));
306 helper.CompleteIconDownload(true, icon_map);
308 base::RunLoop().RunUntilIdle();
309 EXPECT_TRUE(helper.extension());
310 const Extension* extension =
311 service_->GetInstalledExtension(helper.extension()->id());
312 EXPECT_TRUE(extension);
313 EXPECT_EQ(1u, registry()->enabled_extensions().size());
314 EXPECT_TRUE(extension->from_bookmark());
315 EXPECT_EQ(kAppTitle, extension->name());
316 EXPECT_EQ(kAppDescription, extension->description());
317 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
318 EXPECT_FALSE(
319 IconsInfo::GetIconResource(
320 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY).empty());
323 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkAppWithManifest) {
324 WebApplicationInfo web_app_info;
326 scoped_ptr<content::WebContents> contents(
327 content::WebContentsTester::CreateTestWebContents(profile_.get(), NULL));
328 TestBookmarkAppHelper helper(service_, web_app_info, contents.get());
329 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete,
330 base::Unretained(&helper)));
332 content::Manifest manifest;
333 manifest.start_url = GURL(kAppUrl);
334 manifest.name = base::NullableString16(base::UTF8ToUTF16(kAppTitle), false);
335 helper.CompleteGetManifest(manifest);
337 std::map<GURL, std::vector<SkBitmap> > icon_map;
338 helper.CompleteIconDownload(true, icon_map);
340 base::RunLoop().RunUntilIdle();
341 EXPECT_TRUE(helper.extension());
342 const Extension* extension =
343 service_->GetInstalledExtension(helper.extension()->id());
344 EXPECT_TRUE(extension);
345 EXPECT_EQ(1u, registry()->enabled_extensions().size());
346 EXPECT_TRUE(extension->from_bookmark());
347 EXPECT_EQ(kAppTitle, extension->name());
348 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
351 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkAppNoContents) {
352 WebApplicationInfo web_app_info;
353 web_app_info.app_url = GURL(kAppUrl);
354 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
355 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
356 web_app_info.icons.push_back(
357 CreateIconInfoWithBitmap(kIconSizeTiny, SK_ColorRED));
359 TestBookmarkAppHelper helper(service_, web_app_info, NULL);
360 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete,
361 base::Unretained(&helper)));
363 base::RunLoop().RunUntilIdle();
364 EXPECT_TRUE(helper.extension());
365 const Extension* extension =
366 service_->GetInstalledExtension(helper.extension()->id());
367 EXPECT_TRUE(extension);
368 EXPECT_EQ(1u, registry()->enabled_extensions().size());
369 EXPECT_TRUE(extension->from_bookmark());
370 EXPECT_EQ(kAppTitle, extension->name());
371 EXPECT_EQ(kAppDescription, extension->description());
372 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
373 EXPECT_FALSE(
374 IconsInfo::GetIconResource(extension, kIconSizeTiny,
375 ExtensionIconSet::MATCH_EXACTLY).empty());
376 EXPECT_FALSE(
377 IconsInfo::GetIconResource(
378 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY).empty());
379 EXPECT_FALSE(
380 IconsInfo::GetIconResource(extension,
381 kIconSizeSmall * 2,
382 ExtensionIconSet::MATCH_EXACTLY).empty());
383 EXPECT_FALSE(
384 IconsInfo::GetIconResource(
385 extension, kIconSizeMedium, ExtensionIconSet::MATCH_EXACTLY).empty());
386 EXPECT_FALSE(
387 IconsInfo::GetIconResource(extension,
388 kIconSizeMedium * 2,
389 ExtensionIconSet::MATCH_EXACTLY).empty());
392 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateAndUpdateBookmarkApp) {
393 EXPECT_EQ(0u, registry()->enabled_extensions().size());
394 WebApplicationInfo web_app_info;
395 web_app_info.app_url = GURL(kAppUrl);
396 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
397 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
398 web_app_info.icons.push_back(
399 CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED));
401 extensions::CreateOrUpdateBookmarkApp(service_, &web_app_info);
402 base::RunLoop().RunUntilIdle();
405 EXPECT_EQ(1u, registry()->enabled_extensions().size());
406 const Extension* extension =
407 registry()->enabled_extensions().begin()->get();
408 EXPECT_TRUE(extension->from_bookmark());
409 EXPECT_EQ(kAppTitle, extension->name());
410 EXPECT_EQ(kAppDescription, extension->description());
411 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
412 EXPECT_FALSE(extensions::IconsInfo::GetIconResource(
413 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY)
414 .empty());
417 web_app_info.title = base::UTF8ToUTF16(kAlternativeAppTitle);
418 web_app_info.icons[0] = CreateIconInfoWithBitmap(kIconSizeLarge, SK_ColorRED);
420 extensions::CreateOrUpdateBookmarkApp(service_, &web_app_info);
421 base::RunLoop().RunUntilIdle();
424 EXPECT_EQ(1u, registry()->enabled_extensions().size());
425 const Extension* extension =
426 registry()->enabled_extensions().begin()->get();
427 EXPECT_TRUE(extension->from_bookmark());
428 EXPECT_EQ(kAlternativeAppTitle, extension->name());
429 EXPECT_EQ(kAppDescription, extension->description());
430 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension));
431 EXPECT_FALSE(extensions::IconsInfo::GetIconResource(
432 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY)
433 .empty());
434 EXPECT_FALSE(extensions::IconsInfo::GetIconResource(
435 extension, kIconSizeLarge, ExtensionIconSet::MATCH_EXACTLY)
436 .empty());
440 TEST_F(BookmarkAppHelperExtensionServiceTest, GetWebApplicationInfo) {
441 WebApplicationInfo web_app_info;
442 web_app_info.app_url = GURL(kAppUrl);
443 web_app_info.title = base::UTF8ToUTF16(kAppTitle);
444 web_app_info.description = base::UTF8ToUTF16(kAppDescription);
446 extensions::CreateOrUpdateBookmarkApp(service_, &web_app_info);
447 base::RunLoop().RunUntilIdle();
449 EXPECT_EQ(1u, registry()->enabled_extensions().size());
450 base::RunLoop run_loop;
451 extensions::GetWebApplicationInfoFromApp(
452 profile_.get(), registry()->enabled_extensions().begin()->get(),
453 base::Bind(&ValidateWebApplicationInfo, run_loop.QuitClosure(),
454 web_app_info));
455 run_loop.Run();
458 TEST_F(BookmarkAppHelperExtensionServiceTest, LinkedAppIconsAreNotChanged) {
459 WebApplicationInfo web_app_info;
461 // Add two icons with a URL and bitmap, two icons with just a bitmap, an
462 // icon with just URL and an icon in an unsupported size with just a URL.
463 WebApplicationInfo::IconInfo icon_info =
464 CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED);
465 icon_info.url = GURL(kAppIconURL1);
466 web_app_info.icons.push_back(icon_info);
468 icon_info = CreateIconInfoWithBitmap(kIconSizeMedium, SK_ColorRED);
469 icon_info.url = GURL(kAppIconURL2);
470 web_app_info.icons.push_back(icon_info);
472 icon_info.data = SkBitmap();
473 icon_info.url = GURL(kAppIconURL3);
474 icon_info.width = 0;
475 icon_info.height = 0;
476 web_app_info.icons.push_back(icon_info);
478 icon_info.url = GURL(kAppIconURL4);
479 web_app_info.icons.push_back(icon_info);
481 icon_info = CreateIconInfoWithBitmap(kIconSizeLarge, SK_ColorRED);
482 web_app_info.icons.push_back(icon_info);
484 icon_info = CreateIconInfoWithBitmap(kIconSizeUnsupported, SK_ColorRED);
485 web_app_info.icons.push_back(icon_info);
487 // 'Download' one of the icons without a size or bitmap.
488 std::vector<BookmarkAppHelper::BitmapAndSource> downloaded;
489 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
490 GURL(kAppIconURL3),
491 CreateSquareBitmapWithColor(kIconSizeLarge, SK_ColorBLACK)));
493 // Now run the resizing and generation into a new web app info.
494 WebApplicationInfo new_web_app_info;
495 std::map<int, BookmarkAppHelper::BitmapAndSource> size_map =
496 BookmarkAppHelper::ResizeIconsAndGenerateMissing(downloaded,
497 TestSizesToGenerate(),
498 &new_web_app_info);
500 // Now check that the linked app icons (i.e. those with URLs) are matching in
501 // both lists.
502 ValidateAllIconsWithURLsArePresent(web_app_info, new_web_app_info);
503 ValidateAllIconsWithURLsArePresent(new_web_app_info, web_app_info);
506 TEST_F(BookmarkAppHelperTest, UpdateWebAppInfoFromManifest) {
507 WebApplicationInfo web_app_info;
508 web_app_info.title = base::UTF8ToUTF16(kAlternativeAppTitle);
509 web_app_info.app_url = GURL(kAlternativeAppUrl);
510 WebApplicationInfo::IconInfo info;
511 info.url = GURL(kAppIcon1);
512 web_app_info.icons.push_back(info);
514 content::Manifest manifest;
515 manifest.start_url = GURL(kAppUrl);
516 manifest.short_name = base::NullableString16(base::UTF8ToUTF16(kAppShortName),
517 false);
519 BookmarkAppHelper::UpdateWebAppInfoFromManifest(manifest, &web_app_info);
520 EXPECT_EQ(base::UTF8ToUTF16(kAppShortName), web_app_info.title);
521 EXPECT_EQ(GURL(kAppUrl), web_app_info.app_url);
523 // The icon info from |web_app_info| should be left as is, since the manifest
524 // doesn't have any icon information.
525 EXPECT_EQ(1u, web_app_info.icons.size());
526 EXPECT_EQ(GURL(kAppIcon1), web_app_info.icons[0].url);
528 // Test that |manifest.name| takes priority over |manifest.short_name|, and
529 // that icons provided by the manifest replace icons in |web_app_info|.
530 manifest.name = base::NullableString16(base::UTF8ToUTF16(kAppTitle), false);
532 content::Manifest::Icon icon;
533 icon.src = GURL(kAppIcon2);
534 manifest.icons.push_back(icon);
535 icon.src = GURL(kAppIcon3);
536 manifest.icons.push_back(icon);
538 BookmarkAppHelper::UpdateWebAppInfoFromManifest(manifest, &web_app_info);
539 EXPECT_EQ(base::UTF8ToUTF16(kAppTitle), web_app_info.title);
541 EXPECT_EQ(2u, web_app_info.icons.size());
542 EXPECT_EQ(GURL(kAppIcon2), web_app_info.icons[0].url);
543 EXPECT_EQ(GURL(kAppIcon3), web_app_info.icons[1].url);
546 TEST_F(BookmarkAppHelperTest, ConstrainBitmapsToSizes) {
547 std::set<int> desired_sizes;
548 desired_sizes.insert(16);
549 desired_sizes.insert(32);
550 desired_sizes.insert(128);
551 desired_sizes.insert(256);
554 std::vector<BookmarkAppHelper::BitmapAndSource> bitmaps;
555 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(16, SK_ColorRED));
556 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(32, SK_ColorGREEN));
557 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(48, SK_ColorBLUE));
558 bitmaps.push_back(
559 CreateSquareBitmapAndSourceWithColor(144, SK_ColorYELLOW));
561 std::map<int, BookmarkAppHelper::BitmapAndSource> results(
562 BookmarkAppHelper::ConstrainBitmapsToSizes(bitmaps, desired_sizes));
564 EXPECT_EQ(3u, results.size());
565 ValidateBitmapSizeAndColor(results[16].bitmap, 16, SK_ColorRED);
566 ValidateBitmapSizeAndColor(results[32].bitmap, 32, SK_ColorGREEN);
567 ValidateBitmapSizeAndColor(results[128].bitmap, 128, SK_ColorYELLOW);
570 std::vector<BookmarkAppHelper::BitmapAndSource> bitmaps;
571 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(512, SK_ColorRED));
572 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(18, SK_ColorGREEN));
573 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(33, SK_ColorBLUE));
574 bitmaps.push_back(CreateSquareBitmapAndSourceWithColor(17, SK_ColorYELLOW));
576 std::map<int, BookmarkAppHelper::BitmapAndSource> results(
577 BookmarkAppHelper::ConstrainBitmapsToSizes(bitmaps, desired_sizes));
579 EXPECT_EQ(3u, results.size());
580 ValidateBitmapSizeAndColor(results[16].bitmap, 16, SK_ColorYELLOW);
581 ValidateBitmapSizeAndColor(results[32].bitmap, 32, SK_ColorBLUE);
582 ValidateBitmapSizeAndColor(results[256].bitmap, 256, SK_ColorRED);
586 TEST_F(BookmarkAppHelperTest, IsValidBookmarkAppUrl) {
587 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("https://www.chromium.org")));
588 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("http://www.chromium.org/path")));
589 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("ftp://www.chromium.org")));
590 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("chrome://flags")));
593 TEST_F(BookmarkAppHelperTest, IconsGeneratedOnlyWhenNoneLarger) {
594 WebApplicationInfo web_app_info;
595 std::vector<BookmarkAppHelper::BitmapAndSource> downloaded;
597 // Add three icons with a URL and bitmap. 'Download' each of them.
598 WebApplicationInfo::IconInfo icon_info =
599 CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED);
600 icon_info.url = GURL(kAppIconURL1);
601 web_app_info.icons.push_back(icon_info);
602 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
603 icon_info.url, icon_info.data));
605 icon_info = CreateIconInfoWithBitmap(kIconSizeSmallBetweenMediumAndLarge,
606 SK_ColorRED);
607 icon_info.url = GURL(kAppIconURL2);
608 web_app_info.icons.push_back(icon_info);
609 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
610 icon_info.url, icon_info.data));
612 icon_info = CreateIconInfoWithBitmap(kIconSizeLargeBetweenMediumAndLarge,
613 SK_ColorRED);
614 icon_info.url = GURL(kAppIconURL3);
615 web_app_info.icons.push_back(icon_info);
616 downloaded.push_back(BookmarkAppHelper::BitmapAndSource(
617 icon_info.url, icon_info.data));
619 // Now run the resizing and generation.
620 WebApplicationInfo new_web_app_info;
621 std::map<int, BookmarkAppHelper::BitmapAndSource> size_map =
622 BookmarkAppHelper::ResizeIconsAndGenerateMissing(downloaded,
623 TestSizesToGenerate(),
624 &new_web_app_info);
626 // Test that icons are only generated when necessary. The largest icon
627 // downloaded is smaller than EXTENSION_ICON_LARGE, so one icon should be
628 // generated.
629 ValidateOnlyGenerateIconsWhenNoLargerExists(downloaded, size_map,
630 TestSizesToGenerate(), 1);
633 TEST_F(BookmarkAppHelperTest, AllIconsGeneratedWhenOnlyASmallOneIsProvided) {
634 // When only a tiny icon is downloaded (smaller than the three desired
635 // sizes), 3 icons should be generated.
636 TestIconGeneration(kIconSizeTiny, 3);
639 TEST_F(BookmarkAppHelperTest, NoIconsGeneratedWhenAVeryLargeOneIsProvided) {
640 // When an enormous icon is provided, each desired icon size should fall back
641 // to it, and no icons should be generated.
642 TestIconGeneration(kIconSizeGigantor, 0);
645 } // namespace extensions