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
3 // found in the LICENSE file.
5 #include "base/test/test_mock_time_task_runner.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/time/clock.h"
10 #include "base/time/tick_clock.h"
16 // MockTickClock --------------------------------------------------------------
18 // TickClock that always returns the then-current mock time ticks of
19 // |task_runner| as the current time ticks.
20 class MockTickClock
: public TickClock
{
22 explicit MockTickClock(
23 scoped_refptr
<const TestMockTimeTaskRunner
> task_runner
);
26 TimeTicks
NowTicks() override
;
29 scoped_refptr
<const TestMockTimeTaskRunner
> task_runner_
;
31 DISALLOW_COPY_AND_ASSIGN(MockTickClock
);
34 MockTickClock::MockTickClock(
35 scoped_refptr
<const TestMockTimeTaskRunner
> task_runner
)
36 : task_runner_(task_runner
) {
39 TimeTicks
MockTickClock::NowTicks() {
40 return task_runner_
->NowTicks();
43 // MockClock ------------------------------------------------------------------
45 // Clock that always returns the then-current mock time of |task_runner| as the
47 class MockClock
: public Clock
{
49 explicit MockClock(scoped_refptr
<const TestMockTimeTaskRunner
> task_runner
);
55 scoped_refptr
<const TestMockTimeTaskRunner
> task_runner_
;
57 DISALLOW_COPY_AND_ASSIGN(MockClock
);
60 MockClock::MockClock(scoped_refptr
<const TestMockTimeTaskRunner
> task_runner
)
61 : task_runner_(task_runner
) {
64 Time
MockClock::Now() {
65 return task_runner_
->Now();
70 // TestMockTimeTaskRunner -----------------------------------------------------
72 bool TestMockTimeTaskRunner::TemporalOrder::operator()(
73 const TestPendingTask
& first_task
,
74 const TestPendingTask
& second_task
) const {
75 return first_task
.GetTimeToRun() > second_task
.GetTimeToRun();
78 TestMockTimeTaskRunner::TestMockTimeTaskRunner() : now_(Time::UnixEpoch()) {
81 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() {
84 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta
) {
85 DCHECK(thread_checker_
.CalledOnValidThread());
86 DCHECK_GE(delta
, TimeDelta());
88 const TimeTicks original_now_ticks
= now_ticks_
;
89 ProcessAllTasksNoLaterThan(delta
);
90 ForwardClocksUntilTickTime(original_now_ticks
+ delta
);
93 void TestMockTimeTaskRunner::RunUntilIdle() {
94 DCHECK(thread_checker_
.CalledOnValidThread());
95 ProcessAllTasksNoLaterThan(TimeDelta());
98 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() {
99 DCHECK(thread_checker_
.CalledOnValidThread());
100 ProcessAllTasksNoLaterThan(TimeDelta::Max());
103 void TestMockTimeTaskRunner::ClearPendingTasks() {
104 DCHECK(thread_checker_
.CalledOnValidThread());
105 AutoLock
scoped_lock(tasks_lock_
);
106 while (!tasks_
.empty())
110 Time
TestMockTimeTaskRunner::Now() const {
111 DCHECK(thread_checker_
.CalledOnValidThread());
115 TimeTicks
TestMockTimeTaskRunner::NowTicks() const {
116 DCHECK(thread_checker_
.CalledOnValidThread());
120 scoped_ptr
<Clock
> TestMockTimeTaskRunner::GetMockClock() const {
121 DCHECK(thread_checker_
.CalledOnValidThread());
122 return make_scoped_ptr(new MockClock(this));
125 scoped_ptr
<TickClock
> TestMockTimeTaskRunner::GetMockTickClock() const {
126 DCHECK(thread_checker_
.CalledOnValidThread());
127 return make_scoped_ptr(new MockTickClock(this));
130 bool TestMockTimeTaskRunner::HasPendingTask() const {
131 DCHECK(thread_checker_
.CalledOnValidThread());
132 return !tasks_
.empty();
135 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const {
136 DCHECK(thread_checker_
.CalledOnValidThread());
137 return tasks_
.size();
140 TimeDelta
TestMockTimeTaskRunner::NextPendingTaskDelay() const {
141 DCHECK(thread_checker_
.CalledOnValidThread());
142 return tasks_
.empty() ? TimeDelta::Max()
143 : tasks_
.top().GetTimeToRun() - now_ticks_
;
146 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const {
147 return thread_checker_
.CalledOnValidThread();
150 bool TestMockTimeTaskRunner::PostDelayedTask(
151 const tracked_objects::Location
& from_here
,
154 AutoLock
scoped_lock(tasks_lock_
);
155 tasks_
.push(TestPendingTask(from_here
, task
, now_ticks_
, delay
,
156 TestPendingTask::NESTABLE
));
160 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask(
161 const tracked_objects::Location
& from_here
,
164 return PostDelayedTask(from_here
, task
, delay
);
167 bool TestMockTimeTaskRunner::IsElapsingStopped() {
171 void TestMockTimeTaskRunner::OnBeforeSelectingTask() {
172 // Empty default implementation.
175 void TestMockTimeTaskRunner::OnAfterTimePassed() {
176 // Empty default implementation.
179 void TestMockTimeTaskRunner::OnAfterTaskRun() {
180 // Empty default implementation.
183 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta
) {
184 DCHECK_GE(max_delta
, TimeDelta());
185 const TimeTicks original_now_ticks
= now_ticks_
;
186 while (!IsElapsingStopped()) {
187 OnBeforeSelectingTask();
188 TestPendingTask task_info
;
189 if (!DequeueNextTask(original_now_ticks
, max_delta
, &task_info
))
191 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will
192 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not
193 // moving the clock backwards in this case.
194 ForwardClocksUntilTickTime(task_info
.GetTimeToRun());
195 task_info
.task
.Run();
200 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks
) {
201 if (later_ticks
<= now_ticks_
)
204 now_
+= later_ticks
- now_ticks_
;
205 now_ticks_
= later_ticks
;
209 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks
& reference
,
210 const TimeDelta
& max_delta
,
211 TestPendingTask
* next_task
) {
212 AutoLock
scoped_lock(tasks_lock_
);
213 if (!tasks_
.empty() &&
214 (tasks_
.top().GetTimeToRun() - reference
) <= max_delta
) {
215 *next_task
= tasks_
.top();