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 "config.h" | 5 #include "config.h" |
6 #include "platform/scheduler/Scheduler.h" | 6 #include "platform/scheduler/Scheduler.h" |
7 | 7 |
8 #include "platform/TestingPlatformSupport.h" | 8 #include "platform/TestingPlatformSupport.h" |
9 #include "platform/TraceLocation.h" | 9 #include "platform/TraceLocation.h" |
10 #include "public/platform/Platform.h" | 10 #include "public/platform/Platform.h" |
11 #include "public/platform/WebThread.h" | 11 #include "public/platform/WebThread.h" |
12 | 12 |
| 13 #include <gmock/gmock.h> |
13 #include <gtest/gtest.h> | 14 #include <gtest/gtest.h> |
| 15 #include <string> |
| 16 #include <vector> |
14 | 17 |
15 using blink::Scheduler; | 18 using blink::Scheduler; |
| 19 using namespace std; |
16 | 20 |
17 namespace { | 21 namespace { |
18 | 22 |
19 class TestMainThread : public blink::WebThread { | 23 class TestMainThread : public blink::WebThread { |
20 public: | 24 public: |
21 // blink::WebThread implementation. | 25 // blink::WebThread implementation. |
22 virtual void postTask(Task* task) OVERRIDE | 26 virtual void postTask(Task* task) OVERRIDE |
23 { | 27 { |
24 m_pendingTasks.append(adoptPtr(task)); | 28 m_pendingTasks.append(adoptPtr(task)); |
25 } | 29 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 private: | 113 private: |
110 TestMainThread m_mainThread; | 114 TestMainThread m_mainThread; |
111 SharedTimerFunction m_sharedTimerFunction; | 115 SharedTimerFunction m_sharedTimerFunction; |
112 bool m_sharedTimerRunning; | 116 bool m_sharedTimerRunning; |
113 double m_sharedTimerFireInterval; | 117 double m_sharedTimerFireInterval; |
114 }; | 118 }; |
115 | 119 |
116 class SchedulerTest : public testing::Test { | 120 class SchedulerTest : public testing::Test { |
117 public: | 121 public: |
118 SchedulerTest() | 122 SchedulerTest() |
| 123 : m_reentrantCount(0) |
| 124 , m_maxRecursion(4) |
119 { | 125 { |
120 Scheduler::initializeOnMainThread(); | 126 Scheduler::initializeOnMainThread(); |
121 m_scheduler = Scheduler::shared(); | 127 m_scheduler = Scheduler::shared(); |
122 } | 128 } |
123 | 129 |
124 ~SchedulerTest() | 130 ~SchedulerTest() |
125 { | 131 { |
126 Scheduler::shutdown(); | 132 Scheduler::shutdown(); |
127 } | 133 } |
128 | 134 |
129 void runPendingTasks() | 135 void runPendingTasks() |
130 { | 136 { |
131 m_platformSupport.runPendingTasks(); | 137 m_platformSupport.runPendingTasks(); |
132 } | 138 } |
133 | 139 |
| 140 void appendToVector(string value) |
| 141 { |
| 142 m_order.push_back(value); |
| 143 } |
| 144 |
| 145 void appendToVectorReentrantTask() |
| 146 { |
| 147 m_reentrantOrder.push_back(m_reentrantCount++); |
| 148 |
| 149 if (m_reentrantCount > m_maxRecursion) |
| 150 return; |
| 151 Scheduler::shared()->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appen
dToVectorReentrantTask, this)); |
| 152 } |
| 153 |
| 154 void appendToVectorReentrantInputTask() |
| 155 { |
| 156 m_reentrantOrder.push_back(m_reentrantCount++); |
| 157 |
| 158 if (m_reentrantCount > m_maxRecursion) |
| 159 return; |
| 160 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendTo
VectorReentrantInputTask, this)); |
| 161 } |
| 162 |
| 163 void appendToVectorReentrantCompositorTask() |
| 164 { |
| 165 m_reentrantOrder.push_back(m_reentrantCount++); |
| 166 |
| 167 if (m_reentrantCount > m_maxRecursion) |
| 168 return; |
| 169 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::app
endToVectorReentrantCompositorTask, this)); |
| 170 } |
| 171 |
134 protected: | 172 protected: |
135 SchedulerTestingPlatformSupport m_platformSupport; | 173 SchedulerTestingPlatformSupport m_platformSupport; |
136 Scheduler* m_scheduler; | 174 Scheduler* m_scheduler; |
| 175 std::vector<string> m_order; |
| 176 std::vector<int> m_reentrantOrder; |
| 177 int m_reentrantCount; |
| 178 int m_maxRecursion; |
137 }; | 179 }; |
138 | 180 |
139 void orderedTestTask(int value, int* result) | 181 void orderedTestTask(int value, int* result) |
140 { | 182 { |
141 *result = (*result << 4) | value; | 183 *result = (*result << 4) | value; |
142 } | 184 } |
143 | 185 |
144 void unorderedTestTask(int value, int* result) | 186 void unorderedTestTask(int value, int* result) |
145 { | 187 { |
146 *result += value; | 188 *result += value; |
147 } | 189 } |
148 | 190 |
149 void idleTestTask(int value, int* result, double allottedTime) | 191 void idleTestTask(int value, int* result, double allottedTime) |
150 { | 192 { |
151 *result += value; | 193 *result += value; |
152 } | 194 } |
153 | 195 |
154 TEST_F(SchedulerTest, TestPostTask) | 196 TEST_F(SchedulerTest, TestPostTask) |
155 { | 197 { |
156 int result = 0; | 198 int result = 0; |
157 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 1, &result)); | 199 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 1, &result)); |
158 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 2, &result)); | 200 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 2, &result)); |
159 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 3, &result)); | 201 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 3, &result)); |
160 m_scheduler->postTask(FROM_HERE, bind(&orderedTestTask, 4, &result)); | 202 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 4, &result)); |
161 runPendingTasks(); | 203 runPendingTasks(); |
162 EXPECT_EQ(0x1234, result); | 204 EXPECT_EQ(0x1234, result); |
163 } | 205 } |
164 | 206 |
165 TEST_F(SchedulerTest, TestPostMixedTaskTypes) | 207 TEST_F(SchedulerTest, TestPostMixedTaskTypes) |
166 { | 208 { |
167 int result = 0; | 209 int result = 0; |
168 m_scheduler->postTask(FROM_HERE, bind(&unorderedTestTask, 1, &result)); | 210 m_scheduler->postTask(FROM_HERE, WTF::bind(&unorderedTestTask, 1, &result)); |
169 m_scheduler->postInputTask(FROM_HERE, bind(&unorderedTestTask, 2, &result)); | 211 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&unorderedTestTask, 2, &resu
lt)); |
170 m_scheduler->postCompositorTask(FROM_HERE, bind(&unorderedTestTask, 4, &resu
lt)); | 212 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&unorderedTestTask, 4,
&result)); |
171 m_scheduler->postTask(FROM_HERE, bind(&unorderedTestTask, 8, &result)); | 213 m_scheduler->postTask(FROM_HERE, WTF::bind(&unorderedTestTask, 8, &result)); |
172 runPendingTasks(); | 214 runPendingTasks(); |
173 EXPECT_EQ(15, result); | 215 EXPECT_EQ(15, result); |
174 } | 216 } |
175 | 217 |
176 int s_sharedTimerTickCount; | 218 int s_sharedTimerTickCount; |
177 void sharedTimerFunction() | 219 void sharedTimerFunction() |
178 { | 220 { |
179 s_sharedTimerTickCount++; | 221 s_sharedTimerTickCount++; |
180 } | 222 } |
181 | 223 |
(...skipping 12 matching lines...) Expand all Loading... |
194 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 236 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
195 | 237 |
196 m_scheduler->setSharedTimerFiredFunction(nullptr); | 238 m_scheduler->setSharedTimerFiredFunction(nullptr); |
197 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 239 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
198 } | 240 } |
199 | 241 |
200 TEST_F(SchedulerTest, TestIdleTask) | 242 TEST_F(SchedulerTest, TestIdleTask) |
201 { | 243 { |
202 // TODO: Check task allottedTime when implemented in the scheduler. | 244 // TODO: Check task allottedTime when implemented in the scheduler. |
203 int result = 0; | 245 int result = 0; |
204 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 246 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re
sult)); |
205 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 247 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re
sult)); |
206 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 248 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re
sult)); |
207 m_scheduler->postIdleTask(bind<double>(&idleTestTask, 1, &result)); | 249 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re
sult)); |
208 runPendingTasks(); | 250 runPendingTasks(); |
209 EXPECT_EQ(4, result); | 251 EXPECT_EQ(4, result); |
210 } | 252 } |
211 | 253 |
| 254 TEST_F(SchedulerTest, TestTaskPrioritization) |
| 255 { |
| 256 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t
his, string("L1"))); |
| 257 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t
his, string("L2"))); |
| 258 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
or, this, string("I1"))); |
| 259 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
or, this, string("I2"))); |
| 260 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVector, this, string("C1"))); |
| 261 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVector, this, string("C2"))); |
| 262 |
| 263 runPendingTasks(); |
| 264 EXPECT_THAT(m_order, testing::ElementsAre( |
| 265 string("I1"), string("I2"), string("C1"), string("C2"), string("L1"), st
ring("L2"))); |
| 266 } |
| 267 |
| 268 TEST_F(SchedulerTest, TestRentrantTask) |
| 269 { |
| 270 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVectorRee
ntrantTask, this)); |
| 271 runPendingTasks(); |
| 272 |
| 273 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); |
| 274 } |
| 275 |
| 276 |
| 277 TEST_F(SchedulerTest, TestRentrantInputTaskDuringShutdown) |
| 278 { |
| 279 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
orReentrantInputTask, this)); |
| 280 Scheduler::shutdown(); |
| 281 |
| 282 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); |
| 283 } |
| 284 |
| 285 TEST_F(SchedulerTest, TestRentrantCompositorTaskDuringShutdown) |
| 286 { |
| 287 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVectorReentrantCompositorTask, this)); |
| 288 Scheduler::shutdown(); |
| 289 |
| 290 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); |
| 291 } |
| 292 |
212 } // namespace | 293 } // namespace |
OLD | NEW |