Reland "Don't refcount tracking id -> slot id mapping."
[chromium-blink-merge.git] / base / test / test_mock_time_task_runner.cc
blob442d99190b0b87060337b6b67d9d60f7f1d47051
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"
10 namespace base {
12 namespace {
14 // TickClock that always returns the then-current mock time of |task_runner| as
15 // the current time.
16 class MockTickClock : public TickClock {
17 public:
18 explicit MockTickClock(
19 scoped_refptr<const TestMockTimeTaskRunner> task_runner);
20 ~MockTickClock() override;
22 // TickClock:
23 TimeTicks NowTicks() override;
25 private:
26 scoped_refptr<const TestMockTimeTaskRunner> task_runner_;
28 DISALLOW_COPY_AND_ASSIGN(MockTickClock);
31 MockTickClock::MockTickClock(
32 scoped_refptr<const TestMockTimeTaskRunner> task_runner)
33 : task_runner_(task_runner) {
36 MockTickClock::~MockTickClock() {
39 TimeTicks MockTickClock::NowTicks() {
40 return task_runner_->GetCurrentMockTime();
43 } // namespace
45 bool TestMockTimeTaskRunner::TemporalOrder::operator()(
46 const TestPendingTask& first_task,
47 const TestPendingTask& second_task) const {
48 return first_task.GetTimeToRun() > second_task.GetTimeToRun();
51 TestMockTimeTaskRunner::TestMockTimeTaskRunner() {
54 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() {
57 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) {
58 DCHECK(thread_checker_.CalledOnValidThread());
60 OnBeforeSelectingTask();
62 const base::TimeTicks original_now = now_;
63 TestPendingTask task_info;
64 while (DequeueNextTask(original_now, delta, &task_info)) {
65 if (task_info.GetTimeToRun() - now_ > base::TimeDelta()) {
66 now_ = task_info.GetTimeToRun();
67 OnAfterTimePassed();
70 task_info.task.Run();
72 OnAfterTaskRun();
73 OnBeforeSelectingTask();
76 if (!delta.is_max() && now_ - original_now < delta) {
77 now_ = original_now + delta;
78 OnAfterTimePassed();
82 void TestMockTimeTaskRunner::RunUntilIdle() {
83 FastForwardBy(TimeDelta());
86 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() {
87 FastForwardBy(TimeDelta::Max());
90 TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const {
91 DCHECK(thread_checker_.CalledOnValidThread());
92 return now_;
95 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const {
96 DCHECK(thread_checker_.CalledOnValidThread());
97 return make_scoped_ptr(new MockTickClock(this));
100 bool TestMockTimeTaskRunner::HasPendingTask() const {
101 DCHECK(thread_checker_.CalledOnValidThread());
102 return !tasks_.empty();
105 size_t TestMockTimeTaskRunner::GetPendingTaskCount() const {
106 DCHECK(thread_checker_.CalledOnValidThread());
107 return tasks_.size();
110 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_;
115 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const {
116 return thread_checker_.CalledOnValidThread();
119 bool TestMockTimeTaskRunner::PostDelayedTask(
120 const tracked_objects::Location& from_here,
121 const Closure& task,
122 TimeDelta delay) {
123 base::AutoLock scoped_lock(tasks_lock_);
124 tasks_.push(
125 TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE));
126 return true;
129 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask(
130 const tracked_objects::Location& from_here,
131 const Closure& task,
132 TimeDelta delay) {
133 NOTREACHED();
134 return false;
137 void TestMockTimeTaskRunner::OnBeforeSelectingTask() {
138 // Empty default implementation.
141 void TestMockTimeTaskRunner::OnAfterTimePassed() {
142 // Empty default implementation.
145 void TestMockTimeTaskRunner::OnAfterTaskRun() {
146 // Empty default implementation.
149 bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference,
150 const base::TimeDelta& max_delta,
151 TestPendingTask* next_task) {
152 base::AutoLock scoped_lock(tasks_lock_);
153 if (!tasks_.empty() &&
154 (tasks_.top().GetTimeToRun() - reference) <= max_delta) {
155 *next_task = tasks_.top();
156 tasks_.pop();
157 return true;
159 return false;
162 } // namespace base