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