| 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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 140 } |
| 141 Profile* profile() { return &profile_; } | 141 Profile* profile() { return &profile_; } |
| 142 bool completion_callback_called() { return completion_callback_called_; } | 142 bool completion_callback_called() { return completion_callback_called_; } |
| 143 Offliner::RequestStatus request_status() { return request_status_; } | 143 Offliner::RequestStatus request_status() { return request_status_; } |
| 144 bool cancel_callback_called() { return cancel_callback_called_; } | 144 bool cancel_callback_called() { return cancel_callback_called_; } |
| 145 bool SaveInProgress() const { return model_->mock_saving(); } | 145 bool SaveInProgress() const { return model_->mock_saving(); } |
| 146 MockOfflinePageModel* model() const { return model_; } | 146 MockOfflinePageModel* model() const { return model_; } |
| 147 const base::HistogramTester& histograms() const { return histogram_tester_; } | 147 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 148 int64_t progress() { return progress_; } | 148 int64_t progress() { return progress_; } |
| 149 | 149 |
| 150 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
| 151 |
| 150 void CompleteLoading() { | 152 void CompleteLoading() { |
| 151 // For some reason, setting loading to True will call DidStopLoading | 153 offliner()->DocumentOnLoadCompletedInMainFrame(); |
| 152 // on the observers. | 154 PumpLoop(); |
| 153 offliner()->web_contents_tester()->TestSetIsLoading(true); | |
| 154 } | 155 } |
| 155 | 156 |
| 156 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | |
| 157 | |
| 158 private: | 157 private: |
| 159 void OnCompletion(const SavePageRequest& request, | 158 void OnCompletion(const SavePageRequest& request, |
| 160 Offliner::RequestStatus status); | 159 Offliner::RequestStatus status); |
| 161 void OnProgress(const SavePageRequest& request, int64_t bytes); | 160 void OnProgress(const SavePageRequest& request, int64_t bytes); |
| 162 void OnCancel(int64_t offline_id); | 161 void OnCancel(int64_t offline_id); |
| 163 content::TestBrowserThreadBundle thread_bundle_; | 162 content::TestBrowserThreadBundle thread_bundle_; |
| 164 TestingProfile profile_; | 163 TestingProfile profile_; |
| 165 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; | 164 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
| 166 MockOfflinePageModel* model_; | 165 MockOfflinePageModel* model_; |
| 167 bool completion_callback_called_; | 166 bool completion_callback_called_; |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 std::unique_ptr<content::NavigationHandle> handle( | 425 std::unique_ptr<content::NavigationHandle> handle( |
| 427 content::NavigationHandle::CreateNavigationHandleForTesting( | 426 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 428 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, | 427 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 429 net::Error::ERR_NAME_NOT_RESOLVED)); | 428 net::Error::ERR_NAME_NOT_RESOLVED)); |
| 430 // NavigationHandle destruction will trigger DidFinishNavigation code. | 429 // NavigationHandle destruction will trigger DidFinishNavigation code. |
| 431 handle.reset(); | 430 handle.reset(); |
| 432 histograms().ExpectBucketCount( | 431 histograms().ExpectBucketCount( |
| 433 "OfflinePages.Background.BackgroundLoadingFailedCode.async_loading", | 432 "OfflinePages.Background.BackgroundLoadingFailedCode.async_loading", |
| 434 105, // ERR_NAME_NOT_RESOLVED | 433 105, // ERR_NAME_NOT_RESOLVED |
| 435 1); | 434 1); |
| 436 offliner()->DidStopLoading(); | 435 CompleteLoading(); |
| 437 PumpLoop(); | 436 PumpLoop(); |
| 438 | 437 |
| 439 EXPECT_TRUE(completion_callback_called()); | 438 EXPECT_TRUE(completion_callback_called()); |
| 440 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); | 439 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); |
| 441 } | 440 } |
| 442 | 441 |
| 443 TEST_F(BackgroundLoaderOfflinerTest, NoNextOnInternetDisconnected) { | 442 TEST_F(BackgroundLoaderOfflinerTest, NoNextOnInternetDisconnected) { |
| 444 base::Time creation_time = base::Time::Now(); | 443 base::Time creation_time = base::Time::Now(); |
| 445 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 444 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 446 kUserRequested); | 445 kUserRequested); |
| 447 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 446 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 448 progress_callback())); | 447 progress_callback())); |
| 449 | 448 |
| 450 // Create handle with net error code. | 449 // Create handle with net error code. |
| 451 // Called after calling LoadAndSave so we have web_contents to work with. | 450 // Called after calling LoadAndSave so we have web_contents to work with. |
| 452 std::unique_ptr<content::NavigationHandle> handle( | 451 std::unique_ptr<content::NavigationHandle> handle( |
| 453 content::NavigationHandle::CreateNavigationHandleForTesting( | 452 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 454 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, | 453 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 455 net::Error::ERR_INTERNET_DISCONNECTED)); | 454 net::Error::ERR_INTERNET_DISCONNECTED)); |
| 456 // Call DidFinishNavigation with handle that contains error. | 455 // Call DidFinishNavigation with handle that contains error. |
| 457 offliner()->DidFinishNavigation(handle.get()); | 456 offliner()->DidFinishNavigation(handle.get()); |
| 458 // NavigationHandle is always destroyed after finishing navigation. | 457 // NavigationHandle is always destroyed after finishing navigation. |
| 459 handle.reset(); | 458 handle.reset(); |
| 460 offliner()->DidStopLoading(); | 459 CompleteLoading(); |
| 461 PumpLoop(); | 460 PumpLoop(); |
| 462 | 461 |
| 463 EXPECT_TRUE(completion_callback_called()); | 462 EXPECT_TRUE(completion_callback_called()); |
| 464 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); | 463 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); |
| 465 } | 464 } |
| 466 | 465 |
| 467 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { | 466 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { |
| 468 base::Time creation_time = base::Time::Now(); | 467 base::Time creation_time = base::Time::Now(); |
| 469 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 468 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 470 kUserRequested); | 469 kUserRequested); |
| 471 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 470 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 472 progress_callback())); | 471 progress_callback())); |
| 473 // First load | 472 // First load |
| 474 CompleteLoading(); | 473 CompleteLoading(); |
| 475 // Second load | 474 // Second load |
| 476 offliner()->DidStopLoading(); | 475 CompleteLoading(); |
| 477 PumpLoop(); | 476 PumpLoop(); |
| 478 model()->CompleteSavingAsSuccess(); | 477 model()->CompleteSavingAsSuccess(); |
| 479 PumpLoop(); | 478 PumpLoop(); |
| 480 | 479 |
| 481 EXPECT_TRUE(completion_callback_called()); | 480 EXPECT_TRUE(completion_callback_called()); |
| 482 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); | 481 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 483 EXPECT_FALSE(offliner()->is_loading()); | 482 EXPECT_FALSE(offliner()->is_loading()); |
| 484 EXPECT_FALSE(SaveInProgress()); | 483 EXPECT_FALSE(SaveInProgress()); |
| 485 } | 484 } |
| 486 | 485 |
| 487 } // namespace offline_pages | 486 } // namespace offline_pages |
| OLD | NEW |