ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / base / test / test_mock_time_task_runner.cc
blob8e65ccf632ce9ef99c642b4532901ccae4782daf
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 -----------------------------------------------------
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 Time TestMockTimeTaskRunner::Now() const {
104 DCHECK(thread_checker_.CalledOnValidThread());
105 return now_;
108 TimeTicks TestMockTimeTaskRunner::NowTicks() const {
109 DCHECK(thread_checker_.CalledOnValidThread());
110 return now_ticks_;
113 scoped_ptr<Clock> TestMockTimeTaskRunner::GetMockClock() const {
114 DCHECK(thread_checker_.CalledOnValidThread());
115 return make_scoped_ptr(new MockClock(this));
118 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const {
119 DCHECK(thread_checker_.CalledOnValidThread());
120 return make_scoped_ptr(new MockTickClock(this));
123 bool TestMockTimeTaskRunner::HasPendingTask() const {
124 DCHECK(thread_checker_.CalledOnValidThread());
125 return !tasks_.empty();
128 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const {
129 DCHECK(thread_checker_.CalledOnValidThread());
130 return tasks_.size();
133 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const {
134 DCHECK(thread_checker_.CalledOnValidThread());
135 return tasks_.empty() ? TimeDelta::Max()
136 : tasks_.top().GetTimeToRun() - now_ticks_;
139 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const {
140 return thread_checker_.CalledOnValidThread();
143 bool TestMockTimeTaskRunner::PostDelayedTask(
144 const tracked_objects::Location& from_here,
145 const Closure& task,
146 TimeDelta delay) {
147 AutoLock scoped_lock(tasks_lock_);
148 tasks_.push(TestPendingTask(from_here, task, now_ticks_, delay,
149 TestPendingTask::NESTABLE));
150 return true;
153 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask(
154 const tracked_objects::Location& from_here,
155 const Closure& task,
156 TimeDelta delay) {
157 return PostDelayedTask(from_here, task, delay);
160 void TestMockTimeTaskRunner::OnBeforeSelectingTask() {
161 // Empty default implementation.
164 void TestMockTimeTaskRunner::OnAfterTimePassed() {
165 // Empty default implementation.
168 void TestMockTimeTaskRunner::OnAfterTaskRun() {
169 // Empty default implementation.
172 void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) {
173 DCHECK_GE(max_delta, TimeDelta());
174 const TimeTicks original_now_ticks = now_ticks_;
175 while (true) {
176 OnBeforeSelectingTask();
177 TestPendingTask task_info;
178 if (!DequeueNextTask(original_now_ticks, max_delta, &task_info))
179 break;
180 // If tasks were posted with a negative delay, task_info.GetTimeToRun() will
181 // be less than |now_ticks_|. ForwardClocksUntilTickTime() takes care of not
182 // moving the clock backwards in this case.
183 ForwardClocksUntilTickTime(task_info.GetTimeToRun());
184 task_info.task.Run();
185 OnAfterTaskRun();
189 void TestMockTimeTaskRunner::ForwardClocksUntilTickTime(TimeTicks later_ticks) {
190 if (later_ticks <= now_ticks_)
191 return;
193 now_ += later_ticks - now_ticks_;
194 now_ticks_ = later_ticks;
195 OnAfterTimePassed();
198 bool TestMockTimeTaskRunner::DequeueNextTask(const TimeTicks& reference,
199 const TimeDelta& max_delta,
200 TestPendingTask* next_task) {
201 AutoLock scoped_lock(tasks_lock_);
202 if (!tasks_.empty() &&
203 (tasks_.top().GetTimeToRun() - reference) <= max_delta) {
204 *next_task = tasks_.top();
205 tasks_.pop();
206 return true;
208 return false;
211 } // namespace base