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 |
22 // This class exists because gcc doesn't know how to move an OwnPtr. | 27 // This class exists because gcc doesn't know how to move an OwnPtr. |
23 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { | 28 class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> { |
24 public: | 29 public: |
25 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(adoptPt
r(task)) { } | 30 explicit RefCountedTaskContainer(WebTaskRunner::Task* task) : m_task(adoptPt
r(task)) { } |
26 | 31 |
27 ~RefCountedTaskContainer() { } | 32 ~RefCountedTaskContainer() { } |
28 | 33 |
29 void run() | 34 void run() |
30 { | 35 { |
31 m_task->run(); | 36 m_task->run(); |
32 } | 37 } |
33 | 38 |
34 private: | 39 private: |
35 OwnPtr<WebTaskRunner::Task> m_task; | 40 OwnPtr<WebTaskRunner::Task> m_task; |
36 }; | 41 }; |
37 | 42 |
38 class DelayedTask { | 43 class DelayedTask { |
39 public: | 44 public: |
40 DelayedTask(WebTaskRunner::Task* task, double delaySeconds) | 45 DelayedTask(WebTaskRunner::Task* task, double delaySeconds) |
41 : m_task(adoptRef(new RefCountedTaskContainer(task))) | 46 : m_task(adoptRef(new RefCountedTaskContainer(task))) |
42 , m_runTimeSeconds(gCurrentTimeSecs + delaySeconds) | 47 , m_runTimeSeconds(monotonicallyIncreasingTime() + delaySeconds) |
43 , m_delaySeconds(delaySeconds) | 48 , m_delaySeconds(delaySeconds) { } |
44 { | |
45 } | |
46 | 49 |
47 bool operator<(const DelayedTask& other) const | 50 bool operator<(const DelayedTask& other) const |
48 { | 51 { |
49 return m_runTimeSeconds > other.m_runTimeSeconds; | 52 return m_runTimeSeconds > other.m_runTimeSeconds; |
50 } | 53 } |
51 | 54 |
52 void run() const | 55 void run() const |
53 { | 56 { |
54 m_task->run(); | 57 m_task->run(); |
55 } | 58 } |
(...skipping 28 matching lines...) Expand all Loading... |
84 { | 87 { |
85 m_timerTasks->push(DelayedTask(task, delayMs * 0.001)); | 88 m_timerTasks->push(DelayedTask(task, delayMs * 0.001)); |
86 } | 89 } |
87 | 90 |
88 WebTaskRunner* clone() override | 91 WebTaskRunner* clone() override |
89 { | 92 { |
90 ASSERT_NOT_REACHED(); | 93 ASSERT_NOT_REACHED(); |
91 return nullptr; | 94 return nullptr; |
92 } | 95 } |
93 | 96 |
94 double virtualTimeSeconds() const override | |
95 { | |
96 ASSERT_NOT_REACHED(); | |
97 return 0.0; | |
98 } | |
99 | |
100 double monotonicallyIncreasingVirtualTimeSeconds() const override | |
101 { | |
102 return gCurrentTimeSecs; | |
103 } | |
104 | |
105 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED | 97 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED |
106 }; | 98 }; |
107 | 99 |
108 class MockWebScheduler : public WebScheduler { | 100 class MockWebScheduler : public WebScheduler { |
109 public: | 101 public: |
110 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { } | 102 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { } |
111 ~MockWebScheduler() override { } | 103 ~MockWebScheduler() override { } |
112 | 104 |
113 bool shouldYieldForHighPriorityWork() override | 105 bool shouldYieldForHighPriorityWork() override |
114 { | 106 { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); | 273 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); |
282 } | 274 } |
283 | 275 |
284 OwnPtr<FakeWebThread> m_webThread; | 276 OwnPtr<FakeWebThread> m_webThread; |
285 }; | 277 }; |
286 | 278 |
287 class TimerTest : public testing::Test { | 279 class TimerTest : public testing::Test { |
288 public: | 280 public: |
289 void SetUp() override | 281 void SetUp() override |
290 { | 282 { |
| 283 m_originalTimeFunction = setTimeFunctionsForTesting(currentTime); |
| 284 |
291 m_runTimes.clear(); | 285 m_runTimes.clear(); |
292 gCurrentTimeSecs = 10.0; | 286 gCurrentTimeSecs = 10.0; |
293 m_startTime = gCurrentTimeSecs; | 287 m_startTime = gCurrentTimeSecs; |
294 } | 288 } |
295 | 289 |
| 290 void TearDown() override |
| 291 { |
| 292 setTimeFunctionsForTesting(m_originalTimeFunction); |
| 293 } |
| 294 |
296 void countingTask(Timer<TimerTest>*) | 295 void countingTask(Timer<TimerTest>*) |
297 { | 296 { |
298 m_runTimes.append(gCurrentTimeSecs); | 297 m_runTimes.append(monotonicallyIncreasingTime()); |
299 } | 298 } |
300 | 299 |
301 void recordNextFireTimeTask(Timer<TimerTest>* timer) | 300 void recordNextFireTimeTask(Timer<TimerTest>* timer) |
302 { | 301 { |
303 m_nextFireTimes.append(gCurrentTimeSecs + timer->nextFireInterval()); | 302 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn
terval()); |
304 } | 303 } |
305 | 304 |
306 void advanceTimeBy(double timeSecs) | 305 void advanceTimeBy(double timeSecs) |
307 { | 306 { |
308 gCurrentTimeSecs += timeSecs; | 307 gCurrentTimeSecs += timeSecs; |
309 } | 308 } |
310 | 309 |
311 void runUntilIdle() | 310 void runUntilIdle() |
312 { | 311 { |
313 m_platform.runUntilIdle(); | 312 m_platform.runUntilIdle(); |
(...skipping 19 matching lines...) Expand all Loading... |
333 return m_platform.nextTimerTaskDelaySecs(); | 332 return m_platform.nextTimerTaskDelaySecs(); |
334 } | 333 } |
335 | 334 |
336 protected: | 335 protected: |
337 double m_startTime; | 336 double m_startTime; |
338 WTF::Vector<double> m_runTimes; | 337 WTF::Vector<double> m_runTimes; |
339 WTF::Vector<double> m_nextFireTimes; | 338 WTF::Vector<double> m_nextFireTimes; |
340 | 339 |
341 private: | 340 private: |
342 TimerTestPlatform m_platform; | 341 TimerTestPlatform m_platform; |
| 342 TimeFunction m_originalTimeFunction; |
343 }; | 343 }; |
344 | 344 |
345 TEST_F(TimerTest, StartOneShot_Zero) | 345 TEST_F(TimerTest, StartOneShot_Zero) |
346 { | 346 { |
347 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 347 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
348 timer.startOneShot(0, BLINK_FROM_HERE); | 348 timer.startOneShot(0, BLINK_FROM_HERE); |
349 | 349 |
350 ASSERT(hasOneTimerTask()); | 350 ASSERT(hasOneTimerTask()); |
351 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); | 351 EXPECT_FLOAT_EQ(0.0, nextTimerTaskDelaySecs()); |
352 | 352 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 timer.startOneShot(10, BLINK_FROM_HERE); | 442 timer.startOneShot(10, BLINK_FROM_HERE); |
443 | 443 |
444 ASSERT(hasOneTimerTask()); | 444 ASSERT(hasOneTimerTask()); |
445 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 445 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
446 | 446 |
447 timer.stop(); | 447 timer.stop(); |
448 | 448 |
449 runUntilIdle(); | 449 runUntilIdle(); |
450 EXPECT_FALSE(m_runTimes.size()); | 450 EXPECT_FALSE(m_runTimes.size()); |
451 | 451 |
452 double secondPostTime = gCurrentTimeSecs; | 452 double secondPostTime = monotonicallyIncreasingTime(); |
453 timer.startOneShot(10, BLINK_FROM_HERE); | 453 timer.startOneShot(10, BLINK_FROM_HERE); |
454 | 454 |
455 ASSERT(hasOneTimerTask()); | 455 ASSERT(hasOneTimerTask()); |
456 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); | 456 EXPECT_FLOAT_EQ(10.0, nextTimerTaskDelaySecs()); |
457 | 457 |
458 runUntilIdle(); | 458 runUntilIdle(); |
459 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); | 459 EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0)); |
460 } | 460 } |
461 | 461 |
462 TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning) | 462 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); | 760 TimerForTest<TimerTest> timer(this, &TimerTest::countingTask, &taskRunner); |
761 timer.startOneShot(0, BLINK_FROM_HERE); | 761 timer.startOneShot(0, BLINK_FROM_HERE); |
762 | 762 |
763 // Make sure the task was posted on taskRunner. | 763 // Make sure the task was posted on taskRunner. |
764 EXPECT_FALSE(timerTasks.empty()); | 764 EXPECT_FALSE(timerTasks.empty()); |
765 } | 765 } |
766 | 766 |
767 | 767 |
768 } // namespace | 768 } // namespace |
769 } // namespace blink | 769 } // namespace blink |
OLD | NEW |