OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/test/test_mock_time_task_runner.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/ref_counted.h" | |
9 | |
10 namespace base { | |
11 | |
12 namespace { | |
13 | |
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; | |
21 | |
22 // TickClock: | |
23 TimeTicks NowTicks() override; | |
24 | |
25 private: | |
26 scoped_refptr<const TestMockTimeTaskRunner> task_runner_; | |
27 | |
28 DISALLOW_COPY_AND_ASSIGN(MockTickClock); | |
29 }; | |
30 | |
31 MockTickClock::MockTickClock( | |
32 scoped_refptr<const TestMockTimeTaskRunner> task_runner) | |
33 : task_runner_(task_runner) { | |
34 } | |
35 | |
36 MockTickClock::~MockTickClock() { | |
37 } | |
38 | |
39 TimeTicks MockTickClock::NowTicks() { | |
40 return task_runner_->GetCurrentMockTime(); | |
41 } | |
42 | |
43 } // namespace | |
44 | |
45 bool TestMockTimeTaskRunner::TemporalOrder::operator()( | |
46 const TestPendingTask& first_task, | |
47 const TestPendingTask& second_task) const { | |
48 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); | |
49 } | |
50 | |
51 TestMockTimeTaskRunner::TestMockTimeTaskRunner() { | |
52 } | |
53 | |
54 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { | |
55 } | |
56 | |
57 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { | |
58 DCHECK(thread_checker_.CalledOnValidThread()); | |
59 | |
60 OnBeforeSelectingTask(); | |
61 | |
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(); | |
68 } | |
69 | |
70 task_info.task.Run(); | |
71 | |
72 OnAfterTaskRun(); | |
73 OnBeforeSelectingTask(); | |
74 } | |
75 | |
76 if (!delta.is_max() && now_ - original_now < delta) { | |
77 now_ = original_now + delta; | |
78 OnAfterTimePassed(); | |
79 } | |
80 } | |
81 | |
82 void TestMockTimeTaskRunner::RunUntilIdle() { | |
83 FastForwardBy(TimeDelta()); | |
84 } | |
85 | |
86 void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { | |
87 FastForwardBy(TimeDelta::Max()); | |
88 } | |
89 | |
90 TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const { | |
91 DCHECK(thread_checker_.CalledOnValidThread()); | |
92 return now_; | |
93 } | |
94 | |
95 scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { | |
96 DCHECK(thread_checker_.CalledOnValidThread()); | |
97 return make_scoped_ptr(new MockTickClock(this)); | |
98 } | |
99 | |
100 bool TestMockTimeTaskRunner::HasPendingTask() const { | |
101 DCHECK(thread_checker_.CalledOnValidThread()); | |
102 return !tasks_.empty(); | |
103 } | |
104 | |
105 TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { | |
106 DCHECK(thread_checker_.CalledOnValidThread()); | |
107 return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_; | |
108 } | |
109 | |
110 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { | |
111 return thread_checker_.CalledOnValidThread(); | |
112 } | |
113 | |
114 bool TestMockTimeTaskRunner::PostDelayedTask( | |
115 const tracked_objects::Location& from_here, | |
116 const Closure& task, | |
117 TimeDelta delay) { | |
118 base::AutoLock scoped_lock(tasks_lock_); | |
119 tasks_.push( | |
120 TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE)); | |
121 return true; | |
122 } | |
123 | |
124 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( | |
125 const tracked_objects::Location& from_here, | |
126 const Closure& task, | |
127 TimeDelta delay) { | |
128 NOTREACHED(); | |
129 return false; | |
130 } | |
131 | |
132 void TestMockTimeTaskRunner::OnBeforeSelectingTask() { | |
133 // Empty default implementation. | |
134 } | |
135 | |
136 void TestMockTimeTaskRunner::OnAfterTimePassed() { | |
137 // Empty default implementation. | |
138 } | |
139 | |
140 void TestMockTimeTaskRunner::OnAfterTaskRun() { | |
141 // Empty default implementation. | |
142 } | |
143 | |
144 bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference, | |
145 const base::TimeDelta& max_delta, | |
146 TestPendingTask* next_task) { | |
147 base::AutoLock scoped_lock(tasks_lock_); | |
148 if (!tasks_.empty() && | |
149 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { | |
150 *next_task = tasks_.top(); | |
151 tasks_.pop(); | |
152 return true; | |
153 } | |
154 return false; | |
155 } | |
156 | |
157 } // namespace base | |
OLD | NEW |