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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 url_request_context_getter_.get(), | 124 url_request_context_getter_.get(), |
125 base::Bind(&WebApkInstallerRunner::OnCompleted, | 125 base::Bind(&WebApkInstallerRunner::OnCompleted, |
126 base::Unretained(this))); | 126 base::Unretained(this))); |
127 Run(); | 127 Run(); |
128 } | 128 } |
129 | 129 |
130 void RunUpdateWebApk() { | 130 void RunUpdateWebApk() { |
131 const std::string kIconMurmur2Hash = "0"; | 131 const std::string kIconMurmur2Hash = "0"; |
132 const int kWebApkVersion = 1; | 132 const int kWebApkVersion = 1; |
133 | 133 |
134 WebApkInstaller* installer = CreateWebApkInstaller(); | 134 WebApkInstaller* installer = CreateWebApkInstaller(); |
dominickn
2016/12/20 05:05:49
Minor nit: remove this line, and just do CreateWeb
Xi Han
2016/12/20 20:25:10
Done.
| |
135 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
dominickn
2016/12/20 05:05:49
icon_url_to_murmur2_map (std::map is not a hash ma
Xi Han
2016/12/20 20:25:10
same.
| |
136 icon_url_to_murmur2_hash_map[best_icon_url_.spec()] = kIconMurmur2Hash; | |
dominickn
2016/12/20 05:05:49
can this be
std::map<std::string, std::string> ic
Xi Han
2016/12/20 20:25:10
Done.
| |
135 | 137 |
136 installer->UpdateAsyncWithURLRequestContextGetter( | 138 installer->UpdateAsyncWithURLRequestContextGetter( |
137 url_request_context_getter_.get(), | 139 url_request_context_getter_.get(), |
138 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), | 140 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), |
139 kIconMurmur2Hash, | |
140 kDownloadedWebApkPackageName, | 141 kDownloadedWebApkPackageName, |
141 kWebApkVersion); | 142 kWebApkVersion, |
143 false /* stale_manifest */, | |
dominickn
2016/12/20 05:05:49
is_manifest_stale
Xi Han
2016/12/20 20:25:09
Done.
| |
144 icon_url_to_murmur2_hash_map); | |
142 | 145 |
143 Run(); | 146 Run(); |
144 } | 147 } |
145 | 148 |
146 WebApkInstaller* CreateWebApkInstaller() { | 149 WebApkInstaller* CreateWebApkInstaller() { |
147 ShortcutInfo info(GURL::EmptyGURL()); | 150 ShortcutInfo info(GURL::EmptyGURL()); |
148 info.best_icon_url = best_icon_url_; | 151 info.best_icon_url = best_icon_url_; |
149 | 152 |
150 // WebApkInstaller owns itself. | 153 // WebApkInstaller owns itself. |
151 WebApkInstaller* installer = new TestWebApkInstaller( | 154 WebApkInstaller* installer = new TestWebApkInstaller( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 std::string response_content; | 200 std::string response_content; |
198 response_proto->SerializeToString(&response_content); | 201 response_proto->SerializeToString(&response_content); |
199 | 202 |
200 std::unique_ptr<net::test_server::BasicHttpResponse> response( | 203 std::unique_ptr<net::test_server::BasicHttpResponse> response( |
201 new net::test_server::BasicHttpResponse()); | 204 new net::test_server::BasicHttpResponse()); |
202 response->set_code(net::HTTP_OK); | 205 response->set_code(net::HTTP_OK); |
203 response->set_content(response_content); | 206 response->set_content(response_content); |
204 return std::move(response); | 207 return std::move(response); |
205 } | 208 } |
206 | 209 |
210 // Builds WebApk proto and blocks till done. | |
211 class BuildProtoRunner { | |
212 public: | |
213 BuildProtoRunner() {} | |
214 | |
215 ~BuildProtoRunner() {} | |
216 | |
217 void BuildSync( | |
218 const GURL& best_icon_url, | |
219 bool stale_manifest, | |
220 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { | |
221 ShortcutInfo info(GURL::EmptyGURL()); | |
222 info.best_icon_url = best_icon_url; | |
223 | |
224 // WebApkInstaller owns itself. | |
225 WebApkInstaller* installer = | |
226 new TestWebApkInstaller(info, SkBitmap(), false); | |
227 installer->SetTimeoutMs(100); | |
pkotwicz
2016/12/19 23:04:31
Nit: You don't need to set a timeout
Xi Han
2016/12/20 20:25:09
Done.
| |
228 installer->BuildWebApkProtoInBackgroundForTesting( | |
229 base::Bind(&BuildProtoRunner::OnBuiltWebApkProto, | |
230 base::Unretained(this)), | |
231 stale_manifest, | |
232 icon_url_to_murmur2_hash_map); | |
233 | |
234 base::RunLoop run_loop; | |
235 on_completed_callback_ = run_loop.QuitClosure(); | |
236 run_loop.Run(); | |
237 } | |
238 | |
239 webapk::WebApk* GetWebApkRequest() { return webapk_request_.get(); } | |
240 | |
241 private: | |
242 // Called when the |webapk| request is populated. | |
pkotwicz
2016/12/19 23:04:31
|webapk| request -> |webapk_request_|
We use '||'
Xi Han
2016/12/20 20:25:10
Done.
| |
243 void OnBuiltWebApkProto(std::unique_ptr<webapk::WebApk> webapk) { | |
244 webapk_request_ = std::move(webapk); | |
245 on_completed_callback_.Run(); | |
246 } | |
247 | |
248 // The populated webapk::WebApk. | |
249 std::unique_ptr<webapk::WebApk> webapk_request_; | |
250 | |
251 // Called after the installation process has succeeded or failed. | |
pkotwicz
2016/12/19 23:04:31
Please update this comment :)
Xi Han
2016/12/20 20:25:10
Done.
| |
252 base::Closure on_completed_callback_; | |
253 | |
254 DISALLOW_COPY_AND_ASSIGN(BuildProtoRunner); | |
255 }; | |
256 | |
207 } // anonymous namespace | 257 } // anonymous namespace |
208 | 258 |
209 class WebApkInstallerTest : public ::testing::Test { | 259 class WebApkInstallerTest : public ::testing::Test { |
210 public: | 260 public: |
211 typedef base::Callback<std::unique_ptr<net::test_server::HttpResponse>(void)> | 261 typedef base::Callback<std::unique_ptr<net::test_server::HttpResponse>(void)> |
212 WebApkResponseBuilder; | 262 WebApkResponseBuilder; |
213 | 263 |
214 WebApkInstallerTest() | 264 WebApkInstallerTest() |
215 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 265 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
216 ~WebApkInstallerTest() override {} | 266 ~WebApkInstallerTest() override {} |
(...skipping 24 matching lines...) Expand all Loading... | |
241 // WebAPK creation request. | 291 // WebAPK creation request. |
242 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { | 292 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { |
243 webapk_response_builder_ = builder; | 293 webapk_response_builder_ = builder; |
244 } | 294 } |
245 | 295 |
246 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { | 296 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { |
247 return std::unique_ptr<WebApkInstallerRunner>( | 297 return std::unique_ptr<WebApkInstallerRunner>( |
248 new WebApkInstallerRunner(best_icon_url_)); | 298 new WebApkInstallerRunner(best_icon_url_)); |
249 } | 299 } |
250 | 300 |
301 std::unique_ptr<BuildProtoRunner> CreateBuildProtoRunner() { | |
302 return std::unique_ptr<BuildProtoRunner>(new BuildProtoRunner()); | |
303 } | |
304 | |
251 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } | 305 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } |
252 | 306 |
253 private: | 307 private: |
254 // Sets default configuration for running WebApkInstaller. | 308 // Sets default configuration for running WebApkInstaller. |
255 void SetDefaults() { | 309 void SetDefaults() { |
256 GURL best_icon_url = test_server_.GetURL(kBestIconUrl); | 310 GURL best_icon_url = test_server_.GetURL(kBestIconUrl); |
257 SetBestIconUrl(best_icon_url); | 311 SetBestIconUrl(best_icon_url); |
258 GURL server_url = test_server_.GetURL(kServerUrl); | 312 GURL server_url = test_server_.GetURL(kServerUrl); |
259 SetWebApkServerUrl(server_url); | 313 SetWebApkServerUrl(server_url); |
260 GURL download_url = test_server_.GetURL(kDownloadUrl); | 314 GURL download_url = test_server_.GetURL(kDownloadUrl); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 EXPECT_TRUE(runner->success()); | 415 EXPECT_TRUE(runner->success()); |
362 } | 416 } |
363 | 417 |
364 // Test installation succeeds using Google Play. | 418 // Test installation succeeds using Google Play. |
365 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { | 419 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { |
366 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 420 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
367 runner->SetHasGooglePlayWebApkInstallDelegate(true); | 421 runner->SetHasGooglePlayWebApkInstallDelegate(true); |
368 runner->RunInstallWebApk(); | 422 runner->RunInstallWebApk(); |
369 EXPECT_TRUE(runner->success()); | 423 EXPECT_TRUE(runner->success()); |
370 } | 424 } |
425 | |
426 // When there is no Web Manifest available for a site, an empty |best_icon_url| | |
427 // is used to build a WebApk update request. Tests the request can be built | |
428 // properly. | |
429 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { | |
430 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); | |
431 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); | |
432 std::string icon_murmur2_hash_1 = "1"; | |
433 std::string icon_murmur2_hash_2 = "2"; | |
434 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
435 icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; | |
436 icon_url_to_murmur2_hash_map[icon_url_2.spec()] = icon_murmur2_hash_2; | |
437 | |
438 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); | |
439 runner->BuildSync(GURL(""), true, icon_url_to_murmur2_hash_map); | |
440 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | |
441 ASSERT_NE(nullptr, webapk_request); | |
442 | |
443 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
444 ASSERT_EQ(3, manifest.icons_size()); | |
445 | |
446 webapk::Image icons[3]; | |
447 for (int i = 0; i < 3; ++i) | |
448 icons[i] = manifest.icons(i); | |
449 | |
450 EXPECT_EQ("", icons[0].src()); | |
451 EXPECT_FALSE(icons[0].has_hash()); | |
452 EXPECT_TRUE(icons[0].has_image_data()); | |
453 | |
454 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | |
455 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | |
456 EXPECT_FALSE(icons[1].has_image_data()); | |
457 | |
458 EXPECT_EQ(icon_url_2.spec(), icons[2].src()); | |
459 EXPECT_EQ(icon_murmur2_hash_2, icons[2].hash()); | |
460 EXPECT_FALSE(icons[2].has_image_data()); | |
461 } | |
462 | |
463 // Tests a WebApk install or update request is built properly when the Chrome | |
464 // knows the best icon URL of a site after fetching its Web Manifest. | |
465 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { | |
466 GURL icon_url_1 = test_server()->GetURL("/icon.png"); | |
467 GURL best_icon_url = test_server()->GetURL(kBestIconUrl); | |
468 std::string icon_murmur2_hash_1 = "1"; | |
469 std::string best_icon_murmur2_hash = "0"; | |
470 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
471 icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; | |
472 icon_url_to_murmur2_hash_map[best_icon_url.spec()] = | |
473 best_icon_murmur2_hash; | |
474 | |
475 std::unique_ptr<BuildProtoRunner> runner = CreateBuildProtoRunner(); | |
476 runner->BuildSync(best_icon_url, false, icon_url_to_murmur2_hash_map); | |
477 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | |
478 ASSERT_NE(nullptr, webapk_request); | |
479 | |
480 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
481 ASSERT_EQ(2, manifest.icons_size()); | |
482 | |
483 webapk::Image icons[2]; | |
484 for (int i = 0; i < 2; ++i) | |
485 icons[i] = manifest.icons(i); | |
486 | |
487 EXPECT_EQ(best_icon_url.spec(), icons[0].src()); | |
488 EXPECT_EQ(best_icon_murmur2_hash, icons[0].hash()); | |
489 EXPECT_TRUE(icons[0].has_image_data()); | |
490 | |
491 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | |
492 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | |
493 EXPECT_FALSE(icons[1].has_image_data()); | |
494 } | |
OLD | NEW |