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()); | |
benwells
2013/11/25 00:40:16
Why is this created here, when there are Test ones
calamity
2013/11/29 05:45:36
Removed.
| |
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, CreateHostedApp) { | |
135 TestHostedAppTabHelper helper(web_contents()); | |
136 | |
137 std::vector<content::FaviconURL> favicon_urls; | |
138 favicon_urls.push_back( | |
139 content::FaviconURL(GURL("http://www.google.com/favicon.ico"), | |
140 content::FaviconURL::FAVICON)); | |
141 | |
142 // Download should not be initiated because |fetch_icons_immediately| is not | |
benwells
2013/11/25 00:40:16
This comment is out of date.
calamity
2013/11/29 05:45:36
Done.
| |
143 // set. | |
144 helper.DidUpdateFaviconURL(0, favicon_urls); | |
145 EXPECT_EQ(0, helper.pending_requests()); | |
146 | |
147 // Create hosted app which will initiate download. | |
148 helper.CreateHostedApp(*web_app_info()); | |
149 EXPECT_EQ(1, helper.pending_requests()); | |
150 | |
151 std::vector<gfx::Size> sizes(1, gfx::Size(32, 32)); | |
152 helper.DidDownloadFavicon(0, 200, favicon_urls[0].icon_url, | |
153 CreateTestBitmaps(sizes), sizes); | |
154 | |
155 // Create and install the hosted app. | |
156 content::RunMessageLoop(); | |
157 | |
158 EXPECT_TRUE(extension()); | |
159 EXPECT_TRUE(extension()->from_bookmark()); | |
160 | |
161 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension()); | |
162 EXPECT_EQ(1, icons.map().size()); | |
163 EXPECT_FALSE(icons.Get(32, ExtensionIconSet::MATCH_EXACTLY).empty()); | |
164 } | |
165 | |
166 TEST_F(HostedAppTabHelperTest, CreateHostedAppBeforeFaviconURLUpdate) { | |
167 TestHostedAppTabHelper helper(web_contents()); | |
168 const GURL empty_favicon = GURL("http://www.google.com/empty_favicon.ico"); | |
169 | |
170 // This should get downloaded. | |
171 web_app_info()->icons.push_back(WebApplicationInfo::IconInfo()); | |
172 web_app_info()->icons[0].url = GURL("http://www.google.com/favicon2.ico"); | |
173 | |
174 // This is duplicated in the favicon urls and should only be downloaded once. | |
175 web_app_info()->icons.push_back(WebApplicationInfo::IconInfo()); | |
176 web_app_info()->icons[1].url = empty_favicon; | |
177 | |
178 // Initiate creation of hosted app before favicon URLs are loaded. | |
179 helper.CreateHostedApp(*web_app_info()); | |
180 | |
181 std::vector<content::FaviconURL> favicon_urls; | |
182 favicon_urls.push_back( | |
183 content::FaviconURL(GURL("http://www.google.com/favicon.ico"), | |
184 content::FaviconURL::FAVICON)); | |
185 // This is duplicated in the favicon urls and should only be downloaded once. | |
186 favicon_urls.push_back( | |
187 content::FaviconURL(empty_favicon, | |
188 content::FaviconURL::FAVICON)); | |
189 // Invalid icons shouldn't get put into the download queue. | |
190 favicon_urls.push_back( | |
191 content::FaviconURL(GURL("http://www.google.com/invalid.ico"), | |
192 content::FaviconURL::INVALID_ICON)); | |
193 helper.DidUpdateFaviconURL(0, favicon_urls); | |
benwells
2013/11/25 00:40:16
I think it would be clearer to check the number of
calamity
2013/11/29 05:45:36
Done.
| |
194 | |
195 std::vector<gfx::Size> sizes_1(1, gfx::Size(16, 16)); | |
196 helper.DidDownloadFavicon(0, 200, favicon_urls[0].icon_url, | |
197 CreateTestBitmaps(sizes_1), sizes_1); | |
198 | |
199 std::vector<gfx::Size> sizes_2; | |
200 sizes_2.push_back(gfx::Size(32, 32)); | |
201 sizes_2.push_back(gfx::Size(64, 64)); | |
202 helper.DidDownloadFavicon(1, 200, web_app_info()->icons[0].url, | |
203 CreateTestBitmaps(sizes_2), sizes_2); | |
204 | |
205 // Only 1 download should have been initiated for |empty_favicon| even though | |
206 // the URL was in both the web app info and the favicon urls. | |
207 helper.DidDownloadFavicon(2, 200, empty_favicon, | |
208 std::vector<SkBitmap>(), std::vector<gfx::Size>()); | |
209 | |
210 // Create and install the hosted app. | |
211 content::RunMessageLoop(); | |
212 | |
213 EXPECT_TRUE(extension()); | |
214 EXPECT_TRUE(extension()->from_bookmark()); | |
215 | |
216 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension()); | |
217 EXPECT_EQ(3, icons.map().size()); | |
218 EXPECT_FALSE(icons.Get(16, ExtensionIconSet::MATCH_EXACTLY).empty()); | |
219 EXPECT_FALSE(icons.Get(32, ExtensionIconSet::MATCH_EXACTLY).empty()); | |
220 EXPECT_FALSE(icons.Get(64, ExtensionIconSet::MATCH_EXACTLY).empty()); | |
221 } | |
OLD | NEW |