Infobar material design refresh: bg color
[chromium-blink-merge.git] / components / scheduler / renderer / renderer_scheduler_impl_unittest.cc
blobc651486d18c9315d3783c1992daac11d200eeaa6
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
3 // found in the LICENSE file.
5 #include "components/scheduler/renderer/renderer_scheduler_impl.h"
7 #include "base/callback.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "cc/output/begin_frame_args.h"
10 #include "cc/test/ordered_simple_task_runner.h"
11 #include "components/scheduler/child/scheduler_task_runner_delegate_for_test.h"
12 #include "components/scheduler/child/scheduler_task_runner_delegate_impl.h"
13 #include "components/scheduler/child/test_time_source.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace scheduler {
19 namespace {
20 class FakeInputEvent : public blink::WebInputEvent {
21 public:
22 explicit FakeInputEvent(blink::WebInputEvent::Type event_type)
23 : WebInputEvent(sizeof(FakeInputEvent)) {
24 type = event_type;
27 FakeInputEvent(blink::WebInputEvent::Type event_type, int event_modifiers)
28 : WebInputEvent(sizeof(FakeInputEvent)) {
29 type = event_type;
30 modifiers = event_modifiers;
34 void AppendToVectorTestTask(std::vector<std::string>* vector,
35 std::string value) {
36 vector->push_back(value);
39 void AppendToVectorIdleTestTask(std::vector<std::string>* vector,
40 std::string value,
41 base::TimeTicks deadline) {
42 AppendToVectorTestTask(vector, value);
45 void NullTask() {
48 void AppendToVectorReentrantTask(base::SingleThreadTaskRunner* task_runner,
49 std::vector<int>* vector,
50 int* reentrant_count,
51 int max_reentrant_count) {
52 vector->push_back((*reentrant_count)++);
53 if (*reentrant_count < max_reentrant_count) {
54 task_runner->PostTask(
55 FROM_HERE,
56 base::Bind(AppendToVectorReentrantTask, base::Unretained(task_runner),
57 vector, reentrant_count, max_reentrant_count));
61 void IdleTestTask(int* run_count,
62 base::TimeTicks* deadline_out,
63 base::TimeTicks deadline) {
64 (*run_count)++;
65 *deadline_out = deadline;
68 int max_idle_task_reposts = 2;
70 void RepostingIdleTestTask(SingleThreadIdleTaskRunner* idle_task_runner,
71 int* run_count,
72 base::TimeTicks deadline) {
73 if ((*run_count + 1) < max_idle_task_reposts) {
74 idle_task_runner->PostIdleTask(
75 FROM_HERE, base::Bind(&RepostingIdleTestTask,
76 base::Unretained(idle_task_runner), run_count));
78 (*run_count)++;
81 void RepostingUpdateClockIdleTestTask(
82 SingleThreadIdleTaskRunner* idle_task_runner,
83 int* run_count,
84 base::SimpleTestTickClock* clock,
85 base::TimeDelta advance_time,
86 std::vector<base::TimeTicks>* deadlines,
87 base::TimeTicks deadline) {
88 if ((*run_count + 1) < max_idle_task_reposts) {
89 idle_task_runner->PostIdleTask(
90 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
91 base::Unretained(idle_task_runner), run_count,
92 clock, advance_time, deadlines));
94 deadlines->push_back(deadline);
95 (*run_count)++;
96 clock->Advance(advance_time);
99 void WillBeginFrameIdleTask(RendererScheduler* scheduler,
100 base::SimpleTestTickClock* clock,
101 base::TimeTicks deadline) {
102 scheduler->WillBeginFrame(cc::BeginFrameArgs::Create(
103 BEGINFRAME_FROM_HERE, clock->NowTicks(), base::TimeTicks(),
104 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
107 void UpdateClockToDeadlineIdleTestTask(base::SimpleTestTickClock* clock,
108 int* run_count,
109 base::TimeTicks deadline) {
110 clock->Advance(deadline - clock->NowTicks());
111 (*run_count)++;
114 void PostingYieldingTestTask(RendererSchedulerImpl* scheduler,
115 base::SingleThreadTaskRunner* task_runner,
116 bool simulate_input,
117 bool* should_yield_before,
118 bool* should_yield_after) {
119 *should_yield_before = scheduler->ShouldYieldForHighPriorityWork();
120 task_runner->PostTask(FROM_HERE, base::Bind(NullTask));
121 if (simulate_input) {
122 scheduler->DidHandleInputEventOnCompositorThread(
123 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
124 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
126 *should_yield_after = scheduler->ShouldYieldForHighPriorityWork();
129 void AnticipationTestTask(RendererSchedulerImpl* scheduler,
130 bool simulate_input,
131 bool* is_anticipated_before,
132 bool* is_anticipated_after) {
133 *is_anticipated_before = scheduler->IsHighPriorityWorkAnticipated();
134 if (simulate_input) {
135 scheduler->DidHandleInputEventOnCompositorThread(
136 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
137 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
139 *is_anticipated_after = scheduler->IsHighPriorityWorkAnticipated();
142 }; // namespace
144 class RendererSchedulerImplForTest : public RendererSchedulerImpl {
145 public:
146 using RendererSchedulerImpl::OnIdlePeriodEnded;
147 using RendererSchedulerImpl::OnIdlePeriodStarted;
148 using RendererSchedulerImpl::Policy;
149 using RendererSchedulerImpl::PolicyToString;
151 RendererSchedulerImplForTest(
152 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner)
153 : RendererSchedulerImpl(main_task_runner), update_policy_count_(0) {}
155 void UpdatePolicyLocked(UpdateType update_type) override {
156 update_policy_count_++;
157 RendererSchedulerImpl::UpdatePolicyLocked(update_type);
160 void EnsureUrgentPolicyUpdatePostedOnMainThread() {
161 base::AutoLock lock(any_thread_lock_);
162 RendererSchedulerImpl::EnsureUrgentPolicyUpdatePostedOnMainThread(
163 FROM_HERE);
166 void ScheduleDelayedPolicyUpdate(base::TimeTicks now, base::TimeDelta delay) {
167 delayed_update_policy_runner_.SetDeadline(FROM_HERE, delay, now);
170 bool BeginMainFrameOnCriticalPath() {
171 base::AutoLock lock(any_thread_lock_);
172 return AnyThread().begin_main_frame_on_critical_path_;
175 int update_policy_count_;
178 // Lets gtest print human readable Policy values.
179 ::std::ostream& operator<<(::std::ostream& os,
180 const RendererSchedulerImplForTest::Policy& policy) {
181 return os << RendererSchedulerImplForTest::PolicyToString(policy);
184 class RendererSchedulerImplTest : public testing::Test {
185 public:
186 using Policy = RendererSchedulerImpl::Policy;
188 RendererSchedulerImplTest() : clock_(new base::SimpleTestTickClock()) {
189 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
192 RendererSchedulerImplTest(base::MessageLoop* message_loop)
193 : clock_(new base::SimpleTestTickClock()), message_loop_(message_loop) {
194 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
197 ~RendererSchedulerImplTest() override {}
199 void SetUp() override {
200 if (message_loop_) {
201 main_task_runner_ =
202 SchedulerTaskRunnerDelegateImpl::Create(message_loop_.get());
203 } else {
204 mock_task_runner_ = make_scoped_refptr(
205 new cc::OrderedSimpleTaskRunner(clock_.get(), false));
206 main_task_runner_ =
207 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_);
209 Initialize(
210 make_scoped_ptr(new RendererSchedulerImplForTest(main_task_runner_)));
213 void Initialize(scoped_ptr<RendererSchedulerImplForTest> scheduler) {
214 scheduler_ = scheduler.Pass();
215 default_task_runner_ = scheduler_->DefaultTaskRunner();
216 compositor_task_runner_ = scheduler_->CompositorTaskRunner();
217 loading_task_runner_ = scheduler_->LoadingTaskRunner();
218 idle_task_runner_ = scheduler_->IdleTaskRunner();
219 timer_task_runner_ = scheduler_->TimerTaskRunner();
220 scheduler_->GetSchedulerHelperForTesting()->SetTimeSourceForTesting(
221 make_scoped_ptr(new TestTimeSource(clock_.get())));
222 scheduler_->GetSchedulerHelperForTesting()
223 ->GetTaskQueueManagerForTesting()
224 ->SetTimeSourceForTesting(
225 make_scoped_ptr(new TestTimeSource(clock_.get())));
228 void TearDown() override {
229 DCHECK(!mock_task_runner_.get() || !message_loop_.get());
230 scheduler_->Shutdown();
231 if (mock_task_runner_.get()) {
232 // Check that all tests stop posting tasks.
233 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
234 while (mock_task_runner_->RunUntilIdle()) {
236 } else {
237 message_loop_->RunUntilIdle();
239 scheduler_.reset();
242 void RunUntilIdle() {
243 // Only one of mock_task_runner_ or message_loop_ should be set.
244 DCHECK(!mock_task_runner_.get() || !message_loop_.get());
245 if (mock_task_runner_.get())
246 mock_task_runner_->RunUntilIdle();
247 else
248 message_loop_->RunUntilIdle();
251 void DoMainFrame() {
252 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
253 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
254 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
255 scheduler_->DidCommitFrameToCompositor();
258 void EnableIdleTasks() { DoMainFrame(); }
260 Policy CurrentPolicy() {
261 return scheduler_->MainThreadOnly().current_policy_;
264 // Helper for posting several tasks of specific types. |task_descriptor| is a
265 // string with space delimited task identifiers. The first letter of each
266 // task identifier specifies the task type:
267 // - 'D': Default task
268 // - 'C': Compositor task
269 // - 'L': Loading task
270 // - 'I': Idle task
271 // - 'T': Timer task
272 void PostTestTasks(std::vector<std::string>* run_order,
273 const std::string& task_descriptor) {
274 std::istringstream stream(task_descriptor);
275 while (!stream.eof()) {
276 std::string task;
277 stream >> task;
278 switch (task[0]) {
279 case 'D':
280 default_task_runner_->PostTask(
281 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
282 break;
283 case 'C':
284 compositor_task_runner_->PostTask(
285 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
286 break;
287 case 'L':
288 loading_task_runner_->PostTask(
289 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
290 break;
291 case 'I':
292 idle_task_runner_->PostIdleTask(
293 FROM_HERE,
294 base::Bind(&AppendToVectorIdleTestTask, run_order, task));
295 break;
296 case 'T':
297 timer_task_runner_->PostTask(
298 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
299 break;
300 default:
301 NOTREACHED();
306 protected:
307 static base::TimeDelta priority_escalation_after_input_duration() {
308 return base::TimeDelta::FromMilliseconds(
309 RendererSchedulerImpl::kPriorityEscalationAfterInputMillis);
312 static base::TimeDelta maximum_idle_period_duration() {
313 return base::TimeDelta::FromMilliseconds(
314 IdleHelper::kMaximumIdlePeriodMillis);
317 static base::TimeDelta end_idle_when_hidden_delay() {
318 return base::TimeDelta::FromMilliseconds(
319 RendererSchedulerImpl::kEndIdleWhenHiddenDelayMillis);
322 static base::TimeDelta idle_period_starvation_threshold() {
323 return base::TimeDelta::FromMilliseconds(
324 RendererSchedulerImpl::kIdlePeriodStarvationThresholdMillis);
327 template <typename E>
328 static void CallForEachEnumValue(E first,
329 E last,
330 const char* (*function)(E)) {
331 for (E val = first; val < last;
332 val = static_cast<E>(static_cast<int>(val) + 1)) {
333 (*function)(val);
337 static void CheckAllPolicyToString() {
338 CallForEachEnumValue<RendererSchedulerImpl::Policy>(
339 RendererSchedulerImpl::Policy::FIRST_POLICY,
340 RendererSchedulerImpl::Policy::POLICY_COUNT,
341 &RendererSchedulerImpl::PolicyToString);
344 scoped_ptr<base::SimpleTestTickClock> clock_;
345 // Only one of mock_task_runner_ or message_loop_ will be set.
346 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
347 scoped_ptr<base::MessageLoop> message_loop_;
349 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner_;
350 scoped_ptr<RendererSchedulerImplForTest> scheduler_;
351 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
352 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
353 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_;
354 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
355 scoped_refptr<base::SingleThreadTaskRunner> timer_task_runner_;
357 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplTest);
360 TEST_F(RendererSchedulerImplTest, TestPostDefaultTask) {
361 std::vector<std::string> run_order;
362 PostTestTasks(&run_order, "D1 D2 D3 D4");
364 RunUntilIdle();
365 EXPECT_THAT(run_order,
366 testing::ElementsAre(std::string("D1"), std::string("D2"),
367 std::string("D3"), std::string("D4")));
370 TEST_F(RendererSchedulerImplTest, TestPostDefaultAndCompositor) {
371 std::vector<std::string> run_order;
372 PostTestTasks(&run_order, "D1 C1");
373 RunUntilIdle();
374 EXPECT_THAT(run_order, testing::Contains("D1"));
375 EXPECT_THAT(run_order, testing::Contains("C1"));
378 TEST_F(RendererSchedulerImplTest, TestRentrantTask) {
379 int count = 0;
380 std::vector<int> run_order;
381 default_task_runner_->PostTask(
382 FROM_HERE, base::Bind(AppendToVectorReentrantTask, default_task_runner_,
383 &run_order, &count, 5));
384 RunUntilIdle();
386 EXPECT_THAT(run_order, testing::ElementsAre(0, 1, 2, 3, 4));
389 TEST_F(RendererSchedulerImplTest, TestPostIdleTask) {
390 int run_count = 0;
391 base::TimeTicks expected_deadline =
392 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(2300);
393 base::TimeTicks deadline_in_task;
395 clock_->Advance(base::TimeDelta::FromMilliseconds(100));
396 idle_task_runner_->PostIdleTask(
397 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
399 RunUntilIdle();
400 EXPECT_EQ(0, run_count); // Shouldn't run yet as no WillBeginFrame.
402 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
403 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
404 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
405 RunUntilIdle();
406 EXPECT_EQ(0, run_count); // Shouldn't run as no DidCommitFrameToCompositor.
408 clock_->Advance(base::TimeDelta::FromMilliseconds(1200));
409 scheduler_->DidCommitFrameToCompositor();
410 RunUntilIdle();
411 EXPECT_EQ(0, run_count); // We missed the deadline.
413 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
414 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
415 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
416 clock_->Advance(base::TimeDelta::FromMilliseconds(800));
417 scheduler_->DidCommitFrameToCompositor();
418 RunUntilIdle();
419 EXPECT_EQ(1, run_count);
420 EXPECT_EQ(expected_deadline, deadline_in_task);
423 TEST_F(RendererSchedulerImplTest, TestRepostingIdleTask) {
424 int run_count = 0;
426 max_idle_task_reposts = 2;
427 idle_task_runner_->PostIdleTask(
428 FROM_HERE,
429 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
430 EnableIdleTasks();
431 RunUntilIdle();
432 EXPECT_EQ(1, run_count);
434 // Reposted tasks shouldn't run until next idle period.
435 RunUntilIdle();
436 EXPECT_EQ(1, run_count);
438 EnableIdleTasks();
439 RunUntilIdle();
440 EXPECT_EQ(2, run_count);
443 TEST_F(RendererSchedulerImplTest, TestIdleTaskExceedsDeadline) {
444 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
445 int run_count = 0;
447 // Post two UpdateClockToDeadlineIdleTestTask tasks.
448 idle_task_runner_->PostIdleTask(
449 FROM_HERE,
450 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_.get(), &run_count));
451 idle_task_runner_->PostIdleTask(
452 FROM_HERE,
453 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_.get(), &run_count));
455 EnableIdleTasks();
456 RunUntilIdle();
457 // Only the first idle task should execute since it's used up the deadline.
458 EXPECT_EQ(1, run_count);
460 EnableIdleTasks();
461 RunUntilIdle();
462 // Second task should be run on the next idle period.
463 EXPECT_EQ(2, run_count);
466 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskAfterWakeup) {
467 base::TimeTicks deadline_in_task;
468 int run_count = 0;
470 idle_task_runner_->PostIdleTaskAfterWakeup(
471 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
473 EnableIdleTasks();
474 RunUntilIdle();
475 // Shouldn't run yet as no other task woke up the scheduler.
476 EXPECT_EQ(0, run_count);
478 idle_task_runner_->PostIdleTaskAfterWakeup(
479 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
481 EnableIdleTasks();
482 RunUntilIdle();
483 // Another after wakeup idle task shouldn't wake the scheduler.
484 EXPECT_EQ(0, run_count);
486 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
488 RunUntilIdle();
489 EnableIdleTasks(); // Must start a new idle period before idle task runs.
490 RunUntilIdle();
491 // Execution of default task queue task should trigger execution of idle task.
492 EXPECT_EQ(2, run_count);
495 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskAfterWakeupWhileAwake) {
496 base::TimeTicks deadline_in_task;
497 int run_count = 0;
499 idle_task_runner_->PostIdleTaskAfterWakeup(
500 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
501 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
503 RunUntilIdle();
504 EnableIdleTasks(); // Must start a new idle period before idle task runs.
505 RunUntilIdle();
506 // Should run as the scheduler was already awakened by the normal task.
507 EXPECT_EQ(1, run_count);
510 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskWakesAfterWakeupIdleTask) {
511 base::TimeTicks deadline_in_task;
512 int run_count = 0;
514 idle_task_runner_->PostIdleTaskAfterWakeup(
515 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
516 idle_task_runner_->PostIdleTask(
517 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
519 EnableIdleTasks();
520 RunUntilIdle();
521 // Must start a new idle period before after-wakeup idle task runs.
522 EnableIdleTasks();
523 RunUntilIdle();
524 // Normal idle task should wake up after-wakeup idle task.
525 EXPECT_EQ(2, run_count);
528 TEST_F(RendererSchedulerImplTest, TestDelayedEndIdlePeriodCanceled) {
529 int run_count = 0;
531 base::TimeTicks deadline_in_task;
532 idle_task_runner_->PostIdleTask(
533 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
535 // Trigger the beginning of an idle period for 1000ms.
536 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
537 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
538 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
539 DoMainFrame();
541 // End the idle period early (after 500ms), and send a WillBeginFrame which
542 // specifies that the next idle period should end 1000ms from now.
543 clock_->Advance(base::TimeDelta::FromMilliseconds(500));
544 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
545 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
546 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
548 RunUntilIdle();
549 EXPECT_EQ(0, run_count); // Not currently in an idle period.
551 // Trigger the start of the idle period before the task to end the previous
552 // idle period has been triggered.
553 clock_->Advance(base::TimeDelta::FromMilliseconds(400));
554 scheduler_->DidCommitFrameToCompositor();
556 // Post a task which simulates running until after the previous end idle
557 // period delayed task was scheduled for
558 scheduler_->DefaultTaskRunner()->PostTask(FROM_HERE, base::Bind(NullTask));
559 clock_->Advance(base::TimeDelta::FromMilliseconds(300));
561 RunUntilIdle();
562 EXPECT_EQ(1, run_count); // We should still be in the new idle period.
565 TEST_F(RendererSchedulerImplTest, TestDefaultPolicy) {
566 std::vector<std::string> run_order;
567 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
569 EnableIdleTasks();
570 RunUntilIdle();
571 EXPECT_THAT(run_order,
572 testing::ElementsAre(std::string("L1"), std::string("D1"),
573 std::string("C1"), std::string("D2"),
574 std::string("C2"), std::string("I1")));
577 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_CompositorHandlesInput) {
578 std::vector<std::string> run_order;
579 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
581 scheduler_->DidHandleInputEventOnCompositorThread(
582 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
583 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
584 EnableIdleTasks();
585 RunUntilIdle();
586 EXPECT_THAT(run_order,
587 testing::ElementsAre(std::string("C1"), std::string("C2"),
588 std::string("L1"), std::string("D1"),
589 std::string("D2"), std::string("I1")));
592 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_MainThreadHandlesInput) {
593 std::vector<std::string> run_order;
594 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
596 scheduler_->DidHandleInputEventOnCompositorThread(
597 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
598 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
599 EnableIdleTasks();
600 RunUntilIdle();
601 EXPECT_THAT(run_order,
602 testing::ElementsAre(std::string("C1"), std::string("C2"),
603 std::string("L1"), std::string("D1"),
604 std::string("D2"), std::string("I1")));
605 scheduler_->DidHandleInputEventOnMainThread(
606 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
609 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_DidAnimateForInput) {
610 std::vector<std::string> run_order;
611 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
613 scheduler_->DidAnimateForInputOnCompositorThread();
614 EnableIdleTasks();
615 RunUntilIdle();
616 EXPECT_THAT(run_order,
617 testing::ElementsAre(std::string("C1"), std::string("C2"),
618 std::string("D1"), std::string("D2"),
619 std::string("I1")));
622 TEST_F(
623 RendererSchedulerImplTest,
624 TestCompositorPolicy_ExpensiveTimersDontRunWhenMainThreadOnCriticalPath) {
625 std::vector<std::string> run_order;
627 // Simulate a bunch of expensive timer tasks
628 for (int i = 0; i < 10; i++) {
629 timer_task_runner_->PostTask(
630 FROM_HERE, base::Bind(&base::SimpleTestTickClock::Advance,
631 base::Unretained(clock_.get()),
632 base::TimeDelta::FromMilliseconds(500)));
634 RunUntilIdle();
636 // Timers should now be disabled during main thread user userinteractions.
637 PostTestTasks(&run_order, "C1 T1");
639 scheduler_->DidAnimateForInputOnCompositorThread();
640 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
641 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
642 base::TimeDelta::FromMilliseconds(16), cc::BeginFrameArgs::NORMAL));
643 RunUntilIdle();
645 EXPECT_THAT(run_order, testing::ElementsAre(std::string("C1")));
646 clock_->Advance(priority_escalation_after_input_duration() * 2);
648 run_order.clear();
649 RunUntilIdle();
650 EXPECT_THAT(run_order, testing::ElementsAre(std::string("T1")));
653 TEST_F(RendererSchedulerImplTest,
654 TestCompositorPolicy_TimersAlwaysRunIfNoRecentIdlePeriod) {
655 std::vector<std::string> run_order;
656 PostTestTasks(&run_order, "C1 T1");
658 // Simulate no recent idle period.
659 clock_->Advance(idle_period_starvation_threshold() * 2);
661 scheduler_->DidAnimateForInputOnCompositorThread();
662 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
663 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
664 base::TimeDelta::FromMilliseconds(16), cc::BeginFrameArgs::NORMAL));
666 RunUntilIdle();
668 EXPECT_THAT(run_order,
669 testing::ElementsAre(std::string("C1"), std::string("T1")));
672 TEST_F(RendererSchedulerImplTest,
673 TestCompositorPolicy_TimersAlwaysRun_MainThreadNotOnCriticalPath) {
674 std::vector<std::string> run_order;
675 PostTestTasks(&run_order, "C1 T1");
677 scheduler_->DidAnimateForInputOnCompositorThread();
678 cc::BeginFrameArgs begin_frame_args1 = cc::BeginFrameArgs::Create(
679 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
680 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
681 begin_frame_args1.on_critical_path = false;
682 scheduler_->WillBeginFrame(begin_frame_args1);
683 scheduler_->DidCommitFrameToCompositor(); // Starts Idle Period
684 RunUntilIdle();
686 EXPECT_THAT(run_order,
687 testing::ElementsAre(std::string("C1"), std::string("T1")));
689 // End the idle period.
690 clock_->Advance(base::TimeDelta::FromMilliseconds(500));
691 scheduler_->DidAnimateForInputOnCompositorThread();
692 cc::BeginFrameArgs begin_frame_args2 = cc::BeginFrameArgs::Create(
693 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
694 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
695 begin_frame_args2.on_critical_path = false;
696 scheduler_->WillBeginFrame(begin_frame_args2);
698 run_order.clear();
699 PostTestTasks(&run_order, "C1 T1");
700 RunUntilIdle();
702 EXPECT_THAT(run_order,
703 testing::ElementsAre(std::string("C1"), std::string("T1")));
706 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicy_Compositor) {
707 std::vector<std::string> run_order;
708 PostTestTasks(&run_order, "L1 D1 C1 D2 C2 T1 T2");
710 // Observation of touchstart should defer execution of timer, idle and loading
711 // tasks.
712 scheduler_->DidHandleInputEventOnCompositorThread(
713 FakeInputEvent(blink::WebInputEvent::TouchStart),
714 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
715 EnableIdleTasks();
716 RunUntilIdle();
717 EXPECT_THAT(run_order,
718 testing::ElementsAre(std::string("C1"), std::string("C2"),
719 std::string("D1"), std::string("D2")));
721 // Animation or meta events like TapDown/FlingCancel shouldn't affect the
722 // priority.
723 run_order.clear();
724 scheduler_->DidAnimateForInputOnCompositorThread();
725 scheduler_->DidHandleInputEventOnCompositorThread(
726 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel),
727 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
728 scheduler_->DidHandleInputEventOnCompositorThread(
729 FakeInputEvent(blink::WebInputEvent::GestureTapDown),
730 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
731 RunUntilIdle();
732 EXPECT_TRUE(run_order.empty());
734 // Action events like ScrollBegin will kick us back into compositor priority,
735 // allowing service of the timer, loading and idle queues.
736 run_order.clear();
737 scheduler_->DidHandleInputEventOnCompositorThread(
738 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
739 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
740 RunUntilIdle();
742 EXPECT_THAT(run_order,
743 testing::ElementsAre(std::string("L1"), std::string("T1"),
744 std::string("T2")));
747 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicy_MainThread) {
748 std::vector<std::string> run_order;
749 PostTestTasks(&run_order, "L1 D1 C1 D2 C2 T1 T2");
751 // Observation of touchstart should defer execution of timer, idle and loading
752 // tasks.
753 scheduler_->DidHandleInputEventOnCompositorThread(
754 FakeInputEvent(blink::WebInputEvent::TouchStart),
755 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
756 scheduler_->DidHandleInputEventOnMainThread(
757 FakeInputEvent(blink::WebInputEvent::TouchStart));
758 EnableIdleTasks();
759 RunUntilIdle();
760 EXPECT_THAT(run_order,
761 testing::ElementsAre(std::string("C1"), std::string("C2"),
762 std::string("D1"), std::string("D2")));
764 // Meta events like TapDown/FlingCancel shouldn't affect the priority.
765 run_order.clear();
766 scheduler_->DidHandleInputEventOnCompositorThread(
767 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel),
768 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
769 scheduler_->DidHandleInputEventOnMainThread(
770 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel));
771 scheduler_->DidHandleInputEventOnCompositorThread(
772 FakeInputEvent(blink::WebInputEvent::GestureTapDown),
773 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
774 scheduler_->DidHandleInputEventOnMainThread(
775 FakeInputEvent(blink::WebInputEvent::GestureTapDown));
776 RunUntilIdle();
777 EXPECT_TRUE(run_order.empty());
779 // Action events like ScrollBegin will kick us back into compositor priority,
780 // allowing service of the timer, loading and idle queues.
781 run_order.clear();
782 scheduler_->DidHandleInputEventOnCompositorThread(
783 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
784 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
785 scheduler_->DidHandleInputEventOnMainThread(
786 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin));
787 RunUntilIdle();
789 EXPECT_THAT(run_order,
790 testing::ElementsAre(std::string("L1"), std::string("T1"),
791 std::string("T2")));
794 TEST_F(RendererSchedulerImplTest, LoadingPriorityPolicy) {
795 std::vector<std::string> run_order;
796 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
798 scheduler_->OnPageLoadStarted();
799 EnableIdleTasks();
800 RunUntilIdle();
801 // In loading policy compositor tasks are best effort and should be run last.
802 EXPECT_THAT(run_order,
803 testing::ElementsAre(std::string("L1"), std::string("D1"),
804 std::string("D2"), std::string("I1"),
805 std::string("C1"), std::string("C2")));
807 // Advance 1.5s and try again, the loading policy should have ended and the
808 // task order should return to normal.
809 clock_->Advance(base::TimeDelta::FromMilliseconds(1500));
810 run_order.clear();
811 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
812 EnableIdleTasks();
813 RunUntilIdle();
814 EXPECT_THAT(run_order,
815 testing::ElementsAre(std::string("L1"), std::string("D1"),
816 std::string("C1"), std::string("D2"),
817 std::string("C2"), std::string("I1")));
820 TEST_F(RendererSchedulerImplTest,
821 EventConsumedOnCompositorThread_IgnoresMouseMove_WhenMouseUp) {
822 std::vector<std::string> run_order;
823 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
825 scheduler_->DidHandleInputEventOnCompositorThread(
826 FakeInputEvent(blink::WebInputEvent::MouseMove),
827 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
828 EnableIdleTasks();
829 RunUntilIdle();
830 // Note compositor tasks are not prioritized.
831 EXPECT_THAT(run_order,
832 testing::ElementsAre(std::string("D1"), std::string("C1"),
833 std::string("D2"), std::string("C2"),
834 std::string("I1")));
837 TEST_F(RendererSchedulerImplTest,
838 EventForwardedToMainThread_IgnoresMouseMove_WhenMouseUp) {
839 std::vector<std::string> run_order;
840 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
842 scheduler_->DidHandleInputEventOnCompositorThread(
843 FakeInputEvent(blink::WebInputEvent::MouseMove),
844 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
845 EnableIdleTasks();
846 RunUntilIdle();
847 // Note compositor tasks are not prioritized.
848 EXPECT_THAT(run_order,
849 testing::ElementsAre(std::string("D1"), std::string("C1"),
850 std::string("D2"), std::string("C2"),
851 std::string("I1")));
852 scheduler_->DidHandleInputEventOnMainThread(
853 FakeInputEvent(blink::WebInputEvent::MouseMove));
856 TEST_F(RendererSchedulerImplTest,
857 EventConsumedOnCompositorThread_MouseMove_WhenMouseDown) {
858 std::vector<std::string> run_order;
859 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
861 scheduler_->DidHandleInputEventOnCompositorThread(
862 FakeInputEvent(blink::WebInputEvent::MouseMove,
863 blink::WebInputEvent::LeftButtonDown),
864 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
865 EnableIdleTasks();
866 RunUntilIdle();
867 // Note compositor tasks are prioritized.
868 EXPECT_THAT(run_order,
869 testing::ElementsAre(std::string("C1"), std::string("C2"),
870 std::string("D1"), std::string("D2"),
871 std::string("I1")));
874 TEST_F(RendererSchedulerImplTest,
875 EventForwardedToMainThread_MouseMove_WhenMouseDown) {
876 std::vector<std::string> run_order;
877 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
879 scheduler_->DidHandleInputEventOnCompositorThread(
880 FakeInputEvent(blink::WebInputEvent::MouseMove,
881 blink::WebInputEvent::LeftButtonDown),
882 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
883 EnableIdleTasks();
884 RunUntilIdle();
885 // Note compositor tasks are prioritized.
886 EXPECT_THAT(run_order,
887 testing::ElementsAre(std::string("C1"), std::string("C2"),
888 std::string("D1"), std::string("D2"),
889 std::string("I1")));
890 scheduler_->DidHandleInputEventOnMainThread(FakeInputEvent(
891 blink::WebInputEvent::MouseMove, blink::WebInputEvent::LeftButtonDown));
894 TEST_F(RendererSchedulerImplTest, EventConsumedOnCompositorThread_MouseWheel) {
895 std::vector<std::string> run_order;
896 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
898 scheduler_->DidHandleInputEventOnCompositorThread(
899 FakeInputEvent(blink::WebInputEvent::MouseWheel),
900 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
901 EnableIdleTasks();
902 RunUntilIdle();
903 // Note compositor tasks are prioritized.
904 EXPECT_THAT(run_order,
905 testing::ElementsAre(std::string("C1"), std::string("C2"),
906 std::string("D1"), std::string("D2"),
907 std::string("I1")));
910 TEST_F(RendererSchedulerImplTest, EventForwardedToMainThread_MouseWheel) {
911 std::vector<std::string> run_order;
912 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
914 scheduler_->DidHandleInputEventOnCompositorThread(
915 FakeInputEvent(blink::WebInputEvent::MouseWheel),
916 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
917 EnableIdleTasks();
918 RunUntilIdle();
919 // Note compositor tasks are prioritized.
920 EXPECT_THAT(run_order,
921 testing::ElementsAre(std::string("C1"), std::string("C2"),
922 std::string("D1"), std::string("D2"),
923 std::string("I1")));
924 scheduler_->DidHandleInputEventOnMainThread(
925 FakeInputEvent(blink::WebInputEvent::MouseWheel));
928 TEST_F(RendererSchedulerImplTest,
929 EventConsumedOnCompositorThread_IgnoresKeyboardEvents) {
930 std::vector<std::string> run_order;
931 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
933 scheduler_->DidHandleInputEventOnCompositorThread(
934 FakeInputEvent(blink::WebInputEvent::KeyDown),
935 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
936 EnableIdleTasks();
937 RunUntilIdle();
938 // Note compositor tasks are not prioritized.
939 EXPECT_THAT(run_order,
940 testing::ElementsAre(std::string("D1"), std::string("C1"),
941 std::string("D2"), std::string("C2"),
942 std::string("I1")));
945 TEST_F(RendererSchedulerImplTest,
946 EventForwardedToMainThread_IgnoresKeyboardEvents) {
947 std::vector<std::string> run_order;
948 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
950 scheduler_->DidHandleInputEventOnCompositorThread(
951 FakeInputEvent(blink::WebInputEvent::KeyDown),
952 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
953 EnableIdleTasks();
954 RunUntilIdle();
955 // Note compositor tasks are not prioritized.
956 EXPECT_THAT(run_order,
957 testing::ElementsAre(std::string("D1"), std::string("C1"),
958 std::string("D2"), std::string("C2"),
959 std::string("I1")));
960 scheduler_->DidHandleInputEventOnMainThread(
961 FakeInputEvent(blink::WebInputEvent::KeyDown));
964 TEST_F(RendererSchedulerImplTest,
965 TestCompositorPolicyDoesNotStarveDefaultTasks) {
966 std::vector<std::string> run_order;
967 PostTestTasks(&run_order, "D1 C1");
969 for (int i = 0; i < 20; i++) {
970 compositor_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
972 PostTestTasks(&run_order, "C2");
974 scheduler_->DidHandleInputEventOnCompositorThread(
975 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
976 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
977 RunUntilIdle();
978 // Ensure that the default D1 task gets to run at some point before the final
979 // C2 compositor task.
980 EXPECT_THAT(run_order,
981 testing::ElementsAre(std::string("C1"), std::string("D1"),
982 std::string("C2")));
985 TEST_F(RendererSchedulerImplTest,
986 TestCompositorPolicyEnds_CompositorHandlesInput) {
987 std::vector<std::string> run_order;
988 PostTestTasks(&run_order, "D1 C1 D2 C2");
990 scheduler_->DidHandleInputEventOnCompositorThread(
991 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
992 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
993 RunUntilIdle();
994 EXPECT_THAT(run_order,
995 testing::ElementsAre(std::string("C1"), std::string("C2"),
996 std::string("D1"), std::string("D2")));
998 run_order.clear();
999 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1000 PostTestTasks(&run_order, "D1 C1 D2 C2");
1002 // Compositor policy mode should have ended now that the clock has advanced.
1003 RunUntilIdle();
1004 EXPECT_THAT(run_order,
1005 testing::ElementsAre(std::string("D1"), std::string("C1"),
1006 std::string("D2"), std::string("C2")));
1009 TEST_F(RendererSchedulerImplTest,
1010 TestCompositorPolicyEnds_MainThreadHandlesInput) {
1011 std::vector<std::string> run_order;
1012 PostTestTasks(&run_order, "D1 C1 D2 C2");
1014 scheduler_->DidHandleInputEventOnCompositorThread(
1015 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1016 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1017 scheduler_->DidHandleInputEventOnMainThread(
1018 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1019 RunUntilIdle();
1020 EXPECT_THAT(run_order,
1021 testing::ElementsAre(std::string("C1"), std::string("C2"),
1022 std::string("D1"), std::string("D2")));
1024 run_order.clear();
1025 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1026 PostTestTasks(&run_order, "D1 C1 D2 C2");
1028 // Compositor policy mode should have ended now that the clock has advanced.
1029 RunUntilIdle();
1030 EXPECT_THAT(run_order,
1031 testing::ElementsAre(std::string("D1"), std::string("C1"),
1032 std::string("D2"), std::string("C2")));
1035 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicyEndsAfterTimeout) {
1036 std::vector<std::string> run_order;
1037 PostTestTasks(&run_order, "L1 D1 C1 D2 C2");
1039 scheduler_->DidHandleInputEventOnCompositorThread(
1040 FakeInputEvent(blink::WebInputEvent::TouchStart),
1041 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1042 RunUntilIdle();
1043 EXPECT_THAT(run_order,
1044 testing::ElementsAre(std::string("C1"), std::string("C2"),
1045 std::string("D1"), std::string("D2")));
1047 run_order.clear();
1048 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1050 // Don't post any compositor tasks to simulate a very long running event
1051 // handler.
1052 PostTestTasks(&run_order, "D1 D2");
1054 // Touchstart policy mode should have ended now that the clock has advanced.
1055 RunUntilIdle();
1056 EXPECT_THAT(run_order,
1057 testing::ElementsAre(std::string("L1"), std::string("D1"),
1058 std::string("D2")));
1061 TEST_F(RendererSchedulerImplTest,
1062 TestTouchstartPolicyEndsAfterConsecutiveTouchmoves) {
1063 std::vector<std::string> run_order;
1064 PostTestTasks(&run_order, "L1 D1 C1 D2 C2");
1066 // Observation of touchstart should defer execution of idle and loading tasks.
1067 scheduler_->DidHandleInputEventOnCompositorThread(
1068 FakeInputEvent(blink::WebInputEvent::TouchStart),
1069 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1070 RunUntilIdle();
1071 EXPECT_THAT(run_order,
1072 testing::ElementsAre(std::string("C1"), std::string("C2"),
1073 std::string("D1"), std::string("D2")));
1075 // Receiving the first touchmove will not affect scheduler priority.
1076 run_order.clear();
1077 scheduler_->DidHandleInputEventOnCompositorThread(
1078 FakeInputEvent(blink::WebInputEvent::TouchMove),
1079 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1080 RunUntilIdle();
1081 EXPECT_TRUE(run_order.empty());
1083 // Receiving the second touchmove will kick us back into compositor priority.
1084 run_order.clear();
1085 scheduler_->DidHandleInputEventOnCompositorThread(
1086 FakeInputEvent(blink::WebInputEvent::TouchMove),
1087 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1088 RunUntilIdle();
1089 EXPECT_THAT(run_order, testing::ElementsAre(std::string("L1")));
1092 TEST_F(RendererSchedulerImplTest, TestIsHighPriorityWorkAnticipated) {
1093 bool is_anticipated_before = false;
1094 bool is_anticipated_after = false;
1096 bool simulate_input = false;
1097 default_task_runner_->PostTask(
1098 FROM_HERE,
1099 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1100 &is_anticipated_before, &is_anticipated_after));
1101 RunUntilIdle();
1102 // In its default state, without input receipt, the scheduler should indicate
1103 // that no high-priority is anticipated.
1104 EXPECT_FALSE(is_anticipated_before);
1105 EXPECT_FALSE(is_anticipated_after);
1107 simulate_input = true;
1108 default_task_runner_->PostTask(
1109 FROM_HERE,
1110 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1111 &is_anticipated_before, &is_anticipated_after));
1112 RunUntilIdle();
1113 // When input is received, the scheduler should indicate that high-priority
1114 // work is anticipated.
1115 EXPECT_FALSE(is_anticipated_before);
1116 EXPECT_TRUE(is_anticipated_after);
1118 clock_->Advance(priority_escalation_after_input_duration() * 2);
1119 simulate_input = false;
1120 default_task_runner_->PostTask(
1121 FROM_HERE,
1122 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1123 &is_anticipated_before, &is_anticipated_after));
1124 RunUntilIdle();
1125 // Without additional input, the scheduler should indicate that high-priority
1126 // work is no longer anticipated.
1127 EXPECT_FALSE(is_anticipated_before);
1128 EXPECT_FALSE(is_anticipated_after);
1131 TEST_F(RendererSchedulerImplTest, TestShouldYield) {
1132 bool should_yield_before = false;
1133 bool should_yield_after = false;
1135 default_task_runner_->PostTask(
1136 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1137 default_task_runner_, false, &should_yield_before,
1138 &should_yield_after));
1139 RunUntilIdle();
1140 // Posting to default runner shouldn't cause yielding.
1141 EXPECT_FALSE(should_yield_before);
1142 EXPECT_FALSE(should_yield_after);
1144 default_task_runner_->PostTask(
1145 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1146 compositor_task_runner_, false,
1147 &should_yield_before, &should_yield_after));
1148 RunUntilIdle();
1149 // Posting while not in compositor priority shouldn't cause yielding.
1150 EXPECT_FALSE(should_yield_before);
1151 EXPECT_FALSE(should_yield_after);
1153 default_task_runner_->PostTask(
1154 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1155 compositor_task_runner_, true, &should_yield_before,
1156 &should_yield_after));
1157 RunUntilIdle();
1158 // We should be able to switch to compositor priority mid-task.
1159 EXPECT_FALSE(should_yield_before);
1160 EXPECT_TRUE(should_yield_after);
1162 // Receiving a touchstart should immediately trigger yielding, even if
1163 // there's no immediately pending work in the compositor queue.
1164 EXPECT_FALSE(scheduler_->ShouldYieldForHighPriorityWork());
1165 scheduler_->DidHandleInputEventOnCompositorThread(
1166 FakeInputEvent(blink::WebInputEvent::TouchStart),
1167 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1168 EXPECT_TRUE(scheduler_->ShouldYieldForHighPriorityWork());
1169 RunUntilIdle();
1172 TEST_F(RendererSchedulerImplTest, SlowMainThreadInputEvent) {
1173 EXPECT_EQ(Policy::NORMAL, CurrentPolicy());
1175 // An input event should bump us into input priority.
1176 scheduler_->DidHandleInputEventOnCompositorThread(
1177 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1178 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1179 RunUntilIdle();
1180 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy());
1182 // Simulate the input event being queued for a very long time. The compositor
1183 // task we post here represents the enqueued input task.
1184 clock_->Advance(priority_escalation_after_input_duration() * 2);
1185 scheduler_->DidHandleInputEventOnMainThread(
1186 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1187 RunUntilIdle();
1189 // Even though we exceeded the input priority escalation period, we should
1190 // still be in compositor priority since the input remains queued.
1191 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy());
1193 // After the escalation period ends we should go back into normal mode.
1194 clock_->Advance(priority_escalation_after_input_duration() * 2);
1195 RunUntilIdle();
1196 EXPECT_EQ(Policy::NORMAL, CurrentPolicy());
1199 class RendererSchedulerImplWithMockSchedulerTest
1200 : public RendererSchedulerImplTest {
1201 public:
1202 void SetUp() override {
1203 mock_task_runner_ = make_scoped_refptr(
1204 new cc::OrderedSimpleTaskRunner(clock_.get(), false));
1205 main_task_runner_ =
1206 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_);
1207 mock_scheduler_ = new RendererSchedulerImplForTest(main_task_runner_);
1208 Initialize(make_scoped_ptr(mock_scheduler_));
1211 protected:
1212 RendererSchedulerImplForTest* mock_scheduler_;
1215 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1216 OnlyOnePendingUrgentPolicyUpdatey) {
1217 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1218 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1219 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1220 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1222 RunUntilIdle();
1224 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1227 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1228 OnePendingDelayedAndOneUrgentUpdatePolicy) {
1229 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1231 mock_scheduler_->ScheduleDelayedPolicyUpdate(
1232 clock_->NowTicks(), base::TimeDelta::FromMilliseconds(1));
1233 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1235 RunUntilIdle();
1237 // We expect both the urgent and the delayed updates to run.
1238 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1241 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1242 OneUrgentAndOnePendingDelayedUpdatePolicy) {
1243 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1245 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1246 mock_scheduler_->ScheduleDelayedPolicyUpdate(
1247 clock_->NowTicks(), base::TimeDelta::FromMilliseconds(1));
1249 RunUntilIdle();
1251 // We expect both the urgent and the delayed updates to run.
1252 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1255 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1256 UpdatePolicyCountTriggeredByOneInputEvent) {
1257 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1258 // update.
1259 scheduler_->DidHandleInputEventOnCompositorThread(
1260 FakeInputEvent(blink::WebInputEvent::TouchStart),
1261 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1262 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1263 mock_task_runner_->RunPendingTasks();
1264 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1266 scheduler_->DidHandleInputEventOnMainThread(
1267 FakeInputEvent(blink::WebInputEvent::TouchStart));
1268 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1270 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1271 RunUntilIdle();
1273 // We finally expect a delayed policy update 100ms later.
1274 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1277 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1278 UpdatePolicyCountTriggeredByThreeInputEvents) {
1279 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1280 // update.
1281 scheduler_->DidHandleInputEventOnCompositorThread(
1282 FakeInputEvent(blink::WebInputEvent::TouchStart),
1283 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1284 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1285 mock_task_runner_->RunPendingTasks();
1286 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1288 scheduler_->DidHandleInputEventOnMainThread(
1289 FakeInputEvent(blink::WebInputEvent::TouchStart));
1290 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1292 // The second call to DidHandleInputEventOnCompositorThread should not post a
1293 // policy update because we are already in compositor priority.
1294 scheduler_->DidHandleInputEventOnCompositorThread(
1295 FakeInputEvent(blink::WebInputEvent::TouchMove),
1296 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1297 mock_task_runner_->RunPendingTasks();
1298 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1300 // We expect DidHandleInputEvent to trigger a policy update.
1301 scheduler_->DidHandleInputEventOnMainThread(
1302 FakeInputEvent(blink::WebInputEvent::TouchMove));
1303 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1305 // The third call to DidHandleInputEventOnCompositorThread should post a
1306 // policy update because the awaiting_touch_start_response_ flag changed.
1307 scheduler_->DidHandleInputEventOnCompositorThread(
1308 FakeInputEvent(blink::WebInputEvent::TouchMove),
1309 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1310 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1311 mock_task_runner_->RunPendingTasks();
1312 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1314 // We expect DidHandleInputEvent to trigger a policy update.
1315 scheduler_->DidHandleInputEventOnMainThread(
1316 FakeInputEvent(blink::WebInputEvent::TouchMove));
1317 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1319 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1320 RunUntilIdle();
1322 // We finally expect a delayed policy update.
1323 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1326 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1327 UpdatePolicyCountTriggeredByTwoInputEventsWithALongSeparatingDelay) {
1328 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1329 // update.
1330 scheduler_->DidHandleInputEventOnCompositorThread(
1331 FakeInputEvent(blink::WebInputEvent::TouchStart),
1332 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1333 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1334 mock_task_runner_->RunPendingTasks();
1335 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1337 scheduler_->DidHandleInputEventOnMainThread(
1338 FakeInputEvent(blink::WebInputEvent::TouchStart));
1339 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1341 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1342 RunUntilIdle();
1343 // We expect a delayed policy update.
1344 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1346 // We expect the second call to DidHandleInputEventOnCompositorThread to post
1347 // an urgent policy update because we are no longer in compositor priority.
1348 scheduler_->DidHandleInputEventOnCompositorThread(
1349 FakeInputEvent(blink::WebInputEvent::TouchMove),
1350 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1351 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1352 mock_task_runner_->RunPendingTasks();
1353 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1355 scheduler_->DidHandleInputEventOnMainThread(
1356 FakeInputEvent(blink::WebInputEvent::TouchMove));
1357 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1359 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1360 RunUntilIdle();
1362 // We finally expect a delayed policy update.
1363 EXPECT_EQ(4, mock_scheduler_->update_policy_count_);
1366 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1367 EnsureUpdatePolicyNotTriggeredTooOften) {
1368 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1370 scheduler_->DidHandleInputEventOnCompositorThread(
1371 FakeInputEvent(blink::WebInputEvent::TouchStart),
1372 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1373 scheduler_->DidHandleInputEventOnCompositorThread(
1374 FakeInputEvent(blink::WebInputEvent::TouchMove),
1375 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1377 // We expect the first call to IsHighPriorityWorkAnticipated to be called
1378 // after recieving an input event (but before the UpdateTask was processed) to
1379 // call UpdatePolicy.
1380 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1381 scheduler_->IsHighPriorityWorkAnticipated();
1382 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1383 // Subsequent calls should not call UpdatePolicy.
1384 scheduler_->IsHighPriorityWorkAnticipated();
1385 scheduler_->IsHighPriorityWorkAnticipated();
1386 scheduler_->IsHighPriorityWorkAnticipated();
1387 scheduler_->ShouldYieldForHighPriorityWork();
1388 scheduler_->ShouldYieldForHighPriorityWork();
1389 scheduler_->ShouldYieldForHighPriorityWork();
1390 scheduler_->ShouldYieldForHighPriorityWork();
1392 scheduler_->DidHandleInputEventOnMainThread(
1393 FakeInputEvent(blink::WebInputEvent::TouchStart));
1394 scheduler_->DidHandleInputEventOnMainThread(
1395 FakeInputEvent(blink::WebInputEvent::TouchMove));
1397 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1399 RunUntilIdle();
1400 // We expect both the urgent and the delayed updates to run in addition to the
1401 // earlier updated cause by IsHighPriorityWorkAnticipated.
1402 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1405 class RendererSchedulerImplWithMessageLoopTest
1406 : public RendererSchedulerImplTest {
1407 public:
1408 RendererSchedulerImplWithMessageLoopTest()
1409 : RendererSchedulerImplTest(new base::MessageLoop()) {}
1410 ~RendererSchedulerImplWithMessageLoopTest() override {}
1412 void PostFromNestedRunloop(std::vector<
1413 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) {
1414 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get());
1415 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) {
1416 if (pair.second) {
1417 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first);
1418 } else {
1419 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first);
1422 EnableIdleTasks();
1423 message_loop_->RunUntilIdle();
1426 private:
1427 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplWithMessageLoopTest);
1430 TEST_F(RendererSchedulerImplWithMessageLoopTest,
1431 NonNestableIdleTaskDoesntExecuteInNestedLoop) {
1432 std::vector<std::string> order;
1433 idle_task_runner_->PostIdleTask(
1434 FROM_HERE,
1435 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("1")));
1436 idle_task_runner_->PostIdleTask(
1437 FROM_HERE,
1438 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("2")));
1440 std::vector<std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>
1441 tasks_to_post_from_nested_loop;
1442 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1443 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("3")),
1444 false));
1445 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1446 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("4")), true));
1447 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1448 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true));
1450 default_task_runner_->PostTask(
1451 FROM_HERE,
1452 base::Bind(
1453 &RendererSchedulerImplWithMessageLoopTest::PostFromNestedRunloop,
1454 base::Unretained(this),
1455 base::Unretained(&tasks_to_post_from_nested_loop)));
1457 EnableIdleTasks();
1458 RunUntilIdle();
1459 // Note we expect task 3 to run last because it's non-nestable.
1460 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"),
1461 std::string("4"), std::string("5"),
1462 std::string("3")));
1465 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriod) {
1466 base::TimeTicks expected_deadline =
1467 clock_->NowTicks() + maximum_idle_period_duration();
1468 base::TimeTicks deadline_in_task;
1469 int run_count = 0;
1471 idle_task_runner_->PostIdleTask(
1472 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1474 RunUntilIdle();
1475 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period.
1477 scheduler_->BeginFrameNotExpectedSoon();
1478 RunUntilIdle();
1479 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
1480 EXPECT_EQ(expected_deadline, deadline_in_task);
1483 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodWithPendingDelayedTask) {
1484 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30);
1485 base::TimeTicks expected_deadline = clock_->NowTicks() + pending_task_delay;
1486 base::TimeTicks deadline_in_task;
1487 int run_count = 0;
1489 idle_task_runner_->PostIdleTask(
1490 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1491 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1492 pending_task_delay);
1494 scheduler_->BeginFrameNotExpectedSoon();
1495 RunUntilIdle();
1496 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
1497 EXPECT_EQ(expected_deadline, deadline_in_task);
1500 TEST_F(RendererSchedulerImplTest,
1501 TestLongIdlePeriodWithLatePendingDelayedTask) {
1502 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10);
1503 base::TimeTicks deadline_in_task;
1504 int run_count = 0;
1506 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1507 pending_task_delay);
1509 // Advance clock until after delayed task was meant to be run.
1510 clock_->Advance(base::TimeDelta::FromMilliseconds(20));
1512 // Post an idle task and BeginFrameNotExpectedSoon to initiate a long idle
1513 // period. Since there is a late pending delayed task this shouldn't actually
1514 // start an idle period.
1515 idle_task_runner_->PostIdleTask(
1516 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1517 scheduler_->BeginFrameNotExpectedSoon();
1518 RunUntilIdle();
1519 EXPECT_EQ(0, run_count);
1521 // After the delayed task has been run we should trigger an idle period.
1522 clock_->Advance(maximum_idle_period_duration());
1523 RunUntilIdle();
1524 EXPECT_EQ(1, run_count);
1527 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodRepeating) {
1528 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1529 std::vector<base::TimeTicks> actual_deadlines;
1530 int run_count = 0;
1532 max_idle_task_reposts = 3;
1533 base::TimeTicks clock_before(clock_->NowTicks());
1534 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10));
1535 idle_task_runner_->PostIdleTask(
1536 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
1537 idle_task_runner_, &run_count, clock_.get(),
1538 idle_task_runtime, &actual_deadlines));
1539 scheduler_->BeginFrameNotExpectedSoon();
1540 RunUntilIdle();
1541 EXPECT_EQ(3, run_count);
1542 EXPECT_THAT(
1543 actual_deadlines,
1544 testing::ElementsAre(
1545 clock_before + maximum_idle_period_duration(),
1546 clock_before + idle_task_runtime + maximum_idle_period_duration(),
1547 clock_before + (2 * idle_task_runtime) +
1548 maximum_idle_period_duration()));
1550 // Check that idle tasks don't run after the idle period ends with a
1551 // new BeginMainFrame.
1552 max_idle_task_reposts = 5;
1553 idle_task_runner_->PostIdleTask(
1554 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
1555 idle_task_runner_, &run_count, clock_.get(),
1556 idle_task_runtime, &actual_deadlines));
1557 idle_task_runner_->PostIdleTask(
1558 FROM_HERE, base::Bind(&WillBeginFrameIdleTask,
1559 base::Unretained(scheduler_.get()), clock_.get()));
1560 RunUntilIdle();
1561 EXPECT_EQ(4, run_count);
1564 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodDoesNotWakeScheduler) {
1565 base::TimeTicks deadline_in_task;
1566 int run_count = 0;
1568 // Start a long idle period and get the time it should end.
1569 scheduler_->BeginFrameNotExpectedSoon();
1570 // The scheduler should not run the initiate_next_long_idle_period task if
1571 // there are no idle tasks and no other task woke up the scheduler, thus
1572 // the idle period deadline shouldn't update at the end of the current long
1573 // idle period.
1574 base::TimeTicks idle_period_deadline =
1575 scheduler_->CurrentIdleTaskDeadlineForTesting();
1576 clock_->Advance(maximum_idle_period_duration());
1577 RunUntilIdle();
1579 base::TimeTicks new_idle_period_deadline =
1580 scheduler_->CurrentIdleTaskDeadlineForTesting();
1581 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
1583 // Posting a after-wakeup idle task also shouldn't wake the scheduler or
1584 // initiate the next long idle period.
1585 idle_task_runner_->PostIdleTaskAfterWakeup(
1586 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1587 RunUntilIdle();
1588 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
1589 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
1590 EXPECT_EQ(0, run_count);
1592 // Running a normal task should initiate a new long idle period though.
1593 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
1594 RunUntilIdle();
1595 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
1596 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(),
1597 new_idle_period_deadline);
1599 EXPECT_EQ(1, run_count);
1602 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodInTouchStartPolicy) {
1603 base::TimeTicks deadline_in_task;
1604 int run_count = 0;
1606 idle_task_runner_->PostIdleTask(
1607 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1609 // Observation of touchstart should defer the start of the long idle period.
1610 scheduler_->DidHandleInputEventOnCompositorThread(
1611 FakeInputEvent(blink::WebInputEvent::TouchStart),
1612 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1613 scheduler_->BeginFrameNotExpectedSoon();
1614 RunUntilIdle();
1615 EXPECT_EQ(0, run_count);
1617 // The long idle period should start after the touchstart policy has finished.
1618 clock_->Advance(priority_escalation_after_input_duration());
1619 RunUntilIdle();
1620 EXPECT_EQ(1, run_count);
1623 void TestCanExceedIdleDeadlineIfRequiredTask(RendererScheduler* scheduler,
1624 bool* can_exceed_idle_deadline_out,
1625 int* run_count,
1626 base::TimeTicks deadline) {
1627 *can_exceed_idle_deadline_out = scheduler->CanExceedIdleDeadlineIfRequired();
1628 (*run_count)++;
1631 TEST_F(RendererSchedulerImplTest, CanExceedIdleDeadlineIfRequired) {
1632 int run_count = 0;
1633 bool can_exceed_idle_deadline = false;
1635 // Should return false if not in an idle period.
1636 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired());
1638 // Should return false for short idle periods.
1639 idle_task_runner_->PostIdleTask(
1640 FROM_HERE,
1641 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1642 &can_exceed_idle_deadline, &run_count));
1643 EnableIdleTasks();
1644 RunUntilIdle();
1645 EXPECT_EQ(1, run_count);
1646 EXPECT_FALSE(can_exceed_idle_deadline);
1648 // Should return false for a long idle period which is shortened due to a
1649 // pending delayed task.
1650 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1651 base::TimeDelta::FromMilliseconds(10));
1652 idle_task_runner_->PostIdleTask(
1653 FROM_HERE,
1654 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1655 &can_exceed_idle_deadline, &run_count));
1656 scheduler_->BeginFrameNotExpectedSoon();
1657 RunUntilIdle();
1658 EXPECT_EQ(2, run_count);
1659 EXPECT_FALSE(can_exceed_idle_deadline);
1661 // Next long idle period will be for the maximum time, so
1662 // CanExceedIdleDeadlineIfRequired should return true.
1663 clock_->Advance(maximum_idle_period_duration());
1664 idle_task_runner_->PostIdleTask(
1665 FROM_HERE,
1666 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1667 &can_exceed_idle_deadline, &run_count));
1668 RunUntilIdle();
1669 EXPECT_EQ(3, run_count);
1670 EXPECT_TRUE(can_exceed_idle_deadline);
1672 // Next long idle period will be for the maximum time, so
1673 // CanExceedIdleDeadlineIfRequired should return true.
1674 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
1675 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
1676 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
1677 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired());
1680 TEST_F(RendererSchedulerImplTest, TestRendererHiddenIdlePeriod) {
1681 int run_count = 0;
1683 max_idle_task_reposts = 2;
1684 idle_task_runner_->PostIdleTask(
1685 FROM_HERE,
1686 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
1688 // Renderer should start in visible state.
1689 RunUntilIdle();
1690 EXPECT_EQ(0, run_count);
1692 // When we hide the renderer it should start a max deadline idle period, which
1693 // will run an idle task and then immediately start a new idle period, which
1694 // runs the second idle task.
1695 scheduler_->OnRendererHidden();
1696 RunUntilIdle();
1697 EXPECT_EQ(2, run_count);
1699 // Advance time by amount of time by the maximum amount of time we execute
1700 // idle tasks when hidden (plus some slack) - idle period should have ended.
1701 max_idle_task_reposts = 3;
1702 idle_task_runner_->PostIdleTask(
1703 FROM_HERE,
1704 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
1705 clock_->Advance(end_idle_when_hidden_delay() +
1706 base::TimeDelta::FromMilliseconds(10));
1707 RunUntilIdle();
1708 EXPECT_EQ(2, run_count);
1711 TEST_F(RendererSchedulerImplTest, TimerQueueEnabledByDefault) {
1712 std::vector<std::string> run_order;
1713 PostTestTasks(&run_order, "T1 T2");
1714 RunUntilIdle();
1715 EXPECT_THAT(run_order,
1716 testing::ElementsAre(std::string("T1"), std::string("T2")));
1719 TEST_F(RendererSchedulerImplTest, SuspendAndResumeTimerQueue) {
1720 std::vector<std::string> run_order;
1721 PostTestTasks(&run_order, "T1 T2");
1723 scheduler_->SuspendTimerQueue();
1724 RunUntilIdle();
1725 EXPECT_TRUE(run_order.empty());
1727 scheduler_->ResumeTimerQueue();
1728 RunUntilIdle();
1729 EXPECT_THAT(run_order,
1730 testing::ElementsAre(std::string("T1"), std::string("T2")));
1733 TEST_F(RendererSchedulerImplTest, MultipleSuspendsNeedMultipleResumes) {
1734 std::vector<std::string> run_order;
1735 PostTestTasks(&run_order, "T1 T2");
1737 scheduler_->SuspendTimerQueue();
1738 scheduler_->SuspendTimerQueue();
1739 scheduler_->SuspendTimerQueue();
1740 RunUntilIdle();
1741 EXPECT_TRUE(run_order.empty());
1743 scheduler_->ResumeTimerQueue();
1744 RunUntilIdle();
1745 EXPECT_TRUE(run_order.empty());
1747 scheduler_->ResumeTimerQueue();
1748 RunUntilIdle();
1749 EXPECT_TRUE(run_order.empty());
1751 scheduler_->ResumeTimerQueue();
1752 RunUntilIdle();
1753 EXPECT_THAT(run_order,
1754 testing::ElementsAre(std::string("T1"), std::string("T2")));
1757 TEST_F(RendererSchedulerImplTest, PolicyToString) {
1758 CheckAllPolicyToString();
1761 TEST_F(RendererSchedulerImplTest, MismatchedDidHandleInputEventOnMainThread) {
1762 // This should not DCHECK because there was no corresponding compositor side
1763 // call to DidHandleInputEventOnCompositorThread with
1764 // INPUT_EVENT_ACK_STATE_NOT_CONSUMED. There are legitimate reasons for the
1765 // compositor to not be there and we don't want to make debugging impossible.
1766 scheduler_->DidHandleInputEventOnMainThread(
1767 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1770 TEST_F(RendererSchedulerImplTest, BeginMainFrameOnCriticalPath) {
1771 ASSERT_FALSE(scheduler_->BeginMainFrameOnCriticalPath());
1773 cc::BeginFrameArgs begin_frame_args = cc::BeginFrameArgs::Create(
1774 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
1775 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
1776 scheduler_->WillBeginFrame(begin_frame_args);
1777 ASSERT_TRUE(scheduler_->BeginMainFrameOnCriticalPath());
1779 begin_frame_args.on_critical_path = false;
1780 scheduler_->WillBeginFrame(begin_frame_args);
1781 ASSERT_FALSE(scheduler_->BeginMainFrameOnCriticalPath());
1784 TEST_F(RendererSchedulerImplTest, ShutdownPreventsPostingOfNewTasks) {
1785 scheduler_->Shutdown();
1786 std::vector<std::string> run_order;
1787 PostTestTasks(&run_order, "D1 C1");
1788 RunUntilIdle();
1789 EXPECT_TRUE(run_order.empty());
1792 } // namespace scheduler