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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 base::Bind(&WebApkInstallerRunner::OnCompleted, | 100 base::Bind(&WebApkInstallerRunner::OnCompleted, |
| 101 base::Unretained(this))); | 101 base::Unretained(this))); |
| 102 Run(); | 102 Run(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void RunUpdateWebApk() { | 105 void RunUpdateWebApk() { |
| 106 const std::string kIconMurmur2Hash = "0"; | 106 const std::string kIconMurmur2Hash = "0"; |
| 107 const int kWebApkVersion = 1; | 107 const int kWebApkVersion = 1; |
| 108 | 108 |
| 109 WebApkInstaller* installer = CreateWebApkInstaller(); | 109 WebApkInstaller* installer = CreateWebApkInstaller(); |
| 110 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
| 111 icon_url_to_murmur2_hash_map[best_icon_url_.spec()] = kIconMurmur2Hash; | |
| 110 | 112 |
| 111 installer->UpdateAsyncWithURLRequestContextGetter( | 113 installer->UpdateAsyncWithURLRequestContextGetter( |
| 112 url_request_context_getter_.get(), | 114 url_request_context_getter_.get(), |
| 113 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), | 115 base::Bind(&WebApkInstallerRunner::OnCompleted, base::Unretained(this)), |
| 114 kIconMurmur2Hash, | |
| 115 kDownloadedWebApkPackageName, | 116 kDownloadedWebApkPackageName, |
| 116 kWebApkVersion); | 117 kWebApkVersion, |
| 118 false /* stale_manifest */, | |
| 119 icon_url_to_murmur2_hash_map); | |
| 117 | 120 |
| 118 Run(); | 121 Run(); |
| 119 } | 122 } |
| 123 | |
| 124 void BuildWebApkProto( | |
| 125 bool state_manifest, | |
| 126 const std::map<std::string, std::string> icon_url_to_murmur2_hash_map) { | |
| 127 WebApkInstaller* installer = CreateWebApkInstaller(); | |
| 128 installer->BuildWebApkProtoInBackgroundForTesting( | |
| 129 base::Bind(&WebApkInstallerRunner::onBuildWebApkProtoComeplete, | |
| 130 base::Unretained(this)), | |
| 131 state_manifest, | |
| 132 icon_url_to_murmur2_hash_map); | |
| 133 | |
| 134 Run(); | |
| 135 } | |
| 120 | 136 |
| 121 WebApkInstaller* CreateWebApkInstaller() { | 137 WebApkInstaller* CreateWebApkInstaller() { |
| 122 ShortcutInfo info(GURL::EmptyGURL()); | 138 ShortcutInfo info(GURL::EmptyGURL()); |
| 123 info.best_icon_url = best_icon_url_; | 139 info.best_icon_url = best_icon_url_; |
| 124 | 140 |
| 125 // WebApkInstaller owns itself. | 141 // WebApkInstaller owns itself. |
| 126 WebApkInstaller* installer = new TestWebApkInstaller(info, SkBitmap()); | 142 WebApkInstaller* installer = new TestWebApkInstaller(info, SkBitmap()); |
| 127 installer->SetTimeoutMs(100); | 143 installer->SetTimeoutMs(100); |
| 128 return installer; | 144 return installer; |
| 129 } | 145 } |
| 130 | 146 |
| 131 void Run() { | 147 void Run() { |
| 132 base::RunLoop run_loop; | 148 base::RunLoop run_loop; |
| 133 on_completed_callback_ = run_loop.QuitClosure(); | 149 on_completed_callback_ = run_loop.QuitClosure(); |
| 134 run_loop.Run(); | 150 run_loop.Run(); |
| 135 } | 151 } |
| 136 | 152 |
| 137 bool success() { return success_; } | 153 bool success() { return success_; } |
| 138 | 154 |
| 155 webapk::WebApk* GetWebApkRequest() { | |
| 156 return webapk_request_.get(); | |
| 157 } | |
| 158 | |
| 139 private: | 159 private: |
| 140 void OnCompleted(bool success, const std::string& webapk_package) { | 160 void OnCompleted(bool success, const std::string& webapk_package) { |
| 141 success_ = success; | 161 success_ = success; |
| 142 on_completed_callback_.Run(); | 162 on_completed_callback_.Run(); |
| 143 } | 163 } |
| 144 | 164 |
| 165 void onBuildWebApkProtoComeplete( | |
| 166 std::unique_ptr<webapk::WebApk> webapk_request) { | |
| 167 webapk_request_ = std::move(webapk_request); | |
| 168 on_completed_callback_.Run(); | |
| 169 } | |
| 170 | |
| 145 scoped_refptr<net::TestURLRequestContextGetter> | 171 scoped_refptr<net::TestURLRequestContextGetter> |
| 146 url_request_context_getter_; | 172 url_request_context_getter_; |
| 147 | 173 |
| 148 // The Web Manifest's icon URL. | 174 // The Web Manifest's icon URL. |
| 149 const GURL best_icon_url_; | 175 const GURL best_icon_url_; |
| 150 | 176 |
| 151 // Called after the installation process has succeeded or failed. | 177 // Called after the installation process has succeeded or failed. |
| 152 base::Closure on_completed_callback_; | 178 base::Closure on_completed_callback_; |
| 153 | 179 |
| 154 // Whether the installation process succeeded. | 180 // Whether the installation process succeeded. |
| 155 bool success_; | 181 bool success_; |
| 156 | 182 |
| 183 // The request sent to WebAPK server. | |
| 184 std::unique_ptr<webapk::WebApk> webapk_request_; | |
| 185 | |
| 157 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); | 186 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); |
| 158 }; | 187 }; |
| 159 | 188 |
| 160 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download | 189 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download |
| 161 // URL. | 190 // URL. |
| 162 std::unique_ptr<net::test_server::HttpResponse> BuildValidWebApkResponse( | 191 std::unique_ptr<net::test_server::HttpResponse> BuildValidWebApkResponse( |
| 163 const GURL& download_url) { | 192 const GURL& download_url) { |
| 164 std::unique_ptr<webapk::WebApkResponse> response_proto( | 193 std::unique_ptr<webapk::WebApkResponse> response_proto( |
| 165 new webapk::WebApkResponse); | 194 new webapk::WebApkResponse); |
| 166 response_proto->set_package_name(kDownloadedWebApkPackageName); | 195 response_proto->set_package_name(kDownloadedWebApkPackageName); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 runner->RunInstallWebApk(); | 353 runner->RunInstallWebApk(); |
| 325 EXPECT_FALSE(runner->success()); | 354 EXPECT_FALSE(runner->success()); |
| 326 } | 355 } |
| 327 | 356 |
| 328 // Test update succeeding. | 357 // Test update succeeding. |
| 329 TEST_F(WebApkInstallerTest, UpdateSuccess) { | 358 TEST_F(WebApkInstallerTest, UpdateSuccess) { |
| 330 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 359 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 331 runner->RunUpdateWebApk(); | 360 runner->RunUpdateWebApk(); |
| 332 EXPECT_TRUE(runner->success()); | 361 EXPECT_TRUE(runner->success()); |
| 333 } | 362 } |
| 363 | |
| 364 // When there is no Web Manifest available for a site, an empty |best_icon_url| | |
| 365 // is used to build a WebApk update request. Tests the request can be built | |
| 366 // properly. | |
| 367 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { | |
| 368 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); | |
| 369 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); | |
| 370 GURL best_icon_url(""); | |
| 371 std::string icon_murmur2_hash_1 = "1"; | |
| 372 std::string icon_murmur2_hash_2 = "2"; | |
| 373 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
| 374 icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; | |
| 375 icon_url_to_murmur2_hash_map[icon_url_2.spec()] = icon_murmur2_hash_2; | |
| 376 | |
| 377 SetBestIconUrl(best_icon_url); | |
| 378 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
| 379 runner->BuildWebApkProto(true, icon_url_to_murmur2_hash_map); | |
| 380 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | |
| 381 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
| 382 | |
| 383 EXPECT_EQ(3, manifest.icons_size()); | |
| 384 | |
| 385 webapk::Image icons[3]; | |
| 386 for (int i = 0; i < 3; ++i) | |
| 387 icons[i] = manifest.icons(i); | |
| 388 | |
| 389 EXPECT_EQ(best_icon_url.spec(), icons[0].src()); | |
|
pkotwicz
2016/12/09 22:18:55
Replace best_icon_url.spec() with "" to make it su
Xi Han
2016/12/13 20:59:15
Done.
| |
| 390 EXPECT_FALSE(icons[0].has_hash()); | |
| 391 EXPECT_TRUE(icons[0].has_image_data()); | |
| 392 | |
| 393 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | |
| 394 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | |
| 395 EXPECT_FALSE(icons[1].has_image_data()); | |
| 396 | |
| 397 EXPECT_EQ(icon_url_2.spec(), icons[2].src()); | |
| 398 EXPECT_EQ(icon_murmur2_hash_2, icons[2].hash()); | |
| 399 EXPECT_FALSE(icons[2].has_image_data()); | |
| 400 } | |
| 401 | |
| 402 // Tests a WebApk install or update request is built properly when the Chrome | |
| 403 // knows the best icon URL of a site after fetching its Web Manifest. | |
| 404 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsAvailable) { | |
| 405 GURL icon_url_1 = test_server()->GetURL("/icon.png"); | |
| 406 GURL best_icon_url = test_server()->GetURL(kBestIconUrl); | |
| 407 std::string icon_murmur2_hash_1 = "1"; | |
| 408 std::string best_icon_murmur2_hash = "0"; | |
| 409 std::map<std::string, std::string> icon_url_to_murmur2_hash_map; | |
| 410 icon_url_to_murmur2_hash_map[icon_url_1.spec()] = icon_murmur2_hash_1; | |
| 411 icon_url_to_murmur2_hash_map[best_icon_url.spec()] = | |
| 412 best_icon_murmur2_hash; | |
| 413 | |
| 414 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
| 415 runner->BuildWebApkProto(false, icon_url_to_murmur2_hash_map); | |
| 416 webapk::WebApk* webapk_request = runner->GetWebApkRequest(); | |
| 417 webapk::WebAppManifest manifest = webapk_request->manifest(); | |
| 418 | |
| 419 EXPECT_EQ(2, manifest.icons_size()); | |
| 420 | |
| 421 webapk::Image icons[2]; | |
| 422 for (int i = 0; i < 2; ++i) | |
| 423 icons[i] = manifest.icons(i); | |
| 424 | |
| 425 EXPECT_EQ(best_icon_url.spec(), icons[0].src()); | |
| 426 EXPECT_EQ(best_icon_murmur2_hash, icons[0].hash()); | |
| 427 EXPECT_TRUE(icons[0].has_image_data()); | |
| 428 | |
| 429 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | |
| 430 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | |
| 431 EXPECT_FALSE(icons[1].has_image_data()); | |
| 432 } | |
| OLD | NEW |