| 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_feature_list.h" | 10 #include "base/test/scoped_feature_list.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 const int64_t kRequestId = 7; | 37 const int64_t kRequestId = 7; |
| 38 const GURL kHttpUrl("http://www.tunafish.com"); | 38 const GURL kHttpUrl("http://www.tunafish.com"); |
| 39 const GURL kFileUrl("file://salmon.png"); | 39 const GURL kFileUrl("file://salmon.png"); |
| 40 const ClientId kClientId("async_loading", "88"); | 40 const ClientId kClientId("async_loading", "88"); |
| 41 const bool kUserRequested = true; | 41 const bool kUserRequested = true; |
| 42 | 42 |
| 43 // Mock OfflinePageModel for testing the SavePage calls | 43 // Mock OfflinePageModel for testing the SavePage calls |
| 44 class MockOfflinePageModel : public StubOfflinePageModel { | 44 class MockOfflinePageModel : public StubOfflinePageModel { |
| 45 public: | 45 public: |
| 46 MockOfflinePageModel() : mock_saving_(false) {} | 46 MockOfflinePageModel() : mock_saving_(false), mock_deleting_(false) {} |
| 47 ~MockOfflinePageModel() override {} | 47 ~MockOfflinePageModel() override {} |
| 48 | 48 |
| 49 void SavePage(const SavePageParams& save_page_params, | 49 void SavePage(const SavePageParams& save_page_params, |
| 50 std::unique_ptr<OfflinePageArchiver> archiver, | 50 std::unique_ptr<OfflinePageArchiver> archiver, |
| 51 const SavePageCallback& callback) override { | 51 const SavePageCallback& callback) override { |
| 52 mock_saving_ = true; | 52 mock_saving_ = true; |
| 53 save_page_callback_ = callback; | 53 save_page_callback_ = callback; |
| 54 } | 54 } |
| 55 | 55 |
| 56 void CompleteSavingAsArchiveCreationFailed() { | 56 void CompleteSavingAsArchiveCreationFailed() { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 70 } | 70 } |
| 71 | 71 |
| 72 void CompleteSavingAsAlreadyExists() { | 72 void CompleteSavingAsAlreadyExists() { |
| 73 DCHECK(mock_saving_); | 73 DCHECK(mock_saving_); |
| 74 mock_saving_ = false; | 74 mock_saving_ = false; |
| 75 base::ThreadTaskRunnerHandle::Get()->PostTask( | 75 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 76 FROM_HERE, base::Bind(save_page_callback_, | 76 FROM_HERE, base::Bind(save_page_callback_, |
| 77 SavePageResult::ALREADY_EXISTS, 123456)); | 77 SavePageResult::ALREADY_EXISTS, 123456)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, |
| 81 const DeletePageCallback& callback) override { |
| 82 mock_deleting_ = true; |
| 83 callback.Run(DeletePageResult::SUCCESS); |
| 84 } |
| 85 |
| 80 bool mock_saving() const { return mock_saving_; } | 86 bool mock_saving() const { return mock_saving_; } |
| 87 bool mock_deleting() const { return mock_deleting_; } |
| 81 | 88 |
| 82 private: | 89 private: |
| 83 bool mock_saving_; | 90 bool mock_saving_; |
| 91 bool mock_deleting_; |
| 84 SavePageCallback save_page_callback_; | 92 SavePageCallback save_page_callback_; |
| 85 | 93 |
| 86 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); | 94 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); |
| 87 }; | 95 }; |
| 88 | 96 |
| 89 } // namespace | 97 } // namespace |
| 90 | 98 |
| 91 // A BackgroundLoader that we can run tests on. | 99 // A BackgroundLoader that we can run tests on. |
| 92 // Overrides the ResetState so we don't actually try to create any web contents. | 100 // Overrides the ResetState so we don't actually try to create any web contents. |
| 93 // This is a temporary solution to test core BackgroundLoaderOffliner | 101 // This is a temporary solution to test core BackgroundLoaderOffliner |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 156 } |
| 149 Offliner::CancelCallback const cancel_callback() { | 157 Offliner::CancelCallback const cancel_callback() { |
| 150 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, | 158 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, |
| 151 base::Unretained(this)); | 159 base::Unretained(this)); |
| 152 } | 160 } |
| 153 Profile* profile() { return &profile_; } | 161 Profile* profile() { return &profile_; } |
| 154 bool completion_callback_called() { return completion_callback_called_; } | 162 bool completion_callback_called() { return completion_callback_called_; } |
| 155 Offliner::RequestStatus request_status() { return request_status_; } | 163 Offliner::RequestStatus request_status() { return request_status_; } |
| 156 bool cancel_callback_called() { return cancel_callback_called_; } | 164 bool cancel_callback_called() { return cancel_callback_called_; } |
| 157 bool SaveInProgress() const { return model_->mock_saving(); } | 165 bool SaveInProgress() const { return model_->mock_saving(); } |
| 166 bool DeleteCalled() const { return model_->mock_deleting(); } |
| 158 MockOfflinePageModel* model() const { return model_; } | 167 MockOfflinePageModel* model() const { return model_; } |
| 159 const base::HistogramTester& histograms() const { return histogram_tester_; } | 168 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 160 int64_t progress() { return progress_; } | 169 int64_t progress() { return progress_; } |
| 161 OfflinerPolicy* policy() const { return policy_.get(); } | 170 OfflinerPolicy* policy() const { return policy_.get(); } |
| 162 | 171 |
| 163 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | 172 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
| 164 | 173 |
| 165 void CompleteLoading() { | 174 void CompleteLoading() { |
| 166 offliner()->DocumentOnLoadCompletedInMainFrame(); | 175 offliner()->DocumentOnLoadCompletedInMainFrame(); |
| 167 PumpLoop(); | 176 PumpLoop(); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 progress_callback())); | 332 progress_callback())); |
| 324 CompleteLoading(); | 333 CompleteLoading(); |
| 325 PumpLoop(); | 334 PumpLoop(); |
| 326 offliner()->Cancel(cancel_callback()); | 335 offliner()->Cancel(cancel_callback()); |
| 327 PumpLoop(); | 336 PumpLoop(); |
| 328 | 337 |
| 329 // Subsequent save callback cause no crash. | 338 // Subsequent save callback cause no crash. |
| 330 model()->CompleteSavingAsArchiveCreationFailed(); | 339 model()->CompleteSavingAsArchiveCreationFailed(); |
| 331 PumpLoop(); | 340 PumpLoop(); |
| 332 EXPECT_TRUE(cancel_callback_called()); | 341 EXPECT_TRUE(cancel_callback_called()); |
| 342 EXPECT_TRUE(DeleteCalled()); |
| 333 EXPECT_FALSE(completion_callback_called()); | 343 EXPECT_FALSE(completion_callback_called()); |
| 334 EXPECT_FALSE(SaveInProgress()); | 344 EXPECT_FALSE(SaveInProgress()); |
| 335 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 345 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 336 } | 346 } |
| 337 | 347 |
| 338 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { | 348 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { |
| 339 base::Time creation_time = base::Time::Now(); | 349 base::Time creation_time = base::Time::Now(); |
| 340 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 350 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 341 kUserRequested); | 351 kUserRequested); |
| 342 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 352 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 CompleteLoading(); | 634 CompleteLoading(); |
| 625 PumpLoop(); | 635 PumpLoop(); |
| 626 | 636 |
| 627 // One extra part should be added if the flag is on. | 637 // One extra part should be added if the flag is on. |
| 628 content::MHTMLExtraParts* extra_parts = | 638 content::MHTMLExtraParts* extra_parts = |
| 629 content::MHTMLExtraParts::FromWebContents(offliner()->web_contents()); | 639 content::MHTMLExtraParts::FromWebContents(offliner()->web_contents()); |
| 630 EXPECT_EQ(extra_parts->size(), 1); | 640 EXPECT_EQ(extra_parts->size(), 1); |
| 631 } | 641 } |
| 632 | 642 |
| 633 } // namespace offline_pages | 643 } // namespace offline_pages |
| OLD | NEW |