| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, | 138 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, |
| 139 base::Unretained(this)); | 139 base::Unretained(this)); |
| 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 | 149 |
| 149 void CompleteLoading() { | 150 void CompleteLoading() { |
| 150 // For some reason, setting loading to True will call DidStopLoading | 151 // For some reason, setting loading to True will call DidStopLoading |
| 151 // on the observers. | 152 // on the observers. |
| 152 offliner()->web_contents_tester()->TestSetIsLoading(true); | 153 offliner()->web_contents_tester()->TestSetIsLoading(true); |
| 153 } | 154 } |
| 154 | 155 |
| 155 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | 156 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
| 156 | 157 |
| 157 private: | 158 private: |
| 158 void OnCompletion(const SavePageRequest& request, | 159 void OnCompletion(const SavePageRequest& request, |
| 159 Offliner::RequestStatus status); | 160 Offliner::RequestStatus status); |
| 160 void OnProgress(const SavePageRequest& request, int64_t bytes); | 161 void OnProgress(const SavePageRequest& request, int64_t bytes); |
| 161 void OnCancel(int64_t offline_id); | 162 void OnCancel(int64_t offline_id); |
| 162 content::TestBrowserThreadBundle thread_bundle_; | 163 content::TestBrowserThreadBundle thread_bundle_; |
| 163 TestingProfile profile_; | 164 TestingProfile profile_; |
| 164 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; | 165 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
| 165 MockOfflinePageModel* model_; | 166 MockOfflinePageModel* model_; |
| 166 bool completion_callback_called_; | 167 bool completion_callback_called_; |
| 167 bool cancel_callback_called_; | 168 bool cancel_callback_called_; |
| 169 int64_t progress_; |
| 168 Offliner::RequestStatus request_status_; | 170 Offliner::RequestStatus request_status_; |
| 169 base::HistogramTester histogram_tester_; | 171 base::HistogramTester histogram_tester_; |
| 170 | 172 |
| 171 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); | 173 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); |
| 172 }; | 174 }; |
| 173 | 175 |
| 174 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() | 176 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() |
| 175 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 177 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 176 completion_callback_called_(false), | 178 completion_callback_called_(false), |
| 177 cancel_callback_called_(false), | 179 cancel_callback_called_(false), |
| 180 progress_(0LL), |
| 178 request_status_(Offliner::RequestStatus::UNKNOWN) {} | 181 request_status_(Offliner::RequestStatus::UNKNOWN) {} |
| 179 | 182 |
| 180 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} | 183 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} |
| 181 | 184 |
| 182 void BackgroundLoaderOfflinerTest::SetUp() { | 185 void BackgroundLoaderOfflinerTest::SetUp() { |
| 183 model_ = new MockOfflinePageModel(); | 186 model_ = new MockOfflinePageModel(); |
| 184 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); | 187 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); |
| 185 offliner_->SetPageDelayForTest(0L); | 188 offliner_->SetPageDelayForTest(0L); |
| 186 } | 189 } |
| 187 | 190 |
| 188 void BackgroundLoaderOfflinerTest::OnCompletion( | 191 void BackgroundLoaderOfflinerTest::OnCompletion( |
| 189 const SavePageRequest& request, | 192 const SavePageRequest& request, |
| 190 Offliner::RequestStatus status) { | 193 Offliner::RequestStatus status) { |
| 191 DCHECK(!completion_callback_called_); // Expect 1 callback per request. | 194 DCHECK(!completion_callback_called_); // Expect 1 callback per request. |
| 192 completion_callback_called_ = true; | 195 completion_callback_called_ = true; |
| 193 request_status_ = status; | 196 request_status_ = status; |
| 194 } | 197 } |
| 195 | 198 |
| 196 void BackgroundLoaderOfflinerTest::OnProgress(const SavePageRequest& request, | 199 void BackgroundLoaderOfflinerTest::OnProgress(const SavePageRequest& request, |
| 197 int64_t bytes) {} | 200 int64_t bytes) { |
| 201 progress_ = bytes; |
| 202 } |
| 203 |
| 198 void BackgroundLoaderOfflinerTest::OnCancel(int64_t offline_id) { | 204 void BackgroundLoaderOfflinerTest::OnCancel(int64_t offline_id) { |
| 199 DCHECK(!cancel_callback_called_); | 205 DCHECK(!cancel_callback_called_); |
| 200 cancel_callback_called_ = true; | 206 cancel_callback_called_ = true; |
| 201 } | 207 } |
| 202 | 208 |
| 203 TEST_F(BackgroundLoaderOfflinerTest, | 209 TEST_F(BackgroundLoaderOfflinerTest, |
| 204 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { | 210 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { |
| 205 base::Time creation_time = base::Time::Now(); | 211 base::Time creation_time = base::Time::Now(); |
| 206 ClientId custom_tabs_client_id("custom_tabs", "88"); | 212 ClientId custom_tabs_client_id("custom_tabs", "88"); |
| 207 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, | 213 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 254 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 249 kUserRequested); | 255 kUserRequested); |
| 250 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 256 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 251 progress_callback())); | 257 progress_callback())); |
| 252 EXPECT_TRUE(offliner()->is_loading()); | 258 EXPECT_TRUE(offliner()->is_loading()); |
| 253 EXPECT_FALSE(SaveInProgress()); | 259 EXPECT_FALSE(SaveInProgress()); |
| 254 EXPECT_FALSE(completion_callback_called()); | 260 EXPECT_FALSE(completion_callback_called()); |
| 255 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); | 261 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); |
| 256 } | 262 } |
| 257 | 263 |
| 264 TEST_F(BackgroundLoaderOfflinerTest, BytesReportedWillUpdateProgress) { |
| 265 base::Time creation_time = base::Time::Now(); |
| 266 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 267 kUserRequested); |
| 268 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 269 progress_callback())); |
| 270 offliner()->OnNetworkBytesChanged(5LL); |
| 271 EXPECT_EQ(progress(), 5LL); |
| 272 offliner()->OnNetworkBytesChanged(10LL); |
| 273 EXPECT_EQ(progress(), 15LL); |
| 274 } |
| 275 |
| 258 TEST_F(BackgroundLoaderOfflinerTest, CompleteLoadingInitiatesSave) { | 276 TEST_F(BackgroundLoaderOfflinerTest, CompleteLoadingInitiatesSave) { |
| 259 base::Time creation_time = base::Time::Now(); | 277 base::Time creation_time = base::Time::Now(); |
| 260 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 278 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 261 kUserRequested); | 279 kUserRequested); |
| 262 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 280 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 263 progress_callback())); | 281 progress_callback())); |
| 264 CompleteLoading(); | 282 CompleteLoading(); |
| 265 PumpLoop(); | 283 PumpLoop(); |
| 266 EXPECT_FALSE(completion_callback_called()); | 284 EXPECT_FALSE(completion_callback_called()); |
| 267 EXPECT_TRUE(SaveInProgress()); | 285 EXPECT_TRUE(SaveInProgress()); |
| 268 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); | 286 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); |
| 269 } | 287 } |
| 270 | 288 |
| 271 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { | 289 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { |
| 272 base::Time creation_time = base::Time::Now(); | 290 base::Time creation_time = base::Time::Now(); |
| 273 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 291 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 274 kUserRequested); | 292 kUserRequested); |
| 275 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 293 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 276 progress_callback())); | 294 progress_callback())); |
| 277 offliner()->Cancel(cancel_callback()); | 295 offliner()->Cancel(cancel_callback()); |
| 278 PumpLoop(); | 296 PumpLoop(); |
| 297 offliner()->OnNetworkBytesChanged(15LL); |
| 279 EXPECT_TRUE(cancel_callback_called()); | 298 EXPECT_TRUE(cancel_callback_called()); |
| 280 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 299 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 300 EXPECT_EQ(progress(), 0LL); // network bytes not recorded when not busy. |
| 281 } | 301 } |
| 282 | 302 |
| 283 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { | 303 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { |
| 284 base::Time creation_time = base::Time::Now(); | 304 base::Time creation_time = base::Time::Now(); |
| 285 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 305 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 286 kUserRequested); | 306 kUserRequested); |
| 287 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 307 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 288 progress_callback())); | 308 progress_callback())); |
| 289 CompleteLoading(); | 309 CompleteLoading(); |
| 290 PumpLoop(); | 310 PumpLoop(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 311 PumpLoop(); | 331 PumpLoop(); |
| 312 model()->CompleteSavingAsArchiveCreationFailed(); | 332 model()->CompleteSavingAsArchiveCreationFailed(); |
| 313 PumpLoop(); | 333 PumpLoop(); |
| 314 | 334 |
| 315 EXPECT_TRUE(completion_callback_called()); | 335 EXPECT_TRUE(completion_callback_called()); |
| 316 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status()); | 336 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status()); |
| 317 EXPECT_FALSE(offliner()->is_loading()); | 337 EXPECT_FALSE(offliner()->is_loading()); |
| 318 EXPECT_FALSE(SaveInProgress()); | 338 EXPECT_FALSE(SaveInProgress()); |
| 319 } | 339 } |
| 320 | 340 |
| 341 TEST_F(BackgroundLoaderOfflinerTest, ProgressDoesNotUpdateDuringSave) { |
| 342 base::Time creation_time = base::Time::Now(); |
| 343 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 344 kUserRequested); |
| 345 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 346 progress_callback())); |
| 347 offliner()->OnNetworkBytesChanged(10LL); |
| 348 CompleteLoading(); |
| 349 PumpLoop(); |
| 350 offliner()->OnNetworkBytesChanged(15LL); |
| 351 EXPECT_EQ(progress(), 10LL); |
| 352 } |
| 353 |
| 321 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveSuccess) { | 354 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveSuccess) { |
| 322 base::Time creation_time = base::Time::Now(); | 355 base::Time creation_time = base::Time::Now(); |
| 323 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 356 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 324 kUserRequested); | 357 kUserRequested); |
| 325 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 358 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 326 progress_callback())); | 359 progress_callback())); |
| 327 | 360 |
| 328 CompleteLoading(); | 361 CompleteLoading(); |
| 329 PumpLoop(); | 362 PumpLoop(); |
| 330 model()->CompleteSavingAsSuccess(); | 363 model()->CompleteSavingAsSuccess(); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 model()->CompleteSavingAsSuccess(); | 453 model()->CompleteSavingAsSuccess(); |
| 421 PumpLoop(); | 454 PumpLoop(); |
| 422 | 455 |
| 423 EXPECT_TRUE(completion_callback_called()); | 456 EXPECT_TRUE(completion_callback_called()); |
| 424 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); | 457 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 425 EXPECT_FALSE(offliner()->is_loading()); | 458 EXPECT_FALSE(offliner()->is_loading()); |
| 426 EXPECT_FALSE(SaveInProgress()); | 459 EXPECT_FALSE(SaveInProgress()); |
| 427 } | 460 } |
| 428 | 461 |
| 429 } // namespace offline_pages | 462 } // namespace offline_pages |
| OLD | NEW |