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