| 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 | |
| 144 void runUntilIdle() | 139 void runUntilIdle() |
| 145 { | 140 { |
| 146 while (m_timerTasks.size()) { | 141 while (m_timerTasks.size()) { |
| 147 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); | 142 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); |
| 148 m_timerTasks.top().run(); | 143 m_timerTasks.top().run(); |
| 149 m_timerTasks.pop(); | 144 m_timerTasks.pop(); |
| 150 } | 145 } |
| 151 } | 146 } |
| 152 | 147 |
| 153 void runUntilIdleOrDeadlinePassed(double deadline) | 148 void runUntilIdleOrDeadlinePassed(double deadline) |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 advanceTimeBy(2.0); | 686 advanceTimeBy(2.0); |
| 692 timer.augmentRepeatInterval(10); | 687 timer.augmentRepeatInterval(10); |
| 693 | 688 |
| 694 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); | 689 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); |
| 695 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); | 690 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); |
| 696 | 691 |
| 697 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); | 692 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); |
| 698 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0))
; | 693 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0))
; |
| 699 } | 694 } |
| 700 | 695 |
| 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 | |
| 775 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) | 696 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) |
| 776 { | 697 { |
| 777 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); | 698 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); |
| 778 timer.startRepeating(2.0, BLINK_FROM_HERE); | 699 timer.startRepeating(2.0, BLINK_FROM_HERE); |
| 779 | 700 |
| 780 ASSERT(hasOneTimerTask()); | 701 ASSERT(hasOneTimerTask()); |
| 781 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime
+ 2.0 | 702 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime
+ 2.0 |
| 782 | 703 |
| 783 // Simulate timer firing early. Next scheduled task to run at m_startTime +
4.0 | 704 // Simulate timer firing early. Next scheduled task to run at m_startTime +
4.0 |
| 784 advanceTimeBy(1.9); | 705 advanceTimeBy(1.9); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 809 m_startTime + 6.0, | 730 m_startTime + 6.0, |
| 810 m_startTime + 8.0, | 731 m_startTime + 8.0, |
| 811 m_startTime + 10.0, | 732 m_startTime + 10.0, |
| 812 m_startTime + 14.0, | 733 m_startTime + 14.0, |
| 813 m_startTime + 18.0, | 734 m_startTime + 18.0, |
| 814 m_startTime + 28.0)); | 735 m_startTime + 28.0)); |
| 815 } | 736 } |
| 816 | 737 |
| 817 } // namespace | 738 } // namespace |
| 818 } // namespace blink | 739 } // namespace blink |
| OLD | NEW |