Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(667)

Side by Side Diff: third_party/WebKit/Source/platform/TimerTest.cpp

Issue 2266443002: Optimize posting of WTF::Closure and improve scheduler test mocks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cut back to just the WTF::Closure fix plus the scheduler test mock refactor. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "platform/Timer.h" 5 #include "platform/Timer.h"
6 6
7 #include "platform/scheduler/base/task_queue_impl.h"
8 #include "platform/scheduler/child/web_task_runner_impl.h"
9 #include "platform/scheduler/renderer/renderer_scheduler_impl.h"
7 #include "platform/testing/TestingPlatformSupport.h" 10 #include "platform/testing/TestingPlatformSupport.h"
8 #include "public/platform/Platform.h" 11 #include "public/platform/Platform.h"
9 #include "public/platform/WebScheduler.h" 12 #include "public/platform/WebScheduler.h"
10 #include "public/platform/WebThread.h" 13 #include "public/platform/WebThread.h"
11 #include "public/platform/WebViewScheduler.h" 14 #include "public/platform/WebViewScheduler.h"
12 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "wtf/CurrentTime.h"
14 #include "wtf/PtrUtil.h" 18 #include "wtf/PtrUtil.h"
15 #include "wtf/RefCounted.h" 19 #include "wtf/RefCounted.h"
16 #include <memory> 20 #include <memory>
17 #include <queue> 21 #include <queue>
18 22
19 using testing::ElementsAre; 23 using testing::ElementsAre;
20 24
21 namespace blink { 25 namespace blink {
22 namespace { 26 namespace {
23 double gCurrentTimeSecs = 0.0;
24
25 class DelayedTask {
26 public:
27 DelayedTask(WebTaskRunner::Task* task, double delaySeconds)
28 : m_task(task)
29 , m_runTimeSeconds(gCurrentTimeSecs + delaySeconds)
30 , m_delaySeconds(delaySeconds)
31 {
32 }
33
34 bool operator<(const DelayedTask& other) const
35 {
36 return m_runTimeSeconds > other.m_runTimeSeconds;
37 }
38
39 void run() const
40 {
41 m_task->run();
42 }
43
44 double runTimeSeconds() const
45 {
46 return m_runTimeSeconds;
47 }
48
49 double delaySeconds() const
50 {
51 return m_delaySeconds;
52 }
53
54 private:
55 std::unique_ptr<WebTaskRunner::Task> m_task;
56 double m_runTimeSeconds;
57 double m_delaySeconds;
58 };
59
60 class MockWebTaskRunner : public WebTaskRunner {
61 public:
62 explicit MockWebTaskRunner(std::priority_queue<DelayedTask>* timerTasks) : m _timerTasks(timerTasks) { }
63 ~MockWebTaskRunner() override { }
64
65 void postTask(const WebTraceLocation&, Task* task) override
66 {
67 m_timerTasks->push(DelayedTask(task, 0));
68 }
69
70 void postDelayedTask(const WebTraceLocation&, Task* task, double delayMs) ov erride
71 {
72 m_timerTasks->push(DelayedTask(task, delayMs * 0.001));
73 }
74
75 bool runsTasksOnCurrentThread() override
76 {
77 NOTREACHED();
78 return true;
79 }
80
81 std::unique_ptr<WebTaskRunner> clone() override
82 {
83 NOTREACHED();
84 return nullptr;
85 }
86
87 double virtualTimeSeconds() const override
88 {
89 NOTREACHED();
90 return 0.0;
91 }
92
93 double monotonicallyIncreasingVirtualTimeSeconds() const override
94 {
95 return gCurrentTimeSecs;
96 }
97
98 base::SingleThreadTaskRunner* taskRunner() override
99 {
100 ASSERT_NOT_REACHED();
101 return nullptr;
102 }
103
104 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED
105 };
106
107 class MockWebScheduler : public WebScheduler {
108 public:
109 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { }
110 ~MockWebScheduler() override { }
111
112 bool shouldYieldForHighPriorityWork() override
113 {
114 return false;
115 }
116
117 bool canExceedIdleDeadlineIfRequired() override
118 {
119 return false;
120 }
121
122 void postIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override
123 {
124 }
125
126 void postNonNestableIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override
127 {
128 }
129
130 void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*) override
131 {
132 }
133
134 WebTaskRunner* timerTaskRunner() override
135 {
136 return &m_timerWebTaskRunner;
137 }
138
139 WebTaskRunner* loadingTaskRunner() override
140 {
141 NOTREACHED();
142 return nullptr;
143 }
144
145 void runUntilIdle()
146 {
147 while (m_timerTasks.size()) {
148 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds();
149 m_timerTasks.top().run();
150 m_timerTasks.pop();
151 }
152 }
153
154 void runUntilIdleOrDeadlinePassed(double deadline)
155 {
156 while (m_timerTasks.size()) {
157 if (m_timerTasks.top().runTimeSeconds() > deadline) {
158 gCurrentTimeSecs = deadline;
159 break;
160 }
161 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds();
162 m_timerTasks.top().run();
163 m_timerTasks.pop();
164 }
165 }
166
167 void runPendingTasks()
168 {
169 while (m_timerTasks.size() && m_timerTasks.top().runTimeSeconds() <= gCu rrentTimeSecs) {
170 m_timerTasks.top().run();
171 m_timerTasks.pop();
172 }
173 }
174
175 bool hasOneTimerTask() const
176 {
177 return m_timerTasks.size() == 1;
178 }
179
180 double nextTimerTaskDelaySecs() const
181 {
182 ASSERT(hasOneTimerTask());
183 return m_timerTasks.top().delaySeconds();
184 }
185
186 void shutdown() override {}
187 std::unique_ptr<WebViewScheduler> createWebViewScheduler(InterventionReporte r*) override { return nullptr; }
188 void suspendTimerQueue() override { }
189 void resumeTimerQueue() override { }
190 void addPendingNavigation(WebScheduler::NavigatingFrameType) override { }
191 void removePendingNavigation(WebScheduler::NavigatingFrameType) override { }
192 void onNavigationStarted() override { }
193
194 private:
195 std::priority_queue<DelayedTask> m_timerTasks;
196 MockWebTaskRunner m_timerWebTaskRunner;
197 };
198
199 class FakeWebThread : public WebThread {
200 public:
201 FakeWebThread() : m_webScheduler(wrapUnique(new MockWebScheduler())) { }
202 ~FakeWebThread() override { }
203
204 virtual bool isCurrentThread() const
205 {
206 NOTREACHED();
207 return true;
208 }
209
210 virtual PlatformThreadId threadId() const
211 {
212 NOTREACHED();
213 return 0;
214 }
215
216 WebTaskRunner* getWebTaskRunner() override
217 {
218 NOTREACHED();
219 return nullptr;
220 }
221
222 WebScheduler* scheduler() const override
223 {
224 return m_webScheduler.get();
225 }
226
227 virtual void enterRunLoop()
228 {
229 NOTREACHED();
230 }
231
232 virtual void exitRunLoop()
233 {
234 NOTREACHED();
235 }
236
237 private:
238 std::unique_ptr<MockWebScheduler> m_webScheduler;
239 };
240
241 class TimerTestPlatform : public TestingPlatformSupport {
242 public:
243 TimerTestPlatform()
244 : m_webThread(wrapUnique(new FakeWebThread())) { }
245 ~TimerTestPlatform() override { }
246
247 WebThread* currentThread() override
248 {
249 return m_webThread.get();
250 }
251
252 void runUntilIdle()
253 {
254 mockScheduler()->runUntilIdle();
255 }
256
257 void runPendingTasks()
258 {
259 mockScheduler()->runPendingTasks();
260 }
261
262 void runUntilIdleOrDeadlinePassed(double deadline)
263 {
264 mockScheduler()->runUntilIdleOrDeadlinePassed(deadline);
265 }
266
267 bool hasOneTimerTask() const
268 {
269 return mockScheduler()->hasOneTimerTask();
270 }
271
272 double nextTimerTaskDelaySecs() const
273 {
274 return mockScheduler()->nextTimerTaskDelaySecs();
275 }
276
277 private:
278 MockWebScheduler* mockScheduler() const
279 {
280 return static_cast<MockWebScheduler*>(m_webThread->scheduler());
281 }
282
283 std::unique_ptr<FakeWebThread> m_webThread;
284 };
285 27
286 class TimerTest : public testing::Test { 28 class TimerTest : public testing::Test {
287 public: 29 public:
288 void SetUp() override 30 void SetUp() override
289 { 31 {
290 m_runTimes.clear(); 32 m_runTimes.clear();
291 gCurrentTimeSecs = 10.0; 33 m_platform.advanceClockSeconds(10.0);
292 m_startTime = gCurrentTimeSecs; 34 m_startTime = monotonicallyIncreasingTime();
293 } 35 }
294 36
295 void countingTask(TimerBase*) 37 void countingTask(TimerBase*)
296 { 38 {
297 m_runTimes.append(gCurrentTimeSecs); 39 m_runTimes.append(monotonicallyIncreasingTime());
298 } 40 }
299 41
300 void recordNextFireTimeTask(TimerBase* timer) 42 void recordNextFireTimeTask(TimerBase* timer)
301 { 43 {
302 m_nextFireTimes.append(gCurrentTimeSecs + timer->nextFireInterval()); 44 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn terval());
303 } 45 }
304 46
305 void advanceTimeBy(double timeSecs) 47 void runUntilDeadline(double deadline)
306 { 48 {
307 gCurrentTimeSecs += timeSecs; 49 double period = deadline - monotonicallyIncreasingTime();
50 EXPECT_GE(period, 0.0);
51 m_platform.runForPeriodSeconds(period);
52
53 // We may have stopped before the clock advanced to |deadline|.
54 double timeToAdvance = deadline - monotonicallyIncreasingTime();
55 m_platform.advanceClockSeconds(timeToAdvance);
308 } 56 }
309 57
310 void runUntilIdle() 58 // Returns false if there are no pending delayed tasks, otherwise sets |time | to
59 // the delay in seconds till the next pending delayed task is scheduled to f ire.
60 bool timeTillNextDelayedTask(double* time) const
311 { 61 {
312 m_platform.runUntilIdle(); 62 base::TimeTicks nextRunTime;
313 } 63 if (!m_platform.rendererScheduler()->TimerTaskRunner()->GetTimeDomain()- >NextScheduledRunTime(&nextRunTime))
314 64 return false;
315 void runPendingTasks() 65 *time = (nextRunTime - m_platform.rendererScheduler()->TimerTaskRunner() ->GetTimeDomain()->Now()).InSecondsF();
316 { 66 return true;
317 m_platform.runPendingTasks();
318 }
319
320 void runUntilIdleOrDeadlinePassed(double deadline)
321 {
322 m_platform.runUntilIdleOrDeadlinePassed(deadline);
323 }
324
325 bool hasOneTimerTask() const
326 {
327 return m_platform.hasOneTimerTask();
328 }
329
330 double nextTimerTaskDelaySecs() const
331 {
332 return m_platform.nextTimerTaskDelaySecs();
333 } 67 }
334 68
335 protected: 69 protected:
336 double m_startTime; 70 double m_startTime;
337 WTF::Vector<double> m_runTimes; 71 WTF::Vector<double> m_runTimes;
338 WTF::Vector<double> m_nextFireTimes; 72 WTF::Vector<double> m_nextFireTimes;
339 73 TestingPlatformSupportWithMockScheduler m_platform;
340 private:
341 TimerTestPlatform m_platform;
342 }; 74 };
343 75
344 TEST_F(TimerTest, StartOneShot_Zero) 76 TEST_F(TimerTest, StartOneShot_Zero)
345 { 77 {
346 Timer<TimerTest> timer(this, &TimerTest::countingTask); 78 Timer<TimerTest> timer(this, &TimerTest::countingTask);
347 timer.startOneShot(0, BLINK_FROM_HERE); 79 timer.startOneShot(0, BLINK_FROM_HERE);
348 80
349 ASSERT(hasOneTimerTask()); 81 double runTime;
350 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 82 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
351 83
352 runUntilIdle(); 84 m_platform.runUntilIdle();
353 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 85 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
354 } 86 }
355 87
356 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) 88 TEST_F(TimerTest, StartOneShot_ZeroAndCancel)
357 { 89 {
358 Timer<TimerTest> timer(this, &TimerTest::countingTask); 90 Timer<TimerTest> timer(this, &TimerTest::countingTask);
359 timer.startOneShot(0, BLINK_FROM_HERE); 91 timer.startOneShot(0, BLINK_FROM_HERE);
360 92
361 ASSERT(hasOneTimerTask()); 93 double runTime;
362 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 94 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
363 95
364 timer.stop(); 96 timer.stop();
365 97
366 runUntilIdle(); 98 m_platform.runUntilIdle();
367 EXPECT_FALSE(m_runTimes.size()); 99 EXPECT_FALSE(m_runTimes.size());
368 } 100 }
369 101
370 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) 102 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost)
371 { 103 {
372 Timer<TimerTest> timer(this, &TimerTest::countingTask); 104 Timer<TimerTest> timer(this, &TimerTest::countingTask);
373 timer.startOneShot(0, BLINK_FROM_HERE); 105 timer.startOneShot(0, BLINK_FROM_HERE);
374 106
375 ASSERT(hasOneTimerTask()); 107 double runTime;
376 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 108 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
377 109
378 timer.stop(); 110 timer.stop();
379 111
380 runUntilIdle(); 112 m_platform.runUntilIdle();
381 EXPECT_FALSE(m_runTimes.size()); 113 EXPECT_FALSE(m_runTimes.size());
382 114
383 timer.startOneShot(0, BLINK_FROM_HERE); 115 timer.startOneShot(0, BLINK_FROM_HERE);
384 116
385 ASSERT(hasOneTimerTask()); 117 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
386 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
387 118
388 runUntilIdle(); 119 m_platform.runUntilIdle();
389 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 120 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
390 } 121 }
391 122
392 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning) 123 TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning)
393 { 124 {
394 Timer<TimerTest> timer(this, &TimerTest::countingTask); 125 Timer<TimerTest> timer(this, &TimerTest::countingTask);
395 timer.startOneShot(0, BLINK_FROM_HERE); 126 timer.startOneShot(0, BLINK_FROM_HERE);
396 127
397 ASSERT(hasOneTimerTask()); 128 double runTime;
398 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); 129 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
399 130
400 runUntilIdle(); 131 m_platform.runUntilIdle();
401 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); 132 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
402 133
403 timer.startOneShot(0, BLINK_FROM_HERE); 134 timer.startOneShot(0, BLINK_FROM_HERE);
404 135
405 ASSERT(hasOneTimerTask()); 136 EXPECT_FALSE(timeTillNextDelayedTask(&runTime));
406 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs());
407 137
408 runUntilIdle(); 138 m_platform.runUntilIdle();
409 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime)); 139 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime));
410 } 140 }
411 141
412 TEST_F(TimerTest, StartOneShot_NonZero) 142 TEST_F(TimerTest, StartOneShot_NonZero)
413 { 143 {
414 Timer<TimerTest> timer(this, &TimerTest::countingTask); 144 Timer<TimerTest> timer(this, &TimerTest::countingTask);
415 timer.startOneShot(10.0, BLINK_FROM_HERE); 145 timer.startOneShot(10.0, BLINK_FROM_HERE);
416 146
417 ASSERT(hasOneTimerTask()); 147 double runTime;
418 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 148 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
149 EXPECT_FLOAT_EQ(10.0, runTime);
419 150
420 runUntilIdle(); 151 m_platform.runUntilIdle();
421 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 152 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
422 } 153 }
423 154
424 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel) 155 TEST_F(TimerTest, StartOneShot_NonZeroAndCancel)
425 { 156 {
426 Timer<TimerTest> timer(this, &TimerTest::countingTask); 157 Timer<TimerTest> timer(this, &TimerTest::countingTask);
427 timer.startOneShot(10, BLINK_FROM_HERE); 158 timer.startOneShot(10, BLINK_FROM_HERE);
428 159
429 ASSERT(hasOneTimerTask()); 160 double runTime;
430 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 161 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
162 EXPECT_FLOAT_EQ(10.0, runTime);
431 163
432 timer.stop(); 164 timer.stop();
165 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
433 166
434 runUntilIdle(); 167 m_platform.runUntilIdle();
435 EXPECT_FALSE(m_runTimes.size()); 168 EXPECT_FALSE(m_runTimes.size());
436 } 169 }
437 170
438 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) 171 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost)
439 { 172 {
440 Timer<TimerTest> timer(this, &TimerTest::countingTask); 173 Timer<TimerTest> timer(this, &TimerTest::countingTask);
441 timer.startOneShot(10, BLINK_FROM_HERE); 174 timer.startOneShot(10, BLINK_FROM_HERE);
442 175
443 ASSERT(hasOneTimerTask()); 176 double runTime;
444 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 177 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
178 EXPECT_FLOAT_EQ(10.0, runTime);
445 179
446 timer.stop(); 180 timer.stop();
181 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
447 182
448 runUntilIdle(); 183 m_platform.runUntilIdle();
449 EXPECT_FALSE(m_runTimes.size()); 184 EXPECT_FALSE(m_runTimes.size());
450 185
451 double secondPostTime = gCurrentTimeSecs; 186 double secondPostTime = monotonicallyIncreasingTime();
452 timer.startOneShot(10, BLINK_FROM_HERE); 187 timer.startOneShot(10, BLINK_FROM_HERE);
453 188
454 ASSERT(hasOneTimerTask()); 189 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
455 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 190 EXPECT_FLOAT_EQ(10.0, runTime);
456 191
457 runUntilIdle(); 192 m_platform.runUntilIdle();
458 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); 193 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0));
459 } 194 }
460 195
461 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) 196 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning)
462 { 197 {
463 Timer<TimerTest> timer(this, &TimerTest::countingTask); 198 Timer<TimerTest> timer(this, &TimerTest::countingTask);
464 timer.startOneShot(10, BLINK_FROM_HERE); 199 timer.startOneShot(10, BLINK_FROM_HERE);
465 200
466 ASSERT(hasOneTimerTask()); 201 double runTime;
467 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 202 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
203 EXPECT_FLOAT_EQ(10.0, runTime);
468 204
469 runUntilIdle(); 205 m_platform.runUntilIdle();
470 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 206 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
471 207
472 timer.startOneShot(20, BLINK_FROM_HERE); 208 timer.startOneShot(20, BLINK_FROM_HERE);
473 209
474 ASSERT(hasOneTimerTask()); 210 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
475 EXPECT_FLOAT_EQ(20.0, nextTimerTaskDelaySecs()); 211 EXPECT_FLOAT_EQ(20.0, runTime);
476 212
477 runUntilIdle(); 213 m_platform.runUntilIdle();
478 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)) ; 214 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0)) ;
479 } 215 }
480 216
481 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing) 217 TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing)
482 { 218 {
483 Timer<TimerTest> timer(this, &TimerTest::countingTask); 219 Timer<TimerTest> timer(this, &TimerTest::countingTask);
484 timer.startOneShot(10, BLINK_FROM_HERE); 220 timer.startOneShot(10, BLINK_FROM_HERE);
485 timer.startOneShot(10, BLINK_FROM_HERE); 221 timer.startOneShot(10, BLINK_FROM_HERE);
486 222
487 ASSERT(hasOneTimerTask()); 223 double runTime;
488 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); 224 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
225 EXPECT_FLOAT_EQ(10.0, runTime);
489 226
490 runUntilIdle(); 227 m_platform.runUntilIdle();
491 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 228 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
492 } 229 }
493 230
494 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask) 231 TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOriginalTask)
495 { 232 {
496 Timer<TimerTest> timer(this, &TimerTest::countingTask); 233 Timer<TimerTest> timer(this, &TimerTest::countingTask);
497 timer.startOneShot(10, BLINK_FROM_HERE); 234 timer.startOneShot(10, BLINK_FROM_HERE);
498 timer.startOneShot(0, BLINK_FROM_HERE); 235 timer.startOneShot(0, BLINK_FROM_HERE);
499 236
500 runUntilIdle(); 237 m_platform.runUntilIdle();
501 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 0.0)); 238 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 0.0));
502 } 239 }
503 240
504 TEST_F(TimerTest, PostingTimerTwiceWithLaterRunTimeCancelsOriginalTask) 241 TEST_F(TimerTest, PostingTimerTwiceWithLaterRunTimeCancelsOriginalTask)
505 { 242 {
506 Timer<TimerTest> timer(this, &TimerTest::countingTask); 243 Timer<TimerTest> timer(this, &TimerTest::countingTask);
507 timer.startOneShot(0, BLINK_FROM_HERE); 244 timer.startOneShot(0, BLINK_FROM_HERE);
508 timer.startOneShot(10, BLINK_FROM_HERE); 245 timer.startOneShot(10, BLINK_FROM_HERE);
509 246
510 runUntilIdle(); 247 m_platform.runUntilIdle();
511 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0)); 248 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
512 } 249 }
513 250
514 TEST_F(TimerTest, StartRepeatingTask) 251 TEST_F(TimerTest, StartRepeatingTask)
515 { 252 {
516 Timer<TimerTest> timer(this, &TimerTest::countingTask); 253 Timer<TimerTest> timer(this, &TimerTest::countingTask);
517 timer.startRepeating(1.0, BLINK_FROM_HERE); 254 timer.startRepeating(1.0, BLINK_FROM_HERE);
518 255
519 ASSERT(hasOneTimerTask()); 256 double runTime;
520 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs()); 257 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
258 EXPECT_FLOAT_EQ(1.0, runTime);
521 259
522 runUntilIdleOrDeadlinePassed(m_startTime + 5.5); 260 runUntilDeadline(m_startTime + 5.5);
523 EXPECT_THAT(m_runTimes, ElementsAre( 261 EXPECT_THAT(m_runTimes, ElementsAre(
524 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4 .0, m_startTime + 5.0)); 262 m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4 .0, m_startTime + 5.0));
525 } 263 }
526 264
527 TEST_F(TimerTest, StartRepeatingTask_ThenCancel) 265 TEST_F(TimerTest, StartRepeatingTask_ThenCancel)
528 { 266 {
529 Timer<TimerTest> timer(this, &TimerTest::countingTask); 267 Timer<TimerTest> timer(this, &TimerTest::countingTask);
530 timer.startRepeating(1.0, BLINK_FROM_HERE); 268 timer.startRepeating(1.0, BLINK_FROM_HERE);
531 269
532 ASSERT(hasOneTimerTask()); 270 double runTime;
533 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs()); 271 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
272 EXPECT_FLOAT_EQ(1.0, runTime);
534 273
535 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); 274 runUntilDeadline(m_startTime + 2.5);
536 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 275 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
537 276
538 timer.stop(); 277 timer.stop();
539 runUntilIdle(); 278 m_platform.runUntilIdle();
540 279
541 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 280 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
542 } 281 }
543 282
544 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot) 283 TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot)
545 { 284 {
546 Timer<TimerTest> timer(this, &TimerTest::countingTask); 285 Timer<TimerTest> timer(this, &TimerTest::countingTask);
547 timer.startRepeating(1.0, BLINK_FROM_HERE); 286 timer.startRepeating(1.0, BLINK_FROM_HERE);
548 287
549 ASSERT(hasOneTimerTask()); 288 double runTime;
550 EXPECT_FLOAT_EQ(1.0, nextTimerTaskDelaySecs()); 289 EXPECT_TRUE(timeTillNextDelayedTask(&runTime));
290 EXPECT_FLOAT_EQ(1.0, runTime);
551 291
552 runUntilIdleOrDeadlinePassed(m_startTime + 2.5); 292 runUntilDeadline(m_startTime + 2.5);
553 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0)); 293 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
554 294
555 timer.startOneShot(0, BLINK_FROM_HERE); 295 timer.startOneShot(0, BLINK_FROM_HERE);
556 runUntilIdle(); 296 m_platform.runUntilIdle();
557 297
558 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_ startTime + 2.5)); 298 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_ startTime + 2.5));
559 } 299 }
560 300
561 TEST_F(TimerTest, IsActive_NeverPosted) 301 TEST_F(TimerTest, IsActive_NeverPosted)
562 { 302 {
563 Timer<TimerTest> timer(this, &TimerTest::countingTask); 303 Timer<TimerTest> timer(this, &TimerTest::countingTask);
564 304
565 EXPECT_FALSE(timer.isActive()); 305 EXPECT_FALSE(timer.isActive());
566 } 306 }
(...skipping 20 matching lines...) Expand all
587 timer.startRepeating(1.0, BLINK_FROM_HERE); 327 timer.startRepeating(1.0, BLINK_FROM_HERE);
588 328
589 EXPECT_TRUE(timer.isActive()); 329 EXPECT_TRUE(timer.isActive());
590 } 330 }
591 331
592 TEST_F(TimerTest, IsActive_AfterRunning_OneShotZero) 332 TEST_F(TimerTest, IsActive_AfterRunning_OneShotZero)
593 { 333 {
594 Timer<TimerTest> timer(this, &TimerTest::countingTask); 334 Timer<TimerTest> timer(this, &TimerTest::countingTask);
595 timer.startOneShot(0, BLINK_FROM_HERE); 335 timer.startOneShot(0, BLINK_FROM_HERE);
596 336
597 runUntilIdle(); 337 m_platform.runUntilIdle();
598 EXPECT_FALSE(timer.isActive()); 338 EXPECT_FALSE(timer.isActive());
599 } 339 }
600 340
601 TEST_F(TimerTest, IsActive_AfterRunning_OneShotNonZero) 341 TEST_F(TimerTest, IsActive_AfterRunning_OneShotNonZero)
602 { 342 {
603 Timer<TimerTest> timer(this, &TimerTest::countingTask); 343 Timer<TimerTest> timer(this, &TimerTest::countingTask);
604 timer.startOneShot(10, BLINK_FROM_HERE); 344 timer.startOneShot(10, BLINK_FROM_HERE);
605 345
606 runUntilIdle(); 346 m_platform.runUntilIdle();
607 EXPECT_FALSE(timer.isActive()); 347 EXPECT_FALSE(timer.isActive());
608 } 348 }
609 349
610 TEST_F(TimerTest, IsActive_AfterRunning_Repeating) 350 TEST_F(TimerTest, IsActive_AfterRunning_Repeating)
611 { 351 {
612 Timer<TimerTest> timer(this, &TimerTest::countingTask); 352 Timer<TimerTest> timer(this, &TimerTest::countingTask);
613 timer.startRepeating(1.0, BLINK_FROM_HERE); 353 timer.startRepeating(1.0, BLINK_FROM_HERE);
614 354
615 runUntilIdleOrDeadlinePassed(m_startTime + 10); 355 runUntilDeadline(m_startTime + 10);
616 EXPECT_TRUE(timer.isActive()); // It should run until cancelled. 356 EXPECT_TRUE(timer.isActive()); // It should run until cancelled.
617 } 357 }
618 358
619 TEST_F(TimerTest, NextFireInterval_OneShotZero) 359 TEST_F(TimerTest, NextFireInterval_OneShotZero)
620 { 360 {
621 Timer<TimerTest> timer(this, &TimerTest::countingTask); 361 Timer<TimerTest> timer(this, &TimerTest::countingTask);
622 timer.startOneShot(0, BLINK_FROM_HERE); 362 timer.startOneShot(0, BLINK_FROM_HERE);
623 363
624 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 364 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
625 } 365 }
626 366
627 TEST_F(TimerTest, NextFireInterval_OneShotNonZero) 367 TEST_F(TimerTest, NextFireInterval_OneShotNonZero)
628 { 368 {
629 Timer<TimerTest> timer(this, &TimerTest::countingTask); 369 Timer<TimerTest> timer(this, &TimerTest::countingTask);
630 timer.startOneShot(10, BLINK_FROM_HERE); 370 timer.startOneShot(10, BLINK_FROM_HERE);
631 371
632 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 372 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
633 } 373 }
634 374
635 TEST_F(TimerTest, NextFireInterval_OneShotNonZero_AfterAFewSeconds) 375 TEST_F(TimerTest, NextFireInterval_OneShotNonZero_AfterAFewSeconds)
636 { 376 {
377 m_platform.setAutoAdvanceNowToPendingTasks(false);
378
637 Timer<TimerTest> timer(this, &TimerTest::countingTask); 379 Timer<TimerTest> timer(this, &TimerTest::countingTask);
638 timer.startOneShot(10, BLINK_FROM_HERE); 380 timer.startOneShot(10, BLINK_FROM_HERE);
639 381
640 advanceTimeBy(2.0); 382 m_platform.advanceClockSeconds(2.0);
641 EXPECT_FLOAT_EQ(8.0, timer.nextFireInterval()); 383 EXPECT_FLOAT_EQ(8.0, timer.nextFireInterval());
642 } 384 }
643 385
644 TEST_F(TimerTest, NextFireInterval_Repeating) 386 TEST_F(TimerTest, NextFireInterval_Repeating)
645 { 387 {
646 Timer<TimerTest> timer(this, &TimerTest::countingTask); 388 Timer<TimerTest> timer(this, &TimerTest::countingTask);
647 timer.startRepeating(20, BLINK_FROM_HERE); 389 timer.startRepeating(20, BLINK_FROM_HERE);
648 390
649 EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval()); 391 EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval());
650 } 392 }
(...skipping 29 matching lines...) Expand all
680 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 422 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
681 } 423 }
682 424
683 TEST_F(TimerTest, AugmentRepeatInterval) 425 TEST_F(TimerTest, AugmentRepeatInterval)
684 { 426 {
685 Timer<TimerTest> timer(this, &TimerTest::countingTask); 427 Timer<TimerTest> timer(this, &TimerTest::countingTask);
686 timer.startRepeating(10, BLINK_FROM_HERE); 428 timer.startRepeating(10, BLINK_FROM_HERE);
687 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); 429 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval());
688 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 430 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
689 431
690 advanceTimeBy(2.0); 432 m_platform.advanceClockSeconds(2.0);
691 timer.augmentRepeatInterval(10); 433 timer.augmentRepeatInterval(10);
692 434
693 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 435 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
694 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); 436 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval());
695 437
696 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); 438 // NOTE setAutoAdvanceNowToPendingTasks(true) (which uses cc::OrderedSimpleT askRunner)
439 // results in somewhat strange behavior of the test clock which breaks this test.
440 // Specifically the test clock advancing logic ignores newly posted delayed tasks and
441 // advances too far.
442 runUntilDeadline(m_startTime + 50.0);
697 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)) ; 443 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)) ;
698 } 444 }
699 445
700 TEST_F(TimerTest, AugmentRepeatInterval_TimerFireDelayed) 446 TEST_F(TimerTest, AugmentRepeatInterval_TimerFireDelayed)
701 { 447 {
448 m_platform.setAutoAdvanceNowToPendingTasks(false);
449
702 Timer<TimerTest> timer(this, &TimerTest::countingTask); 450 Timer<TimerTest> timer(this, &TimerTest::countingTask);
703 timer.startRepeating(10, BLINK_FROM_HERE); 451 timer.startRepeating(10, BLINK_FROM_HERE);
704 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); 452 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval());
705 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 453 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
706 454
707 advanceTimeBy(123.0); // Make the timer long overdue. 455 m_platform.advanceClockSeconds(123.0); // Make the timer long overdue.
708 timer.augmentRepeatInterval(10); 456 timer.augmentRepeatInterval(10);
709 457
710 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 458 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
711 // The timer is overdue so it should be scheduled to fire immediatly. 459 // The timer is overdue so it should be scheduled to fire immediatly.
712 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); 460 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
713 } 461 }
714 462
715 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) 463 TEST_F(TimerTest, RepeatingTimerDoesNotDrift)
716 { 464 {
465 m_platform.setAutoAdvanceNowToPendingTasks(false);
466
717 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); 467 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask);
718 timer.startRepeating(2.0, BLINK_FROM_HERE); 468 timer.startRepeating(2.0, BLINK_FROM_HERE);
719 469
720 ASSERT(hasOneTimerTask());
721 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0 470 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0
722 471
723 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0 472 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0
724 advanceTimeBy(1.9); 473 m_platform.advanceClockSeconds(1.9);
725 runUntilIdleOrDeadlinePassed(gCurrentTimeSecs + 0.2); 474 runUntilDeadline(monotonicallyIncreasingTime() + 0.2);
726 475
727 advanceTimeBy(2.0); 476 m_platform.runForPeriodSeconds(2.0); // Next scheduled task to run at m_star tTime + 6.0
728 runPendingTasks(); // Next scheduled task to run at m_startTime + 6.0 477 m_platform.runForPeriodSeconds(2.1); // Next scheduled task to run at m_star tTime + 8.0
478 m_platform.runForPeriodSeconds(2.9); // Next scheduled task to run at m_star tTime + 10.0
479 m_platform.runForPeriodSeconds(3.1); // Next scheduled task to run at m_star tTime + 14.0 (skips a beat)
480 m_platform.runForPeriodSeconds(4.0); // Next scheduled task to run at m_star tTime + 18.0 (skips a beat)
481 m_platform.runForPeriodSeconds(10.0); // Next scheduled task to run at m_sta rtTime + 28.0 (skips 5 beats)
729 482
730 advanceTimeBy(2.1);
731 runPendingTasks(); // Next scheduled task to run at m_startTime + 8.0
732
733 advanceTimeBy(2.9);
734 runPendingTasks(); // Next scheduled task to run at m_startTime + 10.0
735
736 advanceTimeBy(3.1);
737 runPendingTasks(); // Next scheduled task to run at m_startTime + 14.0 (skip s a beat)
738
739 advanceTimeBy(4.0);
740 runPendingTasks(); // Next scheduled task to run at m_startTime + 18.0 (skip s a beat)
741
742 advanceTimeBy(10.0); // Next scheduled task to run at m_startTime + 28.0 (sk ips 5 beats)
743 runPendingTasks();
744
745 runUntilIdleOrDeadlinePassed(m_startTime + 5.5);
746 EXPECT_THAT(m_nextFireTimes, ElementsAre( 483 EXPECT_THAT(m_nextFireTimes, ElementsAre(
747 m_startTime + 2.0, 484 m_startTime + 2.0,
748 m_startTime + 4.0, 485 m_startTime + 4.0,
749 m_startTime + 6.0, 486 m_startTime + 6.0,
750 m_startTime + 8.0, 487 m_startTime + 8.0,
751 m_startTime + 10.0, 488 m_startTime + 10.0,
752 m_startTime + 14.0, 489 m_startTime + 14.0,
753 m_startTime + 18.0, 490 m_startTime + 18.0,
754 m_startTime + 28.0)); 491 m_startTime + 28.0));
755 } 492 }
756 493
757 template <typename TimerFiredClass> 494 template <typename TimerFiredClass>
758 class TimerForTest : public TaskRunnerTimer<TimerFiredClass> { 495 class TimerForTest : public TaskRunnerTimer<TimerFiredClass> {
759 public: 496 public:
760 using TimerFiredFunction = typename TaskRunnerTimer<TimerFiredClass>::TimerF iredFunction; 497 using TimerFiredFunction = typename TaskRunnerTimer<TimerFiredClass>::TimerF iredFunction;
761 498
762 ~TimerForTest() override { } 499 ~TimerForTest() override { }
763 500
764 TimerForTest(WebTaskRunner* webTaskRunner, TimerFiredClass* timerFiredClass, TimerFiredFunction timerFiredFunction) 501 TimerForTest(WebTaskRunner* webTaskRunner, TimerFiredClass* timerFiredClass, TimerFiredFunction timerFiredFunction)
765 : TaskRunnerTimer<TimerFiredClass>(webTaskRunner, timerFiredClass, timer FiredFunction) 502 : TaskRunnerTimer<TimerFiredClass>(webTaskRunner, timerFiredClass, timer FiredFunction)
766 { 503 {
767 } 504 }
768 }; 505 };
769 506
770 TEST_F(TimerTest, UserSuppliedWebTaskRunner) 507 TEST_F(TimerTest, UserSuppliedWebTaskRunner)
771 { 508 {
772 std::priority_queue<DelayedTask> timerTasks; 509 scoped_refptr<scheduler::TaskQueue> taskRunner(m_platform.rendererScheduler( )->NewTimerTaskRunner("test"));
773 MockWebTaskRunner taskRunner(&timerTasks); 510 scheduler::WebTaskRunnerImpl webTaskRunner(taskRunner);
774 TimerForTest<TimerTest> timer(&taskRunner, this, &TimerTest::countingTask); 511 TimerForTest<TimerTest> timer(&webTaskRunner, this, &TimerTest::countingTask );
775 timer.startOneShot(0, BLINK_FROM_HERE); 512 timer.startOneShot(0, BLINK_FROM_HERE);
776 513
777 // Make sure the task was posted on taskRunner. 514 // Make sure the task was posted on taskRunner.
778 EXPECT_FALSE(timerTasks.empty()); 515 EXPECT_FALSE(taskRunner->IsEmpty());
779 } 516 }
780 517
781 518
782 } // namespace 519 } // namespace
783 } // namespace blink 520 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/DEPS ('k') | third_party/WebKit/Source/platform/WebTaskRunner.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698