Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / scheduler / renderer / renderer_scheduler_impl_unittest.cc
blob09a8438f10f80b1134b3571596803813cf9cb9c5
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 // Meta events like TapDown/FlingCancel shouldn't affect the priority.
722 run_order.clear();
723 scheduler_->DidHandleInputEventOnCompositorThread(
724 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel),
725 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
726 scheduler_->DidHandleInputEventOnCompositorThread(
727 FakeInputEvent(blink::WebInputEvent::GestureTapDown),
728 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
729 RunUntilIdle();
730 EXPECT_TRUE(run_order.empty());
732 // Action events like ScrollBegin will kick us back into compositor priority,
733 // allowing service of the timer, loading and idle queues.
734 run_order.clear();
735 scheduler_->DidHandleInputEventOnCompositorThread(
736 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
737 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
738 RunUntilIdle();
740 EXPECT_THAT(run_order,
741 testing::ElementsAre(std::string("L1"), std::string("T1"),
742 std::string("T2")));
745 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicy_MainThread) {
746 std::vector<std::string> run_order;
747 PostTestTasks(&run_order, "L1 D1 C1 D2 C2 T1 T2");
749 // Observation of touchstart should defer execution of timer, idle and loading
750 // tasks.
751 scheduler_->DidHandleInputEventOnCompositorThread(
752 FakeInputEvent(blink::WebInputEvent::TouchStart),
753 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
754 scheduler_->DidHandleInputEventOnMainThread(
755 FakeInputEvent(blink::WebInputEvent::TouchStart));
756 EnableIdleTasks();
757 RunUntilIdle();
758 EXPECT_THAT(run_order,
759 testing::ElementsAre(std::string("C1"), std::string("C2"),
760 std::string("D1"), std::string("D2")));
762 // Meta events like TapDown/FlingCancel shouldn't affect the priority.
763 run_order.clear();
764 scheduler_->DidHandleInputEventOnCompositorThread(
765 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel),
766 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
767 scheduler_->DidHandleInputEventOnMainThread(
768 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel));
769 scheduler_->DidHandleInputEventOnCompositorThread(
770 FakeInputEvent(blink::WebInputEvent::GestureTapDown),
771 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
772 scheduler_->DidHandleInputEventOnMainThread(
773 FakeInputEvent(blink::WebInputEvent::GestureTapDown));
774 RunUntilIdle();
775 EXPECT_TRUE(run_order.empty());
777 // Action events like ScrollBegin will kick us back into compositor priority,
778 // allowing service of the timer, loading and idle queues.
779 run_order.clear();
780 scheduler_->DidHandleInputEventOnCompositorThread(
781 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
782 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
783 scheduler_->DidHandleInputEventOnMainThread(
784 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin));
785 RunUntilIdle();
787 EXPECT_THAT(run_order,
788 testing::ElementsAre(std::string("L1"), std::string("T1"),
789 std::string("T2")));
792 TEST_F(RendererSchedulerImplTest, LoadingPriorityPolicy) {
793 std::vector<std::string> run_order;
794 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
796 scheduler_->OnPageLoadStarted();
797 EnableIdleTasks();
798 RunUntilIdle();
799 // In loading policy compositor tasks are best effort and should be run last.
800 EXPECT_THAT(run_order,
801 testing::ElementsAre(std::string("L1"), std::string("D1"),
802 std::string("D2"), std::string("I1"),
803 std::string("C1"), std::string("C2")));
805 // Advance 1.5s and try again, the loading policy should have ended and the
806 // task order should return to normal.
807 clock_->Advance(base::TimeDelta::FromMilliseconds(1500));
808 run_order.clear();
809 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
810 EnableIdleTasks();
811 RunUntilIdle();
812 EXPECT_THAT(run_order,
813 testing::ElementsAre(std::string("L1"), std::string("D1"),
814 std::string("C1"), std::string("D2"),
815 std::string("C2"), std::string("I1")));
818 TEST_F(RendererSchedulerImplTest,
819 EventConsumedOnCompositorThread_IgnoresMouseMove_WhenMouseUp) {
820 std::vector<std::string> run_order;
821 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
823 scheduler_->DidHandleInputEventOnCompositorThread(
824 FakeInputEvent(blink::WebInputEvent::MouseMove),
825 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
826 EnableIdleTasks();
827 RunUntilIdle();
828 // Note compositor tasks are not prioritized.
829 EXPECT_THAT(run_order,
830 testing::ElementsAre(std::string("D1"), std::string("C1"),
831 std::string("D2"), std::string("C2"),
832 std::string("I1")));
835 TEST_F(RendererSchedulerImplTest,
836 EventForwardedToMainThread_IgnoresMouseMove_WhenMouseUp) {
837 std::vector<std::string> run_order;
838 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
840 scheduler_->DidHandleInputEventOnCompositorThread(
841 FakeInputEvent(blink::WebInputEvent::MouseMove),
842 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
843 EnableIdleTasks();
844 RunUntilIdle();
845 // Note compositor tasks are not prioritized.
846 EXPECT_THAT(run_order,
847 testing::ElementsAre(std::string("D1"), std::string("C1"),
848 std::string("D2"), std::string("C2"),
849 std::string("I1")));
850 scheduler_->DidHandleInputEventOnMainThread(
851 FakeInputEvent(blink::WebInputEvent::MouseMove));
854 TEST_F(RendererSchedulerImplTest,
855 EventConsumedOnCompositorThread_MouseMove_WhenMouseDown) {
856 std::vector<std::string> run_order;
857 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
859 scheduler_->DidHandleInputEventOnCompositorThread(
860 FakeInputEvent(blink::WebInputEvent::MouseMove,
861 blink::WebInputEvent::LeftButtonDown),
862 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
863 EnableIdleTasks();
864 RunUntilIdle();
865 // Note compositor tasks are prioritized.
866 EXPECT_THAT(run_order,
867 testing::ElementsAre(std::string("C1"), std::string("C2"),
868 std::string("D1"), std::string("D2"),
869 std::string("I1")));
872 TEST_F(RendererSchedulerImplTest,
873 EventForwardedToMainThread_MouseMove_WhenMouseDown) {
874 std::vector<std::string> run_order;
875 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
877 scheduler_->DidHandleInputEventOnCompositorThread(
878 FakeInputEvent(blink::WebInputEvent::MouseMove,
879 blink::WebInputEvent::LeftButtonDown),
880 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
881 EnableIdleTasks();
882 RunUntilIdle();
883 // Note compositor tasks are prioritized.
884 EXPECT_THAT(run_order,
885 testing::ElementsAre(std::string("C1"), std::string("C2"),
886 std::string("D1"), std::string("D2"),
887 std::string("I1")));
888 scheduler_->DidHandleInputEventOnMainThread(FakeInputEvent(
889 blink::WebInputEvent::MouseMove, blink::WebInputEvent::LeftButtonDown));
892 TEST_F(RendererSchedulerImplTest, EventConsumedOnCompositorThread_MouseWheel) {
893 std::vector<std::string> run_order;
894 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
896 scheduler_->DidHandleInputEventOnCompositorThread(
897 FakeInputEvent(blink::WebInputEvent::MouseWheel),
898 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
899 EnableIdleTasks();
900 RunUntilIdle();
901 // Note compositor tasks are prioritized.
902 EXPECT_THAT(run_order,
903 testing::ElementsAre(std::string("C1"), std::string("C2"),
904 std::string("D1"), std::string("D2"),
905 std::string("I1")));
908 TEST_F(RendererSchedulerImplTest, EventForwardedToMainThread_MouseWheel) {
909 std::vector<std::string> run_order;
910 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
912 scheduler_->DidHandleInputEventOnCompositorThread(
913 FakeInputEvent(blink::WebInputEvent::MouseWheel),
914 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
915 EnableIdleTasks();
916 RunUntilIdle();
917 // Note compositor tasks are prioritized.
918 EXPECT_THAT(run_order,
919 testing::ElementsAre(std::string("C1"), std::string("C2"),
920 std::string("D1"), std::string("D2"),
921 std::string("I1")));
922 scheduler_->DidHandleInputEventOnMainThread(
923 FakeInputEvent(blink::WebInputEvent::MouseWheel));
926 TEST_F(RendererSchedulerImplTest,
927 EventConsumedOnCompositorThread_IgnoresKeyboardEvents) {
928 std::vector<std::string> run_order;
929 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
931 scheduler_->DidHandleInputEventOnCompositorThread(
932 FakeInputEvent(blink::WebInputEvent::KeyDown),
933 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
934 EnableIdleTasks();
935 RunUntilIdle();
936 // Note compositor tasks are not prioritized.
937 EXPECT_THAT(run_order,
938 testing::ElementsAre(std::string("D1"), std::string("C1"),
939 std::string("D2"), std::string("C2"),
940 std::string("I1")));
943 TEST_F(RendererSchedulerImplTest,
944 EventForwardedToMainThread_IgnoresKeyboardEvents) {
945 std::vector<std::string> run_order;
946 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
948 scheduler_->DidHandleInputEventOnCompositorThread(
949 FakeInputEvent(blink::WebInputEvent::KeyDown),
950 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
951 EnableIdleTasks();
952 RunUntilIdle();
953 // Note compositor tasks are not prioritized.
954 EXPECT_THAT(run_order,
955 testing::ElementsAre(std::string("D1"), std::string("C1"),
956 std::string("D2"), std::string("C2"),
957 std::string("I1")));
958 scheduler_->DidHandleInputEventOnMainThread(
959 FakeInputEvent(blink::WebInputEvent::KeyDown));
962 TEST_F(RendererSchedulerImplTest,
963 TestCompositorPolicyDoesNotStarveDefaultTasks) {
964 std::vector<std::string> run_order;
965 PostTestTasks(&run_order, "D1 C1");
967 for (int i = 0; i < 20; i++) {
968 compositor_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
970 PostTestTasks(&run_order, "C2");
972 scheduler_->DidHandleInputEventOnCompositorThread(
973 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
974 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
975 RunUntilIdle();
976 // Ensure that the default D1 task gets to run at some point before the final
977 // C2 compositor task.
978 EXPECT_THAT(run_order,
979 testing::ElementsAre(std::string("C1"), std::string("D1"),
980 std::string("C2")));
983 TEST_F(RendererSchedulerImplTest,
984 TestCompositorPolicyEnds_CompositorHandlesInput) {
985 std::vector<std::string> run_order;
986 PostTestTasks(&run_order, "D1 C1 D2 C2");
988 scheduler_->DidHandleInputEventOnCompositorThread(
989 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
990 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
991 RunUntilIdle();
992 EXPECT_THAT(run_order,
993 testing::ElementsAre(std::string("C1"), std::string("C2"),
994 std::string("D1"), std::string("D2")));
996 run_order.clear();
997 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
998 PostTestTasks(&run_order, "D1 C1 D2 C2");
1000 // Compositor policy mode should have ended now that the clock has advanced.
1001 RunUntilIdle();
1002 EXPECT_THAT(run_order,
1003 testing::ElementsAre(std::string("D1"), std::string("C1"),
1004 std::string("D2"), std::string("C2")));
1007 TEST_F(RendererSchedulerImplTest,
1008 TestCompositorPolicyEnds_MainThreadHandlesInput) {
1009 std::vector<std::string> run_order;
1010 PostTestTasks(&run_order, "D1 C1 D2 C2");
1012 scheduler_->DidHandleInputEventOnCompositorThread(
1013 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1014 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1015 scheduler_->DidHandleInputEventOnMainThread(
1016 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1017 RunUntilIdle();
1018 EXPECT_THAT(run_order,
1019 testing::ElementsAre(std::string("C1"), std::string("C2"),
1020 std::string("D1"), std::string("D2")));
1022 run_order.clear();
1023 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1024 PostTestTasks(&run_order, "D1 C1 D2 C2");
1026 // Compositor policy mode should have ended now that the clock has advanced.
1027 RunUntilIdle();
1028 EXPECT_THAT(run_order,
1029 testing::ElementsAre(std::string("D1"), std::string("C1"),
1030 std::string("D2"), std::string("C2")));
1033 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicyEndsAfterTimeout) {
1034 std::vector<std::string> run_order;
1035 PostTestTasks(&run_order, "L1 D1 C1 D2 C2");
1037 scheduler_->DidHandleInputEventOnCompositorThread(
1038 FakeInputEvent(blink::WebInputEvent::TouchStart),
1039 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1040 RunUntilIdle();
1041 EXPECT_THAT(run_order,
1042 testing::ElementsAre(std::string("C1"), std::string("C2"),
1043 std::string("D1"), std::string("D2")));
1045 run_order.clear();
1046 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1048 // Don't post any compositor tasks to simulate a very long running event
1049 // handler.
1050 PostTestTasks(&run_order, "D1 D2");
1052 // Touchstart policy mode should have ended now that the clock has advanced.
1053 RunUntilIdle();
1054 EXPECT_THAT(run_order,
1055 testing::ElementsAre(std::string("L1"), std::string("D1"),
1056 std::string("D2")));
1059 TEST_F(RendererSchedulerImplTest,
1060 TestTouchstartPolicyEndsAfterConsecutiveTouchmoves) {
1061 std::vector<std::string> run_order;
1062 PostTestTasks(&run_order, "L1 D1 C1 D2 C2");
1064 // Observation of touchstart should defer execution of idle and loading tasks.
1065 scheduler_->DidHandleInputEventOnCompositorThread(
1066 FakeInputEvent(blink::WebInputEvent::TouchStart),
1067 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1068 RunUntilIdle();
1069 EXPECT_THAT(run_order,
1070 testing::ElementsAre(std::string("C1"), std::string("C2"),
1071 std::string("D1"), std::string("D2")));
1073 // Receiving the first touchmove will not affect scheduler priority.
1074 run_order.clear();
1075 scheduler_->DidHandleInputEventOnCompositorThread(
1076 FakeInputEvent(blink::WebInputEvent::TouchMove),
1077 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1078 RunUntilIdle();
1079 EXPECT_TRUE(run_order.empty());
1081 // Receiving the second touchmove will kick us back into compositor priority.
1082 run_order.clear();
1083 scheduler_->DidHandleInputEventOnCompositorThread(
1084 FakeInputEvent(blink::WebInputEvent::TouchMove),
1085 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1086 RunUntilIdle();
1087 EXPECT_THAT(run_order, testing::ElementsAre(std::string("L1")));
1090 TEST_F(RendererSchedulerImplTest, TestIsHighPriorityWorkAnticipated) {
1091 bool is_anticipated_before = false;
1092 bool is_anticipated_after = false;
1094 bool simulate_input = false;
1095 default_task_runner_->PostTask(
1096 FROM_HERE,
1097 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1098 &is_anticipated_before, &is_anticipated_after));
1099 RunUntilIdle();
1100 // In its default state, without input receipt, the scheduler should indicate
1101 // that no high-priority is anticipated.
1102 EXPECT_FALSE(is_anticipated_before);
1103 EXPECT_FALSE(is_anticipated_after);
1105 simulate_input = true;
1106 default_task_runner_->PostTask(
1107 FROM_HERE,
1108 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1109 &is_anticipated_before, &is_anticipated_after));
1110 RunUntilIdle();
1111 // When input is received, the scheduler should indicate that high-priority
1112 // work is anticipated.
1113 EXPECT_FALSE(is_anticipated_before);
1114 EXPECT_TRUE(is_anticipated_after);
1116 clock_->Advance(priority_escalation_after_input_duration() * 2);
1117 simulate_input = false;
1118 default_task_runner_->PostTask(
1119 FROM_HERE,
1120 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1121 &is_anticipated_before, &is_anticipated_after));
1122 RunUntilIdle();
1123 // Without additional input, the scheduler should indicate that high-priority
1124 // work is no longer anticipated.
1125 EXPECT_FALSE(is_anticipated_before);
1126 EXPECT_FALSE(is_anticipated_after);
1129 TEST_F(RendererSchedulerImplTest, TestShouldYield) {
1130 bool should_yield_before = false;
1131 bool should_yield_after = false;
1133 default_task_runner_->PostTask(
1134 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1135 default_task_runner_, false, &should_yield_before,
1136 &should_yield_after));
1137 RunUntilIdle();
1138 // Posting to default runner shouldn't cause yielding.
1139 EXPECT_FALSE(should_yield_before);
1140 EXPECT_FALSE(should_yield_after);
1142 default_task_runner_->PostTask(
1143 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1144 compositor_task_runner_, false,
1145 &should_yield_before, &should_yield_after));
1146 RunUntilIdle();
1147 // Posting while not in compositor priority shouldn't cause yielding.
1148 EXPECT_FALSE(should_yield_before);
1149 EXPECT_FALSE(should_yield_after);
1151 default_task_runner_->PostTask(
1152 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1153 compositor_task_runner_, true, &should_yield_before,
1154 &should_yield_after));
1155 RunUntilIdle();
1156 // We should be able to switch to compositor priority mid-task.
1157 EXPECT_FALSE(should_yield_before);
1158 EXPECT_TRUE(should_yield_after);
1160 // Receiving a touchstart should immediately trigger yielding, even if
1161 // there's no immediately pending work in the compositor queue.
1162 EXPECT_FALSE(scheduler_->ShouldYieldForHighPriorityWork());
1163 scheduler_->DidHandleInputEventOnCompositorThread(
1164 FakeInputEvent(blink::WebInputEvent::TouchStart),
1165 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1166 EXPECT_TRUE(scheduler_->ShouldYieldForHighPriorityWork());
1167 RunUntilIdle();
1170 TEST_F(RendererSchedulerImplTest, SlowMainThreadInputEvent) {
1171 EXPECT_EQ(Policy::NORMAL, CurrentPolicy());
1173 // An input event should bump us into input priority.
1174 scheduler_->DidHandleInputEventOnCompositorThread(
1175 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1176 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1177 RunUntilIdle();
1178 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy());
1180 // Simulate the input event being queued for a very long time. The compositor
1181 // task we post here represents the enqueued input task.
1182 clock_->Advance(priority_escalation_after_input_duration() * 2);
1183 scheduler_->DidHandleInputEventOnMainThread(
1184 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1185 RunUntilIdle();
1187 // Even though we exceeded the input priority escalation period, we should
1188 // still be in compositor priority since the input remains queued.
1189 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy());
1191 // After the escalation period ends we should go back into normal mode.
1192 clock_->Advance(priority_escalation_after_input_duration() * 2);
1193 RunUntilIdle();
1194 EXPECT_EQ(Policy::NORMAL, CurrentPolicy());
1197 class RendererSchedulerImplWithMockSchedulerTest
1198 : public RendererSchedulerImplTest {
1199 public:
1200 void SetUp() override {
1201 mock_task_runner_ = make_scoped_refptr(
1202 new cc::OrderedSimpleTaskRunner(clock_.get(), false));
1203 main_task_runner_ =
1204 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_);
1205 mock_scheduler_ = new RendererSchedulerImplForTest(main_task_runner_);
1206 Initialize(make_scoped_ptr(mock_scheduler_));
1209 protected:
1210 RendererSchedulerImplForTest* mock_scheduler_;
1213 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1214 OnlyOnePendingUrgentPolicyUpdatey) {
1215 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1216 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1217 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1218 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1220 RunUntilIdle();
1222 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1225 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1226 OnePendingDelayedAndOneUrgentUpdatePolicy) {
1227 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1229 mock_scheduler_->ScheduleDelayedPolicyUpdate(
1230 clock_->NowTicks(), base::TimeDelta::FromMilliseconds(1));
1231 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1233 RunUntilIdle();
1235 // We expect both the urgent and the delayed updates to run.
1236 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1239 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1240 OneUrgentAndOnePendingDelayedUpdatePolicy) {
1241 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1243 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1244 mock_scheduler_->ScheduleDelayedPolicyUpdate(
1245 clock_->NowTicks(), base::TimeDelta::FromMilliseconds(1));
1247 RunUntilIdle();
1249 // We expect both the urgent and the delayed updates to run.
1250 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1253 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1254 UpdatePolicyCountTriggeredByOneInputEvent) {
1255 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1256 // update.
1257 scheduler_->DidHandleInputEventOnCompositorThread(
1258 FakeInputEvent(blink::WebInputEvent::TouchStart),
1259 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1260 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1261 mock_task_runner_->RunPendingTasks();
1262 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1264 scheduler_->DidHandleInputEventOnMainThread(
1265 FakeInputEvent(blink::WebInputEvent::TouchStart));
1266 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1268 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1269 RunUntilIdle();
1271 // We finally expect a delayed policy update 100ms later.
1272 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1275 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1276 UpdatePolicyCountTriggeredByThreeInputEvents) {
1277 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1278 // update.
1279 scheduler_->DidHandleInputEventOnCompositorThread(
1280 FakeInputEvent(blink::WebInputEvent::TouchStart),
1281 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1282 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1283 mock_task_runner_->RunPendingTasks();
1284 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1286 scheduler_->DidHandleInputEventOnMainThread(
1287 FakeInputEvent(blink::WebInputEvent::TouchStart));
1288 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1290 // The second call to DidHandleInputEventOnCompositorThread should not post a
1291 // policy update because we are already in compositor priority.
1292 scheduler_->DidHandleInputEventOnCompositorThread(
1293 FakeInputEvent(blink::WebInputEvent::TouchMove),
1294 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1295 mock_task_runner_->RunPendingTasks();
1296 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1298 // We expect DidHandleInputEvent to trigger a policy update.
1299 scheduler_->DidHandleInputEventOnMainThread(
1300 FakeInputEvent(blink::WebInputEvent::TouchMove));
1301 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1303 // The third call to DidHandleInputEventOnCompositorThread should post a
1304 // policy update because the awaiting_touch_start_response_ flag changed.
1305 scheduler_->DidHandleInputEventOnCompositorThread(
1306 FakeInputEvent(blink::WebInputEvent::TouchMove),
1307 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1308 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1309 mock_task_runner_->RunPendingTasks();
1310 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1312 // We expect DidHandleInputEvent to trigger a policy update.
1313 scheduler_->DidHandleInputEventOnMainThread(
1314 FakeInputEvent(blink::WebInputEvent::TouchMove));
1315 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1317 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1318 RunUntilIdle();
1320 // We finally expect a delayed policy update.
1321 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1324 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1325 UpdatePolicyCountTriggeredByTwoInputEventsWithALongSeparatingDelay) {
1326 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1327 // update.
1328 scheduler_->DidHandleInputEventOnCompositorThread(
1329 FakeInputEvent(blink::WebInputEvent::TouchStart),
1330 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1331 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1332 mock_task_runner_->RunPendingTasks();
1333 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1335 scheduler_->DidHandleInputEventOnMainThread(
1336 FakeInputEvent(blink::WebInputEvent::TouchStart));
1337 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1339 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1340 RunUntilIdle();
1341 // We expect a delayed policy update.
1342 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1344 // We expect the second call to DidHandleInputEventOnCompositorThread to post
1345 // an urgent policy update because we are no longer in compositor priority.
1346 scheduler_->DidHandleInputEventOnCompositorThread(
1347 FakeInputEvent(blink::WebInputEvent::TouchMove),
1348 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1349 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1350 mock_task_runner_->RunPendingTasks();
1351 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1353 scheduler_->DidHandleInputEventOnMainThread(
1354 FakeInputEvent(blink::WebInputEvent::TouchMove));
1355 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1357 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1358 RunUntilIdle();
1360 // We finally expect a delayed policy update.
1361 EXPECT_EQ(4, mock_scheduler_->update_policy_count_);
1364 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1365 EnsureUpdatePolicyNotTriggeredTooOften) {
1366 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1368 scheduler_->DidHandleInputEventOnCompositorThread(
1369 FakeInputEvent(blink::WebInputEvent::TouchStart),
1370 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1371 scheduler_->DidHandleInputEventOnCompositorThread(
1372 FakeInputEvent(blink::WebInputEvent::TouchMove),
1373 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1375 // We expect the first call to IsHighPriorityWorkAnticipated to be called
1376 // after recieving an input event (but before the UpdateTask was processed) to
1377 // call UpdatePolicy.
1378 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1379 scheduler_->IsHighPriorityWorkAnticipated();
1380 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1381 // Subsequent calls should not call UpdatePolicy.
1382 scheduler_->IsHighPriorityWorkAnticipated();
1383 scheduler_->IsHighPriorityWorkAnticipated();
1384 scheduler_->IsHighPriorityWorkAnticipated();
1385 scheduler_->ShouldYieldForHighPriorityWork();
1386 scheduler_->ShouldYieldForHighPriorityWork();
1387 scheduler_->ShouldYieldForHighPriorityWork();
1388 scheduler_->ShouldYieldForHighPriorityWork();
1390 scheduler_->DidHandleInputEventOnMainThread(
1391 FakeInputEvent(blink::WebInputEvent::TouchStart));
1392 scheduler_->DidHandleInputEventOnMainThread(
1393 FakeInputEvent(blink::WebInputEvent::TouchMove));
1395 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1397 RunUntilIdle();
1398 // We expect both the urgent and the delayed updates to run in addition to the
1399 // earlier updated cause by IsHighPriorityWorkAnticipated.
1400 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1403 class RendererSchedulerImplWithMessageLoopTest
1404 : public RendererSchedulerImplTest {
1405 public:
1406 RendererSchedulerImplWithMessageLoopTest()
1407 : RendererSchedulerImplTest(new base::MessageLoop()) {}
1408 ~RendererSchedulerImplWithMessageLoopTest() override {}
1410 void PostFromNestedRunloop(std::vector<
1411 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) {
1412 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get());
1413 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) {
1414 if (pair.second) {
1415 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first);
1416 } else {
1417 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first);
1420 EnableIdleTasks();
1421 message_loop_->RunUntilIdle();
1424 private:
1425 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplWithMessageLoopTest);
1428 TEST_F(RendererSchedulerImplWithMessageLoopTest,
1429 NonNestableIdleTaskDoesntExecuteInNestedLoop) {
1430 std::vector<std::string> order;
1431 idle_task_runner_->PostIdleTask(
1432 FROM_HERE,
1433 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("1")));
1434 idle_task_runner_->PostIdleTask(
1435 FROM_HERE,
1436 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("2")));
1438 std::vector<std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>
1439 tasks_to_post_from_nested_loop;
1440 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1441 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("3")),
1442 false));
1443 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1444 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("4")), true));
1445 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1446 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true));
1448 default_task_runner_->PostTask(
1449 FROM_HERE,
1450 base::Bind(
1451 &RendererSchedulerImplWithMessageLoopTest::PostFromNestedRunloop,
1452 base::Unretained(this),
1453 base::Unretained(&tasks_to_post_from_nested_loop)));
1455 EnableIdleTasks();
1456 RunUntilIdle();
1457 // Note we expect task 3 to run last because it's non-nestable.
1458 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"),
1459 std::string("4"), std::string("5"),
1460 std::string("3")));
1463 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriod) {
1464 base::TimeTicks expected_deadline =
1465 clock_->NowTicks() + maximum_idle_period_duration();
1466 base::TimeTicks deadline_in_task;
1467 int run_count = 0;
1469 idle_task_runner_->PostIdleTask(
1470 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1472 RunUntilIdle();
1473 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period.
1475 scheduler_->BeginFrameNotExpectedSoon();
1476 RunUntilIdle();
1477 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
1478 EXPECT_EQ(expected_deadline, deadline_in_task);
1481 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodWithPendingDelayedTask) {
1482 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30);
1483 base::TimeTicks expected_deadline = clock_->NowTicks() + pending_task_delay;
1484 base::TimeTicks deadline_in_task;
1485 int run_count = 0;
1487 idle_task_runner_->PostIdleTask(
1488 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1489 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1490 pending_task_delay);
1492 scheduler_->BeginFrameNotExpectedSoon();
1493 RunUntilIdle();
1494 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
1495 EXPECT_EQ(expected_deadline, deadline_in_task);
1498 TEST_F(RendererSchedulerImplTest,
1499 TestLongIdlePeriodWithLatePendingDelayedTask) {
1500 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10);
1501 base::TimeTicks deadline_in_task;
1502 int run_count = 0;
1504 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1505 pending_task_delay);
1507 // Advance clock until after delayed task was meant to be run.
1508 clock_->Advance(base::TimeDelta::FromMilliseconds(20));
1510 // Post an idle task and BeginFrameNotExpectedSoon to initiate a long idle
1511 // period. Since there is a late pending delayed task this shouldn't actually
1512 // start an idle period.
1513 idle_task_runner_->PostIdleTask(
1514 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1515 scheduler_->BeginFrameNotExpectedSoon();
1516 RunUntilIdle();
1517 EXPECT_EQ(0, run_count);
1519 // After the delayed task has been run we should trigger an idle period.
1520 clock_->Advance(maximum_idle_period_duration());
1521 RunUntilIdle();
1522 EXPECT_EQ(1, run_count);
1525 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodRepeating) {
1526 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1527 std::vector<base::TimeTicks> actual_deadlines;
1528 int run_count = 0;
1530 max_idle_task_reposts = 3;
1531 base::TimeTicks clock_before(clock_->NowTicks());
1532 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10));
1533 idle_task_runner_->PostIdleTask(
1534 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
1535 idle_task_runner_, &run_count, clock_.get(),
1536 idle_task_runtime, &actual_deadlines));
1537 scheduler_->BeginFrameNotExpectedSoon();
1538 RunUntilIdle();
1539 EXPECT_EQ(3, run_count);
1540 EXPECT_THAT(
1541 actual_deadlines,
1542 testing::ElementsAre(
1543 clock_before + maximum_idle_period_duration(),
1544 clock_before + idle_task_runtime + maximum_idle_period_duration(),
1545 clock_before + (2 * idle_task_runtime) +
1546 maximum_idle_period_duration()));
1548 // Check that idle tasks don't run after the idle period ends with a
1549 // new BeginMainFrame.
1550 max_idle_task_reposts = 5;
1551 idle_task_runner_->PostIdleTask(
1552 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
1553 idle_task_runner_, &run_count, clock_.get(),
1554 idle_task_runtime, &actual_deadlines));
1555 idle_task_runner_->PostIdleTask(
1556 FROM_HERE, base::Bind(&WillBeginFrameIdleTask,
1557 base::Unretained(scheduler_.get()), clock_.get()));
1558 RunUntilIdle();
1559 EXPECT_EQ(4, run_count);
1562 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodDoesNotWakeScheduler) {
1563 base::TimeTicks deadline_in_task;
1564 int run_count = 0;
1566 // Start a long idle period and get the time it should end.
1567 scheduler_->BeginFrameNotExpectedSoon();
1568 // The scheduler should not run the initiate_next_long_idle_period task if
1569 // there are no idle tasks and no other task woke up the scheduler, thus
1570 // the idle period deadline shouldn't update at the end of the current long
1571 // idle period.
1572 base::TimeTicks idle_period_deadline =
1573 scheduler_->CurrentIdleTaskDeadlineForTesting();
1574 clock_->Advance(maximum_idle_period_duration());
1575 RunUntilIdle();
1577 base::TimeTicks new_idle_period_deadline =
1578 scheduler_->CurrentIdleTaskDeadlineForTesting();
1579 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
1581 // Posting a after-wakeup idle task also shouldn't wake the scheduler or
1582 // initiate the next long idle period.
1583 idle_task_runner_->PostIdleTaskAfterWakeup(
1584 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1585 RunUntilIdle();
1586 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
1587 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
1588 EXPECT_EQ(0, run_count);
1590 // Running a normal task should initiate a new long idle period though.
1591 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
1592 RunUntilIdle();
1593 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
1594 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(),
1595 new_idle_period_deadline);
1597 EXPECT_EQ(1, run_count);
1600 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodInTouchStartPolicy) {
1601 base::TimeTicks deadline_in_task;
1602 int run_count = 0;
1604 idle_task_runner_->PostIdleTask(
1605 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1607 // Observation of touchstart should defer the start of the long idle period.
1608 scheduler_->DidHandleInputEventOnCompositorThread(
1609 FakeInputEvent(blink::WebInputEvent::TouchStart),
1610 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1611 scheduler_->BeginFrameNotExpectedSoon();
1612 RunUntilIdle();
1613 EXPECT_EQ(0, run_count);
1615 // The long idle period should start after the touchstart policy has finished.
1616 clock_->Advance(priority_escalation_after_input_duration());
1617 RunUntilIdle();
1618 EXPECT_EQ(1, run_count);
1621 void TestCanExceedIdleDeadlineIfRequiredTask(RendererScheduler* scheduler,
1622 bool* can_exceed_idle_deadline_out,
1623 int* run_count,
1624 base::TimeTicks deadline) {
1625 *can_exceed_idle_deadline_out = scheduler->CanExceedIdleDeadlineIfRequired();
1626 (*run_count)++;
1629 TEST_F(RendererSchedulerImplTest, CanExceedIdleDeadlineIfRequired) {
1630 int run_count = 0;
1631 bool can_exceed_idle_deadline = false;
1633 // Should return false if not in an idle period.
1634 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired());
1636 // Should return false for short idle periods.
1637 idle_task_runner_->PostIdleTask(
1638 FROM_HERE,
1639 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1640 &can_exceed_idle_deadline, &run_count));
1641 EnableIdleTasks();
1642 RunUntilIdle();
1643 EXPECT_EQ(1, run_count);
1644 EXPECT_FALSE(can_exceed_idle_deadline);
1646 // Should return false for a long idle period which is shortened due to a
1647 // pending delayed task.
1648 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1649 base::TimeDelta::FromMilliseconds(10));
1650 idle_task_runner_->PostIdleTask(
1651 FROM_HERE,
1652 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1653 &can_exceed_idle_deadline, &run_count));
1654 scheduler_->BeginFrameNotExpectedSoon();
1655 RunUntilIdle();
1656 EXPECT_EQ(2, run_count);
1657 EXPECT_FALSE(can_exceed_idle_deadline);
1659 // Next long idle period will be for the maximum time, so
1660 // CanExceedIdleDeadlineIfRequired should return true.
1661 clock_->Advance(maximum_idle_period_duration());
1662 idle_task_runner_->PostIdleTask(
1663 FROM_HERE,
1664 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1665 &can_exceed_idle_deadline, &run_count));
1666 RunUntilIdle();
1667 EXPECT_EQ(3, run_count);
1668 EXPECT_TRUE(can_exceed_idle_deadline);
1670 // Next long idle period will be for the maximum time, so
1671 // CanExceedIdleDeadlineIfRequired should return true.
1672 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
1673 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
1674 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
1675 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired());
1678 TEST_F(RendererSchedulerImplTest, TestRendererHiddenIdlePeriod) {
1679 int run_count = 0;
1681 max_idle_task_reposts = 2;
1682 idle_task_runner_->PostIdleTask(
1683 FROM_HERE,
1684 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
1686 // Renderer should start in visible state.
1687 RunUntilIdle();
1688 EXPECT_EQ(0, run_count);
1690 // When we hide the renderer it should start a max deadline idle period, which
1691 // will run an idle task and then immediately start a new idle period, which
1692 // runs the second idle task.
1693 scheduler_->OnRendererHidden();
1694 RunUntilIdle();
1695 EXPECT_EQ(2, run_count);
1697 // Advance time by amount of time by the maximum amount of time we execute
1698 // idle tasks when hidden (plus some slack) - idle period should have ended.
1699 max_idle_task_reposts = 3;
1700 idle_task_runner_->PostIdleTask(
1701 FROM_HERE,
1702 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
1703 clock_->Advance(end_idle_when_hidden_delay() +
1704 base::TimeDelta::FromMilliseconds(10));
1705 RunUntilIdle();
1706 EXPECT_EQ(2, run_count);
1709 TEST_F(RendererSchedulerImplTest, TimerQueueEnabledByDefault) {
1710 std::vector<std::string> run_order;
1711 PostTestTasks(&run_order, "T1 T2");
1712 RunUntilIdle();
1713 EXPECT_THAT(run_order,
1714 testing::ElementsAre(std::string("T1"), std::string("T2")));
1717 TEST_F(RendererSchedulerImplTest, SuspendAndResumeTimerQueue) {
1718 std::vector<std::string> run_order;
1719 PostTestTasks(&run_order, "T1 T2");
1721 scheduler_->SuspendTimerQueue();
1722 RunUntilIdle();
1723 EXPECT_TRUE(run_order.empty());
1725 scheduler_->ResumeTimerQueue();
1726 RunUntilIdle();
1727 EXPECT_THAT(run_order,
1728 testing::ElementsAre(std::string("T1"), std::string("T2")));
1731 TEST_F(RendererSchedulerImplTest, MultipleSuspendsNeedMultipleResumes) {
1732 std::vector<std::string> run_order;
1733 PostTestTasks(&run_order, "T1 T2");
1735 scheduler_->SuspendTimerQueue();
1736 scheduler_->SuspendTimerQueue();
1737 scheduler_->SuspendTimerQueue();
1738 RunUntilIdle();
1739 EXPECT_TRUE(run_order.empty());
1741 scheduler_->ResumeTimerQueue();
1742 RunUntilIdle();
1743 EXPECT_TRUE(run_order.empty());
1745 scheduler_->ResumeTimerQueue();
1746 RunUntilIdle();
1747 EXPECT_TRUE(run_order.empty());
1749 scheduler_->ResumeTimerQueue();
1750 RunUntilIdle();
1751 EXPECT_THAT(run_order,
1752 testing::ElementsAre(std::string("T1"), std::string("T2")));
1755 TEST_F(RendererSchedulerImplTest, PolicyToString) {
1756 CheckAllPolicyToString();
1759 TEST_F(RendererSchedulerImplTest, MismatchedDidHandleInputEventOnMainThread) {
1760 // This should not DCHECK because there was no corresponding compositor side
1761 // call to DidHandleInputEventOnCompositorThread with
1762 // INPUT_EVENT_ACK_STATE_NOT_CONSUMED. There are legitimate reasons for the
1763 // compositor to not be there and we don't want to make debugging impossible.
1764 scheduler_->DidHandleInputEventOnMainThread(
1765 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1768 TEST_F(RendererSchedulerImplTest, BeginMainFrameOnCriticalPath) {
1769 ASSERT_FALSE(scheduler_->BeginMainFrameOnCriticalPath());
1771 cc::BeginFrameArgs begin_frame_args = cc::BeginFrameArgs::Create(
1772 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
1773 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
1774 scheduler_->WillBeginFrame(begin_frame_args);
1775 ASSERT_TRUE(scheduler_->BeginMainFrameOnCriticalPath());
1777 begin_frame_args.on_critical_path = false;
1778 scheduler_->WillBeginFrame(begin_frame_args);
1779 ASSERT_FALSE(scheduler_->BeginMainFrameOnCriticalPath());
1782 TEST_F(RendererSchedulerImplTest, ShutdownPreventsPostingOfNewTasks) {
1783 scheduler_->Shutdown();
1784 std::vector<std::string> run_order;
1785 PostTestTasks(&run_order, "D1 C1");
1786 RunUntilIdle();
1787 EXPECT_TRUE(run_order.empty());
1790 } // namespace scheduler