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/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
11 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 12 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
12 #include "chrome/browser/net/prediction_options.h" | 13 #include "chrome/browser/net/prediction_options.h" |
13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
15 #include "components/content_settings/core/common/pref_names.h" | 16 #include "components/content_settings/core/common/pref_names.h" |
16 #include "components/offline_pages/content/background_loader/background_loader_c
ontents_stub.h" | 17 #include "components/offline_pages/content/background_loader/background_loader_c
ontents_stub.h" |
17 #include "components/offline_pages/core/background/offliner.h" | 18 #include "components/offline_pages/core/background/offliner.h" |
18 #include "components/offline_pages/core/background/save_page_request.h" | 19 #include "components/offline_pages/core/background/save_page_request.h" |
19 #include "components/offline_pages/core/stub_offline_page_model.h" | 20 #include "components/offline_pages/core/stub_offline_page_model.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 67 |
67 bool mock_saving() const { return mock_saving_; } | 68 bool mock_saving() const { return mock_saving_; } |
68 | 69 |
69 private: | 70 private: |
70 bool mock_saving_; | 71 bool mock_saving_; |
71 SavePageCallback save_page_callback_; | 72 SavePageCallback save_page_callback_; |
72 | 73 |
73 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); | 74 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); |
74 }; | 75 }; |
75 | 76 |
76 void PumpLoop() { | |
77 base::RunLoop().RunUntilIdle(); | |
78 } | |
79 } // namespace | 77 } // namespace |
80 | 78 |
81 // A BackgroundLoader that we can run tests on. | 79 // A BackgroundLoader that we can run tests on. |
82 // Overrides the ResetState so we don't actually try to create any web contents. | 80 // Overrides the ResetState so we don't actually try to create any web contents. |
83 // This is a temporary solution to test core BackgroundLoaderOffliner | 81 // This is a temporary solution to test core BackgroundLoaderOffliner |
84 // functionality until we straighten out assumptions made by RequestCoordinator | 82 // functionality until we straighten out assumptions made by RequestCoordinator |
85 // so that the ResetState method is no longer needed. | 83 // so that the ResetState method is no longer needed. |
86 class TestBackgroundLoaderOffliner : public BackgroundLoaderOffliner { | 84 class TestBackgroundLoaderOffliner : public BackgroundLoaderOffliner { |
87 public: | 85 public: |
88 explicit TestBackgroundLoaderOffliner( | 86 explicit TestBackgroundLoaderOffliner( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 bool SaveInProgress() const { return model_->mock_saving(); } | 136 bool SaveInProgress() const { return model_->mock_saving(); } |
139 MockOfflinePageModel* model() const { return model_; } | 137 MockOfflinePageModel* model() const { return model_; } |
140 const base::HistogramTester& histograms() const { return histogram_tester_; } | 138 const base::HistogramTester& histograms() const { return histogram_tester_; } |
141 | 139 |
142 void CompleteLoading() { | 140 void CompleteLoading() { |
143 // For some reason, setting loading to True will call DidStopLoading | 141 // For some reason, setting loading to True will call DidStopLoading |
144 // on the observers. | 142 // on the observers. |
145 offliner()->web_contents_tester()->TestSetIsLoading(true); | 143 offliner()->web_contents_tester()->TestSetIsLoading(true); |
146 } | 144 } |
147 | 145 |
| 146 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
| 147 |
148 private: | 148 private: |
149 void OnCompletion(const SavePageRequest& request, | 149 void OnCompletion(const SavePageRequest& request, |
150 Offliner::RequestStatus status); | 150 Offliner::RequestStatus status); |
151 content::TestBrowserThreadBundle thread_bundle_; | 151 content::TestBrowserThreadBundle thread_bundle_; |
152 TestingProfile profile_; | 152 TestingProfile profile_; |
153 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; | 153 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
154 MockOfflinePageModel* model_; | 154 MockOfflinePageModel* model_; |
155 bool completion_callback_called_; | 155 bool completion_callback_called_; |
156 Offliner::RequestStatus request_status_; | 156 Offliner::RequestStatus request_status_; |
157 base::HistogramTester histogram_tester_; | 157 base::HistogramTester histogram_tester_; |
158 | 158 |
159 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); | 159 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); |
160 }; | 160 }; |
161 | 161 |
162 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() | 162 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() |
163 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 163 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
164 completion_callback_called_(false), | 164 completion_callback_called_(false), |
165 request_status_(Offliner::RequestStatus::UNKNOWN) {} | 165 request_status_(Offliner::RequestStatus::UNKNOWN) {} |
166 | 166 |
167 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} | 167 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} |
168 | 168 |
169 void BackgroundLoaderOfflinerTest::SetUp() { | 169 void BackgroundLoaderOfflinerTest::SetUp() { |
170 model_ = new MockOfflinePageModel(); | 170 model_ = new MockOfflinePageModel(); |
171 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); | 171 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); |
| 172 offliner_->SetPageDelayForTest(0L); |
172 } | 173 } |
173 | 174 |
174 void BackgroundLoaderOfflinerTest::OnCompletion( | 175 void BackgroundLoaderOfflinerTest::OnCompletion( |
175 const SavePageRequest& request, | 176 const SavePageRequest& request, |
176 Offliner::RequestStatus status) { | 177 Offliner::RequestStatus status) { |
177 DCHECK(!completion_callback_called_); // Expect 1 callback per request. | 178 DCHECK(!completion_callback_called_); // Expect 1 callback per request. |
178 completion_callback_called_ = true; | 179 completion_callback_called_ = true; |
179 request_status_ = status; | 180 request_status_ = status; |
180 } | 181 } |
181 | 182 |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 offliner()->DidFinishNavigation(handle.get()); | 362 offliner()->DidFinishNavigation(handle.get()); |
362 // NavigationHandle is always destroyed after finishing navigation. | 363 // NavigationHandle is always destroyed after finishing navigation. |
363 handle.reset(); | 364 handle.reset(); |
364 offliner()->DidStopLoading(); | 365 offliner()->DidStopLoading(); |
365 PumpLoop(); | 366 PumpLoop(); |
366 | 367 |
367 EXPECT_TRUE(completion_callback_called()); | 368 EXPECT_TRUE(completion_callback_called()); |
368 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); | 369 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); |
369 } | 370 } |
370 | 371 |
| 372 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { |
| 373 base::Time creation_time = base::Time::Now(); |
| 374 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 375 kUserRequested); |
| 376 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 377 // First load |
| 378 CompleteLoading(); |
| 379 // Second load |
| 380 offliner()->DidStopLoading(); |
| 381 PumpLoop(); |
| 382 model()->CompleteSavingAsSuccess(); |
| 383 PumpLoop(); |
| 384 |
| 385 EXPECT_TRUE(completion_callback_called()); |
| 386 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 387 EXPECT_FALSE(offliner()->is_loading()); |
| 388 EXPECT_FALSE(SaveInProgress()); |
| 389 } |
| 390 |
371 } // namespace offline_pages | 391 } // namespace offline_pages |
OLD | NEW |