| 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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 MockWebTaskRunner m_timerWebTaskRunner; | 184 MockWebTaskRunner m_timerWebTaskRunner; |
| 190 }; | 185 }; |
| 191 | 186 |
| 192 class FakeWebThread : public WebThread { | 187 class FakeWebThread : public WebThread { |
| 193 public: | 188 public: |
| 194 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { } | 189 FakeWebThread() : m_webScheduler(adoptPtr(new MockWebScheduler())) { } |
| 195 ~FakeWebThread() override { } | 190 ~FakeWebThread() override { } |
| 196 | 191 |
| 197 virtual bool isCurrentThread() const | 192 virtual bool isCurrentThread() const |
| 198 { | 193 { |
| 199 ASSERT_NOT_REACHED(); | |
| 200 return true; | 194 return true; |
| 201 } | 195 } |
| 202 | 196 |
| 203 virtual PlatformThreadId threadId() const | 197 virtual PlatformThreadId threadId() const |
| 204 { | 198 { |
| 205 ASSERT_NOT_REACHED(); | 199 ASSERT_NOT_REACHED(); |
| 206 return 0; | 200 return 0; |
| 207 } | 201 } |
| 208 | 202 |
| 209 WebTaskRunner* taskRunner() override | 203 WebTaskRunner* taskRunner() override |
| (...skipping 10 matching lines...) Expand all Loading... |
| 220 virtual void enterRunLoop() | 214 virtual void enterRunLoop() |
| 221 { | 215 { |
| 222 ASSERT_NOT_REACHED(); | 216 ASSERT_NOT_REACHED(); |
| 223 } | 217 } |
| 224 | 218 |
| 225 virtual void exitRunLoop() | 219 virtual void exitRunLoop() |
| 226 { | 220 { |
| 227 ASSERT_NOT_REACHED(); | 221 ASSERT_NOT_REACHED(); |
| 228 } | 222 } |
| 229 | 223 |
| 224 double virtualTimeSeconds() override |
| 225 { |
| 226 ASSERT_NOT_REACHED(); |
| 227 return 0.0; |
| 228 } |
| 229 |
| 230 double monotonicallyIncreasingVirtualTimeSeconds() override |
| 231 { |
| 232 return gCurrentTimeSecs; |
| 233 } |
| 234 |
| 230 private: | 235 private: |
| 231 OwnPtr<MockWebScheduler> m_webScheduler; | 236 OwnPtr<MockWebScheduler> m_webScheduler; |
| 232 }; | 237 }; |
| 233 | 238 |
| 234 class TimerTestPlatform : public TestingPlatformSupport { | 239 class TimerTestPlatform : public TestingPlatformSupport { |
| 235 public: | 240 public: |
| 236 TimerTestPlatform() | 241 TimerTestPlatform() |
| 237 : m_webThread(adoptPtr(new FakeWebThread())) { } | 242 : m_webThread(adoptPtr(new FakeWebThread())) { } |
| 238 ~TimerTestPlatform() override { } | 243 ~TimerTestPlatform() override { } |
| 239 | 244 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); | 278 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); |
| 274 } | 279 } |
| 275 | 280 |
| 276 OwnPtr<FakeWebThread> m_webThread; | 281 OwnPtr<FakeWebThread> m_webThread; |
| 277 }; | 282 }; |
| 278 | 283 |
| 279 class TimerTest : public testing::Test { | 284 class TimerTest : public testing::Test { |
| 280 public: | 285 public: |
| 281 void SetUp() override | 286 void SetUp() override |
| 282 { | 287 { |
| 283 WTF::setMonotonicallyIncreasingTimeFunction(currentTime); | |
| 284 | |
| 285 m_runTimes.clear(); | 288 m_runTimes.clear(); |
| 286 gCurrentTimeSecs = 10.0; | 289 gCurrentTimeSecs = 10.0; |
| 287 m_startTime = gCurrentTimeSecs; | 290 m_startTime = gCurrentTimeSecs; |
| 288 } | 291 } |
| 289 | 292 |
| 290 void countingTask(Timer<TimerTest>*) | 293 void countingTask(Timer<TimerTest>*) |
| 291 { | 294 { |
| 292 m_runTimes.append(monotonicallyIncreasingTime()); | 295 m_runTimes.append(gCurrentTimeSecs); |
| 293 } | 296 } |
| 294 | 297 |
| 295 void recordNextFireTimeTask(Timer<TimerTest>* timer) | 298 void recordNextFireTimeTask(Timer<TimerTest>* timer) |
| 296 { | 299 { |
| 297 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn
terval()); | 300 m_nextFireTimes.append(gCurrentTimeSecs + timer->nextFireInterval()); |
| 298 } | 301 } |
| 299 | 302 |
| 300 void advanceTimeBy(double timeSecs) | 303 void advanceTimeBy(double timeSecs) |
| 301 { | 304 { |
| 302 gCurrentTimeSecs += timeSecs; | 305 gCurrentTimeSecs += timeSecs; |
| 303 } | 306 } |
| 304 | 307 |
| 305 void runUntilIdle() | 308 void runUntilIdle() |
| 306 { | 309 { |
| 307 m_platform.runUntilIdle(); | 310 m_platform.runUntilIdle(); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 timer.startOneShot(10, BLINK_FROM_HERE); | 439 timer.startOneShot(10, BLINK_FROM_HERE); |
| 437 | 440 |
| 438 ASSERT(hasOneTimerTask()); | 441 ASSERT(hasOneTimerTask()); |
| 439 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 442 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
| 440 | 443 |
| 441 timer.stop(); | 444 timer.stop(); |
| 442 | 445 |
| 443 runUntilIdle(); | 446 runUntilIdle(); |
| 444 EXPECT_FALSE(m_runTimes.size()); | 447 EXPECT_FALSE(m_runTimes.size()); |
| 445 | 448 |
| 446 double secondPostTime = monotonicallyIncreasingTime(); | 449 double secondPostTime = gCurrentTimeSecs; |
| 447 timer.startOneShot(10, BLINK_FROM_HERE); | 450 timer.startOneShot(10, BLINK_FROM_HERE); |
| 448 | 451 |
| 449 ASSERT(hasOneTimerTask()); | 452 ASSERT(hasOneTimerTask()); |
| 450 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 453 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
| 451 | 454 |
| 452 runUntilIdle(); | 455 runUntilIdle(); |
| 453 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); | 456 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); |
| 454 } | 457 } |
| 455 | 458 |
| 456 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) | 459 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); | 757 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); |
| 755 timer.startOneShot(0, BLINK_FROM_HERE); | 758 timer.startOneShot(0, BLINK_FROM_HERE); |
| 756 | 759 |
| 757 // Make sure the task was posted on taskRunner. | 760 // Make sure the task was posted on taskRunner. |
| 758 EXPECT_FALSE(timerTasks.empty()); | 761 EXPECT_FALSE(timerTasks.empty()); |
| 759 } | 762 } |
| 760 | 763 |
| 761 | 764 |
| 762 } // namespace | 765 } // namespace |
| 763 } // namespace blink | 766 } // namespace blink |
| OLD | NEW |