| 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 15 matching lines...) Expand all Loading... |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 27 |
| 28 namespace offline_pages { | 28 namespace offline_pages { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 // put test constants here | 31 // put test constants here |
| 32 const GURL kUrl("http://universe.com/everything"); | 32 const GURL kUrl("http://universe.com/everything"); |
| 33 const ClientId kClientId("bookmark", "42"); | 33 const ClientId kClientId("bookmark", "42"); |
| 34 const int kRequestId(1); | 34 const int kRequestId(1); |
| 35 const long kTestTimeoutSeconds = 1; | 35 const long kTestTimeoutSeconds = 1; |
| 36 const long kTestTimeBudgetSeconds = 200; |
| 36 const int kBatteryPercentageHigh = 75; | 37 const int kBatteryPercentageHigh = 75; |
| 37 const bool kPowerRequired = true; | 38 const bool kPowerRequired = true; |
| 38 const bool kUserRequested = true; | 39 const bool kUserRequested = true; |
| 40 const int kAttemptCount = 1; |
| 39 } // namespace | 41 } // namespace |
| 40 | 42 |
| 41 class SchedulerStub : public Scheduler { | 43 class SchedulerStub : public Scheduler { |
| 42 public: | 44 public: |
| 43 SchedulerStub() | 45 SchedulerStub() |
| 44 : schedule_called_(false), | 46 : schedule_called_(false), |
| 45 unschedule_called_(false), | 47 unschedule_called_(false), |
| 46 conditions_(false, 0, false) {} | 48 conditions_(false, 0, false) {} |
| 47 | 49 |
| 48 void Schedule(const TriggerConditions& trigger_conditions) override { | 50 void Schedule(const TriggerConditions& trigger_conditions) override { |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 // which won't time out immediately, so the watchdog thread doesn't kill valid | 509 // which won't time out immediately, so the watchdog thread doesn't kill valid |
| 508 // tasks too soon. | 510 // tasks too soon. |
| 509 WaitForCallback(); | 511 WaitForCallback(); |
| 510 PumpLoop(); | 512 PumpLoop(); |
| 511 | 513 |
| 512 EXPECT_TRUE(OfflinerWasCanceled()); | 514 EXPECT_TRUE(OfflinerWasCanceled()); |
| 513 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, | 515 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, |
| 514 last_offlining_status()); | 516 last_offlining_status()); |
| 515 } | 517 } |
| 516 | 518 |
| 519 |
| 520 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) { |
| 521 // Build a request to use with the pre-renderer, and put it on the queue. |
| 522 offline_pages::SavePageRequest request1( |
| 523 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); |
| 524 offline_pages::SavePageRequest request2( |
| 525 kRequestId + 1, kUrl, kClientId, base::Time::Now(), kUserRequested); |
| 526 request2.set_attempt_count(kAttemptCount); |
| 527 coordinator()->queue()->AddRequest( |
| 528 request1, |
| 529 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 530 base::Unretained(this))); |
| 531 coordinator()->queue()->AddRequest( |
| 532 request1, |
| 533 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 534 base::Unretained(this))); |
| 535 PumpLoop(); |
| 536 |
| 537 // Set up for the call to StartProcessing. |
| 538 DeviceConditions device_conditions( |
| 539 !kPowerRequired, kBatteryPercentageHigh, |
| 540 net::NetworkChangeNotifier::CONNECTION_3G); |
| 541 base::Callback<void(bool)> callback = |
| 542 base::Bind(&RequestCoordinatorTest::WaitingCallbackFunction, |
| 543 base::Unretained(this)); |
| 544 |
| 545 // Sending the request to the offliner. |
| 546 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); |
| 547 PumpLoop(); |
| 548 |
| 549 // Advance the mock clock far enough to exceed our time budget. |
| 550 AdvanceClockBy(base::TimeDelta::FromSeconds(kTestTimeBudgetSeconds)); |
| 551 PumpLoop(); |
| 552 |
| 553 // TryNextRequest should decide that there is no more work to be done, |
| 554 // and call back to the scheduler, even though there is another request in the |
| 555 // queue. There should be one request left in the queue. |
| 556 // Verify the request gets removed from the queue, and wait for callbacks. |
| 557 coordinator()->queue()->GetRequests( |
| 558 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 559 base::Unretained(this))); |
| 560 PumpLoop(); |
| 561 |
| 562 // We should find one request in the queue. |
| 563 EXPECT_EQ(1UL, last_requests().size()); |
| 564 } |
| 565 |
| 517 } // namespace offline_pages | 566 } // namespace offline_pages |
| OLD | NEW |