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

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 'start1' 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 80
80 void InstallOrUpdateWebApkFromGooglePlay(const std::string& package_name, 81 void InstallOrUpdateWebApkFromGooglePlay(const std::string& package_name,
81 int version, 82 int version,
82 const std::string& token) override { 83 const std::string& token) override {
83 PostTaskToRunSuccessCallback(); 84 PostTaskToRunSuccessCallback();
84 } 85 }
85 86
86 void PostTaskToRunSuccessCallback() { 87 void PostTaskToRunSuccessCallback() {
87 base::ThreadTaskRunnerHandle::Get()->PostTask( 88 base::ThreadTaskRunnerHandle::Get()->PostTask(
88 FROM_HERE, 89 FROM_HERE,
89 base::Bind(&TestWebApkInstaller::OnSuccess, base::Unretained(this))); 90 base::Bind(&TestWebApkInstaller::OnResult, base::Unretained(this),
91 WebApkInstallResult::SUCCESS));
90 } 92 }
91 93
92 private: 94 private:
93 // Whether the Google Play Services can be used and the install delegate is 95 // Whether the Google Play Services can be used and the install delegate is
94 // available. 96 // available.
95 bool can_use_google_play_install_service_; 97 bool can_use_google_play_install_service_;
96 98
97 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller); 99 DISALLOW_COPY_AND_ASSIGN(TestWebApkInstaller);
98 }; 100 };
99 101
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 installer->SetTimeoutMs(100); 147 installer->SetTimeoutMs(100);
146 return installer; 148 return installer;
147 } 149 }
148 150
149 void Run() { 151 void Run() {
150 base::RunLoop run_loop; 152 base::RunLoop run_loop;
151 on_completed_callback_ = run_loop.QuitClosure(); 153 on_completed_callback_ = run_loop.QuitClosure();
152 run_loop.Run(); 154 run_loop.Run();
153 } 155 }
154 156
155 bool success() { return success_; } 157 WebApkInstallResult result() { return result_; }
156 158
157 private: 159 private:
158 void OnCompleted(bool success, 160 void OnCompleted(WebApkInstallResult result,
159 bool relax_updates, 161 bool relax_updates,
160 const std::string& webapk_package) { 162 const std::string& webapk_package) {
161 success_ = success; 163 result_ = result;
162 on_completed_callback_.Run(); 164 on_completed_callback_.Run();
163 } 165 }
164 166
165 content::BrowserContext* browser_context_; 167 content::BrowserContext* browser_context_;
166 168
167 // The Web Manifest's icon URL. 169 // The Web Manifest's icon URL.
168 const GURL best_primary_icon_url_; 170 const GURL best_primary_icon_url_;
169 171
170 // Called after the installation process has succeeded or failed. 172 // Called after the installation process has succeeded or failed.
171 base::Closure on_completed_callback_; 173 base::Closure on_completed_callback_;
172 174
173 // Whether the installation process succeeded. 175 // The result of the installation process.
174 bool success_; 176 WebApkInstallResult result_;
175 177
176 // Whether Google Play Service can be used and the install delegate is 178 // Whether Google Play Service can be used and the install delegate is
177 // available. 179 // available.
178 bool can_use_google_play_install_service_; 180 bool can_use_google_play_install_service_;
179 181
180 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner); 182 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerRunner);
181 }; 183 };
182 184
183 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download 185 // Builds a webapk::WebApkResponse with |download_url| as the WebAPK download
184 // URL. 186 // URL.
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 // Builds response to the WebAPK creation request. 334 // Builds response to the WebAPK creation request.
333 WebApkResponseBuilder webapk_response_builder_; 335 WebApkResponseBuilder webapk_response_builder_;
334 336
335 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest); 337 DISALLOW_COPY_AND_ASSIGN(WebApkInstallerTest);
336 }; 338 };
337 339
338 // Test installation succeeding. 340 // Test installation succeeding.
339 TEST_F(WebApkInstallerTest, Success) { 341 TEST_F(WebApkInstallerTest, Success) {
340 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 342 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
341 runner->RunInstallWebApk(); 343 runner->RunInstallWebApk();
342 EXPECT_TRUE(runner->success()); 344 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result());
343 } 345 }
344 346
345 // Test that installation fails if fetching the bitmap at the best primary icon 347 // Test that installation fails if fetching the bitmap at the best primary icon
346 // URL times out. In a perfect world the fetch would never time out because the 348 // URL times out. In a perfect world the fetch would never time out because the
347 // bitmap at the best primary icon URL should be in the HTTP cache. 349 // bitmap at the best primary icon URL should be in the HTTP cache.
348 TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) { 350 TEST_F(WebApkInstallerTest, BestPrimaryIconUrlDownloadTimesOut) {
349 GURL best_primary_icon_url = test_server()->GetURL("/slow?1000"); 351 GURL best_primary_icon_url = test_server()->GetURL("/slow?1000");
350 SetBestPrimaryIconUrl(best_primary_icon_url); 352 SetBestPrimaryIconUrl(best_primary_icon_url);
351 353
352 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 354 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
353 runner->RunInstallWebApk(); 355 runner->RunInstallWebApk();
354 EXPECT_FALSE(runner->success()); 356 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
355 } 357 }
356 358
357 // Test that installation fails if the WebAPK creation request times out. 359 // Test that installation fails if the WebAPK creation request times out.
358 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) { 360 TEST_F(WebApkInstallerTest, CreateWebApkRequestTimesOut) {
359 GURL server_url = test_server()->GetURL("/slow?1000"); 361 GURL server_url = test_server()->GetURL("/slow?1000");
360 SetWebApkServerUrl(server_url); 362 SetWebApkServerUrl(server_url);
361 363
362 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 364 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
363 runner->RunInstallWebApk(); 365 runner->RunInstallWebApk();
364 EXPECT_FALSE(runner->success()); 366 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
365 } 367 }
366 368
367 // Test that installation fails if the WebAPK download times out. 369 // Test that installation fails if the WebAPK download times out.
368 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) { 370 TEST_F(WebApkInstallerTest, WebApkDownloadTimesOut) {
369 GURL download_url = test_server()->GetURL("/slow?1000"); 371 GURL download_url = test_server()->GetURL("/slow?1000");
370 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); 372 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url));
371 373
372 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 374 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
373 runner->RunInstallWebApk(); 375 runner->RunInstallWebApk();
374 EXPECT_FALSE(runner->success()); 376 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
375 } 377 }
376 378
377 // Test that installation fails if the WebAPK download fails. 379 // Test that installation fails if the WebAPK download fails.
378 TEST_F(WebApkInstallerTest, WebApkDownloadFails) { 380 TEST_F(WebApkInstallerTest, WebApkDownloadFails) {
379 GURL download_url = test_server()->GetURL("/nocontent"); 381 GURL download_url = test_server()->GetURL("/nocontent");
380 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url)); 382 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, download_url));
381 383
382 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 384 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
383 runner->RunInstallWebApk(); 385 runner->RunInstallWebApk();
384 EXPECT_FALSE(runner->success()); 386 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
385 } 387 }
386 388
387 namespace { 389 namespace {
388 390
389 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse. 391 // Returns an HttpResponse which cannot be parsed as a webapk::WebApkResponse.
390 std::unique_ptr<net::test_server::HttpResponse> 392 std::unique_ptr<net::test_server::HttpResponse>
391 BuildUnparsableWebApkResponse() { 393 BuildUnparsableWebApkResponse() {
392 std::unique_ptr<net::test_server::BasicHttpResponse> response( 394 std::unique_ptr<net::test_server::BasicHttpResponse> response(
393 new net::test_server::BasicHttpResponse()); 395 new net::test_server::BasicHttpResponse());
394 response->set_code(net::HTTP_OK); 396 response->set_code(net::HTTP_OK);
395 response->set_content("😀"); 397 response->set_content("😀");
396 return std::move(response); 398 return std::move(response);
397 } 399 }
398 400
399 } // anonymous namespace 401 } // anonymous namespace
400 402
401 // Test that an HTTP response which cannot be parsed as a webapk::WebApkResponse 403 // Test that an HTTP response which cannot be parsed as a webapk::WebApkResponse
402 // is handled properly. 404 // is handled properly.
403 TEST_F(WebApkInstallerTest, UnparsableCreateWebApkResponse) { 405 TEST_F(WebApkInstallerTest, UnparsableCreateWebApkResponse) {
404 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse)); 406 SetWebApkResponseBuilder(base::Bind(&BuildUnparsableWebApkResponse));
405 407
406 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 408 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
407 runner->RunInstallWebApk(); 409 runner->RunInstallWebApk();
408 EXPECT_FALSE(runner->success()); 410 EXPECT_EQ(WebApkInstallResult::FAILURE, runner->result());
409 } 411 }
410 412
411 // Test update succeeding. 413 // Test update succeeding.
412 TEST_F(WebApkInstallerTest, UpdateSuccess) { 414 TEST_F(WebApkInstallerTest, UpdateSuccess) {
413 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 415 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
414 runner->RunUpdateWebApk(); 416 runner->RunUpdateWebApk();
415 EXPECT_TRUE(runner->success()); 417 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result());
416 } 418 }
417 419
418 // Test that an update suceeds if the WebAPK server returns a HTTP response with 420 // Test that an update suceeds if the WebAPK server returns a HTTP response with
419 // an empty download URL. The WebAPK server sends an empty download URL when: 421 // an empty download URL. The WebAPK server sends an empty download URL when:
420 // - The server is unable to update the WebAPK in the way that the client 422 // - The server is unable to update the WebAPK in the way that the client
421 // requested. 423 // requested.
422 // AND 424 // AND
423 // - The most up to date version of the WebAPK on the server is identical to the 425 // - The most up to date version of the WebAPK on the server is identical to the
424 // one installed on the client. 426 // one installed on the client.
425 TEST_F(WebApkInstallerTest, UpdateSuccessWithEmptyDownloadUrlInResponse) { 427 TEST_F(WebApkInstallerTest, UpdateSuccessWithEmptyDownloadUrlInResponse) {
426 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, GURL())); 428 SetWebApkResponseBuilder(base::Bind(&BuildValidWebApkResponse, GURL()));
427 429
428 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 430 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
429 runner->RunUpdateWebApk(); 431 runner->RunUpdateWebApk();
430 EXPECT_TRUE(runner->success()); 432 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result());
431 } 433 }
432 434
433 // Test installation succeeds using Google Play. 435 // Test installation succeeds using Google Play.
434 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) { 436 TEST_F(WebApkInstallerTest, InstallFromGooglePlaySuccess) {
435 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner(); 437 std::unique_ptr<WebApkInstallerRunner> runner = CreateWebApkInstallerRunner();
436 runner->SetCanUseGooglePlayInstallService(true); 438 runner->SetCanUseGooglePlayInstallService(true);
437 runner->RunInstallWebApk(); 439 runner->RunInstallWebApk();
438 EXPECT_TRUE(runner->success()); 440 EXPECT_EQ(WebApkInstallResult::SUCCESS, runner->result());
439 } 441 }
440 442
441 // When there is no Web Manifest available for a site, an empty 443 // When there is no Web Manifest available for a site, an empty
442 // |best_primary_icon_url| is used to build a WebApk update request. Tests the 444 // |best_primary_icon_url| is used to build a WebApk update request. Tests the
443 // request can be built properly. 445 // request can be built properly.
444 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) { 446 TEST_F(WebApkInstallerTest, BuildWebApkProtoWhenManifestIsObsolete) {
445 GURL icon_url_1 = test_server()->GetURL("/icon1.png"); 447 GURL icon_url_1 = test_server()->GetURL("/icon1.png");
446 GURL icon_url_2 = test_server()->GetURL("/icon2.png"); 448 GURL icon_url_2 = test_server()->GetURL("/icon2.png");
447 std::string icon_murmur2_hash_1 = "1"; 449 std::string icon_murmur2_hash_1 = "1";
448 std::string icon_murmur2_hash_2 = "2"; 450 std::string icon_murmur2_hash_2 = "2";
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 icons[i] = manifest.icons(i); 504 icons[i] = manifest.icons(i);
503 505
504 EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src()); 506 EXPECT_EQ(best_primary_icon_url.spec(), icons[0].src());
505 EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash()); 507 EXPECT_EQ(best_primary_icon_murmur2_hash, icons[0].hash());
506 EXPECT_TRUE(icons[0].has_image_data()); 508 EXPECT_TRUE(icons[0].has_image_data());
507 509
508 EXPECT_EQ(icon_url_1.spec(), icons[1].src()); 510 EXPECT_EQ(icon_url_1.spec(), icons[1].src());
509 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash()); 511 EXPECT_EQ(icon_murmur2_hash_1, icons[1].hash());
510 EXPECT_FALSE(icons[1].has_image_data()); 512 EXPECT_FALSE(icons[1].has_image_data());
511 } 513 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698