cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / scheduler / renderer / renderer_scheduler_impl_unittest.cc
blobf34f2f01fb524c18ca836e10c31361a7b985785f
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 static base::TimeDelta suspend_timers_when_backgrounded_delay() {
328 return base::TimeDelta::FromMilliseconds(
329 RendererSchedulerImpl::kSuspendTimersWhenBackgroundedDelayMillis);
332 template <typename E>
333 static void CallForEachEnumValue(E first,
334 E last,
335 const char* (*function)(E)) {
336 for (E val = first; val < last;
337 val = static_cast<E>(static_cast<int>(val) + 1)) {
338 (*function)(val);
342 static void CheckAllPolicyToString() {
343 CallForEachEnumValue<RendererSchedulerImpl::Policy>(
344 RendererSchedulerImpl::Policy::FIRST_POLICY,
345 RendererSchedulerImpl::Policy::POLICY_COUNT,
346 &RendererSchedulerImpl::PolicyToString);
349 scoped_ptr<base::SimpleTestTickClock> clock_;
350 // Only one of mock_task_runner_ or message_loop_ will be set.
351 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
352 scoped_ptr<base::MessageLoop> message_loop_;
354 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner_;
355 scoped_ptr<RendererSchedulerImplForTest> scheduler_;
356 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
357 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
358 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_;
359 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
360 scoped_refptr<base::SingleThreadTaskRunner> timer_task_runner_;
362 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplTest);
365 TEST_F(RendererSchedulerImplTest, TestPostDefaultTask) {
366 std::vector<std::string> run_order;
367 PostTestTasks(&run_order, "D1 D2 D3 D4");
369 RunUntilIdle();
370 EXPECT_THAT(run_order,
371 testing::ElementsAre(std::string("D1"), std::string("D2"),
372 std::string("D3"), std::string("D4")));
375 TEST_F(RendererSchedulerImplTest, TestPostDefaultAndCompositor) {
376 std::vector<std::string> run_order;
377 PostTestTasks(&run_order, "D1 C1");
378 RunUntilIdle();
379 EXPECT_THAT(run_order, testing::Contains("D1"));
380 EXPECT_THAT(run_order, testing::Contains("C1"));
383 TEST_F(RendererSchedulerImplTest, TestRentrantTask) {
384 int count = 0;
385 std::vector<int> run_order;
386 default_task_runner_->PostTask(
387 FROM_HERE, base::Bind(AppendToVectorReentrantTask, default_task_runner_,
388 &run_order, &count, 5));
389 RunUntilIdle();
391 EXPECT_THAT(run_order, testing::ElementsAre(0, 1, 2, 3, 4));
394 TEST_F(RendererSchedulerImplTest, TestPostIdleTask) {
395 int run_count = 0;
396 base::TimeTicks expected_deadline =
397 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(2300);
398 base::TimeTicks deadline_in_task;
400 clock_->Advance(base::TimeDelta::FromMilliseconds(100));
401 idle_task_runner_->PostIdleTask(
402 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
404 RunUntilIdle();
405 EXPECT_EQ(0, run_count); // Shouldn't run yet as no WillBeginFrame.
407 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
408 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
409 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
410 RunUntilIdle();
411 EXPECT_EQ(0, run_count); // Shouldn't run as no DidCommitFrameToCompositor.
413 clock_->Advance(base::TimeDelta::FromMilliseconds(1200));
414 scheduler_->DidCommitFrameToCompositor();
415 RunUntilIdle();
416 EXPECT_EQ(0, run_count); // We missed the deadline.
418 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
419 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
420 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
421 clock_->Advance(base::TimeDelta::FromMilliseconds(800));
422 scheduler_->DidCommitFrameToCompositor();
423 RunUntilIdle();
424 EXPECT_EQ(1, run_count);
425 EXPECT_EQ(expected_deadline, deadline_in_task);
428 TEST_F(RendererSchedulerImplTest, TestRepostingIdleTask) {
429 int run_count = 0;
431 max_idle_task_reposts = 2;
432 idle_task_runner_->PostIdleTask(
433 FROM_HERE,
434 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
435 EnableIdleTasks();
436 RunUntilIdle();
437 EXPECT_EQ(1, run_count);
439 // Reposted tasks shouldn't run until next idle period.
440 RunUntilIdle();
441 EXPECT_EQ(1, run_count);
443 EnableIdleTasks();
444 RunUntilIdle();
445 EXPECT_EQ(2, run_count);
448 TEST_F(RendererSchedulerImplTest, TestIdleTaskExceedsDeadline) {
449 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
450 int run_count = 0;
452 // Post two UpdateClockToDeadlineIdleTestTask tasks.
453 idle_task_runner_->PostIdleTask(
454 FROM_HERE,
455 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_.get(), &run_count));
456 idle_task_runner_->PostIdleTask(
457 FROM_HERE,
458 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_.get(), &run_count));
460 EnableIdleTasks();
461 RunUntilIdle();
462 // Only the first idle task should execute since it's used up the deadline.
463 EXPECT_EQ(1, run_count);
465 EnableIdleTasks();
466 RunUntilIdle();
467 // Second task should be run on the next idle period.
468 EXPECT_EQ(2, run_count);
471 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskAfterWakeup) {
472 base::TimeTicks deadline_in_task;
473 int run_count = 0;
475 idle_task_runner_->PostIdleTaskAfterWakeup(
476 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
478 EnableIdleTasks();
479 RunUntilIdle();
480 // Shouldn't run yet as no other task woke up the scheduler.
481 EXPECT_EQ(0, run_count);
483 idle_task_runner_->PostIdleTaskAfterWakeup(
484 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
486 EnableIdleTasks();
487 RunUntilIdle();
488 // Another after wakeup idle task shouldn't wake the scheduler.
489 EXPECT_EQ(0, run_count);
491 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
493 RunUntilIdle();
494 EnableIdleTasks(); // Must start a new idle period before idle task runs.
495 RunUntilIdle();
496 // Execution of default task queue task should trigger execution of idle task.
497 EXPECT_EQ(2, run_count);
500 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskAfterWakeupWhileAwake) {
501 base::TimeTicks deadline_in_task;
502 int run_count = 0;
504 idle_task_runner_->PostIdleTaskAfterWakeup(
505 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
506 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
508 RunUntilIdle();
509 EnableIdleTasks(); // Must start a new idle period before idle task runs.
510 RunUntilIdle();
511 // Should run as the scheduler was already awakened by the normal task.
512 EXPECT_EQ(1, run_count);
515 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskWakesAfterWakeupIdleTask) {
516 base::TimeTicks deadline_in_task;
517 int run_count = 0;
519 idle_task_runner_->PostIdleTaskAfterWakeup(
520 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
521 idle_task_runner_->PostIdleTask(
522 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
524 EnableIdleTasks();
525 RunUntilIdle();
526 // Must start a new idle period before after-wakeup idle task runs.
527 EnableIdleTasks();
528 RunUntilIdle();
529 // Normal idle task should wake up after-wakeup idle task.
530 EXPECT_EQ(2, run_count);
533 TEST_F(RendererSchedulerImplTest, TestDelayedEndIdlePeriodCanceled) {
534 int run_count = 0;
536 base::TimeTicks deadline_in_task;
537 idle_task_runner_->PostIdleTask(
538 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
540 // Trigger the beginning of an idle period for 1000ms.
541 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
542 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
543 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
544 DoMainFrame();
546 // End the idle period early (after 500ms), and send a WillBeginFrame which
547 // specifies that the next idle period should end 1000ms from now.
548 clock_->Advance(base::TimeDelta::FromMilliseconds(500));
549 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
550 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
551 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
553 RunUntilIdle();
554 EXPECT_EQ(0, run_count); // Not currently in an idle period.
556 // Trigger the start of the idle period before the task to end the previous
557 // idle period has been triggered.
558 clock_->Advance(base::TimeDelta::FromMilliseconds(400));
559 scheduler_->DidCommitFrameToCompositor();
561 // Post a task which simulates running until after the previous end idle
562 // period delayed task was scheduled for
563 scheduler_->DefaultTaskRunner()->PostTask(FROM_HERE, base::Bind(NullTask));
564 clock_->Advance(base::TimeDelta::FromMilliseconds(300));
566 RunUntilIdle();
567 EXPECT_EQ(1, run_count); // We should still be in the new idle period.
570 TEST_F(RendererSchedulerImplTest, TestDefaultPolicy) {
571 std::vector<std::string> run_order;
572 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
574 EnableIdleTasks();
575 RunUntilIdle();
576 EXPECT_THAT(run_order,
577 testing::ElementsAre(std::string("L1"), std::string("D1"),
578 std::string("C1"), std::string("D2"),
579 std::string("C2"), std::string("I1")));
582 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_CompositorHandlesInput) {
583 std::vector<std::string> run_order;
584 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
586 scheduler_->DidHandleInputEventOnCompositorThread(
587 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
588 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
589 EnableIdleTasks();
590 RunUntilIdle();
591 EXPECT_THAT(run_order,
592 testing::ElementsAre(std::string("C1"), std::string("C2"),
593 std::string("L1"), std::string("D1"),
594 std::string("D2"), std::string("I1")));
597 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_MainThreadHandlesInput) {
598 std::vector<std::string> run_order;
599 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
601 scheduler_->DidHandleInputEventOnCompositorThread(
602 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
603 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
604 EnableIdleTasks();
605 RunUntilIdle();
606 EXPECT_THAT(run_order,
607 testing::ElementsAre(std::string("C1"), std::string("C2"),
608 std::string("L1"), std::string("D1"),
609 std::string("D2"), std::string("I1")));
610 scheduler_->DidHandleInputEventOnMainThread(
611 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
614 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_DidAnimateForInput) {
615 std::vector<std::string> run_order;
616 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
618 scheduler_->DidAnimateForInputOnCompositorThread();
619 EnableIdleTasks();
620 RunUntilIdle();
621 EXPECT_THAT(run_order,
622 testing::ElementsAre(std::string("C1"), std::string("C2"),
623 std::string("D1"), std::string("D2"),
624 std::string("I1")));
627 TEST_F(
628 RendererSchedulerImplTest,
629 TestCompositorPolicy_ExpensiveTimersDontRunWhenMainThreadOnCriticalPath) {
630 std::vector<std::string> run_order;
632 // Simulate a bunch of expensive timer tasks
633 for (int i = 0; i < 10; i++) {
634 timer_task_runner_->PostTask(
635 FROM_HERE, base::Bind(&base::SimpleTestTickClock::Advance,
636 base::Unretained(clock_.get()),
637 base::TimeDelta::FromMilliseconds(500)));
639 RunUntilIdle();
641 // Timers should now be disabled during main thread user userinteractions.
642 PostTestTasks(&run_order, "C1 T1");
644 scheduler_->DidAnimateForInputOnCompositorThread();
645 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
646 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
647 base::TimeDelta::FromMilliseconds(16), cc::BeginFrameArgs::NORMAL));
648 RunUntilIdle();
650 EXPECT_THAT(run_order, testing::ElementsAre(std::string("C1")));
651 clock_->Advance(priority_escalation_after_input_duration() * 2);
653 run_order.clear();
654 RunUntilIdle();
655 EXPECT_THAT(run_order, testing::ElementsAre(std::string("T1")));
658 TEST_F(RendererSchedulerImplTest,
659 TestCompositorPolicy_TimersAlwaysRunIfNoRecentIdlePeriod) {
660 std::vector<std::string> run_order;
661 PostTestTasks(&run_order, "C1 T1");
663 // Simulate no recent idle period.
664 clock_->Advance(idle_period_starvation_threshold() * 2);
666 scheduler_->DidAnimateForInputOnCompositorThread();
667 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
668 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
669 base::TimeDelta::FromMilliseconds(16), cc::BeginFrameArgs::NORMAL));
671 RunUntilIdle();
673 EXPECT_THAT(run_order,
674 testing::ElementsAre(std::string("C1"), std::string("T1")));
677 TEST_F(RendererSchedulerImplTest,
678 TestCompositorPolicy_TimersAlwaysRun_MainThreadNotOnCriticalPath) {
679 std::vector<std::string> run_order;
680 PostTestTasks(&run_order, "C1 T1");
682 scheduler_->DidAnimateForInputOnCompositorThread();
683 cc::BeginFrameArgs begin_frame_args1 = cc::BeginFrameArgs::Create(
684 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
685 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
686 begin_frame_args1.on_critical_path = false;
687 scheduler_->WillBeginFrame(begin_frame_args1);
688 scheduler_->DidCommitFrameToCompositor(); // Starts Idle Period
689 RunUntilIdle();
691 EXPECT_THAT(run_order,
692 testing::ElementsAre(std::string("C1"), std::string("T1")));
694 // End the idle period.
695 clock_->Advance(base::TimeDelta::FromMilliseconds(500));
696 scheduler_->DidAnimateForInputOnCompositorThread();
697 cc::BeginFrameArgs begin_frame_args2 = cc::BeginFrameArgs::Create(
698 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
699 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
700 begin_frame_args2.on_critical_path = false;
701 scheduler_->WillBeginFrame(begin_frame_args2);
703 run_order.clear();
704 PostTestTasks(&run_order, "C1 T1");
705 RunUntilIdle();
707 EXPECT_THAT(run_order,
708 testing::ElementsAre(std::string("C1"), std::string("T1")));
711 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicy_Compositor) {
712 std::vector<std::string> run_order;
713 PostTestTasks(&run_order, "L1 D1 C1 D2 C2 T1 T2");
715 // Observation of touchstart should defer execution of timer, idle and loading
716 // tasks.
717 scheduler_->DidHandleInputEventOnCompositorThread(
718 FakeInputEvent(blink::WebInputEvent::TouchStart),
719 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
720 EnableIdleTasks();
721 RunUntilIdle();
722 EXPECT_THAT(run_order,
723 testing::ElementsAre(std::string("C1"), std::string("C2"),
724 std::string("D1"), std::string("D2")));
726 // Animation or meta events like TapDown/FlingCancel shouldn't affect the
727 // priority.
728 run_order.clear();
729 scheduler_->DidAnimateForInputOnCompositorThread();
730 scheduler_->DidHandleInputEventOnCompositorThread(
731 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel),
732 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
733 scheduler_->DidHandleInputEventOnCompositorThread(
734 FakeInputEvent(blink::WebInputEvent::GestureTapDown),
735 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
736 RunUntilIdle();
737 EXPECT_TRUE(run_order.empty());
739 // Action events like ScrollBegin will kick us back into compositor priority,
740 // allowing service of the timer, loading and idle queues.
741 run_order.clear();
742 scheduler_->DidHandleInputEventOnCompositorThread(
743 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
744 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
745 RunUntilIdle();
747 EXPECT_THAT(run_order,
748 testing::ElementsAre(std::string("L1"), std::string("T1"),
749 std::string("T2")));
752 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicy_MainThread) {
753 std::vector<std::string> run_order;
754 PostTestTasks(&run_order, "L1 D1 C1 D2 C2 T1 T2");
756 // Observation of touchstart should defer execution of timer, idle and loading
757 // tasks.
758 scheduler_->DidHandleInputEventOnCompositorThread(
759 FakeInputEvent(blink::WebInputEvent::TouchStart),
760 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
761 scheduler_->DidHandleInputEventOnMainThread(
762 FakeInputEvent(blink::WebInputEvent::TouchStart));
763 EnableIdleTasks();
764 RunUntilIdle();
765 EXPECT_THAT(run_order,
766 testing::ElementsAre(std::string("C1"), std::string("C2"),
767 std::string("D1"), std::string("D2")));
769 // Meta events like TapDown/FlingCancel shouldn't affect the priority.
770 run_order.clear();
771 scheduler_->DidHandleInputEventOnCompositorThread(
772 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel),
773 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
774 scheduler_->DidHandleInputEventOnMainThread(
775 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel));
776 scheduler_->DidHandleInputEventOnCompositorThread(
777 FakeInputEvent(blink::WebInputEvent::GestureTapDown),
778 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
779 scheduler_->DidHandleInputEventOnMainThread(
780 FakeInputEvent(blink::WebInputEvent::GestureTapDown));
781 RunUntilIdle();
782 EXPECT_TRUE(run_order.empty());
784 // Action events like ScrollBegin will kick us back into compositor priority,
785 // allowing service of the timer, loading and idle queues.
786 run_order.clear();
787 scheduler_->DidHandleInputEventOnCompositorThread(
788 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
789 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
790 scheduler_->DidHandleInputEventOnMainThread(
791 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin));
792 RunUntilIdle();
794 EXPECT_THAT(run_order,
795 testing::ElementsAre(std::string("L1"), std::string("T1"),
796 std::string("T2")));
799 TEST_F(RendererSchedulerImplTest, LoadingPriorityPolicy) {
800 std::vector<std::string> run_order;
801 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
803 scheduler_->OnPageLoadStarted();
804 EnableIdleTasks();
805 RunUntilIdle();
806 // In loading policy compositor tasks are best effort and should be run last.
807 EXPECT_THAT(run_order,
808 testing::ElementsAre(std::string("L1"), std::string("D1"),
809 std::string("D2"), std::string("I1"),
810 std::string("C1"), std::string("C2")));
812 // Advance 1.5s and try again, the loading policy should have ended and the
813 // task order should return to normal.
814 clock_->Advance(base::TimeDelta::FromMilliseconds(1500));
815 run_order.clear();
816 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
817 EnableIdleTasks();
818 RunUntilIdle();
819 EXPECT_THAT(run_order,
820 testing::ElementsAre(std::string("L1"), std::string("D1"),
821 std::string("C1"), std::string("D2"),
822 std::string("C2"), std::string("I1")));
825 TEST_F(RendererSchedulerImplTest,
826 EventConsumedOnCompositorThread_IgnoresMouseMove_WhenMouseUp) {
827 std::vector<std::string> run_order;
828 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
830 scheduler_->DidHandleInputEventOnCompositorThread(
831 FakeInputEvent(blink::WebInputEvent::MouseMove),
832 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
833 EnableIdleTasks();
834 RunUntilIdle();
835 // Note compositor tasks are not prioritized.
836 EXPECT_THAT(run_order,
837 testing::ElementsAre(std::string("D1"), std::string("C1"),
838 std::string("D2"), std::string("C2"),
839 std::string("I1")));
842 TEST_F(RendererSchedulerImplTest,
843 EventForwardedToMainThread_IgnoresMouseMove_WhenMouseUp) {
844 std::vector<std::string> run_order;
845 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
847 scheduler_->DidHandleInputEventOnCompositorThread(
848 FakeInputEvent(blink::WebInputEvent::MouseMove),
849 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
850 EnableIdleTasks();
851 RunUntilIdle();
852 // Note compositor tasks are not prioritized.
853 EXPECT_THAT(run_order,
854 testing::ElementsAre(std::string("D1"), std::string("C1"),
855 std::string("D2"), std::string("C2"),
856 std::string("I1")));
857 scheduler_->DidHandleInputEventOnMainThread(
858 FakeInputEvent(blink::WebInputEvent::MouseMove));
861 TEST_F(RendererSchedulerImplTest,
862 EventConsumedOnCompositorThread_MouseMove_WhenMouseDown) {
863 std::vector<std::string> run_order;
864 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
866 scheduler_->DidHandleInputEventOnCompositorThread(
867 FakeInputEvent(blink::WebInputEvent::MouseMove,
868 blink::WebInputEvent::LeftButtonDown),
869 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
870 EnableIdleTasks();
871 RunUntilIdle();
872 // Note compositor tasks are prioritized.
873 EXPECT_THAT(run_order,
874 testing::ElementsAre(std::string("C1"), std::string("C2"),
875 std::string("D1"), std::string("D2"),
876 std::string("I1")));
879 TEST_F(RendererSchedulerImplTest,
880 EventForwardedToMainThread_MouseMove_WhenMouseDown) {
881 std::vector<std::string> run_order;
882 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
884 scheduler_->DidHandleInputEventOnCompositorThread(
885 FakeInputEvent(blink::WebInputEvent::MouseMove,
886 blink::WebInputEvent::LeftButtonDown),
887 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
888 EnableIdleTasks();
889 RunUntilIdle();
890 // Note compositor tasks are prioritized.
891 EXPECT_THAT(run_order,
892 testing::ElementsAre(std::string("C1"), std::string("C2"),
893 std::string("D1"), std::string("D2"),
894 std::string("I1")));
895 scheduler_->DidHandleInputEventOnMainThread(FakeInputEvent(
896 blink::WebInputEvent::MouseMove, blink::WebInputEvent::LeftButtonDown));
899 TEST_F(RendererSchedulerImplTest, EventConsumedOnCompositorThread_MouseWheel) {
900 std::vector<std::string> run_order;
901 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
903 scheduler_->DidHandleInputEventOnCompositorThread(
904 FakeInputEvent(blink::WebInputEvent::MouseWheel),
905 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
906 EnableIdleTasks();
907 RunUntilIdle();
908 // Note compositor tasks are prioritized.
909 EXPECT_THAT(run_order,
910 testing::ElementsAre(std::string("C1"), std::string("C2"),
911 std::string("D1"), std::string("D2"),
912 std::string("I1")));
915 TEST_F(RendererSchedulerImplTest, EventForwardedToMainThread_MouseWheel) {
916 std::vector<std::string> run_order;
917 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
919 scheduler_->DidHandleInputEventOnCompositorThread(
920 FakeInputEvent(blink::WebInputEvent::MouseWheel),
921 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
922 EnableIdleTasks();
923 RunUntilIdle();
924 // Note compositor tasks are prioritized.
925 EXPECT_THAT(run_order,
926 testing::ElementsAre(std::string("C1"), std::string("C2"),
927 std::string("D1"), std::string("D2"),
928 std::string("I1")));
929 scheduler_->DidHandleInputEventOnMainThread(
930 FakeInputEvent(blink::WebInputEvent::MouseWheel));
933 TEST_F(RendererSchedulerImplTest,
934 EventConsumedOnCompositorThread_IgnoresKeyboardEvents) {
935 std::vector<std::string> run_order;
936 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
938 scheduler_->DidHandleInputEventOnCompositorThread(
939 FakeInputEvent(blink::WebInputEvent::KeyDown),
940 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
941 EnableIdleTasks();
942 RunUntilIdle();
943 // Note compositor tasks are not prioritized.
944 EXPECT_THAT(run_order,
945 testing::ElementsAre(std::string("D1"), std::string("C1"),
946 std::string("D2"), std::string("C2"),
947 std::string("I1")));
950 TEST_F(RendererSchedulerImplTest,
951 EventForwardedToMainThread_IgnoresKeyboardEvents) {
952 std::vector<std::string> run_order;
953 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
955 scheduler_->DidHandleInputEventOnCompositorThread(
956 FakeInputEvent(blink::WebInputEvent::KeyDown),
957 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
958 EnableIdleTasks();
959 RunUntilIdle();
960 // Note compositor tasks are not prioritized.
961 EXPECT_THAT(run_order,
962 testing::ElementsAre(std::string("D1"), std::string("C1"),
963 std::string("D2"), std::string("C2"),
964 std::string("I1")));
965 scheduler_->DidHandleInputEventOnMainThread(
966 FakeInputEvent(blink::WebInputEvent::KeyDown));
969 TEST_F(RendererSchedulerImplTest,
970 TestCompositorPolicyDoesNotStarveDefaultTasks) {
971 std::vector<std::string> run_order;
972 PostTestTasks(&run_order, "D1 C1");
974 for (int i = 0; i < 20; i++) {
975 compositor_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
977 PostTestTasks(&run_order, "C2");
979 scheduler_->DidHandleInputEventOnCompositorThread(
980 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
981 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
982 RunUntilIdle();
983 // Ensure that the default D1 task gets to run at some point before the final
984 // C2 compositor task.
985 EXPECT_THAT(run_order,
986 testing::ElementsAre(std::string("C1"), std::string("D1"),
987 std::string("C2")));
990 TEST_F(RendererSchedulerImplTest,
991 TestCompositorPolicyEnds_CompositorHandlesInput) {
992 std::vector<std::string> run_order;
993 PostTestTasks(&run_order, "D1 C1 D2 C2");
995 scheduler_->DidHandleInputEventOnCompositorThread(
996 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
997 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
998 RunUntilIdle();
999 EXPECT_THAT(run_order,
1000 testing::ElementsAre(std::string("C1"), std::string("C2"),
1001 std::string("D1"), std::string("D2")));
1003 run_order.clear();
1004 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1005 PostTestTasks(&run_order, "D1 C1 D2 C2");
1007 // Compositor policy mode should have ended now that the clock has advanced.
1008 RunUntilIdle();
1009 EXPECT_THAT(run_order,
1010 testing::ElementsAre(std::string("D1"), std::string("C1"),
1011 std::string("D2"), std::string("C2")));
1014 TEST_F(RendererSchedulerImplTest,
1015 TestCompositorPolicyEnds_MainThreadHandlesInput) {
1016 std::vector<std::string> run_order;
1017 PostTestTasks(&run_order, "D1 C1 D2 C2");
1019 scheduler_->DidHandleInputEventOnCompositorThread(
1020 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1021 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1022 scheduler_->DidHandleInputEventOnMainThread(
1023 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1024 RunUntilIdle();
1025 EXPECT_THAT(run_order,
1026 testing::ElementsAre(std::string("C1"), std::string("C2"),
1027 std::string("D1"), std::string("D2")));
1029 run_order.clear();
1030 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1031 PostTestTasks(&run_order, "D1 C1 D2 C2");
1033 // Compositor policy mode should have ended now that the clock has advanced.
1034 RunUntilIdle();
1035 EXPECT_THAT(run_order,
1036 testing::ElementsAre(std::string("D1"), std::string("C1"),
1037 std::string("D2"), std::string("C2")));
1040 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicyEndsAfterTimeout) {
1041 std::vector<std::string> run_order;
1042 PostTestTasks(&run_order, "L1 D1 C1 D2 C2");
1044 scheduler_->DidHandleInputEventOnCompositorThread(
1045 FakeInputEvent(blink::WebInputEvent::TouchStart),
1046 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1047 RunUntilIdle();
1048 EXPECT_THAT(run_order,
1049 testing::ElementsAre(std::string("C1"), std::string("C2"),
1050 std::string("D1"), std::string("D2")));
1052 run_order.clear();
1053 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1055 // Don't post any compositor tasks to simulate a very long running event
1056 // handler.
1057 PostTestTasks(&run_order, "D1 D2");
1059 // Touchstart policy mode should have ended now that the clock has advanced.
1060 RunUntilIdle();
1061 EXPECT_THAT(run_order,
1062 testing::ElementsAre(std::string("L1"), std::string("D1"),
1063 std::string("D2")));
1066 TEST_F(RendererSchedulerImplTest,
1067 TestTouchstartPolicyEndsAfterConsecutiveTouchmoves) {
1068 std::vector<std::string> run_order;
1069 PostTestTasks(&run_order, "L1 D1 C1 D2 C2");
1071 // Observation of touchstart should defer execution of idle and loading tasks.
1072 scheduler_->DidHandleInputEventOnCompositorThread(
1073 FakeInputEvent(blink::WebInputEvent::TouchStart),
1074 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1075 RunUntilIdle();
1076 EXPECT_THAT(run_order,
1077 testing::ElementsAre(std::string("C1"), std::string("C2"),
1078 std::string("D1"), std::string("D2")));
1080 // Receiving the first touchmove will not affect scheduler priority.
1081 run_order.clear();
1082 scheduler_->DidHandleInputEventOnCompositorThread(
1083 FakeInputEvent(blink::WebInputEvent::TouchMove),
1084 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1085 RunUntilIdle();
1086 EXPECT_TRUE(run_order.empty());
1088 // Receiving the second touchmove will kick us back into compositor priority.
1089 run_order.clear();
1090 scheduler_->DidHandleInputEventOnCompositorThread(
1091 FakeInputEvent(blink::WebInputEvent::TouchMove),
1092 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1093 RunUntilIdle();
1094 EXPECT_THAT(run_order, testing::ElementsAre(std::string("L1")));
1097 TEST_F(RendererSchedulerImplTest, TestIsHighPriorityWorkAnticipated) {
1098 bool is_anticipated_before = false;
1099 bool is_anticipated_after = false;
1101 bool simulate_input = false;
1102 default_task_runner_->PostTask(
1103 FROM_HERE,
1104 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1105 &is_anticipated_before, &is_anticipated_after));
1106 RunUntilIdle();
1107 // In its default state, without input receipt, the scheduler should indicate
1108 // that no high-priority is anticipated.
1109 EXPECT_FALSE(is_anticipated_before);
1110 EXPECT_FALSE(is_anticipated_after);
1112 simulate_input = true;
1113 default_task_runner_->PostTask(
1114 FROM_HERE,
1115 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1116 &is_anticipated_before, &is_anticipated_after));
1117 RunUntilIdle();
1118 // When input is received, the scheduler should indicate that high-priority
1119 // work is anticipated.
1120 EXPECT_FALSE(is_anticipated_before);
1121 EXPECT_TRUE(is_anticipated_after);
1123 clock_->Advance(priority_escalation_after_input_duration() * 2);
1124 simulate_input = false;
1125 default_task_runner_->PostTask(
1126 FROM_HERE,
1127 base::Bind(&AnticipationTestTask, scheduler_.get(), simulate_input,
1128 &is_anticipated_before, &is_anticipated_after));
1129 RunUntilIdle();
1130 // Without additional input, the scheduler should indicate that high-priority
1131 // work is no longer anticipated.
1132 EXPECT_FALSE(is_anticipated_before);
1133 EXPECT_FALSE(is_anticipated_after);
1136 TEST_F(RendererSchedulerImplTest, TestShouldYield) {
1137 bool should_yield_before = false;
1138 bool should_yield_after = false;
1140 default_task_runner_->PostTask(
1141 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1142 default_task_runner_, false, &should_yield_before,
1143 &should_yield_after));
1144 RunUntilIdle();
1145 // Posting to default runner shouldn't cause yielding.
1146 EXPECT_FALSE(should_yield_before);
1147 EXPECT_FALSE(should_yield_after);
1149 default_task_runner_->PostTask(
1150 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1151 compositor_task_runner_, false,
1152 &should_yield_before, &should_yield_after));
1153 RunUntilIdle();
1154 // Posting while not in compositor priority shouldn't cause yielding.
1155 EXPECT_FALSE(should_yield_before);
1156 EXPECT_FALSE(should_yield_after);
1158 default_task_runner_->PostTask(
1159 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1160 compositor_task_runner_, true, &should_yield_before,
1161 &should_yield_after));
1162 RunUntilIdle();
1163 // We should be able to switch to compositor priority mid-task.
1164 EXPECT_FALSE(should_yield_before);
1165 EXPECT_TRUE(should_yield_after);
1167 // Receiving a touchstart should immediately trigger yielding, even if
1168 // there's no immediately pending work in the compositor queue.
1169 EXPECT_FALSE(scheduler_->ShouldYieldForHighPriorityWork());
1170 scheduler_->DidHandleInputEventOnCompositorThread(
1171 FakeInputEvent(blink::WebInputEvent::TouchStart),
1172 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1173 EXPECT_TRUE(scheduler_->ShouldYieldForHighPriorityWork());
1174 RunUntilIdle();
1177 TEST_F(RendererSchedulerImplTest, SlowMainThreadInputEvent) {
1178 EXPECT_EQ(Policy::NORMAL, CurrentPolicy());
1180 // An input event should bump us into input priority.
1181 scheduler_->DidHandleInputEventOnCompositorThread(
1182 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1183 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1184 RunUntilIdle();
1185 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy());
1187 // Simulate the input event being queued for a very long time. The compositor
1188 // task we post here represents the enqueued input task.
1189 clock_->Advance(priority_escalation_after_input_duration() * 2);
1190 scheduler_->DidHandleInputEventOnMainThread(
1191 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1192 RunUntilIdle();
1194 // Even though we exceeded the input priority escalation period, we should
1195 // still be in compositor priority since the input remains queued.
1196 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy());
1198 // After the escalation period ends we should go back into normal mode.
1199 clock_->Advance(priority_escalation_after_input_duration() * 2);
1200 RunUntilIdle();
1201 EXPECT_EQ(Policy::NORMAL, CurrentPolicy());
1204 class RendererSchedulerImplWithMockSchedulerTest
1205 : public RendererSchedulerImplTest {
1206 public:
1207 void SetUp() override {
1208 mock_task_runner_ = make_scoped_refptr(
1209 new cc::OrderedSimpleTaskRunner(clock_.get(), false));
1210 main_task_runner_ =
1211 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_);
1212 mock_scheduler_ = new RendererSchedulerImplForTest(main_task_runner_);
1213 Initialize(make_scoped_ptr(mock_scheduler_));
1216 protected:
1217 RendererSchedulerImplForTest* mock_scheduler_;
1220 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1221 OnlyOnePendingUrgentPolicyUpdatey) {
1222 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1223 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1224 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1225 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1227 RunUntilIdle();
1229 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1232 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1233 OnePendingDelayedAndOneUrgentUpdatePolicy) {
1234 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1236 mock_scheduler_->ScheduleDelayedPolicyUpdate(
1237 clock_->NowTicks(), base::TimeDelta::FromMilliseconds(1));
1238 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1240 RunUntilIdle();
1242 // We expect both the urgent and the delayed updates to run.
1243 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1246 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1247 OneUrgentAndOnePendingDelayedUpdatePolicy) {
1248 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1250 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1251 mock_scheduler_->ScheduleDelayedPolicyUpdate(
1252 clock_->NowTicks(), base::TimeDelta::FromMilliseconds(1));
1254 RunUntilIdle();
1256 // We expect both the urgent and the delayed updates to run.
1257 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1260 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1261 UpdatePolicyCountTriggeredByOneInputEvent) {
1262 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1263 // update.
1264 scheduler_->DidHandleInputEventOnCompositorThread(
1265 FakeInputEvent(blink::WebInputEvent::TouchStart),
1266 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1267 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1268 mock_task_runner_->RunPendingTasks();
1269 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1271 scheduler_->DidHandleInputEventOnMainThread(
1272 FakeInputEvent(blink::WebInputEvent::TouchStart));
1273 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1275 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1276 RunUntilIdle();
1278 // We finally expect a delayed policy update 100ms later.
1279 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1282 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1283 UpdatePolicyCountTriggeredByThreeInputEvents) {
1284 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1285 // update.
1286 scheduler_->DidHandleInputEventOnCompositorThread(
1287 FakeInputEvent(blink::WebInputEvent::TouchStart),
1288 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1289 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1290 mock_task_runner_->RunPendingTasks();
1291 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1293 scheduler_->DidHandleInputEventOnMainThread(
1294 FakeInputEvent(blink::WebInputEvent::TouchStart));
1295 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1297 // The second call to DidHandleInputEventOnCompositorThread should not post a
1298 // policy update because we are already in compositor priority.
1299 scheduler_->DidHandleInputEventOnCompositorThread(
1300 FakeInputEvent(blink::WebInputEvent::TouchMove),
1301 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1302 mock_task_runner_->RunPendingTasks();
1303 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1305 // We expect DidHandleInputEvent to trigger a policy update.
1306 scheduler_->DidHandleInputEventOnMainThread(
1307 FakeInputEvent(blink::WebInputEvent::TouchMove));
1308 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1310 // The third call to DidHandleInputEventOnCompositorThread should post a
1311 // policy update because the awaiting_touch_start_response_ flag changed.
1312 scheduler_->DidHandleInputEventOnCompositorThread(
1313 FakeInputEvent(blink::WebInputEvent::TouchMove),
1314 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1315 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1316 mock_task_runner_->RunPendingTasks();
1317 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1319 // We expect DidHandleInputEvent to trigger a policy update.
1320 scheduler_->DidHandleInputEventOnMainThread(
1321 FakeInputEvent(blink::WebInputEvent::TouchMove));
1322 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1324 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1325 RunUntilIdle();
1327 // We finally expect a delayed policy update.
1328 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1331 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1332 UpdatePolicyCountTriggeredByTwoInputEventsWithALongSeparatingDelay) {
1333 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1334 // update.
1335 scheduler_->DidHandleInputEventOnCompositorThread(
1336 FakeInputEvent(blink::WebInputEvent::TouchStart),
1337 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1338 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1339 mock_task_runner_->RunPendingTasks();
1340 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1342 scheduler_->DidHandleInputEventOnMainThread(
1343 FakeInputEvent(blink::WebInputEvent::TouchStart));
1344 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1346 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1347 RunUntilIdle();
1348 // We expect a delayed policy update.
1349 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1351 // We expect the second call to DidHandleInputEventOnCompositorThread to post
1352 // an urgent policy update because we are no longer in compositor priority.
1353 scheduler_->DidHandleInputEventOnCompositorThread(
1354 FakeInputEvent(blink::WebInputEvent::TouchMove),
1355 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1356 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1357 mock_task_runner_->RunPendingTasks();
1358 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1360 scheduler_->DidHandleInputEventOnMainThread(
1361 FakeInputEvent(blink::WebInputEvent::TouchMove));
1362 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1364 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1365 RunUntilIdle();
1367 // We finally expect a delayed policy update.
1368 EXPECT_EQ(4, mock_scheduler_->update_policy_count_);
1371 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1372 EnsureUpdatePolicyNotTriggeredTooOften) {
1373 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1375 scheduler_->DidHandleInputEventOnCompositorThread(
1376 FakeInputEvent(blink::WebInputEvent::TouchStart),
1377 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1378 scheduler_->DidHandleInputEventOnCompositorThread(
1379 FakeInputEvent(blink::WebInputEvent::TouchMove),
1380 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1382 // We expect the first call to IsHighPriorityWorkAnticipated to be called
1383 // after recieving an input event (but before the UpdateTask was processed) to
1384 // call UpdatePolicy.
1385 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1386 scheduler_->IsHighPriorityWorkAnticipated();
1387 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1388 // Subsequent calls should not call UpdatePolicy.
1389 scheduler_->IsHighPriorityWorkAnticipated();
1390 scheduler_->IsHighPriorityWorkAnticipated();
1391 scheduler_->IsHighPriorityWorkAnticipated();
1392 scheduler_->ShouldYieldForHighPriorityWork();
1393 scheduler_->ShouldYieldForHighPriorityWork();
1394 scheduler_->ShouldYieldForHighPriorityWork();
1395 scheduler_->ShouldYieldForHighPriorityWork();
1397 scheduler_->DidHandleInputEventOnMainThread(
1398 FakeInputEvent(blink::WebInputEvent::TouchStart));
1399 scheduler_->DidHandleInputEventOnMainThread(
1400 FakeInputEvent(blink::WebInputEvent::TouchMove));
1402 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1404 RunUntilIdle();
1405 // We expect both the urgent and the delayed updates to run in addition to the
1406 // earlier updated cause by IsHighPriorityWorkAnticipated.
1407 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1410 class RendererSchedulerImplWithMessageLoopTest
1411 : public RendererSchedulerImplTest {
1412 public:
1413 RendererSchedulerImplWithMessageLoopTest()
1414 : RendererSchedulerImplTest(new base::MessageLoop()) {}
1415 ~RendererSchedulerImplWithMessageLoopTest() override {}
1417 void PostFromNestedRunloop(std::vector<
1418 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) {
1419 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get());
1420 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) {
1421 if (pair.second) {
1422 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first);
1423 } else {
1424 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first);
1427 EnableIdleTasks();
1428 message_loop_->RunUntilIdle();
1431 private:
1432 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplWithMessageLoopTest);
1435 TEST_F(RendererSchedulerImplWithMessageLoopTest,
1436 NonNestableIdleTaskDoesntExecuteInNestedLoop) {
1437 std::vector<std::string> order;
1438 idle_task_runner_->PostIdleTask(
1439 FROM_HERE,
1440 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("1")));
1441 idle_task_runner_->PostIdleTask(
1442 FROM_HERE,
1443 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("2")));
1445 std::vector<std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>
1446 tasks_to_post_from_nested_loop;
1447 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1448 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("3")),
1449 false));
1450 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1451 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("4")), true));
1452 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1453 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true));
1455 default_task_runner_->PostTask(
1456 FROM_HERE,
1457 base::Bind(
1458 &RendererSchedulerImplWithMessageLoopTest::PostFromNestedRunloop,
1459 base::Unretained(this),
1460 base::Unretained(&tasks_to_post_from_nested_loop)));
1462 EnableIdleTasks();
1463 RunUntilIdle();
1464 // Note we expect task 3 to run last because it's non-nestable.
1465 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"),
1466 std::string("4"), std::string("5"),
1467 std::string("3")));
1470 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriod) {
1471 base::TimeTicks expected_deadline =
1472 clock_->NowTicks() + maximum_idle_period_duration();
1473 base::TimeTicks deadline_in_task;
1474 int run_count = 0;
1476 idle_task_runner_->PostIdleTask(
1477 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1479 RunUntilIdle();
1480 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period.
1482 scheduler_->BeginFrameNotExpectedSoon();
1483 RunUntilIdle();
1484 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
1485 EXPECT_EQ(expected_deadline, deadline_in_task);
1488 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodWithPendingDelayedTask) {
1489 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30);
1490 base::TimeTicks expected_deadline = clock_->NowTicks() + pending_task_delay;
1491 base::TimeTicks deadline_in_task;
1492 int run_count = 0;
1494 idle_task_runner_->PostIdleTask(
1495 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1496 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1497 pending_task_delay);
1499 scheduler_->BeginFrameNotExpectedSoon();
1500 RunUntilIdle();
1501 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
1502 EXPECT_EQ(expected_deadline, deadline_in_task);
1505 TEST_F(RendererSchedulerImplTest,
1506 TestLongIdlePeriodWithLatePendingDelayedTask) {
1507 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10);
1508 base::TimeTicks deadline_in_task;
1509 int run_count = 0;
1511 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1512 pending_task_delay);
1514 // Advance clock until after delayed task was meant to be run.
1515 clock_->Advance(base::TimeDelta::FromMilliseconds(20));
1517 // Post an idle task and BeginFrameNotExpectedSoon to initiate a long idle
1518 // period. Since there is a late pending delayed task this shouldn't actually
1519 // start an idle period.
1520 idle_task_runner_->PostIdleTask(
1521 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1522 scheduler_->BeginFrameNotExpectedSoon();
1523 RunUntilIdle();
1524 EXPECT_EQ(0, run_count);
1526 // After the delayed task has been run we should trigger an idle period.
1527 clock_->Advance(maximum_idle_period_duration());
1528 RunUntilIdle();
1529 EXPECT_EQ(1, run_count);
1532 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodRepeating) {
1533 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1534 std::vector<base::TimeTicks> actual_deadlines;
1535 int run_count = 0;
1537 max_idle_task_reposts = 3;
1538 base::TimeTicks clock_before(clock_->NowTicks());
1539 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10));
1540 idle_task_runner_->PostIdleTask(
1541 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
1542 idle_task_runner_, &run_count, clock_.get(),
1543 idle_task_runtime, &actual_deadlines));
1544 scheduler_->BeginFrameNotExpectedSoon();
1545 RunUntilIdle();
1546 EXPECT_EQ(3, run_count);
1547 EXPECT_THAT(
1548 actual_deadlines,
1549 testing::ElementsAre(
1550 clock_before + maximum_idle_period_duration(),
1551 clock_before + idle_task_runtime + maximum_idle_period_duration(),
1552 clock_before + (2 * idle_task_runtime) +
1553 maximum_idle_period_duration()));
1555 // Check that idle tasks don't run after the idle period ends with a
1556 // new BeginMainFrame.
1557 max_idle_task_reposts = 5;
1558 idle_task_runner_->PostIdleTask(
1559 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
1560 idle_task_runner_, &run_count, clock_.get(),
1561 idle_task_runtime, &actual_deadlines));
1562 idle_task_runner_->PostIdleTask(
1563 FROM_HERE, base::Bind(&WillBeginFrameIdleTask,
1564 base::Unretained(scheduler_.get()), clock_.get()));
1565 RunUntilIdle();
1566 EXPECT_EQ(4, run_count);
1569 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodDoesNotWakeScheduler) {
1570 base::TimeTicks deadline_in_task;
1571 int run_count = 0;
1573 // Start a long idle period and get the time it should end.
1574 scheduler_->BeginFrameNotExpectedSoon();
1575 // The scheduler should not run the initiate_next_long_idle_period task if
1576 // there are no idle tasks and no other task woke up the scheduler, thus
1577 // the idle period deadline shouldn't update at the end of the current long
1578 // idle period.
1579 base::TimeTicks idle_period_deadline =
1580 scheduler_->CurrentIdleTaskDeadlineForTesting();
1581 clock_->Advance(maximum_idle_period_duration());
1582 RunUntilIdle();
1584 base::TimeTicks new_idle_period_deadline =
1585 scheduler_->CurrentIdleTaskDeadlineForTesting();
1586 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
1588 // Posting a after-wakeup idle task also shouldn't wake the scheduler or
1589 // initiate the next long idle period.
1590 idle_task_runner_->PostIdleTaskAfterWakeup(
1591 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1592 RunUntilIdle();
1593 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
1594 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
1595 EXPECT_EQ(0, run_count);
1597 // Running a normal task should initiate a new long idle period though.
1598 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
1599 RunUntilIdle();
1600 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
1601 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(),
1602 new_idle_period_deadline);
1604 EXPECT_EQ(1, run_count);
1607 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodInTouchStartPolicy) {
1608 base::TimeTicks deadline_in_task;
1609 int run_count = 0;
1611 idle_task_runner_->PostIdleTask(
1612 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1614 // Observation of touchstart should defer the start of the long idle period.
1615 scheduler_->DidHandleInputEventOnCompositorThread(
1616 FakeInputEvent(blink::WebInputEvent::TouchStart),
1617 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1618 scheduler_->BeginFrameNotExpectedSoon();
1619 RunUntilIdle();
1620 EXPECT_EQ(0, run_count);
1622 // The long idle period should start after the touchstart policy has finished.
1623 clock_->Advance(priority_escalation_after_input_duration());
1624 RunUntilIdle();
1625 EXPECT_EQ(1, run_count);
1628 void TestCanExceedIdleDeadlineIfRequiredTask(RendererScheduler* scheduler,
1629 bool* can_exceed_idle_deadline_out,
1630 int* run_count,
1631 base::TimeTicks deadline) {
1632 *can_exceed_idle_deadline_out = scheduler->CanExceedIdleDeadlineIfRequired();
1633 (*run_count)++;
1636 TEST_F(RendererSchedulerImplTest, CanExceedIdleDeadlineIfRequired) {
1637 int run_count = 0;
1638 bool can_exceed_idle_deadline = false;
1640 // Should return false if not in an idle period.
1641 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired());
1643 // Should return false for short idle periods.
1644 idle_task_runner_->PostIdleTask(
1645 FROM_HERE,
1646 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1647 &can_exceed_idle_deadline, &run_count));
1648 EnableIdleTasks();
1649 RunUntilIdle();
1650 EXPECT_EQ(1, run_count);
1651 EXPECT_FALSE(can_exceed_idle_deadline);
1653 // Should return false for a long idle period which is shortened due to a
1654 // pending delayed task.
1655 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1656 base::TimeDelta::FromMilliseconds(10));
1657 idle_task_runner_->PostIdleTask(
1658 FROM_HERE,
1659 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1660 &can_exceed_idle_deadline, &run_count));
1661 scheduler_->BeginFrameNotExpectedSoon();
1662 RunUntilIdle();
1663 EXPECT_EQ(2, run_count);
1664 EXPECT_FALSE(can_exceed_idle_deadline);
1666 // Next long idle period will be for the maximum time, so
1667 // CanExceedIdleDeadlineIfRequired should return true.
1668 clock_->Advance(maximum_idle_period_duration());
1669 idle_task_runner_->PostIdleTask(
1670 FROM_HERE,
1671 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1672 &can_exceed_idle_deadline, &run_count));
1673 RunUntilIdle();
1674 EXPECT_EQ(3, run_count);
1675 EXPECT_TRUE(can_exceed_idle_deadline);
1677 // Next long idle period will be for the maximum time, so
1678 // CanExceedIdleDeadlineIfRequired should return true.
1679 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
1680 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
1681 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
1682 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired());
1685 TEST_F(RendererSchedulerImplTest, TestRendererHiddenIdlePeriod) {
1686 int run_count = 0;
1688 max_idle_task_reposts = 2;
1689 idle_task_runner_->PostIdleTask(
1690 FROM_HERE,
1691 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
1693 // Renderer should start in visible state.
1694 RunUntilIdle();
1695 EXPECT_EQ(0, run_count);
1697 // When we hide the renderer it should start a max deadline idle period, which
1698 // will run an idle task and then immediately start a new idle period, which
1699 // runs the second idle task.
1700 scheduler_->OnRendererHidden();
1701 RunUntilIdle();
1702 EXPECT_EQ(2, run_count);
1704 // Advance time by amount of time by the maximum amount of time we execute
1705 // idle tasks when hidden (plus some slack) - idle period should have ended.
1706 max_idle_task_reposts = 3;
1707 idle_task_runner_->PostIdleTask(
1708 FROM_HERE,
1709 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
1710 clock_->Advance(end_idle_when_hidden_delay() +
1711 base::TimeDelta::FromMilliseconds(10));
1712 RunUntilIdle();
1713 EXPECT_EQ(2, run_count);
1716 TEST_F(RendererSchedulerImplTest, TimerQueueEnabledByDefault) {
1717 std::vector<std::string> run_order;
1718 PostTestTasks(&run_order, "T1 T2");
1719 RunUntilIdle();
1720 EXPECT_THAT(run_order,
1721 testing::ElementsAre(std::string("T1"), std::string("T2")));
1724 TEST_F(RendererSchedulerImplTest, SuspendAndResumeTimerQueue) {
1725 std::vector<std::string> run_order;
1726 PostTestTasks(&run_order, "T1 T2");
1728 scheduler_->SuspendTimerQueue();
1729 RunUntilIdle();
1730 EXPECT_TRUE(run_order.empty());
1732 scheduler_->ResumeTimerQueue();
1733 RunUntilIdle();
1734 EXPECT_THAT(run_order,
1735 testing::ElementsAre(std::string("T1"), std::string("T2")));
1738 TEST_F(RendererSchedulerImplTest, MultipleSuspendsNeedMultipleResumes) {
1739 std::vector<std::string> run_order;
1740 PostTestTasks(&run_order, "T1 T2");
1742 scheduler_->SuspendTimerQueue();
1743 scheduler_->SuspendTimerQueue();
1744 scheduler_->SuspendTimerQueue();
1745 RunUntilIdle();
1746 EXPECT_TRUE(run_order.empty());
1748 scheduler_->ResumeTimerQueue();
1749 RunUntilIdle();
1750 EXPECT_TRUE(run_order.empty());
1752 scheduler_->ResumeTimerQueue();
1753 RunUntilIdle();
1754 EXPECT_TRUE(run_order.empty());
1756 scheduler_->ResumeTimerQueue();
1757 RunUntilIdle();
1758 EXPECT_THAT(run_order,
1759 testing::ElementsAre(std::string("T1"), std::string("T2")));
1762 TEST_F(RendererSchedulerImplTest, PolicyToString) {
1763 CheckAllPolicyToString();
1766 TEST_F(RendererSchedulerImplTest, MismatchedDidHandleInputEventOnMainThread) {
1767 // This should not DCHECK because there was no corresponding compositor side
1768 // call to DidHandleInputEventOnCompositorThread with
1769 // INPUT_EVENT_ACK_STATE_NOT_CONSUMED. There are legitimate reasons for the
1770 // compositor to not be there and we don't want to make debugging impossible.
1771 scheduler_->DidHandleInputEventOnMainThread(
1772 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1775 TEST_F(RendererSchedulerImplTest, BeginMainFrameOnCriticalPath) {
1776 ASSERT_FALSE(scheduler_->BeginMainFrameOnCriticalPath());
1778 cc::BeginFrameArgs begin_frame_args = cc::BeginFrameArgs::Create(
1779 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
1780 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
1781 scheduler_->WillBeginFrame(begin_frame_args);
1782 ASSERT_TRUE(scheduler_->BeginMainFrameOnCriticalPath());
1784 begin_frame_args.on_critical_path = false;
1785 scheduler_->WillBeginFrame(begin_frame_args);
1786 ASSERT_FALSE(scheduler_->BeginMainFrameOnCriticalPath());
1789 TEST_F(RendererSchedulerImplTest, ShutdownPreventsPostingOfNewTasks) {
1790 scheduler_->Shutdown();
1791 std::vector<std::string> run_order;
1792 PostTestTasks(&run_order, "D1 C1");
1793 RunUntilIdle();
1794 EXPECT_TRUE(run_order.empty());
1797 TEST_F(RendererSchedulerImplTest, TestRendererBackgroundedTimerSuspension) {
1798 scheduler_->SetTimerQueueSuspensionWhenBackgroundedEnabled(true);
1800 std::vector<std::string> run_order;
1801 PostTestTasks(&run_order, "T1 T2");
1803 // The background signal will not immediately suspend the timer queue.
1804 scheduler_->OnRendererBackgrounded();
1805 RunUntilIdle();
1806 EXPECT_THAT(run_order,
1807 testing::ElementsAre(std::string("T1"), std::string("T2")));
1809 run_order.clear();
1810 PostTestTasks(&run_order, "T3");
1811 RunUntilIdle();
1812 EXPECT_THAT(run_order, testing::ElementsAre(std::string("T3")));
1814 // Advance the time until after the scheduled timer queue suspension.
1815 run_order.clear();
1816 clock_->Advance(suspend_timers_when_backgrounded_delay() +
1817 base::TimeDelta::FromMilliseconds(10));
1818 RunUntilIdle();
1819 ASSERT_TRUE(run_order.empty());
1821 // Timer tasks should be suspended until the foregrounded signal.
1822 PostTestTasks(&run_order, "T4 T5");
1823 RunUntilIdle();
1824 EXPECT_TRUE(run_order.empty());
1826 scheduler_->OnRendererForegrounded();
1827 RunUntilIdle();
1828 EXPECT_THAT(run_order,
1829 testing::ElementsAre(std::string("T4"), std::string("T5")));
1831 // Subsequent timer tasks should fire as usual.
1832 run_order.clear();
1833 PostTestTasks(&run_order, "T6");
1834 RunUntilIdle();
1835 EXPECT_THAT(run_order, testing::ElementsAre(std::string("T6")));
1837 } // namespace scheduler