Chromium Code Reviews| 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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 | 234 |
| 235 const RequestQueue::UpdateMultipleRequestResults& last_remove_results() | 235 const RequestQueue::UpdateMultipleRequestResults& last_remove_results() |
| 236 const { | 236 const { |
| 237 return last_remove_results_; | 237 return last_remove_results_; |
| 238 } | 238 } |
| 239 | 239 |
| 240 void EnableOfflinerCallback(bool enable) { | 240 void EnableOfflinerCallback(bool enable) { |
| 241 offliner_->enable_callback(enable); | 241 offliner_->enable_callback(enable); |
| 242 } | 242 } |
| 243 | 243 |
| 244 void ScheduleForTest() { | |
| 245 coordinator_->ScheduleAsNeeded(); | |
| 246 } | |
| 247 | |
| 248 void CallRequestNotPicked(bool non_user_requested_tasks_remaining) { | |
| 249 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining); | |
| 250 } | |
| 251 | |
| 244 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 252 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { |
| 245 coordinator_->SetOfflinerTimeoutForTest(timeout); | 253 coordinator_->SetOfflinerTimeoutForTest(timeout); |
| 246 } | 254 } |
| 247 | 255 |
| 248 void SetDeviceConditionsForTest(DeviceConditions device_conditions) { | 256 void SetDeviceConditionsForTest(DeviceConditions device_conditions) { |
| 249 coordinator_->SetDeviceConditionsForTest(device_conditions); | 257 coordinator_->SetDeviceConditionsForTest(device_conditions); |
| 250 } | 258 } |
| 251 | 259 |
| 252 void WaitForCallback() { | 260 void WaitForCallback() { |
| 253 waiter_.Wait(); | 261 waiter_.Wait(); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 // Check the request queue is as expected. | 392 // Check the request queue is as expected. |
| 385 EXPECT_EQ(1UL, last_requests().size()); | 393 EXPECT_EQ(1UL, last_requests().size()); |
| 386 EXPECT_EQ(kUrl1, last_requests()[0].url()); | 394 EXPECT_EQ(kUrl1, last_requests()[0].url()); |
| 387 EXPECT_EQ(kClientId1, last_requests()[0].client_id()); | 395 EXPECT_EQ(kClientId1, last_requests()[0].client_id()); |
| 388 | 396 |
| 389 // Expect that the scheduler got notified. | 397 // Expect that the scheduler got notified. |
| 390 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( | 398 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( |
| 391 coordinator()->scheduler()); | 399 coordinator()->scheduler()); |
| 392 EXPECT_TRUE(scheduler_stub->schedule_called()); | 400 EXPECT_TRUE(scheduler_stub->schedule_called()); |
| 393 EXPECT_EQ(coordinator() | 401 EXPECT_EQ(coordinator() |
| 394 ->GetTriggerConditionsForUserRequest() | 402 ->GetTriggerConditions(last_requests()[0].user_requested()) |
| 395 .minimum_battery_percentage, | 403 .minimum_battery_percentage, |
| 396 scheduler_stub->conditions()->minimum_battery_percentage); | 404 scheduler_stub->conditions()->minimum_battery_percentage); |
| 397 | 405 |
| 398 // Check that the observer got the notification that a page is available | 406 // Check that the observer got the notification that a page is available |
| 399 EXPECT_TRUE(observer().added_called()); | 407 EXPECT_TRUE(observer().added_called()); |
| 400 } | 408 } |
| 401 | 409 |
| 402 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { | 410 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { |
| 403 // Add a request to the queue, wait for callbacks to finish. | 411 // Add a request to the queue, wait for callbacks to finish. |
| 404 offline_pages::SavePageRequest request( | 412 offline_pages::SavePageRequest request( |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 540 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); | 548 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); |
| 541 PumpLoop(); | 549 PumpLoop(); |
| 542 | 550 |
| 543 // Request no longer in the queue (for single attempt policy). | 551 // Request no longer in the queue (for single attempt policy). |
| 544 EXPECT_EQ(1UL, last_requests().size()); | 552 EXPECT_EQ(1UL, last_requests().size()); |
| 545 // Verify foreground cancel not counted as an attempt after all. | 553 // Verify foreground cancel not counted as an attempt after all. |
| 546 const SavePageRequest& found_request = last_requests().front(); | 554 const SavePageRequest& found_request = last_requests().front(); |
| 547 EXPECT_EQ(0L, found_request.completed_attempt_count()); | 555 EXPECT_EQ(0L, found_request.completed_attempt_count()); |
| 548 } | 556 } |
| 549 | 557 |
| 558 // If one item completes, and there are no more user requeted items left, | |
| 559 // we should make a scheduler entry for a non-user requested item. | |
| 560 TEST_F(RequestCoordinatorTest, RequestNotPickedNonUserRequestedItemsRemain) { | |
| 561 // Call start processing just to set up a scheduler callback. | |
| 562 DeviceConditions device_conditions(false, 75, | |
| 563 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 564 base::Callback<void(bool)> callback = | |
| 565 base::Bind( | |
| 566 &RequestCoordinatorTest::EmptyCallbackFunction, | |
| 567 base::Unretained(this)); | |
| 568 coordinator()->StartProcessing(device_conditions, callback); | |
| 569 | |
| 570 // Call RequestNotPicked, and make sure we pick schedule a task for non user | |
| 571 // requested conditions. | |
| 572 CallRequestNotPicked(true); | |
| 573 PumpLoop(); | |
| 574 | |
| 575 // The scheduler should have been called to schedule the non-user requested | |
| 576 // task. | |
| 577 SchedulerStub* scheduler_stub = | |
| 578 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler()); | |
| 579 EXPECT_TRUE(scheduler_stub->schedule_called()); | |
|
dougarnett
2016/08/29 15:47:22
Seems like we also should check that Unschedule()
Pete Williamson
2016/09/01 00:10:31
Done.
| |
| 580 const Scheduler::TriggerConditions* conditions = scheduler_stub->conditions(); | |
| 581 EXPECT_EQ(conditions->require_power_connected, | |
| 582 coordinator()->policy()->PowerRequired(!kUserRequested)); | |
| 583 EXPECT_EQ( | |
| 584 conditions->minimum_battery_percentage, | |
| 585 coordinator()->policy()->BatteryPercentageRequired(!kUserRequested)); | |
| 586 EXPECT_EQ(conditions->require_unmetered_network, | |
| 587 coordinator()->policy()->UnmeteredNetworkRequired(!kUserRequested)); | |
| 588 } | |
| 589 | |
| 590 TEST_F(RequestCoordinatorTest, SchedulerGetsLeastRestrictiveConditions) { | |
| 591 // Put two requests on the queue - The first is user requested, and | |
| 592 // the second is not user requested. | |
| 593 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, | |
| 594 base::Time::Now(), kUserRequested); | |
| 595 coordinator()->queue()->AddRequest( | |
| 596 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, | |
| 597 base::Unretained(this))); | |
| 598 offline_pages::SavePageRequest request2(kRequestId2, kUrl2, kClientId2, | |
| 599 base::Time::Now(), !kUserRequested); | |
| 600 coordinator()->queue()->AddRequest( | |
| 601 request2, base::Bind(&RequestCoordinatorTest::AddRequestDone, | |
| 602 base::Unretained(this))); | |
| 603 PumpLoop(); | |
| 604 | |
| 605 // Trigger the scheduler to schedule for the least restrictive condition. | |
| 606 ScheduleForTest(); | |
| 607 PumpLoop(); | |
| 608 | |
| 609 // Expect that the scheduler got notified, and it is at user_requested | |
| 610 // priority. | |
| 611 SchedulerStub* scheduler_stub = | |
| 612 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler()); | |
| 613 const Scheduler::TriggerConditions* conditions = scheduler_stub->conditions(); | |
| 614 EXPECT_TRUE(scheduler_stub->schedule_called()); | |
| 615 EXPECT_EQ(conditions->require_power_connected, | |
| 616 coordinator()->policy()->PowerRequired(kUserRequested)); | |
| 617 EXPECT_EQ( | |
| 618 conditions->minimum_battery_percentage, | |
| 619 coordinator()->policy()->BatteryPercentageRequired(kUserRequested)); | |
| 620 EXPECT_EQ(conditions->require_unmetered_network, | |
| 621 coordinator()->policy()->UnmeteredNetworkRequired(kUserRequested)); | |
| 622 } | |
| 623 | |
| 550 // This tests a StopProcessing call before we have actually started the | 624 // This tests a StopProcessing call before we have actually started the |
| 551 // prerenderer. | 625 // prerenderer. |
| 552 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { | 626 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { |
| 553 // Add a request to the queue, wait for callbacks to finish. | 627 // Add a request to the queue, wait for callbacks to finish. |
| 554 offline_pages::SavePageRequest request( | 628 offline_pages::SavePageRequest request( |
| 555 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 629 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 556 coordinator()->queue()->AddRequest( | 630 coordinator()->queue()->AddRequest( |
| 557 request, | 631 request, |
| 558 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 632 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 559 base::Unretained(this))); | 633 base::Unretained(this))); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 828 PumpLoop(); | 902 PumpLoop(); |
| 829 | 903 |
| 830 EXPECT_TRUE(observer().completed_called()); | 904 EXPECT_TRUE(observer().completed_called()); |
| 831 EXPECT_EQ(RequestCoordinator::SavePageStatus::REMOVED, | 905 EXPECT_EQ(RequestCoordinator::SavePageStatus::REMOVED, |
| 832 observer().last_status()); | 906 observer().last_status()); |
| 833 EXPECT_EQ(1UL, last_remove_results().size()); | 907 EXPECT_EQ(1UL, last_remove_results().size()); |
| 834 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0))); | 908 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0))); |
| 835 } | 909 } |
| 836 | 910 |
| 837 } // namespace offline_pages | 911 } // namespace offline_pages |
| OLD | NEW |