Chromium Code Reviews| 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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(); |
| 135 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
| 136 icon_url_to_murmur2_hash_map[best_icon_url_.spec()] = kIconMurmur2Hash; | |
| 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 */, | |
| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 241 // WebAPK creation request. | 244 // WebAPK creation request. |
| 242 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { | 245 void SetWebApkResponseBuilder(const WebApkResponseBuilder& builder) { |
| 243 webapk_response_builder_ = builder; | 246 webapk_response_builder_ = builder; |
| 244 } | 247 } |
| 245 | 248 |
| 246 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { | 249 std::unique_ptr<WebApkInstallerRunner> CreateWebApkInstallerRunner() { |
| 247 return std::unique_ptr<WebApkInstallerRunner>( | 250 return std::unique_ptr<WebApkInstallerRunner>( |
| 248 new WebApkInstallerRunner(best_icon_url_)); | 251 new WebApkInstallerRunner(best_icon_url_)); |
| 249 } | 252 } |
| 250 | 253 |
| 254 void BuildWebApkProto( | |
|
pkotwicz
2016/12/19 21:59:53
Sorry for being a pain. Can you move this function
| |
| 255 const GURL& best_icon_url, | |
| 256 bool stale_manifest, | |
| 257 const std::map<std::string, std::string>& icon_url_to_murmur2_hash_map) { | |
| 258 ShortcutInfo info(GURL::EmptyGURL()); | |
| 259 info.best_icon_url = best_icon_url; | |
| 260 | |
| 261 // WebApkInstaller owns itself. | |
| 262 WebApkInstaller* installer = | |
| 263 new TestWebApkInstaller(info, SkBitmap(), false); | |
| 264 installer->SetTimeoutMs(100); | |
| 265 installer->BuildWebApkProtoInBackgroundForTesting( | |
| 266 base::Bind(&WebApkInstallerTest::OnBuiltWebApkProto, | |
| 267 base::Unretained(this)), | |
| 268 stale_manifest, | |
| 269 icon_url_to_murmur2_hash_map); | |
| 270 | |
| 271 base::RunLoop run_loop; | |
| 272 on_completed_callback_ = run_loop.QuitClosure(); | |
| 273 run_loop.Run(); | |
| 274 } | |
| 275 | |
| 276 webapk::WebApk* GetWebApkRequest() { return webapk_request_.get(); } | |
| 277 | |
| 251 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } | 278 net::test_server::EmbeddedTestServer* test_server() { return &test_server_; } |
| 252 | 279 |
| 253 private: | 280 private: |
| 254 // Sets default configuration for running WebApkInstaller. | 281 // Sets default configuration for running WebApkInstaller. |
| 255 void SetDefaults() { | 282 void SetDefaults() { |
| 256 GURL best_icon_url = test_server_.GetURL(kBestIconUrl); | 283 GURL best_icon_url = test_server_.GetURL(kBestIconUrl); |
| 257 SetBestIconUrl(best_icon_url); | 284 SetBestIconUrl(best_icon_url); |
| 258 GURL server_url = test_server_.GetURL(kServerUrl); | 285 GURL server_url = test_server_.GetURL(kServerUrl); |
| 259 SetWebApkServerUrl(server_url); | 286 SetWebApkServerUrl(server_url); |
| 260 GURL download_url = test_server_.GetURL(kDownloadUrl); | 287 GURL download_url = test_server_.GetURL(kDownloadUrl); |
| 261 SetWebApkResponseBuilder( | 288 SetWebApkResponseBuilder( |
| 262 base::Bind(&BuildValidWebApkResponse, download_url)); | 289 base::Bind(&BuildValidWebApkResponse, download_url)); |
| 263 } | 290 } |
| 264 | 291 |
| 265 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( | 292 std::unique_ptr<net::test_server::HttpResponse> HandleWebApkRequest( |
| 266 const net::test_server::HttpRequest& request) { | 293 const net::test_server::HttpRequest& request) { |
| 267 return (request.relative_url == kServerUrl) | 294 return (request.relative_url == kServerUrl) |
| 268 ? webapk_response_builder_.Run() | 295 ? webapk_response_builder_.Run() |
| 269 : std::unique_ptr<net::test_server::HttpResponse>(); | 296 : std::unique_ptr<net::test_server::HttpResponse>(); |
| 270 } | 297 } |
| 271 | 298 |
| 299 // Called when the |webapk| request is populated. | |
| 300 void OnBuiltWebApkProto(std::unique_ptr<webapk::WebApk> webapk) { | |
| 301 webapk_request_ = std::move(webapk); | |
| 302 on_completed_callback_.Run(); | |
| 303 } | |
| 304 | |
| 272 content::TestBrowserThreadBundle thread_bundle_; | 305 content::TestBrowserThreadBundle thread_bundle_; |
| 273 net::EmbeddedTestServer test_server_; | 306 net::EmbeddedTestServer test_server_; |
| 274 | 307 |
| 275 // Web Manifest's icon URL. | 308 // Web Manifest's icon URL. |
| 276 GURL best_icon_url_; | 309 GURL best_icon_url_; |
| 277 | 310 |
| 278 // Builds response to the WebAPK creation request. | 311 // Builds response to the WebAPK creation request. |
| 279 WebApkResponseBuilder webapk_response_builder_; | 312 WebApkResponseBuilder webapk_response_builder_; |
| 280 | 313 |
| 314 // The populated webapk::WebApk. | |
| 315 std::unique_ptr<webapk::WebApk> webapk_request_; | |
| 316 | |
| 317 // Called after the installation process has succeeded or failed. | |
| 318 base::Closure on_completed_callback_; | |
| 319 | |
| 281 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); | 320 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); |
| 282 }; | 321 }; |
| 283 | 322 |
| 284 // Test installation succeeding. | 323 // Test installation succeeding. |
| 285 TEST_F(WebApkInstallerTest, Success) { | 324 TEST_F(WebApkInstallerTest, Success) { |
| 286 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 325 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 287 runner->RunInstallWebApk(); | 326 runner->RunInstallWebApk(); |
| 288 EXPECT_TRUE(runner->success()); | 327 EXPECT_TRUE(runner->success()); |
| 289 } | 328 } |
| 290 | 329 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 EXPECT_TRUE(runner->success()); | 400 EXPECT_TRUE(runner->success()); |
| 362 } | 401 } |
| 363 | 402 |
| 364 // Test installation succeeds using Google Play. | 403 // Test installation succeeds using Google Play. |
| 365 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { | 404 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { |
| 366 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 405 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 367 runner->SetHasGooglePlayWebApkInstallDelegate(true); | 406 runner->SetHasGooglePlayWebApkInstallDelegate(true); |
| 368 runner->RunInstallWebApk(); | 407 runner->RunInstallWebApk(); |
| 369 EXPECT_TRUE(runner->success()); | 408 EXPECT_TRUE(runner->success()); |
| 370 } | 409 } |
| 410 | |
| 411 // When there is no Web Manifest available for a site, an empty |best_icon_url| | |
| 412 // is used to build a WebApk update request. Tests the request can be built | |
| 413 // properly. | |
| 414 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { | |
| 415 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); | |
| 416 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); | |
| 417 std::string icon_murmur2_hash_1 = "1"; | |
| 418 std::string icon_murmur2_hash_2 = "2"; | |
| 419 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
| 420 icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; | |
| 421 icon_url_to_murmur2_hash_map[icon_url_2.spec()] = icon_murmur2_hash_2; | |
| 422 | |
| 423 BuildWebApkProto(GURL(""), true, icon_url_to_murmur2_hash_map); | |
| 424 webapk::WebApk* webapk_request = GetWebApkRequest(); | |
| 425 ASSERT_NE(nullptr, webapk_request); | |
| 426 | |
| 427 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
| 428 ASSERT_EQ(3, manifest.icons_size()); | |
| 429 | |
| 430 webapk::Image icons[3]; | |
| 431 for (int i = 0; i < 3; ++i) | |
| 432 icons[i] = manifest.icons(i); | |
| 433 | |
| 434 EXPECT_EQ("", icons[0].src()); | |
| 435 EXPECT_FALSE(icons[0].has_hash()); | |
| 436 EXPECT_TRUE(icons[0].has_image_data()); | |
| 437 | |
| 438 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | |
| 439 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | |
| 440 EXPECT_FALSE(icons[1].has_image_data()); | |
| 441 | |
| 442 EXPECT_EQ(icon_url_2.spec(), icons[2].src()); | |
| 443 EXPECT_EQ(icon_murmur2_hash_2, icons[2].hash()); | |
| 444 EXPECT_FALSE(icons[2].has_image_data()); | |
| 445 } | |
| 446 | |
| 447 // Tests a WebApk install or update request is built properly when the Chrome | |
| 448 // knows the best icon URL of a site after fetching its Web Manifest. | |
| 449 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { | |
| 450 GURL icon_url_1 = test_server()->GetURL("/icon.png"); | |
| 451 GURL best_icon_url = test_server()->GetURL(kBestIconUrl); | |
| 452 std::string icon_murmur2_hash_1 = "1"; | |
| 453 std::string best_icon_murmur2_hash = "0"; | |
| 454 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
| 455 icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; | |
| 456 icon_url_to_murmur2_hash_map[best_icon_url.spec()] = | |
| 457 best_icon_murmur2_hash; | |
| 458 | |
| 459 BuildWebApkProto(best_icon_url, false, icon_url_to_murmur2_hash_map); | |
| 460 webapk::WebApk* webapk_request = GetWebApkRequest(); | |
| 461 ASSERT_NE(nullptr, webapk_request); | |
| 462 | |
| 463 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
| 464 ASSERT_EQ(2, manifest.icons_size()); | |
| 465 | |
| 466 webapk::Image icons[2]; | |
| 467 for (int i = 0; i < 2; ++i) | |
| 468 icons[i] = manifest.icons(i); | |
| 469 | |
| 470 EXPECT_EQ(best_icon_url.spec(), icons[0].src()); | |
| 471 EXPECT_EQ(best_icon_murmur2_hash, icons[0].hash()); | |
| 472 EXPECT_TRUE(icons[0].has_image_data()); | |
| 473 | |
| 474 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | |
| 475 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | |
| 476 EXPECT_FALSE(icons[1].has_image_data()); | |
| 477 } | |
| OLD | NEW |