| 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() { return stub_->web_contents(); } |
| 98 |
| 94 bool is_loading() { return stub_->is_loading(); } | 99 bool is_loading() { return stub_->is_loading(); } |
| 95 | 100 |
| 96 protected: | 101 protected: |
| 97 void ResetState() override; | 102 void ResetState() override; |
| 98 | 103 |
| 99 private: | 104 private: |
| 100 background_loader::BackgroundLoaderContentsStub* stub_; | 105 background_loader::BackgroundLoaderContentsStub* stub_; |
| 101 }; | 106 }; |
| 102 | 107 |
| 103 TestBackgroundLoaderOffliner::TestBackgroundLoaderOffliner( | 108 TestBackgroundLoaderOffliner::TestBackgroundLoaderOffliner( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 130 Profile* profile() { return &profile_; } | 135 Profile* profile() { return &profile_; } |
| 131 bool completion_callback_called() { return completion_callback_called_; } | 136 bool completion_callback_called() { return completion_callback_called_; } |
| 132 Offliner::RequestStatus request_status() { return request_status_; } | 137 Offliner::RequestStatus request_status() { return request_status_; } |
| 133 bool SaveInProgress() const { return model_->mock_saving(); } | 138 bool SaveInProgress() const { return model_->mock_saving(); } |
| 134 MockOfflinePageModel* model() const { return model_; } | 139 MockOfflinePageModel* model() const { return model_; } |
| 135 const base::HistogramTester& histograms() const { return histogram_tester_; } | 140 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 136 | 141 |
| 137 void CompleteLoading() { | 142 void CompleteLoading() { |
| 138 // For some reason, setting loading to True will call DidStopLoading | 143 // For some reason, setting loading to True will call DidStopLoading |
| 139 // on the observers. | 144 // on the observers. |
| 140 offliner()->web_contents()->TestSetIsLoading(true); | 145 offliner()->web_contents_tester()->TestSetIsLoading(true); |
| 141 } | 146 } |
| 142 | 147 |
| 143 private: | 148 private: |
| 144 void OnCompletion(const SavePageRequest& request, | 149 void OnCompletion(const SavePageRequest& request, |
| 145 Offliner::RequestStatus status); | 150 Offliner::RequestStatus status); |
| 146 content::TestBrowserThreadBundle thread_bundle_; | 151 content::TestBrowserThreadBundle thread_bundle_; |
| 147 TestingProfile profile_; | 152 TestingProfile profile_; |
| 148 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; | 153 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
| 149 MockOfflinePageModel* model_; | 154 MockOfflinePageModel* model_; |
| 150 bool completion_callback_called_; | 155 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(); | 338 base::Time creation_time = base::Time::Now(); |
| 334 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 339 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 335 kUserRequested); | 340 kUserRequested); |
| 336 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 341 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 337 offliner()->WebContentsDestroyed(); | 342 offliner()->WebContentsDestroyed(); |
| 338 | 343 |
| 339 EXPECT_TRUE(completion_callback_called()); | 344 EXPECT_TRUE(completion_callback_called()); |
| 340 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); | 345 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED, request_status()); |
| 341 } | 346 } |
| 342 | 347 |
| 348 TEST_F(BackgroundLoaderOfflinerTest, FailsOnErrorPage) { |
| 349 base::Time creation_time = base::Time::Now(); |
| 350 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 351 kUserRequested); |
| 352 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 353 |
| 354 // Create handle with net error code. |
| 355 // Called after calling LoadAndSave so we have web_contents to work with. |
| 356 std::unique_ptr<content::NavigationHandle> handle( |
| 357 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 358 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 359 net::Error::ERR_NAME_NOT_RESOLVED)); |
| 360 // Call DidFinishNavigation with handle that contains error. |
| 361 offliner()->DidFinishNavigation(handle.get()); |
| 362 // NavigationHandle is always destroyed after finishing navigation. |
| 363 handle.reset(); |
| 364 offliner()->DidStopLoading(); |
| 365 PumpLoop(); |
| 366 |
| 367 EXPECT_TRUE(completion_callback_called()); |
| 368 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); |
| 369 } |
| 370 |
| 343 } // namespace offline_pages | 371 } // namespace offline_pages |
| OLD | NEW |