| Index: Source/platform/TimerTest.cpp
|
| diff --git a/Source/platform/TimerTest.cpp b/Source/platform/TimerTest.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ee2542e4533ea3dfb955f18041274391ec386ff1
|
| --- /dev/null
|
| +++ b/Source/platform/TimerTest.cpp
|
| @@ -0,0 +1,680 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "config.h"
|
| +#include "platform/Timer.h"
|
| +
|
| +#include "public/platform/Platform.h"
|
| +#include "public/platform/WebScheduler.h"
|
| +#include "public/platform/WebThread.h"
|
| +#include <gmock/gmock.h>
|
| +#include <gtest/gtest.h>
|
| +#include <queue>
|
| +
|
| +using testing::ElementsAre;
|
| +
|
| +namespace blink {
|
| +namespace {
|
| +double gCurrentTimeSecs = 0.0;
|
| +
|
| +double CurrentTime()
|
| +{
|
| + return gCurrentTimeSecs;
|
| +}
|
| +
|
| +// This class exists because gcc doesn't know how to move an OwnPtr.
|
| +class RefCountedTaskContainer : public RefCounted<RefCountedTaskContainer> {
|
| +public:
|
| + explicit RefCountedTaskContainer(WebThread::Task* task) : m_task(adoptPtr(task)) { }
|
| +
|
| + ~RefCountedTaskContainer() { }
|
| +
|
| + void run()
|
| + {
|
| + m_task->run();
|
| + }
|
| +
|
| +private:
|
| + OwnPtr<WebThread::Task> m_task;
|
| +};
|
| +
|
| +class DelayedTask {
|
| +public:
|
| + DelayedTask(WebThread::Task* task, long long delayMs)
|
| + : m_task(adoptRef(new RefCountedTaskContainer(task)))
|
| + , m_runTimeSecs(monotonicallyIncreasingTime() + 0.001 * static_cast<double>(delayMs))
|
| + , m_delayMs(delayMs) { }
|
| +
|
| + bool operator<(const DelayedTask& other) const
|
| + {
|
| + return m_runTimeSecs > other.m_runTimeSecs;
|
| + }
|
| +
|
| + void run() const
|
| + {
|
| + m_task->run();
|
| + }
|
| +
|
| + double runTimeSecs() const
|
| + {
|
| + return m_runTimeSecs;
|
| + }
|
| +
|
| + long long delayMs() const
|
| + {
|
| + return m_delayMs;
|
| + }
|
| +
|
| +private:
|
| + RefPtr<RefCountedTaskContainer> m_task;
|
| + double m_runTimeSecs;
|
| + long long m_delayMs;
|
| +};
|
| +
|
| +class MockWebScheduler : public WebScheduler {
|
| +public:
|
| + explicit MockWebScheduler(std::priority_queue<DelayedTask>* timerTasks) : m_timerTasks(timerTasks) { }
|
| + ~MockWebScheduler() override { }
|
| +
|
| + bool shouldYieldForHighPriorityWork() override
|
| + {
|
| + return false;
|
| + }
|
| +
|
| + bool canExceedIdleDeadlineIfRequired() override
|
| + {
|
| + return false;
|
| + }
|
| +
|
| + void postIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override
|
| + {
|
| + }
|
| +
|
| + void postNonNestableIdleTask(const WebTraceLocation&, WebThread::IdleTask*) override
|
| + {
|
| + }
|
| +
|
| + void postIdleTaskAfterWakeup(const WebTraceLocation&, WebThread::IdleTask*) override
|
| + {
|
| + }
|
| +
|
| + void postLoadingTask(const WebTraceLocation&, WebThread::Task*) override
|
| + {
|
| + }
|
| +
|
| + void postTimerTask(const WebTraceLocation&, WebThread::Task* task, long long delayMs) override
|
| + {
|
| + m_timerTasks->push(DelayedTask(task, delayMs));
|
| + }
|
| +
|
| +private:
|
| + std::priority_queue<DelayedTask>* m_timerTasks;
|
| +};
|
| +
|
| +class FakeWebThread : public WebThread {
|
| +public:
|
| + explicit FakeWebThread(WebScheduler* webScheduler) : m_webScheduler(webScheduler) { }
|
| + ~FakeWebThread() override { }
|
| +
|
| + // WebThread implementation:
|
| + void postTask(const WebTraceLocation&, Task*)
|
| + {
|
| + ASSERT_NOT_REACHED();
|
| + }
|
| +
|
| + virtual void postDelayedTask(const WebTraceLocation&, Task*, long long)
|
| + {
|
| + ASSERT_NOT_REACHED();
|
| + }
|
| +
|
| + virtual bool isCurrentThread() const
|
| + {
|
| + ASSERT_NOT_REACHED();
|
| + return true;
|
| + }
|
| +
|
| + virtual PlatformThreadId threadId() const
|
| + {
|
| + ASSERT_NOT_REACHED();
|
| + return 0;
|
| + }
|
| +
|
| + WebScheduler* scheduler() const override
|
| + {
|
| + return m_webScheduler;
|
| + }
|
| +
|
| + virtual void enterRunLoop()
|
| + {
|
| + ASSERT_NOT_REACHED();
|
| + }
|
| +
|
| + virtual void exitRunLoop()
|
| + {
|
| + ASSERT_NOT_REACHED();
|
| + }
|
| +
|
| +private:
|
| + WebScheduler* m_webScheduler;
|
| +};
|
| +
|
| +class TimerTestPlatform : public Platform {
|
| +public:
|
| + explicit TimerTestPlatform(WebThread* webThread) : m_webThread(webThread) { }
|
| + ~TimerTestPlatform() override { }
|
| +
|
| + WebThread* currentThread() override
|
| + {
|
| + return m_webThread;
|
| + }
|
| +
|
| + void cryptographicallyRandomValues(unsigned char*, size_t) override
|
| + {
|
| + ASSERT_NOT_REACHED();
|
| + }
|
| +
|
| +private:
|
| + WebThread* m_webThread;
|
| +};
|
| +
|
| +class TimerTest : public testing::Test {
|
| +public:
|
| + void SetUp() override
|
| + {
|
| + m_timerTasks = adoptPtr(new std::priority_queue<DelayedTask>);
|
| + m_mockWebScheduler = adoptPtr(new MockWebScheduler(m_timerTasks.get()));
|
| + m_fakeWebThread = adoptPtr(new FakeWebThread(m_mockWebScheduler.get()));
|
| + m_platform = adoptPtr(new TimerTestPlatform(m_fakeWebThread.get()));
|
| + m_oldPlatform = Platform::current();
|
| + Platform::initialize(m_platform.get());
|
| + WTF::setMonotonicallyIncreasingTimeFunction(CurrentTime);
|
| +
|
| + m_runTimes.clear();
|
| + gCurrentTimeSecs = 10.0;
|
| + m_startTime = gCurrentTimeSecs;
|
| + }
|
| +
|
| + void TearDown() override
|
| + {
|
| + Platform::initialize(m_oldPlatform);
|
| + }
|
| +
|
| + void CountingTask(Timer<TimerTest>*)
|
| + {
|
| + m_runTimes.push_back(monotonicallyIncreasingTime());
|
| + }
|
| +
|
| + void AdvanceTimeTo(double timeSecs)
|
| + {
|
| + gCurrentTimeSecs = timeSecs;
|
| + }
|
| +
|
| + void AdvanceTimeBy(double timeSecs)
|
| + {
|
| + gCurrentTimeSecs += timeSecs;
|
| + }
|
| +
|
| + void RunUntilIdle()
|
| + {
|
| + while (!m_timerTasks->empty()) {
|
| + AdvanceTimeTo(m_timerTasks->top().runTimeSecs());
|
| + m_timerTasks->top().run();
|
| + m_timerTasks->pop();
|
| + }
|
| + }
|
| +
|
| + void RunUntilIdleOrDeadlinePassed(double deadline)
|
| + {
|
| + while (!m_timerTasks->empty() && m_timerTasks->top().runTimeSecs() < deadline) {
|
| + AdvanceTimeTo(m_timerTasks->top().runTimeSecs());
|
| + m_timerTasks->top().run();
|
| + m_timerTasks->pop();
|
| + }
|
| + }
|
| +
|
| +protected:
|
| + double m_startTime;
|
| + std::vector<double> m_runTimes;
|
| + OwnPtr<std::priority_queue<DelayedTask>> m_timerTasks;
|
| +
|
| +private:
|
| + OwnPtr<MockWebScheduler> m_mockWebScheduler;
|
| + OwnPtr<FakeWebThread> m_fakeWebThread;
|
| + OwnPtr<TimerTestPlatform> m_platform;
|
| + Platform* m_oldPlatform;
|
| +};
|
| +
|
| +TEST_F(TimerTest, StartOneShot_Zero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(0ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartOneShot_ZeroAndCancel)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(0ll, m_timerTasks->top().delayMs());
|
| +
|
| + timer.stop();
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_TRUE(m_runTimes.empty());
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartOneShot_ZeroAndCancelThenRepost)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(0ll, m_timerTasks->top().delayMs());
|
| +
|
| + timer.stop();
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_TRUE(m_runTimes.empty());
|
| +
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(0ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartOneShot_Zero_RepostingAfterRunning)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(0ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime));
|
| +
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(0ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime, m_startTime));
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartOneShot_NonZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10.0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(10000ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartOneShot_NonZeroAndCancel)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(10000ll, m_timerTasks->top().delayMs());
|
| +
|
| + timer.stop();
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_TRUE(m_runTimes.empty());
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartOneShot_NonZeroAndCancelThenRepost)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(10000ll, m_timerTasks->top().delayMs());
|
| +
|
| + timer.stop();
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_TRUE(m_runTimes.empty());
|
| +
|
| + double secondPostTime = monotonicallyIncreasingTime();
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(10000ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(secondPostTime + 10.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartOneShot_NonZero_RepostingAfterRunning)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(10000ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
|
| +
|
| + timer.startOneShot(20, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(20000ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0, m_startTime + 30.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, PostingTimerTwiceWithSameRunTimeDoesNothing)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(10000ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, PostingTimerTwiceWithNewerRunTimeCancelsOrigionalTask)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 0.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, PostingTimerTwiceWithLaterRunTimeCancelsOrigionalTask)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 10.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartRepeatingTask)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startRepeating(1.0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(1000ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdleOrDeadlinePassed(m_startTime + 5.5);
|
| + EXPECT_THAT(m_runTimes, ElementsAre(
|
| + m_startTime + 1.0, m_startTime + 2.0, m_startTime + 3.0, m_startTime + 4.0, m_startTime + 5.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartRepeatingTask_ThenCancel)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startRepeating(1.0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(1000ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdleOrDeadlinePassed(m_startTime + 2.5);
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
|
| +
|
| + timer.stop();
|
| + RunUntilIdle();
|
| +
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, StartRepeatingTask_ThenPostOneShot)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startRepeating(1.0, FROM_HERE);
|
| +
|
| + ASSERT_EQ(1ul, m_timerTasks->size());
|
| + EXPECT_EQ(1000ll, m_timerTasks->top().delayMs());
|
| +
|
| + RunUntilIdleOrDeadlinePassed(m_startTime + 2.5);
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0));
|
| +
|
| + timer.startOneShot(0, FROM_HERE);
|
| + RunUntilIdle();
|
| +
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 1.0, m_startTime + 2.0, m_startTime + 2.0));
|
| +}
|
| +
|
| +TEST_F(TimerTest, IsActive_NeverPosted)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| +
|
| + EXPECT_FALSE(timer.isActive());
|
| +}
|
| +
|
| +TEST_F(TimerTest, IsActive_AfterPosting_OneShotZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + EXPECT_TRUE(timer.isActive());
|
| +}
|
| +
|
| +TEST_F(TimerTest, IsActive_AfterPosting_OneShotNonZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + EXPECT_TRUE(timer.isActive());
|
| +}
|
| +
|
| +TEST_F(TimerTest, IsActive_AfterPosting_Repeating)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startRepeating(1.0, FROM_HERE);
|
| +
|
| + EXPECT_TRUE(timer.isActive());
|
| +}
|
| +
|
| +TEST_F(TimerTest, IsActive_AfterRunning_OneShotZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_FALSE(timer.isActive());
|
| +}
|
| +
|
| +TEST_F(TimerTest, IsActive_AfterRunning_OneShotNonZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + RunUntilIdle();
|
| + EXPECT_FALSE(timer.isActive());
|
| +}
|
| +
|
| +TEST_F(TimerTest, IsActive_AfterRunning_Repeating)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startRepeating(1.0, FROM_HERE);
|
| +
|
| + RunUntilIdleOrDeadlinePassed(m_startTime + 10);
|
| + EXPECT_TRUE(timer.isActive()); // It should run until cancelled.
|
| +}
|
| +
|
| +TEST_F(TimerTest, NextFireInterval_OneShotZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
|
| +}
|
| +
|
| +TEST_F(TimerTest, NextFireInterval_OneShotNonZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
|
| +}
|
| +
|
| +TEST_F(TimerTest, NextFireInterval_OneShotNonZero_AfterAFewSeconds)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + AdvanceTimeBy(2.0);
|
| + EXPECT_FLOAT_EQ(8.0, timer.nextFireInterval());
|
| +}
|
| +
|
| +TEST_F(TimerTest, NextFireInterval_Repeating)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startRepeating(20, FROM_HERE);
|
| +
|
| + EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval());
|
| +}
|
| +
|
| +TEST_F(TimerTest, RepeatInterval_NeverStarted)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| +
|
| + EXPECT_FLOAT_EQ(0.0, timer.repeatInterval());
|
| +}
|
| +
|
| +TEST_F(TimerTest, RepeatInterval_OneShotZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(0, FROM_HERE);
|
| +
|
| + EXPECT_FLOAT_EQ(0.0, timer.repeatInterval());
|
| +}
|
| +
|
| +TEST_F(TimerTest, RepeatInterval_OneShotNonZero)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startOneShot(10, FROM_HERE);
|
| +
|
| + EXPECT_FLOAT_EQ(0.0, timer.repeatInterval());
|
| +}
|
| +
|
| +TEST_F(TimerTest, RepeatInterval_Repeating)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startRepeating(20, FROM_HERE);
|
| +
|
| + EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
|
| +}
|
| +
|
| +TEST_F(TimerTest, AugmentRepeatInterval)
|
| +{
|
| + Timer<TimerTest> timer((TimerTest*) this, &TimerTest::CountingTask);
|
| + timer.startRepeating(10, FROM_HERE);
|
| + EXPECT_FLOAT_EQ(10.0, timer.repeatInterval());
|
| + EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
|
| +
|
| + AdvanceTimeBy(2.0); // augmentRepeatInterval reposts based off the current time.
|
| + timer.augmentRepeatInterval(10);
|
| +
|
| + EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
|
| + EXPECT_FLOAT_EQ(20.0, timer.nextFireInterval());
|
| +
|
| + RunUntilIdleOrDeadlinePassed(m_startTime + 50.0);
|
| + EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 22.0, m_startTime + 42.0));
|
| +}
|
| +
|
| +class MockTimerWithAlignment : public TimerBase {
|
| +public:
|
| + MockTimerWithAlignment() : m_lastFireTime(0.0), m_alignedFireTime(0.0) { }
|
| +
|
| + virtual void fired() override
|
| + {
|
| + }
|
| +
|
| + double alignedFireTime(double fireTime) const override
|
| + {
|
| + m_lastFireTime = fireTime;
|
| + return m_alignedFireTime;
|
| + }
|
| +
|
| + void setAlignedFireTime(double alignedFireTime)
|
| + {
|
| + m_alignedFireTime = alignedFireTime;
|
| + }
|
| +
|
| + double lastFireTime() const
|
| + {
|
| + return m_lastFireTime;
|
| + }
|
| +
|
| +private:
|
| + mutable double m_lastFireTime;
|
| + double m_alignedFireTime;
|
| +};
|
| +
|
| +TEST_F(TimerTest, TimerAlignment_OneShotZero)
|
| +{
|
| + MockTimerWithAlignment timer;
|
| + timer.setAlignedFireTime(m_startTime + 1.0);
|
| +
|
| + timer.start(0.0, 0.0, FROM_HERE);
|
| +
|
| + // The nextFireInterval gets overrriden.
|
| + EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval());
|
| + EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
|
| + EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
|
| +}
|
| +
|
| +TEST_F(TimerTest, TimerAlignment_OneShotNonZero)
|
| +{
|
| + MockTimerWithAlignment timer;
|
| + timer.setAlignedFireTime(m_startTime + 1.0);
|
| +
|
| + timer.start(0.5, 0.0, FROM_HERE);
|
| +
|
| + // The nextFireInterval gets overrriden.
|
| + EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval());
|
| + EXPECT_FLOAT_EQ(0.5, timer.nextUnalignedFireInterval());
|
| + EXPECT_FLOAT_EQ(m_startTime + 0.5, timer.lastFireTime());
|
| +}
|
| +
|
| +TEST_F(TimerTest, DidChangeAlignmentInterval)
|
| +{
|
| + MockTimerWithAlignment timer;
|
| + timer.setAlignedFireTime(m_startTime + 1.0);
|
| +
|
| + timer.start(0.0, 0.0, FROM_HERE);
|
| +
|
| + EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval());
|
| + EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
|
| + EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
|
| +
|
| + timer.setAlignedFireTime(m_startTime);
|
| + timer.didChangeAlignmentInterval(monotonicallyIncreasingTime());
|
| +
|
| + EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
|
| + EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
|
| + EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
|
| +}
|
| +
|
| +
|
| +} // namespace
|
| +} // namespace blink
|
|
|