Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: components/offline_pages/background/request_coordinator_unittest.cc

Issue 2400393002: Set up a backup schedule request if we have deferred requests. (Closed)
Patch Set: CR feedback per Dimich Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 const int kBatteryPercentageHigh = 75; 44 const int kBatteryPercentageHigh = 75;
45 const bool kPowerRequired = true; 45 const bool kPowerRequired = true;
46 const bool kUserRequested = true; 46 const bool kUserRequested = true;
47 const int kAttemptCount = 1; 47 const int kAttemptCount = 1;
48 } // namespace 48 } // namespace
49 49
50 class SchedulerStub : public Scheduler { 50 class SchedulerStub : public Scheduler {
51 public: 51 public:
52 SchedulerStub() 52 SchedulerStub()
53 : schedule_called_(false), 53 : schedule_called_(false),
54 backup_schedule_called_(false),
54 unschedule_called_(false), 55 unschedule_called_(false),
56 schedule_delay_(0L),
55 conditions_(false, 0, false) {} 57 conditions_(false, 0, false) {}
56 58
57 void Schedule(const TriggerConditions& trigger_conditions) override { 59 void Schedule(const TriggerConditions& trigger_conditions) override {
58 schedule_called_ = true; 60 schedule_called_ = true;
59 conditions_ = trigger_conditions; 61 conditions_ = trigger_conditions;
60 } 62 }
61 63
64 void BackupSchedule(const TriggerConditions& trigger_conditions,
65 long delay_in_seconds) override {
66 backup_schedule_called_ = true;
67 schedule_delay_ = delay_in_seconds;
68 conditions_ = trigger_conditions;
69 }
70
71
62 // Unschedules the currently scheduled task, if any. 72 // Unschedules the currently scheduled task, if any.
63 void Unschedule() override { 73 void Unschedule() override {
64 unschedule_called_ = true; 74 unschedule_called_ = true;
65 } 75 }
66 76
67 bool schedule_called() const { return schedule_called_; } 77 bool schedule_called() const { return schedule_called_; }
68 78
79 bool backup_schedule_called() const { return backup_schedule_called_;}
80
69 bool unschedule_called() const { return unschedule_called_; } 81 bool unschedule_called() const { return unschedule_called_; }
70 82
71 TriggerConditions const* conditions() const { return &conditions_; } 83 TriggerConditions const* conditions() const { return &conditions_; }
72 84
73 private: 85 private:
74 bool schedule_called_; 86 bool schedule_called_;
87 bool backup_schedule_called_;
75 bool unschedule_called_; 88 bool unschedule_called_;
89 long schedule_delay_;
76 TriggerConditions conditions_; 90 TriggerConditions conditions_;
77 }; 91 };
78 92
79 class OfflinerStub : public Offliner { 93 class OfflinerStub : public Offliner {
80 public: 94 public:
81 OfflinerStub() 95 OfflinerStub()
82 : request_(kRequestId1, kUrl1, kClientId1, base::Time::Now(), 96 : request_(kRequestId1, kUrl1, kClientId1, base::Time::Now(),
83 kUserRequested), 97 kUserRequested),
84 disable_loading_(false), 98 disable_loading_(false),
85 enable_callback_(false), 99 enable_callback_(false),
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 net::NetworkChangeNotifier::ConnectionType connection) { 289 net::NetworkChangeNotifier::ConnectionType connection) {
276 coordinator()->SetNetworkConditionsForTest(connection); 290 coordinator()->SetNetworkConditionsForTest(connection);
277 } 291 }
278 292
279 void SetEffectiveConnectionTypeForTest(net::EffectiveConnectionType type) { 293 void SetEffectiveConnectionTypeForTest(net::EffectiveConnectionType type) {
280 network_quality_estimator_->SetEffectiveConnectionTypeForTest(type); 294 network_quality_estimator_->SetEffectiveConnectionTypeForTest(type);
281 } 295 }
282 296
283 void ScheduleForTest() { coordinator_->ScheduleAsNeeded(); } 297 void ScheduleForTest() { coordinator_->ScheduleAsNeeded(); }
284 298
285 void CallRequestNotPicked(bool non_user_requested_tasks_remaining) { 299 void CallRequestNotPicked(bool non_user_requested_tasks_remaining,
300 bool disabled_tasks_remaining) {
301 if (disabled_tasks_remaining)
302 coordinator_->disabled_requests_.insert(kRequestId1);
303 else
304 coordinator_->disabled_requests_.clear();
305
286 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining); 306 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining);
287 } 307 }
288 308
289 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { 309 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) {
290 coordinator_->SetOfflinerTimeoutForTest(timeout); 310 coordinator_->SetOfflinerTimeoutForTest(timeout);
291 } 311 }
292 312
293 void SetDeviceConditionsForTest(DeviceConditions device_conditions) { 313 void SetDeviceConditionsForTest(DeviceConditions device_conditions) {
294 coordinator_->SetDeviceConditionsForTest(device_conditions); 314 coordinator_->SetDeviceConditionsForTest(device_conditions);
295 } 315 }
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 // Request still in the queue. 700 // Request still in the queue.
681 EXPECT_EQ(1UL, last_requests().size()); 701 EXPECT_EQ(1UL, last_requests().size());
682 // Verify prerendering cancel not counted as an attempt after all. 702 // Verify prerendering cancel not counted as an attempt after all.
683 const std::unique_ptr<SavePageRequest>& found_request = 703 const std::unique_ptr<SavePageRequest>& found_request =
684 last_requests().front(); 704 last_requests().front();
685 EXPECT_EQ(0L, found_request->completed_attempt_count()); 705 EXPECT_EQ(0L, found_request->completed_attempt_count());
686 } 706 }
687 707
688 // If one item completes, and there are no more user requeted items left, 708 // If one item completes, and there are no more user requeted items left,
689 // we should make a scheduler entry for a non-user requested item. 709 // we should make a scheduler entry for a non-user requested item.
710 TEST_F(RequestCoordinatorTest, RequestNotPickedDisabledItemsRemain) {
711 // Call start processing just to set up a scheduler callback.
712 DeviceConditions device_conditions(false, 75,
713 net::NetworkChangeNotifier::CONNECTION_3G);
714 base::Callback<void(bool)> callback = base::Bind(
715 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this));
716 coordinator()->StartProcessing(device_conditions, callback);
717 EXPECT_TRUE(is_starting());
718
719 // Call RequestNotPicked, simulating a request on the disabled list.
720 CallRequestNotPicked(false, true);
721 PumpLoop();
722
723 EXPECT_FALSE(is_starting());
724
725 // The scheduler should have been called to schedule the disabled task for
726 // 5 minutes from now.
727 SchedulerStub* scheduler_stub =
728 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler());
729 EXPECT_TRUE(scheduler_stub->backup_schedule_called());
730 EXPECT_TRUE(scheduler_stub->unschedule_called());
731 }
732
733 // If one item completes, and there are no more user requeted items left,
734 // we should make a scheduler entry for a non-user requested item.
690 TEST_F(RequestCoordinatorTest, RequestNotPickedNonUserRequestedItemsRemain) { 735 TEST_F(RequestCoordinatorTest, RequestNotPickedNonUserRequestedItemsRemain) {
691 // Call start processing just to set up a scheduler callback. 736 // Call start processing just to set up a scheduler callback.
692 DeviceConditions device_conditions(false, 75, 737 DeviceConditions device_conditions(false, 75,
693 net::NetworkChangeNotifier::CONNECTION_3G); 738 net::NetworkChangeNotifier::CONNECTION_3G);
694 base::Callback<void(bool)> callback = base::Bind( 739 base::Callback<void(bool)> callback = base::Bind(
695 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this)); 740 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this));
696 coordinator()->StartProcessing(device_conditions, callback); 741 coordinator()->StartProcessing(device_conditions, callback);
697 EXPECT_TRUE(is_starting()); 742 EXPECT_TRUE(is_starting());
698 743
699 // Call RequestNotPicked, and make sure we pick schedule a task for non user 744 // Call RequestNotPicked, and make sure we pick schedule a task for non user
700 // requested conditions. 745 // requested conditions, with no tasks on the disabled list.
701 CallRequestNotPicked(true); 746 CallRequestNotPicked(true, false);
702 PumpLoop(); 747 PumpLoop();
703 748
704 EXPECT_FALSE(is_starting()); 749 EXPECT_FALSE(is_starting());
705 750
706 // The scheduler should have been called to schedule the non-user requested 751 // The scheduler should have been called to schedule the non-user requested
707 // task. 752 // task.
708 SchedulerStub* scheduler_stub = 753 SchedulerStub* scheduler_stub =
709 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler()); 754 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler());
710 EXPECT_TRUE(scheduler_stub->schedule_called()); 755 EXPECT_TRUE(scheduler_stub->schedule_called());
711 EXPECT_TRUE(scheduler_stub->unschedule_called()); 756 EXPECT_TRUE(scheduler_stub->unschedule_called());
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 // Now whether processing triggered immediately depends on whether test 1200 // Now whether processing triggered immediately depends on whether test
1156 // is run on svelte device or not. 1201 // is run on svelte device or not.
1157 if (base::SysInfo::IsLowEndDevice()) { 1202 if (base::SysInfo::IsLowEndDevice()) {
1158 EXPECT_FALSE(is_busy()); 1203 EXPECT_FALSE(is_busy());
1159 } else { 1204 } else {
1160 EXPECT_TRUE(is_busy()); 1205 EXPECT_TRUE(is_busy());
1161 } 1206 }
1162 } 1207 }
1163 1208
1164 } // namespace offline_pages 1209 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698