| 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 29 matching lines...) Expand all Loading... |
| 87 { | 82 { |
| 88 m_timerTasks->push(DelayedTask(task, delayMs * 0.001)); | 83 m_timerTasks->push(DelayedTask(task, delayMs * 0.001)); |
| 89 } | 84 } |
| 90 | 85 |
| 91 WebTaskRunner* clone() override | 86 WebTaskRunner* clone() override |
| 92 { | 87 { |
| 93 ASSERT_NOT_REACHED(); | 88 ASSERT_NOT_REACHED(); |
| 94 return nullptr; | 89 return nullptr; |
| 95 } | 90 } |
| 96 | 91 |
| 92 double virtualTimeSeconds() const override |
| 93 { |
| 94 ASSERT_NOT_REACHED(); |
| 95 return 0.0; |
| 96 } |
| 97 |
| 98 double monotonicallyIncreasingVirtualTimeSeconds() const override |
| 99 { |
| 100 return gCurrentTimeSecs; |
| 101 } |
| 102 |
| 97 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED | 103 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED |
| 98 }; | 104 }; |
| 99 | 105 |
| 100 class MockWebScheduler : public WebScheduler { | 106 class MockWebScheduler : public WebScheduler { |
| 101 public: | 107 public: |
| 102 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { } | 108 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { } |
| 103 ~MockWebScheduler() override { } | 109 ~MockWebScheduler() override { } |
| 104 | 110 |
| 105 bool shouldYieldForHighPriorityWork() override | 111 bool shouldYieldForHighPriorityWork() override |
| 106 { | 112 { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); | 279 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); |
| 274 } | 280 } |
| 275 | 281 |
| 276 OwnPtr<FakeWebThread> m_webThread; | 282 OwnPtr<FakeWebThread> m_webThread; |
| 277 }; | 283 }; |
| 278 | 284 |
| 279 class TimerTest : public testing::Test { | 285 class TimerTest : public testing::Test { |
| 280 public: | 286 public: |
| 281 void SetUp() override | 287 void SetUp() override |
| 282 { | 288 { |
| 283 m_originalTimeFunction = setTimeFunctionsForTesting(currentTime); | |
| 284 | |
| 285 m_runTimes.clear(); | 289 m_runTimes.clear(); |
| 286 gCurrentTimeSecs = 10.0; | 290 gCurrentTimeSecs = 10.0; |
| 287 m_startTime = gCurrentTimeSecs; | 291 m_startTime = gCurrentTimeSecs; |
| 288 } | 292 } |
| 289 | 293 |
| 290 void TearDown() override | 294 void TearDown() override |
| 291 { | 295 { |
| 292 setTimeFunctionsForTesting(m_originalTimeFunction); | 296 setTimeFunctionsForTesting(m_originalTimeFunction); |
| 293 } | 297 } |
| 294 | 298 |
| 295 void countingTask(Timer<TimerTest>*) | 299 void countingTask(Timer<TimerTest>*) |
| 296 { | 300 { |
| 297 m_runTimes.append(monotonicallyIncreasingTime()); | 301 m_runTimes.append(gCurrentTimeSecs); |
| 298 } | 302 } |
| 299 | 303 |
| 300 void recordNextFireTimeTask(Timer<TimerTest>* timer) | 304 void recordNextFireTimeTask(Timer<TimerTest>* timer) |
| 301 { | 305 { |
| 302 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn
terval()); | 306 m_nextFireTimes.append(gCurrentTimeSecs + timer->nextFireInterval()); |
| 303 } | 307 } |
| 304 | 308 |
| 305 void advanceTimeBy(double timeSecs) | 309 void advanceTimeBy(double timeSecs) |
| 306 { | 310 { |
| 307 gCurrentTimeSecs += timeSecs; | 311 gCurrentTimeSecs += timeSecs; |
| 308 } | 312 } |
| 309 | 313 |
| 310 void runUntilIdle() | 314 void runUntilIdle() |
| 311 { | 315 { |
| 312 m_platform.runUntilIdle(); | 316 m_platform.runUntilIdle(); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 timer.startOneShot(10, BLINK_FROM_HERE); | 446 timer.startOneShot(10, BLINK_FROM_HERE); |
| 443 | 447 |
| 444 ASSERT(hasOneTimerTask()); | 448 ASSERT(hasOneTimerTask()); |
| 445 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 449 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
| 446 | 450 |
| 447 timer.stop(); | 451 timer.stop(); |
| 448 | 452 |
| 449 runUntilIdle(); | 453 runUntilIdle(); |
| 450 EXPECT_FALSE(m_runTimes.size()); | 454 EXPECT_FALSE(m_runTimes.size()); |
| 451 | 455 |
| 452 double secondPostTime = monotonicallyIncreasingTime(); | 456 double secondPostTime = gCurrentTimeSecs; |
| 453 timer.startOneShot(10, BLINK_FROM_HERE); | 457 timer.startOneShot(10, BLINK_FROM_HERE); |
| 454 | 458 |
| 455 ASSERT(hasOneTimerTask()); | 459 ASSERT(hasOneTimerTask()); |
| 456 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 460 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
| 457 | 461 |
| 458 runUntilIdle(); | 462 runUntilIdle(); |
| 459 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); | 463 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); |
| 460 } | 464 } |
| 461 | 465 |
| 462 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) | 466 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); | 764 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); |
| 761 timer.startOneShot(0, BLINK_FROM_HERE); | 765 timer.startOneShot(0, BLINK_FROM_HERE); |
| 762 | 766 |
| 763 // Make sure the task was posted on taskRunner. | 767 // Make sure the task was posted on taskRunner. |
| 764 EXPECT_FALSE(timerTasks.empty()); | 768 EXPECT_FALSE(timerTasks.empty()); |
| 765 } | 769 } |
| 766 | 770 |
| 767 | 771 |
| 768 } // namespace | 772 } // namespace |
| 769 } // namespace blink | 773 } // namespace blink |
| OLD | NEW |