Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: chrome/browser/android/webapk/webapk_installer_unittest.cc

Issue 2733543002: [Android:WebAPK] Don't add webapp to homescreen if WebAPK install times out part 1/3 (Closed)
Patch Set: Merge branch 'start' into refactor_shortcut_helper3 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/single_thread_task_runner.h" 18 #include "base/single_thread_task_runner.h"
19 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
20 #include "chrome/browser/android/shortcut_info.h" 20 #include "chrome/browser/android/shortcut_info.h"
21 #include "chrome/browser/android/webapk/webapk.pb.h" 21 #include "chrome/browser/android/webapk/webapk.pb.h"
22 #include "chrome/browser/android/webapk/webapk_install_service.h"
22 #include "chrome/common/chrome_switches.h" 23 #include "chrome/common/chrome_switches.h"
23 #include "chrome/test/base/testing_profile.h" 24 #include "chrome/test/base/testing_profile.h"
24 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
25 #include "content/public/test/test_browser_thread_bundle.h" 26 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "net/test/embedded_test_server/embedded_test_server.h" 27 #include "net/test/embedded_test_server/embedded_test_server.h"
27 #include "net/test/embedded_test_server/http_request.h" 28 #include "net/test/embedded_test_server/http_request.h"
28 #include "net/test/embedded_test_server/http_response.h" 29 #include "net/test/embedded_test_server/http_response.h"
29 #include "net/url_request/url_request_test_util.h" 30 #include "net/url_request/url_request_test_util.h"
30 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
31 #include "third_party/skia/include/core/SkBitmap.h" 32 #include "third_party/skia/include/core/SkBitmap.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 86
86 void InstallOrUpdateWebApkFromGooglePlay(const std::string& package_name, 87 void InstallOrUpdateWebApkFromGooglePlay(const std::string& package_name,
87 int version, 88 int version,
88 const std::string& token) override { 89 const std::string& token) override {
89 PostTaskToRunSuccessCallback(); 90 PostTaskToRunSuccessCallback();
90 } 91 }
91 92
92 void PostTaskToRunSuccessCallback() { 93 void PostTaskToRunSuccessCallback() {
93 base::ThreadTaskRunnerHandle::Get()->PostTask( 94 base::ThreadTaskRunnerHandle::Get()->PostTask(
94 FROM_HERE, 95 FROM_HERE,
95 base::Bind(&TestWebApkInstaller::OnSuccess, base::Unretained(this))); 96 base::Bind(&TestWebApkInstaller::OnResult, base::Unretained(this),
97 WebApkInstallResult::SUCCESS));
96 } 98 }
97 99
98 private: 100 private:
99 // Whether the Google Play Services can be used and the install delegate is 101 // Whether the Google Play Services can be used and the install delegate is
100 // available. 102 // available.
101 bool can_use_google_play_install_service_; 103 bool can_use_google_play_install_service_;
102 104
103 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); 105 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller);
104 }; 106 };
105 107
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 installer->SetTimeoutMs(100); 153 installer->SetTimeoutMs(100);
152 return installer; 154 return installer;
153 } 155 }
154 156
155 void Run() { 157 void Run() {
156 base::RunLoop run_loop; 158 base::RunLoop run_loop;
157 on_completed_callback_ = run_loop.QuitClosure(); 159 on_completed_callback_ = run_loop.QuitClosure();
158 run_loop.Run(); 160 run_loop.Run();
159 } 161 }
160 162
161 bool success() { return success_; } 163 WebApkInstallResult result() { return result_; }
162 164
163 private: 165 private:
164 void OnCompleted(bool success, 166 void OnCompleted(WebApkInstallResult result,
165 bool relax_updates, 167 bool relax_updates,
166 const std::string& webapk_package) { 168 const std::string& webapk_package) {
167 success_ = success; 169 result_ = result;
168 on_completed_callback_.Run(); 170 on_completed_callback_.Run();
169 } 171 }
170 172
171 content::BrowserContext* browser_context_; 173 content::BrowserContext* browser_context_;
172 174
173 // The Web Manifest's icon URL. 175 // The Web Manifest's icon URL.
174 const GURL best_primary_icon_url_; 176 const GURL best_primary_icon_url_;
175 177
176 // Called after the installation process has succeeded or failed. 178 // Called after the installation process has succeeded or failed.
177 base::Closure on_completed_callback_; 179 base::Closure on_completed_callback_;
178 180
179 // Whether the installation process succeeded. 181 // The result of the installation process.
180 bool success_; 182 WebApkInstallResult result_;
181 183
182 // Whether Google Play Service can be used and the install delegate is 184 // Whether Google Play Service can be used and the install delegate is
183 // available. 185 // available.
184 bool can_use_google_play_install_service_; 186 bool can_use_google_play_install_service_;
185 187
186 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); 188 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner);
187 }; 189 };
188 190
189 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download 191 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download
190 // URL. 192 // URL.
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 // Builds response to the WebAPK creation request. 340 // Builds response to the WebAPK creation request.
339 WebApkResponseBuilder webapk_response_builder_; 341 WebApkResponseBuilder webapk_response_builder_;
340 342
341 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); 343 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest);
342 }; 344 };
343 345
344 // Test installation succeeding. 346 // Test installation succeeding.
345 TEST_F(WebApkInstallerTest, Success) { 347 TEST_F(WebApkInstallerTest, Success) {
346 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 348 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
347 runner->RunInstallWebApk(); 349 runner->RunInstallWebApk();
348 EXPECT_TRUE(runner->success()); 350 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result());
349 } 351 }
350 352
351 // Test that installation fails if fetching the bitmap at the best primary icon 353 // Test that installation fails if fetching the bitmap at the best primary icon
352 // URL times out. In a perfect world the fetch would never time out because the 354 // URL times out. In a perfect world the fetch would never time out because the
353 // bitmap at the best primary icon URL should be in the HTTP cache. 355 // bitmap at the best primary icon URL should be in the HTTP cache.
354 TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) { 356 TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) {
355 GURL best_primary_icon_url = test_server()->GetURL("/slow?1000"); 357 GURL best_primary_icon_url = test_server()->GetURL("/slow?1000");
356 SetBestPrimaryIconUrl(best_primary_icon_url); 358 SetBestPrimaryIconUrl(best_primary_icon_url);
357 359
358 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 360 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
359 runner->RunInstallWebApk(); 361 runner->RunInstallWebApk();
360 EXPECT_FALSE(runner->success()); 362 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
361 } 363 }
362 364
363 // Test that installation fails if the WebAPK creation request times out. 365 // Test that installation fails if the WebAPK creation request times out.
364 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { 366 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) {
365 GURL server_url = test_server()->GetURL("/slow?1000"); 367 GURL server_url = test_server()->GetURL("/slow?1000");
366 SetWebApkServerUrl(server_url); 368 SetWebApkServerUrl(server_url);
367 369
368 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 370 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
369 runner->RunInstallWebApk(); 371 runner->RunInstallWebApk();
370 EXPECT_FALSE(runner->success()); 372 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
371 } 373 }
372 374
373 // Test that installation fails if the WebAPK download times out. 375 // Test that installation fails if the WebAPK download times out.
374 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { 376 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) {
375 GURL download_url = test_server()->GetURL("/slow?1000"); 377 GURL download_url = test_server()->GetURL("/slow?1000");
376 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); 378 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url));
377 379
378 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 380 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
379 runner->RunInstallWebApk(); 381 runner->RunInstallWebApk();
380 EXPECT_FALSE(runner->success()); 382 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
381 } 383 }
382 384
383 // Test that installation fails if the WebAPK download fails. 385 // Test that installation fails if the WebAPK download fails.
384 TEST_F(WebApkInstallerTest, WebApkDownloadFails) { 386 TEST_F(WebApkInstallerTest, WebApkDownloadFails) {
385 GURL download_url = test_server()->GetURL("/nocontent"); 387 GURL download_url = test_server()->GetURL("/nocontent");
386 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); 388 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url));
387 389
388 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 390 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
389 runner->RunInstallWebApk(); 391 runner->RunInstallWebApk();
390 EXPECT_FALSE(runner->success()); 392 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
391 } 393 }
392 394
393 namespace { 395 namespace {
394 396
395 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. 397 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse.
396 std::unique_ptr<net::test_server::HttpResponse> 398 std::unique_ptr<net::test_server::HttpResponse>
397 BuildUnparsableWebApkResponse() { 399 BuildUnparsableWebApkResponse() {
398 std::unique_ptr<net::test_server::BasicHttpResponse> response( 400 std::unique_ptr<net::test_server::BasicHttpResponse> response(
399 new net::test_server::BasicHttpResponse()); 401 new net::test_server::BasicHttpResponse());
400 response->set_code(net::HTTP_OK); 402 response->set_code(net::HTTP_OK);
401 response->set_content("😀"); 403 response->set_content("😀");
402 return std::move(response); 404 return std::move(response);
403 } 405 }
404 406
405 } // anonymous namespace 407 } // anonymous namespace
406 408
407 // Test that an HTTP response which cannot be parsed as a webapk::WebApkResponse 409 // Test that an HTTP response which cannot be parsed as a webapk::WebApkResponse
408 // is handled properly. 410 // is handled properly.
409 TEST_F(WebApkInstallerTest, UnparsableCreateWebApkResponse) { 411 TEST_F(WebApkInstallerTest, UnparsableCreateWebApkResponse) {
410 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse)); 412 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse));
411 413
412 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 414 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
413 runner->RunInstallWebApk(); 415 runner->RunInstallWebApk();
414 EXPECT_FALSE(runner->success()); 416 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
415 } 417 }
416 418
417 // Test update succeeding. 419 // Test update succeeding.
418 TEST_F(WebApkInstallerTest, UpdateSuccess) { 420 TEST_F(WebApkInstallerTest, UpdateSuccess) {
419 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 421 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
420 runner->RunUpdateWebApk(); 422 runner->RunUpdateWebApk();
421 EXPECT_TRUE(runner->success()); 423 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result());
422 } 424 }
423 425
424 // Test that an update suceeds if the WebAPK server returns a HTTP response with 426 // Test that an update suceeds if the WebAPK server returns a HTTP response with
425 // an empty download URL. The WebAPK server sends an empty download URL when: 427 // an empty download URL. The WebAPK server sends an empty download URL when:
426 // - The server is unable to update the WebAPK in the way that the client 428 // - The server is unable to update the WebAPK in the way that the client
427 // requested. 429 // requested.
428 // AND 430 // AND
429 // - The most up to date version of the WebAPK on the server is identical to the 431 // - The most up to date version of the WebAPK on the server is identical to the
430 // one installed on the client. 432 // one installed on the client.
431 TEST_F(WebApkInstallerTest, UpdateSuccessWithEmptyDownloadUrlInResponse) { 433 TEST_F(WebApkInstallerTest, UpdateSuccessWithEmptyDownloadUrlInResponse) {
432 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, GURL())); 434 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, GURL()));
433 435
434 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 436 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
435 runner->RunUpdateWebApk(); 437 runner->RunUpdateWebApk();
436 EXPECT_TRUE(runner->success()); 438 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result());
437 } 439 }
438 440
439 // Test installation succeeds using Google Play. 441 // Test installation succeeds using Google Play.
440 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { 442 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) {
441 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 443 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
442 runner->SetCanUseGooglePlayInstallService(true); 444 runner->SetCanUseGooglePlayInstallService(true);
443 runner->RunInstallWebApk(); 445 runner->RunInstallWebApk();
444 EXPECT_TRUE(runner->success()); 446 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result());
445 } 447 }
446 448
447 // When there is no Web Manifest available for a site, an empty 449 // When there is no Web Manifest available for a site, an empty
448 // |best_primary_icon_url| is used to build a WebApk update request. Tests the 450 // |best_primary_icon_url| is used to build a WebApk update request. Tests the
449 // request can be built properly. 451 // request can be built properly.
450 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { 452 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) {
451 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); 453 GURL icon_url_1 = test_server()->GetURL("/icon1.png");
452 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); 454 GURL icon_url_2 = test_server()->GetURL("/icon2.png");
453 std::string icon_murmur2_hash_1 = "1"; 455 std::string icon_murmur2_hash_1 = "1";
454 std::string icon_murmur2_hash_2 = "2"; 456 std::string icon_murmur2_hash_2 = "2";
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 icons[i] = manifest.icons(i); 510 icons[i] = manifest.icons(i);
509 511
510 EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src()); 512 EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src());
511 EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash()); 513 EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash());
512 EXPECT_TRUE(icons[0].has_image_data()); 514 EXPECT_TRUE(icons[0].has_image_data());
513 515
514 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); 516 EXPECT_EQ(icon_url_1.spec(), icons[1].src());
515 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); 517 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash());
516 EXPECT_FALSE(icons[1].has_image_data()); 518 EXPECT_FALSE(icons[1].has_image_data());
517 } 519 }
OLDNEW
« no previous file with comments | « chrome/browser/android/webapk/webapk_installer.cc ('k') | chrome/browser/android/webapk/webapk_update_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698