| 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/offline_pages/background_loader_offliner.h" | 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
| 10 #include "base/test/scoped_mock_time_message_loop_task_runner.h" | 10 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 namespace offline_pages { | 29 namespace offline_pages { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const int64_t kRequestId = 7; | 33 const int64_t kRequestId = 7; |
| 34 const GURL kHttpUrl("http://www.tunafish.com"); | 34 const GURL kHttpUrl("http://www.tunafish.com"); |
| 35 const GURL kFileUrl("file://salmon.png"); | 35 const GURL kFileUrl("file://salmon.png"); |
| 36 const ClientId kClientId("AsyncLoading", "88"); | 36 const ClientId kClientId("async_loading", "88"); |
| 37 const bool kUserRequested = true; | 37 const bool kUserRequested = true; |
| 38 | 38 |
| 39 // Mock OfflinePageModel for testing the SavePage calls | 39 // Mock OfflinePageModel for testing the SavePage calls |
| 40 class MockOfflinePageModel : public StubOfflinePageModel { | 40 class MockOfflinePageModel : public StubOfflinePageModel { |
| 41 public: | 41 public: |
| 42 MockOfflinePageModel() : mock_saving_(false) {} | 42 MockOfflinePageModel() : mock_saving_(false) {} |
| 43 ~MockOfflinePageModel() override {} | 43 ~MockOfflinePageModel() override {} |
| 44 | 44 |
| 45 void SavePage(const SavePageParams& save_page_params, | 45 void SavePage(const SavePageParams& save_page_params, |
| 46 std::unique_ptr<OfflinePageArchiver> archiver, | 46 std::unique_ptr<OfflinePageArchiver> archiver, |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 EXPECT_TRUE(completion_callback_called()); | 414 EXPECT_TRUE(completion_callback_called()); |
| 415 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); | 415 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); |
| 416 } | 416 } |
| 417 | 417 |
| 418 TEST_F(BackgroundLoaderOfflinerTest, FailsOnErrorPage) { | 418 TEST_F(BackgroundLoaderOfflinerTest, FailsOnErrorPage) { |
| 419 base::Time creation_time = base::Time::Now(); | 419 base::Time creation_time = base::Time::Now(); |
| 420 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 420 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 421 kUserRequested); | 421 kUserRequested); |
| 422 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 422 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 423 progress_callback())); | 423 progress_callback())); |
| 424 | |
| 425 // Create handle with net error code. | 424 // Create handle with net error code. |
| 426 // Called after calling LoadAndSave so we have web_contents to work with. | 425 // Called after calling LoadAndSave so we have web_contents to work with. |
| 427 std::unique_ptr<content::NavigationHandle> handle( | 426 std::unique_ptr<content::NavigationHandle> handle( |
| 428 content::NavigationHandle::CreateNavigationHandleForTesting( | 427 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 429 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, | 428 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 430 net::Error::ERR_NAME_NOT_RESOLVED)); | 429 net::Error::ERR_NAME_NOT_RESOLVED)); |
| 431 // Call DidFinishNavigation with handle that contains error. | 430 // NavigationHandle destruction will trigger DidFinishNavigation code. |
| 432 offliner()->DidFinishNavigation(handle.get()); | |
| 433 // NavigationHandle is always destroyed after finishing navigation. | |
| 434 handle.reset(); | 431 handle.reset(); |
| 432 histograms().ExpectBucketCount( |
| 433 "OfflinePages.Background.BackgroundLoadingFailedCode.async_loading", |
| 434 105, // ERR_NAME_NOT_RESOLVED |
| 435 1); |
| 435 offliner()->DidStopLoading(); | 436 offliner()->DidStopLoading(); |
| 436 PumpLoop(); | 437 PumpLoop(); |
| 437 | 438 |
| 438 EXPECT_TRUE(completion_callback_called()); | 439 EXPECT_TRUE(completion_callback_called()); |
| 439 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); | 440 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); |
| 440 } | 441 } |
| 441 | 442 |
| 442 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { | 443 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { |
| 443 base::Time creation_time = base::Time::Now(); | 444 base::Time creation_time = base::Time::Now(); |
| 444 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 445 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 445 kUserRequested); | 446 kUserRequested); |
| 446 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 447 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 447 progress_callback())); | 448 progress_callback())); |
| 448 // First load | 449 // First load |
| 449 CompleteLoading(); | 450 CompleteLoading(); |
| 450 // Second load | 451 // Second load |
| 451 offliner()->DidStopLoading(); | 452 offliner()->DidStopLoading(); |
| 452 PumpLoop(); | 453 PumpLoop(); |
| 453 model()->CompleteSavingAsSuccess(); | 454 model()->CompleteSavingAsSuccess(); |
| 454 PumpLoop(); | 455 PumpLoop(); |
| 455 | 456 |
| 456 EXPECT_TRUE(completion_callback_called()); | 457 EXPECT_TRUE(completion_callback_called()); |
| 457 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); | 458 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 458 EXPECT_FALSE(offliner()->is_loading()); | 459 EXPECT_FALSE(offliner()->is_loading()); |
| 459 EXPECT_FALSE(SaveInProgress()); | 460 EXPECT_FALSE(SaveInProgress()); |
| 460 } | 461 } |
| 461 | 462 |
| 462 } // namespace offline_pages | 463 } // namespace offline_pages |
| OLD | NEW |