| 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/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 11 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
| 12 #include "chrome/browser/net/prediction_options.h" | 12 #include "chrome/browser/net/prediction_options.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "components/content_settings/core/common/pref_names.h" | 15 #include "components/content_settings/core/common/pref_names.h" |
| 16 #include "components/offline_pages/content/background_loader/background_loader_c
ontents_stub.h" | 16 #include "components/offline_pages/content/background_loader/background_loader_c
ontents_stub.h" |
| 17 #include "components/offline_pages/core/background/offliner.h" | 17 #include "components/offline_pages/core/background/offliner.h" |
| 18 #include "components/offline_pages/core/background/save_page_request.h" | 18 #include "components/offline_pages/core/background/save_page_request.h" |
| 19 #include "components/offline_pages/core/stub_offline_page_model.h" | 19 #include "components/offline_pages/core/stub_offline_page_model.h" |
| 20 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
| 21 #include "content/public/browser/navigation_handle.h" |
| 22 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "content/public/test/web_contents_tester.h" | 24 #include "content/public/test/web_contents_tester.h" |
| 25 #include "net/base/net_errors.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 27 |
| 25 namespace offline_pages { | 28 namespace offline_pages { |
| 26 | 29 |
| 27 namespace { | 30 namespace { |
| 28 | 31 |
| 29 const int64_t kRequestId = 7; | 32 const int64_t kRequestId = 7; |
| 30 const GURL kHttpUrl("http://www.tunafish.com"); | 33 const GURL kHttpUrl("http://www.tunafish.com"); |
| 31 const GURL kFileUrl("file://salmon.png"); | 34 const GURL kFileUrl("file://salmon.png"); |
| 32 const ClientId kClientId("AsyncLoading", "88"); | 35 const ClientId kClientId("AsyncLoading", "88"); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 // This is a temporary solution to test core BackgroundLoaderOffliner | 83 // This is a temporary solution to test core BackgroundLoaderOffliner |
| 81 // functionality until we straighten out assumptions made by RequestCoordinator | 84 // functionality until we straighten out assumptions made by RequestCoordinator |
| 82 // so that the ResetState method is no longer needed. | 85 // so that the ResetState method is no longer needed. |
| 83 class TestBackgroundLoaderOffliner : public BackgroundLoaderOffliner { | 86 class TestBackgroundLoaderOffliner : public BackgroundLoaderOffliner { |
| 84 public: | 87 public: |
| 85 explicit TestBackgroundLoaderOffliner( | 88 explicit TestBackgroundLoaderOffliner( |
| 86 content::BrowserContext* browser_context, | 89 content::BrowserContext* browser_context, |
| 87 const OfflinerPolicy* policy, | 90 const OfflinerPolicy* policy, |
| 88 OfflinePageModel* offline_page_model); | 91 OfflinePageModel* offline_page_model); |
| 89 ~TestBackgroundLoaderOffliner() override; | 92 ~TestBackgroundLoaderOffliner() override; |
| 90 content::WebContentsTester* web_contents() { | 93 content::WebContentsTester* web_contents_tester() { |
| 91 return content::WebContentsTester::For(stub_->web_contents()); | 94 return content::WebContentsTester::For(stub_->web_contents()); |
| 92 } | 95 } |
| 93 | 96 |
| 97 content::WebContents* web_contents() { |
| 98 return stub_->web_contents(); |
| 99 } |
| 100 |
| 94 bool is_loading() { return stub_->is_loading(); } | 101 bool is_loading() { return stub_->is_loading(); } |
| 95 | 102 |
| 96 protected: | 103 protected: |
| 97 void ResetState() override; | 104 void ResetState() override; |
| 98 | 105 |
| 99 private: | 106 private: |
| 100 background_loader::BackgroundLoaderContentsStub* stub_; | 107 background_loader::BackgroundLoaderContentsStub* stub_; |
| 101 }; | 108 }; |
| 102 | 109 |
| 103 TestBackgroundLoaderOffliner::TestBackgroundLoaderOffliner( | 110 TestBackgroundLoaderOffliner::TestBackgroundLoaderOffliner( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 130 Profile* profile() { return &profile_; } | 137 Profile* profile() { return &profile_; } |
| 131 bool completion_callback_called() { return completion_callback_called_; } | 138 bool completion_callback_called() { return completion_callback_called_; } |
| 132 Offliner::RequestStatus request_status() { return request_status_; } | 139 Offliner::RequestStatus request_status() { return request_status_; } |
| 133 bool SaveInProgress() const { return model_->mock_saving(); } | 140 bool SaveInProgress() const { return model_->mock_saving(); } |
| 134 MockOfflinePageModel* model() const { return model_; } | 141 MockOfflinePageModel* model() const { return model_; } |
| 135 const base::HistogramTester& histograms() const { return histogram_tester_; } | 142 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 136 | 143 |
| 137 void CompleteLoading() { | 144 void CompleteLoading() { |
| 138 // For some reason, setting loading to True will call DidStopLoading | 145 // For some reason, setting loading to True will call DidStopLoading |
| 139 // on the observers. | 146 // on the observers. |
| 140 offliner()->web_contents()->TestSetIsLoading(true); | 147 offliner()->web_contents_tester()->TestSetIsLoading(true); |
| 141 } | 148 } |
| 142 | 149 |
| 143 private: | 150 private: |
| 144 void OnCompletion(const SavePageRequest& request, | 151 void OnCompletion(const SavePageRequest& request, |
| 145 Offliner::RequestStatus status); | 152 Offliner::RequestStatus status); |
| 146 content::TestBrowserThreadBundle thread_bundle_; | 153 content::TestBrowserThreadBundle thread_bundle_; |
| 147 TestingProfile profile_; | 154 TestingProfile profile_; |
| 148 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; | 155 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
| 149 MockOfflinePageModel* model_; | 156 MockOfflinePageModel* model_; |
| 150 bool completion_callback_called_; | 157 bool completion_callback_called_; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 base::Time creation_time = base::Time::Now(); | 340 base::Time creation_time = base::Time::Now(); |
| 334 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 341 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 335 kUserRequested); | 342 kUserRequested); |
| 336 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 343 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 337 offliner()->WebContentsDestroyed(); | 344 offliner()->WebContentsDestroyed(); |
| 338 | 345 |
| 339 EXPECT_TRUE(completion_callback_called()); | 346 EXPECT_TRUE(completion_callback_called()); |
| 340 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); | 347 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); |
| 341 } | 348 } |
| 342 | 349 |
| 350 TEST_F(BackgroundLoaderOfflinerTest, FailsOnErrorPage) { |
| 351 base::Time creation_time = base::Time::Now(); |
| 352 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 353 kUserRequested); |
| 354 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 355 |
| 356 // Create handle with net error code. |
| 357 // Called after calling LoadAndSave so we have web_contents to work with. |
| 358 std::unique_ptr<content::NavigationHandle> handle( |
| 359 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 360 kHttpUrl, |
| 361 offliner()->web_contents()->GetMainFrame(), |
| 362 true, |
| 363 net::Error::ERR_NAME_NOT_RESOLVED)); |
| 364 // Call DidFinishNavigation with handle that contains error. |
| 365 offliner()->DidFinishNavigation(handle.get()); |
| 366 // NavigationHandle is always destroyed after finishing navigation. |
| 367 handle.reset(); |
| 368 offliner()->DidStopLoading(); |
| 369 PumpLoop(); |
| 370 |
| 371 EXPECT_TRUE(completion_callback_called()); |
| 372 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); |
| 373 } |
| 374 |
| 343 } // namespace offline_pages | 375 } // namespace offline_pages |
| OLD | NEW |