OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/scheduler/renderer_scheduler_impl.h" | 5 #include "components/scheduler/renderer/renderer_scheduler_impl.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "cc/output/begin_frame_args.h" | 8 #include "cc/output/begin_frame_args.h" |
9 #include "cc/test/ordered_simple_task_runner.h" | 9 #include "cc/test/ordered_simple_task_runner.h" |
10 #include "cc/test/test_now_source.h" | 10 #include "cc/test/test_now_source.h" |
11 #include "content/child/scheduler/nestable_task_runner_for_test.h" | 11 #include "components/scheduler/child/nestable_task_runner_for_test.h" |
12 #include "content/child/scheduler/scheduler_message_loop_delegate.h" | 12 #include "components/scheduler/child/scheduler_message_loop_delegate.h" |
13 #include "content/test/test_time_source.h" | 13 #include "components/scheduler/child/test_time_source.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace content { | 17 namespace scheduler { |
18 | 18 |
19 namespace { | 19 namespace { |
20 class FakeInputEvent : public blink::WebInputEvent { | 20 class FakeInputEvent : public blink::WebInputEvent { |
21 public: | 21 public: |
22 explicit FakeInputEvent(blink::WebInputEvent::Type event_type) | 22 explicit FakeInputEvent(blink::WebInputEvent::Type event_type) |
23 : WebInputEvent(sizeof(FakeInputEvent)) { | 23 : WebInputEvent(sizeof(FakeInputEvent)) { |
24 type = event_type; | 24 type = event_type; |
25 } | 25 } |
26 | 26 |
27 FakeInputEvent(blink::WebInputEvent::Type event_type, int event_modifiers) | 27 FakeInputEvent(blink::WebInputEvent::Type event_type, int event_modifiers) |
(...skipping 10 matching lines...) Expand all Loading... |
38 | 38 |
39 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, | 39 void AppendToVectorIdleTestTask(std::vector<std::string>* vector, |
40 std::string value, | 40 std::string value, |
41 base::TimeTicks deadline) { | 41 base::TimeTicks deadline) { |
42 AppendToVectorTestTask(vector, value); | 42 AppendToVectorTestTask(vector, value); |
43 } | 43 } |
44 | 44 |
45 void NullTask() { | 45 void NullTask() { |
46 } | 46 } |
47 | 47 |
48 void AppendToVectorReentrantTask( | 48 void AppendToVectorReentrantTask(base::SingleThreadTaskRunner* task_runner, |
49 base::SingleThreadTaskRunner* task_runner, | 49 std::vector<int>* vector, |
50 std::vector<int>* vector, | 50 int* reentrant_count, |
51 int* reentrant_count, | 51 int max_reentrant_count) { |
52 int max_reentrant_count) { | |
53 vector->push_back((*reentrant_count)++); | 52 vector->push_back((*reentrant_count)++); |
54 if (*reentrant_count < max_reentrant_count) { | 53 if (*reentrant_count < max_reentrant_count) { |
55 task_runner->PostTask( | 54 task_runner->PostTask( |
56 FROM_HERE, base::Bind(AppendToVectorReentrantTask, | 55 FROM_HERE, |
57 base::Unretained(task_runner), vector, | 56 base::Bind(AppendToVectorReentrantTask, base::Unretained(task_runner), |
58 reentrant_count, max_reentrant_count)); | 57 vector, reentrant_count, max_reentrant_count)); |
59 } | 58 } |
60 } | 59 } |
61 | 60 |
62 void IdleTestTask(int* run_count, | 61 void IdleTestTask(int* run_count, |
63 base::TimeTicks* deadline_out, | 62 base::TimeTicks* deadline_out, |
64 base::TimeTicks deadline) { | 63 base::TimeTicks deadline) { |
65 (*run_count)++; | 64 (*run_count)++; |
66 *deadline_out = deadline; | 65 *deadline_out = deadline; |
67 } | 66 } |
68 | 67 |
69 int max_idle_task_reposts = 2; | 68 int max_idle_task_reposts = 2; |
70 | 69 |
71 void RepostingIdleTestTask( | 70 void RepostingIdleTestTask(SingleThreadIdleTaskRunner* idle_task_runner, |
72 SingleThreadIdleTaskRunner* idle_task_runner, | 71 int* run_count, |
73 int* run_count, | 72 base::TimeTicks deadline) { |
74 base::TimeTicks deadline) { | |
75 if ((*run_count + 1) < max_idle_task_reposts) { | 73 if ((*run_count + 1) < max_idle_task_reposts) { |
76 idle_task_runner->PostIdleTask( | 74 idle_task_runner->PostIdleTask( |
77 FROM_HERE, | 75 FROM_HERE, base::Bind(&RepostingIdleTestTask, |
78 base::Bind(&RepostingIdleTestTask, base::Unretained(idle_task_runner), | 76 base::Unretained(idle_task_runner), run_count)); |
79 run_count)); | |
80 } | 77 } |
81 (*run_count)++; | 78 (*run_count)++; |
82 } | 79 } |
83 | 80 |
84 void UpdateClockToDeadlineIdleTestTask( | 81 void UpdateClockToDeadlineIdleTestTask( |
85 cc::TestNowSource* clock, | 82 cc::TestNowSource* clock, |
86 base::SingleThreadTaskRunner* task_runner, | 83 base::SingleThreadTaskRunner* task_runner, |
87 int* run_count, | 84 int* run_count, |
88 base::TimeTicks deadline) { | 85 base::TimeTicks deadline) { |
89 clock->SetNow(deadline); | 86 clock->SetNow(deadline); |
90 // Due to the way in which OrderedSimpleTestRunner orders tasks and the fact | 87 // Due to the way in which OrderedSimpleTestRunner orders tasks and the fact |
91 // that we updated the time within a task, the delayed pending task to call | 88 // that we updated the time within a task, the delayed pending task to call |
92 // EndIdlePeriod will not happen until after a TaskQueueManager DoWork, so | 89 // EndIdlePeriod will not happen until after a TaskQueueManager DoWork, so |
93 // post a normal task here to ensure it runs before the next idle task. | 90 // post a normal task here to ensure it runs before the next idle task. |
94 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); | 91 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); |
95 (*run_count)++; | 92 (*run_count)++; |
96 } | 93 } |
97 | 94 |
98 void PostingYieldingTestTask( | 95 void PostingYieldingTestTask(RendererSchedulerImpl* scheduler, |
99 RendererSchedulerImpl* scheduler, | 96 base::SingleThreadTaskRunner* task_runner, |
100 base::SingleThreadTaskRunner* task_runner, | 97 bool simulate_input, |
101 bool simulate_input, | 98 bool* should_yield_before, |
102 bool* should_yield_before, | 99 bool* should_yield_after) { |
103 bool* should_yield_after) { | |
104 *should_yield_before = scheduler->ShouldYieldForHighPriorityWork(); | 100 *should_yield_before = scheduler->ShouldYieldForHighPriorityWork(); |
105 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); | 101 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); |
106 if (simulate_input) { | 102 if (simulate_input) { |
107 scheduler->DidReceiveInputEventOnCompositorThread( | 103 scheduler->DidReceiveInputEventOnCompositorThread( |
108 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); | 104 FakeInputEvent(blink::WebInputEvent::GestureFlingStart)); |
109 } | 105 } |
110 *should_yield_after = scheduler->ShouldYieldForHighPriorityWork(); | 106 *should_yield_after = scheduler->ShouldYieldForHighPriorityWork(); |
111 } | 107 } |
112 | 108 |
113 void AnticipationTestTask(RendererSchedulerImpl* scheduler, | 109 void AnticipationTestTask(RendererSchedulerImpl* scheduler, |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 RunUntilIdle(); | 542 RunUntilIdle(); |
547 EXPECT_TRUE(run_order.empty()); | 543 EXPECT_TRUE(run_order.empty()); |
548 | 544 |
549 // Action events like ScrollBegin will kick us back into compositor priority, | 545 // Action events like ScrollBegin will kick us back into compositor priority, |
550 // allowing service of the timer, loading and idle queues. | 546 // allowing service of the timer, loading and idle queues. |
551 run_order.clear(); | 547 run_order.clear(); |
552 scheduler_->DidReceiveInputEventOnCompositorThread( | 548 scheduler_->DidReceiveInputEventOnCompositorThread( |
553 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin)); | 549 FakeInputEvent(blink::WebInputEvent::GestureScrollBegin)); |
554 RunUntilIdle(); | 550 RunUntilIdle(); |
555 | 551 |
556 EXPECT_THAT(run_order, | 552 EXPECT_THAT(run_order, testing::ElementsAre(std::string("L1"))); |
557 testing::ElementsAre(std::string("L1"))); | |
558 } | 553 } |
559 | 554 |
560 TEST_F(RendererSchedulerImplTest, | 555 TEST_F(RendererSchedulerImplTest, |
561 DidReceiveInputEventOnCompositorThread_IgnoresMouseMove_WhenMouseUp) { | 556 DidReceiveInputEventOnCompositorThread_IgnoresMouseMove_WhenMouseUp) { |
562 std::vector<std::string> run_order; | 557 std::vector<std::string> run_order; |
563 PostTestTasks(&run_order, "I1 D1 C1 D2 C2"); | 558 PostTestTasks(&run_order, "I1 D1 C1 D2 C2"); |
564 | 559 |
565 scheduler_->DidReceiveInputEventOnCompositorThread( | 560 scheduler_->DidReceiveInputEventOnCompositorThread( |
566 FakeInputEvent(blink::WebInputEvent::MouseMove)); | 561 FakeInputEvent(blink::WebInputEvent::MouseMove)); |
567 EnableIdleTasks(); | 562 EnableIdleTasks(); |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1160 } | 1155 } |
1161 | 1156 |
1162 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodWithPendingDelayedTask) { | 1157 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodWithPendingDelayedTask) { |
1163 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30); | 1158 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(30); |
1164 base::TimeTicks expected_deadline = clock_->Now() + pending_task_delay; | 1159 base::TimeTicks expected_deadline = clock_->Now() + pending_task_delay; |
1165 base::TimeTicks deadline_in_task; | 1160 base::TimeTicks deadline_in_task; |
1166 int run_count = 0; | 1161 int run_count = 0; |
1167 | 1162 |
1168 idle_task_runner_->PostIdleTask( | 1163 idle_task_runner_->PostIdleTask( |
1169 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 1164 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
1170 default_task_runner_->PostDelayedTask( | 1165 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
1171 FROM_HERE, base::Bind(&NullTask), pending_task_delay); | 1166 pending_task_delay); |
1172 | 1167 |
1173 scheduler_->BeginFrameNotExpectedSoon(); | 1168 scheduler_->BeginFrameNotExpectedSoon(); |
1174 RunUntilIdle(); | 1169 RunUntilIdle(); |
1175 EXPECT_EQ(1, run_count); // Should have run in a long idle time. | 1170 EXPECT_EQ(1, run_count); // Should have run in a long idle time. |
1176 EXPECT_EQ(expected_deadline, deadline_in_task); | 1171 EXPECT_EQ(expected_deadline, deadline_in_task); |
1177 } | 1172 } |
1178 | 1173 |
1179 TEST_F(RendererSchedulerImplTest, | 1174 TEST_F(RendererSchedulerImplTest, |
1180 TestLongIdlePeriodWithLatePendingDelayedTask) { | 1175 TestLongIdlePeriodWithLatePendingDelayedTask) { |
1181 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10); | 1176 base::TimeDelta pending_task_delay = base::TimeDelta::FromMilliseconds(10); |
1182 base::TimeTicks deadline_in_task; | 1177 base::TimeTicks deadline_in_task; |
1183 int run_count = 0; | 1178 int run_count = 0; |
1184 | 1179 |
1185 default_task_runner_->PostDelayedTask( | 1180 default_task_runner_->PostDelayedTask(FROM_HERE, base::Bind(&NullTask), |
1186 FROM_HERE, base::Bind(&NullTask), pending_task_delay); | 1181 pending_task_delay); |
1187 | 1182 |
1188 // Advance clock until after delayed task was meant to be run. | 1183 // Advance clock until after delayed task was meant to be run. |
1189 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(20)); | 1184 clock_->AdvanceNow(base::TimeDelta::FromMilliseconds(20)); |
1190 | 1185 |
1191 // Post an idle task and BeginFrameNotExpectedSoon to initiate a long idle | 1186 // Post an idle task and BeginFrameNotExpectedSoon to initiate a long idle |
1192 // period. Since there is a late pending delayed task this shouldn't actually | 1187 // period. Since there is a late pending delayed task this shouldn't actually |
1193 // start an idle period. | 1188 // start an idle period. |
1194 idle_task_runner_->PostIdleTask( | 1189 idle_task_runner_->PostIdleTask( |
1195 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | 1190 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
1196 scheduler_->BeginFrameNotExpectedSoon(); | 1191 scheduler_->BeginFrameNotExpectedSoon(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1246 clock_->AdvanceNow(maximum_idle_period_duration()); | 1241 clock_->AdvanceNow(maximum_idle_period_duration()); |
1247 RunUntilIdle(); | 1242 RunUntilIdle(); |
1248 | 1243 |
1249 base::TimeTicks new_idle_period_deadline = | 1244 base::TimeTicks new_idle_period_deadline = |
1250 scheduler_->CurrentIdleTaskDeadlineForTesting(); | 1245 scheduler_->CurrentIdleTaskDeadlineForTesting(); |
1251 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | 1246 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); |
1252 | 1247 |
1253 // Posting a after-wakeup idle task also shouldn't wake the scheduler or | 1248 // Posting a after-wakeup idle task also shouldn't wake the scheduler or |
1254 // initiate the next long idle period. | 1249 // initiate the next long idle period. |
1255 idle_task_runner_->PostIdleTaskAfterWakeup( | 1250 idle_task_runner_->PostIdleTaskAfterWakeup( |
1256 FROM_HERE, | 1251 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
1257 base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
1258 RunUntilIdle(); | 1252 RunUntilIdle(); |
1259 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting(); | 1253 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting(); |
1260 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); | 1254 EXPECT_EQ(idle_period_deadline, new_idle_period_deadline); |
1261 EXPECT_EQ(0, run_count); | 1255 EXPECT_EQ(0, run_count); |
1262 | 1256 |
1263 // Running a normal task should initiate a new long idle period though. | 1257 // Running a normal task should initiate a new long idle period though. |
1264 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); | 1258 default_task_runner_->PostTask(FROM_HERE, base::Bind(&NullTask)); |
1265 RunUntilIdle(); | 1259 RunUntilIdle(); |
1266 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting(); | 1260 new_idle_period_deadline = scheduler_->CurrentIdleTaskDeadlineForTesting(); |
1267 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(), | 1261 EXPECT_EQ(idle_period_deadline + maximum_idle_period_duration(), |
1268 new_idle_period_deadline); | 1262 new_idle_period_deadline); |
1269 | 1263 |
1270 EXPECT_EQ(1, run_count); | 1264 EXPECT_EQ(1, run_count); |
1271 } | 1265 } |
1272 | 1266 |
1273 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodInTouchStartPolicy) { | 1267 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodInTouchStartPolicy) { |
1274 base::TimeTicks deadline_in_task; | 1268 base::TimeTicks deadline_in_task; |
1275 int run_count = 0; | 1269 int run_count = 0; |
1276 | 1270 |
1277 idle_task_runner_->PostIdleTask( | 1271 idle_task_runner_->PostIdleTask( |
1278 FROM_HERE, | 1272 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); |
1279 base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); | |
1280 | 1273 |
1281 // Observation of touchstart should defer the start of the long idle period. | 1274 // Observation of touchstart should defer the start of the long idle period. |
1282 scheduler_->DidReceiveInputEventOnCompositorThread( | 1275 scheduler_->DidReceiveInputEventOnCompositorThread( |
1283 FakeInputEvent(blink::WebInputEvent::TouchStart)); | 1276 FakeInputEvent(blink::WebInputEvent::TouchStart)); |
1284 scheduler_->BeginFrameNotExpectedSoon(); | 1277 scheduler_->BeginFrameNotExpectedSoon(); |
1285 RunUntilIdle(); | 1278 RunUntilIdle(); |
1286 EXPECT_EQ(0, run_count); | 1279 EXPECT_EQ(0, run_count); |
1287 | 1280 |
1288 // The long idle period should start after the touchstart policy has finished. | 1281 // The long idle period should start after the touchstart policy has finished. |
1289 clock_->AdvanceNow(priority_escalation_after_input_duration()); | 1282 clock_->AdvanceNow(priority_escalation_after_input_duration()); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1417 scheduler_->ResumeTimerQueue(); | 1410 scheduler_->ResumeTimerQueue(); |
1418 RunUntilIdle(); | 1411 RunUntilIdle(); |
1419 EXPECT_TRUE(run_order.empty()); | 1412 EXPECT_TRUE(run_order.empty()); |
1420 | 1413 |
1421 scheduler_->ResumeTimerQueue(); | 1414 scheduler_->ResumeTimerQueue(); |
1422 RunUntilIdle(); | 1415 RunUntilIdle(); |
1423 EXPECT_THAT(run_order, | 1416 EXPECT_THAT(run_order, |
1424 testing::ElementsAre(std::string("T1"), std::string("T2"))); | 1417 testing::ElementsAre(std::string("T1"), std::string("T2"))); |
1425 } | 1418 } |
1426 | 1419 |
1427 } // namespace content | 1420 } // namespace scheduler |
OLD | NEW |