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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 // The package name of the downloaded WebAPK. | 55 // The package name of the downloaded WebAPK. |
| 56 const char* kDownloadedWebApkPackageName = "party.unicode"; | 56 const char* kDownloadedWebApkPackageName = "party.unicode"; |
| 57 | 57 |
| 58 // WebApkInstaller subclass where | 58 // WebApkInstaller subclass where |
| 59 // WebApkInstaller::StartInstallingDownloadedWebApk() and | 59 // WebApkInstaller::StartInstallingDownloadedWebApk() and |
| 60 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() and | 60 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() and |
| 61 // WebApkInstaller::CanUseGooglePlayInstallService() and | 61 // WebApkInstaller::CanUseGooglePlayInstallService() and |
| 62 // WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay() are stubbed out. | 62 // WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay() are stubbed out. |
| 63 class TestWebApkInstaller : public WebApkInstaller { | 63 class TestWebApkInstaller : public WebApkInstaller { |
| 64 public: | 64 public: |
| 65 TestWebApkInstaller(content::BrowserContext* browser_context, | 65 TestWebApkInstaller(content::BrowserContext* browser_context) |
| 66 const ShortcutInfo& shortcut_info, | 66 : WebApkInstaller(browser_context) {} |
| 67 const SkBitmap& primary_icon, | |
| 68 const SkBitmap& badge_icon) | |
| 69 : WebApkInstaller(browser_context, | |
| 70 shortcut_info, | |
| 71 primary_icon, | |
| 72 badge_icon) {} | |
| 73 | 67 |
| 74 void InstallOrUpdateWebApk(const std::string& package_name, | 68 void InstallOrUpdateWebApk(const std::string& package_name, |
| 75 int version, | 69 int version, |
| 76 const std::string& token) override { | 70 const std::string& token) override { |
| 77 PostTaskToRunSuccessCallback(); | 71 PostTaskToRunSuccessCallback(); |
| 78 } | 72 } |
| 79 | 73 |
| 80 void PostTaskToRunSuccessCallback() { | 74 void PostTaskToRunSuccessCallback() { |
| 81 base::ThreadTaskRunnerHandle::Get()->PostTask( | 75 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 82 FROM_HERE, | 76 FROM_HERE, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 94 WebApkInstallerRunner(content::BrowserContext* browser_context, | 88 WebApkInstallerRunner(content::BrowserContext* browser_context, |
| 95 const GURL& best_primary_icon_url, | 89 const GURL& best_primary_icon_url, |
| 96 const GURL& best_badge_icon_url) | 90 const GURL& best_badge_icon_url) |
| 97 : browser_context_(browser_context), | 91 : browser_context_(browser_context), |
| 98 best_primary_icon_url_(best_primary_icon_url), | 92 best_primary_icon_url_(best_primary_icon_url), |
| 99 best_badge_icon_url_(best_badge_icon_url) {} | 93 best_badge_icon_url_(best_badge_icon_url) {} |
| 100 | 94 |
| 101 ~WebApkInstallerRunner() {} | 95 ~WebApkInstallerRunner() {} |
| 102 | 96 |
| 103 void RunInstallWebApk() { | 97 void RunInstallWebApk() { |
| 98 base::RunLoop run_loop; | |
| 99 on_completed_callback_ = run_loop.QuitClosure(); | |
| 100 | |
| 101 ShortcutInfo info(GURL::EmptyGURL()); | |
|
dominickn
2017/06/14 00:14:23
Use GURL() (EmptyGURL() should only be used when y
pkotwicz
2017/06/15 02:25:20
Ok. This has to be
ShortcutInfo info((GURL()));
| |
| 102 info.best_primary_icon_url = best_primary_icon_url_; | |
| 103 info.best_badge_icon_url = best_badge_icon_url_; | |
| 104 WebApkInstaller::InstallAsyncForTesting( | 104 WebApkInstaller::InstallAsyncForTesting( |
| 105 CreateWebApkInstaller(), base::Bind(&WebApkInstallerRunner::OnCompleted, | 105 CreateWebApkInstaller(), info, SkBitmap(), SkBitmap(), |
| 106 base::Unretained(this))); | 106 base::Bind(&WebApkInstallerRunner::OnCompleted, |
| 107 Run(); | 107 base::Unretained(this))); |
| 108 | |
| 109 run_loop.Run(); | |
| 108 } | 110 } |
| 109 | 111 |
| 110 void RunUpdateWebApk() { | 112 void RunUpdateWebApk(const std::string& serialized_proto) { |
| 111 const int kWebApkVersion = 1; | 113 base::RunLoop run_loop; |
| 114 on_completed_callback_ = run_loop.QuitClosure(); | |
| 112 | 115 |
| 113 std::map<std::string, std::string> icon_url_to_murmur2_hash{ | 116 std::map<std::string, std::string> icon_url_to_murmur2_hash{ |
| 114 {best_primary_icon_url_.spec(), "0"}, | 117 {best_primary_icon_url_.spec(), "0"}, |
| 115 {best_badge_icon_url_.spec(), "0"}}; | 118 {best_badge_icon_url_.spec(), "0"}}; |
| 119 std::unique_ptr<std::vector<uint8_t>> serialized_proto_vector( | |
| 120 new std::vector<uint8_t>(serialized_proto.begin(), | |
|
dominickn
2017/06/14 00:14:23
base::MakeUnique
| |
| 121 serialized_proto.end())); | |
| 116 | 122 |
| 117 WebApkInstaller::UpdateAsyncForTesting( | 123 WebApkInstaller::UpdateAsyncForTesting( |
| 118 CreateWebApkInstaller(), kDownloadedWebApkPackageName, kWebApkVersion, | 124 CreateWebApkInstaller(), kDownloadedWebApkPackageName, |
| 119 icon_url_to_murmur2_hash, false /* is_manifest_stale */, | 125 GURL() /* start_url */, base::string16() /* short_name */, |
| 126 std::move(serialized_proto_vector), | |
| 120 base::Bind(&WebApkInstallerRunner::OnCompleted, | 127 base::Bind(&WebApkInstallerRunner::OnCompleted, |
| 121 base::Unretained(this))); | 128 base::Unretained(this))); |
| 122 Run(); | 129 |
| 130 run_loop.Run(); | |
| 123 } | 131 } |
| 124 | 132 |
| 125 WebApkInstaller* CreateWebApkInstaller() { | 133 WebApkInstaller* CreateWebApkInstaller() { |
| 126 ShortcutInfo info(GURL::EmptyGURL()); | |
| 127 info.best_primary_icon_url = best_primary_icon_url_; | |
| 128 info.best_badge_icon_url = best_badge_icon_url_; | |
| 129 | |
| 130 // WebApkInstaller owns itself. | 134 // WebApkInstaller owns itself. |
| 131 WebApkInstaller* installer = | 135 WebApkInstaller* installer = new TestWebApkInstaller(browser_context_); |
| 132 new TestWebApkInstaller(browser_context_, info, SkBitmap(), SkBitmap()); | |
| 133 installer->SetTimeoutMs(100); | 136 installer->SetTimeoutMs(100); |
| 134 return installer; | 137 return installer; |
| 135 } | 138 } |
| 136 | 139 |
| 137 void Run() { | |
| 138 base::RunLoop run_loop; | |
| 139 on_completed_callback_ = run_loop.QuitClosure(); | |
| 140 run_loop.Run(); | |
| 141 } | |
| 142 | |
| 143 WebApkInstallResult result() { return result_; } | 140 WebApkInstallResult result() { return result_; } |
| 144 | 141 |
| 145 private: | 142 private: |
| 146 void OnCompleted(WebApkInstallResult result, | 143 void OnCompleted(WebApkInstallResult result, |
| 147 bool relax_updates, | 144 bool relax_updates, |
| 148 const std::string& webapk_package) { | 145 const std::string& webapk_package) { |
| 149 result_ = result; | 146 result_ = result; |
| 150 on_completed_callback_.Run(); | 147 on_completed_callback_.Run(); |
| 151 } | 148 } |
| 152 | 149 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 | 205 |
| 209 base::RunLoop run_loop; | 206 base::RunLoop run_loop; |
| 210 on_completed_callback_ = run_loop.QuitClosure(); | 207 on_completed_callback_ = run_loop.QuitClosure(); |
| 211 run_loop.Run(); | 208 run_loop.Run(); |
| 212 } | 209 } |
| 213 | 210 |
| 214 webapk::WebApk* GetWebApkRequest() { return webapk_request_.get(); } | 211 webapk::WebApk* GetWebApkRequest() { return webapk_request_.get(); } |
| 215 | 212 |
| 216 private: | 213 private: |
| 217 // Called when the |webapk_request_| is populated. | 214 // Called when the |webapk_request_| is populated. |
| 218 void OnBuiltWebApkProto(std::unique_ptr<webapk::WebApk> webapk) { | 215 void OnBuiltWebApkProto( |
| 219 webapk_request_ = std::move(webapk); | 216 std::unique_ptr<std::vector<uint8_t>> serialized_proto) { |
| 217 webapk_request_.reset(new webapk::WebApk); | |
|
dominickn
2017/06/14 00:14:23
base::MakeUnique
| |
| 218 webapk_request_->ParseFromArray(serialized_proto->data(), | |
| 219 serialized_proto->size()); | |
| 220 on_completed_callback_.Run(); | 220 on_completed_callback_.Run(); |
| 221 } | 221 } |
| 222 | 222 |
| 223 content::BrowserContext* browser_context_; | 223 content::BrowserContext* browser_context_; |
| 224 | 224 |
| 225 // The populated webapk::WebApk. | 225 // The populated webapk::WebApk. |
| 226 std::unique_ptr<webapk::WebApk> webapk_request_; | 226 std::unique_ptr<webapk::WebApk> webapk_request_; |
| 227 | 227 |
| 228 // Called after the |webapk_request_| is built. | 228 // Called after the |webapk_request_| is built. |
| 229 base::Closure on_completed_callback_; | 229 base::Closure on_completed_callback_; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 381 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse)); | 381 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse)); |
| 382 | 382 |
| 383 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 383 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 384 runner->RunInstallWebApk(); | 384 runner->RunInstallWebApk(); |
| 385 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | 385 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
| 386 } | 386 } |
| 387 | 387 |
| 388 // Test update succeeding. | 388 // Test update succeeding. |
| 389 TEST_F(WebApkInstallerTest, UpdateSuccess) { | 389 TEST_F(WebApkInstallerTest, UpdateSuccess) { |
| 390 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 390 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 391 runner->RunUpdateWebApk(); | 391 runner->RunUpdateWebApk("non-empty"); |
| 392 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); | 392 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); |
| 393 } | 393 } |
| 394 | 394 |
| 395 // Test that an update suceeds if the WebAPK server returns a HTTP response with | 395 // Test that an update suceeds if the WebAPK server returns a HTTP response with |
| 396 // an empty token. The WebAPK server sends an empty download URL when: | 396 // an empty token. The WebAPK server sends an empty download URL when: |
| 397 // - The server is unable to update the WebAPK in the way that the client | 397 // - The server is unable to update the WebAPK in the way that the client |
| 398 // requested. | 398 // requested. |
| 399 // AND | 399 // AND |
| 400 // - The most up to date version of the WebAPK on the server is identical to the | 400 // - The most up to date version of the WebAPK on the server is identical to the |
| 401 // one installed on the client. | 401 // one installed on the client. |
| 402 TEST_F(WebApkInstallerTest, UpdateSuccessWithEmptyDownloadUrlInResponse) { | 402 TEST_F(WebApkInstallerTest, UpdateSuccessWithEmptyDownloadUrlInResponse) { |
| 403 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, "")); | 403 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, "")); |
| 404 | 404 |
| 405 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 405 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 406 runner->RunUpdateWebApk(); | 406 runner->RunUpdateWebApk("non-empty"); |
| 407 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); | 407 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); |
| 408 } | 408 } |
| 409 | 409 |
| 410 // Test that an update fails if an empty proto is passed to UpdateAsync(). | |
| 411 TEST_F(WebApkInstallerTest, UpdateFailsEmptyProto) { | |
| 412 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
| 413 runner->RunUpdateWebApk(""); | |
| 414 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | |
| 415 } | |
| 416 | |
| 410 // When there is no Web Manifest available for a site, an empty | 417 // When there is no Web Manifest available for a site, an empty |
| 411 // |best_primary_icon_url| is used to build a WebApk update request. Tests the | 418 // |best_primary_icon_url| is used to build a WebApk update request. Tests the |
| 412 // request can be built properly. | 419 // request can be built properly. |
| 413 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { | 420 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { |
| 414 std::string icon_url_1 = test_server()->GetURL("/icon1.png").spec(); | 421 std::string icon_url_1 = test_server()->GetURL("/icon1.png").spec(); |
| 415 std::string icon_url_2 = test_server()->GetURL("/icon2.png").spec(); | 422 std::string icon_url_2 = test_server()->GetURL("/icon2.png").spec(); |
| 416 std::map<std::string, std::string> icon_url_to_murmur2_hash; | 423 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| 417 icon_url_to_murmur2_hash[icon_url_1] = "1"; | 424 icon_url_to_murmur2_hash[icon_url_1] = "1"; |
| 418 icon_url_to_murmur2_hash[icon_url_2] = "2"; | 425 icon_url_to_murmur2_hash[icon_url_2] = "2"; |
| 419 | 426 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 520 EXPECT_FALSE(icons[0].has_image_data()); | 527 EXPECT_FALSE(icons[0].has_image_data()); |
| 521 | 528 |
| 522 // Check protobuf fields for kBestPrimaryIconUrl. | 529 // Check protobuf fields for kBestPrimaryIconUrl. |
| 523 EXPECT_EQ(best_icon_url, icons[1].src()); | 530 EXPECT_EQ(best_icon_url, icons[1].src()); |
| 524 EXPECT_EQ(icon_url_to_murmur2_hash[best_icon_url], icons[1].hash()); | 531 EXPECT_EQ(icon_url_to_murmur2_hash[best_icon_url], icons[1].hash()); |
| 525 EXPECT_THAT(icons[1].usages(), | 532 EXPECT_THAT(icons[1].usages(), |
| 526 testing::ElementsAre(webapk::Image::PRIMARY_ICON, | 533 testing::ElementsAre(webapk::Image::PRIMARY_ICON, |
| 527 webapk::Image::BADGE_ICON)); | 534 webapk::Image::BADGE_ICON)); |
| 528 EXPECT_TRUE(icons[1].has_image_data()); | 535 EXPECT_TRUE(icons[1].has_image_data()); |
| 529 } | 536 } |
| OLD | NEW |