OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "components/offline_pages/content/background_loader/background_loader_c
ontents_stub.h" |
| 13 #include "components/offline_pages/core/background/offliner.h" |
| 14 #include "components/offline_pages/core/background/save_page_request.h" |
| 15 #include "components/offline_pages/core/stub_offline_page_model.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "content/public/test/web_contents_tester.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace offline_pages { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const int64_t kRequestId = 7; |
| 25 const GURL kHttpUrl("http://www.tunafish.com"); |
| 26 const GURL kFileUrl("file://salmon.png"); |
| 27 const ClientId kClientId("AsyncLoading", "88"); |
| 28 const bool kUserRequested = true; |
| 29 |
| 30 // Mock OfflinePageModel for testing the SavePage calls |
| 31 class MockOfflinePageModel : public StubOfflinePageModel { |
| 32 public: |
| 33 MockOfflinePageModel() : mock_saving_(false) {} |
| 34 ~MockOfflinePageModel() override {} |
| 35 |
| 36 void SavePage(const SavePageParams& save_page_params, |
| 37 std::unique_ptr<OfflinePageArchiver> archiver, |
| 38 const SavePageCallback& callback) override { |
| 39 mock_saving_ = true; |
| 40 save_page_callback_ = callback; |
| 41 } |
| 42 |
| 43 void CompleteSavingAsArchiveCreationFailed() { |
| 44 DCHECK(mock_saving_); |
| 45 mock_saving_ = false; |
| 46 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 47 FROM_HERE, base::Bind(save_page_callback_, |
| 48 SavePageResult::ARCHIVE_CREATION_FAILED, 0)); |
| 49 } |
| 50 |
| 51 void CompleteSavingAsSuccess() { |
| 52 DCHECK(mock_saving_); |
| 53 mock_saving_ = false; |
| 54 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 55 FROM_HERE, |
| 56 base::Bind(save_page_callback_, SavePageResult::SUCCESS, 123456)); |
| 57 } |
| 58 |
| 59 bool mock_saving() const { return mock_saving_; } |
| 60 |
| 61 private: |
| 62 bool mock_saving_; |
| 63 SavePageCallback save_page_callback_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); |
| 66 }; |
| 67 |
| 68 void PumpLoop() { |
| 69 base::RunLoop().RunUntilIdle(); |
| 70 } |
| 71 } // namespace |
| 72 |
| 73 // A BackgroundLoader that we can run tests on. |
| 74 // Overrides the ResetState so we don't actually try to create any web contents. |
| 75 // This is a temporary solution to test core BackgroundLoaderOffliner |
| 76 // functionality until we straighten out assumptions made by RequestCoordinator |
| 77 // so that the ResetState method is no longer needed. |
| 78 class TestBackgroundLoaderOffliner : public BackgroundLoaderOffliner { |
| 79 public: |
| 80 explicit TestBackgroundLoaderOffliner( |
| 81 content::BrowserContext* browser_context, |
| 82 const OfflinerPolicy* policy, |
| 83 OfflinePageModel* offline_page_model); |
| 84 ~TestBackgroundLoaderOffliner() override; |
| 85 content::WebContentsTester* web_contents() { |
| 86 return content::WebContentsTester::For(stub_->web_contents()); |
| 87 } |
| 88 |
| 89 bool is_loading() { return stub_->is_loading(); } |
| 90 |
| 91 protected: |
| 92 void ResetState() override; |
| 93 |
| 94 private: |
| 95 background_loader::BackgroundLoaderContentsStub* stub_; |
| 96 }; |
| 97 |
| 98 TestBackgroundLoaderOffliner::TestBackgroundLoaderOffliner( |
| 99 content::BrowserContext* browser_context, |
| 100 const OfflinerPolicy* policy, |
| 101 OfflinePageModel* offline_page_model) |
| 102 : BackgroundLoaderOffliner(browser_context, policy, offline_page_model) {} |
| 103 |
| 104 TestBackgroundLoaderOffliner::~TestBackgroundLoaderOffliner() {} |
| 105 |
| 106 void TestBackgroundLoaderOffliner::ResetState() { |
| 107 pending_request_.reset(); |
| 108 stub_ = new background_loader::BackgroundLoaderContentsStub(browser_context_); |
| 109 loader_.reset(stub_); |
| 110 content::WebContentsObserver::Observe(stub_->web_contents()); |
| 111 } |
| 112 |
| 113 class BackgroundLoaderOfflinerTest : public testing::Test { |
| 114 public: |
| 115 BackgroundLoaderOfflinerTest(); |
| 116 ~BackgroundLoaderOfflinerTest() override; |
| 117 |
| 118 void SetUp() override; |
| 119 |
| 120 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); } |
| 121 Offliner::CompletionCallback const callback() { |
| 122 return base::Bind(&BackgroundLoaderOfflinerTest::OnCompletion, |
| 123 base::Unretained(this)); |
| 124 } |
| 125 Profile* profile() { return &profile_; } |
| 126 bool completion_callback_called() { return completion_callback_called_; } |
| 127 Offliner::RequestStatus request_status() { return request_status_; } |
| 128 bool SaveInProgress() const { return model_->mock_saving(); } |
| 129 MockOfflinePageModel* model() const { return model_; } |
| 130 |
| 131 void CompleteLoading() { |
| 132 offliner()->web_contents()->TestSetIsLoading(false); |
| 133 } |
| 134 |
| 135 private: |
| 136 void OnCompletion(const SavePageRequest& request, |
| 137 Offliner::RequestStatus status); |
| 138 content::TestBrowserThreadBundle thread_bundle_; |
| 139 TestingProfile profile_; |
| 140 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; |
| 141 MockOfflinePageModel* model_; |
| 142 bool completion_callback_called_; |
| 143 Offliner::RequestStatus request_status_; |
| 144 |
| 145 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); |
| 146 }; |
| 147 |
| 148 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() |
| 149 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 150 completion_callback_called_(false), |
| 151 request_status_(Offliner::RequestStatus::UNKNOWN) {} |
| 152 |
| 153 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} |
| 154 |
| 155 void BackgroundLoaderOfflinerTest::SetUp() { |
| 156 model_ = new MockOfflinePageModel(); |
| 157 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); |
| 158 } |
| 159 |
| 160 void BackgroundLoaderOfflinerTest::OnCompletion( |
| 161 const SavePageRequest& request, |
| 162 Offliner::RequestStatus status) { |
| 163 DCHECK(!completion_callback_called_); // Expect 1 callback per request. |
| 164 completion_callback_called_ = true; |
| 165 request_status_ = status; |
| 166 } |
| 167 |
| 168 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { |
| 169 base::Time creation_time = base::Time::Now(); |
| 170 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 171 kUserRequested); |
| 172 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 173 EXPECT_TRUE(offliner()->is_loading()); |
| 174 EXPECT_FALSE(SaveInProgress()); |
| 175 |
| 176 offliner()->Cancel(); |
| 177 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 178 } |
| 179 |
| 180 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { |
| 181 base::Time creation_time = base::Time::Now(); |
| 182 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 183 kUserRequested); |
| 184 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 185 EXPECT_TRUE(offliner()->is_loading()); |
| 186 |
| 187 CompleteLoading(); |
| 188 PumpLoop(); |
| 189 EXPECT_FALSE(completion_callback_called()); |
| 190 // Save still in progress because does not support canceling. |
| 191 EXPECT_TRUE(SaveInProgress()); |
| 192 offliner()->Cancel(); |
| 193 EXPECT_TRUE(offliner()->is_loading()); // Offliner did not reset. |
| 194 |
| 195 // Subsequent save callback cause no crash. |
| 196 model()->CompleteSavingAsArchiveCreationFailed(); |
| 197 PumpLoop(); |
| 198 EXPECT_FALSE(completion_callback_called()); |
| 199 EXPECT_FALSE(SaveInProgress()); |
| 200 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. |
| 201 } |
| 202 |
| 203 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { |
| 204 base::Time creation_time = base::Time::Now(); |
| 205 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 206 kUserRequested); |
| 207 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 208 EXPECT_TRUE(offliner()->is_loading()); |
| 209 |
| 210 CompleteLoading(); |
| 211 PumpLoop(); |
| 212 model()->CompleteSavingAsArchiveCreationFailed(); |
| 213 PumpLoop(); |
| 214 EXPECT_TRUE(completion_callback_called()); |
| 215 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status()); |
| 216 EXPECT_FALSE(offliner()->is_loading()); |
| 217 EXPECT_FALSE(SaveInProgress()); |
| 218 } |
| 219 |
| 220 TEST_F(BackgroundLoaderOfflinerTest, LoadAndSaveSuccess) { |
| 221 base::Time creation_time = base::Time::Now(); |
| 222 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 223 kUserRequested); |
| 224 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); |
| 225 EXPECT_TRUE(offliner()->is_loading()); |
| 226 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); |
| 227 |
| 228 CompleteLoading(); |
| 229 PumpLoop(); |
| 230 EXPECT_FALSE(completion_callback_called()); |
| 231 EXPECT_TRUE(SaveInProgress()); |
| 232 |
| 233 model()->CompleteSavingAsSuccess(); |
| 234 PumpLoop(); |
| 235 EXPECT_TRUE(completion_callback_called()); |
| 236 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 237 EXPECT_FALSE(offliner()->is_loading()); |
| 238 EXPECT_FALSE(SaveInProgress()); |
| 239 } |
| 240 |
| 241 } // namespace offline_pages |
OLD | NEW |