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 "config.h" | 5 #include "config.h" |
6 #include "platform/Timer.h" | 6 #include "platform/Timer.h" |
7 | 7 |
8 #include "platform/testing/TestingPlatformSupport.h" | 8 #include "platform/testing/TestingPlatformSupport.h" |
9 #include "public/platform/Platform.h" | 9 #include "public/platform/Platform.h" |
10 #include "public/platform/WebScheduler.h" | 10 #include "public/platform/WebScheduler.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 { | 129 { |
130 return &m_timerWebTaskRunner; | 130 return &m_timerWebTaskRunner; |
131 } | 131 } |
132 | 132 |
133 WebTaskRunner* loadingTaskRunner() override | 133 WebTaskRunner* loadingTaskRunner() override |
134 { | 134 { |
135 ASSERT_NOT_REACHED(); | 135 ASSERT_NOT_REACHED(); |
136 return nullptr; | 136 return nullptr; |
137 } | 137 } |
138 | 138 |
| 139 void postTimerTaskAt(const WebTraceLocation&, WebTaskRunner::Task* task, dou
ble monotonicTime) override |
| 140 { |
| 141 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea
singTime()) * 1000)); |
| 142 } |
| 143 |
139 void runUntilIdle() | 144 void runUntilIdle() |
140 { | 145 { |
141 while (m_timerTasks.size()) { | 146 while (m_timerTasks.size()) { |
142 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); | 147 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); |
143 m_timerTasks.top().run(); | 148 m_timerTasks.top().run(); |
144 m_timerTasks.pop(); | 149 m_timerTasks.pop(); |
145 } | 150 } |
146 } | 151 } |
147 | 152 |
148 void runUntilIdleOrDeadlinePassed(double deadline) | 153 void runUntilIdleOrDeadlinePassed(double deadline) |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 advanceTimeBy(2.0); | 691 advanceTimeBy(2.0); |
687 timer.augmentRepeatInterval(10); | 692 timer.augmentRepeatInterval(10); |
688 | 693 |
689 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); | 694 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); |
690 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); | 695 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); |
691 | 696 |
692 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); | 697 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); |
693 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0))
; | 698 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0))
; |
694 } | 699 } |
695 | 700 |
| 701 class MockTimerWithAlignment : public TimerBase { |
| 702 public: |
| 703 MockTimerWithAlignment() : m_lastFireTime(0.0), m_alignedFireTime(0.0) { } |
| 704 |
| 705 void fired() override |
| 706 { |
| 707 } |
| 708 |
| 709 double alignedFireTime(double fireTime) const override |
| 710 { |
| 711 m_lastFireTime = fireTime; |
| 712 return m_alignedFireTime; |
| 713 } |
| 714 |
| 715 void setAlignedFireTime(double alignedFireTime) |
| 716 { |
| 717 m_alignedFireTime = alignedFireTime; |
| 718 } |
| 719 |
| 720 double lastFireTime() const |
| 721 { |
| 722 return m_lastFireTime; |
| 723 } |
| 724 |
| 725 private: |
| 726 mutable double m_lastFireTime; |
| 727 double m_alignedFireTime; |
| 728 }; |
| 729 |
| 730 TEST_F(TimerTest, TimerAlignment_OneShotZero) |
| 731 { |
| 732 MockTimerWithAlignment timer; |
| 733 timer.setAlignedFireTime(m_startTime + 1.0); |
| 734 |
| 735 timer.start(0.0, 0.0, BLINK_FROM_HERE); |
| 736 |
| 737 // The nextFireInterval gets overrriden. |
| 738 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); |
| 739 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); |
| 740 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); |
| 741 } |
| 742 |
| 743 TEST_F(TimerTest, TimerAlignment_OneShotNonZero) |
| 744 { |
| 745 MockTimerWithAlignment timer; |
| 746 timer.setAlignedFireTime(m_startTime + 1.0); |
| 747 |
| 748 timer.start(0.5, 0.0, BLINK_FROM_HERE); |
| 749 |
| 750 // The nextFireInterval gets overrriden. |
| 751 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); |
| 752 EXPECT_FLOAT_EQ(0.5, timer.nextUnalignedFireInterval()); |
| 753 EXPECT_FLOAT_EQ(m_startTime + 0.5, timer.lastFireTime()); |
| 754 } |
| 755 |
| 756 TEST_F(TimerTest, DidChangeAlignmentInterval) |
| 757 { |
| 758 MockTimerWithAlignment timer; |
| 759 timer.setAlignedFireTime(m_startTime + 1.0); |
| 760 |
| 761 timer.start(0.0, 0.0, BLINK_FROM_HERE); |
| 762 |
| 763 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); |
| 764 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); |
| 765 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); |
| 766 |
| 767 timer.setAlignedFireTime(m_startTime); |
| 768 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime()); |
| 769 |
| 770 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); |
| 771 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); |
| 772 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); |
| 773 } |
| 774 |
696 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) | 775 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) |
697 { | 776 { |
698 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); | 777 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); |
699 timer.startRepeating(2.0, BLINK_FROM_HERE); | 778 timer.startRepeating(2.0, BLINK_FROM_HERE); |
700 | 779 |
701 ASSERT(hasOneTimerTask()); | 780 ASSERT(hasOneTimerTask()); |
702 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime
+ 2.0 | 781 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime
+ 2.0 |
703 | 782 |
704 // Simulate timer firing early. Next scheduled task to run at m_startTime +
4.0 | 783 // Simulate timer firing early. Next scheduled task to run at m_startTime +
4.0 |
705 advanceTimeBy(1.9); | 784 advanceTimeBy(1.9); |
(...skipping 24 matching lines...) Expand all Loading... |
730 m_startTime + 6.0, | 809 m_startTime + 6.0, |
731 m_startTime + 8.0, | 810 m_startTime + 8.0, |
732 m_startTime + 10.0, | 811 m_startTime + 10.0, |
733 m_startTime + 14.0, | 812 m_startTime + 14.0, |
734 m_startTime + 18.0, | 813 m_startTime + 18.0, |
735 m_startTime + 28.0)); | 814 m_startTime + 28.0)); |
736 } | 815 } |
737 | 816 |
738 } // namespace | 817 } // namespace |
739 } // namespace blink | 818 } // namespace blink |
OLD | NEW |