Exclude ProxyAuthOnUserBoardScreenTest.ProxyAuthDialogOnUserBoardScreen/1 under MSan
[chromium-blink-merge.git] / base / test / test_mock_time_task_runner.cc
blobf2e18a3503379588194ca005b27f4da3013663f4
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"
12 namespace base {
14 namespace {
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 {
21 public:
22 explicit MockTickClock(
23 scoped_refptr<const TestMockTimeTaskRunner> task_runner);
25 // TickClock:
26 TimeTicks NowTicks() override;
28 private:
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
46 // current time.
47 class MockClock : public Clock {
48 public:
49 explicit MockClock(scoped_refptr<const TestMockTimeTaskRunner> task_runner);
51 // Clock:
52 Time Now() override;
54 private:
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();
68 } // namespace
70 // TestMockTimeTaskRunner::TestOrderedPendingTask -----------------------------
72 // Subclass of TestPendingTask which has a strictly monotonically increasing ID
73 // for every task, so that tasks posted with the same 'time to run' can be run
74 // in the order of being posted.
75 struct TestMockTimeTaskRunner::TestOrderedPendingTask
76 : public base::TestPendingTask {
77 TestOrderedPendingTask();
78 TestOrderedPendingTask(const tracked_objects::Location& location,
79 const Closure& task,
80 TimeTicks post_time,
81 TimeDelta delay,
82 size_t ordinal,
83 TestNestability nestability);
84 ~TestOrderedPendingTask();
86 size_t ordinal;
89 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask()
90 : ordinal(0) {
93 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask(
94 const tracked_objects::Location& location,
95 const Closure& task,
96 TimeTicks post_time,
97 TimeDelta delay,
98 size_t ordinal,
99 TestNestability nestability)
100 : base::TestPendingTask(location, task, post_time, delay, nestability),
101 ordinal(ordinal) {
104 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() {
107 // TestMockTimeTaskRunner -----------------------------------------------------
109 bool TestMockTimeTaskRunner::TemporalOrder::operator()(
110 const TestOrderedPendingTask& first_task,
111 const TestOrderedPendingTask& second_task) const {
112 if (first_task.GetTimeToRun() == second_task.GetTimeToRun())
113 return first_task.ordinal > second_task.ordinal;
114 return first_task.GetTimeToRun() > second_task.GetTimeToRun();
117 TestMockTimeTaskRunner::TestMockTimeTaskRunner()
118 : now_(Time::UnixEpoch()), next_task_ordinal_(0) {
121 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() {
124 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) {
125 DCHECK(thread_checker_.CalledOnValidThread());
126 DCHECK_GE(delta, TimeDelta());
128 const TimeTicks original_now_ticks = now_ticks_;
129 ProcessAllTasksNoLaterThan(delta);
130 ForwardClocksUntilTickTime(original_now_ticks + delta);
133 void TestMockTimeTaskRunner::RunUntilIdle() {
134 DCHECK(thread_checker_.CalledOnValidThread());
135 ProcessAllTasksNoLaterThan(TimeDelta());
138 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() {
139 DCHECK(thread_checker_.CalledOnValidThread());
140 ProcessAllTasksNoLaterThan(TimeDelta::Max());
143 void TestMockTimeTaskRunner::ClearPendingTasks() {
144 DCHECK(thread_checker_.CalledOnValidThread());
145 AutoLock scoped_lock(tasks_lock_);
146 while (!tasks_.empty())
147 tasks_.pop();
150 Time TestMockTimeTaskRunner::Now() const {
151 DCHECK(thread_checker_.CalledOnValidThread());
152 return now_;
155 TimeTicks TestMockTimeTaskRunner::NowTicks() const {
156 DCHECK(thread_checker_.CalledOnValidThread());
157 return now_ticks_;
160 scoped_ptr<Clock> TestMockTimeTaskRunner::GetMockClock() const {
161 DCHECK(thread_checker_.CalledOnValidThread());
162 return make_scoped_ptr(new MockClock(this));
165 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const {
166 DCHECK(thread_checker_.CalledOnValidThread());
167 return make_scoped_ptr(new MockTickClock(this));
170 bool TestMockTimeTaskRunner::HasPendingTask() const {
171 DCHECK(thread_checker_.CalledOnValidThread());
172 return !tasks_.empty();
175 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const {
176 DCHECK(thread_checker_.CalledOnValidThread());
177 return tasks_.size();
180 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const {
181 DCHECK(thread_checker_.CalledOnValidThread());
182 return tasks_.empty() ? TimeDelta::Max()
183 : tasks_.top().GetTimeToRun() - now_ticks_;
186 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const {
187 return thread_checker_.CalledOnValidThread();
190 bool TestMockTimeTaskRunner::PostDelayedTask(
191 const tracked_objects::Location& from_here,
192 const Closure& task,
193 TimeDelta delay) {
194 AutoLock scoped_lock(tasks_lock_);
195 tasks_.push(TestOrderedPendingTask(from_here, task, now_ticks_, delay,
196 next_task_ordinal_++,
197 TestPendingTask::NESTABLE));
198 return true;
201 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask(
202 const tracked_objects::Location& from_here,
203 const Closure& task,
204 TimeDelta delay) {
205 return PostDelayedTask(from_here, task, delay);
208 bool TestMockTimeTaskRunner::IsElapsingStopped() {
209 return false;
212 void TestMockTimeTaskRunner::OnBeforeSelectingTask() {
213 // Empty default implementation.
216 void TestMockTimeTaskRunner::OnAfterTimePassed() {
217 // Empty default implementation.
220 void TestMockTimeTaskRunner::OnAfterTaskRun() {
221 // Empty default implementation.
224 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) {
225 DCHECK_GE(max_delta, TimeDelta());
226 const TimeTicks original_now_ticks = now_ticks_;
227 while (!IsElapsingStopped()) {
228 OnBeforeSelectingTask();
229 TestPendingTask task_info;
230 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info))
231 break;
232 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will
233 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not
234 // moving the clock backwards in this case.
235 ForwardClocksUntilTickTime(task_info.GetTimeToRun());
236 task_info.task.Run();
237 OnAfterTaskRun();
241 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) {
242 if (later_ticks <= now_ticks_)
243 return;
245 now_ += later_ticks - now_ticks_;
246 now_ticks_ = later_ticks;
247 OnAfterTimePassed();
250 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference,
251 const TimeDelta& max_delta,
252 TestPendingTask* next_task) {
253 AutoLock scoped_lock(tasks_lock_);
254 if (!tasks_.empty() &&
255 (tasks_.top().GetTimeToRun() - reference) <= max_delta) {
256 *next_task = tasks_.top();
257 tasks_.pop();
258 return true;
260 return false;
263 } // namespace base