| 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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 | 232 |
| 233 void EnableOfflinerCallback(bool enable) { | 233 void EnableOfflinerCallback(bool enable) { |
| 234 offliner_->enable_callback(enable); | 234 offliner_->enable_callback(enable); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void SetNetworkConditionsForTest( | 237 void SetNetworkConditionsForTest( |
| 238 net::NetworkChangeNotifier::ConnectionType connection) { | 238 net::NetworkChangeNotifier::ConnectionType connection) { |
| 239 coordinator()->SetNetworkConditionsForTest(connection); | 239 coordinator()->SetNetworkConditionsForTest(connection); |
| 240 } | 240 } |
| 241 | 241 |
| 242 void ScheduleForTest() { coordinator_->ScheduleAsNeeded(); } |
| 243 |
| 244 void CallRequestNotPicked(bool non_user_requested_tasks_remaining) { |
| 245 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining); |
| 246 } |
| 247 |
| 242 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 248 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { |
| 243 coordinator_->SetOfflinerTimeoutForTest(timeout); | 249 coordinator_->SetOfflinerTimeoutForTest(timeout); |
| 244 } | 250 } |
| 245 | 251 |
| 246 void SetDeviceConditionsForTest(DeviceConditions device_conditions) { | 252 void SetDeviceConditionsForTest(DeviceConditions device_conditions) { |
| 247 coordinator_->SetDeviceConditionsForTest(device_conditions); | 253 coordinator_->SetDeviceConditionsForTest(device_conditions); |
| 248 } | 254 } |
| 249 | 255 |
| 250 void WaitForCallback() { | 256 void WaitForCallback() { |
| 251 waiter_.Wait(); | 257 waiter_.Wait(); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 // Check the request queue is as expected. | 390 // Check the request queue is as expected. |
| 385 EXPECT_EQ(1UL, last_requests().size()); | 391 EXPECT_EQ(1UL, last_requests().size()); |
| 386 EXPECT_EQ(kUrl1, last_requests()[0].url()); | 392 EXPECT_EQ(kUrl1, last_requests()[0].url()); |
| 387 EXPECT_EQ(kClientId1, last_requests()[0].client_id()); | 393 EXPECT_EQ(kClientId1, last_requests()[0].client_id()); |
| 388 | 394 |
| 389 // Expect that the scheduler got notified. | 395 // Expect that the scheduler got notified. |
| 390 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( | 396 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( |
| 391 coordinator()->scheduler()); | 397 coordinator()->scheduler()); |
| 392 EXPECT_TRUE(scheduler_stub->schedule_called()); | 398 EXPECT_TRUE(scheduler_stub->schedule_called()); |
| 393 EXPECT_EQ(coordinator() | 399 EXPECT_EQ(coordinator() |
| 394 ->GetTriggerConditionsForUserRequest() | 400 ->GetTriggerConditions(last_requests()[0].user_requested()) |
| 395 .minimum_battery_percentage, | 401 .minimum_battery_percentage, |
| 396 scheduler_stub->conditions()->minimum_battery_percentage); | 402 scheduler_stub->conditions()->minimum_battery_percentage); |
| 397 | 403 |
| 398 // Check that the observer got the notification that a page is available | 404 // Check that the observer got the notification that a page is available |
| 399 EXPECT_TRUE(observer().added_called()); | 405 EXPECT_TRUE(observer().added_called()); |
| 400 } | 406 } |
| 401 | 407 |
| 402 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { | 408 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { |
| 403 // Add a request to the queue, wait for callbacks to finish. | 409 // Add a request to the queue, wait for callbacks to finish. |
| 404 offline_pages::SavePageRequest request( | 410 offline_pages::SavePageRequest request( |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); | 581 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); |
| 576 PumpLoop(); | 582 PumpLoop(); |
| 577 | 583 |
| 578 // Request still in the queue. | 584 // Request still in the queue. |
| 579 EXPECT_EQ(1UL, last_requests().size()); | 585 EXPECT_EQ(1UL, last_requests().size()); |
| 580 // Verify prerendering cancel not counted as an attempt after all. | 586 // Verify prerendering cancel not counted as an attempt after all. |
| 581 const SavePageRequest& found_request = last_requests().front(); | 587 const SavePageRequest& found_request = last_requests().front(); |
| 582 EXPECT_EQ(0L, found_request.completed_attempt_count()); | 588 EXPECT_EQ(0L, found_request.completed_attempt_count()); |
| 583 } | 589 } |
| 584 | 590 |
| 591 // If one item completes, and there are no more user requeted items left, |
| 592 // we should make a scheduler entry for a non-user requested item. |
| 593 TEST_F(RequestCoordinatorTest, RequestNotPickedNonUserRequestedItemsRemain) { |
| 594 // Call start processing just to set up a scheduler callback. |
| 595 DeviceConditions device_conditions(false, 75, |
| 596 net::NetworkChangeNotifier::CONNECTION_3G); |
| 597 base::Callback<void(bool)> callback = base::Bind( |
| 598 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this)); |
| 599 coordinator()->StartProcessing(device_conditions, callback); |
| 600 |
| 601 // Call RequestNotPicked, and make sure we pick schedule a task for non user |
| 602 // requested conditions. |
| 603 CallRequestNotPicked(true); |
| 604 PumpLoop(); |
| 605 |
| 606 // The scheduler should have been called to schedule the non-user requested |
| 607 // task. |
| 608 SchedulerStub* scheduler_stub = |
| 609 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler()); |
| 610 EXPECT_TRUE(scheduler_stub->schedule_called()); |
| 611 EXPECT_TRUE(scheduler_stub->unschedule_called()); |
| 612 const Scheduler::TriggerConditions* conditions = scheduler_stub->conditions(); |
| 613 EXPECT_EQ(conditions->require_power_connected, |
| 614 coordinator()->policy()->PowerRequired(!kUserRequested)); |
| 615 EXPECT_EQ( |
| 616 conditions->minimum_battery_percentage, |
| 617 coordinator()->policy()->BatteryPercentageRequired(!kUserRequested)); |
| 618 EXPECT_EQ(conditions->require_unmetered_network, |
| 619 coordinator()->policy()->UnmeteredNetworkRequired(!kUserRequested)); |
| 620 } |
| 621 |
| 622 TEST_F(RequestCoordinatorTest, SchedulerGetsLeastRestrictiveConditions) { |
| 623 // Put two requests on the queue - The first is user requested, and |
| 624 // the second is not user requested. |
| 625 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, |
| 626 base::Time::Now(), kUserRequested); |
| 627 coordinator()->queue()->AddRequest( |
| 628 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 629 base::Unretained(this))); |
| 630 offline_pages::SavePageRequest request2(kRequestId2, kUrl2, kClientId2, |
| 631 base::Time::Now(), !kUserRequested); |
| 632 coordinator()->queue()->AddRequest( |
| 633 request2, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 634 base::Unretained(this))); |
| 635 PumpLoop(); |
| 636 |
| 637 // Trigger the scheduler to schedule for the least restrictive condition. |
| 638 ScheduleForTest(); |
| 639 PumpLoop(); |
| 640 |
| 641 // Expect that the scheduler got notified, and it is at user_requested |
| 642 // priority. |
| 643 SchedulerStub* scheduler_stub = |
| 644 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler()); |
| 645 const Scheduler::TriggerConditions* conditions = scheduler_stub->conditions(); |
| 646 EXPECT_TRUE(scheduler_stub->schedule_called()); |
| 647 EXPECT_EQ(conditions->require_power_connected, |
| 648 coordinator()->policy()->PowerRequired(kUserRequested)); |
| 649 EXPECT_EQ(conditions->minimum_battery_percentage, |
| 650 coordinator()->policy()->BatteryPercentageRequired(kUserRequested)); |
| 651 EXPECT_EQ(conditions->require_unmetered_network, |
| 652 coordinator()->policy()->UnmeteredNetworkRequired(kUserRequested)); |
| 653 } |
| 654 |
| 585 // This tests a StopProcessing call before we have actually started the | 655 // This tests a StopProcessing call before we have actually started the |
| 586 // prerenderer. | 656 // prerenderer. |
| 587 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { | 657 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { |
| 588 // Add a request to the queue, wait for callbacks to finish. | 658 // Add a request to the queue, wait for callbacks to finish. |
| 589 offline_pages::SavePageRequest request( | 659 offline_pages::SavePageRequest request( |
| 590 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 660 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 591 coordinator()->queue()->AddRequest( | 661 coordinator()->queue()->AddRequest( |
| 592 request, | 662 request, |
| 593 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 663 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 594 base::Unretained(this))); | 664 base::Unretained(this))); |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G); | 993 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G); |
| 924 | 994 |
| 925 // Resume the request while connected. | 995 // Resume the request while connected. |
| 926 coordinator()->ResumeRequests(request_ids); | 996 coordinator()->ResumeRequests(request_ids); |
| 927 EXPECT_FALSE(is_busy()); | 997 EXPECT_FALSE(is_busy()); |
| 928 PumpLoop(); | 998 PumpLoop(); |
| 929 EXPECT_TRUE(is_busy()); | 999 EXPECT_TRUE(is_busy()); |
| 930 } | 1000 } |
| 931 | 1001 |
| 932 } // namespace offline_pages | 1002 } // namespace offline_pages |
| OLD | NEW |