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