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

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

Powered by Google App Engine
This is Rietveld 408576698