| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/scheduler/renderer_scheduler_impl.h" | 5 #include "content/child/scheduler/scheduler_helper.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "cc/output/begin_frame_args.h" | |
| 9 #include "cc/test/ordered_simple_task_runner.h" | 8 #include "cc/test/ordered_simple_task_runner.h" |
| 10 #include "content/renderer/scheduler/nestable_task_runner_for_test.h" | 9 #include "content/child/scheduler/nestable_task_runner_for_test.h" |
| 11 #include "content/renderer/scheduler/renderer_scheduler_message_loop_delegate.h" | 10 #include "content/child/scheduler/scheduler_message_loop_delegate.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 14 using testing::_; |
| 15 using testing::Invoke; |
| 16 using testing::Return; |
| 17 |
| 15 namespace content { | 18 namespace content { |
| 16 | 19 |
| 17 namespace { | 20 namespace { |
| 18 class FakeInputEvent : public blink::WebInputEvent { | |
| 19 public: | |
| 20 explicit FakeInputEvent(blink::WebInputEvent::Type event_type) | |
| 21 : WebInputEvent(sizeof(FakeInputEvent)) { | |
| 22 type = event_type; | |
| 23 } | |
| 24 | |
| 25 FakeInputEvent(blink::WebInputEvent::Type event_type, int event_modifiers) | |
| 26 : WebInputEvent(sizeof(FakeInputEvent)) { | |
| 27 type = event_type; | |
| 28 modifiers = event_modifiers; | |
| 29 } | |
| 30 }; | |
| 31 | |
| 32 void AppendToVectorTestTask(std::vector<std::string>* vector, | 21 void AppendToVectorTestTask(std::vector<std::string>* vector, |
| 33 std::string value) { | 22 std::string value) { |
| 34 vector->push_back(value); | 23 vector->push_back(value); |
| 35 } | 24 } |
| 36 | 25 |
| 37 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, | 26 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, |
| 38 std::string value, | 27 std::string value, |
| 39 base::TimeTicks deadline) { | 28 base::TimeTicks deadline) { |
| 40 AppendToVectorTestTask(vector, value); | 29 AppendToVectorTestTask(vector, value); |
| 41 } | 30 } |
| 42 | 31 |
| 32 void NullTask() { |
| 33 } |
| 34 |
| 35 void AppendToVectorReentrantTask( |
| 36 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 37 std::vector<int>* vector, |
| 38 int* reentrant_count, |
| 39 int max_reentrant_count) { |
| 40 vector->push_back((*reentrant_count)++); |
| 41 if (*reentrant_count < max_reentrant_count) { |
| 42 task_runner->PostTask( |
| 43 FROM_HERE, base::Bind(AppendToVectorReentrantTask, task_runner, vector, |
| 44 reentrant_count, max_reentrant_count)); |
| 45 } |
| 46 } |
| 47 |
| 48 void NullIdleTask(base::TimeTicks) { |
| 49 } |
| 50 |
| 51 void IdleTestTask(int* run_count, |
| 52 base::TimeTicks* deadline_out, |
| 53 base::TimeTicks deadline) { |
| 54 (*run_count)++; |
| 55 *deadline_out = deadline; |
| 56 } |
| 57 |
| 58 int max_idle_task_reposts = 2; |
| 59 |
| 60 void RepostingIdleTestTask( |
| 61 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner, |
| 62 int* run_count, |
| 63 base::TimeTicks deadline) { |
| 64 if ((*run_count + 1) < max_idle_task_reposts) { |
| 65 idle_task_runner->PostIdleTask( |
| 66 FROM_HERE, |
| 67 base::Bind(&RepostingIdleTestTask, idle_task_runner, run_count)); |
| 68 } |
| 69 (*run_count)++; |
| 70 } |
| 71 |
| 72 void UpdateClockToDeadlineIdleTestTask( |
| 73 scoped_refptr<cc::TestNowSource> clock, |
| 74 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 75 int* run_count, |
| 76 base::TimeTicks deadline) { |
| 77 clock->SetNow(deadline); |
| 78 // Due to the way in which OrderedSimpleTestRunner orders tasks and the fact |
| 79 // that we updated the time within a task, the delayed pending task to call |
| 80 // EndIdlePeriod will not happen until after a TaskQueueManager DoWork, so |
| 81 // post a normal task here to ensure it runs before the next idle task. |
| 82 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); |
| 83 (*run_count)++; |
| 84 } |
| 85 |
| 86 |
| 43 }; // namespace | 87 }; // namespace |
| 44 | 88 |
| 45 class RendererSchedulerImplTest : public testing::Test { | 89 class SchedulerHelperForTest : public SchedulerHelper, |
| 90 public SchedulerHelper::SchedulerHelperDelegate { |
| 46 public: | 91 public: |
| 47 using Policy = RendererSchedulerImpl::Policy; | 92 explicit SchedulerHelperForTest( |
| 93 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner) |
| 94 : SchedulerHelper(main_task_runner, |
| 95 this, |
| 96 "test.scheduler", |
| 97 TRACE_DISABLED_BY_DEFAULT("test.scheduler"), |
| 98 TASK_QUEUE_COUNT) {} |
| 48 | 99 |
| 49 RendererSchedulerImplTest() | 100 ~SchedulerHelperForTest() override {} |
| 101 |
| 102 using SchedulerHelper::CanExceedIdleDeadlineIfRequired; |
| 103 using SchedulerHelper::EndIdlePeriod; |
| 104 using SchedulerHelper::StartIdlePeriod; |
| 105 using SchedulerHelper::InitiateLongIdlePeriod; |
| 106 |
| 107 // SchedulerHelperDelegate implementation: |
| 108 MOCK_METHOD2(CanEnterLongIdlePeriod, |
| 109 bool(base::TimeTicks now, |
| 110 base::TimeDelta* next_long_idle_period_delay_out)); |
| 111 }; |
| 112 |
| 113 class SchedulerHelperTest : public testing::Test { |
| 114 public: |
| 115 SchedulerHelperTest() |
| 50 : clock_(cc::TestNowSource::Create(5000)), | 116 : clock_(cc::TestNowSource::Create(5000)), |
| 51 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_, false)), | 117 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_, false)), |
| 52 nestable_task_runner_( | 118 nestable_task_runner_( |
| 53 NestableTaskRunnerForTest::Create(mock_task_runner_)), | 119 NestableTaskRunnerForTest::Create(mock_task_runner_)), |
| 54 scheduler_(new RendererSchedulerImpl(nestable_task_runner_)), | 120 scheduler_helper_(new SchedulerHelperForTest(nestable_task_runner_)), |
| 55 default_task_runner_(scheduler_->DefaultTaskRunner()), | 121 default_task_runner_(scheduler_helper_->DefaultTaskRunner()), |
| 56 compositor_task_runner_(scheduler_->CompositorTaskRunner()), | 122 idle_task_runner_(scheduler_helper_->IdleTaskRunner()) { |
| 57 loading_task_runner_(scheduler_->LoadingTaskRunner()), | 123 scheduler_helper_->SetTimeSourceForTesting(clock_); |
| 58 idle_task_runner_(scheduler_->IdleTaskRunner()) { | |
| 59 scheduler_->SetTimeSourceForTesting(clock_); | |
| 60 } | 124 } |
| 61 | 125 |
| 62 RendererSchedulerImplTest(base::MessageLoop* message_loop) | 126 SchedulerHelperTest(base::MessageLoop* message_loop) |
| 63 : clock_(cc::TestNowSource::Create(5000)), | 127 : clock_(cc::TestNowSource::Create(5000)), |
| 64 message_loop_(message_loop), | 128 message_loop_(message_loop), |
| 65 nestable_task_runner_( | 129 nestable_task_runner_( |
| 66 RendererSchedulerMessageLoopDelegate::Create(message_loop)), | 130 SchedulerMessageLoopDelegate::Create(message_loop)), |
| 67 scheduler_(new RendererSchedulerImpl(nestable_task_runner_)), | 131 scheduler_helper_(new SchedulerHelperForTest(nestable_task_runner_)), |
| 68 default_task_runner_(scheduler_->DefaultTaskRunner()), | 132 default_task_runner_(scheduler_helper_->DefaultTaskRunner()), |
| 69 compositor_task_runner_(scheduler_->CompositorTaskRunner()), | 133 idle_task_runner_(scheduler_helper_->IdleTaskRunner()) { |
| 70 loading_task_runner_(scheduler_->LoadingTaskRunner()), | 134 scheduler_helper_->SetTimeSourceForTesting(clock_); |
| 71 idle_task_runner_(scheduler_->IdleTaskRunner()) { | |
| 72 scheduler_->SetTimeSourceForTesting(clock_); | |
| 73 } | 135 } |
| 74 ~RendererSchedulerImplTest() override {} | 136 ~SchedulerHelperTest() override {} |
| 75 | 137 |
| 76 void TearDown() override { | 138 void TearDown() override { |
| 77 DCHECK(!mock_task_runner_.get() || !message_loop_.get()); | 139 DCHECK(!mock_task_runner_.get() || !message_loop_.get()); |
| 78 if (mock_task_runner_.get()) { | 140 if (mock_task_runner_.get()) { |
| 79 // Check that all tests stop posting tasks. | 141 // Check that all tests stop posting tasks. |
| 80 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | 142 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); |
| 81 while (mock_task_runner_->RunUntilIdle()) { | 143 while (mock_task_runner_->RunUntilIdle()) { |
| 82 } | 144 } |
| 83 } else { | 145 } else { |
| 84 message_loop_->RunUntilIdle(); | 146 message_loop_->RunUntilIdle(); |
| 85 } | 147 } |
| 86 } | 148 } |
| 87 | 149 |
| 88 void RunUntilIdle() { | 150 void RunUntilIdle() { |
| 89 // Only one of mock_task_runner_ or message_loop_ should be set. | 151 // Only one of mock_task_runner_ or message_loop_ should be set. |
| 90 DCHECK(!mock_task_runner_.get() || !message_loop_.get()); | 152 DCHECK(!mock_task_runner_.get() || !message_loop_.get()); |
| 91 if (mock_task_runner_.get()) | 153 if (mock_task_runner_.get()) |
| 92 mock_task_runner_->RunUntilIdle(); | 154 mock_task_runner_->RunUntilIdle(); |
| 93 else | 155 else |
| 94 message_loop_->RunUntilIdle(); | 156 message_loop_->RunUntilIdle(); |
| 95 } | 157 } |
| 96 | 158 |
| 97 void DoMainFrame() { | |
| 98 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | |
| 99 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | |
| 100 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | |
| 101 scheduler_->DidCommitFrameToCompositor(); | |
| 102 } | |
| 103 | |
| 104 void EnableIdleTasks() { DoMainFrame(); } | |
| 105 | |
| 106 Policy CurrentPolicy() { return scheduler_->current_policy_; } | |
| 107 | |
| 108 void EnsureUrgentPolicyUpdatePostedOnMainThread() { | |
| 109 base::AutoLock lock(scheduler_->incoming_signals_lock_); | |
| 110 scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread(FROM_HERE); | |
| 111 } | |
| 112 | |
| 113 void ScheduleDelayedPolicyUpdate(base::TimeDelta delay) { | |
| 114 scheduler_->delayed_update_policy_runner_.SetDeadline(FROM_HERE, delay, | |
| 115 clock_->Now()); | |
| 116 } | |
| 117 | |
| 118 // Helper for posting several tasks of specific types. |task_descriptor| is a | 159 // Helper for posting several tasks of specific types. |task_descriptor| is a |
| 119 // string with space delimited task identifiers. The first letter of each | 160 // string with space delimited task identifiers. The first letter of each |
| 120 // task identifier specifies the task type: | 161 // task identifier specifies the task type: |
| 121 // - 'D': Default task | 162 // - 'D': Default task |
| 122 // - 'C': Compositor task | |
| 123 // - 'L': Loading task | |
| 124 // - 'I': Idle task | 163 // - 'I': Idle task |
| 125 void PostTestTasks(std::vector<std::string>* run_order, | 164 void PostTestTasks(std::vector<std::string>* run_order, |
| 126 const std::string& task_descriptor) { | 165 const std::string& task_descriptor) { |
| 127 std::istringstream stream(task_descriptor); | 166 std::istringstream stream(task_descriptor); |
| 128 while (!stream.eof()) { | 167 while (!stream.eof()) { |
| 129 std::string task; | 168 std::string task; |
| 130 stream >> task; | 169 stream >> task; |
| 131 switch (task[0]) { | 170 switch (task[0]) { |
| 132 case 'D': | 171 case 'D': |
| 133 default_task_runner_->PostTask( | 172 default_task_runner_->PostTask( |
| 134 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task)); | 173 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task)); |
| 135 break; | 174 break; |
| 136 case 'C': | |
| 137 compositor_task_runner_->PostTask( | |
| 138 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task)); | |
| 139 break; | |
| 140 case 'L': | |
| 141 loading_task_runner_->PostTask( | |
| 142 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task)); | |
| 143 break; | |
| 144 case 'I': | 175 case 'I': |
| 145 idle_task_runner_->PostIdleTask( | 176 idle_task_runner_->PostIdleTask( |
| 146 FROM_HERE, | 177 FROM_HERE, |
| 147 base::Bind(&AppendToVectorIdleTestTask, run_order, task)); | 178 base::Bind(&AppendToVectorIdleTestTask, run_order, task)); |
| 148 break; | 179 break; |
| 149 default: | 180 default: |
| 150 NOTREACHED(); | 181 NOTREACHED(); |
| 151 } | 182 } |
| 152 } | 183 } |
| 153 } | 184 } |
| 154 | 185 |
| 155 protected: | 186 protected: |
| 156 static base::TimeDelta priority_escalation_after_input_duration() { | 187 static base::TimeDelta maximum_idle_period_duration() { |
| 157 return base::TimeDelta::FromMilliseconds( | 188 return base::TimeDelta::FromMilliseconds( |
| 158 RendererSchedulerImpl::kPriorityEscalationAfterInputMillis); | 189 SchedulerHelper::kMaximumIdlePeriodMillis); |
| 159 } | 190 } |
| 160 | 191 |
| 161 static base::TimeDelta maximum_idle_period_duration() { | 192 base::TimeTicks CurrentIdleTaskDeadlineForTesting() { |
| 162 return base::TimeDelta::FromMilliseconds( | |
| 163 RendererSchedulerImpl::kMaximumIdlePeriodMillis); | |
| 164 } | |
| 165 | |
| 166 base::TimeTicks CurrentIdleTaskDeadline() { | |
| 167 base::TimeTicks deadline; | 193 base::TimeTicks deadline; |
| 168 scheduler_->CurrentIdleTaskDeadlineCallback(&deadline); | 194 scheduler_helper_->CurrentIdleTaskDeadlineCallback(&deadline); |
| 169 return deadline; | 195 return deadline; |
| 170 } | 196 } |
| 171 | 197 |
| 172 scoped_refptr<cc::TestNowSource> clock_; | 198 scoped_refptr<cc::TestNowSource> clock_; |
| 173 // Only one of mock_task_runner_ or message_loop_ will be set. | 199 // Only one of mock_task_runner_ or message_loop_ will be set. |
| 174 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; | 200 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_; |
| 175 scoped_ptr<base::MessageLoop> message_loop_; | 201 scoped_ptr<base::MessageLoop> message_loop_; |
| 176 | 202 |
| 177 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_; | 203 scoped_refptr<NestableSingleThreadTaskRunner> nestable_task_runner_; |
| 178 scoped_ptr<RendererSchedulerImpl> scheduler_; | 204 scoped_ptr<SchedulerHelperForTest> scheduler_helper_; |
| 179 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_; | 205 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_; |
| 180 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; | |
| 181 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_; | |
| 182 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; | 206 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; |
| 183 | 207 |
| 184 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplTest); | 208 DISALLOW_COPY_AND_ASSIGN(SchedulerHelperTest); |
| 185 }; | 209 }; |
| 186 | 210 |
| 187 void NullTask() { | 211 TEST_F(SchedulerHelperTest, TestPostDefaultTask) { |
| 188 } | |
| 189 | |
| 190 void AppendToVectorReentrantTask( | |
| 191 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 192 std::vector<int>* vector, | |
| 193 int* reentrant_count, | |
| 194 int max_reentrant_count) { | |
| 195 vector->push_back((*reentrant_count)++); | |
| 196 if (*reentrant_count < max_reentrant_count) { | |
| 197 task_runner->PostTask( | |
| 198 FROM_HERE, base::Bind(AppendToVectorReentrantTask, task_runner, vector, | |
| 199 reentrant_count, max_reentrant_count)); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 void IdleTestTask(int* run_count, | |
| 204 base::TimeTicks* deadline_out, | |
| 205 base::TimeTicks deadline) { | |
| 206 (*run_count)++; | |
| 207 *deadline_out = deadline; | |
| 208 } | |
| 209 | |
| 210 void RepostingIdleTestTask( | |
| 211 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner, | |
| 212 int* run_count, | |
| 213 base::TimeTicks deadline) { | |
| 214 if (*run_count == 0) { | |
| 215 idle_task_runner->PostIdleTask( | |
| 216 FROM_HERE, | |
| 217 base::Bind(&RepostingIdleTestTask, idle_task_runner, run_count)); | |
| 218 } | |
| 219 (*run_count)++; | |
| 220 } | |
| 221 | |
| 222 void UpdateClockToDeadlineIdleTestTask( | |
| 223 scoped_refptr<cc::TestNowSource> clock, | |
| 224 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 225 int* run_count, | |
| 226 base::TimeTicks deadline) { | |
| 227 clock->SetNow(deadline); | |
| 228 // Due to the way in which OrderedSimpleTestRunner orders tasks and the fact | |
| 229 // that we updated the time within a task, the delayed pending task to call | |
| 230 // EndIdlePeriod will not happen until after a TaskQueueManager DoWork, so | |
| 231 // post a normal task here to ensure it runs before the next idle task. | |
| 232 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); | |
| 233 (*run_count)++; | |
| 234 } | |
| 235 | |
| 236 void PostingYieldingTestTask( | |
| 237 RendererSchedulerImpl* scheduler, | |
| 238 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 239 bool simulate_input, | |
| 240 bool* should_yield_before, | |
| 241 bool* should_yield_after) { | |
| 242 *should_yield_before = scheduler->ShouldYieldForHighPriorityWork(); | |
| 243 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); | |
| 244 if (simulate_input) { | |
| 245 scheduler->DidReceiveInputEventOnCompositorThread( | |
| 246 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | |
| 247 } | |
| 248 *should_yield_after = scheduler->ShouldYieldForHighPriorityWork(); | |
| 249 } | |
| 250 | |
| 251 void AnticipationTestTask(RendererSchedulerImpl* scheduler, | |
| 252 bool simulate_input, | |
| 253 bool* is_anticipated_before, | |
| 254 bool* is_anticipated_after) { | |
| 255 *is_anticipated_before = scheduler->IsHighPriorityWorkAnticipated(); | |
| 256 if (simulate_input) { | |
| 257 scheduler->DidReceiveInputEventOnCompositorThread( | |
| 258 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | |
| 259 } | |
| 260 *is_anticipated_after = scheduler->IsHighPriorityWorkAnticipated(); | |
| 261 } | |
| 262 | |
| 263 TEST_F(RendererSchedulerImplTest, TestPostDefaultTask) { | |
| 264 std::vector<std::string> run_order; | 212 std::vector<std::string> run_order; |
| 265 PostTestTasks(&run_order, "D1 D2 D3 D4"); | 213 PostTestTasks(&run_order, "D1 D2 D3 D4"); |
| 266 | 214 |
| 267 RunUntilIdle(); | 215 RunUntilIdle(); |
| 268 EXPECT_THAT(run_order, | 216 EXPECT_THAT(run_order, |
| 269 testing::ElementsAre(std::string("D1"), std::string("D2"), | 217 testing::ElementsAre(std::string("D1"), std::string("D2"), |
| 270 std::string("D3"), std::string("D4"))); | 218 std::string("D3"), std::string("D4"))); |
| 271 } | 219 } |
| 272 | 220 |
| 273 TEST_F(RendererSchedulerImplTest, TestPostDefaultAndCompositor) { | 221 TEST_F(SchedulerHelperTest, TestRentrantTask) { |
| 274 std::vector<std::string> run_order; | |
| 275 PostTestTasks(&run_order, "D1 C1"); | |
| 276 RunUntilIdle(); | |
| 277 EXPECT_THAT(run_order, testing::Contains("D1")); | |
| 278 EXPECT_THAT(run_order, testing::Contains("C1")); | |
| 279 } | |
| 280 | |
| 281 TEST_F(RendererSchedulerImplTest, TestRentrantTask) { | |
| 282 int count = 0; | 222 int count = 0; |
| 283 std::vector<int> run_order; | 223 std::vector<int> run_order; |
| 284 default_task_runner_->PostTask( | 224 default_task_runner_->PostTask( |
| 285 FROM_HERE, base::Bind(AppendToVectorReentrantTask, default_task_runner_, | 225 FROM_HERE, base::Bind(AppendToVectorReentrantTask, default_task_runner_, |
| 286 &run_order, &count, 5)); | 226 &run_order, &count, 5)); |
| 287 RunUntilIdle(); | 227 RunUntilIdle(); |
| 288 | 228 |
| 289 EXPECT_THAT(run_order, testing::ElementsAre(0, 1, 2, 3, 4)); | 229 EXPECT_THAT(run_order, testing::ElementsAre(0, 1, 2, 3, 4)); |
| 290 } | 230 } |
| 291 | 231 |
| 292 TEST_F(RendererSchedulerImplTest, TestPostIdleTask) { | 232 TEST_F(SchedulerHelperTest, TestPostIdleTask) { |
| 293 int run_count = 0; | 233 int run_count = 0; |
| 294 base::TimeTicks expected_deadline = | 234 base::TimeTicks expected_deadline = |
| 295 clock_->Now() + base::TimeDelta::FromMilliseconds(2300); | 235 clock_->Now() + base::TimeDelta::FromMilliseconds(2300); |
| 296 base::TimeTicks deadline_in_task; | 236 base::TimeTicks deadline_in_task; |
| 297 | 237 |
| 298 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); | 238 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); |
| 299 idle_task_runner_->PostIdleTask( | 239 idle_task_runner_->PostIdleTask( |
| 300 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 240 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 301 | 241 |
| 302 RunUntilIdle(); | 242 RunUntilIdle(); |
| 303 EXPECT_EQ(0, run_count); // Shouldn't run yet as no WillBeginFrame. | 243 EXPECT_EQ(0, run_count); |
| 304 | 244 |
| 305 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | 245 scheduler_helper_->StartIdlePeriod( |
| 306 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | 246 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 307 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | 247 clock_->Now(), |
| 308 RunUntilIdle(); | 248 expected_deadline); |
| 309 EXPECT_EQ(0, run_count); // Shouldn't run as no DidCommitFrameToCompositor. | |
| 310 | |
| 311 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(1200)); | |
| 312 scheduler_->DidCommitFrameToCompositor(); | |
| 313 RunUntilIdle(); | |
| 314 EXPECT_EQ(0, run_count); // We missed the deadline. | |
| 315 | |
| 316 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | |
| 317 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | |
| 318 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | |
| 319 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(800)); | |
| 320 scheduler_->DidCommitFrameToCompositor(); | |
| 321 RunUntilIdle(); | 249 RunUntilIdle(); |
| 322 EXPECT_EQ(1, run_count); | 250 EXPECT_EQ(1, run_count); |
| 323 EXPECT_EQ(expected_deadline, deadline_in_task); | 251 EXPECT_EQ(expected_deadline, deadline_in_task); |
| 324 } | 252 } |
| 325 | 253 |
| 326 TEST_F(RendererSchedulerImplTest, TestRepostingIdleTask) { | 254 TEST_F(SchedulerHelperTest, TestPostIdleTask_EndIdlePeriod) { |
| 255 int run_count = 0; |
| 256 base::TimeTicks deadline_in_task; |
| 257 |
| 258 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(100)); |
| 259 idle_task_runner_->PostIdleTask( |
| 260 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 261 |
| 262 RunUntilIdle(); |
| 263 EXPECT_EQ(0, run_count); |
| 264 |
| 265 scheduler_helper_->StartIdlePeriod( |
| 266 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 267 clock_->Now(), |
| 268 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 269 scheduler_helper_->EndIdlePeriod(); |
| 270 RunUntilIdle(); |
| 271 EXPECT_EQ(0, run_count); |
| 272 } |
| 273 |
| 274 TEST_F(SchedulerHelperTest, TestRepostingIdleTask) { |
| 327 int run_count = 0; | 275 int run_count = 0; |
| 328 | 276 |
| 277 max_idle_task_reposts = 2; |
| 329 idle_task_runner_->PostIdleTask( | 278 idle_task_runner_->PostIdleTask( |
| 330 FROM_HERE, | 279 FROM_HERE, |
| 331 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); | 280 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); |
| 332 EnableIdleTasks(); | 281 scheduler_helper_->StartIdlePeriod( |
| 282 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 283 clock_->Now(), |
| 284 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 333 RunUntilIdle(); | 285 RunUntilIdle(); |
| 334 EXPECT_EQ(1, run_count); | 286 EXPECT_EQ(1, run_count); |
| 335 | 287 |
| 336 // Reposted tasks shouldn't run until next idle period. | 288 // Reposted tasks shouldn't run until next idle period. |
| 337 RunUntilIdle(); | 289 RunUntilIdle(); |
| 338 EXPECT_EQ(1, run_count); | 290 EXPECT_EQ(1, run_count); |
| 339 | 291 |
| 340 EnableIdleTasks(); | 292 scheduler_helper_->StartIdlePeriod( |
| 293 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 294 clock_->Now(), |
| 295 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 341 RunUntilIdle(); | 296 RunUntilIdle(); |
| 342 EXPECT_EQ(2, run_count); | 297 EXPECT_EQ(2, run_count); |
| 343 } | 298 } |
| 344 | 299 |
| 345 TEST_F(RendererSchedulerImplTest, TestIdleTaskExceedsDeadline) { | 300 TEST_F(SchedulerHelperTest, TestIdleTaskExceedsDeadline) { |
| 346 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
| 347 int run_count = 0; | 301 int run_count = 0; |
| 348 | 302 |
| 349 // Post two UpdateClockToDeadlineIdleTestTask tasks. | 303 // Post two UpdateClockToDeadlineIdleTestTask tasks. |
| 350 idle_task_runner_->PostIdleTask( | 304 idle_task_runner_->PostIdleTask( |
| 351 FROM_HERE, base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, | 305 FROM_HERE, base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, |
| 352 default_task_runner_, &run_count)); | 306 default_task_runner_, &run_count)); |
| 353 idle_task_runner_->PostIdleTask( | 307 idle_task_runner_->PostIdleTask( |
| 354 FROM_HERE, base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, | 308 FROM_HERE, base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, |
| 355 default_task_runner_, &run_count)); | 309 default_task_runner_, &run_count)); |
| 356 | 310 |
| 357 EnableIdleTasks(); | 311 scheduler_helper_->StartIdlePeriod( |
| 312 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 313 clock_->Now(), |
| 314 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 358 RunUntilIdle(); | 315 RunUntilIdle(); |
| 359 // Only the first idle task should execute since it's used up the deadline. | 316 // Only the first idle task should execute since it's used up the deadline. |
| 360 EXPECT_EQ(1, run_count); | 317 EXPECT_EQ(1, run_count); |
| 361 | 318 |
| 362 EnableIdleTasks(); | 319 scheduler_helper_->EndIdlePeriod(); |
| 320 scheduler_helper_->StartIdlePeriod( |
| 321 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 322 clock_->Now(), |
| 323 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 363 RunUntilIdle(); | 324 RunUntilIdle(); |
| 364 // Second task should be run on the next idle period. | 325 // Second task should be run on the next idle period. |
| 365 EXPECT_EQ(2, run_count); | 326 EXPECT_EQ(2, run_count); |
| 366 } | 327 } |
| 367 | 328 |
| 368 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskAfterWakeup) { | 329 TEST_F(SchedulerHelperTest, TestPostIdleTaskAfterWakeup) { |
| 369 base::TimeTicks deadline_in_task; | 330 base::TimeTicks deadline_in_task; |
| 370 int run_count = 0; | 331 int run_count = 0; |
| 371 | 332 |
| 372 idle_task_runner_->PostIdleTaskAfterWakeup( | 333 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 373 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 334 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 374 | 335 |
| 375 EnableIdleTasks(); | 336 scheduler_helper_->StartIdlePeriod( |
| 337 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 338 clock_->Now(), |
| 339 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 376 RunUntilIdle(); | 340 RunUntilIdle(); |
| 377 // Shouldn't run yet as no other task woke up the scheduler. | 341 // Shouldn't run yet as no other task woke up the scheduler. |
| 378 EXPECT_EQ(0, run_count); | 342 EXPECT_EQ(0, run_count); |
| 379 | 343 |
| 380 idle_task_runner_->PostIdleTaskAfterWakeup( | 344 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 381 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 345 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 382 | 346 |
| 383 EnableIdleTasks(); | 347 scheduler_helper_->StartIdlePeriod( |
| 348 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 349 clock_->Now(), |
| 350 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 384 RunUntilIdle(); | 351 RunUntilIdle(); |
| 385 // Another after wakeup idle task shouldn't wake the scheduler. | 352 // Another after wakeup idle task shouldn't wake the scheduler. |
| 386 EXPECT_EQ(0, run_count); | 353 EXPECT_EQ(0, run_count); |
| 387 | 354 |
| 388 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | 355 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); |
| 389 | 356 |
| 390 RunUntilIdle(); | 357 RunUntilIdle(); |
| 391 EnableIdleTasks(); // Must start a new idle period before idle task runs. | 358 // Must start a new idle period before idle task runs. |
| 359 scheduler_helper_->StartIdlePeriod( |
| 360 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 361 clock_->Now(), |
| 362 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 392 RunUntilIdle(); | 363 RunUntilIdle(); |
| 393 // Execution of default task queue task should trigger execution of idle task. | 364 // Execution of default task queue task should trigger execution of idle task. |
| 394 EXPECT_EQ(2, run_count); | 365 EXPECT_EQ(2, run_count); |
| 395 } | 366 } |
| 396 | 367 |
| 397 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskAfterWakeupWhileAwake) { | 368 TEST_F(SchedulerHelperTest, TestPostIdleTaskAfterWakeupWhileAwake) { |
| 398 base::TimeTicks deadline_in_task; | 369 base::TimeTicks deadline_in_task; |
| 399 int run_count = 0; | 370 int run_count = 0; |
| 400 | 371 |
| 401 idle_task_runner_->PostIdleTaskAfterWakeup( | 372 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 402 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 373 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 403 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | 374 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); |
| 404 | 375 |
| 405 RunUntilIdle(); | 376 RunUntilIdle(); |
| 406 EnableIdleTasks(); // Must start a new idle period before idle task runs. | 377 // Must start a new idle period before idle task runs. |
| 378 scheduler_helper_->StartIdlePeriod( |
| 379 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 380 clock_->Now(), |
| 381 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 407 RunUntilIdle(); | 382 RunUntilIdle(); |
| 408 // Should run as the scheduler was already awakened by the normal task. | 383 // Should run as the scheduler was already awakened by the normal task. |
| 409 EXPECT_EQ(1, run_count); | 384 EXPECT_EQ(1, run_count); |
| 410 } | 385 } |
| 411 | 386 |
| 412 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskWakesAfterWakeupIdleTask) { | 387 TEST_F(SchedulerHelperTest, TestPostIdleTaskWakesAfterWakeupIdleTask) { |
| 413 base::TimeTicks deadline_in_task; | 388 base::TimeTicks deadline_in_task; |
| 414 int run_count = 0; | 389 int run_count = 0; |
| 415 | 390 |
| 416 idle_task_runner_->PostIdleTaskAfterWakeup( | 391 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 417 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 392 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 418 idle_task_runner_->PostIdleTask( | 393 idle_task_runner_->PostIdleTask( |
| 419 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 394 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 420 | 395 |
| 421 EnableIdleTasks(); | 396 scheduler_helper_->StartIdlePeriod( |
| 397 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 398 clock_->Now(), |
| 399 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 422 RunUntilIdle(); | 400 RunUntilIdle(); |
| 423 // Must start a new idle period before after-wakeup idle task runs. | 401 // Must start a new idle period before after-wakeup idle task runs. |
| 424 EnableIdleTasks(); | 402 scheduler_helper_->StartIdlePeriod( |
| 403 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 404 clock_->Now(), |
| 405 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 425 RunUntilIdle(); | 406 RunUntilIdle(); |
| 426 // Normal idle task should wake up after-wakeup idle task. | 407 // Normal idle task should wake up after-wakeup idle task. |
| 427 EXPECT_EQ(2, run_count); | 408 EXPECT_EQ(2, run_count); |
| 428 } | 409 } |
| 429 | 410 |
| 430 TEST_F(RendererSchedulerImplTest, TestDelayedEndIdlePeriodCanceled) { | 411 TEST_F(SchedulerHelperTest, TestDelayedEndIdlePeriod) { |
| 431 int run_count = 0; | 412 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); |
| 413 TaskQueueManager* task_queue_manager = |
| 414 scheduler_helper_->SchedulerTaskQueueManager(); |
| 432 | 415 |
| 433 base::TimeTicks deadline_in_task; | 416 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 434 idle_task_runner_->PostIdleTask( | 417 .Times(2) |
| 435 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 418 .WillRepeatedly(Return(true)); |
| 436 | 419 |
| 437 // Trigger the beginning of an idle period for 1000ms. | 420 // We need an idle task posted or InitiateLongIdlePeriod will use the |
| 438 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | 421 // control_task_after_wakeup_runner_ instead of the control_task_runner_. |
| 439 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | 422 idle_task_runner_->PostIdleTask(FROM_HERE, base::Bind(&NullIdleTask)); |
| 440 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | 423 scheduler_helper_->InitiateLongIdlePeriod(); |
| 441 DoMainFrame(); | |
| 442 | 424 |
| 443 // End the idle period early (after 500ms), and send a WillBeginFrame which | 425 // Check there is a pending delayed task. |
| 444 // specifies that the next idle period should end 1000ms from now. | 426 EXPECT_GT( |
| 445 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(500)); | 427 task_queue_manager->NextPendingDelayedTaskRunTime(), base::TimeTicks()); |
| 446 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | |
| 447 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | |
| 448 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | |
| 449 | |
| 450 RunUntilIdle(); | |
| 451 EXPECT_EQ(0, run_count); // Not currently in an idle period. | |
| 452 | |
| 453 // Trigger the start of the idle period before the task to end the previous | |
| 454 // idle period has been triggered. | |
| 455 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(400)); | |
| 456 scheduler_->DidCommitFrameToCompositor(); | |
| 457 | |
| 458 // Post a task which simulates running until after the previous end idle | |
| 459 // period delayed task was scheduled for | |
| 460 scheduler_->DefaultTaskRunner()->PostTask(FROM_HERE, base::Bind(NullTask)); | |
| 461 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(300)); | |
| 462 | |
| 463 RunUntilIdle(); | |
| 464 EXPECT_EQ(1, run_count); // We should still be in the new idle period. | |
| 465 } | |
| 466 | |
| 467 TEST_F(RendererSchedulerImplTest, TestDefaultPolicy) { | |
| 468 std::vector<std::string> run_order; | |
| 469 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2"); | |
| 470 | |
| 471 EnableIdleTasks(); | |
| 472 RunUntilIdle(); | |
| 473 EXPECT_THAT(run_order, | |
| 474 testing::ElementsAre(std::string("L1"), std::string("D1"), | |
| 475 std::string("C1"), std::string("D2"), | |
| 476 std::string("C2"), std::string("I1"))); | |
| 477 } | |
| 478 | |
| 479 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy) { | |
| 480 std::vector<std::string> run_order; | |
| 481 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2"); | |
| 482 | |
| 483 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 484 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | |
| 485 EnableIdleTasks(); | |
| 486 RunUntilIdle(); | |
| 487 EXPECT_THAT(run_order, | |
| 488 testing::ElementsAre(std::string("C1"), std::string("C2"), | |
| 489 std::string("D1"), std::string("D2"), | |
| 490 std::string("L1"), std::string("I1"))); | |
| 491 } | |
| 492 | |
| 493 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_DidAnimateForInput) { | |
| 494 std::vector<std::string> run_order; | |
| 495 PostTestTasks(&run_order, "I1 D1 C1 D2 C2"); | |
| 496 | |
| 497 scheduler_->DidAnimateForInputOnCompositorThread(); | |
| 498 EnableIdleTasks(); | |
| 499 RunUntilIdle(); | |
| 500 EXPECT_THAT(run_order, | |
| 501 testing::ElementsAre(std::string("C1"), std::string("C2"), | |
| 502 std::string("D1"), std::string("D2"), | |
| 503 std::string("I1"))); | |
| 504 } | |
| 505 | |
| 506 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicy) { | |
| 507 std::vector<std::string> run_order; | |
| 508 PostTestTasks(&run_order, "L1 D1 C1 D2 C2"); | |
| 509 | |
| 510 // Observation of touchstart should defer execution of loading tasks. | |
| 511 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 512 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
| 513 RunUntilIdle(); | |
| 514 EXPECT_THAT(run_order, | |
| 515 testing::ElementsAre(std::string("C1"), std::string("C2"), | |
| 516 std::string("D1"), std::string("D2"))); | |
| 517 | |
| 518 // Meta events like TapDown/FlingCancel shouldn't affect the priority. | |
| 519 run_order.clear(); | |
| 520 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 521 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel)); | |
| 522 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 523 FakeInputEvent(blink::WebInputEvent::GestureTapDown)); | |
| 524 RunUntilIdle(); | |
| 525 EXPECT_TRUE(run_order.empty()); | |
| 526 | |
| 527 // Action events like ScrollBegin will kick us back into compositor priority, | |
| 528 // allowing servie of the loading and idle queues. | |
| 529 run_order.clear(); | |
| 530 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 531 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin)); | |
| 532 RunUntilIdle(); | |
| 533 EXPECT_THAT(run_order, testing::ElementsAre(std::string("L1"))); | |
| 534 } | |
| 535 | |
| 536 TEST_F(RendererSchedulerImplTest, | |
| 537 DidReceiveInputEventOnCompositorThread_IgnoresMouseMove_WhenMouseUp) { | |
| 538 std::vector<std::string> run_order; | |
| 539 PostTestTasks(&run_order, "I1 D1 C1 D2 C2"); | |
| 540 | |
| 541 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 542 FakeInputEvent(blink::WebInputEvent::MouseMove)); | |
| 543 EnableIdleTasks(); | |
| 544 RunUntilIdle(); | |
| 545 // Note compositor tasks are not prioritized. | |
| 546 EXPECT_THAT(run_order, | |
| 547 testing::ElementsAre(std::string("D1"), std::string("C1"), | |
| 548 std::string("D2"), std::string("C2"), | |
| 549 std::string("I1"))); | |
| 550 } | |
| 551 | |
| 552 TEST_F(RendererSchedulerImplTest, | |
| 553 DidReceiveInputEventOnCompositorThread_MouseMove_WhenMouseDown) { | |
| 554 std::vector<std::string> run_order; | |
| 555 PostTestTasks(&run_order, "I1 D1 C1 D2 C2"); | |
| 556 | |
| 557 scheduler_->DidReceiveInputEventOnCompositorThread(FakeInputEvent( | |
| 558 blink::WebInputEvent::MouseMove, blink::WebInputEvent::LeftButtonDown)); | |
| 559 EnableIdleTasks(); | |
| 560 RunUntilIdle(); | |
| 561 // Note compositor tasks are prioritized. | |
| 562 EXPECT_THAT(run_order, | |
| 563 testing::ElementsAre(std::string("C1"), std::string("C2"), | |
| 564 std::string("D1"), std::string("D2"), | |
| 565 std::string("I1"))); | |
| 566 } | |
| 567 | |
| 568 TEST_F(RendererSchedulerImplTest, | |
| 569 DidReceiveInputEventOnCompositorThread_MouseWheel) { | |
| 570 std::vector<std::string> run_order; | |
| 571 PostTestTasks(&run_order, "I1 D1 C1 D2 C2"); | |
| 572 | |
| 573 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 574 FakeInputEvent(blink::WebInputEvent::MouseWheel)); | |
| 575 EnableIdleTasks(); | |
| 576 RunUntilIdle(); | |
| 577 // Note compositor tasks are prioritized. | |
| 578 EXPECT_THAT(run_order, | |
| 579 testing::ElementsAre(std::string("C1"), std::string("C2"), | |
| 580 std::string("D1"), std::string("D2"), | |
| 581 std::string("I1"))); | |
| 582 } | |
| 583 | |
| 584 TEST_F(RendererSchedulerImplTest, | |
| 585 DidReceiveInputEventOnCompositorThread_IgnoresKeyboardEvents) { | |
| 586 std::vector<std::string> run_order; | |
| 587 PostTestTasks(&run_order, "I1 D1 C1 D2 C2"); | |
| 588 | |
| 589 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 590 FakeInputEvent(blink::WebInputEvent::KeyDown)); | |
| 591 EnableIdleTasks(); | |
| 592 RunUntilIdle(); | |
| 593 // Note compositor tasks are not prioritized. | |
| 594 EXPECT_THAT(run_order, | |
| 595 testing::ElementsAre(std::string("D1"), std::string("C1"), | |
| 596 std::string("D2"), std::string("C2"), | |
| 597 std::string("I1"))); | |
| 598 } | |
| 599 | |
| 600 TEST_F(RendererSchedulerImplTest, | |
| 601 TestCompositorPolicyDoesNotStarveDefaultTasks) { | |
| 602 std::vector<std::string> run_order; | |
| 603 PostTestTasks(&run_order, "D1 C1"); | |
| 604 | |
| 605 for (int i = 0; i < 20; i++) { | |
| 606 compositor_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
| 607 } | |
| 608 PostTestTasks(&run_order, "C2"); | |
| 609 | |
| 610 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 611 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | |
| 612 RunUntilIdle(); | |
| 613 // Ensure that the default D1 task gets to run at some point before the final | |
| 614 // C2 compositor task. | |
| 615 EXPECT_THAT(run_order, | |
| 616 testing::ElementsAre(std::string("C1"), std::string("D1"), | |
| 617 std::string("C2"))); | |
| 618 } | |
| 619 | |
| 620 TEST_F(RendererSchedulerImplTest, TestCompositorPolicyEnds) { | |
| 621 std::vector<std::string> run_order; | |
| 622 PostTestTasks(&run_order, "D1 C1 D2 C2"); | |
| 623 | |
| 624 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 625 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | |
| 626 DoMainFrame(); | |
| 627 RunUntilIdle(); | |
| 628 EXPECT_THAT(run_order, | |
| 629 testing::ElementsAre(std::string("C1"), std::string("C2"), | |
| 630 std::string("D1"), std::string("D2"))); | |
| 631 | |
| 632 run_order.clear(); | |
| 633 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(1000)); | |
| 634 PostTestTasks(&run_order, "D1 C1 D2 C2"); | |
| 635 | |
| 636 // Compositor policy mode should have ended now that the clock has advanced. | |
| 637 RunUntilIdle(); | |
| 638 EXPECT_THAT(run_order, | |
| 639 testing::ElementsAre(std::string("D1"), std::string("C1"), | |
| 640 std::string("D2"), std::string("C2"))); | |
| 641 } | |
| 642 | |
| 643 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicyEndsAfterTimeout) { | |
| 644 std::vector<std::string> run_order; | |
| 645 PostTestTasks(&run_order, "L1 D1 C1 D2 C2"); | |
| 646 | |
| 647 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 648 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
| 649 RunUntilIdle(); | |
| 650 EXPECT_THAT(run_order, | |
| 651 testing::ElementsAre(std::string("C1"), std::string("C2"), | |
| 652 std::string("D1"), std::string("D2"))); | |
| 653 | |
| 654 run_order.clear(); | |
| 655 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(1000)); | |
| 656 | |
| 657 // Don't post any compositor tasks to simulate a very long running event | |
| 658 // handler. | |
| 659 PostTestTasks(&run_order, "D1 D2"); | |
| 660 | |
| 661 // Touchstart policy mode should have ended now that the clock has advanced. | |
| 662 RunUntilIdle(); | |
| 663 EXPECT_THAT(run_order, | |
| 664 testing::ElementsAre(std::string("L1"), std::string("D1"), | |
| 665 std::string("D2"))); | |
| 666 } | |
| 667 | |
| 668 TEST_F(RendererSchedulerImplTest, | |
| 669 TestTouchstartPolicyEndsAfterConsecutiveTouchmoves) { | |
| 670 std::vector<std::string> run_order; | |
| 671 PostTestTasks(&run_order, "L1 D1 C1 D2 C2"); | |
| 672 | |
| 673 // Observation of touchstart should defer execution of idle and loading tasks. | |
| 674 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 675 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
| 676 DoMainFrame(); | |
| 677 RunUntilIdle(); | |
| 678 EXPECT_THAT(run_order, | |
| 679 testing::ElementsAre(std::string("C1"), std::string("C2"), | |
| 680 std::string("D1"), std::string("D2"))); | |
| 681 | |
| 682 // Receiving the first touchmove will not affect scheduler priority. | |
| 683 run_order.clear(); | |
| 684 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 685 FakeInputEvent(blink::WebInputEvent::TouchMove)); | |
| 686 DoMainFrame(); | |
| 687 RunUntilIdle(); | |
| 688 EXPECT_TRUE(run_order.empty()); | |
| 689 | |
| 690 // Receiving the second touchmove will kick us back into compositor priority. | |
| 691 run_order.clear(); | |
| 692 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 693 FakeInputEvent(blink::WebInputEvent::TouchMove)); | |
| 694 RunUntilIdle(); | |
| 695 EXPECT_THAT(run_order, testing::ElementsAre(std::string("L1"))); | |
| 696 } | |
| 697 | |
| 698 TEST_F(RendererSchedulerImplTest, TestIsHighPriorityWorkAnticipated) { | |
| 699 bool is_anticipated_before = false; | |
| 700 bool is_anticipated_after = false; | |
| 701 | |
| 702 bool simulate_input = false; | |
| 703 default_task_runner_->PostTask( | |
| 704 FROM_HERE, | |
| 705 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input, | |
| 706 &is_anticipated_before, &is_anticipated_after)); | |
| 707 RunUntilIdle(); | |
| 708 // In its default state, without input receipt, the scheduler should indicate | |
| 709 // that no high-priority is anticipated. | |
| 710 EXPECT_FALSE(is_anticipated_before); | |
| 711 EXPECT_FALSE(is_anticipated_after); | |
| 712 | |
| 713 simulate_input = true; | |
| 714 default_task_runner_->PostTask( | |
| 715 FROM_HERE, | |
| 716 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input, | |
| 717 &is_anticipated_before, &is_anticipated_after)); | |
| 718 RunUntilIdle(); | |
| 719 // When input is received, the scheduler should indicate that high-priority | |
| 720 // work is anticipated. | |
| 721 EXPECT_FALSE(is_anticipated_before); | |
| 722 EXPECT_TRUE(is_anticipated_after); | |
| 723 | |
| 724 clock_->AdvanceNow(priority_escalation_after_input_duration() * 2); | |
| 725 simulate_input = false; | |
| 726 default_task_runner_->PostTask( | |
| 727 FROM_HERE, | |
| 728 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input, | |
| 729 &is_anticipated_before, &is_anticipated_after)); | |
| 730 RunUntilIdle(); | |
| 731 // Without additional input, the scheduler should indicate that high-priority | |
| 732 // work is no longer anticipated. | |
| 733 EXPECT_FALSE(is_anticipated_before); | |
| 734 EXPECT_FALSE(is_anticipated_after); | |
| 735 } | |
| 736 | |
| 737 TEST_F(RendererSchedulerImplTest, TestShouldYield) { | |
| 738 bool should_yield_before = false; | |
| 739 bool should_yield_after = false; | |
| 740 | |
| 741 default_task_runner_->PostTask( | |
| 742 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(), | |
| 743 default_task_runner_, false, &should_yield_before, | |
| 744 &should_yield_after)); | |
| 745 RunUntilIdle(); | |
| 746 // Posting to default runner shouldn't cause yielding. | |
| 747 EXPECT_FALSE(should_yield_before); | |
| 748 EXPECT_FALSE(should_yield_after); | |
| 749 | |
| 750 default_task_runner_->PostTask( | |
| 751 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(), | |
| 752 compositor_task_runner_, false, | |
| 753 &should_yield_before, &should_yield_after)); | |
| 754 RunUntilIdle(); | |
| 755 // Posting while not in compositor priority shouldn't cause yielding. | |
| 756 EXPECT_FALSE(should_yield_before); | |
| 757 EXPECT_FALSE(should_yield_after); | |
| 758 | |
| 759 default_task_runner_->PostTask( | |
| 760 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(), | |
| 761 compositor_task_runner_, true, &should_yield_before, | |
| 762 &should_yield_after)); | |
| 763 RunUntilIdle(); | |
| 764 // We should be able to switch to compositor priority mid-task. | |
| 765 EXPECT_FALSE(should_yield_before); | |
| 766 EXPECT_TRUE(should_yield_after); | |
| 767 | |
| 768 // Receiving a touchstart should immediately trigger yielding, even if | |
| 769 // there's no immediately pending work in the compositor queue. | |
| 770 EXPECT_FALSE(scheduler_->ShouldYieldForHighPriorityWork()); | |
| 771 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 772 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
| 773 EXPECT_TRUE(scheduler_->ShouldYieldForHighPriorityWork()); | |
| 774 RunUntilIdle(); | |
| 775 } | |
| 776 | |
| 777 TEST_F(RendererSchedulerImplTest, SlowInputEvent) { | |
| 778 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 779 | |
| 780 // An input event should bump us into input priority. | |
| 781 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 782 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | |
| 783 RunUntilIdle(); | |
| 784 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 785 | |
| 786 // Simulate the input event being queued for a very long time. The compositor | |
| 787 // task we post here represents the enqueued input task. | |
| 788 clock_->AdvanceNow(priority_escalation_after_input_duration() * 2); | |
| 789 compositor_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
| 790 RunUntilIdle(); | |
| 791 | |
| 792 // Even though we exceeded the input priority escalation period, we should | |
| 793 // still be in compositor priority since the input remains queued. | |
| 794 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 795 | |
| 796 // Simulate the input event triggering a composition. This should start the | |
| 797 // countdown for going back into normal policy. | |
| 798 DoMainFrame(); | |
| 799 RunUntilIdle(); | |
| 800 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 801 | |
| 802 // After the escalation period ends we should go back into normal mode. | |
| 803 clock_->AdvanceNow(priority_escalation_after_input_duration() * 2); | |
| 804 RunUntilIdle(); | |
| 805 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 806 } | |
| 807 | |
| 808 TEST_F(RendererSchedulerImplTest, SlowNoOpInputEvent) { | |
| 809 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 810 | |
| 811 // An input event should bump us into input priority. | |
| 812 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 813 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | |
| 814 RunUntilIdle(); | |
| 815 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 816 | |
| 817 // Simulate the input event being queued for a very long time. The compositor | |
| 818 // task we post here represents the enqueued input task. | |
| 819 clock_->AdvanceNow(priority_escalation_after_input_duration() * 2); | |
| 820 compositor_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
| 821 RunUntilIdle(); | |
| 822 | |
| 823 // Even though we exceeded the input priority escalation period, we should | |
| 824 // still be in compositor priority since the input remains queued. | |
| 825 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 826 | |
| 827 // If we let the compositor queue drain, we should fall out of input | |
| 828 // priority. | |
| 829 clock_->AdvanceNow(priority_escalation_after_input_duration() * 2); | |
| 830 RunUntilIdle(); | |
| 831 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 832 } | |
| 833 | |
| 834 TEST_F(RendererSchedulerImplTest, NoOpInputEvent) { | |
| 835 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 836 | |
| 837 // An input event should bump us into input priority. | |
| 838 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 839 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | |
| 840 RunUntilIdle(); | |
| 841 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 842 | |
| 843 // If nothing else happens after this, we should drop out of compositor | |
| 844 // priority after the escalation period ends and stop polling. | |
| 845 clock_->AdvanceNow(priority_escalation_after_input_duration() * 2); | |
| 846 RunUntilIdle(); | |
| 847 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 848 EXPECT_FALSE(mock_task_runner_->HasPendingTasks()); | |
| 849 } | |
| 850 | |
| 851 TEST_F(RendererSchedulerImplTest, NoOpInputEventExtendsEscalationPeriod) { | |
| 852 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 853 | |
| 854 // Simulate one handled input event. | |
| 855 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 856 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin)); | |
| 857 RunUntilIdle(); | |
| 858 DoMainFrame(); | |
| 859 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 860 | |
| 861 // Send a no-op input event in the middle of the escalation period. | |
| 862 clock_->AdvanceNow(priority_escalation_after_input_duration() / 2); | |
| 863 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 864 FakeInputEvent(blink::WebInputEvent::GestureScrollUpdate)); | |
| 865 RunUntilIdle(); | |
| 866 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 867 | |
| 868 // The escalation period should have been extended by the new input event. | |
| 869 clock_->AdvanceNow(3 * priority_escalation_after_input_duration() / 4); | |
| 870 RunUntilIdle(); | |
| 871 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 872 | |
| 873 clock_->AdvanceNow(priority_escalation_after_input_duration() / 2); | |
| 874 RunUntilIdle(); | |
| 875 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 876 } | |
| 877 | |
| 878 TEST_F(RendererSchedulerImplTest, InputArrivesAfterBeginFrame) { | |
| 879 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 880 | |
| 881 cc::BeginFrameArgs args = cc::BeginFrameArgs::Create( | |
| 882 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | |
| 883 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL); | |
| 884 clock_->AdvanceNow(priority_escalation_after_input_duration() / 2); | |
| 885 | |
| 886 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 887 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin)); | |
| 888 | |
| 889 // Simulate a BeginMainFrame task from the past. | |
| 890 clock_->AdvanceNow(2 * priority_escalation_after_input_duration()); | |
| 891 scheduler_->WillBeginFrame(args); | |
| 892 scheduler_->DidCommitFrameToCompositor(); | |
| 893 | |
| 894 // This task represents the queued-up input event. | |
| 895 compositor_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | |
| 896 | |
| 897 // Should remain in input priority policy since the input event hasn't been | |
| 898 // processed yet. | |
| 899 clock_->AdvanceNow(2 * priority_escalation_after_input_duration()); | |
| 900 RunUntilIdle(); | |
| 901 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | |
| 902 | |
| 903 // Process the input event with a new BeginMainFrame. | |
| 904 DoMainFrame(); | |
| 905 clock_->AdvanceNow(2 * priority_escalation_after_input_duration()); | |
| 906 RunUntilIdle(); | |
| 907 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | |
| 908 } | |
| 909 | |
| 910 class RendererSchedulerImplForTest : public RendererSchedulerImpl { | |
| 911 public: | |
| 912 RendererSchedulerImplForTest( | |
| 913 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner) | |
| 914 : RendererSchedulerImpl(main_task_runner), update_policy_count_(0) {} | |
| 915 | |
| 916 void UpdatePolicyLocked() override { | |
| 917 update_policy_count_++; | |
| 918 RendererSchedulerImpl::UpdatePolicyLocked(); | |
| 919 } | |
| 920 | |
| 921 int update_policy_count_; | |
| 922 }; | |
| 923 | |
| 924 TEST_F(RendererSchedulerImplTest, OnlyOnePendingUrgentPolicyUpdatey) { | |
| 925 RendererSchedulerImplForTest* mock_scheduler = | |
| 926 new RendererSchedulerImplForTest(nestable_task_runner_); | |
| 927 scheduler_.reset(mock_scheduler); | |
| 928 | |
| 929 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
| 930 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
| 931 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
| 932 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
| 933 | 428 |
| 934 RunUntilIdle(); | 429 RunUntilIdle(); |
| 935 | 430 |
| 936 EXPECT_EQ(1, mock_scheduler->update_policy_count_); | 431 // If the delayed task ran, it will an InitiateLongIdlePeriod on the control |
| 432 // task after wake up queue. |
| 433 EXPECT_FALSE(task_queue_manager->IsQueueEmpty( |
| 434 SchedulerHelper::CONTROL_TASK_AFTER_WAKEUP_QUEUE)); |
| 937 } | 435 } |
| 938 | 436 |
| 939 TEST_F(RendererSchedulerImplTest, OnePendingDelayedAndOneUrgentUpdatePolicy) { | 437 TEST_F(SchedulerHelperTest, TestDelayedEndIdlePeriodCanceled) { |
| 940 RendererSchedulerImplForTest* mock_scheduler = | |
| 941 new RendererSchedulerImplForTest(nestable_task_runner_); | |
| 942 scheduler_.reset(mock_scheduler); | |
| 943 scheduler_->SetTimeSourceForTesting(clock_); | |
| 944 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | 438 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); |
| 439 TaskQueueManager* task_queue_manager = |
| 440 scheduler_helper_->SchedulerTaskQueueManager(); |
| 945 | 441 |
| 946 ScheduleDelayedPolicyUpdate(base::TimeDelta::FromMilliseconds(1)); | 442 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 947 EnsureUrgentPolicyUpdatePostedOnMainThread(); | 443 .Times(1) |
| 444 .WillRepeatedly(Return(true)); |
| 948 | 445 |
| 446 // We need an idle task posted or InitiateLongIdlePeriod will use the |
| 447 // control_task_after_wakeup_runner_ instead of the control_task_runner_. |
| 448 idle_task_runner_->PostIdleTask(FROM_HERE, base::Bind(&NullIdleTask)); |
| 449 scheduler_helper_->InitiateLongIdlePeriod(); |
| 450 |
| 451 // Check there is a pending delayed task. |
| 452 EXPECT_GT( |
| 453 task_queue_manager->NextPendingDelayedTaskRunTime(), base::TimeTicks()); |
| 454 |
| 455 scheduler_helper_->EndIdlePeriod(); |
| 949 RunUntilIdle(); | 456 RunUntilIdle(); |
| 950 | 457 |
| 951 // We expect both the urgent and the delayed updates to run. | 458 // If the delayed task didn't run, there will be nothing on the control task |
| 952 EXPECT_EQ(2, mock_scheduler->update_policy_count_); | 459 // after wake up queue. |
| 460 EXPECT_TRUE(task_queue_manager->IsQueueEmpty( |
| 461 SchedulerHelper::CONTROL_TASK_AFTER_WAKEUP_QUEUE)); |
| 953 } | 462 } |
| 954 | 463 |
| 955 TEST_F(RendererSchedulerImplTest, OneUrgentAndOnePendingDelayedUpdatePolicy) { | 464 class SchedulerHelperWithMessageLoopTest : public SchedulerHelperTest { |
| 956 RendererSchedulerImplForTest* mock_scheduler = | |
| 957 new RendererSchedulerImplForTest(nestable_task_runner_); | |
| 958 scheduler_.reset(mock_scheduler); | |
| 959 scheduler_->SetTimeSourceForTesting(clock_); | |
| 960 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
| 961 | |
| 962 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
| 963 ScheduleDelayedPolicyUpdate(base::TimeDelta::FromMilliseconds(1)); | |
| 964 | |
| 965 RunUntilIdle(); | |
| 966 | |
| 967 // We expect both the urgent and the delayed updates to run. | |
| 968 EXPECT_EQ(2, mock_scheduler->update_policy_count_); | |
| 969 } | |
| 970 | |
| 971 TEST_F(RendererSchedulerImplTest, UpdatePolicyCountTriggeredByOneInputEvent) { | |
| 972 RendererSchedulerImplForTest* mock_scheduler = | |
| 973 new RendererSchedulerImplForTest(nestable_task_runner_); | |
| 974 scheduler_.reset(mock_scheduler); | |
| 975 scheduler_->SetTimeSourceForTesting(clock_); | |
| 976 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
| 977 | |
| 978 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 979 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
| 980 | |
| 981 RunUntilIdle(); | |
| 982 | |
| 983 // We expect an urgent policy update followed by a delayed one 100ms later. | |
| 984 EXPECT_EQ(2, mock_scheduler->update_policy_count_); | |
| 985 } | |
| 986 | |
| 987 TEST_F(RendererSchedulerImplTest, UpdatePolicyCountTriggeredByTwoInputEvents) { | |
| 988 RendererSchedulerImplForTest* mock_scheduler = | |
| 989 new RendererSchedulerImplForTest(nestable_task_runner_); | |
| 990 scheduler_.reset(mock_scheduler); | |
| 991 scheduler_->SetTimeSourceForTesting(clock_); | |
| 992 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
| 993 | |
| 994 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 995 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
| 996 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 997 FakeInputEvent(blink::WebInputEvent::TouchMove)); | |
| 998 | |
| 999 RunUntilIdle(); | |
| 1000 | |
| 1001 // We expect an urgent policy update followed by a delayed one 100ms later. | |
| 1002 EXPECT_EQ(2, mock_scheduler->update_policy_count_); | |
| 1003 } | |
| 1004 | |
| 1005 TEST_F(RendererSchedulerImplTest, EnsureUpdatePolicyNotTriggeredTooOften) { | |
| 1006 RendererSchedulerImplForTest* mock_scheduler = | |
| 1007 new RendererSchedulerImplForTest(nestable_task_runner_); | |
| 1008 scheduler_.reset(mock_scheduler); | |
| 1009 scheduler_->SetTimeSourceForTesting(clock_); | |
| 1010 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
| 1011 | |
| 1012 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 1013 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
| 1014 scheduler_->DidReceiveInputEventOnCompositorThread( | |
| 1015 FakeInputEvent(blink::WebInputEvent::TouchMove)); | |
| 1016 | |
| 1017 // We expect the first call to IsHighPriorityWorkAnticipated to be called | |
| 1018 // after recieving an input event (but before the UpdateTask was processed) to | |
| 1019 // call UpdatePolicy. | |
| 1020 EXPECT_EQ(0, mock_scheduler->update_policy_count_); | |
| 1021 scheduler_->IsHighPriorityWorkAnticipated(); | |
| 1022 EXPECT_EQ(1, mock_scheduler->update_policy_count_); | |
| 1023 // Subsequent calls should not call UpdatePolicy. | |
| 1024 scheduler_->IsHighPriorityWorkAnticipated(); | |
| 1025 scheduler_->IsHighPriorityWorkAnticipated(); | |
| 1026 scheduler_->IsHighPriorityWorkAnticipated(); | |
| 1027 scheduler_->ShouldYieldForHighPriorityWork(); | |
| 1028 scheduler_->ShouldYieldForHighPriorityWork(); | |
| 1029 scheduler_->ShouldYieldForHighPriorityWork(); | |
| 1030 scheduler_->ShouldYieldForHighPriorityWork(); | |
| 1031 | |
| 1032 EXPECT_EQ(1, mock_scheduler->update_policy_count_); | |
| 1033 | |
| 1034 RunUntilIdle(); | |
| 1035 // We expect both the urgent and the delayed updates to run in addition to the | |
| 1036 // earlier updated cause by IsHighPriorityWorkAnticipated. | |
| 1037 EXPECT_EQ(3, mock_scheduler->update_policy_count_); | |
| 1038 } | |
| 1039 | |
| 1040 class RendererSchedulerImplWithMessageLoopTest | |
| 1041 : public RendererSchedulerImplTest { | |
| 1042 public: | 465 public: |
| 1043 RendererSchedulerImplWithMessageLoopTest() | 466 SchedulerHelperWithMessageLoopTest() |
| 1044 : RendererSchedulerImplTest(new base::MessageLoop()) {} | 467 : SchedulerHelperTest(new base::MessageLoop()) {} |
| 1045 ~RendererSchedulerImplWithMessageLoopTest() override {} | 468 ~SchedulerHelperWithMessageLoopTest() override {} |
| 1046 | 469 |
| 1047 void PostFromNestedRunloop(std::vector< | 470 void PostFromNestedRunloop(std::vector< |
| 1048 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) { | 471 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) { |
| 1049 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get()); | 472 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get()); |
| 1050 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) { | 473 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) { |
| 1051 if (pair.second) { | 474 if (pair.second) { |
| 1052 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first); | 475 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first); |
| 1053 } else { | 476 } else { |
| 1054 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first); | 477 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first); |
| 1055 } | 478 } |
| 1056 } | 479 } |
| 1057 EnableIdleTasks(); | 480 scheduler_helper_->StartIdlePeriod( |
| 481 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 482 clock_->Now(), |
| 483 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 1058 message_loop_->RunUntilIdle(); | 484 message_loop_->RunUntilIdle(); |
| 1059 } | 485 } |
| 1060 | 486 |
| 1061 private: | 487 private: |
| 1062 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplWithMessageLoopTest); | 488 DISALLOW_COPY_AND_ASSIGN(SchedulerHelperWithMessageLoopTest); |
| 1063 }; | 489 }; |
| 1064 | 490 |
| 1065 TEST_F(RendererSchedulerImplWithMessageLoopTest, | 491 TEST_F(SchedulerHelperWithMessageLoopTest, |
| 1066 NonNestableIdleTaskDoesntExecuteInNestedLoop) { | 492 NonNestableIdleTaskDoesntExecuteInNestedLoop) { |
| 1067 std::vector<std::string> order; | 493 std::vector<std::string> order; |
| 1068 idle_task_runner_->PostIdleTask( | 494 idle_task_runner_->PostIdleTask( |
| 1069 FROM_HERE, | 495 FROM_HERE, |
| 1070 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("1"))); | 496 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("1"))); |
| 1071 idle_task_runner_->PostIdleTask( | 497 idle_task_runner_->PostIdleTask( |
| 1072 FROM_HERE, | 498 FROM_HERE, |
| 1073 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("2"))); | 499 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("2"))); |
| 1074 | 500 |
| 1075 std::vector<std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>> | 501 std::vector<std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>> |
| 1076 tasks_to_post_from_nested_loop; | 502 tasks_to_post_from_nested_loop; |
| 1077 tasks_to_post_from_nested_loop.push_back(std::make_pair( | 503 tasks_to_post_from_nested_loop.push_back(std::make_pair( |
| 1078 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("3")), | 504 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("3")), |
| 1079 false)); | 505 false)); |
| 1080 tasks_to_post_from_nested_loop.push_back(std::make_pair( | 506 tasks_to_post_from_nested_loop.push_back(std::make_pair( |
| 1081 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("4")), true)); | 507 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("4")), true)); |
| 1082 tasks_to_post_from_nested_loop.push_back(std::make_pair( | 508 tasks_to_post_from_nested_loop.push_back(std::make_pair( |
| 1083 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true)); | 509 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true)); |
| 1084 | 510 |
| 1085 default_task_runner_->PostTask( | 511 default_task_runner_->PostTask( |
| 1086 FROM_HERE, | 512 FROM_HERE, |
| 1087 base::Bind( | 513 base::Bind(&SchedulerHelperWithMessageLoopTest::PostFromNestedRunloop, |
| 1088 &RendererSchedulerImplWithMessageLoopTest::PostFromNestedRunloop, | 514 base::Unretained(this), |
| 1089 base::Unretained(this), | 515 base::Unretained(&tasks_to_post_from_nested_loop))); |
| 1090 base::Unretained(&tasks_to_post_from_nested_loop))); | |
| 1091 | 516 |
| 1092 EnableIdleTasks(); | 517 scheduler_helper_->StartIdlePeriod( |
| 518 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 519 clock_->Now(), |
| 520 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 1093 RunUntilIdle(); | 521 RunUntilIdle(); |
| 1094 // Note we expect task 3 to run last because it's non-nestable. | 522 // Note we expect task 3 to run last because it's non-nestable. |
| 1095 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"), | 523 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"), |
| 1096 std::string("4"), std::string("5"), | 524 std::string("4"), std::string("5"), |
| 1097 std::string("3"))); | 525 std::string("3"))); |
| 1098 } | 526 } |
| 1099 | 527 |
| 1100 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriod) { | 528 TEST_F(SchedulerHelperTest, TestLongIdlePeriod) { |
| 1101 base::TimeTicks expected_deadline = | 529 base::TimeTicks expected_deadline = |
| 1102 clock_->Now() + maximum_idle_period_duration(); | 530 clock_->Now() + maximum_idle_period_duration(); |
| 1103 base::TimeTicks deadline_in_task; | 531 base::TimeTicks deadline_in_task; |
| 1104 int run_count = 0; | 532 int run_count = 0; |
| 1105 | 533 |
| 1106 idle_task_runner_->PostIdleTask( | 534 idle_task_runner_->PostIdleTask( |
| 1107 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 535 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 1108 | 536 |
| 537 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 538 .Times(2) |
| 539 .WillRepeatedly(Return(true)); |
| 540 |
| 1109 RunUntilIdle(); | 541 RunUntilIdle(); |
| 1110 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period. | 542 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period. |
| 1111 | 543 |
| 1112 scheduler_->BeginFrameNotExpectedSoon(); | 544 scheduler_helper_->InitiateLongIdlePeriod(); |
| 1113 RunUntilIdle(); | 545 RunUntilIdle(); |
| 1114 EXPECT_EQ(1, run_count); // Should have run in a long idle time. | 546 EXPECT_EQ(1, run_count); // Should have run in a long idle time. |
| 1115 EXPECT_EQ(expected_deadline, deadline_in_task); | 547 EXPECT_EQ(expected_deadline, deadline_in_task); |
| 1116 } | 548 } |
| 1117 | 549 |
| 1118 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodWithPendingDelayedTask) { | 550 TEST_F(SchedulerHelperTest, TestLongIdlePeriodWithPendingDelayedTask) { |
| 1119 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30); | 551 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30); |
| 1120 base::TimeTicks expected_deadline = clock_->Now() + pending_task_delay; | 552 base::TimeTicks expected_deadline = clock_->Now() + pending_task_delay; |
| 1121 base::TimeTicks deadline_in_task; | 553 base::TimeTicks deadline_in_task; |
| 1122 int run_count = 0; | 554 int run_count = 0; |
| 1123 | 555 |
| 556 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 557 .Times(2) |
| 558 .WillRepeatedly(Return(true)); |
| 559 |
| 1124 idle_task_runner_->PostIdleTask( | 560 idle_task_runner_->PostIdleTask( |
| 1125 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 561 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 1126 default_task_runner_->PostDelayedTask( | 562 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 1127 FROM_HERE, base::Bind(&NullTask), pending_task_delay); | 563 pending_task_delay); |
| 1128 | 564 |
| 1129 scheduler_->BeginFrameNotExpectedSoon(); | 565 scheduler_helper_->InitiateLongIdlePeriod(); |
| 1130 RunUntilIdle(); | 566 RunUntilIdle(); |
| 1131 EXPECT_EQ(1, run_count); // Should have run in a long idle time. | 567 EXPECT_EQ(1, run_count); // Should have run in a long idle time. |
| 1132 EXPECT_EQ(expected_deadline, deadline_in_task); | 568 EXPECT_EQ(expected_deadline, deadline_in_task); |
| 1133 } | 569 } |
| 1134 | 570 |
| 1135 TEST_F(RendererSchedulerImplTest, | 571 TEST_F(SchedulerHelperTest, TestLongIdlePeriodWithLatePendingDelayedTask) { |
| 1136 TestLongIdlePeriodWithLatePendingDelayedTask) { | |
| 1137 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10); | 572 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10); |
| 1138 base::TimeTicks deadline_in_task; | 573 base::TimeTicks deadline_in_task; |
| 1139 int run_count = 0; | 574 int run_count = 0; |
| 1140 | 575 |
| 1141 default_task_runner_->PostDelayedTask( | 576 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 1142 FROM_HERE, base::Bind(&NullTask), pending_task_delay); | 577 .Times(3) |
| 578 .WillRepeatedly(Return(true)); |
| 579 |
| 580 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 581 pending_task_delay); |
| 1143 | 582 |
| 1144 // Advance clock until after delayed task was meant to be run. | 583 // Advance clock until after delayed task was meant to be run. |
| 1145 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(20)); | 584 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(20)); |
| 1146 | 585 |
| 1147 // Post an idle task and BeginFrameNotExpectedSoon to initiate a long idle | 586 // Post an idle task and then InitiateLongIdlePeriod. Since there is a late |
| 1148 // period. Since there is a late pending delayed task this shouldn't actually | 587 // pending delayed task this shouldn't actually start an idle period. |
| 1149 // start an idle period. | |
| 1150 idle_task_runner_->PostIdleTask( | 588 idle_task_runner_->PostIdleTask( |
| 1151 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 589 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 1152 scheduler_->BeginFrameNotExpectedSoon(); | 590 scheduler_helper_->InitiateLongIdlePeriod(); |
| 1153 RunUntilIdle(); | 591 RunUntilIdle(); |
| 1154 EXPECT_EQ(0, run_count); | 592 EXPECT_EQ(0, run_count); |
| 1155 | 593 |
| 1156 // After the delayed task has been run we should trigger an idle period. | 594 // After the delayed task has been run we should trigger an idle period. |
| 1157 clock_->AdvanceNow(maximum_idle_period_duration()); | 595 clock_->AdvanceNow(maximum_idle_period_duration()); |
| 1158 RunUntilIdle(); | 596 RunUntilIdle(); |
| 1159 EXPECT_EQ(1, run_count); | 597 EXPECT_EQ(1, run_count); |
| 1160 } | 598 } |
| 1161 | 599 |
| 1162 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodRepeating) { | 600 TEST_F(SchedulerHelperTest, TestLongIdlePeriodRepeating) { |
| 1163 int run_count = 0; | 601 int run_count = 0; |
| 1164 | 602 |
| 603 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 604 .Times(2) |
| 605 .WillRepeatedly(Return(true)); |
| 606 |
| 607 max_idle_task_reposts = 3; |
| 1165 idle_task_runner_->PostIdleTask( | 608 idle_task_runner_->PostIdleTask( |
| 1166 FROM_HERE, | 609 FROM_HERE, |
| 1167 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); | 610 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); |
| 1168 | 611 |
| 1169 scheduler_->BeginFrameNotExpectedSoon(); | 612 scheduler_helper_->InitiateLongIdlePeriod(); |
| 1170 RunUntilIdle(); | 613 RunUntilIdle(); |
| 1171 EXPECT_EQ(1, run_count); // Should only run once per idle period. | 614 EXPECT_EQ(1, run_count); // Should only run once per idle period. |
| 1172 | 615 |
| 1173 // Advance time to start of next long idle period and check task reposted task | 616 // Advance time to start of next long idle period and check task reposted task |
| 1174 // gets run. | 617 // gets run. |
| 1175 clock_->AdvanceNow(maximum_idle_period_duration()); | 618 clock_->AdvanceNow(maximum_idle_period_duration()); |
| 1176 RunUntilIdle(); | 619 RunUntilIdle(); |
| 1177 EXPECT_EQ(2, run_count); | 620 EXPECT_EQ(2, run_count); |
| 1178 | 621 |
| 1179 // Advance time to start of next long idle period then end idle period with a | 622 // Advance time to start of next long idle period then end the idle period and |
| 1180 // new BeginMainFrame and check idle task doesn't run. | 623 // check the task doesn't get run. |
| 1181 clock_->AdvanceNow(maximum_idle_period_duration()); | 624 clock_->AdvanceNow(maximum_idle_period_duration()); |
| 1182 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | 625 scheduler_helper_->EndIdlePeriod(); |
| 1183 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | |
| 1184 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | |
| 1185 RunUntilIdle(); | 626 RunUntilIdle(); |
| 1186 EXPECT_EQ(2, run_count); | 627 EXPECT_EQ(2, run_count); |
| 1187 } | 628 } |
| 1188 | 629 |
| 1189 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodDoesNotWakeScheduler) { | 630 TEST_F(SchedulerHelperTest, TestLongIdlePeriodDoesNotWakeScheduler) { |
| 1190 base::TimeTicks deadline_in_task; | 631 base::TimeTicks deadline_in_task; |
| 1191 int run_count = 0; | 632 int run_count = 0; |
| 1192 | 633 |
| 634 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 635 .Times(3) |
| 636 .WillRepeatedly(Return(true)); |
| 637 |
| 1193 // Start a long idle period and get the time it should end. | 638 // Start a long idle period and get the time it should end. |
| 1194 scheduler_->BeginFrameNotExpectedSoon(); | 639 scheduler_helper_->InitiateLongIdlePeriod(); |
| 1195 // The scheduler should not run the initiate_next_long_idle_period task if | 640 // The scheduler should not run the initiate_next_long_idle_period task if |
| 1196 // there are no idle tasks and no other task woke up the scheduler, thus | 641 // there are no idle tasks and no other task woke up the scheduler, thus |
| 1197 // the idle period deadline shouldn't update at the end of the current long | 642 // the idle period deadline shouldn't update at the end of the current long |
| 1198 // idle period. | 643 // idle period. |
| 1199 base::TimeTicks idle_period_deadline = CurrentIdleTaskDeadline(); | 644 base::TimeTicks idle_period_deadline = CurrentIdleTaskDeadlineForTesting(); |
| 1200 clock_->AdvanceNow(maximum_idle_period_duration()); | 645 clock_->AdvanceNow(maximum_idle_period_duration()); |
| 1201 RunUntilIdle(); | 646 RunUntilIdle(); |
| 1202 | 647 |
| 1203 base::TimeTicks new_idle_period_deadline = CurrentIdleTaskDeadline(); | 648 base::TimeTicks new_idle_period_deadline = |
| 649 CurrentIdleTaskDeadlineForTesting(); |
| 1204 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | 650 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); |
| 1205 | 651 |
| 1206 // Posting a after-wakeup idle task also shouldn't wake the scheduler or | 652 // Posting a after-wakeup idle task also shouldn't wake the scheduler or |
| 1207 // initiate the next long idle period. | 653 // initiate the next long idle period. |
| 1208 idle_task_runner_->PostIdleTaskAfterWakeup( | 654 idle_task_runner_->PostIdleTaskAfterWakeup( |
| 1209 FROM_HERE, | 655 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 1210 base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 1211 RunUntilIdle(); | 656 RunUntilIdle(); |
| 1212 new_idle_period_deadline = CurrentIdleTaskDeadline(); | 657 new_idle_period_deadline = CurrentIdleTaskDeadlineForTesting(); |
| 1213 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | 658 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); |
| 1214 EXPECT_EQ(0, run_count); | 659 EXPECT_EQ(0, run_count); |
| 1215 | 660 |
| 1216 // Running a normal task should initiate a new long idle period though. | 661 // Running a normal task should initiate a new long idle period though. |
| 1217 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | 662 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); |
| 1218 RunUntilIdle(); | 663 RunUntilIdle(); |
| 1219 new_idle_period_deadline = CurrentIdleTaskDeadline(); | 664 new_idle_period_deadline = CurrentIdleTaskDeadlineForTesting(); |
| 1220 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(), | 665 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(), |
| 1221 new_idle_period_deadline); | 666 new_idle_period_deadline); |
| 1222 | 667 |
| 1223 EXPECT_EQ(1, run_count); | 668 EXPECT_EQ(1, run_count); |
| 1224 } | 669 } |
| 1225 | 670 |
| 1226 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodInTouchStartPolicy) { | 671 TEST_F(SchedulerHelperTest, TestLongIdlePeriodWhenNotCanEnterLongIdlePeriod) { |
| 672 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(1000); |
| 673 base::TimeDelta halfDelay = base::TimeDelta::FromMilliseconds(500); |
| 674 base::TimeTicks delayOver = clock_->Now() + delay; |
| 1227 base::TimeTicks deadline_in_task; | 675 base::TimeTicks deadline_in_task; |
| 1228 int run_count = 0; | 676 int run_count = 0; |
| 1229 | 677 |
| 678 ON_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 679 .WillByDefault(Invoke([&delay, &delayOver]( |
| 680 base::TimeTicks now, |
| 681 base::TimeDelta* next_long_idle_period_delay_out) { |
| 682 if (now >= delayOver) |
| 683 return true; |
| 684 *next_long_idle_period_delay_out = delay; |
| 685 return false; |
| 686 })); |
| 687 |
| 688 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)).Times(3); |
| 689 |
| 1230 idle_task_runner_->PostIdleTask( | 690 idle_task_runner_->PostIdleTask( |
| 1231 FROM_HERE, | 691 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
| 1232 base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
| 1233 | 692 |
| 1234 // Observation of touchstart should defer the start of the long idle period. | 693 // Make sure Idle tasks don't run until the delay has occured. |
| 1235 scheduler_->DidReceiveInputEventOnCompositorThread( | 694 scheduler_helper_->InitiateLongIdlePeriod(); |
| 1236 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
| 1237 scheduler_->BeginFrameNotExpectedSoon(); | |
| 1238 RunUntilIdle(); | 695 RunUntilIdle(); |
| 1239 EXPECT_EQ(0, run_count); | 696 EXPECT_EQ(0, run_count); |
| 1240 | 697 |
| 1241 // The long idle period should start after the touchstart policy has finished. | 698 clock_->AdvanceNow(halfDelay); |
| 1242 clock_->AdvanceNow(priority_escalation_after_input_duration()); | 699 RunUntilIdle(); |
| 700 EXPECT_EQ(0, run_count); |
| 701 |
| 702 // Delay is finished, idle task should run. |
| 703 clock_->AdvanceNow(halfDelay); |
| 1243 RunUntilIdle(); | 704 RunUntilIdle(); |
| 1244 EXPECT_EQ(1, run_count); | 705 EXPECT_EQ(1, run_count); |
| 1245 } | 706 } |
| 1246 | 707 |
| 1247 void TestCanExceedIdleDeadlineIfRequiredTask(RendererScheduler* scheduler, | 708 void TestCanExceedIdleDeadlineIfRequiredTask(SchedulerHelperForTest* scheduler, |
| 1248 bool* can_exceed_idle_deadline_out, | 709 bool* can_exceed_idle_deadline_out, |
| 1249 int* run_count, | 710 int* run_count, |
| 1250 base::TimeTicks deadline) { | 711 base::TimeTicks deadline) { |
| 1251 *can_exceed_idle_deadline_out = scheduler->CanExceedIdleDeadlineIfRequired(); | 712 *can_exceed_idle_deadline_out = scheduler->CanExceedIdleDeadlineIfRequired(); |
| 1252 (*run_count)++; | 713 (*run_count)++; |
| 1253 } | 714 } |
| 1254 | 715 |
| 1255 TEST_F(RendererSchedulerImplTest, CanExceedIdleDeadlineIfRequired) { | 716 TEST_F(SchedulerHelperTest, CanExceedIdleDeadlineIfRequired) { |
| 1256 int run_count = 0; | 717 int run_count = 0; |
| 1257 bool can_exceed_idle_deadline = false; | 718 bool can_exceed_idle_deadline = false; |
| 1258 | 719 |
| 720 EXPECT_CALL(*scheduler_helper_, CanEnterLongIdlePeriod(_, _)) |
| 721 .Times(3) |
| 722 .WillRepeatedly(Return(true)); |
| 723 |
| 1259 // Should return false if not in an idle period. | 724 // Should return false if not in an idle period. |
| 1260 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired()); | 725 EXPECT_FALSE(scheduler_helper_->CanExceedIdleDeadlineIfRequired()); |
| 1261 | 726 |
| 1262 // Should return false for short idle periods. | 727 // Should return false for short idle periods. |
| 1263 idle_task_runner_->PostIdleTask( | 728 idle_task_runner_->PostIdleTask( |
| 1264 FROM_HERE, | 729 FROM_HERE, base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, |
| 1265 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(), | 730 scheduler_helper_.get(), &can_exceed_idle_deadline, |
| 1266 &can_exceed_idle_deadline, &run_count)); | 731 &run_count)); |
| 1267 EnableIdleTasks(); | 732 scheduler_helper_->StartIdlePeriod( |
| 733 SchedulerHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, |
| 734 clock_->Now(), |
| 735 clock_->Now() + base::TimeDelta::FromMilliseconds(10)); |
| 1268 RunUntilIdle(); | 736 RunUntilIdle(); |
| 1269 EXPECT_EQ(1, run_count); | 737 EXPECT_EQ(1, run_count); |
| 1270 EXPECT_FALSE(can_exceed_idle_deadline); | 738 EXPECT_FALSE(can_exceed_idle_deadline); |
| 1271 | 739 |
| 1272 // Should return false for a long idle period which is shortened due to a | 740 // Should return false for a long idle period which is shortened due to a |
| 1273 // pending delayed task. | 741 // pending delayed task. |
| 1274 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), | 742 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
| 1275 base::TimeDelta::FromMilliseconds(10)); | 743 base::TimeDelta::FromMilliseconds(10)); |
| 1276 idle_task_runner_->PostIdleTask( | 744 idle_task_runner_->PostIdleTask( |
| 1277 FROM_HERE, | 745 FROM_HERE, base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, |
| 1278 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(), | 746 scheduler_helper_.get(), &can_exceed_idle_deadline, |
| 1279 &can_exceed_idle_deadline, &run_count)); | 747 &run_count)); |
| 1280 scheduler_->BeginFrameNotExpectedSoon(); | 748 scheduler_helper_->InitiateLongIdlePeriod(); |
| 1281 RunUntilIdle(); | 749 RunUntilIdle(); |
| 1282 EXPECT_EQ(2, run_count); | 750 EXPECT_EQ(2, run_count); |
| 1283 EXPECT_FALSE(can_exceed_idle_deadline); | 751 EXPECT_FALSE(can_exceed_idle_deadline); |
| 1284 | 752 |
| 1285 // Next long idle period will be for the maximum time, so | 753 // Next long idle period will be for the maximum time, so |
| 1286 // CanExceedIdleDeadlineIfRequired should return true. | 754 // CanExceedIdleDeadlineIfRequired should return true. |
| 1287 clock_->AdvanceNow(maximum_idle_period_duration()); | 755 clock_->AdvanceNow(maximum_idle_period_duration()); |
| 1288 idle_task_runner_->PostIdleTask( | 756 idle_task_runner_->PostIdleTask( |
| 1289 FROM_HERE, | 757 FROM_HERE, base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, |
| 1290 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(), | 758 scheduler_helper_.get(), &can_exceed_idle_deadline, |
| 1291 &can_exceed_idle_deadline, &run_count)); | 759 &run_count)); |
| 1292 RunUntilIdle(); | 760 RunUntilIdle(); |
| 1293 EXPECT_EQ(3, run_count); | 761 EXPECT_EQ(3, run_count); |
| 1294 EXPECT_TRUE(can_exceed_idle_deadline); | 762 EXPECT_TRUE(can_exceed_idle_deadline); |
| 763 } |
| 1295 | 764 |
| 1296 // Next long idle period will be for the maximum time, so | 765 TEST_F(SchedulerHelperTest, IsShutdown) { |
| 1297 // CanExceedIdleDeadlineIfRequired should return true. | 766 EXPECT_FALSE(scheduler_helper_->IsShutdown()); |
| 1298 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | 767 |
| 1299 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | 768 scheduler_helper_->Shutdown(); |
| 1300 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | 769 EXPECT_TRUE(scheduler_helper_->IsShutdown()); |
| 1301 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired()); | |
| 1302 } | 770 } |
| 1303 | 771 |
| 1304 } // namespace content | 772 } // namespace content |
| OLD | NEW |