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) |
| 44 { |
| 45 } |
49 | 46 |
50 bool operator<(const DelayedTask& other) const | 47 bool operator<(const DelayedTask& other) const |
51 { | 48 { |
52 return m_runTimeSeconds > other.m_runTimeSeconds; | 49 return m_runTimeSeconds > other.m_runTimeSeconds; |
53 } | 50 } |
54 | 51 |
55 void run() const | 52 void run() const |
56 { | 53 { |
57 m_task->run(); | 54 m_task->run(); |
58 } | 55 } |
(...skipping 28 matching lines...) Expand all Loading... |
87 { | 84 { |
88 m_timerTasks->push(DelayedTask(task, delayMs * 0.001)); | 85 m_timerTasks->push(DelayedTask(task, delayMs * 0.001)); |
89 } | 86 } |
90 | 87 |
91 WebTaskRunner* clone() override | 88 WebTaskRunner* clone() override |
92 { | 89 { |
93 ASSERT_NOT_REACHED(); | 90 ASSERT_NOT_REACHED(); |
94 return nullptr; | 91 return nullptr; |
95 } | 92 } |
96 | 93 |
| 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 |
97 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED | 105 std::priority_queue<DelayedTask>* m_timerTasks; // NOT OWNED |
98 }; | 106 }; |
99 | 107 |
100 class MockWebScheduler : public WebScheduler { | 108 class MockWebScheduler : public WebScheduler { |
101 public: | 109 public: |
102 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { } | 110 MockWebScheduler() : m_timerWebTaskRunner(&m_timerTasks) { } |
103 ~MockWebScheduler() override { } | 111 ~MockWebScheduler() override { } |
104 | 112 |
105 bool shouldYieldForHighPriorityWork() override | 113 bool shouldYieldForHighPriorityWork() override |
106 { | 114 { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); | 281 return static_cast<MockWebScheduler*>(m_webThread->scheduler()); |
274 } | 282 } |
275 | 283 |
276 OwnPtr<FakeWebThread> m_webThread; | 284 OwnPtr<FakeWebThread> m_webThread; |
277 }; | 285 }; |
278 | 286 |
279 class TimerTest : public testing::Test { | 287 class TimerTest : public testing::Test { |
280 public: | 288 public: |
281 void SetUp() override | 289 void SetUp() override |
282 { | 290 { |
283 m_originalTimeFunction = setTimeFunctionsForTesting(currentTime); | |
284 | |
285 m_runTimes.clear(); | 291 m_runTimes.clear(); |
286 gCurrentTimeSecs = 10.0; | 292 gCurrentTimeSecs = 10.0; |
287 m_startTime = gCurrentTimeSecs; | 293 m_startTime = gCurrentTimeSecs; |
288 } | 294 } |
289 | 295 |
290 void TearDown() override | |
291 { | |
292 setTimeFunctionsForTesting(m_originalTimeFunction); | |
293 } | |
294 | |
295 void countingTask(Timer<TimerTest>*) | 296 void countingTask(Timer<TimerTest>*) |
296 { | 297 { |
297 m_runTimes.append(monotonicallyIncreasingTime()); | 298 m_runTimes.append(gCurrentTimeSecs); |
298 } | 299 } |
299 | 300 |
300 void recordNextFireTimeTask(Timer<TimerTest>* timer) | 301 void recordNextFireTimeTask(Timer<TimerTest>* timer) |
301 { | 302 { |
302 m_nextFireTimes.append(monotonicallyIncreasingTime() + timer->nextFireIn
terval()); | 303 m_nextFireTimes.append(gCurrentTimeSecs + timer->nextFireInterval()); |
303 } | 304 } |
304 | 305 |
305 void advanceTimeBy(double timeSecs) | 306 void advanceTimeBy(double timeSecs) |
306 { | 307 { |
307 gCurrentTimeSecs += timeSecs; | 308 gCurrentTimeSecs += timeSecs; |
308 } | 309 } |
309 | 310 |
310 void runUntilIdle() | 311 void runUntilIdle() |
311 { | 312 { |
312 m_platform.runUntilIdle(); | 313 m_platform.runUntilIdle(); |
(...skipping 19 matching lines...) Expand all Loading... |
332 return m_platform.nextTimerTaskDelaySecs(); | 333 return m_platform.nextTimerTaskDelaySecs(); |
333 } | 334 } |
334 | 335 |
335 protected: | 336 protected: |
336 double m_startTime; | 337 double m_startTime; |
337 WTF::Vector<double> m_runTimes; | 338 WTF::Vector<double> m_runTimes; |
338 WTF::Vector<double> m_nextFireTimes; | 339 WTF::Vector<double> m_nextFireTimes; |
339 | 340 |
340 private: | 341 private: |
341 TimerTestPlatform m_platform; | 342 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 = monotonicallyIncreasingTime(); | 452 double secondPostTime = gCurrentTimeSecs; |
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 |