OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/android/webapk/webapk_installer.h" | 5 #include "chrome/browser/android/webapk/webapk_installer.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... | |
33 #include "url/gurl.h" | 33 #include "url/gurl.h" |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const base::FilePath::CharType kTestDataDir[] = | 37 const base::FilePath::CharType kTestDataDir[] = |
38 FILE_PATH_LITERAL("chrome/test/data"); | 38 FILE_PATH_LITERAL("chrome/test/data"); |
39 | 39 |
40 // URL of mock WebAPK server. | 40 // URL of mock WebAPK server. |
41 const char* kServerUrl = "/webapkserver/"; | 41 const char* kServerUrl = "/webapkserver/"; |
42 | 42 |
43 // The best primary icon URL from Web Manifest. We use a random file in the test | 43 // The URLs of best icons from Web Manifest. We use a random file in the test |
44 // data directory. Since WebApkInstaller does not try to decode the file as an | 44 // data directory. Since WebApkInstaller does not try to decode the file as an |
45 // image it is OK that the file is not an image. | 45 // image it is OK that the file is not an image. |
46 const char* kBestPrimaryIconUrl = "/simple.html"; | 46 const char* kBestPrimaryIconUrl = "/simple.html"; |
47 const char* kBestBadgeIconUrl = "/nostore.html"; | |
47 | 48 |
48 // URL of file to download from the WebAPK server. We use a random file in the | 49 // URL of file to download from the WebAPK server. We use a random file in the |
49 // test data directory. | 50 // test data directory. |
50 const char* kDownloadUrl = "/simple.html"; | 51 const char* kDownloadUrl = "/simple.html"; |
51 | 52 |
52 // The package name of the downloaded WebAPK. | 53 // The package name of the downloaded WebAPK. |
53 const char* kDownloadedWebApkPackageName = "party.unicode"; | 54 const char* kDownloadedWebApkPackageName = "party.unicode"; |
54 | 55 |
55 // WebApkInstaller subclass where | 56 // WebApkInstaller subclass where |
56 // WebApkInstaller::StartInstallingDownloadedWebApk() and | 57 // WebApkInstaller::StartInstallingDownloadedWebApk() and |
57 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() and | 58 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() and |
58 // WebApkInstaller::CanUseGooglePlayInstallService() and | 59 // WebApkInstaller::CanUseGooglePlayInstallService() and |
59 // WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay() are stubbed out. | 60 // WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay() are stubbed out. |
60 class TestWebApkInstaller : public WebApkInstaller { | 61 class TestWebApkInstaller : public WebApkInstaller { |
61 public: | 62 public: |
62 TestWebApkInstaller(content::BrowserContext* browser_context, | 63 TestWebApkInstaller(content::BrowserContext* browser_context, |
63 const ShortcutInfo& shortcut_info, | 64 const ShortcutInfo& shortcut_info, |
64 const SkBitmap& shortcut_icon, | 65 const SkBitmap& primary_icon, |
66 const SkBitmap& badge_icon, | |
65 bool can_install_webapks) | 67 bool can_install_webapks) |
66 : WebApkInstaller(browser_context, shortcut_info, shortcut_icon), | 68 : WebApkInstaller(browser_context, |
69 shortcut_info, | |
70 primary_icon, | |
71 badge_icon), | |
67 can_install_webapks_(can_install_webapks) {} | 72 can_install_webapks_(can_install_webapks) {} |
68 | 73 |
69 bool CanInstallWebApks() override { return can_install_webapks_; } | 74 bool CanInstallWebApks() override { return can_install_webapks_; } |
70 | 75 |
71 void InstallOrUpdateWebApk(const std::string& package_name, | 76 void InstallOrUpdateWebApk(const std::string& package_name, |
72 int version, | 77 int version, |
73 const std::string& token) override { | 78 const std::string& token) override { |
74 PostTaskToRunSuccessCallback(); | 79 PostTaskToRunSuccessCallback(); |
75 } | 80 } |
76 | 81 |
77 void PostTaskToRunSuccessCallback() { | 82 void PostTaskToRunSuccessCallback() { |
78 base::ThreadTaskRunnerHandle::Get()->PostTask( | 83 base::ThreadTaskRunnerHandle::Get()->PostTask( |
79 FROM_HERE, | 84 FROM_HERE, |
80 base::Bind(&TestWebApkInstaller::OnResult, base::Unretained(this), | 85 base::Bind(&TestWebApkInstaller::OnResult, base::Unretained(this), |
81 WebApkInstallResult::SUCCESS)); | 86 WebApkInstallResult::SUCCESS)); |
82 } | 87 } |
83 | 88 |
84 private: | 89 private: |
85 // Whether the Google Play Services can be used and the install delegate is | 90 // Whether the Google Play Services can be used and the install delegate is |
86 // available. | 91 // available. |
87 bool can_install_webapks_; | 92 bool can_install_webapks_; |
88 | 93 |
89 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); | 94 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); |
90 }; | 95 }; |
91 | 96 |
92 // Runs the WebApkInstaller installation process/update and blocks till done. | 97 // Runs the WebApkInstaller installation process/update and blocks till done. |
93 class WebApkInstallerRunner { | 98 class WebApkInstallerRunner { |
94 public: | 99 public: |
95 WebApkInstallerRunner(content::BrowserContext* browser_context, | 100 WebApkInstallerRunner(content::BrowserContext* browser_context, |
96 const GURL& best_primary_icon_url) | 101 const GURL& best_primary_icon_url, |
102 const GURL& best_badge_icon_url) | |
97 : browser_context_(browser_context), | 103 : browser_context_(browser_context), |
98 best_primary_icon_url_(best_primary_icon_url), | 104 best_primary_icon_url_(best_primary_icon_url), |
105 best_badge_icon_url_(best_badge_icon_url), | |
99 can_install_webapks_(true) {} | 106 can_install_webapks_(true) {} |
100 | 107 |
101 ~WebApkInstallerRunner() {} | 108 ~WebApkInstallerRunner() {} |
102 | 109 |
103 void SetCanInstallWebApks(bool can_install_webapks) { | 110 void SetCanInstallWebApks(bool can_install_webapks) { |
104 can_install_webapks_ = can_install_webapks; | 111 can_install_webapks_ = can_install_webapks; |
105 } | 112 } |
106 | 113 |
107 void RunInstallWebApk() { | 114 void RunInstallWebApk() { |
108 WebApkInstaller::InstallAsyncForTesting( | 115 WebApkInstaller::InstallAsyncForTesting( |
109 CreateWebApkInstaller(), base::Bind(&WebApkInstallerRunner::OnCompleted, | 116 CreateWebApkInstaller(), base::Bind(&WebApkInstallerRunner::OnCompleted, |
110 base::Unretained(this))); | 117 base::Unretained(this))); |
111 Run(); | 118 Run(); |
112 } | 119 } |
113 | 120 |
114 void RunUpdateWebApk() { | 121 void RunUpdateWebApk() { |
115 const int kWebApkVersion = 1; | 122 const int kWebApkVersion = 1; |
116 | 123 |
117 std::map<std::string, std::string> icon_url_to_murmur2_hash{ | 124 std::map<std::string, std::string> icon_url_to_murmur2_hash{ |
118 {best_primary_icon_url_.spec(), "0"}}; | 125 {best_primary_icon_url_.spec(), "0"}, |
126 {best_badge_icon_url_.spec(), "0"}}; | |
119 | 127 |
120 WebApkInstaller::UpdateAsyncForTesting( | 128 WebApkInstaller::UpdateAsyncForTesting( |
121 CreateWebApkInstaller(), kDownloadedWebApkPackageName, kWebApkVersion, | 129 CreateWebApkInstaller(), kDownloadedWebApkPackageName, kWebApkVersion, |
122 icon_url_to_murmur2_hash, false /* is_manifest_stale */, | 130 icon_url_to_murmur2_hash, false /* is_manifest_stale */, |
123 base::Bind(&WebApkInstallerRunner::OnCompleted, | 131 base::Bind(&WebApkInstallerRunner::OnCompleted, |
124 base::Unretained(this))); | 132 base::Unretained(this))); |
125 Run(); | 133 Run(); |
126 } | 134 } |
127 | 135 |
128 WebApkInstaller* CreateWebApkInstaller() { | 136 WebApkInstaller* CreateWebApkInstaller() { |
129 ShortcutInfo info(GURL::EmptyGURL()); | 137 ShortcutInfo info(GURL::EmptyGURL()); |
130 info.best_primary_icon_url = best_primary_icon_url_; | 138 info.best_primary_icon_url = best_primary_icon_url_; |
139 info.best_badge_icon_url = best_badge_icon_url_; | |
131 | 140 |
132 // WebApkInstaller owns itself. | 141 // WebApkInstaller owns itself. |
133 WebApkInstaller* installer = new TestWebApkInstaller( | 142 WebApkInstaller* installer = new TestWebApkInstaller( |
134 browser_context_, info, SkBitmap(), can_install_webapks_); | 143 browser_context_, info, SkBitmap(), SkBitmap(), can_install_webapks_); |
135 installer->SetTimeoutMs(100); | 144 installer->SetTimeoutMs(100); |
136 return installer; | 145 return installer; |
137 } | 146 } |
138 | 147 |
139 void Run() { | 148 void Run() { |
140 base::RunLoop run_loop; | 149 base::RunLoop run_loop; |
141 on_completed_callback_ = run_loop.QuitClosure(); | 150 on_completed_callback_ = run_loop.QuitClosure(); |
142 run_loop.Run(); | 151 run_loop.Run(); |
143 } | 152 } |
144 | 153 |
145 WebApkInstallResult result() { return result_; } | 154 WebApkInstallResult result() { return result_; } |
146 | 155 |
147 private: | 156 private: |
148 void OnCompleted(WebApkInstallResult result, | 157 void OnCompleted(WebApkInstallResult result, |
149 bool relax_updates, | 158 bool relax_updates, |
150 const std::string& webapk_package) { | 159 const std::string& webapk_package) { |
151 result_ = result; | 160 result_ = result; |
152 on_completed_callback_.Run(); | 161 on_completed_callback_.Run(); |
153 } | 162 } |
154 | 163 |
155 content::BrowserContext* browser_context_; | 164 content::BrowserContext* browser_context_; |
156 | 165 |
157 // The Web Manifest's icon URL. | 166 // The Web Manifest's icon URLs. |
158 const GURL best_primary_icon_url_; | 167 const GURL best_primary_icon_url_; |
168 const GURL best_badge_icon_url_; | |
159 | 169 |
160 // Called after the installation process has succeeded or failed. | 170 // Called after the installation process has succeeded or failed. |
161 base::Closure on_completed_callback_; | 171 base::Closure on_completed_callback_; |
162 | 172 |
163 // The result of the installation process. | 173 // The result of the installation process. |
164 WebApkInstallResult result_; | 174 WebApkInstallResult result_; |
165 | 175 |
166 // Whether the device supports installation of WebApks. | 176 // Whether the device supports installation of WebApks. |
167 bool can_install_webapks_; | 177 bool can_install_webapks_; |
168 | 178 |
(...skipping 21 matching lines...) Expand all Loading... | |
190 // Builds WebApk proto and blocks till done. | 200 // Builds WebApk proto and blocks till done. |
191 class BuildProtoRunner { | 201 class BuildProtoRunner { |
192 public: | 202 public: |
193 explicit BuildProtoRunner(content::BrowserContext* browser_context) | 203 explicit BuildProtoRunner(content::BrowserContext* browser_context) |
194 : browser_context_(browser_context) {} | 204 : browser_context_(browser_context) {} |
195 | 205 |
196 ~BuildProtoRunner() {} | 206 ~BuildProtoRunner() {} |
197 | 207 |
198 void BuildSync( | 208 void BuildSync( |
199 const GURL& best_primary_icon_url, | 209 const GURL& best_primary_icon_url, |
210 const GURL& best_badge_icon_url, | |
200 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 211 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
201 bool is_manifest_stale) { | 212 bool is_manifest_stale) { |
202 ShortcutInfo info(GURL::EmptyGURL()); | 213 ShortcutInfo info(GURL::EmptyGURL()); |
203 info.best_primary_icon_url = best_primary_icon_url; | 214 info.best_primary_icon_url = best_primary_icon_url; |
215 info.best_badge_icon_url = best_badge_icon_url; | |
204 | 216 |
205 // WebApkInstaller owns itself. | 217 // WebApkInstaller owns itself. |
206 WebApkInstaller* installer = | 218 WebApkInstaller* installer = new TestWebApkInstaller( |
207 new TestWebApkInstaller(browser_context_, info, SkBitmap(), false); | 219 browser_context_, info, SkBitmap(), SkBitmap(), false); |
208 installer->BuildWebApkProtoInBackgroundForTesting( | 220 installer->BuildWebApkProtoInBackgroundForTesting( |
209 base::Bind(&BuildProtoRunner::OnBuiltWebApkProto, | 221 base::Bind(&BuildProtoRunner::OnBuiltWebApkProto, |
210 base::Unretained(this)), | 222 base::Unretained(this)), |
211 icon_url_to_murmur2_hash, is_manifest_stale); | 223 icon_url_to_murmur2_hash, is_manifest_stale); |
212 | 224 |
213 base::RunLoop run_loop; | 225 base::RunLoop run_loop; |
214 on_completed_callback_ = run_loop.QuitClosure(); | 226 on_completed_callback_ = run_loop.QuitClosure(); |
215 run_loop.Run(); | 227 run_loop.Run(); |
216 } | 228 } |
217 | 229 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 void TearDown() override { | 272 void TearDown() override { |
261 profile_.reset(); | 273 profile_.reset(); |
262 base::RunLoop().RunUntilIdle(); | 274 base::RunLoop().RunUntilIdle(); |
263 } | 275 } |
264 | 276 |
265 // Sets the best Web Manifest's primary icon URL. | 277 // Sets the best Web Manifest's primary icon URL. |
266 void SetBestPrimaryIconUrl(const GURL& best_primary_icon_url) { | 278 void SetBestPrimaryIconUrl(const GURL& best_primary_icon_url) { |
267 best_primary_icon_url_ = best_primary_icon_url; | 279 best_primary_icon_url_ = best_primary_icon_url; |
268 } | 280 } |
269 | 281 |
282 // Sets the best Web Manifest's badge icon URL. | |
283 void SetBestBadgeIconUrl(const GURL& best_badge_icon_url) { | |
284 best_badge_icon_url_ = best_badge_icon_url; | |
285 } | |
286 | |
270 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller | 287 // Sets the URL to send the webapk::CreateWebApkRequest to. WebApkInstaller |
271 // should fail if the URL is not |kServerUrl|. | 288 // should fail if the URL is not |kServerUrl|. |
272 void SetWebApkServerUrl(const GURL& server_url) { | 289 void SetWebApkServerUrl(const GURL& server_url) { |
273 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 290 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
274 switches::kWebApkServerUrl, server_url.spec()); | 291 switches::kWebApkServerUrl, server_url.spec()); |
275 } | 292 } |
276 | 293 |
277 // Sets the function that should be used to build the response to the | 294 // Sets the function that should be used to build the response to the |
278 // WebAPK creation request. | 295 // WebAPK creation request. |
279 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { | 296 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { |
280 webapk_response_builder_ = builder; | 297 webapk_response_builder_ = builder; |
281 } | 298 } |
282 | 299 |
283 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { | 300 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { |
284 return std::unique_ptr<WebApkInstallerRunner>( | 301 return std::unique_ptr<WebApkInstallerRunner>(new WebApkInstallerRunner( |
285 new WebApkInstallerRunner(profile_.get(), best_primary_icon_url_)); | 302 profile_.get(), best_primary_icon_url_, best_badge_icon_url_)); |
286 } | 303 } |
287 | 304 |
288 std::unique_ptr<BuildProtoRunner> CreateBuildProtoRunner() { | 305 std::unique_ptr<BuildProtoRunner> CreateBuildProtoRunner() { |
289 return std::unique_ptr<BuildProtoRunner>( | 306 return std::unique_ptr<BuildProtoRunner>( |
290 new BuildProtoRunner(profile_.get())); | 307 new BuildProtoRunner(profile_.get())); |
291 } | 308 } |
292 | 309 |
293 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } | 310 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } |
294 | 311 |
295 private: | 312 private: |
296 // Sets default configuration for running WebApkInstaller. | 313 // Sets default configuration for running WebApkInstaller. |
297 void SetDefaults() { | 314 void SetDefaults() { |
298 GURL best_primary_icon_url = test_server_.GetURL(kBestPrimaryIconUrl); | 315 SetBestPrimaryIconUrl(test_server_.GetURL(kBestPrimaryIconUrl)); |
299 SetBestPrimaryIconUrl(best_primary_icon_url); | 316 SetBestBadgeIconUrl(test_server_.GetURL(kBestBadgeIconUrl)); |
300 GURL server_url = test_server_.GetURL(kServerUrl); | 317 SetWebApkServerUrl(test_server_.GetURL(kServerUrl)); |
301 SetWebApkServerUrl(server_url); | 318 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, |
302 GURL download_url = test_server_.GetURL(kDownloadUrl); | 319 test_server_.GetURL(kDownloadUrl))); |
303 SetWebApkResponseBuilder( | |
304 base::Bind(&BuildValidWebApkResponse, download_url)); | |
305 } | 320 } |
306 | 321 |
307 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( | 322 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( |
308 const net::test_server::HttpRequest& request) { | 323 const net::test_server::HttpRequest& request) { |
309 return (request.relative_url == kServerUrl) | 324 return (request.relative_url == kServerUrl) |
310 ? webapk_response_builder_.Run() | 325 ? webapk_response_builder_.Run() |
311 : std::unique_ptr<net::test_server::HttpResponse>(); | 326 : std::unique_ptr<net::test_server::HttpResponse>(); |
312 } | 327 } |
313 | 328 |
314 std::unique_ptr<TestingProfile> profile_; | 329 std::unique_ptr<TestingProfile> profile_; |
315 content::TestBrowserThreadBundle thread_bundle_; | 330 content::TestBrowserThreadBundle thread_bundle_; |
316 net::EmbeddedTestServer test_server_; | 331 net::EmbeddedTestServer test_server_; |
317 | 332 |
318 // Web Manifest's icon URL. | 333 // Web Manifest's icon URLs. |
319 GURL best_primary_icon_url_; | 334 GURL best_primary_icon_url_; |
335 GURL best_badge_icon_url_; | |
320 | 336 |
321 // Builds response to the WebAPK creation request. | 337 // Builds response to the WebAPK creation request. |
322 WebApkResponseBuilder webapk_response_builder_; | 338 WebApkResponseBuilder webapk_response_builder_; |
323 | 339 |
324 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); | 340 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); |
325 }; | 341 }; |
326 | 342 |
327 // Test installation succeeding. | 343 // Test installation succeeding. |
328 TEST_F(WebApkInstallerTest, Success) { | 344 TEST_F(WebApkInstallerTest, Success) { |
329 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 345 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
330 runner->RunInstallWebApk(); | 346 runner->RunInstallWebApk(); |
331 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); | 347 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); |
332 } | 348 } |
333 | 349 |
334 // Test that installation fails if fetching the bitmap at the best primary icon | 350 // Test that installation fails if fetching the bitmap at the best primary icon |
335 // URL times out. In a perfect world the fetch would never time out because the | 351 // URL times out. In a perfect world the fetch would never time out because the |
336 // bitmap at the best primary icon URL should be in the HTTP cache. | 352 // bitmap at the best primary icon URL should be in the HTTP cache. |
337 TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) { | 353 TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) { |
338 GURL best_primary_icon_url = test_server()->GetURL("/slow?1000"); | 354 SetBestPrimaryIconUrl(test_server()->GetURL("/slow?1000")); |
339 SetBestPrimaryIconUrl(best_primary_icon_url); | |
340 | 355 |
341 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 356 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
342 runner->RunInstallWebApk(); | 357 runner->RunInstallWebApk(); |
358 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | |
359 } | |
360 | |
361 // Test that installation fails if fetching the bitmap at the best badge icon | |
362 // URL times out. In a perfect world the fetch would never time out because the | |
363 // bitmap at the best badge icon URL should be in the HTTP cache. | |
364 TEST_F(WebApkInstallerTest, BestBadgeIconUrlDownloadTimesOut) { | |
365 SetBestBadgeIconUrl(test_server()->GetURL("/slow?1000")); | |
366 | |
367 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
368 runner->RunInstallWebApk(); | |
343 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | 369 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
344 } | 370 } |
345 | 371 |
346 // Test that installation fails if the WebAPK creation request times out. | 372 // Test that installation fails if the WebAPK creation request times out. |
347 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { | 373 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { |
348 GURL server_url = test_server()->GetURL("/slow?1000"); | 374 SetWebApkServerUrl(test_server()->GetURL("/slow?1000")); |
349 SetWebApkServerUrl(server_url); | |
350 | 375 |
351 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 376 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
352 runner->RunInstallWebApk(); | 377 runner->RunInstallWebApk(); |
353 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | 378 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
354 } | 379 } |
355 | 380 |
356 namespace { | 381 namespace { |
357 | 382 |
358 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. | 383 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. |
359 std::unique_ptr<net::test_server::HttpResponse> | 384 std::unique_ptr<net::test_server::HttpResponse> |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
396 | 421 |
397 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 422 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
398 runner->RunUpdateWebApk(); | 423 runner->RunUpdateWebApk(); |
399 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); | 424 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); |
400 } | 425 } |
401 | 426 |
402 // When there is no Web Manifest available for a site, an empty | 427 // When there is no Web Manifest available for a site, an empty |
403 // |best_primary_icon_url| is used to build a WebApk update request. Tests the | 428 // |best_primary_icon_url| is used to build a WebApk update request. Tests the |
404 // request can be built properly. | 429 // request can be built properly. |
405 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { | 430 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { |
406 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); | 431 std::string icon_url_1 = test_server()->GetURL("/icon1.png").spec(); |
407 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); | 432 std::string icon_url_2 = test_server()->GetURL("/icon2.png").spec(); |
408 std::string icon_murmur2_hash_1 = "1"; | |
409 std::string icon_murmur2_hash_2 = "2"; | |
410 std::map<std::string, std::string> icon_url_to_murmur2_hash; | 433 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
411 icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; | 434 icon_url_to_murmur2_hash[icon_url_1] = "1"; |
412 icon_url_to_murmur2_hash[icon_url_2.spec()] = icon_murmur2_hash_2; | 435 icon_url_to_murmur2_hash[icon_url_2] = "2"; |
413 | 436 |
414 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); | 437 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
415 runner->BuildSync(GURL(""), icon_url_to_murmur2_hash, | 438 runner->BuildSync(GURL(), GURL(), icon_url_to_murmur2_hash, |
416 true /* is_manifest_stale*/); | 439 true /* is_manifest_stale*/); |
417 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | 440 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
418 ASSERT_NE(nullptr, webapk_request); | 441 ASSERT_NE(nullptr, webapk_request); |
419 | 442 |
420 webapk::WebAppManifest manifest = webapk_request->manifest(); | 443 webapk::WebAppManifest manifest = webapk_request->manifest(); |
421 ASSERT_EQ(3, manifest.icons_size()); | 444 ASSERT_EQ(3, manifest.icons_size()); |
422 | 445 |
423 webapk::Image icons[3]; | 446 webapk::Image icons[3]; |
424 for (int i = 0; i < 3; ++i) | 447 for (int i = 0; i < 3; ++i) |
425 icons[i] = manifest.icons(i); | 448 icons[i] = manifest.icons(i); |
426 | 449 |
427 EXPECT_EQ("", icons[0].src()); | 450 EXPECT_EQ("", icons[0].src()); |
428 EXPECT_FALSE(icons[0].has_hash()); | 451 EXPECT_FALSE(icons[0].has_hash()); |
429 EXPECT_TRUE(icons[0].has_image_data()); | 452 EXPECT_TRUE(icons[0].has_image_data()); |
430 | 453 |
431 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | 454 EXPECT_EQ(icon_url_1, icons[1].src()); |
432 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | 455 EXPECT_EQ(icon_url_to_murmur2_hash[icon_url_1], icons[1].hash()); |
433 EXPECT_FALSE(icons[1].has_image_data()); | 456 EXPECT_FALSE(icons[1].has_image_data()); |
434 | 457 |
435 EXPECT_EQ(icon_url_2.spec(), icons[2].src()); | 458 EXPECT_EQ(icon_url_2, icons[2].src()); |
436 EXPECT_EQ(icon_murmur2_hash_2, icons[2].hash()); | 459 EXPECT_EQ(icon_url_to_murmur2_hash[icon_url_2], icons[2].hash()); |
437 EXPECT_FALSE(icons[2].has_image_data()); | 460 EXPECT_FALSE(icons[2].has_image_data()); |
438 } | 461 } |
439 | 462 |
440 // Tests a WebApk install or update request is built properly when the Chrome | 463 // Tests a WebApk install or update request is built properly when the Chrome |
441 // knows the best icon URL of a site after fetching its Web Manifest. | 464 // knows the best icon URL of a site after fetching its Web Manifest. |
442 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { | 465 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { |
443 GURL icon_url_1 = test_server()->GetURL("/icon.png"); | 466 std::string icon_url_1 = test_server()->GetURL("/icon.png").spec(); |
444 GURL best_primary_icon_url = test_server()->GetURL(kBestPrimaryIconUrl); | 467 std::string best_primary_icon_url = |
445 std::string icon_murmur2_hash_1 = "1"; | 468 test_server()->GetURL(kBestPrimaryIconUrl).spec(); |
446 std::string best_primary_icon_murmur2_hash = "0"; | 469 std::string best_badge_icon_url = |
470 test_server()->GetURL(kBestBadgeIconUrl).spec(); | |
447 std::map<std::string, std::string> icon_url_to_murmur2_hash; | 471 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
448 icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; | 472 icon_url_to_murmur2_hash[icon_url_1] = "0"; |
449 icon_url_to_murmur2_hash[best_primary_icon_url.spec()] = | 473 icon_url_to_murmur2_hash[best_primary_icon_url] = "1"; |
450 best_primary_icon_murmur2_hash; | 474 icon_url_to_murmur2_hash[best_badge_icon_url] = "2"; |
451 | 475 |
452 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); | 476 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); |
453 runner->BuildSync(best_primary_icon_url, icon_url_to_murmur2_hash, | 477 runner->BuildSync(GURL(best_primary_icon_url), GURL(best_badge_icon_url), |
454 false /* is_manifest_stale*/); | 478 icon_url_to_murmur2_hash, false /* is_manifest_stale*/); |
455 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | 479 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); |
456 ASSERT_NE(nullptr, webapk_request); | 480 ASSERT_NE(nullptr, webapk_request); |
457 | 481 |
482 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
483 ASSERT_EQ(3, manifest.icons_size()); | |
484 | |
485 webapk::Image icons[3]; | |
486 for (int i = 0; i < 3; ++i) | |
487 icons[i] = manifest.icons(i); | |
488 | |
489 // Check protobuf fields for /icon.png. | |
490 EXPECT_EQ(icon_url_1, icons[0].src()); | |
491 EXPECT_EQ(icon_url_to_murmur2_hash[icon_url_1], icons[0].hash()); | |
pkotwicz
2017/04/07 15:37:55
We should also check that the usages are not speci
F
2017/04/07 17:22:56
Done.
| |
492 EXPECT_FALSE(icons[0].has_image_data()); | |
493 | |
494 // Check protobuf fields for kBestBadgeIconUrl. | |
495 EXPECT_EQ(best_badge_icon_url, icons[1].src()); | |
496 EXPECT_EQ(icon_url_to_murmur2_hash[best_badge_icon_url], icons[1].hash()); | |
497 ASSERT_EQ(1, icons[1].usages_size()); | |
498 EXPECT_EQ(webapk::Image::BADGE_ICON, icons[1].usages(0)); | |
pkotwicz
2017/04/07 15:34:05
Nit: You can use
EXPECT_THAT(icon[1].usages(), te
F
2017/04/07 17:22:55
Done.
| |
499 EXPECT_TRUE(icons[1].has_image_data()); | |
500 | |
501 // Check protobuf fields for kBestPrimaryIconUrl. | |
502 EXPECT_EQ(best_primary_icon_url, icons[2].src()); | |
503 EXPECT_EQ(icon_url_to_murmur2_hash[best_primary_icon_url], icons[2].hash()); | |
504 ASSERT_EQ(1, icons[2].usages_size()); | |
505 EXPECT_EQ(webapk::Image::PRIMARY_ICON, icons[2].usages(0)); | |
pkotwicz
2017/04/07 15:34:04
Nit: You can use
EXPECT_THAT(icon[2].usages(), te
F
2017/04/07 17:22:55
Done.
| |
506 EXPECT_TRUE(icons[2].has_image_data()); | |
507 } | |
508 | |
509 // Tests a WebApk install or update request is built properly when the Chrome | |
510 // knows the best icon URL of a site after fetching its Web Manifest, and | |
511 // primary icon and badge icon share the same URL. | |
512 TEST_F(WebApkInstallerTest, BuildWebApkProtoPrimaryIconAndBadgeIconSameUrl) { | |
513 std::string icon_url_1 = test_server()->GetURL("/icon.png").spec(); | |
514 std::string best_icon_url = test_server()->GetURL(kBestPrimaryIconUrl).spec(); | |
515 std::map<std::string, std::string> icon_url_to_murmur2_hash; | |
516 icon_url_to_murmur2_hash[icon_url_1] = "1"; | |
517 icon_url_to_murmur2_hash[best_icon_url] = "0"; | |
518 | |
519 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); | |
520 runner->BuildSync(GURL(best_icon_url), GURL(best_icon_url), | |
521 icon_url_to_murmur2_hash, false /* is_manifest_stale*/); | |
522 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | |
523 ASSERT_NE(nullptr, webapk_request); | |
524 | |
458 webapk::WebAppManifest manifest = webapk_request->manifest(); | 525 webapk::WebAppManifest manifest = webapk_request->manifest(); |
459 ASSERT_EQ(2, manifest.icons_size()); | 526 ASSERT_EQ(2, manifest.icons_size()); |
460 | 527 |
461 webapk::Image icons[2]; | 528 webapk::Image icons[2]; |
462 for (int i = 0; i < 2; ++i) | 529 for (int i = 0; i < 2; ++i) |
463 icons[i] = manifest.icons(i); | 530 icons[i] = manifest.icons(i); |
464 | 531 |
465 EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src()); | 532 // Check protobuf fields for /icon.png. |
466 EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash()); | 533 EXPECT_EQ(icon_url_1, icons[0].src()); |
467 EXPECT_TRUE(icons[0].has_image_data()); | 534 EXPECT_EQ(icon_url_to_murmur2_hash[icon_url_1], icons[0].hash()); |
pkotwicz
2017/04/07 15:37:55
We should also check that the usages are not speci
F
2017/04/07 17:22:55
Done.
| |
535 EXPECT_FALSE(icons[0].has_image_data()); | |
468 | 536 |
469 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | 537 // Check protobuf fields for kBestPrimaryIconUrl. |
470 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | 538 EXPECT_EQ(best_icon_url, icons[1].src()); |
471 EXPECT_FALSE(icons[1].has_image_data()); | 539 EXPECT_EQ(icon_url_to_murmur2_hash[best_icon_url], icons[1].hash()); |
540 ASSERT_EQ(2, icons[1].usages_size()); | |
541 EXPECT_EQ(webapk::Image::PRIMARY_ICON, icons[1].usages(0)); | |
542 EXPECT_EQ(webapk::Image::BADGE_ICON, icons[1].usages(1)); | |
pkotwicz
2017/04/07 15:34:04
Nit: You can use
EXPECT_THAT(icons[1].usages(), t
F
2017/04/07 17:22:56
Done.
| |
543 EXPECT_TRUE(icons[1].has_image_data()); | |
472 } | 544 } |
473 | 545 |
474 TEST_F(WebApkInstallerTest, FailsWhenInstallDisabled) { | 546 TEST_F(WebApkInstallerTest, FailsWhenInstallDisabled) { |
475 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 547 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
476 runner->SetCanInstallWebApks(false); | 548 runner->SetCanInstallWebApks(false); |
477 runner->RunInstallWebApk(); | 549 runner->RunInstallWebApk(); |
478 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | 550 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
479 } | 551 } |
OLD | NEW |