Chromium Code Reviews| 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 "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" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 { | 128 { |
| 129 return &m_timerWebTaskRunner; | 129 return &m_timerWebTaskRunner; |
| 130 } | 130 } |
| 131 | 131 |
| 132 WebTaskRunner* loadingTaskRunner() override | 132 WebTaskRunner* loadingTaskRunner() override |
| 133 { | 133 { |
| 134 ASSERT_NOT_REACHED(); | 134 ASSERT_NOT_REACHED(); |
| 135 return nullptr; | 135 return nullptr; |
| 136 } | 136 } |
| 137 | 137 |
| 138 void postTimerTaskAt(const WebTraceLocation&, WebTaskRunner::Task* task, dou ble monotonicTime) override | |
| 139 { | |
| 140 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea singTime()) * 1000)); | |
| 141 } | |
| 142 | |
| 143 void runUntilIdle() | 138 void runUntilIdle() |
| 144 { | 139 { |
| 145 while (m_timerTasks.size()) { | 140 while (m_timerTasks.size()) { |
| 146 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); | 141 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); |
| 147 m_timerTasks.top().run(); | 142 m_timerTasks.top().run(); |
| 148 m_timerTasks.pop(); | 143 m_timerTasks.pop(); |
| 149 } | 144 } |
| 150 } | 145 } |
| 151 | 146 |
| 152 void runUntilIdleOrDeadlinePassed(double deadline) | 147 void runUntilIdleOrDeadlinePassed(double deadline) |
| (...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 696 { | 691 { |
| 697 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 692 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
| 698 timer.startRepeating(20, BLINK_FROM_HERE); | 693 timer.startRepeating(20, BLINK_FROM_HERE); |
| 699 | 694 |
| 700 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); | 695 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); |
| 701 } | 696 } |
| 702 | 697 |
| 703 TEST_F(TimerTest, AugmentRepeatInterval) | 698 TEST_F(TimerTest, AugmentRepeatInterval) |
| 704 { | 699 { |
| 705 Timer<TimerTest> timer(this, &TimerTest::countingTask); | 700 Timer<TimerTest> timer(this, &TimerTest::countingTask); |
| 701 fprintf(stderr, "m_startTime = %f\n", m_startTime); | |
|
Sami
2015/11/17 10:12:21
Might want to remove this :)
alex clarke (OOO till 29th)
2015/11/23 17:13:46
Done.
| |
| 706 timer.startRepeating(10, BLINK_FROM_HERE); | 702 timer.startRepeating(10, BLINK_FROM_HERE); |
| 707 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); | 703 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); |
| 708 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); | 704 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); |
| 709 | 705 |
| 710 advanceTimeBy(2.0); | 706 advanceTimeBy(2.0); |
| 711 timer.augmentRepeatInterval(10); | 707 timer.augmentRepeatInterval(10); |
| 712 | 708 |
| 713 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); | 709 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); |
| 714 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); | 710 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); |
| 715 | 711 |
| 716 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); | 712 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); |
| 717 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)) ; | 713 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)) ; |
| 718 } | 714 } |
| 719 | 715 |
| 720 class MockTimerWithAlignment : public TimerBase { | |
| 721 public: | |
| 722 MockTimerWithAlignment() : m_lastFireTime(0.0), m_alignedFireTime(0.0) { } | |
| 723 | |
| 724 void fired() override | |
| 725 { | |
| 726 } | |
| 727 | |
| 728 double alignedFireTime(double fireTime) const override | |
| 729 { | |
| 730 m_lastFireTime = fireTime; | |
| 731 return m_alignedFireTime; | |
| 732 } | |
| 733 | |
| 734 void setAlignedFireTime(double alignedFireTime) | |
| 735 { | |
| 736 m_alignedFireTime = alignedFireTime; | |
| 737 } | |
| 738 | |
| 739 double lastFireTime() const | |
| 740 { | |
| 741 return m_lastFireTime; | |
| 742 } | |
| 743 | |
| 744 private: | |
| 745 mutable double m_lastFireTime; | |
| 746 double m_alignedFireTime; | |
| 747 }; | |
| 748 | |
| 749 TEST_F(TimerTest, TimerAlignment_OneShotZero) | |
| 750 { | |
| 751 MockTimerWithAlignment timer; | |
| 752 timer.setAlignedFireTime(m_startTime + 1.0); | |
| 753 | |
| 754 timer.start(0.0, 0.0, BLINK_FROM_HERE); | |
| 755 | |
| 756 // The nextFireInterval gets overrriden. | |
| 757 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); | |
| 758 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); | |
| 759 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); | |
| 760 } | |
| 761 | |
| 762 TEST_F(TimerTest, TimerAlignment_OneShotNonZero) | |
| 763 { | |
| 764 MockTimerWithAlignment timer; | |
| 765 timer.setAlignedFireTime(m_startTime + 1.0); | |
| 766 | |
| 767 timer.start(0.5, 0.0, BLINK_FROM_HERE); | |
| 768 | |
| 769 // The nextFireInterval gets overrriden. | |
| 770 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); | |
| 771 EXPECT_FLOAT_EQ(0.5, timer.nextUnalignedFireInterval()); | |
| 772 EXPECT_FLOAT_EQ(m_startTime + 0.5, timer.lastFireTime()); | |
| 773 } | |
| 774 | |
| 775 TEST_F(TimerTest, DidChangeAlignmentInterval) | |
| 776 { | |
| 777 MockTimerWithAlignment timer; | |
| 778 timer.setAlignedFireTime(m_startTime + 1.0); | |
| 779 | |
| 780 timer.start(0.0, 0.0, BLINK_FROM_HERE); | |
| 781 | |
| 782 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval()); | |
| 783 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); | |
| 784 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); | |
| 785 | |
| 786 timer.setAlignedFireTime(m_startTime); | |
| 787 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime()); | |
| 788 | |
| 789 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval()); | |
| 790 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval()); | |
| 791 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime()); | |
| 792 } | |
| 793 | |
| 794 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) | 716 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) |
| 795 { | 717 { |
| 796 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); | 718 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); |
| 797 timer.startRepeating(2.0, BLINK_FROM_HERE); | 719 timer.startRepeating(2.0, BLINK_FROM_HERE); |
| 798 | 720 |
| 799 ASSERT(hasOneTimerTask()); | 721 ASSERT(hasOneTimerTask()); |
| 800 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0 | 722 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0 |
| 801 | 723 |
| 802 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0 | 724 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0 |
| 803 advanceTimeBy(1.9); | 725 advanceTimeBy(1.9); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 828 m_startTime + 6.0, | 750 m_startTime + 6.0, |
| 829 m_startTime + 8.0, | 751 m_startTime + 8.0, |
| 830 m_startTime + 10.0, | 752 m_startTime + 10.0, |
| 831 m_startTime + 14.0, | 753 m_startTime + 14.0, |
| 832 m_startTime + 18.0, | 754 m_startTime + 18.0, |
| 833 m_startTime + 28.0)); | 755 m_startTime + 28.0)); |
| 834 } | 756 } |
| 835 | 757 |
| 836 } // namespace | 758 } // namespace |
| 837 } // namespace blink | 759 } // namespace blink |
| OLD | NEW |