OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 #include "chrome/browser/ui/web_applications/hosted_app_tab_helper.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" |
| 11 #include "chrome/browser/extensions/crx_installer.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/extensions/test_extension_service.h" |
| 14 #include "chrome/browser/extensions/test_extension_system.h" |
| 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" |
| 17 #include "chrome/common/web_application_info.h" |
| 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_observer.h" |
| 22 #include "content/public/browser/notification_registrar.h" |
| 23 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/browser/notification_source.h" |
| 25 #include "content/public/common/favicon_url.h" |
| 26 #include "content/public/test/test_renderer_host.h" |
| 27 #include "content/public/test/test_utils.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "third_party/skia/include/core/SkBitmap.h" |
| 30 |
| 31 using content::RenderViewHostTester; |
| 32 |
| 33 namespace { |
| 34 |
| 35 // Creates valid SkBitmaps of the dimensions found in |sizes| and pushes them |
| 36 // into |bitmaps|. |
| 37 std::vector<SkBitmap> CreateTestBitmaps(const std::vector<gfx::Size>& sizes) { |
| 38 std::vector<SkBitmap> bitmaps(sizes.size()); |
| 39 for (size_t i = 0; i < sizes.size(); ++i) { |
| 40 SkBitmap& bitmap = bitmaps[i]; |
| 41 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 42 sizes[i].width(), |
| 43 sizes[i].height()); |
| 44 bitmap.allocPixels(); |
| 45 bitmap.eraseColor(SK_ColorRED); |
| 46 } |
| 47 return bitmaps; |
| 48 } |
| 49 |
| 50 class TestHostedAppTabHelper : public HostedAppTabHelper { |
| 51 public: |
| 52 explicit TestHostedAppTabHelper(content::WebContents* web_contents) |
| 53 : HostedAppTabHelper(web_contents), |
| 54 id_counter_(0) { |
| 55 } |
| 56 virtual ~TestHostedAppTabHelper() {} |
| 57 |
| 58 virtual int DownloadImage(const GURL& url) OVERRIDE { |
| 59 return id_counter_++; |
| 60 } |
| 61 |
| 62 private: |
| 63 int id_counter_; |
| 64 DISALLOW_COPY_AND_ASSIGN(TestHostedAppTabHelper); |
| 65 }; |
| 66 |
| 67 class HostedAppTabHelperTest : public ChromeRenderViewHostTestHarness, |
| 68 public content::NotificationObserver { |
| 69 protected: |
| 70 HostedAppTabHelperTest() : extension_(NULL) { |
| 71 } |
| 72 |
| 73 virtual ~HostedAppTabHelperTest() { |
| 74 } |
| 75 |
| 76 virtual void SetUp() OVERRIDE { |
| 77 ChromeRenderViewHostTestHarness::SetUp(); |
| 78 |
| 79 web_app_info_.app_url = GURL("http://www.google.com"); |
| 80 web_app_info_.title = ASCIIToUTF16("Hosted App"); |
| 81 extension_ = NULL; |
| 82 |
| 83 // A real extension service is needed for the CrxInstaller. |
| 84 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 85 static_cast<extensions::TestExtensionSystem*>( |
| 86 extensions::ExtensionSystem::Get(profile()))-> |
| 87 CreateExtensionService( |
| 88 CommandLine::ForCurrentProcess(), |
| 89 temp_dir_.path(), |
| 90 false); |
| 91 |
| 92 HostedAppTabHelper::CreateForWebContents(web_contents()); |
| 93 |
| 94 // Listen for notifications which indicate the hosted app installation has |
| 95 // finished. |
| 96 registrar_.Add(this, |
| 97 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
| 98 content::Source<extensions::CrxInstaller>(NULL)); |
| 99 |
| 100 registrar_.Add(this, |
| 101 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 102 content::NotificationService::AllSources()); |
| 103 } |
| 104 |
| 105 // Overriden from content::NotificationObserver: |
| 106 void Observe(int type, |
| 107 const content::NotificationSource& source, |
| 108 const content::NotificationDetails& details) OVERRIDE { |
| 109 if (type == chrome::NOTIFICATION_CRX_INSTALLER_DONE) |
| 110 extension_ = content::Details<const extensions::Extension>(details).ptr(); |
| 111 |
| 112 base::MessageLoop::current()->Quit(); |
| 113 } |
| 114 |
| 115 const extensions::Extension* extension() { |
| 116 return extension_; |
| 117 } |
| 118 |
| 119 WebApplicationInfo* web_app_info() { |
| 120 return &web_app_info_; |
| 121 } |
| 122 |
| 123 private: |
| 124 const extensions::Extension* extension_; |
| 125 WebApplicationInfo web_app_info_; |
| 126 content::NotificationRegistrar registrar_; |
| 127 base::ScopedTempDir temp_dir_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(HostedAppTabHelperTest); |
| 130 }; |
| 131 |
| 132 } // namespace |
| 133 |
| 134 TEST_F(HostedAppTabHelperTest, DownloadWindowIcon) { |
| 135 TestHostedAppTabHelper helper(web_contents()); |
| 136 // Make the helper fetch icons immediately. |
| 137 helper.SetDelegate(NULL); |
| 138 |
| 139 std::vector<content::FaviconURL> favicon_urls; |
| 140 favicon_urls.push_back( |
| 141 content::FaviconURL(GURL("http://www.google.com/favicon.ico"), |
| 142 content::FaviconURL::FAVICON)); |
| 143 |
| 144 // Updating the favicon should result in the download being initiated. |
| 145 helper.DidUpdateFaviconURL(0, favicon_urls); |
| 146 |
| 147 EXPECT_EQ(1, helper.pending_requests()); |
| 148 |
| 149 std::vector<gfx::Size> sizes; |
| 150 sizes.push_back(gfx::Size(32, 32)); |
| 151 helper.DidDownloadFavicon(0, 200, favicon_urls[0].icon_url, |
| 152 CreateTestBitmaps(sizes), sizes); |
| 153 EXPECT_EQ(0, helper.pending_requests()); |
| 154 |
| 155 EXPECT_EQ(1u, helper.image_family().size()); |
| 156 EXPECT_EQ(gfx::Size(32, 32), helper.image_family().GetBest(0, 0)->Size()); |
| 157 } |
| 158 |
| 159 TEST_F(HostedAppTabHelperTest, CreateHostedApp) { |
| 160 TestHostedAppTabHelper helper(web_contents()); |
| 161 |
| 162 std::vector<content::FaviconURL> favicon_urls; |
| 163 favicon_urls.push_back( |
| 164 content::FaviconURL(GURL("http://www.google.com/favicon.ico"), |
| 165 content::FaviconURL::FAVICON)); |
| 166 |
| 167 // Download should not be initiated because |fetch_icons_immediately| is not |
| 168 // set. |
| 169 helper.DidUpdateFaviconURL(0, favicon_urls); |
| 170 EXPECT_EQ(0, helper.pending_requests()); |
| 171 |
| 172 // Create hosted app which will initiate download. |
| 173 helper.CreateHostedApp(*web_app_info()); |
| 174 EXPECT_EQ(1, helper.pending_requests()); |
| 175 |
| 176 std::vector<gfx::Size> sizes(1, gfx::Size(32, 32)); |
| 177 helper.DidDownloadFavicon(0, 200, favicon_urls[0].icon_url, |
| 178 CreateTestBitmaps(sizes), sizes); |
| 179 |
| 180 // Create and install the hosted app. |
| 181 content::RunMessageLoop(); |
| 182 |
| 183 EXPECT_TRUE(extension()); |
| 184 EXPECT_TRUE(extension()->from_bookmark()); |
| 185 |
| 186 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension()); |
| 187 EXPECT_EQ(1, icons.map().size()); |
| 188 EXPECT_FALSE(icons.Get(32, ExtensionIconSet::MATCH_EXACTLY).empty()); |
| 189 } |
| 190 |
| 191 TEST_F(HostedAppTabHelperTest, CreateHostedAppBeforeFaviconURLUpdate) { |
| 192 TestHostedAppTabHelper helper(web_contents()); |
| 193 const GURL empty_favicon = GURL("http://www.google.com/empty_favicon.ico"); |
| 194 |
| 195 // This should get downloaded. |
| 196 web_app_info()->icons.push_back(WebApplicationInfo::IconInfo()); |
| 197 web_app_info()->icons[0].url = GURL("http://www.google.com/favicon2.ico"); |
| 198 |
| 199 // This is duplicated in the favicon urls and should only be downloaded once. |
| 200 web_app_info()->icons.push_back(WebApplicationInfo::IconInfo()); |
| 201 web_app_info()->icons[1].url = empty_favicon; |
| 202 |
| 203 // Initiate creation of hosted app before favicon URLs are loaded. |
| 204 helper.CreateHostedApp(*web_app_info()); |
| 205 |
| 206 std::vector<content::FaviconURL> favicon_urls; |
| 207 favicon_urls.push_back( |
| 208 content::FaviconURL(GURL("http://www.google.com/favicon.ico"), |
| 209 content::FaviconURL::FAVICON)); |
| 210 // This is duplicated in the favicon urls and should only be downloaded once. |
| 211 favicon_urls.push_back( |
| 212 content::FaviconURL(empty_favicon, |
| 213 content::FaviconURL::FAVICON)); |
| 214 // Invalid icons shouldn't get put into the download queue. |
| 215 favicon_urls.push_back( |
| 216 content::FaviconURL(GURL("http://www.google.com/invalid.ico"), |
| 217 content::FaviconURL::INVALID_ICON)); |
| 218 helper.DidUpdateFaviconURL(0, favicon_urls); |
| 219 |
| 220 std::vector<gfx::Size> sizes_1(1, gfx::Size(16, 16)); |
| 221 helper.DidDownloadFavicon(0, 200, favicon_urls[0].icon_url, |
| 222 CreateTestBitmaps(sizes_1), sizes_1); |
| 223 |
| 224 std::vector<gfx::Size> sizes_2; |
| 225 sizes_2.push_back(gfx::Size(32, 32)); |
| 226 sizes_2.push_back(gfx::Size(64, 64)); |
| 227 helper.DidDownloadFavicon(1, 200, web_app_info()->icons[0].url, |
| 228 CreateTestBitmaps(sizes), sizes_2); |
| 229 |
| 230 // Only 1 download should have been initiated for |empty_favicon| even though |
| 231 // the URL was in both the web app info and the favicon urls. |
| 232 helper.DidDownloadFavicon(2, 200, empty_favicon, |
| 233 std::vector<SkBitmap>(), std::vector<gfx::Size>()); |
| 234 |
| 235 // Create and install the hosted app. |
| 236 content::RunMessageLoop(); |
| 237 |
| 238 EXPECT_TRUE(extension()); |
| 239 EXPECT_TRUE(extension()->from_bookmark()); |
| 240 |
| 241 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension()); |
| 242 EXPECT_EQ(3, icons.map().size()); |
| 243 EXPECT_FALSE(icons.Get(16, ExtensionIconSet::MATCH_EXACTLY).empty()); |
| 244 EXPECT_FALSE(icons.Get(32, ExtensionIconSet::MATCH_EXACTLY).empty()); |
| 245 EXPECT_FALSE(icons.Get(64, ExtensionIconSet::MATCH_EXACTLY).empty()); |
| 246 } |
OLD | NEW |