Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / scheduler / renderer / renderer_scheduler_impl_unittest.cc
blobe0caee0ed8f21a2c84c4db91e3dbb8ce8c8ac513
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 enum class SimulateInputType {
130 None,
131 TouchStart,
132 TouchEnd,
133 GestureScrollBegin,
134 GestureScrollEnd
137 void AnticipationTestTask(RendererSchedulerImpl* scheduler,
138 SimulateInputType simulate_input,
139 bool* is_anticipated_before,
140 bool* is_anticipated_after) {
141 *is_anticipated_before = scheduler->IsHighPriorityWorkAnticipated();
142 switch (simulate_input) {
143 case SimulateInputType::None:
144 break;
146 case SimulateInputType::TouchStart:
147 scheduler->DidHandleInputEventOnCompositorThread(
148 FakeInputEvent(blink::WebInputEvent::TouchStart),
149 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
150 break;
152 case SimulateInputType::TouchEnd:
153 scheduler->DidHandleInputEventOnCompositorThread(
154 FakeInputEvent(blink::WebInputEvent::TouchEnd),
155 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
156 break;
158 case SimulateInputType::GestureScrollBegin:
159 scheduler->DidHandleInputEventOnCompositorThread(
160 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
161 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
162 break;
164 case SimulateInputType::GestureScrollEnd:
165 scheduler->DidHandleInputEventOnCompositorThread(
166 FakeInputEvent(blink::WebInputEvent::GestureScrollEnd),
167 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
168 break;
170 *is_anticipated_after = scheduler->IsHighPriorityWorkAnticipated();
173 }; // namespace
175 class RendererSchedulerImplForTest : public RendererSchedulerImpl {
176 public:
177 using RendererSchedulerImpl::OnIdlePeriodEnded;
178 using RendererSchedulerImpl::OnIdlePeriodStarted;
180 RendererSchedulerImplForTest(
181 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner)
182 : RendererSchedulerImpl(main_task_runner), update_policy_count_(0) {}
184 void UpdatePolicyLocked(UpdateType update_type) override {
185 update_policy_count_++;
186 RendererSchedulerImpl::UpdatePolicyLocked(update_type);
188 std::string use_case =
189 RendererScheduler::UseCaseToString(MainThreadOnly().current_use_case);
190 if (MainThreadOnly().touchstart_expected_soon) {
191 use_cases_.push_back(use_case + " scroll expected");
192 } else {
193 use_cases_.push_back(use_case);
197 void EnsureUrgentPolicyUpdatePostedOnMainThread() {
198 base::AutoLock lock(any_thread_lock_);
199 RendererSchedulerImpl::EnsureUrgentPolicyUpdatePostedOnMainThread(
200 FROM_HERE);
203 void ScheduleDelayedPolicyUpdate(base::TimeTicks now, base::TimeDelta delay) {
204 delayed_update_policy_runner_.SetDeadline(FROM_HERE, delay, now);
207 bool BeginMainFrameOnCriticalPath() {
208 base::AutoLock lock(any_thread_lock_);
209 return AnyThread().begin_main_frame_on_critical_path;
212 int update_policy_count_;
213 std::vector<std::string> use_cases_;
216 // Lets gtest print human readable Policy values.
217 ::std::ostream& operator<<(::std::ostream& os,
218 const RendererScheduler::UseCase& use_case) {
219 return os << RendererScheduler::UseCaseToString(use_case);
222 class RendererSchedulerImplTest : public testing::Test {
223 public:
224 using UseCase = RendererSchedulerImpl::UseCase;
226 RendererSchedulerImplTest() : clock_(new base::SimpleTestTickClock()) {
227 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
230 RendererSchedulerImplTest(base::MessageLoop* message_loop)
231 : clock_(new base::SimpleTestTickClock()), message_loop_(message_loop) {
232 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
235 ~RendererSchedulerImplTest() override {}
237 void SetUp() override {
238 if (message_loop_) {
239 main_task_runner_ =
240 SchedulerTaskRunnerDelegateImpl::Create(message_loop_.get());
241 } else {
242 mock_task_runner_ = make_scoped_refptr(
243 new cc::OrderedSimpleTaskRunner(clock_.get(), false));
244 main_task_runner_ =
245 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_);
247 Initialize(
248 make_scoped_ptr(new RendererSchedulerImplForTest(main_task_runner_)));
251 void Initialize(scoped_ptr<RendererSchedulerImplForTest> scheduler) {
252 scheduler_ = scheduler.Pass();
253 default_task_runner_ = scheduler_->DefaultTaskRunner();
254 compositor_task_runner_ = scheduler_->CompositorTaskRunner();
255 loading_task_runner_ = scheduler_->LoadingTaskRunner();
256 idle_task_runner_ = scheduler_->IdleTaskRunner();
257 timer_task_runner_ = scheduler_->TimerTaskRunner();
258 scheduler_->GetSchedulerHelperForTesting()->SetTimeSourceForTesting(
259 make_scoped_ptr(new TestTimeSource(clock_.get())));
260 scheduler_->GetSchedulerHelperForTesting()
261 ->GetTaskQueueManagerForTesting()
262 ->SetTimeSourceForTesting(
263 make_scoped_ptr(new TestTimeSource(clock_.get())));
264 scheduler_->GetLoadingTaskCostEstimatorForTesting()
265 ->SetTimeSourceForTesting(
266 make_scoped_ptr(new TestTimeSource(clock_.get())));
267 scheduler_->GetTimerTaskCostEstimatorForTesting()->SetTimeSourceForTesting(
268 make_scoped_ptr(new TestTimeSource(clock_.get())));
271 void TearDown() override {
272 DCHECK(!mock_task_runner_.get() || !message_loop_.get());
273 scheduler_->Shutdown();
274 if (mock_task_runner_.get()) {
275 // Check that all tests stop posting tasks.
276 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
277 while (mock_task_runner_->RunUntilIdle()) {
279 } else {
280 message_loop_->RunUntilIdle();
282 scheduler_.reset();
285 void RunUntilIdle() {
286 // Only one of mock_task_runner_ or message_loop_ should be set.
287 DCHECK(!mock_task_runner_.get() || !message_loop_.get());
288 if (mock_task_runner_.get())
289 mock_task_runner_->RunUntilIdle();
290 else
291 message_loop_->RunUntilIdle();
294 void DoMainFrame() {
295 cc::BeginFrameArgs begin_frame_args = cc::BeginFrameArgs::Create(
296 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
297 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
298 begin_frame_args.on_critical_path = false;
299 scheduler_->WillBeginFrame(begin_frame_args);
300 scheduler_->DidCommitFrameToCompositor();
303 void ForceMainThreadScrollingUseCase() {
304 cc::BeginFrameArgs begin_frame_args = cc::BeginFrameArgs::Create(
305 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
306 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
307 begin_frame_args.on_critical_path = true;
308 scheduler_->WillBeginFrame(begin_frame_args);
311 void EnableIdleTasks() { DoMainFrame(); }
313 UseCase CurrentUseCase() {
314 return scheduler_->MainThreadOnly().current_use_case;
317 UseCase ForceUpdatePolicyAndGetCurrentUseCase() {
318 scheduler_->ForceUpdatePolicy();
319 return scheduler_->MainThreadOnly().current_use_case;
322 bool TouchStartExpectedSoon() {
323 return scheduler_->MainThreadOnly().touchstart_expected_soon;
326 bool HaveSeenABeginMainframe() {
327 return scheduler_->MainThreadOnly().have_seen_a_begin_main_frame;
330 bool LoadingTasksSeemExpensive() {
331 return scheduler_->MainThreadOnly().loading_tasks_seem_expensive;
334 bool TimerTasksSeemExpensive() {
335 return scheduler_->MainThreadOnly().timer_tasks_seem_expensive;
338 // Helper for posting several tasks of specific types. |task_descriptor| is a
339 // string with space delimited task identifiers. The first letter of each
340 // task identifier specifies the task type:
341 // - 'D': Default task
342 // - 'C': Compositor task
343 // - 'L': Loading task
344 // - 'I': Idle task
345 // - 'T': Timer task
346 void PostTestTasks(std::vector<std::string>* run_order,
347 const std::string& task_descriptor) {
348 std::istringstream stream(task_descriptor);
349 while (!stream.eof()) {
350 std::string task;
351 stream >> task;
352 switch (task[0]) {
353 case 'D':
354 default_task_runner_->PostTask(
355 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
356 break;
357 case 'C':
358 compositor_task_runner_->PostTask(
359 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
360 break;
361 case 'L':
362 loading_task_runner_->PostTask(
363 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
364 break;
365 case 'I':
366 idle_task_runner_->PostIdleTask(
367 FROM_HERE,
368 base::Bind(&AppendToVectorIdleTestTask, run_order, task));
369 break;
370 case 'T':
371 timer_task_runner_->PostTask(
372 FROM_HERE, base::Bind(&AppendToVectorTestTask, run_order, task));
373 break;
374 default:
375 NOTREACHED();
380 protected:
381 static base::TimeDelta priority_escalation_after_input_duration() {
382 return base::TimeDelta::FromMilliseconds(
383 UserModel::kGestureEstimationLimitMillis);
386 static base::TimeDelta subsequent_input_expected_after_input_duration() {
387 return base::TimeDelta::FromMilliseconds(
388 UserModel::kExpectSubsequentGestureMillis);
391 static base::TimeDelta maximum_idle_period_duration() {
392 return base::TimeDelta::FromMilliseconds(
393 IdleHelper::kMaximumIdlePeriodMillis);
396 static base::TimeDelta end_idle_when_hidden_delay() {
397 return base::TimeDelta::FromMilliseconds(
398 RendererSchedulerImpl::kEndIdleWhenHiddenDelayMillis);
401 static base::TimeDelta idle_period_starvation_threshold() {
402 return base::TimeDelta::FromMilliseconds(
403 RendererSchedulerImpl::kIdlePeriodStarvationThresholdMillis);
406 static base::TimeDelta suspend_timers_when_backgrounded_delay() {
407 return base::TimeDelta::FromMilliseconds(
408 RendererSchedulerImpl::kSuspendTimersWhenBackgroundedDelayMillis);
411 template <typename E>
412 static void CallForEachEnumValue(E first,
413 E last,
414 const char* (*function)(E)) {
415 for (E val = first; val < last;
416 val = static_cast<E>(static_cast<int>(val) + 1)) {
417 (*function)(val);
421 static void CheckAllUseCaseToString() {
422 CallForEachEnumValue<RendererSchedulerImpl::UseCase>(
423 RendererSchedulerImpl::UseCase::FIRST_USE_CASE,
424 RendererSchedulerImpl::UseCase::USE_CASE_COUNT,
425 &RendererSchedulerImpl::UseCaseToString);
428 scoped_ptr<base::SimpleTestTickClock> clock_;
429 // Only one of mock_task_runner_ or message_loop_ will be set.
430 scoped_refptr<cc::OrderedSimpleTaskRunner> mock_task_runner_;
431 scoped_ptr<base::MessageLoop> message_loop_;
433 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner_;
434 scoped_ptr<RendererSchedulerImplForTest> scheduler_;
435 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
436 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
437 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_;
438 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
439 scoped_refptr<base::SingleThreadTaskRunner> timer_task_runner_;
441 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplTest);
444 TEST_F(RendererSchedulerImplTest, TestPostDefaultTask) {
445 std::vector<std::string> run_order;
446 PostTestTasks(&run_order, "D1 D2 D3 D4");
448 RunUntilIdle();
449 EXPECT_THAT(run_order,
450 testing::ElementsAre(std::string("D1"), std::string("D2"),
451 std::string("D3"), std::string("D4")));
454 TEST_F(RendererSchedulerImplTest, TestPostDefaultAndCompositor) {
455 std::vector<std::string> run_order;
456 PostTestTasks(&run_order, "D1 C1");
457 RunUntilIdle();
458 EXPECT_THAT(run_order, testing::Contains("D1"));
459 EXPECT_THAT(run_order, testing::Contains("C1"));
462 TEST_F(RendererSchedulerImplTest, TestRentrantTask) {
463 int count = 0;
464 std::vector<int> run_order;
465 default_task_runner_->PostTask(
466 FROM_HERE, base::Bind(AppendToVectorReentrantTask, default_task_runner_,
467 &run_order, &count, 5));
468 RunUntilIdle();
470 EXPECT_THAT(run_order, testing::ElementsAre(0, 1, 2, 3, 4));
473 TEST_F(RendererSchedulerImplTest, TestPostIdleTask) {
474 int run_count = 0;
475 base::TimeTicks expected_deadline =
476 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(2300);
477 base::TimeTicks deadline_in_task;
479 clock_->Advance(base::TimeDelta::FromMilliseconds(100));
480 idle_task_runner_->PostIdleTask(
481 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
483 RunUntilIdle();
484 EXPECT_EQ(0, run_count); // Shouldn't run yet as no WillBeginFrame.
486 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
487 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
488 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
489 RunUntilIdle();
490 EXPECT_EQ(0, run_count); // Shouldn't run as no DidCommitFrameToCompositor.
492 clock_->Advance(base::TimeDelta::FromMilliseconds(1200));
493 scheduler_->DidCommitFrameToCompositor();
494 RunUntilIdle();
495 EXPECT_EQ(0, run_count); // We missed the deadline.
497 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
498 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
499 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
500 clock_->Advance(base::TimeDelta::FromMilliseconds(800));
501 scheduler_->DidCommitFrameToCompositor();
502 RunUntilIdle();
503 EXPECT_EQ(1, run_count);
504 EXPECT_EQ(expected_deadline, deadline_in_task);
507 TEST_F(RendererSchedulerImplTest, TestRepostingIdleTask) {
508 int run_count = 0;
510 max_idle_task_reposts = 2;
511 idle_task_runner_->PostIdleTask(
512 FROM_HERE,
513 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
514 EnableIdleTasks();
515 RunUntilIdle();
516 EXPECT_EQ(1, run_count);
518 // Reposted tasks shouldn't run until next idle period.
519 RunUntilIdle();
520 EXPECT_EQ(1, run_count);
522 EnableIdleTasks();
523 RunUntilIdle();
524 EXPECT_EQ(2, run_count);
527 TEST_F(RendererSchedulerImplTest, TestIdleTaskExceedsDeadline) {
528 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
529 int run_count = 0;
531 // Post two UpdateClockToDeadlineIdleTestTask tasks.
532 idle_task_runner_->PostIdleTask(
533 FROM_HERE,
534 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_.get(), &run_count));
535 idle_task_runner_->PostIdleTask(
536 FROM_HERE,
537 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_.get(), &run_count));
539 EnableIdleTasks();
540 RunUntilIdle();
541 // Only the first idle task should execute since it's used up the deadline.
542 EXPECT_EQ(1, run_count);
544 EnableIdleTasks();
545 RunUntilIdle();
546 // Second task should be run on the next idle period.
547 EXPECT_EQ(2, run_count);
550 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskAfterWakeup) {
551 base::TimeTicks deadline_in_task;
552 int run_count = 0;
554 idle_task_runner_->PostIdleTaskAfterWakeup(
555 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
557 EnableIdleTasks();
558 RunUntilIdle();
559 // Shouldn't run yet as no other task woke up the scheduler.
560 EXPECT_EQ(0, run_count);
562 idle_task_runner_->PostIdleTaskAfterWakeup(
563 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
565 EnableIdleTasks();
566 RunUntilIdle();
567 // Another after wakeup idle task shouldn't wake the scheduler.
568 EXPECT_EQ(0, run_count);
570 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
572 RunUntilIdle();
573 EnableIdleTasks(); // Must start a new idle period before idle task runs.
574 RunUntilIdle();
575 // Execution of default task queue task should trigger execution of idle task.
576 EXPECT_EQ(2, run_count);
579 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskAfterWakeupWhileAwake) {
580 base::TimeTicks deadline_in_task;
581 int run_count = 0;
583 idle_task_runner_->PostIdleTaskAfterWakeup(
584 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
585 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
587 RunUntilIdle();
588 EnableIdleTasks(); // Must start a new idle period before idle task runs.
589 RunUntilIdle();
590 // Should run as the scheduler was already awakened by the normal task.
591 EXPECT_EQ(1, run_count);
594 TEST_F(RendererSchedulerImplTest, TestPostIdleTaskWakesAfterWakeupIdleTask) {
595 base::TimeTicks deadline_in_task;
596 int run_count = 0;
598 idle_task_runner_->PostIdleTaskAfterWakeup(
599 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
600 idle_task_runner_->PostIdleTask(
601 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
603 EnableIdleTasks();
604 RunUntilIdle();
605 // Must start a new idle period before after-wakeup idle task runs.
606 EnableIdleTasks();
607 RunUntilIdle();
608 // Normal idle task should wake up after-wakeup idle task.
609 EXPECT_EQ(2, run_count);
612 TEST_F(RendererSchedulerImplTest, TestDelayedEndIdlePeriodCanceled) {
613 int run_count = 0;
615 base::TimeTicks deadline_in_task;
616 idle_task_runner_->PostIdleTask(
617 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
619 // Trigger the beginning of an idle period for 1000ms.
620 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
621 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
622 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
623 DoMainFrame();
625 // End the idle period early (after 500ms), and send a WillBeginFrame which
626 // specifies that the next idle period should end 1000ms from now.
627 clock_->Advance(base::TimeDelta::FromMilliseconds(500));
628 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
629 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
630 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
632 RunUntilIdle();
633 EXPECT_EQ(0, run_count); // Not currently in an idle period.
635 // Trigger the start of the idle period before the task to end the previous
636 // idle period has been triggered.
637 clock_->Advance(base::TimeDelta::FromMilliseconds(400));
638 scheduler_->DidCommitFrameToCompositor();
640 // Post a task which simulates running until after the previous end idle
641 // period delayed task was scheduled for
642 scheduler_->DefaultTaskRunner()->PostTask(FROM_HERE, base::Bind(NullTask));
643 clock_->Advance(base::TimeDelta::FromMilliseconds(300));
645 RunUntilIdle();
646 EXPECT_EQ(1, run_count); // We should still be in the new idle period.
649 TEST_F(RendererSchedulerImplTest, TestDefaultPolicy) {
650 std::vector<std::string> run_order;
651 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
653 EnableIdleTasks();
654 RunUntilIdle();
655 EXPECT_THAT(run_order,
656 testing::ElementsAre(std::string("L1"), std::string("D1"),
657 std::string("C1"), std::string("D2"),
658 std::string("C2"), std::string("I1")));
659 EXPECT_EQ(RendererScheduler::UseCase::NONE, CurrentUseCase());
662 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_CompositorHandlesInput) {
663 std::vector<std::string> run_order;
664 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
666 scheduler_->DidHandleInputEventOnCompositorThread(
667 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
668 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
669 EnableIdleTasks();
670 RunUntilIdle();
671 EXPECT_THAT(run_order,
672 testing::ElementsAre(std::string("L1"), std::string("D1"),
673 std::string("C1"), std::string("D2"),
674 std::string("C2"), std::string("I1")));
675 EXPECT_EQ(RendererScheduler::UseCase::COMPOSITOR_GESTURE, CurrentUseCase());
678 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_MainThreadHandlesInput) {
679 std::vector<std::string> run_order;
680 PostTestTasks(&run_order, "L1 I1 D1 C1 D2 C2");
682 scheduler_->DidHandleInputEventOnCompositorThread(
683 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
684 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
685 EnableIdleTasks();
686 RunUntilIdle();
687 EXPECT_THAT(run_order,
688 testing::ElementsAre(std::string("L1"), std::string("D1"),
689 std::string("C1"), std::string("D2"),
690 std::string("C2"), std::string("I1")));
691 EXPECT_EQ(RendererScheduler::UseCase::COMPOSITOR_GESTURE, CurrentUseCase());
692 scheduler_->DidHandleInputEventOnMainThread(
693 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
696 TEST_F(RendererSchedulerImplTest, TestCompositorPolicy_DidAnimateForInput) {
697 std::vector<std::string> run_order;
698 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
700 scheduler_->DidAnimateForInputOnCompositorThread();
701 EnableIdleTasks();
702 RunUntilIdle();
703 EXPECT_THAT(run_order,
704 testing::ElementsAre(std::string("D1"), std::string("C1"),
705 std::string("D2"), std::string("C2"),
706 std::string("I1")));
707 EXPECT_EQ(RendererScheduler::UseCase::COMPOSITOR_GESTURE, CurrentUseCase());
710 TEST_F(
711 RendererSchedulerImplTest,
712 TestCompositorPolicy_ExpensiveTimersDontRunWhenMainThreadOnCriticalPath) {
713 std::vector<std::string> run_order;
715 // RunUntilIdle won't actually run all of the SimpleTestTickClock::Advance
716 // tasks unless we set AutoAdvanceNow to true :/
717 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
719 // Simulate a bunch of expensive timer tasks
720 for (int i = 0; i < 10; i++) {
721 timer_task_runner_->PostTask(
722 FROM_HERE, base::Bind(&base::SimpleTestTickClock::Advance,
723 base::Unretained(clock_.get()),
724 base::TimeDelta::FromMilliseconds(500)));
727 RunUntilIdle();
729 // Switch back to not auto-advancing because we want to be in control of when
730 // time advances.
731 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(false);
733 // Timers should now be disabled during main thread user user interactions.
734 PostTestTasks(&run_order, "C1 T1");
736 // Trigger main_thread_gesture UseCase
737 scheduler_->DidAnimateForInputOnCompositorThread();
738 cc::BeginFrameArgs begin_frame_args1 = cc::BeginFrameArgs::Create(
739 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
740 base::TimeDelta::FromMilliseconds(16), cc::BeginFrameArgs::NORMAL);
741 begin_frame_args1.on_critical_path = true;
742 scheduler_->WillBeginFrame(begin_frame_args1);
743 RunUntilIdle();
744 EXPECT_EQ(RendererScheduler::UseCase::MAIN_THREAD_GESTURE, CurrentUseCase());
746 EXPECT_THAT(run_order, testing::ElementsAre(std::string("C1")));
747 clock_->Advance(subsequent_input_expected_after_input_duration() * 2);
749 run_order.clear();
750 RunUntilIdle();
751 EXPECT_EQ(RendererScheduler::UseCase::NONE, CurrentUseCase());
752 EXPECT_THAT(run_order, testing::ElementsAre(std::string("T1")));
755 TEST_F(RendererSchedulerImplTest, Navigation_ResetsTaskCostEstimations) {
756 std::vector<std::string> run_order;
758 // RunUntilIdle won't actually run all of the SimpleTestTickClock::Advance
759 // tasks unless we set AutoAdvanceNow to true :/
760 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
762 // Simulate a bunch of expensive timer tasks
763 for (int i = 0; i < 10; i++) {
764 timer_task_runner_->PostTask(
765 FROM_HERE, base::Bind(&base::SimpleTestTickClock::Advance,
766 base::Unretained(clock_.get()),
767 base::TimeDelta::FromMilliseconds(500)));
770 RunUntilIdle();
772 // Switch back to not auto-advancing because we want to be in control of when
773 // time advances.
774 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(false);
776 scheduler_->OnPageLoadStarted();
777 PostTestTasks(&run_order, "C1 T1");
779 scheduler_->DidAnimateForInputOnCompositorThread();
780 cc::BeginFrameArgs begin_frame_args1 = cc::BeginFrameArgs::Create(
781 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
782 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
783 begin_frame_args1.on_critical_path = true;
784 scheduler_->WillBeginFrame(begin_frame_args1);
785 scheduler_->DidCommitFrameToCompositor(); // Starts Idle Period
786 RunUntilIdle();
788 EXPECT_THAT(run_order,
789 testing::ElementsAre(std::string("C1"), std::string("T1")));
792 TEST_F(RendererSchedulerImplTest,
793 TestCompositorPolicy_TimersAlwaysRun_MainThreadNotOnCriticalPath) {
794 std::vector<std::string> run_order;
795 PostTestTasks(&run_order, "C1 T1");
797 scheduler_->DidAnimateForInputOnCompositorThread();
798 cc::BeginFrameArgs begin_frame_args1 = cc::BeginFrameArgs::Create(
799 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
800 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
801 begin_frame_args1.on_critical_path = false;
802 scheduler_->WillBeginFrame(begin_frame_args1);
803 scheduler_->DidCommitFrameToCompositor(); // Starts Idle Period
804 RunUntilIdle();
806 EXPECT_THAT(run_order,
807 testing::ElementsAre(std::string("C1"), std::string("T1")));
809 // End the idle period.
810 clock_->Advance(base::TimeDelta::FromMilliseconds(500));
811 scheduler_->DidAnimateForInputOnCompositorThread();
812 cc::BeginFrameArgs begin_frame_args2 = cc::BeginFrameArgs::Create(
813 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
814 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
815 begin_frame_args2.on_critical_path = false;
816 scheduler_->WillBeginFrame(begin_frame_args2);
818 run_order.clear();
819 PostTestTasks(&run_order, "C1 T1");
820 RunUntilIdle();
822 EXPECT_THAT(run_order,
823 testing::ElementsAre(std::string("C1"), std::string("T1")));
826 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicy_Compositor) {
827 std::vector<std::string> run_order;
828 PostTestTasks(&run_order, "L1 D1 C1 D2 C2 T1 T2");
830 // Observation of touchstart should defer execution of timer, idle and loading
831 // tasks.
832 scheduler_->DidHandleInputEventOnCompositorThread(
833 FakeInputEvent(blink::WebInputEvent::TouchStart),
834 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
835 EnableIdleTasks();
836 RunUntilIdle();
837 EXPECT_THAT(run_order,
838 testing::ElementsAre(std::string("C1"), std::string("C2"),
839 std::string("D1"), std::string("D2")));
841 // Animation or meta events like TapDown/FlingCancel shouldn't affect the
842 // priority.
843 run_order.clear();
844 scheduler_->DidAnimateForInputOnCompositorThread();
845 scheduler_->DidHandleInputEventOnCompositorThread(
846 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel),
847 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
848 scheduler_->DidHandleInputEventOnCompositorThread(
849 FakeInputEvent(blink::WebInputEvent::GestureTapDown),
850 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
851 RunUntilIdle();
852 EXPECT_TRUE(run_order.empty());
854 // Action events like ScrollBegin will kick us back into compositor priority,
855 // allowing service of the timer, loading and idle queues.
856 run_order.clear();
857 scheduler_->DidHandleInputEventOnCompositorThread(
858 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
859 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
860 RunUntilIdle();
862 EXPECT_THAT(run_order,
863 testing::ElementsAre(std::string("L1"), std::string("T1"),
864 std::string("T2")));
867 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicy_MainThread) {
868 std::vector<std::string> run_order;
869 PostTestTasks(&run_order, "L1 D1 C1 D2 C2 T1 T2");
871 // Observation of touchstart should defer execution of timer, idle and loading
872 // tasks.
873 scheduler_->DidHandleInputEventOnCompositorThread(
874 FakeInputEvent(blink::WebInputEvent::TouchStart),
875 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
876 scheduler_->DidHandleInputEventOnMainThread(
877 FakeInputEvent(blink::WebInputEvent::TouchStart));
878 EnableIdleTasks();
879 RunUntilIdle();
880 EXPECT_THAT(run_order,
881 testing::ElementsAre(std::string("C1"), std::string("C2"),
882 std::string("D1"), std::string("D2")));
884 // Meta events like TapDown/FlingCancel shouldn't affect the priority.
885 run_order.clear();
886 scheduler_->DidHandleInputEventOnCompositorThread(
887 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel),
888 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
889 scheduler_->DidHandleInputEventOnMainThread(
890 FakeInputEvent(blink::WebInputEvent::GestureFlingCancel));
891 scheduler_->DidHandleInputEventOnCompositorThread(
892 FakeInputEvent(blink::WebInputEvent::GestureTapDown),
893 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
894 scheduler_->DidHandleInputEventOnMainThread(
895 FakeInputEvent(blink::WebInputEvent::GestureTapDown));
896 RunUntilIdle();
897 EXPECT_TRUE(run_order.empty());
899 // Action events like ScrollBegin will kick us back into compositor priority,
900 // allowing service of the timer, loading and idle queues.
901 run_order.clear();
902 scheduler_->DidHandleInputEventOnCompositorThread(
903 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
904 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
905 scheduler_->DidHandleInputEventOnMainThread(
906 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin));
907 RunUntilIdle();
909 EXPECT_THAT(run_order,
910 testing::ElementsAre(std::string("L1"), std::string("T1"),
911 std::string("T2")));
914 // TODO(alexclarke): Reenable once we've reinstaed the Loading UseCase.
915 TEST_F(RendererSchedulerImplTest, DISABLED_LoadingUseCase) {
916 std::vector<std::string> run_order;
917 PostTestTasks(&run_order, "I1 D1 C1 T1 L1 D2 C2 T2 L2");
919 scheduler_->OnPageLoadStarted();
920 EnableIdleTasks();
921 RunUntilIdle();
923 // In loading policy, loading tasks are prioritized other others.
924 std::string loading_policy_expected[] = {
925 std::string("D1"), std::string("L1"), std::string("D2"),
926 std::string("L2"), std::string("C1"), std::string("T1"),
927 std::string("C2"), std::string("T2"), std::string("I1")};
928 EXPECT_THAT(run_order, testing::ElementsAreArray(loading_policy_expected));
929 EXPECT_EQ(RendererScheduler::UseCase::LOADING, CurrentUseCase());
931 // Advance 15s and try again, the loading policy should have ended and the
932 // task order should return to the NONE use case where loading tasks are no
933 // longer prioritized.
934 clock_->Advance(base::TimeDelta::FromMilliseconds(150000));
935 run_order.clear();
936 PostTestTasks(&run_order, "I1 D1 C1 T1 L1 D2 C2 T2 L2");
937 EnableIdleTasks();
938 RunUntilIdle();
940 std::string default_order_expected[] = {
941 std::string("D1"), std::string("C1"), std::string("T1"),
942 std::string("L1"), std::string("D2"), std::string("C2"),
943 std::string("T2"), std::string("L2"), std::string("I1")};
944 EXPECT_THAT(run_order, testing::ElementsAreArray(default_order_expected));
945 EXPECT_EQ(RendererScheduler::UseCase::NONE, CurrentUseCase());
948 TEST_F(RendererSchedulerImplTest,
949 EventConsumedOnCompositorThread_IgnoresMouseMove_WhenMouseUp) {
950 std::vector<std::string> run_order;
951 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
953 ForceMainThreadScrollingUseCase();
954 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
955 scheduler_->DidHandleInputEventOnCompositorThread(
956 FakeInputEvent(blink::WebInputEvent::MouseMove),
957 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
958 RunUntilIdle();
959 // Note compositor tasks are not prioritized.
960 EXPECT_EQ(RendererScheduler::UseCase::NONE, CurrentUseCase());
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")));
967 TEST_F(RendererSchedulerImplTest,
968 EventForwardedToMainThread_IgnoresMouseMove_WhenMouseUp) {
969 std::vector<std::string> run_order;
970 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
972 ForceMainThreadScrollingUseCase();
973 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
974 scheduler_->DidHandleInputEventOnCompositorThread(
975 FakeInputEvent(blink::WebInputEvent::MouseMove),
976 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
977 RunUntilIdle();
978 // Note compositor tasks are not prioritized.
979 EXPECT_EQ(RendererScheduler::UseCase::NONE, CurrentUseCase());
980 EXPECT_THAT(run_order,
981 testing::ElementsAre(std::string("D1"), std::string("C1"),
982 std::string("D2"), std::string("C2"),
983 std::string("I1")));
986 TEST_F(RendererSchedulerImplTest,
987 EventConsumedOnCompositorThread_MouseMove_WhenMouseDown) {
988 std::vector<std::string> run_order;
989 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
991 ForceMainThreadScrollingUseCase();
992 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
993 scheduler_->DidHandleInputEventOnCompositorThread(
994 FakeInputEvent(blink::WebInputEvent::MouseMove,
995 blink::WebInputEvent::LeftButtonDown),
996 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
997 RunUntilIdle();
998 // Note compositor tasks are prioritized.
999 EXPECT_THAT(run_order,
1000 testing::ElementsAre(std::string("C1"), std::string("C2"),
1001 std::string("D1"), std::string("D2"),
1002 std::string("I1")));
1005 TEST_F(RendererSchedulerImplTest,
1006 EventForwardedToMainThread_MouseMove_WhenMouseDown) {
1007 std::vector<std::string> run_order;
1008 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
1010 ForceMainThreadScrollingUseCase();
1011 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
1012 scheduler_->DidHandleInputEventOnCompositorThread(
1013 FakeInputEvent(blink::WebInputEvent::MouseMove,
1014 blink::WebInputEvent::LeftButtonDown),
1015 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1016 RunUntilIdle();
1017 // Note compositor tasks are prioritized.
1018 EXPECT_THAT(run_order,
1019 testing::ElementsAre(std::string("C1"), std::string("C2"),
1020 std::string("D1"), std::string("D2"),
1021 std::string("I1")));
1022 scheduler_->DidHandleInputEventOnMainThread(FakeInputEvent(
1023 blink::WebInputEvent::MouseMove, blink::WebInputEvent::LeftButtonDown));
1026 TEST_F(RendererSchedulerImplTest, EventConsumedOnCompositorThread_MouseWheel) {
1027 std::vector<std::string> run_order;
1028 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
1030 ForceMainThreadScrollingUseCase();
1031 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
1032 scheduler_->DidHandleInputEventOnCompositorThread(
1033 FakeInputEvent(blink::WebInputEvent::MouseWheel),
1034 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1035 RunUntilIdle();
1036 // Note compositor tasks are prioritized.
1037 EXPECT_THAT(run_order,
1038 testing::ElementsAre(std::string("C1"), std::string("C2"),
1039 std::string("D1"), std::string("D2"),
1040 std::string("I1")));
1043 TEST_F(RendererSchedulerImplTest, EventForwardedToMainThread_MouseWheel) {
1044 std::vector<std::string> run_order;
1045 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
1047 ForceMainThreadScrollingUseCase();
1048 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
1049 scheduler_->DidHandleInputEventOnCompositorThread(
1050 FakeInputEvent(blink::WebInputEvent::MouseWheel),
1051 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1052 RunUntilIdle();
1053 // Note compositor tasks are prioritized.
1054 EXPECT_THAT(run_order,
1055 testing::ElementsAre(std::string("C1"), std::string("C2"),
1056 std::string("D1"), std::string("D2"),
1057 std::string("I1")));
1058 EXPECT_EQ(RendererScheduler::UseCase::MAIN_THREAD_GESTURE, CurrentUseCase());
1061 TEST_F(RendererSchedulerImplTest,
1062 EventConsumedOnCompositorThread_IgnoresKeyboardEvents) {
1063 std::vector<std::string> run_order;
1064 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
1066 ForceMainThreadScrollingUseCase();
1067 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
1068 scheduler_->DidHandleInputEventOnCompositorThread(
1069 FakeInputEvent(blink::WebInputEvent::KeyDown),
1070 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1071 RunUntilIdle();
1072 // Note compositor tasks are not prioritized.
1073 EXPECT_THAT(run_order,
1074 testing::ElementsAre(std::string("D1"), std::string("C1"),
1075 std::string("D2"), std::string("C2"),
1076 std::string("I1")));
1077 EXPECT_EQ(RendererScheduler::UseCase::NONE, CurrentUseCase());
1080 TEST_F(RendererSchedulerImplTest,
1081 EventForwardedToMainThread_IgnoresKeyboardEvents) {
1082 std::vector<std::string> run_order;
1083 PostTestTasks(&run_order, "I1 D1 C1 D2 C2");
1085 ForceMainThreadScrollingUseCase();
1086 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
1087 scheduler_->DidHandleInputEventOnCompositorThread(
1088 FakeInputEvent(blink::WebInputEvent::KeyDown),
1089 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1090 RunUntilIdle();
1091 // Note compositor tasks are not prioritized.
1092 EXPECT_THAT(run_order,
1093 testing::ElementsAre(std::string("D1"), std::string("C1"),
1094 std::string("D2"), std::string("C2"),
1095 std::string("I1")));
1096 EXPECT_EQ(RendererScheduler::UseCase::NONE, CurrentUseCase());
1097 // Note compositor tasks are not prioritized.
1098 scheduler_->DidHandleInputEventOnMainThread(
1099 FakeInputEvent(blink::WebInputEvent::KeyDown));
1102 TEST_F(RendererSchedulerImplTest,
1103 TestMainthreadScrollingUseCaseDoesNotStarveDefaultTasks) {
1104 ForceMainThreadScrollingUseCase();
1105 scheduler_->DidCommitFrameToCompositor(); // Enable Idle tasks.
1107 std::vector<std::string> run_order;
1108 PostTestTasks(&run_order, "D1 C1");
1110 for (int i = 0; i < 20; i++) {
1111 compositor_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
1113 PostTestTasks(&run_order, "C2");
1115 scheduler_->DidHandleInputEventOnCompositorThread(
1116 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1117 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1118 RunUntilIdle();
1119 // Ensure that the default D1 task gets to run at some point before the final
1120 // C2 compositor task.
1121 EXPECT_THAT(run_order,
1122 testing::ElementsAre(std::string("C1"), std::string("D1"),
1123 std::string("C2")));
1126 TEST_F(RendererSchedulerImplTest,
1127 TestCompositorPolicyEnds_CompositorHandlesInput) {
1128 scheduler_->DidHandleInputEventOnCompositorThread(
1129 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1130 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1131 EXPECT_EQ(UseCase::COMPOSITOR_GESTURE,
1132 ForceUpdatePolicyAndGetCurrentUseCase());
1134 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1135 EXPECT_EQ(UseCase::NONE, ForceUpdatePolicyAndGetCurrentUseCase());
1138 TEST_F(RendererSchedulerImplTest,
1139 TestCompositorPolicyEnds_MainThreadHandlesInput) {
1140 scheduler_->DidHandleInputEventOnCompositorThread(
1141 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1142 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1143 scheduler_->DidHandleInputEventOnMainThread(
1144 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1145 EXPECT_EQ(UseCase::COMPOSITOR_GESTURE,
1146 ForceUpdatePolicyAndGetCurrentUseCase());
1148 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1149 EXPECT_EQ(UseCase::NONE, ForceUpdatePolicyAndGetCurrentUseCase());
1152 TEST_F(RendererSchedulerImplTest, TestTouchstartPolicyEndsAfterTimeout) {
1153 std::vector<std::string> run_order;
1154 PostTestTasks(&run_order, "L1 D1 C1 D2 C2");
1156 scheduler_->DidHandleInputEventOnCompositorThread(
1157 FakeInputEvent(blink::WebInputEvent::TouchStart),
1158 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1159 RunUntilIdle();
1160 EXPECT_THAT(run_order,
1161 testing::ElementsAre(std::string("C1"), std::string("C2"),
1162 std::string("D1"), std::string("D2")));
1164 run_order.clear();
1165 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1167 // Don't post any compositor tasks to simulate a very long running event
1168 // handler.
1169 PostTestTasks(&run_order, "D1 D2");
1171 // Touchstart policy mode should have ended now that the clock has advanced.
1172 RunUntilIdle();
1173 EXPECT_THAT(run_order,
1174 testing::ElementsAre(std::string("L1"), std::string("D1"),
1175 std::string("D2")));
1178 TEST_F(RendererSchedulerImplTest,
1179 TestTouchstartPolicyEndsAfterConsecutiveTouchmoves) {
1180 std::vector<std::string> run_order;
1181 PostTestTasks(&run_order, "L1 D1 C1 D2 C2");
1183 // Observation of touchstart should defer execution of idle and loading tasks.
1184 scheduler_->DidHandleInputEventOnCompositorThread(
1185 FakeInputEvent(blink::WebInputEvent::TouchStart),
1186 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1187 RunUntilIdle();
1188 EXPECT_THAT(run_order,
1189 testing::ElementsAre(std::string("C1"), std::string("C2"),
1190 std::string("D1"), std::string("D2")));
1192 // Receiving the first touchmove will not affect scheduler priority.
1193 run_order.clear();
1194 scheduler_->DidHandleInputEventOnCompositorThread(
1195 FakeInputEvent(blink::WebInputEvent::TouchMove),
1196 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1197 RunUntilIdle();
1198 EXPECT_TRUE(run_order.empty());
1200 // Receiving the second touchmove will kick us back into compositor priority.
1201 run_order.clear();
1202 scheduler_->DidHandleInputEventOnCompositorThread(
1203 FakeInputEvent(blink::WebInputEvent::TouchMove),
1204 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1205 RunUntilIdle();
1206 EXPECT_THAT(run_order, testing::ElementsAre(std::string("L1")));
1209 TEST_F(RendererSchedulerImplTest, TestIsHighPriorityWorkAnticipated) {
1210 bool is_anticipated_before = false;
1211 bool is_anticipated_after = false;
1213 default_task_runner_->PostTask(
1214 FROM_HERE, base::Bind(&AnticipationTestTask, scheduler_.get(),
1215 SimulateInputType::None, &is_anticipated_before,
1216 &is_anticipated_after));
1217 RunUntilIdle();
1218 // In its default state, without input receipt, the scheduler should indicate
1219 // that no high-priority is anticipated.
1220 EXPECT_FALSE(is_anticipated_before);
1221 EXPECT_FALSE(is_anticipated_after);
1223 default_task_runner_->PostTask(
1224 FROM_HERE, base::Bind(&AnticipationTestTask, scheduler_.get(),
1225 SimulateInputType::TouchStart,
1226 &is_anticipated_before, &is_anticipated_after));
1227 bool dummy;
1228 default_task_runner_->PostTask(
1229 FROM_HERE, base::Bind(&AnticipationTestTask, scheduler_.get(),
1230 SimulateInputType::TouchEnd, &dummy, &dummy));
1231 default_task_runner_->PostTask(
1232 FROM_HERE,
1233 base::Bind(&AnticipationTestTask, scheduler_.get(),
1234 SimulateInputType::GestureScrollBegin, &dummy, &dummy));
1235 default_task_runner_->PostTask(
1236 FROM_HERE,
1237 base::Bind(&AnticipationTestTask, scheduler_.get(),
1238 SimulateInputType::GestureScrollEnd, &dummy, &dummy));
1240 RunUntilIdle();
1241 // When input is received, the scheduler should indicate that high-priority
1242 // work is anticipated.
1243 EXPECT_FALSE(is_anticipated_before);
1244 EXPECT_TRUE(is_anticipated_after);
1246 clock_->Advance(priority_escalation_after_input_duration() * 2);
1247 default_task_runner_->PostTask(
1248 FROM_HERE, base::Bind(&AnticipationTestTask, scheduler_.get(),
1249 SimulateInputType::None, &is_anticipated_before,
1250 &is_anticipated_after));
1251 RunUntilIdle();
1252 // Without additional input, the scheduler should go into NONE
1253 // use case but with scrolling expected where high-priority work is still
1254 // anticipated.
1255 EXPECT_EQ(UseCase::NONE, CurrentUseCase());
1256 EXPECT_TRUE(TouchStartExpectedSoon());
1257 EXPECT_TRUE(is_anticipated_before);
1258 EXPECT_TRUE(is_anticipated_after);
1260 clock_->Advance(subsequent_input_expected_after_input_duration() * 2);
1261 default_task_runner_->PostTask(
1262 FROM_HERE, base::Bind(&AnticipationTestTask, scheduler_.get(),
1263 SimulateInputType::None, &is_anticipated_before,
1264 &is_anticipated_after));
1265 RunUntilIdle();
1266 // Eventually the scheduler should go into the default use case where
1267 // high-priority work is no longer anticipated.
1268 EXPECT_EQ(UseCase::NONE, CurrentUseCase());
1269 EXPECT_FALSE(TouchStartExpectedSoon());
1270 EXPECT_FALSE(is_anticipated_before);
1271 EXPECT_FALSE(is_anticipated_after);
1274 TEST_F(RendererSchedulerImplTest, TestShouldYield) {
1275 bool should_yield_before = false;
1276 bool should_yield_after = false;
1278 ForceMainThreadScrollingUseCase();
1280 default_task_runner_->PostTask(
1281 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1282 default_task_runner_, false, &should_yield_before,
1283 &should_yield_after));
1284 RunUntilIdle();
1285 // Posting to default runner shouldn't cause yielding.
1286 EXPECT_FALSE(should_yield_before);
1287 EXPECT_FALSE(should_yield_after);
1289 default_task_runner_->PostTask(
1290 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1291 compositor_task_runner_, false,
1292 &should_yield_before, &should_yield_after));
1293 RunUntilIdle();
1294 // Posting while not mainthread scrolling shouldn't cause yielding.
1295 EXPECT_FALSE(should_yield_before);
1296 EXPECT_FALSE(should_yield_after);
1298 default_task_runner_->PostTask(
1299 FROM_HERE, base::Bind(&PostingYieldingTestTask, scheduler_.get(),
1300 compositor_task_runner_, true, &should_yield_before,
1301 &should_yield_after));
1302 RunUntilIdle();
1303 // We should be able to switch to compositor priority mid-task.
1304 EXPECT_FALSE(should_yield_before);
1305 EXPECT_TRUE(should_yield_after);
1308 TEST_F(RendererSchedulerImplTest, TestShouldYield_TouchStart) {
1309 // Receiving a touchstart should immediately trigger yielding, even if
1310 // there's no immediately pending work in the compositor queue.
1311 EXPECT_FALSE(scheduler_->ShouldYieldForHighPriorityWork());
1312 scheduler_->DidHandleInputEventOnCompositorThread(
1313 FakeInputEvent(blink::WebInputEvent::TouchStart),
1314 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1315 EXPECT_TRUE(scheduler_->ShouldYieldForHighPriorityWork());
1316 RunUntilIdle();
1319 TEST_F(RendererSchedulerImplTest, SlowMainThreadInputEvent) {
1320 EXPECT_EQ(UseCase::NONE, CurrentUseCase());
1322 // An input event should bump us into input priority.
1323 scheduler_->DidHandleInputEventOnCompositorThread(
1324 FakeInputEvent(blink::WebInputEvent::GestureFlingStart),
1325 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1326 RunUntilIdle();
1327 EXPECT_EQ(UseCase::COMPOSITOR_GESTURE, CurrentUseCase());
1329 // Simulate the input event being queued for a very long time. The compositor
1330 // task we post here represents the enqueued input task.
1331 clock_->Advance(priority_escalation_after_input_duration() * 2);
1332 scheduler_->DidHandleInputEventOnMainThread(
1333 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1334 RunUntilIdle();
1336 // Even though we exceeded the input priority escalation period, we should
1337 // still be in compositor priority since the input remains queued.
1338 EXPECT_EQ(UseCase::COMPOSITOR_GESTURE, CurrentUseCase());
1340 // After the escalation period ends we should go back into normal mode.
1341 clock_->Advance(priority_escalation_after_input_duration() * 2);
1342 RunUntilIdle();
1343 EXPECT_EQ(UseCase::NONE, CurrentUseCase());
1346 class RendererSchedulerImplWithMockSchedulerTest
1347 : public RendererSchedulerImplTest {
1348 public:
1349 void SetUp() override {
1350 mock_task_runner_ = make_scoped_refptr(
1351 new cc::OrderedSimpleTaskRunner(clock_.get(), false));
1352 main_task_runner_ =
1353 SchedulerTaskRunnerDelegateForTest::Create(mock_task_runner_);
1354 mock_scheduler_ = new RendererSchedulerImplForTest(main_task_runner_);
1355 Initialize(make_scoped_ptr(mock_scheduler_));
1358 protected:
1359 RendererSchedulerImplForTest* mock_scheduler_;
1362 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1363 OnlyOnePendingUrgentPolicyUpdatey) {
1364 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1365 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1366 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1367 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1369 RunUntilIdle();
1371 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1374 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1375 OnePendingDelayedAndOneUrgentUpdatePolicy) {
1376 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1378 mock_scheduler_->ScheduleDelayedPolicyUpdate(
1379 clock_->NowTicks(), base::TimeDelta::FromMilliseconds(1));
1380 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1382 RunUntilIdle();
1384 // We expect both the urgent and the delayed updates to run.
1385 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1388 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1389 OneUrgentAndOnePendingDelayedUpdatePolicy) {
1390 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1392 mock_scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread();
1393 mock_scheduler_->ScheduleDelayedPolicyUpdate(
1394 clock_->NowTicks(), base::TimeDelta::FromMilliseconds(1));
1396 RunUntilIdle();
1398 // We expect both the urgent and the delayed updates to run.
1399 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1402 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1403 UpdatePolicyCountTriggeredByOneInputEvent) {
1404 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1405 // update.
1406 scheduler_->DidHandleInputEventOnCompositorThread(
1407 FakeInputEvent(blink::WebInputEvent::TouchStart),
1408 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1409 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1410 mock_task_runner_->RunPendingTasks();
1411 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1413 scheduler_->DidHandleInputEventOnMainThread(
1414 FakeInputEvent(blink::WebInputEvent::TouchStart));
1415 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1417 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1418 RunUntilIdle();
1420 // We finally expect a delayed policy update 100ms later.
1421 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1424 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1425 UpdatePolicyCountTriggeredByThreeInputEvents) {
1426 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1427 // update.
1428 scheduler_->DidHandleInputEventOnCompositorThread(
1429 FakeInputEvent(blink::WebInputEvent::TouchStart),
1430 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1431 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1432 mock_task_runner_->RunPendingTasks();
1433 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1435 scheduler_->DidHandleInputEventOnMainThread(
1436 FakeInputEvent(blink::WebInputEvent::TouchStart));
1437 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1439 // The second call to DidHandleInputEventOnCompositorThread should not post a
1440 // policy update because we are already in compositor priority.
1441 scheduler_->DidHandleInputEventOnCompositorThread(
1442 FakeInputEvent(blink::WebInputEvent::TouchMove),
1443 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1444 mock_task_runner_->RunPendingTasks();
1445 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1447 // We expect DidHandleInputEvent to trigger a policy update.
1448 scheduler_->DidHandleInputEventOnMainThread(
1449 FakeInputEvent(blink::WebInputEvent::TouchMove));
1450 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1452 // The third call to DidHandleInputEventOnCompositorThread should post a
1453 // policy update because the awaiting_touch_start_response_ flag changed.
1454 scheduler_->DidHandleInputEventOnCompositorThread(
1455 FakeInputEvent(blink::WebInputEvent::TouchMove),
1456 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1457 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1458 mock_task_runner_->RunPendingTasks();
1459 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1461 // We expect DidHandleInputEvent to trigger a policy update.
1462 scheduler_->DidHandleInputEventOnMainThread(
1463 FakeInputEvent(blink::WebInputEvent::TouchMove));
1464 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1466 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1467 RunUntilIdle();
1469 // We finally expect a delayed policy update.
1470 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1473 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1474 UpdatePolicyCountTriggeredByTwoInputEventsWithALongSeparatingDelay) {
1475 // We expect DidHandleInputEventOnCompositorThread to post an urgent policy
1476 // update.
1477 scheduler_->DidHandleInputEventOnCompositorThread(
1478 FakeInputEvent(blink::WebInputEvent::TouchStart),
1479 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1480 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1481 mock_task_runner_->RunPendingTasks();
1482 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1484 scheduler_->DidHandleInputEventOnMainThread(
1485 FakeInputEvent(blink::WebInputEvent::TouchStart));
1486 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1488 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1489 RunUntilIdle();
1490 // We expect a delayed policy update.
1491 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1493 // We expect the second call to DidHandleInputEventOnCompositorThread to post
1494 // an urgent policy update because we are no longer in compositor priority.
1495 scheduler_->DidHandleInputEventOnCompositorThread(
1496 FakeInputEvent(blink::WebInputEvent::TouchMove),
1497 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1498 EXPECT_EQ(2, mock_scheduler_->update_policy_count_);
1499 mock_task_runner_->RunPendingTasks();
1500 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1502 scheduler_->DidHandleInputEventOnMainThread(
1503 FakeInputEvent(blink::WebInputEvent::TouchMove));
1504 EXPECT_EQ(3, mock_scheduler_->update_policy_count_);
1506 clock_->Advance(base::TimeDelta::FromMilliseconds(1000));
1507 RunUntilIdle();
1509 // We finally expect a delayed policy update.
1510 EXPECT_EQ(4, mock_scheduler_->update_policy_count_);
1513 TEST_F(RendererSchedulerImplWithMockSchedulerTest,
1514 EnsureUpdatePolicyNotTriggeredTooOften) {
1515 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1517 scheduler_->DidHandleInputEventOnCompositorThread(
1518 FakeInputEvent(blink::WebInputEvent::TouchStart),
1519 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1520 scheduler_->DidHandleInputEventOnCompositorThread(
1521 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin),
1522 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
1524 // We expect the first call to IsHighPriorityWorkAnticipated to be called
1525 // after recieving an input event (but before the UpdateTask was processed) to
1526 // call UpdatePolicy.
1527 EXPECT_EQ(0, mock_scheduler_->update_policy_count_);
1528 scheduler_->IsHighPriorityWorkAnticipated();
1529 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1530 // Subsequent calls should not call UpdatePolicy.
1531 scheduler_->IsHighPriorityWorkAnticipated();
1532 scheduler_->IsHighPriorityWorkAnticipated();
1533 scheduler_->IsHighPriorityWorkAnticipated();
1534 scheduler_->ShouldYieldForHighPriorityWork();
1535 scheduler_->ShouldYieldForHighPriorityWork();
1536 scheduler_->ShouldYieldForHighPriorityWork();
1537 scheduler_->ShouldYieldForHighPriorityWork();
1539 scheduler_->DidHandleInputEventOnMainThread(
1540 FakeInputEvent(blink::WebInputEvent::TouchStart));
1541 scheduler_->DidHandleInputEventOnMainThread(
1542 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin));
1544 EXPECT_EQ(1, mock_scheduler_->update_policy_count_);
1546 // We expect both the urgent and the delayed updates to run in addition to the
1547 // earlier updated cause by IsHighPriorityWorkAnticipated, a final update
1548 // transitions from 'not_scrolling scroll expected' to 'not_scrolling'.
1549 RunUntilIdle();
1550 EXPECT_THAT(mock_scheduler_->use_cases_,
1551 testing::ElementsAre(std::string("compositor_gesture"),
1552 std::string("compositor_gesture"),
1553 std::string("none scroll expected"),
1554 std::string("none")));
1557 class RendererSchedulerImplWithMessageLoopTest
1558 : public RendererSchedulerImplTest {
1559 public:
1560 RendererSchedulerImplWithMessageLoopTest()
1561 : RendererSchedulerImplTest(new base::MessageLoop()) {}
1562 ~RendererSchedulerImplWithMessageLoopTest() override {}
1564 void PostFromNestedRunloop(std::vector<
1565 std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>* tasks) {
1566 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop_.get());
1567 for (std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>& pair : *tasks) {
1568 if (pair.second) {
1569 idle_task_runner_->PostIdleTask(FROM_HERE, pair.first);
1570 } else {
1571 idle_task_runner_->PostNonNestableIdleTask(FROM_HERE, pair.first);
1574 EnableIdleTasks();
1575 message_loop_->RunUntilIdle();
1578 private:
1579 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImplWithMessageLoopTest);
1582 TEST_F(RendererSchedulerImplWithMessageLoopTest,
1583 NonNestableIdleTaskDoesntExecuteInNestedLoop) {
1584 std::vector<std::string> order;
1585 idle_task_runner_->PostIdleTask(
1586 FROM_HERE,
1587 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("1")));
1588 idle_task_runner_->PostIdleTask(
1589 FROM_HERE,
1590 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("2")));
1592 std::vector<std::pair<SingleThreadIdleTaskRunner::IdleTask, bool>>
1593 tasks_to_post_from_nested_loop;
1594 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1595 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("3")),
1596 false));
1597 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1598 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("4")), true));
1599 tasks_to_post_from_nested_loop.push_back(std::make_pair(
1600 base::Bind(&AppendToVectorIdleTestTask, &order, std::string("5")), true));
1602 default_task_runner_->PostTask(
1603 FROM_HERE,
1604 base::Bind(
1605 &RendererSchedulerImplWithMessageLoopTest::PostFromNestedRunloop,
1606 base::Unretained(this),
1607 base::Unretained(&tasks_to_post_from_nested_loop)));
1609 EnableIdleTasks();
1610 RunUntilIdle();
1611 // Note we expect task 3 to run last because it's non-nestable.
1612 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"),
1613 std::string("4"), std::string("5"),
1614 std::string("3")));
1617 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriod) {
1618 base::TimeTicks expected_deadline =
1619 clock_->NowTicks() + maximum_idle_period_duration();
1620 base::TimeTicks deadline_in_task;
1621 int run_count = 0;
1623 idle_task_runner_->PostIdleTask(
1624 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1626 RunUntilIdle();
1627 EXPECT_EQ(0, run_count); // Shouldn't run yet as no idle period.
1629 scheduler_->BeginFrameNotExpectedSoon();
1630 RunUntilIdle();
1631 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
1632 EXPECT_EQ(expected_deadline, deadline_in_task);
1635 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodWithPendingDelayedTask) {
1636 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30);
1637 base::TimeTicks expected_deadline = clock_->NowTicks() + pending_task_delay;
1638 base::TimeTicks deadline_in_task;
1639 int run_count = 0;
1641 idle_task_runner_->PostIdleTask(
1642 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1643 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1644 pending_task_delay);
1646 scheduler_->BeginFrameNotExpectedSoon();
1647 RunUntilIdle();
1648 EXPECT_EQ(1, run_count); // Should have run in a long idle time.
1649 EXPECT_EQ(expected_deadline, deadline_in_task);
1652 TEST_F(RendererSchedulerImplTest,
1653 TestLongIdlePeriodWithLatePendingDelayedTask) {
1654 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10);
1655 base::TimeTicks deadline_in_task;
1656 int run_count = 0;
1658 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1659 pending_task_delay);
1661 // Advance clock until after delayed task was meant to be run.
1662 clock_->Advance(base::TimeDelta::FromMilliseconds(20));
1664 // Post an idle task and BeginFrameNotExpectedSoon to initiate a long idle
1665 // period. Since there is a late pending delayed task this shouldn't actually
1666 // start an idle period.
1667 idle_task_runner_->PostIdleTask(
1668 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1669 scheduler_->BeginFrameNotExpectedSoon();
1670 RunUntilIdle();
1671 EXPECT_EQ(0, run_count);
1673 // After the delayed task has been run we should trigger an idle period.
1674 clock_->Advance(maximum_idle_period_duration());
1675 RunUntilIdle();
1676 EXPECT_EQ(1, run_count);
1679 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodRepeating) {
1680 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1681 std::vector<base::TimeTicks> actual_deadlines;
1682 int run_count = 0;
1684 max_idle_task_reposts = 3;
1685 base::TimeTicks clock_before(clock_->NowTicks());
1686 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10));
1687 idle_task_runner_->PostIdleTask(
1688 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
1689 idle_task_runner_, &run_count, clock_.get(),
1690 idle_task_runtime, &actual_deadlines));
1691 scheduler_->BeginFrameNotExpectedSoon();
1692 RunUntilIdle();
1693 EXPECT_EQ(3, run_count);
1694 EXPECT_THAT(
1695 actual_deadlines,
1696 testing::ElementsAre(
1697 clock_before + maximum_idle_period_duration(),
1698 clock_before + idle_task_runtime + maximum_idle_period_duration(),
1699 clock_before + (2 * idle_task_runtime) +
1700 maximum_idle_period_duration()));
1702 // Check that idle tasks don't run after the idle period ends with a
1703 // new BeginMainFrame.
1704 max_idle_task_reposts = 5;
1705 idle_task_runner_->PostIdleTask(
1706 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask,
1707 idle_task_runner_, &run_count, clock_.get(),
1708 idle_task_runtime, &actual_deadlines));
1709 idle_task_runner_->PostIdleTask(
1710 FROM_HERE, base::Bind(&WillBeginFrameIdleTask,
1711 base::Unretained(scheduler_.get()), clock_.get()));
1712 RunUntilIdle();
1713 EXPECT_EQ(4, run_count);
1716 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodDoesNotWakeScheduler) {
1717 base::TimeTicks deadline_in_task;
1718 int run_count = 0;
1720 // Start a long idle period and get the time it should end.
1721 scheduler_->BeginFrameNotExpectedSoon();
1722 // The scheduler should not run the initiate_next_long_idle_period task if
1723 // there are no idle tasks and no other task woke up the scheduler, thus
1724 // the idle period deadline shouldn't update at the end of the current long
1725 // idle period.
1726 base::TimeTicks idle_period_deadline =
1727 scheduler_->CurrentIdleTaskDeadlineForTesting();
1728 clock_->Advance(maximum_idle_period_duration());
1729 RunUntilIdle();
1731 base::TimeTicks new_idle_period_deadline =
1732 scheduler_->CurrentIdleTaskDeadlineForTesting();
1733 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
1735 // Posting a after-wakeup idle task also shouldn't wake the scheduler or
1736 // initiate the next long idle period.
1737 idle_task_runner_->PostIdleTaskAfterWakeup(
1738 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1739 RunUntilIdle();
1740 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
1741 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline);
1742 EXPECT_EQ(0, run_count);
1744 // Running a normal task should initiate a new long idle period though.
1745 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask));
1746 RunUntilIdle();
1747 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting();
1748 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(),
1749 new_idle_period_deadline);
1751 EXPECT_EQ(1, run_count);
1754 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodInTouchStartPolicy) {
1755 base::TimeTicks deadline_in_task;
1756 int run_count = 0;
1758 idle_task_runner_->PostIdleTask(
1759 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1761 // Observation of touchstart should defer the start of the long idle period.
1762 scheduler_->DidHandleInputEventOnCompositorThread(
1763 FakeInputEvent(blink::WebInputEvent::TouchStart),
1764 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
1765 scheduler_->BeginFrameNotExpectedSoon();
1766 RunUntilIdle();
1767 EXPECT_EQ(0, run_count);
1769 // The long idle period should start after the touchstart policy has finished.
1770 clock_->Advance(priority_escalation_after_input_duration());
1771 RunUntilIdle();
1772 EXPECT_EQ(1, run_count);
1775 void TestCanExceedIdleDeadlineIfRequiredTask(RendererScheduler* scheduler,
1776 bool* can_exceed_idle_deadline_out,
1777 int* run_count,
1778 base::TimeTicks deadline) {
1779 *can_exceed_idle_deadline_out = scheduler->CanExceedIdleDeadlineIfRequired();
1780 (*run_count)++;
1783 TEST_F(RendererSchedulerImplTest, CanExceedIdleDeadlineIfRequired) {
1784 int run_count = 0;
1785 bool can_exceed_idle_deadline = false;
1787 // Should return false if not in an idle period.
1788 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired());
1790 // Should return false for short idle periods.
1791 idle_task_runner_->PostIdleTask(
1792 FROM_HERE,
1793 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1794 &can_exceed_idle_deadline, &run_count));
1795 EnableIdleTasks();
1796 RunUntilIdle();
1797 EXPECT_EQ(1, run_count);
1798 EXPECT_FALSE(can_exceed_idle_deadline);
1800 // Should return false for a long idle period which is shortened due to a
1801 // pending delayed task.
1802 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask),
1803 base::TimeDelta::FromMilliseconds(10));
1804 idle_task_runner_->PostIdleTask(
1805 FROM_HERE,
1806 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1807 &can_exceed_idle_deadline, &run_count));
1808 scheduler_->BeginFrameNotExpectedSoon();
1809 RunUntilIdle();
1810 EXPECT_EQ(2, run_count);
1811 EXPECT_FALSE(can_exceed_idle_deadline);
1813 // Next long idle period will be for the maximum time, so
1814 // CanExceedIdleDeadlineIfRequired should return true.
1815 clock_->Advance(maximum_idle_period_duration());
1816 idle_task_runner_->PostIdleTask(
1817 FROM_HERE,
1818 base::Bind(&TestCanExceedIdleDeadlineIfRequiredTask, scheduler_.get(),
1819 &can_exceed_idle_deadline, &run_count));
1820 RunUntilIdle();
1821 EXPECT_EQ(3, run_count);
1822 EXPECT_TRUE(can_exceed_idle_deadline);
1824 // Next long idle period will be for the maximum time, so
1825 // CanExceedIdleDeadlineIfRequired should return true.
1826 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
1827 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
1828 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
1829 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired());
1832 TEST_F(RendererSchedulerImplTest, TestRendererHiddenIdlePeriod) {
1833 int run_count = 0;
1835 max_idle_task_reposts = 2;
1836 idle_task_runner_->PostIdleTask(
1837 FROM_HERE,
1838 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
1840 // Renderer should start in visible state.
1841 RunUntilIdle();
1842 EXPECT_EQ(0, run_count);
1844 // When we hide the renderer it should start a max deadline idle period, which
1845 // will run an idle task and then immediately start a new idle period, which
1846 // runs the second idle task.
1847 scheduler_->OnRendererHidden();
1848 RunUntilIdle();
1849 EXPECT_EQ(2, run_count);
1851 // Advance time by amount of time by the maximum amount of time we execute
1852 // idle tasks when hidden (plus some slack) - idle period should have ended.
1853 max_idle_task_reposts = 3;
1854 idle_task_runner_->PostIdleTask(
1855 FROM_HERE,
1856 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count));
1857 clock_->Advance(end_idle_when_hidden_delay() +
1858 base::TimeDelta::FromMilliseconds(10));
1859 RunUntilIdle();
1860 EXPECT_EQ(2, run_count);
1863 TEST_F(RendererSchedulerImplTest, TimerQueueEnabledByDefault) {
1864 std::vector<std::string> run_order;
1865 PostTestTasks(&run_order, "T1 T2");
1866 RunUntilIdle();
1867 EXPECT_THAT(run_order,
1868 testing::ElementsAre(std::string("T1"), std::string("T2")));
1871 TEST_F(RendererSchedulerImplTest, SuspendAndResumeTimerQueue) {
1872 std::vector<std::string> run_order;
1873 PostTestTasks(&run_order, "T1 T2");
1875 scheduler_->SuspendTimerQueue();
1876 RunUntilIdle();
1877 EXPECT_TRUE(run_order.empty());
1879 scheduler_->ResumeTimerQueue();
1880 RunUntilIdle();
1881 EXPECT_THAT(run_order,
1882 testing::ElementsAre(std::string("T1"), std::string("T2")));
1885 TEST_F(RendererSchedulerImplTest, MultipleSuspendsNeedMultipleResumes) {
1886 std::vector<std::string> run_order;
1887 PostTestTasks(&run_order, "T1 T2");
1889 scheduler_->SuspendTimerQueue();
1890 scheduler_->SuspendTimerQueue();
1891 scheduler_->SuspendTimerQueue();
1892 RunUntilIdle();
1893 EXPECT_TRUE(run_order.empty());
1895 scheduler_->ResumeTimerQueue();
1896 RunUntilIdle();
1897 EXPECT_TRUE(run_order.empty());
1899 scheduler_->ResumeTimerQueue();
1900 RunUntilIdle();
1901 EXPECT_TRUE(run_order.empty());
1903 scheduler_->ResumeTimerQueue();
1904 RunUntilIdle();
1905 EXPECT_THAT(run_order,
1906 testing::ElementsAre(std::string("T1"), std::string("T2")));
1909 TEST_F(RendererSchedulerImplTest, UseCaseToString) {
1910 CheckAllUseCaseToString();
1913 TEST_F(RendererSchedulerImplTest, MismatchedDidHandleInputEventOnMainThread) {
1914 // This should not DCHECK because there was no corresponding compositor side
1915 // call to DidHandleInputEventOnCompositorThread with
1916 // INPUT_EVENT_ACK_STATE_NOT_CONSUMED. There are legitimate reasons for the
1917 // compositor to not be there and we don't want to make debugging impossible.
1918 scheduler_->DidHandleInputEventOnMainThread(
1919 FakeInputEvent(blink::WebInputEvent::GestureFlingStart));
1922 TEST_F(RendererSchedulerImplTest, BeginMainFrameOnCriticalPath) {
1923 ASSERT_FALSE(scheduler_->BeginMainFrameOnCriticalPath());
1925 cc::BeginFrameArgs begin_frame_args = cc::BeginFrameArgs::Create(
1926 BEGINFRAME_FROM_HERE, clock_->NowTicks(), base::TimeTicks(),
1927 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL);
1928 scheduler_->WillBeginFrame(begin_frame_args);
1929 ASSERT_TRUE(scheduler_->BeginMainFrameOnCriticalPath());
1931 begin_frame_args.on_critical_path = false;
1932 scheduler_->WillBeginFrame(begin_frame_args);
1933 ASSERT_FALSE(scheduler_->BeginMainFrameOnCriticalPath());
1936 TEST_F(RendererSchedulerImplTest, ShutdownPreventsPostingOfNewTasks) {
1937 scheduler_->Shutdown();
1938 std::vector<std::string> run_order;
1939 PostTestTasks(&run_order, "D1 C1");
1940 RunUntilIdle();
1941 EXPECT_TRUE(run_order.empty());
1944 TEST_F(RendererSchedulerImplTest, TestRendererBackgroundedTimerSuspension) {
1945 scheduler_->SetTimerQueueSuspensionWhenBackgroundedEnabled(true);
1947 std::vector<std::string> run_order;
1948 PostTestTasks(&run_order, "T1 T2");
1950 // The background signal will not immediately suspend the timer queue.
1951 scheduler_->OnRendererBackgrounded();
1952 RunUntilIdle();
1953 EXPECT_THAT(run_order,
1954 testing::ElementsAre(std::string("T1"), std::string("T2")));
1956 run_order.clear();
1957 PostTestTasks(&run_order, "T3");
1958 RunUntilIdle();
1959 EXPECT_THAT(run_order, testing::ElementsAre(std::string("T3")));
1961 // Advance the time until after the scheduled timer queue suspension.
1962 run_order.clear();
1963 clock_->Advance(suspend_timers_when_backgrounded_delay() +
1964 base::TimeDelta::FromMilliseconds(10));
1965 RunUntilIdle();
1966 ASSERT_TRUE(run_order.empty());
1968 // Timer tasks should be suspended until the foregrounded signal.
1969 PostTestTasks(&run_order, "T4 T5");
1970 RunUntilIdle();
1971 EXPECT_TRUE(run_order.empty());
1973 scheduler_->OnRendererForegrounded();
1974 RunUntilIdle();
1975 EXPECT_THAT(run_order,
1976 testing::ElementsAre(std::string("T4"), std::string("T5")));
1978 // Subsequent timer tasks should fire as usual.
1979 run_order.clear();
1980 PostTestTasks(&run_order, "T6");
1981 RunUntilIdle();
1982 EXPECT_THAT(run_order, testing::ElementsAre(std::string("T6")));
1985 TEST_F(RendererSchedulerImplTest,
1986 ExpensiveLoadingTasksNotBlockedTillFirstBeginMainFrame) {
1987 std::vector<std::string> run_order;
1989 // RunUntilIdle won't actually run all of the SimpleTestTickClock::Advance
1990 // tasks unless we set AutoAdvanceNow to true :/
1991 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
1993 // Simulate a bunch of expensive loading tasks
1994 for (int i = 0; i < 10; i++) {
1995 loading_task_runner_->PostTask(
1996 FROM_HERE, base::Bind(&base::SimpleTestTickClock::Advance,
1997 base::Unretained(clock_.get()),
1998 base::TimeDelta::FromMilliseconds(500)));
2001 RunUntilIdle();
2003 PostTestTasks(&run_order, "L1 D1");
2004 RunUntilIdle();
2006 EXPECT_EQ(UseCase::NONE, ForceUpdatePolicyAndGetCurrentUseCase());
2007 EXPECT_FALSE(HaveSeenABeginMainframe());
2008 EXPECT_TRUE(LoadingTasksSeemExpensive());
2009 EXPECT_FALSE(TimerTasksSeemExpensive());
2010 EXPECT_THAT(run_order,
2011 testing::ElementsAre(std::string("L1"), std::string("D1")));
2013 // Emit a BeginMainFrame, and the loading task should get blocked.
2014 DoMainFrame();
2015 run_order.clear();
2017 PostTestTasks(&run_order, "L1 D1");
2018 RunUntilIdle();
2020 EXPECT_EQ(RendererScheduler::UseCase::NONE, CurrentUseCase());
2021 EXPECT_TRUE(HaveSeenABeginMainframe());
2022 EXPECT_TRUE(LoadingTasksSeemExpensive());
2023 EXPECT_FALSE(TimerTasksSeemExpensive());
2024 EXPECT_THAT(run_order,
2025 testing::ElementsAre(std::string("L1"), std::string("D1")));
2028 } // namespace scheduler