OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/extensions/bookmark_app_helper.h" | 5 #include "chrome/browser/extensions/bookmark_app_helper.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/extensions/extension_service_test_base.h" | 9 #include "chrome/browser/extensions/extension_service_test_base.h" |
10 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 10 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
11 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
12 #include "content/public/browser/render_process_host.h" | 12 #include "content/public/browser/render_process_host.h" |
13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
14 #include "content/public/test/web_contents_tester.h" | 14 #include "content/public/test/web_contents_tester.h" |
15 #include "extensions/browser/extension_registry.h" | 15 #include "extensions/browser/extension_registry.h" |
16 #include "extensions/common/constants.h" | 16 #include "extensions/common/constants.h" |
17 #include "extensions/common/extension_icon_set.h" | 17 #include "extensions/common/extension_icon_set.h" |
18 #include "extensions/common/manifest_handlers/icons_handler.h" | 18 #include "extensions/common/manifest_handlers/icons_handler.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
21 #include "ui/gfx/skia_util.h" | 21 #include "ui/gfx/skia_util.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const char kAppUrl[] = "http://www.chromium.org"; | 25 const char kAppUrl[] = "http://www.chromium.org"; |
| 26 const char kAlternativeAppUrl[] = "http://www.notchromium.org"; |
26 const char kAppTitle[] = "Test title"; | 27 const char kAppTitle[] = "Test title"; |
| 28 const char kAppShortName[] = "Test short name"; |
27 const char kAlternativeAppTitle[] = "Different test title"; | 29 const char kAlternativeAppTitle[] = "Different test title"; |
28 const char kAppDescription[] = "Test description"; | 30 const char kAppDescription[] = "Test description"; |
29 | 31 |
30 const int kIconSizeTiny = extension_misc::EXTENSION_ICON_BITTY; | 32 const int kIconSizeTiny = extension_misc::EXTENSION_ICON_BITTY; |
31 const int kIconSizeSmall = extension_misc::EXTENSION_ICON_SMALL; | 33 const int kIconSizeSmall = extension_misc::EXTENSION_ICON_SMALL; |
32 const int kIconSizeMedium = extension_misc::EXTENSION_ICON_MEDIUM; | 34 const int kIconSizeMedium = extension_misc::EXTENSION_ICON_MEDIUM; |
33 const int kIconSizeLarge = extension_misc::EXTENSION_ICON_LARGE; | 35 const int kIconSizeLarge = extension_misc::EXTENSION_ICON_LARGE; |
34 | 36 |
35 class BookmarkAppHelperTest : public testing::Test { | 37 class BookmarkAppHelperTest : public testing::Test { |
36 public: | 38 public: |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 content::WebContents* contents) | 124 content::WebContents* contents) |
123 : BookmarkAppHelper(service, web_app_info, contents) {} | 125 : BookmarkAppHelper(service, web_app_info, contents) {} |
124 | 126 |
125 virtual ~TestBookmarkAppHelper() {} | 127 virtual ~TestBookmarkAppHelper() {} |
126 | 128 |
127 void CreationComplete(const extensions::Extension* extension, | 129 void CreationComplete(const extensions::Extension* extension, |
128 const WebApplicationInfo& web_app_info) { | 130 const WebApplicationInfo& web_app_info) { |
129 extension_ = extension; | 131 extension_ = extension; |
130 } | 132 } |
131 | 133 |
| 134 void CompleteGetManifest(const content::Manifest& manifest) { |
| 135 BookmarkAppHelper::OnDidGetManifest(manifest); |
| 136 } |
| 137 |
132 void CompleteIconDownload( | 138 void CompleteIconDownload( |
133 bool success, | 139 bool success, |
134 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) { | 140 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) { |
135 BookmarkAppHelper::OnIconsDownloaded(success, bitmaps); | 141 BookmarkAppHelper::OnIconsDownloaded(success, bitmaps); |
136 } | 142 } |
137 | 143 |
138 const Extension* extension() { return extension_; } | 144 const Extension* extension() { return extension_; } |
139 | 145 |
140 private: | 146 private: |
141 const Extension* extension_; | 147 const Extension* extension_; |
(...skipping 26 matching lines...) Expand all Loading... |
168 EXPECT_EQ(1u, service_->extensions()->size()); | 174 EXPECT_EQ(1u, service_->extensions()->size()); |
169 EXPECT_TRUE(extension->from_bookmark()); | 175 EXPECT_TRUE(extension->from_bookmark()); |
170 EXPECT_EQ(kAppTitle, extension->name()); | 176 EXPECT_EQ(kAppTitle, extension->name()); |
171 EXPECT_EQ(kAppDescription, extension->description()); | 177 EXPECT_EQ(kAppDescription, extension->description()); |
172 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension)); | 178 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension)); |
173 EXPECT_FALSE( | 179 EXPECT_FALSE( |
174 IconsInfo::GetIconResource( | 180 IconsInfo::GetIconResource( |
175 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY).empty()); | 181 extension, kIconSizeSmall, ExtensionIconSet::MATCH_EXACTLY).empty()); |
176 } | 182 } |
177 | 183 |
| 184 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkAppWithManifest) { |
| 185 WebApplicationInfo web_app_info; |
| 186 |
| 187 scoped_ptr<content::WebContents> contents( |
| 188 content::WebContentsTester::CreateTestWebContents(profile_.get(), NULL)); |
| 189 TestBookmarkAppHelper helper(service_, web_app_info, contents.get()); |
| 190 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete, |
| 191 base::Unretained(&helper))); |
| 192 |
| 193 content::Manifest manifest; |
| 194 manifest.start_url = GURL(kAppUrl); |
| 195 manifest.name = base::NullableString16(base::UTF8ToUTF16(kAppTitle), false); |
| 196 helper.CompleteGetManifest(manifest); |
| 197 |
| 198 std::map<GURL, std::vector<SkBitmap> > icon_map; |
| 199 helper.CompleteIconDownload(true, icon_map); |
| 200 |
| 201 base::RunLoop().RunUntilIdle(); |
| 202 EXPECT_TRUE(helper.extension()); |
| 203 const Extension* extension = |
| 204 service_->GetInstalledExtension(helper.extension()->id()); |
| 205 EXPECT_TRUE(extension); |
| 206 EXPECT_EQ(1u, service_->extensions()->size()); |
| 207 EXPECT_TRUE(extension->from_bookmark()); |
| 208 EXPECT_EQ(kAppTitle, extension->name()); |
| 209 EXPECT_EQ(GURL(kAppUrl), AppLaunchInfo::GetLaunchWebURL(extension)); |
| 210 } |
| 211 |
178 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkAppNoContents) { | 212 TEST_F(BookmarkAppHelperExtensionServiceTest, CreateBookmarkAppNoContents) { |
179 WebApplicationInfo web_app_info; | 213 WebApplicationInfo web_app_info; |
180 web_app_info.app_url = GURL(kAppUrl); | 214 web_app_info.app_url = GURL(kAppUrl); |
181 web_app_info.title = base::UTF8ToUTF16(kAppTitle); | 215 web_app_info.title = base::UTF8ToUTF16(kAppTitle); |
182 web_app_info.description = base::UTF8ToUTF16(kAppDescription); | 216 web_app_info.description = base::UTF8ToUTF16(kAppDescription); |
183 web_app_info.icons.push_back( | 217 web_app_info.icons.push_back( |
184 CreateIconInfoWithBitmap(kIconSizeTiny, SK_ColorRED)); | 218 CreateIconInfoWithBitmap(kIconSizeTiny, SK_ColorRED)); |
185 | 219 |
186 TestBookmarkAppHelper helper(service_, web_app_info, NULL); | 220 TestBookmarkAppHelper helper(service_, web_app_info, NULL); |
187 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete, | 221 helper.Create(base::Bind(&TestBookmarkAppHelper::CreationComplete, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 EXPECT_EQ(1u, registry()->enabled_extensions().size()); | 313 EXPECT_EQ(1u, registry()->enabled_extensions().size()); |
280 base::RunLoop run_loop; | 314 base::RunLoop run_loop; |
281 extensions::GetWebApplicationInfoFromApp( | 315 extensions::GetWebApplicationInfoFromApp( |
282 profile_.get(), | 316 profile_.get(), |
283 service_->extensions()->begin()->get(), | 317 service_->extensions()->begin()->get(), |
284 base::Bind( | 318 base::Bind( |
285 &ValidateWebApplicationInfo, run_loop.QuitClosure(), web_app_info)); | 319 &ValidateWebApplicationInfo, run_loop.QuitClosure(), web_app_info)); |
286 run_loop.Run(); | 320 run_loop.Run(); |
287 } | 321 } |
288 | 322 |
| 323 TEST_F(BookmarkAppHelperTest, UpdateWebAppInfoFromManifest) { |
| 324 WebApplicationInfo web_app_info; |
| 325 web_app_info.title = base::UTF8ToUTF16(kAlternativeAppTitle); |
| 326 web_app_info.app_url = GURL(kAlternativeAppUrl); |
| 327 |
| 328 content::Manifest manifest; |
| 329 manifest.start_url = GURL(kAppUrl); |
| 330 manifest.short_name = base::NullableString16(base::UTF8ToUTF16(kAppShortName), |
| 331 false); |
| 332 |
| 333 BookmarkAppHelper::UpdateWebAppInfoFromManifest(manifest, &web_app_info); |
| 334 EXPECT_EQ(base::UTF8ToUTF16(kAppShortName), web_app_info.title); |
| 335 EXPECT_EQ(GURL(kAppUrl), web_app_info.app_url); |
| 336 |
| 337 // Test that |manifest.name| takes priority over |manifest.short_name| |
| 338 manifest.name = base::NullableString16(base::UTF8ToUTF16(kAppTitle), false); |
| 339 BookmarkAppHelper::UpdateWebAppInfoFromManifest(manifest, &web_app_info); |
| 340 EXPECT_EQ(base::UTF8ToUTF16(kAppTitle), web_app_info.title); |
| 341 } |
| 342 |
289 TEST_F(BookmarkAppHelperTest, ConstrainBitmapsToSizes) { | 343 TEST_F(BookmarkAppHelperTest, ConstrainBitmapsToSizes) { |
290 std::set<int> desired_sizes; | 344 std::set<int> desired_sizes; |
291 desired_sizes.insert(16); | 345 desired_sizes.insert(16); |
292 desired_sizes.insert(32); | 346 desired_sizes.insert(32); |
293 desired_sizes.insert(128); | 347 desired_sizes.insert(128); |
294 desired_sizes.insert(256); | 348 desired_sizes.insert(256); |
295 | 349 |
296 { | 350 { |
297 std::vector<SkBitmap> bitmaps; | 351 std::vector<SkBitmap> bitmaps; |
298 bitmaps.push_back(CreateSquareBitmapWithColor(16, SK_ColorRED)); | 352 bitmaps.push_back(CreateSquareBitmapWithColor(16, SK_ColorRED)); |
(...skipping 27 matching lines...) Expand all Loading... |
326 } | 380 } |
327 | 381 |
328 TEST_F(BookmarkAppHelperTest, IsValidBookmarkAppUrl) { | 382 TEST_F(BookmarkAppHelperTest, IsValidBookmarkAppUrl) { |
329 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("https://www.chromium.org"))); | 383 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("https://www.chromium.org"))); |
330 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("http://www.chromium.org/path"))); | 384 EXPECT_TRUE(IsValidBookmarkAppUrl(GURL("http://www.chromium.org/path"))); |
331 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("ftp://www.chromium.org"))); | 385 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("ftp://www.chromium.org"))); |
332 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("chrome://flags"))); | 386 EXPECT_FALSE(IsValidBookmarkAppUrl(GURL("chrome://flags"))); |
333 } | 387 } |
334 | 388 |
335 } // namespace extensions | 389 } // namespace extensions |
OLD | NEW |