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 "components/offline_pages/background/request_coordinator.h" | 5 #include "components/offline_pages/background/request_coordinator.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 if (offliner_.get() == nullptr) { | 119 if (offliner_.get() == nullptr) { |
120 offliner_.reset(new OfflinerStub()); | 120 offliner_.reset(new OfflinerStub()); |
121 } | 121 } |
122 return offliner_.get(); | 122 return offliner_.get(); |
123 } | 123 } |
124 | 124 |
125 private: | 125 private: |
126 std::unique_ptr<OfflinerStub> offliner_; | 126 std::unique_ptr<OfflinerStub> offliner_; |
127 }; | 127 }; |
128 | 128 |
| 129 class ObserverStub : public RequestCoordinator::Observer { |
| 130 public: |
| 131 ObserverStub() |
| 132 : added_called_(false), |
| 133 succeeded_called_(false), |
| 134 failed_called_(false), |
| 135 changed_called_(false), |
| 136 removed_called_(false), |
| 137 state_(SavePageRequest::RequestState::PRERENDERING) {} |
| 138 |
| 139 void Clear() { |
| 140 added_called_ = false; |
| 141 succeeded_called_ = false; |
| 142 failed_called_ = false; |
| 143 changed_called_ = false; |
| 144 removed_called_ = false; |
| 145 state_ = SavePageRequest::RequestState::PRERENDERING; |
| 146 } |
| 147 |
| 148 void OnAdded(const SavePageRequest& request) override { |
| 149 added_called_ = true; |
| 150 } |
| 151 |
| 152 void OnSucceeded(const SavePageRequest& request) override { |
| 153 succeeded_called_ = true; |
| 154 } |
| 155 |
| 156 void OnFailed(const SavePageRequest& request, |
| 157 RequestCoordinator::SavePageStatus status) override { |
| 158 failed_called_ = true; |
| 159 } |
| 160 |
| 161 void OnChanged(const SavePageRequest& request) override { |
| 162 changed_called_ = true; |
| 163 state_ = request.request_state(); |
| 164 } |
| 165 |
| 166 void OnRemoved(const SavePageRequest& request, |
| 167 RequestCoordinator::SavePageStatus status) override { |
| 168 removed_called_ = true; |
| 169 } |
| 170 |
| 171 bool added_called() { return added_called_; } |
| 172 bool succeeded_called() { return succeeded_called_; } |
| 173 bool failed_called() { return failed_called_; } |
| 174 bool changed_called() { return changed_called_; } |
| 175 bool removed_called() { return removed_called_; } |
| 176 SavePageRequest::RequestState state() { return state_; } |
| 177 |
| 178 private: |
| 179 bool added_called_; |
| 180 bool succeeded_called_; |
| 181 bool failed_called_; |
| 182 bool changed_called_; |
| 183 bool removed_called_; |
| 184 SavePageRequest::RequestState state_; |
| 185 }; |
| 186 |
129 class RequestCoordinatorTest | 187 class RequestCoordinatorTest |
130 : public testing::Test { | 188 : public testing::Test { |
131 public: | 189 public: |
132 RequestCoordinatorTest(); | 190 RequestCoordinatorTest(); |
133 ~RequestCoordinatorTest() override; | 191 ~RequestCoordinatorTest() override; |
134 | 192 |
135 void SetUp() override; | 193 void SetUp() override; |
136 | 194 |
137 void PumpLoop(); | 195 void PumpLoop(); |
138 | 196 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 void AdvanceClockBy(base::TimeDelta delta) { | 252 void AdvanceClockBy(base::TimeDelta delta) { |
195 task_runner_->FastForwardBy(delta); | 253 task_runner_->FastForwardBy(delta); |
196 } | 254 } |
197 | 255 |
198 Offliner::RequestStatus last_offlining_status() const { | 256 Offliner::RequestStatus last_offlining_status() const { |
199 return coordinator_->last_offlining_status_; | 257 return coordinator_->last_offlining_status_; |
200 } | 258 } |
201 | 259 |
202 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } | 260 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } |
203 | 261 |
| 262 ObserverStub observer() { return observer_; } |
| 263 |
204 private: | 264 private: |
205 RequestQueue::GetRequestsResult last_get_requests_result_; | 265 RequestQueue::GetRequestsResult last_get_requests_result_; |
206 std::vector<SavePageRequest> last_requests_; | 266 std::vector<SavePageRequest> last_requests_; |
207 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 267 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
208 base::ThreadTaskRunnerHandle task_runner_handle_; | 268 base::ThreadTaskRunnerHandle task_runner_handle_; |
209 std::unique_ptr<RequestCoordinator> coordinator_; | 269 std::unique_ptr<RequestCoordinator> coordinator_; |
210 OfflinerStub* offliner_; | 270 OfflinerStub* offliner_; |
211 base::WaitableEvent waiter_; | 271 base::WaitableEvent waiter_; |
| 272 ObserverStub observer_; |
212 }; | 273 }; |
213 | 274 |
214 RequestCoordinatorTest::RequestCoordinatorTest() | 275 RequestCoordinatorTest::RequestCoordinatorTest() |
215 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), | 276 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), |
216 task_runner_(new base::TestMockTimeTaskRunner), | 277 task_runner_(new base::TestMockTimeTaskRunner), |
217 task_runner_handle_(task_runner_), | 278 task_runner_handle_(task_runner_), |
218 offliner_(nullptr), | 279 offliner_(nullptr), |
219 waiter_(base::WaitableEvent::ResetPolicy::MANUAL, | 280 waiter_(base::WaitableEvent::ResetPolicy::MANUAL, |
220 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 281 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
221 | 282 |
222 RequestCoordinatorTest::~RequestCoordinatorTest() {} | 283 RequestCoordinatorTest::~RequestCoordinatorTest() {} |
223 | 284 |
224 void RequestCoordinatorTest::SetUp() { | 285 void RequestCoordinatorTest::SetUp() { |
225 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); | 286 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); |
226 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); | 287 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); |
227 // Save the offliner for use by the tests. | 288 // Save the offliner for use by the tests. |
228 offliner_ = | 289 offliner_ = |
229 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); | 290 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); |
230 std::unique_ptr<RequestQueueInMemoryStore> | 291 std::unique_ptr<RequestQueueInMemoryStore> |
231 store(new RequestQueueInMemoryStore()); | 292 store(new RequestQueueInMemoryStore()); |
232 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); | 293 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); |
233 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); | 294 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); |
234 coordinator_.reset(new RequestCoordinator( | 295 coordinator_.reset(new RequestCoordinator( |
235 std::move(policy), std::move(factory), std::move(queue), | 296 std::move(policy), std::move(factory), std::move(queue), |
236 std::move(scheduler_stub))); | 297 std::move(scheduler_stub))); |
| 298 coordinator_->AddObserver(&observer_); |
237 } | 299 } |
238 | 300 |
239 void RequestCoordinatorTest::PumpLoop() { | 301 void RequestCoordinatorTest::PumpLoop() { |
240 task_runner_->RunUntilIdle(); | 302 task_runner_->RunUntilIdle(); |
241 } | 303 } |
242 | 304 |
243 void RequestCoordinatorTest::GetRequestsDone( | 305 void RequestCoordinatorTest::GetRequestsDone( |
244 RequestQueue::GetRequestsResult result, | 306 RequestQueue::GetRequestsResult result, |
245 const std::vector<SavePageRequest>& requests) { | 307 const std::vector<SavePageRequest>& requests) { |
246 last_get_requests_result_ = result; | 308 last_get_requests_result_ = result; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 EXPECT_EQ(kClientId1, last_requests()[0].client_id()); | 376 EXPECT_EQ(kClientId1, last_requests()[0].client_id()); |
315 | 377 |
316 // Expect that the scheduler got notified. | 378 // Expect that the scheduler got notified. |
317 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( | 379 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( |
318 coordinator()->scheduler()); | 380 coordinator()->scheduler()); |
319 EXPECT_TRUE(scheduler_stub->schedule_called()); | 381 EXPECT_TRUE(scheduler_stub->schedule_called()); |
320 EXPECT_EQ(coordinator() | 382 EXPECT_EQ(coordinator() |
321 ->GetTriggerConditionsForUserRequest() | 383 ->GetTriggerConditionsForUserRequest() |
322 .minimum_battery_percentage, | 384 .minimum_battery_percentage, |
323 scheduler_stub->conditions()->minimum_battery_percentage); | 385 scheduler_stub->conditions()->minimum_battery_percentage); |
| 386 |
| 387 // Check that the observer got the notification that a page is available |
| 388 EXPECT_TRUE(observer().added_called()); |
324 } | 389 } |
325 | 390 |
326 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { | 391 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { |
327 // Add a request to the queue, wait for callbacks to finish. | 392 // Add a request to the queue, wait for callbacks to finish. |
328 offline_pages::SavePageRequest request( | 393 offline_pages::SavePageRequest request( |
329 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 394 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
330 request.MarkAttemptStarted(base::Time::Now()); | 395 request.MarkAttemptStarted(base::Time::Now()); |
331 coordinator()->queue()->AddRequest( | 396 coordinator()->queue()->AddRequest( |
332 request, | 397 request, |
333 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 398 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
(...skipping 21 matching lines...) Expand all Loading... |
355 // Verify the request gets removed from the queue, and wait for callbacks. | 420 // Verify the request gets removed from the queue, and wait for callbacks. |
356 coordinator()->queue()->GetRequests( | 421 coordinator()->queue()->GetRequests( |
357 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 422 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
358 base::Unretained(this))); | 423 base::Unretained(this))); |
359 PumpLoop(); | 424 PumpLoop(); |
360 | 425 |
361 // We should not find any requests in the queue anymore. | 426 // We should not find any requests in the queue anymore. |
362 // RequestPicker should *not* have tried to start an additional job, | 427 // RequestPicker should *not* have tried to start an additional job, |
363 // because the request queue is empty now. | 428 // because the request queue is empty now. |
364 EXPECT_EQ(0UL, last_requests().size()); | 429 EXPECT_EQ(0UL, last_requests().size()); |
| 430 // Check that the observer got the notification that we succeeded. |
| 431 EXPECT_TRUE(observer().succeeded_called()); |
365 } | 432 } |
366 | 433 |
367 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { | 434 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { |
368 // Add a request to the queue, wait for callbacks to finish. | 435 // Add a request to the queue, wait for callbacks to finish. |
369 offline_pages::SavePageRequest request( | 436 offline_pages::SavePageRequest request( |
370 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 437 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
371 request.MarkAttemptStarted(base::Time::Now()); | 438 request.MarkAttemptStarted(base::Time::Now()); |
372 coordinator()->queue()->AddRequest( | 439 coordinator()->queue()->AddRequest( |
373 request, | 440 request, |
374 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 441 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 | 476 |
410 // Verify neither request is removed from the queue; wait for callbacks. | 477 // Verify neither request is removed from the queue; wait for callbacks. |
411 coordinator()->queue()->GetRequests( | 478 coordinator()->queue()->GetRequests( |
412 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 479 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
413 base::Unretained(this))); | 480 base::Unretained(this))); |
414 PumpLoop(); | 481 PumpLoop(); |
415 | 482 |
416 // Now just one request in the queue since failed request removed | 483 // Now just one request in the queue since failed request removed |
417 // (for single attempt policy). | 484 // (for single attempt policy). |
418 EXPECT_EQ(1UL, last_requests().size()); | 485 EXPECT_EQ(1UL, last_requests().size()); |
| 486 // Check that the observer got the notification that we failed |
| 487 EXPECT_TRUE(observer().failed_called()); |
419 } | 488 } |
420 | 489 |
421 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { | 490 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { |
422 // Add a request to the queue, wait for callbacks to finish. | 491 // Add a request to the queue, wait for callbacks to finish. |
423 offline_pages::SavePageRequest request( | 492 offline_pages::SavePageRequest request( |
424 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 493 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
425 request.MarkAttemptStarted(base::Time::Now()); | 494 request.MarkAttemptStarted(base::Time::Now()); |
426 coordinator()->queue()->AddRequest( | 495 coordinator()->queue()->AddRequest( |
427 request, base::Bind(&RequestCoordinatorTest::AddRequestDone, | 496 request, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
428 base::Unretained(this))); | 497 base::Unretained(this))); |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 // Wait for async get to finish. | 714 // Wait for async get to finish. |
646 WaitForCallback(); | 715 WaitForCallback(); |
647 PumpLoop(); | 716 PumpLoop(); |
648 | 717 |
649 // Check that the statuses found in the callback match what we expect. | 718 // Check that the statuses found in the callback match what we expect. |
650 EXPECT_EQ(2UL, last_requests().size()); | 719 EXPECT_EQ(2UL, last_requests().size()); |
651 EXPECT_EQ(kRequestId1, last_requests().at(0).request_id()); | 720 EXPECT_EQ(kRequestId1, last_requests().at(0).request_id()); |
652 EXPECT_EQ(kRequestId2, last_requests().at(1).request_id()); | 721 EXPECT_EQ(kRequestId2, last_requests().at(1).request_id()); |
653 } | 722 } |
654 | 723 |
| 724 TEST_F(RequestCoordinatorTest, PauseAndResumeObserver) { |
| 725 // Add a request to the queue. |
| 726 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, |
| 727 base::Time::Now(), kUserRequested); |
| 728 coordinator()->queue()->AddRequest( |
| 729 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 730 base::Unretained(this))); |
| 731 PumpLoop(); |
| 732 |
| 733 // Pause the request. |
| 734 std::vector<int64_t> request_ids; |
| 735 request_ids.push_back(kRequestId1); |
| 736 coordinator()->PauseRequests(request_ids); |
| 737 PumpLoop(); |
| 738 |
| 739 EXPECT_TRUE(observer().changed_called()); |
| 740 EXPECT_EQ(SavePageRequest::RequestState::PAUSED, observer().state()); |
| 741 |
| 742 // Clear out the observer before the next call. |
| 743 observer().Clear(); |
| 744 |
| 745 // Resume the request. |
| 746 coordinator()->ResumeRequests(request_ids); |
| 747 PumpLoop(); |
| 748 |
| 749 EXPECT_TRUE(observer().changed_called()); |
| 750 EXPECT_EQ(SavePageRequest::RequestState::AVAILABLE, observer().state()); |
| 751 } |
| 752 |
| 753 TEST_F(RequestCoordinatorTest, ObserverdRemoveRequest) { |
| 754 // Add a request to the queue. |
| 755 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, |
| 756 base::Time::Now(), kUserRequested); |
| 757 coordinator()->queue()->AddRequest( |
| 758 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 759 base::Unretained(this))); |
| 760 PumpLoop(); |
| 761 |
| 762 // Remove the request. |
| 763 std::vector<int64_t> request_ids; |
| 764 request_ids.push_back(kRequestId1); |
| 765 coordinator()->RemoveRequests(request_ids); |
| 766 PumpLoop(); |
| 767 |
| 768 EXPECT_TRUE(observer().removed_called()); |
| 769 } |
| 770 |
655 } // namespace offline_pages | 771 } // namespace offline_pages |
OLD | NEW |