Chromium Code Reviews| Index: base/test/test_mock_time_task_runner.h |
| diff --git a/base/test/test_mock_time_task_runner.h b/base/test/test_mock_time_task_runner.h |
| index 2f59892b6d58d5abe943e5e5dbc5d0aee3471e3a..04467c486e94e4d89c8c59fa416e8410e1ee1a58 100644 |
| --- a/base/test/test_mock_time_task_runner.h |
| +++ b/base/test/test_mock_time_task_runner.h |
| @@ -14,11 +14,13 @@ |
| #include "base/synchronization/lock.h" |
| #include "base/test/test_pending_task.h" |
| #include "base/threading/thread_checker.h" |
| -#include "base/time/tick_clock.h" |
| #include "base/time/time.h" |
| namespace base { |
| +class Clock; |
| +class TickClock; |
| + |
| // Runs pending tasks in the order of the tasks' post time + delay, and keeps |
| // track of a mock (virtual) tick clock time that can be fast-forwarded. |
| // |
| @@ -36,13 +38,19 @@ namespace base { |
| // |
| // This is a slightly more sophisticated version of TestSimpleTaskRunner, in |
| // that it supports running delayed tasks in the correct temporal order. |
| -class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner { |
| +class TestMockTimeTaskRunner : public SingleThreadTaskRunner { |
| public: |
| + // Constructs an instance whose virtual time will start at the Unix epoch, and |
| + // whose time ticks will still start at zero. |
| TestMockTimeTaskRunner(); |
| + // Constructs an instance whose virtual time will start at |initial_time|. |
| + // Note that its virtual time ticks will still start at zero. |
| + TestMockTimeTaskRunner(Time initial_time); |
|
Lei Zhang
2015/02/04 20:27:37
Nobody uses this ctor. If you do keep it, add the
engedy
2015/02/05 10:22:57
Removed.
|
| + |
| // Fast-forwards virtual time by |delta|, causing all tasks with a remaining |
| // delay less than or equal to |delta| to be executed. |
| - void FastForwardBy(base::TimeDelta delta); |
| + void FastForwardBy(TimeDelta delta); |
| // Fast-forwards virtual time just until all tasks are executed. |
| void FastForwardUntilNoTasksRemain(); |
| @@ -52,11 +60,19 @@ class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner { |
| // elapse. |
| void RunUntilIdle(); |
| - // Returns the current virtual time. |
| - TimeTicks GetCurrentMockTime() const; |
| + // Returns the current virtual time (initially starting at the Unix epoch or |
| + // the |initial_time| if one was specified in the constructor). |
| + Time Now() const; |
| + |
| + // Returns the current virtual tick time (initially starting at 0). |
| + TimeTicks NowTicks() const; |
| - // Returns a TickClock that uses the mock time of |this| as its time source. |
| - // The returned TickClock will hold a reference to |this|. |
| + // Returns a Clock that uses the virtual time of |this| as its time source. |
| + // The returned Clock will hold a reference to |this|. |
| + scoped_ptr<Clock> GetMockClock() const; |
| + |
| + // Returns a TickClock that uses the virtual time ticks of |this| as its tick |
| + // source. The returned TickClock will hold a reference to |this|. |
| scoped_ptr<TickClock> GetMockTickClock() const; |
| bool HasPendingTask() const; |
| @@ -66,12 +82,11 @@ class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner { |
| // SingleThreadTaskRunner: |
| bool RunsTasksOnCurrentThread() const override; |
| bool PostDelayedTask(const tracked_objects::Location& from_here, |
| - const base::Closure& task, |
| - TimeDelta delay) override; |
| - bool PostNonNestableDelayedTask( |
| - const tracked_objects::Location& from_here, |
| - const base::Closure& task, |
| - TimeDelta delay) override; |
| + const Closure& task, |
| + TimeDelta delay) override; |
| + bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| + const Closure& task, |
| + TimeDelta delay) override; |
| protected: |
| ~TestMockTimeTaskRunner() override; |
| @@ -100,20 +115,25 @@ class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner { |
| std::vector<TestPendingTask>, |
| TemporalOrder> TaskPriorityQueue; |
| + // Forwards |now_ticks_| until it equals |later_ticks|, and forwards |now_| by |
| + // the same amount. Calls OnAfterTimePassed() if |now_ticks_| < |later_ticks|. |
| + void ForwardClocksUntilTickTime(TimeTicks later_ticks); |
| + |
| // Returns the |next_task| to run if there is any with a running time that is |
| // at most |reference| + |max_delta|. This additional complexity is required |
| // so that |max_delta| == TimeDelta::Max() can be supported. |
| - bool DequeueNextTask(const base::TimeTicks& reference, |
| - const base::TimeDelta& max_delta, |
| + bool DequeueNextTask(const TimeTicks& reference, |
| + const TimeDelta& max_delta, |
| TestPendingTask* next_task); |
| - base::ThreadChecker thread_checker_; |
| - base::TimeTicks now_; |
| + ThreadChecker thread_checker_; |
| + Time now_; |
| + TimeTicks now_ticks_; |
| // Temporally ordered heap of pending tasks. Must only be accessed while the |
| // |tasks_lock_| is held. |
| TaskPriorityQueue tasks_; |
| - base::Lock tasks_lock_; |
| + Lock tasks_lock_; |
| DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); |
| }; |