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