| 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" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 BackgroundLoaderOfflinerTest(); | 125 BackgroundLoaderOfflinerTest(); |
| 126 ~BackgroundLoaderOfflinerTest() override; | 126 ~BackgroundLoaderOfflinerTest() override; |
| 127 | 127 |
| 128 void SetUp() override; | 128 void SetUp() override; |
| 129 | 129 |
| 130 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); } | 130 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); } |
| 131 Offliner::CompletionCallback const callback() { | 131 Offliner::CompletionCallback const callback() { |
| 132 return base::Bind(&BackgroundLoaderOfflinerTest::OnCompletion, | 132 return base::Bind(&BackgroundLoaderOfflinerTest::OnCompletion, |
| 133 base::Unretained(this)); | 133 base::Unretained(this)); |
| 134 } | 134 } |
| 135 Offliner::CancelCallback const cancel_callback() { |
| 136 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, |
| 137 base::Unretained(this)); |
| 138 } |
| 135 Profile* profile() { return &profile_; } | 139 Profile* profile() { return &profile_; } |
| 136 bool completion_callback_called() { return completion_callback_called_; } | 140 bool completion_callback_called() { return completion_callback_called_; } |
| 137 Offliner::RequestStatus request_status() { return request_status_; } | 141 Offliner::RequestStatus request_status() { return request_status_; } |
| 142 bool cancel_callback_called() { return cancel_callback_called_; } |
| 138 bool SaveInProgress() const { return model_->mock_saving(); } | 143 bool SaveInProgress() const { return model_->mock_saving(); } |
| 139 MockOfflinePageModel* model() const { return model_; } | 144 MockOfflinePageModel* model() const { return model_; } |
| 140 const base::HistogramTester& histograms() const { return histogram_tester_; } | 145 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 141 | 146 |
| 142 void CompleteLoading() { | 147 void CompleteLoading() { |
| 143 // For some reason, setting loading to True will call DidStopLoading | 148 // For some reason, setting loading to True will call DidStopLoading |
| 144 // on the observers. | 149 // on the observers. |
| 145 offliner()->web_contents_tester()->TestSetIsLoading(true); | 150 offliner()->web_contents_tester()->TestSetIsLoading(true); |
| 146 } | 151 } |
| 147 | 152 |
| 148 private: | 153 private: |
| 149 void OnCompletion(const SavePageRequest& request, | 154 void OnCompletion(const SavePageRequest& request, |
| 150 Offliner::RequestStatus status); | 155 Offliner::RequestStatus status); |
| 156 void OnCancel(int64_t offline_id); |
| 151 content::TestBrowserThreadBundle thread_bundle_; | 157 content::TestBrowserThreadBundle thread_bundle_; |
| 152 TestingProfile profile_; | 158 TestingProfile profile_; |
| 153 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; | 159 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
| 154 MockOfflinePageModel* model_; | 160 MockOfflinePageModel* model_; |
| 155 bool completion_callback_called_; | 161 bool completion_callback_called_; |
| 162 bool cancel_callback_called_; |
| 156 Offliner::RequestStatus request_status_; | 163 Offliner::RequestStatus request_status_; |
| 157 base::HistogramTester histogram_tester_; | 164 base::HistogramTester histogram_tester_; |
| 158 | 165 |
| 159 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); | 166 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); |
| 160 }; | 167 }; |
| 161 | 168 |
| 162 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() | 169 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() |
| 163 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 170 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 164 completion_callback_called_(false), | 171 completion_callback_called_(false), |
| 172 cancel_callback_called_(false), |
| 165 request_status_(Offliner::RequestStatus::UNKNOWN) {} | 173 request_status_(Offliner::RequestStatus::UNKNOWN) {} |
| 166 | 174 |
| 167 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} | 175 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} |
| 168 | 176 |
| 169 void BackgroundLoaderOfflinerTest::SetUp() { | 177 void BackgroundLoaderOfflinerTest::SetUp() { |
| 170 model_ = new MockOfflinePageModel(); | 178 model_ = new MockOfflinePageModel(); |
| 171 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); | 179 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); |
| 172 } | 180 } |
| 173 | 181 |
| 174 void BackgroundLoaderOfflinerTest::OnCompletion( | 182 void BackgroundLoaderOfflinerTest::OnCompletion( |
| 175 const SavePageRequest& request, | 183 const SavePageRequest& request, |
| 176 Offliner::RequestStatus status) { | 184 Offliner::RequestStatus status) { |
| 177 DCHECK(!completion_callback_called_); // Expect 1 callback per request. | 185 DCHECK(!completion_callback_called_); // Expect 1 callback per request. |
| 178 completion_callback_called_ = true; | 186 completion_callback_called_ = true; |
| 179 request_status_ = status; | 187 request_status_ = status; |
| 180 } | 188 } |
| 181 | 189 |
| 190 void BackgroundLoaderOfflinerTest::OnCancel(int64_t offline_id) { |
| 191 DCHECK(!cancel_callback_called_); |
| 192 cancel_callback_called_ = true; |
| 193 } |
| 194 |
| 182 TEST_F(BackgroundLoaderOfflinerTest, | 195 TEST_F(BackgroundLoaderOfflinerTest, |
| 183 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { | 196 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { |
| 184 base::Time creation_time = base::Time::Now(); | 197 base::Time creation_time = base::Time::Now(); |
| 185 ClientId custom_tabs_client_id("custom_tabs", "88"); | 198 ClientId custom_tabs_client_id("custom_tabs", "88"); |
| 186 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, | 199 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, |
| 187 creation_time, kUserRequested); | 200 creation_time, kUserRequested); |
| 188 | 201 |
| 189 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); | 202 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 190 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); | 203 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); |
| 191 histograms().ExpectBucketCount( | 204 histograms().ExpectBucketCount( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 EXPECT_FALSE(completion_callback_called()); | 254 EXPECT_FALSE(completion_callback_called()); |
| 242 EXPECT_TRUE(SaveInProgress()); | 255 EXPECT_TRUE(SaveInProgress()); |
| 243 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); | 256 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); |
| 244 } | 257 } |
| 245 | 258 |
| 246 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { | 259 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { |
| 247 base::Time creation_time = base::Time::Now(); | 260 base::Time creation_time = base::Time::Now(); |
| 248 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 261 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 249 kUserRequested); | 262 kUserRequested); |
| 250 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 263 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 251 offliner()->Cancel(); | 264 offliner()->Cancel(cancel_callback()); |
| 265 PumpLoop(); |
| 266 EXPECT_TRUE(cancel_callback_called()); |
| 252 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 267 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 253 } | 268 } |
| 254 | 269 |
| 255 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { | 270 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { |
| 256 base::Time creation_time = base::Time::Now(); | 271 base::Time creation_time = base::Time::Now(); |
| 257 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 272 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 258 kUserRequested); | 273 kUserRequested); |
| 259 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 274 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 260 CompleteLoading(); | 275 CompleteLoading(); |
| 261 PumpLoop(); | 276 PumpLoop(); |
| 262 offliner()->Cancel(); | 277 offliner()->Cancel(cancel_callback()); |
| 278 PumpLoop(); |
| 263 | 279 |
| 264 // Subsequent save callback cause no crash. | 280 // Subsequent save callback cause no crash. |
| 265 model()->CompleteSavingAsArchiveCreationFailed(); | 281 model()->CompleteSavingAsArchiveCreationFailed(); |
| 266 PumpLoop(); | 282 PumpLoop(); |
| 283 EXPECT_TRUE(cancel_callback_called()); |
| 267 EXPECT_FALSE(completion_callback_called()); | 284 EXPECT_FALSE(completion_callback_called()); |
| 268 EXPECT_FALSE(SaveInProgress()); | 285 EXPECT_FALSE(SaveInProgress()); |
| 269 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 286 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 270 } | 287 } |
| 271 | 288 |
| 272 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { | 289 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { |
| 273 base::Time creation_time = base::Time::Now(); | 290 base::Time creation_time = base::Time::Now(); |
| 274 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 291 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 275 kUserRequested); | 292 kUserRequested); |
| 276 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); | 293 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 // NavigationHandle is always destroyed after finishing navigation. | 379 // NavigationHandle is always destroyed after finishing navigation. |
| 363 handle.reset(); | 380 handle.reset(); |
| 364 offliner()->DidStopLoading(); | 381 offliner()->DidStopLoading(); |
| 365 PumpLoop(); | 382 PumpLoop(); |
| 366 | 383 |
| 367 EXPECT_TRUE(completion_callback_called()); | 384 EXPECT_TRUE(completion_callback_called()); |
| 368 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); | 385 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_RETRY, request_status()); |
| 369 } | 386 } |
| 370 | 387 |
| 371 } // namespace offline_pages | 388 } // namespace offline_pages |
| OLD | NEW |