From 2b477e793c1a5c30253a20181aac3b8feaa017cd Mon Sep 17 00:00:00 2001 From: "calamity@chromium.org" Date: Tue, 1 Apr 2014 09:43:30 +0000 Subject: [PATCH] Add a function to create a bookmark app from a WebApplicationInfo. This CL adds a function which installs a bookmark app that is created from a WebApplicationInfo. This is part of an effort to consolidate code that deals with bookmark apps and write tests for them and is part of laying the groundwork for syncing of bookmark apps. BUG=318607 Review URL: https://codereview.chromium.org/202523011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@260823 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/extensions/bookmark_app_helper.cc | 9 ++++ chrome/browser/extensions/bookmark_app_helper.h | 5 ++ .../extensions/bookmark_app_helper_unittest.cc | 59 ++++++++++++++++++++++ .../views/extensions/bookmark_app_bubble_view.cc | 9 ++-- 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/chrome/browser/extensions/bookmark_app_helper.cc b/chrome/browser/extensions/bookmark_app_helper.cc index 24ce2f88fd00..3c5b43d02bd2 100644 --- a/chrome/browser/extensions/bookmark_app_helper.cc +++ b/chrome/browser/extensions/bookmark_app_helper.cc @@ -12,6 +12,7 @@ #include "chrome/browser/extensions/tab_helper.h" #include "chrome/common/extensions/extension_constants.h" #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" +#include "chrome/common/extensions/manifest_handlers/icons_handler.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/notification_source.h" #include "content/public/browser/web_contents.h" @@ -282,4 +283,12 @@ void BookmarkAppHelper::Observe(int type, } } +void CreateOrUpdateBookmarkApp(ExtensionService* service, + WebApplicationInfo& web_app_info) { + scoped_refptr installer( + extensions::CrxInstaller::CreateSilent(service)); + installer->set_error_on_unsupported_requirements(true); + installer->InstallWebApp(web_app_info); +} + } // namespace extensions diff --git a/chrome/browser/extensions/bookmark_app_helper.h b/chrome/browser/extensions/bookmark_app_helper.h index 6218dc3cdad0..af3045a1f393 100644 --- a/chrome/browser/extensions/bookmark_app_helper.h +++ b/chrome/browser/extensions/bookmark_app_helper.h @@ -88,6 +88,11 @@ class BookmarkAppHelper : public content::NotificationObserver { content::NotificationRegistrar registrar_; }; +// Creates or updates a bookmark app from the given |web_app_info|. Icons will +// not be downloaded so only supplied icon data will be used. +void CreateOrUpdateBookmarkApp(ExtensionService* service, + WebApplicationInfo& web_app_info); + } // namespace extensions #endif // CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_ diff --git a/chrome/browser/extensions/bookmark_app_helper_unittest.cc b/chrome/browser/extensions/bookmark_app_helper_unittest.cc index bac705e660a1..6a9d376ba8ab 100644 --- a/chrome/browser/extensions/bookmark_app_helper_unittest.cc +++ b/chrome/browser/extensions/bookmark_app_helper_unittest.cc @@ -10,6 +10,7 @@ #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" #include "chrome/common/extensions/manifest_handlers/icons_handler.h" #include "chrome/test/base/testing_profile.h" +#include "extensions/browser/extension_registry.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -18,9 +19,11 @@ namespace { #if !defined(OS_ANDROID) const char kAppUrl[] = "http://www.chromium.org"; const char kAppTitle[] = "Test title"; +const char kAlternativeAppTitle[] = "Different test title"; const char kAppDescription[] = "Test description"; const int kIconSizeSmall = extension_misc::EXTENSION_ICON_SMALL; +const int kIconSizeLarge = extension_misc::EXTENSION_ICON_LARGE; #endif class BookmarkAppHelperTest : public testing::Test { @@ -64,6 +67,16 @@ void ValidateBitmapSizeAndColor(SkBitmap bitmap, int size, SkColor color) { EXPECT_EQ(size, bitmap.height()); } +#if !defined(OS_ANDROID) +WebApplicationInfo::IconInfo CreateIconInfoWithBitmap(int size, SkColor color) { + WebApplicationInfo::IconInfo icon_info; + icon_info.width = size; + icon_info.height = size; + icon_info.data = CreateSquareBitmapWithColor(size, color); + return icon_info; +} +#endif + } // namespace namespace extensions { @@ -127,6 +140,52 @@ TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkApp) { IconsInfo::GetIconResource( extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY).empty()); } + +TEST_F(BookmarkAppHelperExtensionServiceTest, CreateAndUpdateBookmarkApp) { + EXPECT_EQ(0u, registry_->enabled_extensions().size()); + WebApplicationInfo web_app_info; + web_app_info.app_url = GURL(kAppUrl); + web_app_info.title = base::UTF8ToUTF16(kAppTitle); + web_app_info.description = base::UTF8ToUTF16(kAppDescription); + web_app_info.icons.push_back( + CreateIconInfoWithBitmap(kIconSizeSmall, SK_ColorRED)); + + extensions::CreateOrUpdateBookmarkApp(service_, web_app_info); + base::RunLoop().RunUntilIdle(); + + { + EXPECT_EQ(1u, registry_->enabled_extensions().size()); + const Extension* extension = service_->extensions()->begin()->get(); + EXPECT_TRUE(extension->from_bookmark()); + EXPECT_EQ(kAppTitle, extension->name()); + EXPECT_EQ(kAppDescription, extension->description()); + EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension)); + EXPECT_FALSE(extensions::IconsInfo::GetIconResource( + extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY) + .empty()); + } + + web_app_info.title = base::UTF8ToUTF16(kAlternativeAppTitle); + web_app_info.icons[0] = CreateIconInfoWithBitmap(kIconSizeLarge, SK_ColorRED); + + extensions::CreateOrUpdateBookmarkApp(service_, web_app_info); + base::RunLoop().RunUntilIdle(); + + { + EXPECT_EQ(1u, registry_->enabled_extensions().size()); + const Extension* extension = service_->extensions()->begin()->get(); + EXPECT_TRUE(extension->from_bookmark()); + EXPECT_EQ(kAlternativeAppTitle, extension->name()); + EXPECT_EQ(kAppDescription, extension->description()); + EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension)); + EXPECT_TRUE(extensions::IconsInfo::GetIconResource( + extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY) + .empty()); + EXPECT_FALSE(extensions::IconsInfo::GetIconResource( + extension, kIconSizeLarge, ExtensionIconSet::MATCH_EXACTLY) + .empty()); + } +} #endif TEST_F(BookmarkAppHelperTest, ConstrainBitmapsToSizes) { diff --git a/chrome/browser/ui/views/extensions/bookmark_app_bubble_view.cc b/chrome/browser/ui/views/extensions/bookmark_app_bubble_view.cc index ec96d23c5f20..b6c32eb892f8 100644 --- a/chrome/browser/ui/views/extensions/bookmark_app_bubble_view.cc +++ b/chrome/browser/ui/views/extensions/bookmark_app_bubble_view.cc @@ -7,7 +7,8 @@ #include "base/strings/string16.h" #include "base/strings/utf_string_conversions.h" #include "chrome/browser/extensions/app_icon_loader_impl.h" -#include "chrome/browser/extensions/crx_installer.h" +#include "chrome/browser/extensions/bookmark_app_helper.h" +#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/launch_util.h" #include "chrome/browser/profiles/profile.h" #include "extensions/browser/pref_names.h" @@ -257,8 +258,6 @@ void BookmarkAppBubbleView::ApplyEdits() { WebApplicationInfo install_info(web_app_info_); install_info.title = title_tf_->text(); - scoped_refptr installer( - extensions::CrxInstaller::CreateSilent(profile_->GetExtensionService())); - installer->set_error_on_unsupported_requirements(true); - installer->InstallWebApp(install_info); + extensions::CreateOrUpdateBookmarkApp(profile_->GetExtensionService(), + install_info); } -- 2.11.4.GIT