| 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 "platform/Timer.h" | 5 #include "platform/Timer.h" |
| 6 | 6 |
| 7 #include "platform/testing/TestingPlatformSupport.h" | 7 #include "platform/testing/TestingPlatformSupport.h" |
| 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" |
| 11 #include "public/platform/WebViewScheduler.h" | 11 #include "public/platform/WebViewScheduler.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include <queue> | 14 #include <queue> |
| 15 | 15 |
| 16 using testing::ElementsAre; | 16 using testing::ElementsAre; |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 namespace { | 19 namespace { |
| 20 double gCurrentTimeSecs = 0.0; | 20 double gCurrentTimeSecs = 0.0; |
| 21 | 21 |
| 22 double currentTime() | |
| 23 { | |
| 24 return gCurrentTimeSecs; | |
| 25 } | |
| 26 | |
| 27 // This class exists because gcc doesn't know how to move an OwnPtr. | 22 // This class exists because gcc doesn't know how to move an OwnPtr. |
| 28 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { | 23 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { |
| 29 public: | 24 public: |
| 30 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(adoptPt
r(task)) { } | 25 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(adoptPt
r(task)) { } |
| 31 | 26 |
| 32 ~RefCountedTaskContainer() { } | 27 ~RefCountedTaskContainer() { } |
| 33 | 28 |
| 34 void run() | 29 void run() |
| 35 { | 30 { |
| 36 m_task->run(); | 31 m_task->run(); |
| 37 } | 32 } |
| 38 | 33 |
| 39 private: | 34 private: |
| 40 OwnPtr<WebTaskRunner::Task> m_task; | 35 OwnPtr<WebTaskRunner::Task> m_task; |
| 41 }; | 36 }; |
| 42 | 37 |
| 43 class DelayedTask { | 38 class DelayedTask { |
| 44 public: | 39 public: |
| 45 DelayedTask(WebTaskRunner::Task* task, double delaySeconds) | 40 DelayedTask(WebTaskRunner::Task* task, double delaySeconds) |
| 46 : m_task(adoptRef(new RefCountedTaskContainer(task))) | 41 : m_task(adoptRef(new RefCountedTaskContainer(task))) |
| 47 , m_runTimeSeconds(monotonicallyIncreasingTime() + delaySeconds) | 42 , m_runTimeSeconds(gCurrentTimeSecs + delaySeconds) |
| 48 , m_delaySeconds(delaySeconds) { } | 43 , m_delaySeconds(delaySeconds) { } |
| 49 | 44 |
| 50 bool operator<(const DelayedTask& other) const | 45 bool operator<(const DelayedTask& other) const |
| 51 { | 46 { |
| 52 return m_runTimeSeconds > other.m_runTimeSeconds; | 47 return m_runTimeSeconds > other.m_runTimeSeconds; |
| 53 } | 48 } |
| 54 | 49 |
| 55 void run() const | 50 void run() const |
| 56 { | 51 { |
| 57 m_task->run(); | 52 m_task->run(); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 bool hasOneTimerTask() const | 255 bool hasOneTimerTask() const |
| 261 { | 256 { |
| 262 return mockScheduler()->hasOneTimerTask(); | 257 return mockScheduler()->hasOneTimerTask(); |
| 263 } | 258 } |
| 264 | 259 |
| 265 double nextTimerTaskDelaySecs() const | 260 double nextTimerTaskDelaySecs() const |
| 266 { | 261 { |
| 267 return mockScheduler()->nextTimerTaskDelaySecs(); | 262 return mockScheduler()->nextTimerTaskDelaySecs(); |
| 268 } | 263 } |
| 269 | 264 |
| 265 double monotonicallyIncreasingVirtualTimeSeconds() override |
| 266 { |
| 267 return gCurrentTimeSecs; |
| 268 } |
| 269 |
| 270 private: | 270 private: |
| 271 MockWebScheduler* mockScheduler() const | 271 MockWebScheduler* mockScheduler() const |
| 272 { | 272 { |
| 273 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); | 273 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); |
| 274 } | 274 } |
| 275 | 275 |
| 276 OwnPtr<FakeWebThread> m_webThread; | 276 OwnPtr<FakeWebThread> m_webThread; |
| 277 }; | 277 }; |
| 278 | 278 |
| 279 class TimerTest : public testing::Test { | 279 class TimerTest : public testing::Test { |
| 280 public: | 280 public: |
| 281 void SetUp() override | 281 void SetUp() override |
| 282 { | 282 { |
| 283 WTF::setMonotonicallyIncreasingTimeFunction(currentTime); | |
| 284 | |
| 285 m_runTimes.clear(); | 283 m_runTimes.clear(); |
| 286 gCurrentTimeSecs = 10.0; | 284 gCurrentTimeSecs = 10.0; |
| 287 m_startTime = gCurrentTimeSecs; | 285 m_startTime = gCurrentTimeSecs; |
| 288 } | 286 } |
| 289 | 287 |
| 290 void countingTask(Timer<TimerTest>*) | 288 void countingTask(Timer<TimerTest>*) |
| 291 { | 289 { |
| 292 m_runTimes.append(monotonicallyIncreasingTime()); | 290 m_runTimes.append(gCurrentTimeSecs); |
| 293 } | 291 } |
| 294 | 292 |
| 295 void recordNextFireTimeTask(Timer<TimerTest>* timer) | 293 void recordNextFireTimeTask(Timer<TimerTest>* timer) |
| 296 { | 294 { |
| 297 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn
terval()); | 295 m_nextFireTimes.append(gCurrentTimeSecs + timer->nextFireInterval()); |
| 298 } | 296 } |
| 299 | 297 |
| 300 void advanceTimeBy(double timeSecs) | 298 void advanceTimeBy(double timeSecs) |
| 301 { | 299 { |
| 302 gCurrentTimeSecs += timeSecs; | 300 gCurrentTimeSecs += timeSecs; |
| 303 } | 301 } |
| 304 | 302 |
| 305 void runUntilIdle() | 303 void runUntilIdle() |
| 306 { | 304 { |
| 307 m_platform.runUntilIdle(); | 305 m_platform.runUntilIdle(); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 timer.startOneShot(10, BLINK_FROM_HERE); | 434 timer.startOneShot(10, BLINK_FROM_HERE); |
| 437 | 435 |
| 438 ASSERT(hasOneTimerTask()); | 436 ASSERT(hasOneTimerTask()); |
| 439 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 437 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
| 440 | 438 |
| 441 timer.stop(); | 439 timer.stop(); |
| 442 | 440 |
| 443 runUntilIdle(); | 441 runUntilIdle(); |
| 444 EXPECT_FALSE(m_runTimes.size()); | 442 EXPECT_FALSE(m_runTimes.size()); |
| 445 | 443 |
| 446 double secondPostTime = monotonicallyIncreasingTime(); | 444 double secondPostTime = gCurrentTimeSecs; |
| 447 timer.startOneShot(10, BLINK_FROM_HERE); | 445 timer.startOneShot(10, BLINK_FROM_HERE); |
| 448 | 446 |
| 449 ASSERT(hasOneTimerTask()); | 447 ASSERT(hasOneTimerTask()); |
| 450 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 448 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
| 451 | 449 |
| 452 runUntilIdle(); | 450 runUntilIdle(); |
| 453 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); | 451 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); |
| 454 } | 452 } |
| 455 | 453 |
| 456 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) | 454 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 754 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); | 752 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); |
| 755 timer.startOneShot(0, BLINK_FROM_HERE); | 753 timer.startOneShot(0, BLINK_FROM_HERE); |
| 756 | 754 |
| 757 // Make sure the task was posted on taskRunner. | 755 // Make sure the task was posted on taskRunner. |
| 758 EXPECT_FALSE(timerTasks.empty()); | 756 EXPECT_FALSE(timerTasks.empty()); |
| 759 } | 757 } |
| 760 | 758 |
| 761 | 759 |
| 762 } // namespace | 760 } // namespace |
| 763 } // namespace blink | 761 } // namespace blink |
| OLD | NEW |