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 // WebApkInstaller subclass where | 55 // WebApkInstaller subclass where |
56 // WebApkInstaller::StartInstallingDownloadedWebApk() and | 56 // WebApkInstaller::StartInstallingDownloadedWebApk() and |
57 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() and | 57 // WebApkInstaller::StartUpdateUsingDownloadedWebApk() and |
58 // WebApkInstaller::CanUseGooglePlayInstallService() and | 58 // WebApkInstaller::CanUseGooglePlayInstallService() and |
59 // WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay() are stubbed out. | 59 // WebApkInstaller::InstallOrUpdateWebApkFromGooglePlay() are stubbed out. |
60 class TestWebApkInstaller : public WebApkInstaller { | 60 class TestWebApkInstaller : public WebApkInstaller { |
61 public: | 61 public: |
62 TestWebApkInstaller(content::BrowserContext* browser_context, | 62 TestWebApkInstaller(content::BrowserContext* browser_context, |
63 const ShortcutInfo& shortcut_info, | 63 const ShortcutInfo& shortcut_info, |
64 const SkBitmap& shortcut_icon, | 64 const SkBitmap& shortcut_icon, |
65 bool can_use_google_play_install_service) | 65 bool can_install_webapks) |
66 : WebApkInstaller(browser_context, shortcut_info, shortcut_icon), | 66 : WebApkInstaller(browser_context, shortcut_info, shortcut_icon), |
67 can_use_google_play_install_service_( | 67 can_install_webapks_(can_install_webapks) {} |
68 can_use_google_play_install_service) {} | |
69 | 68 |
70 void InstallDownloadedWebApk( | 69 bool CanInstallWebApks() override { return can_install_webapks_; } |
71 JNIEnv* env, | 70 |
72 const base::android::ScopedJavaLocalRef<jstring>& file_path, | 71 void InstallOrUpdateWebApk(const std::string& package_name, |
73 const base::android::ScopedJavaLocalRef<jstring>& package_name) override { | 72 int version, |
| 73 const std::string& token) override { |
74 PostTaskToRunSuccessCallback(); | 74 PostTaskToRunSuccessCallback(); |
75 } | 75 } |
76 | 76 |
77 void UpdateUsingDownloadedWebApk( | |
78 JNIEnv* env, | |
79 const base::android::ScopedJavaLocalRef<jstring>& file_path) override { | |
80 PostTaskToRunSuccessCallback(); | |
81 } | |
82 | |
83 bool CanUseGooglePlayInstallService() override { | |
84 return can_use_google_play_install_service_; | |
85 } | |
86 | |
87 void InstallOrUpdateWebApkFromGooglePlay(const std::string& package_name, | |
88 int version, | |
89 const std::string& token) override { | |
90 PostTaskToRunSuccessCallback(); | |
91 } | |
92 | |
93 void PostTaskToRunSuccessCallback() { | 77 void PostTaskToRunSuccessCallback() { |
94 base::ThreadTaskRunnerHandle::Get()->PostTask( | 78 base::ThreadTaskRunnerHandle::Get()->PostTask( |
95 FROM_HERE, | 79 FROM_HERE, |
96 base::Bind(&TestWebApkInstaller::OnResult, base::Unretained(this), | 80 base::Bind(&TestWebApkInstaller::OnResult, base::Unretained(this), |
97 WebApkInstallResult::SUCCESS)); | 81 WebApkInstallResult::SUCCESS)); |
98 } | 82 } |
99 | 83 |
100 private: | 84 private: |
101 // Whether the Google Play Services can be used and the install delegate is | 85 // Whether the Google Play Services can be used and the install delegate is |
102 // available. | 86 // available. |
103 bool can_use_google_play_install_service_; | 87 bool can_install_webapks_; |
104 | 88 |
105 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); | 89 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); |
106 }; | 90 }; |
107 | 91 |
108 // Runs the WebApkInstaller installation process/update and blocks till done. | 92 // Runs the WebApkInstaller installation process/update and blocks till done. |
109 class WebApkInstallerRunner { | 93 class WebApkInstallerRunner { |
110 public: | 94 public: |
111 WebApkInstallerRunner(content::BrowserContext* browser_context, | 95 WebApkInstallerRunner(content::BrowserContext* browser_context, |
112 const GURL& best_primary_icon_url) | 96 const GURL& best_primary_icon_url) |
113 : browser_context_(browser_context), | 97 : browser_context_(browser_context), |
114 best_primary_icon_url_(best_primary_icon_url), | 98 best_primary_icon_url_(best_primary_icon_url), |
115 can_use_google_play_install_service_(false) {} | 99 can_install_webapks_(true) {} |
116 | 100 |
117 ~WebApkInstallerRunner() {} | 101 ~WebApkInstallerRunner() {} |
118 | 102 |
119 void SetCanUseGooglePlayInstallService( | 103 void SetCanInstallWebApks(bool can_install_webapks) { |
120 bool can_use_google_play_install_service) { | 104 can_install_webapks_ = can_install_webapks; |
121 can_use_google_play_install_service_ = can_use_google_play_install_service; | |
122 } | 105 } |
123 | 106 |
124 void RunInstallWebApk() { | 107 void RunInstallWebApk() { |
125 WebApkInstaller::InstallAsyncForTesting( | 108 WebApkInstaller::InstallAsyncForTesting( |
126 CreateWebApkInstaller(), base::Bind(&WebApkInstallerRunner::OnCompleted, | 109 CreateWebApkInstaller(), base::Bind(&WebApkInstallerRunner::OnCompleted, |
127 base::Unretained(this))); | 110 base::Unretained(this))); |
128 Run(); | 111 Run(); |
129 } | 112 } |
130 | 113 |
131 void RunUpdateWebApk() { | 114 void RunUpdateWebApk() { |
132 const int kWebApkVersion = 1; | 115 const int kWebApkVersion = 1; |
133 | 116 |
134 std::map<std::string, std::string> icon_url_to_murmur2_hash{ | 117 std::map<std::string, std::string> icon_url_to_murmur2_hash{ |
135 {best_primary_icon_url_.spec(), "0"}}; | 118 {best_primary_icon_url_.spec(), "0"}}; |
136 | 119 |
137 WebApkInstaller::UpdateAsyncForTesting( | 120 WebApkInstaller::UpdateAsyncForTesting( |
138 CreateWebApkInstaller(), kDownloadedWebApkPackageName, kWebApkVersion, | 121 CreateWebApkInstaller(), kDownloadedWebApkPackageName, kWebApkVersion, |
139 icon_url_to_murmur2_hash, false /* is_manifest_stale */, | 122 icon_url_to_murmur2_hash, false /* is_manifest_stale */, |
140 base::Bind(&WebApkInstallerRunner::OnCompleted, | 123 base::Bind(&WebApkInstallerRunner::OnCompleted, |
141 base::Unretained(this))); | 124 base::Unretained(this))); |
142 Run(); | 125 Run(); |
143 } | 126 } |
144 | 127 |
145 WebApkInstaller* CreateWebApkInstaller() { | 128 WebApkInstaller* CreateWebApkInstaller() { |
146 ShortcutInfo info(GURL::EmptyGURL()); | 129 ShortcutInfo info(GURL::EmptyGURL()); |
147 info.best_primary_icon_url = best_primary_icon_url_; | 130 info.best_primary_icon_url = best_primary_icon_url_; |
148 | 131 |
149 // WebApkInstaller owns itself. | 132 // WebApkInstaller owns itself. |
150 WebApkInstaller* installer = | 133 WebApkInstaller* installer = new TestWebApkInstaller( |
151 new TestWebApkInstaller(browser_context_, info, SkBitmap(), | 134 browser_context_, info, SkBitmap(), can_install_webapks_); |
152 can_use_google_play_install_service_); | |
153 installer->SetTimeoutMs(100); | 135 installer->SetTimeoutMs(100); |
154 return installer; | 136 return installer; |
155 } | 137 } |
156 | 138 |
157 void Run() { | 139 void Run() { |
158 base::RunLoop run_loop; | 140 base::RunLoop run_loop; |
159 on_completed_callback_ = run_loop.QuitClosure(); | 141 on_completed_callback_ = run_loop.QuitClosure(); |
160 run_loop.Run(); | 142 run_loop.Run(); |
161 } | 143 } |
162 | 144 |
(...skipping 11 matching lines...) Expand all Loading... |
174 | 156 |
175 // The Web Manifest's icon URL. | 157 // The Web Manifest's icon URL. |
176 const GURL best_primary_icon_url_; | 158 const GURL best_primary_icon_url_; |
177 | 159 |
178 // Called after the installation process has succeeded or failed. | 160 // Called after the installation process has succeeded or failed. |
179 base::Closure on_completed_callback_; | 161 base::Closure on_completed_callback_; |
180 | 162 |
181 // The result of the installation process. | 163 // The result of the installation process. |
182 WebApkInstallResult result_; | 164 WebApkInstallResult result_; |
183 | 165 |
184 // Whether Google Play Service can be used and the install delegate is | 166 // Whether the device supports installation of WebApks. |
185 // available. | 167 bool can_install_webapks_; |
186 bool can_use_google_play_install_service_; | |
187 | 168 |
188 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); | 169 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); |
189 }; | 170 }; |
190 | 171 |
191 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download | 172 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download |
192 // URL. | 173 // URL. |
193 std::unique_ptr<net::test_server::HttpResponse> BuildValidWebApkResponse( | 174 std::unique_ptr<net::test_server::HttpResponse> BuildValidWebApkResponse( |
194 const GURL& download_url) { | 175 const GURL& download_url) { |
195 std::unique_ptr<webapk::WebApkResponse> response_proto( | 176 std::unique_ptr<webapk::WebApkResponse> response_proto( |
196 new webapk::WebApkResponse); | 177 new webapk::WebApkResponse); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 // Test that installation fails if the WebAPK creation request times out. | 346 // Test that installation fails if the WebAPK creation request times out. |
366 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { | 347 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { |
367 GURL server_url = test_server()->GetURL("/slow?1000"); | 348 GURL server_url = test_server()->GetURL("/slow?1000"); |
368 SetWebApkServerUrl(server_url); | 349 SetWebApkServerUrl(server_url); |
369 | 350 |
370 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 351 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
371 runner->RunInstallWebApk(); | 352 runner->RunInstallWebApk(); |
372 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | 353 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
373 } | 354 } |
374 | 355 |
375 // Test that installation fails if the WebAPK download times out. | |
376 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { | |
377 GURL download_url = test_server()->GetURL("/slow?1000"); | |
378 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); | |
379 | |
380 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
381 runner->RunInstallWebApk(); | |
382 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | |
383 } | |
384 | |
385 // Test that installation fails if the WebAPK download fails. | |
386 TEST_F(WebApkInstallerTest, WebApkDownloadFails) { | |
387 GURL download_url = test_server()->GetURL("/nocontent"); | |
388 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); | |
389 | |
390 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
391 runner->RunInstallWebApk(); | |
392 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); | |
393 } | |
394 | |
395 namespace { | 356 namespace { |
396 | 357 |
397 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. | 358 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. |
398 std::unique_ptr<net::test_server::HttpResponse> | 359 std::unique_ptr<net::test_server::HttpResponse> |
399 BuildUnparsableWebApkResponse() { | 360 BuildUnparsableWebApkResponse() { |
400 std::unique_ptr<net::test_server::BasicHttpResponse> response( | 361 std::unique_ptr<net::test_server::BasicHttpResponse> response( |
401 new net::test_server::BasicHttpResponse()); | 362 new net::test_server::BasicHttpResponse()); |
402 response->set_code(net::HTTP_OK); | 363 response->set_code(net::HTTP_OK); |
403 response->set_content("😀"); | 364 response->set_content("😀"); |
404 return std::move(response); | 365 return std::move(response); |
(...skipping 26 matching lines...) Expand all Loading... |
431 // - The most up to date version of the WebAPK on the server is identical to the | 392 // - The most up to date version of the WebAPK on the server is identical to the |
432 // one installed on the client. | 393 // one installed on the client. |
433 TEST_F(WebApkInstallerTest, UpdateSuccessWithEmptyDownloadUrlInResponse) { | 394 TEST_F(WebApkInstallerTest, UpdateSuccessWithEmptyDownloadUrlInResponse) { |
434 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, GURL())); | 395 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, GURL())); |
435 | 396 |
436 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | 397 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
437 runner->RunUpdateWebApk(); | 398 runner->RunUpdateWebApk(); |
438 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); | 399 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); |
439 } | 400 } |
440 | 401 |
441 // Test installation succeeds using Google Play. | |
442 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { | |
443 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); | |
444 runner->SetCanUseGooglePlayInstallService(true); | |
445 runner->RunInstallWebApk(); | |
446 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result()); | |
447 } | |
448 | |
449 // When there is no Web Manifest available for a site, an empty | 402 // When there is no Web Manifest available for a site, an empty |
450 // |best_primary_icon_url| is used to build a WebApk update request. Tests the | 403 // |best_primary_icon_url| is used to build a WebApk update request. Tests the |
451 // request can be built properly. | 404 // request can be built properly. |
452 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { | 405 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { |
453 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); | 406 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); |
454 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); | 407 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); |
455 std::string icon_murmur2_hash_1 = "1"; | 408 std::string icon_murmur2_hash_1 = "1"; |
456 std::string icon_murmur2_hash_2 = "2"; | 409 std::string icon_murmur2_hash_2 = "2"; |
457 std::map<std::string, std::string> icon_url_to_murmur2_hash; | 410 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
458 icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; | 411 icon_url_to_murmur2_hash[icon_url_1.spec()] = icon_murmur2_hash_1; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 icons[i] = manifest.icons(i); | 463 icons[i] = manifest.icons(i); |
511 | 464 |
512 EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src()); | 465 EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src()); |
513 EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash()); | 466 EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash()); |
514 EXPECT_TRUE(icons[0].has_image_data()); | 467 EXPECT_TRUE(icons[0].has_image_data()); |
515 | 468 |
516 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); | 469 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); |
517 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); | 470 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); |
518 EXPECT_FALSE(icons[1].has_image_data()); | 471 EXPECT_FALSE(icons[1].has_image_data()); |
519 } | 472 } |
| 473 |
| 474 TEST_F(WebApkInstallerTest, FailsWhenInstallDisabled) { |
| 475 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); |
| 476 runner->SetCanInstallWebApks(false); |
| 477 runner->RunInstallWebApk(); |
| 478 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result()); |
| 479 } |
OLD | NEW |