| 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 } |
| 84 |
| 80 bool mock_saving() const { return mock_saving_; } | 85 bool mock_saving() const { return mock_saving_; } |
| 86 bool mock_deleting() const { return mock_deleting_; } |
| 81 | 87 |
| 82 private: | 88 private: |
| 83 bool mock_saving_; | 89 bool mock_saving_; |
| 90 bool mock_deleting_; |
| 84 SavePageCallback save_page_callback_; | 91 SavePageCallback save_page_callback_; |
| 85 | 92 |
| 86 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); | 93 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); |
| 87 }; | 94 }; |
| 88 | 95 |
| 89 } // namespace | 96 } // namespace |
| 90 | 97 |
| 91 // A BackgroundLoader that we can run tests on. | 98 // A BackgroundLoader that we can run tests on. |
| 92 // Overrides the ResetState so we don't actually try to create any web contents. | 99 // Overrides the ResetState so we don't actually try to create any web contents. |
| 93 // This is a temporary solution to test core BackgroundLoaderOffliner | 100 // This is a temporary solution to test core BackgroundLoaderOffliner |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 155 } |
| 149 Offliner::CancelCallback const cancel_callback() { | 156 Offliner::CancelCallback const cancel_callback() { |
| 150 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, | 157 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel, |
| 151 base::Unretained(this)); | 158 base::Unretained(this)); |
| 152 } | 159 } |
| 153 Profile* profile() { return &profile_; } | 160 Profile* profile() { return &profile_; } |
| 154 bool completion_callback_called() { return completion_callback_called_; } | 161 bool completion_callback_called() { return completion_callback_called_; } |
| 155 Offliner::RequestStatus request_status() { return request_status_; } | 162 Offliner::RequestStatus request_status() { return request_status_; } |
| 156 bool cancel_callback_called() { return cancel_callback_called_; } | 163 bool cancel_callback_called() { return cancel_callback_called_; } |
| 157 bool SaveInProgress() const { return model_->mock_saving(); } | 164 bool SaveInProgress() const { return model_->mock_saving(); } |
| 165 bool DeleteCalled() const { return model_->mock_deleting(); } |
| 158 MockOfflinePageModel* model() const { return model_; } | 166 MockOfflinePageModel* model() const { return model_; } |
| 159 const base::HistogramTester& histograms() const { return histogram_tester_; } | 167 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 160 int64_t progress() { return progress_; } | 168 int64_t progress() { return progress_; } |
| 161 OfflinerPolicy* policy() const { return policy_.get(); } | 169 OfflinerPolicy* policy() const { return policy_.get(); } |
| 162 | 170 |
| 163 void PumpLoop() { base::RunLoop().RunUntilIdle(); } | 171 void PumpLoop() { base::RunLoop().RunUntilIdle(); } |
| 164 | 172 |
| 165 void CompleteLoading() { | 173 void CompleteLoading() { |
| 166 offliner()->DocumentOnLoadCompletedInMainFrame(); | 174 offliner()->DocumentOnLoadCompletedInMainFrame(); |
| 167 PumpLoop(); | 175 PumpLoop(); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 progress_callback())); | 331 progress_callback())); |
| 324 CompleteLoading(); | 332 CompleteLoading(); |
| 325 PumpLoop(); | 333 PumpLoop(); |
| 326 offliner()->Cancel(cancel_callback()); | 334 offliner()->Cancel(cancel_callback()); |
| 327 PumpLoop(); | 335 PumpLoop(); |
| 328 | 336 |
| 329 // Subsequent save callback cause no crash. | 337 // Subsequent save callback cause no crash. |
| 330 model()->CompleteSavingAsArchiveCreationFailed(); | 338 model()->CompleteSavingAsArchiveCreationFailed(); |
| 331 PumpLoop(); | 339 PumpLoop(); |
| 332 EXPECT_TRUE(cancel_callback_called()); | 340 EXPECT_TRUE(cancel_callback_called()); |
| 341 EXPECT_TRUE(DeleteCalled()); |
| 333 EXPECT_FALSE(completion_callback_called()); | 342 EXPECT_FALSE(completion_callback_called()); |
| 334 EXPECT_FALSE(SaveInProgress()); | 343 EXPECT_FALSE(SaveInProgress()); |
| 335 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. | 344 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 336 } | 345 } |
| 337 | 346 |
| 338 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { | 347 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { |
| 339 base::Time creation_time = base::Time::Now(); | 348 base::Time creation_time = base::Time::Now(); |
| 340 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 349 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 341 kUserRequested); | 350 kUserRequested); |
| 342 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 351 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 CompleteLoading(); | 633 CompleteLoading(); |
| 625 PumpLoop(); | 634 PumpLoop(); |
| 626 | 635 |
| 627 // One extra part should be added if the flag is on. | 636 // One extra part should be added if the flag is on. |
| 628 content::MHTMLExtraParts* extra_parts = | 637 content::MHTMLExtraParts* extra_parts = |
| 629 content::MHTMLExtraParts::FromWebContents(offliner()->web_contents()); | 638 content::MHTMLExtraParts::FromWebContents(offliner()->web_contents()); |
| 630 EXPECT_EQ(extra_parts->size(), 1); | 639 EXPECT_EQ(extra_parts->size(), 1); |
| 631 } | 640 } |
| 632 | 641 |
| 633 } // namespace offline_pages | 642 } // namespace offline_pages |
| OLD | NEW |