OLD | NEW |
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 "config.h" | 5 #include "config.h" |
6 #include "platform/Timer.h" | 6 #include "platform/Timer.h" |
7 | 7 |
8 #include "public/platform/Platform.h" | 8 #include "public/platform/Platform.h" |
9 #include "public/platform/WebScheduler.h" | 9 #include "public/platform/WebScheduler.h" |
10 #include "public/platform/WebThread.h" | 10 #include "public/platform/WebThread.h" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 return nullptr; | 128 return nullptr; |
129 } | 129 } |
130 | 130 |
131 void postTimerTaskAt(const WebTraceLocation&, WebTaskRunner::Task* task, dou
ble monotonicTime) override | 131 void postTimerTaskAt(const WebTraceLocation&, WebTaskRunner::Task* task, dou
ble monotonicTime) override |
132 { | 132 { |
133 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea
singTime()) * 1000)); | 133 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea
singTime()) * 1000)); |
134 } | 134 } |
135 | 135 |
136 void runUntilIdle() | 136 void runUntilIdle() |
137 { | 137 { |
138 while (!m_timerTasks.empty()) { | 138 while (m_timerTasks.size()) { |
139 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); | 139 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); |
140 m_timerTasks.top().run(); | 140 m_timerTasks.top().run(); |
141 m_timerTasks.pop(); | 141 m_timerTasks.pop(); |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 void runUntilIdleOrDeadlinePassed(double deadline) | 145 void runUntilIdleOrDeadlinePassed(double deadline) |
146 { | 146 { |
147 while (!m_timerTasks.empty()) { | 147 while (m_timerTasks.size()) { |
148 if (m_timerTasks.top().runTimeSeconds() > deadline) { | 148 if (m_timerTasks.top().runTimeSeconds() > deadline) { |
149 gCurrentTimeSecs = deadline; | 149 gCurrentTimeSecs = deadline; |
150 break; | 150 break; |
151 } | 151 } |
152 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); | 152 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); |
153 m_timerTasks.top().run(); | 153 m_timerTasks.top().run(); |
154 m_timerTasks.pop(); | 154 m_timerTasks.pop(); |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 void runPendingTasks() | 158 void runPendingTasks() |
159 { | 159 { |
160 while (!m_timerTasks.empty() && m_timerTasks.top().runTimeSeconds() <= g
CurrentTimeSecs) { | 160 while (m_timerTasks.size() && m_timerTasks.top().runTimeSeconds() <= gCu
rrentTimeSecs) { |
161 m_timerTasks.top().run(); | 161 m_timerTasks.top().run(); |
162 m_timerTasks.pop(); | 162 m_timerTasks.pop(); |
163 } | 163 } |
164 } | 164 } |
165 | 165 |
166 bool hasOneTimerTask() const | 166 bool hasOneTimerTask() const |
167 { | 167 { |
168 return m_timerTasks.size() == 1; | 168 return m_timerTasks.size() == 1; |
169 } | 169 } |
170 | 170 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 m_startTime = gCurrentTimeSecs; | 291 m_startTime = gCurrentTimeSecs; |
292 } | 292 } |
293 | 293 |
294 void TearDown() override | 294 void TearDown() override |
295 { | 295 { |
296 Platform::initialize(m_oldPlatform); | 296 Platform::initialize(m_oldPlatform); |
297 } | 297 } |
298 | 298 |
299 void countingTask(Timer<TimerTest>*) | 299 void countingTask(Timer<TimerTest>*) |
300 { | 300 { |
301 m_runTimes.push_back(monotonicallyIncreasingTime()); | 301 m_runTimes.append(monotonicallyIncreasingTime()); |
302 } | 302 } |
303 | 303 |
304 void recordNextFireTimeTask(Timer<TimerTest>* timer) | 304 void recordNextFireTimeTask(Timer<TimerTest>* timer) |
305 { | 305 { |
306 m_nextFireTimes.push_back(monotonicallyIncreasingTime() + timer->nextFir
eInterval()); | 306 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn
terval()); |
307 } | 307 } |
308 | 308 |
309 void advanceTimeBy(double timeSecs) | 309 void advanceTimeBy(double timeSecs) |
310 { | 310 { |
311 gCurrentTimeSecs += timeSecs; | 311 gCurrentTimeSecs += timeSecs; |
312 } | 312 } |
313 | 313 |
314 void runUntilIdle() | 314 void runUntilIdle() |
315 { | 315 { |
316 m_platform->runUntilIdle(); | 316 m_platform->runUntilIdle(); |
(...skipping 14 matching lines...) Expand all Loading... |
331 return m_platform->hasOneTimerTask(); | 331 return m_platform->hasOneTimerTask(); |
332 } | 332 } |
333 | 333 |
334 double nextTimerTaskDelaySecs() const | 334 double nextTimerTaskDelaySecs() const |
335 { | 335 { |
336 return m_platform->nextTimerTaskDelaySecs(); | 336 return m_platform->nextTimerTaskDelaySecs(); |
337 } | 337 } |
338 | 338 |
339 protected: | 339 protected: |
340 double m_startTime; | 340 double m_startTime; |
341 // TODO(alexclarke): Migrate to WTF::Vector and add gmock matcher support. | 341 WTF::Vector<double> m_runTimes; |
342 std::vector<double> m_runTimes; | 342 WTF::Vector<double> m_nextFireTimes; |
343 std::vector<double> m_nextFireTimes; | |
344 | 343 |
345 private: | 344 private: |
346 OwnPtr<TimerTestPlatform> m_platform; | 345 OwnPtr<TimerTestPlatform> m_platform; |
347 Platform* m_oldPlatform; | 346 Platform* m_oldPlatform; |
348 }; | 347 }; |
349 | 348 |
350 TEST_F(TimerTest, StartOneShot_Zero) | 349 TEST_F(TimerTest, StartOneShot_Zero) |
351 { | 350 { |
352 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 351 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
353 timer.startOneShot(0, FROM_HERE); | 352 timer.startOneShot(0, FROM_HERE); |
354 | 353 |
355 ASSERT(hasOneTimerTask()); | 354 ASSERT(hasOneTimerTask()); |
356 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); | 355 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); |
357 | 356 |
358 runUntilIdle(); | 357 runUntilIdle(); |
359 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); | 358 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); |
360 } | 359 } |
361 | 360 |
362 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) | 361 TEST_F(TimerTest, StartOneShot_ZeroAndCancel) |
363 { | 362 { |
364 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 363 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
365 timer.startOneShot(0, FROM_HERE); | 364 timer.startOneShot(0, FROM_HERE); |
366 | 365 |
367 ASSERT(hasOneTimerTask()); | 366 ASSERT(hasOneTimerTask()); |
368 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); | 367 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); |
369 | 368 |
370 timer.stop(); | 369 timer.stop(); |
371 | 370 |
372 runUntilIdle(); | 371 runUntilIdle(); |
373 EXPECT_TRUE(m_runTimes.empty()); | 372 EXPECT_FALSE(m_runTimes.size()); |
374 } | 373 } |
375 | 374 |
376 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) | 375 TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost) |
377 { | 376 { |
378 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 377 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
379 timer.startOneShot(0, FROM_HERE); | 378 timer.startOneShot(0, FROM_HERE); |
380 | 379 |
381 ASSERT(hasOneTimerTask()); | 380 ASSERT(hasOneTimerTask()); |
382 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); | 381 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); |
383 | 382 |
384 timer.stop(); | 383 timer.stop(); |
385 | 384 |
386 runUntilIdle(); | 385 runUntilIdle(); |
387 EXPECT_TRUE(m_runTimes.empty()); | 386 EXPECT_FALSE(m_runTimes.size()); |
388 | 387 |
389 timer.startOneShot(0, FROM_HERE); | 388 timer.startOneShot(0, FROM_HERE); |
390 | 389 |
391 ASSERT(hasOneTimerTask()); | 390 ASSERT(hasOneTimerTask()); |
392 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); | 391 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); |
393 | 392 |
394 runUntilIdle(); | 393 runUntilIdle(); |
395 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); | 394 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime)); |
396 } | 395 } |
397 | 396 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 { | 430 { |
432 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 431 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
433 timer.startOneShot(10, FROM_HERE); | 432 timer.startOneShot(10, FROM_HERE); |
434 | 433 |
435 ASSERT(hasOneTimerTask()); | 434 ASSERT(hasOneTimerTask()); |
436 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 435 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
437 | 436 |
438 timer.stop(); | 437 timer.stop(); |
439 | 438 |
440 runUntilIdle(); | 439 runUntilIdle(); |
441 EXPECT_TRUE(m_runTimes.empty()); | 440 EXPECT_FALSE(m_runTimes.size()); |
442 } | 441 } |
443 | 442 |
444 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) | 443 TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost) |
445 { | 444 { |
446 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 445 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
447 timer.startOneShot(10, FROM_HERE); | 446 timer.startOneShot(10, FROM_HERE); |
448 | 447 |
449 ASSERT(hasOneTimerTask()); | 448 ASSERT(hasOneTimerTask()); |
450 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 449 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
451 | 450 |
452 timer.stop(); | 451 timer.stop(); |
453 | 452 |
454 runUntilIdle(); | 453 runUntilIdle(); |
455 EXPECT_TRUE(m_runTimes.empty()); | 454 EXPECT_FALSE(m_runTimes.size()); |
456 | 455 |
457 double secondPostTime = monotonicallyIncreasingTime(); | 456 double secondPostTime = monotonicallyIncreasingTime(); |
458 timer.startOneShot(10, FROM_HERE); | 457 timer.startOneShot(10, FROM_HERE); |
459 | 458 |
460 ASSERT(hasOneTimerTask()); | 459 ASSERT(hasOneTimerTask()); |
461 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 460 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
462 | 461 |
463 runUntilIdle(); | 462 runUntilIdle(); |
464 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); | 463 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); |
465 } | 464 } |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 m_startTime + 6.0, | 813 m_startTime + 6.0, |
815 m_startTime + 8.0, | 814 m_startTime + 8.0, |
816 m_startTime + 10.0, | 815 m_startTime + 10.0, |
817 m_startTime + 14.0, | 816 m_startTime + 14.0, |
818 m_startTime + 18.0, | 817 m_startTime + 18.0, |
819 m_startTime + 28.0)); | 818 m_startTime + 28.0)); |
820 } | 819 } |
821 | 820 |
822 } // namespace | 821 } // namespace |
823 } // namespace blink | 822 } // namespace blink |
OLD | NEW |