| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/test/test_mock_time_task_runner.h" | 5 #include "base/test/test_mock_time_task_runner.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/time/clock.h" | 9 #include "base/time/clock.h" |
| 10 #include "base/time/tick_clock.h" | 10 #include "base/time/tick_clock.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 MockClock::MockClock(scoped_refptr<const TestMockTimeTaskRunner> task_runner) | 60 MockClock::MockClock(scoped_refptr<const TestMockTimeTaskRunner> task_runner) |
| 61 : task_runner_(task_runner) { | 61 : task_runner_(task_runner) { |
| 62 } | 62 } |
| 63 | 63 |
| 64 Time MockClock::Now() { | 64 Time MockClock::Now() { |
| 65 return task_runner_->Now(); | 65 return task_runner_->Now(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 } // namespace | 68 } // namespace |
| 69 | 69 |
| 70 // TestMockTimeTaskRunner::TestOrderedPendingTask ----------------------------- |
| 71 |
| 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(); |
| 85 |
| 86 size_t ordinal; |
| 87 }; |
| 88 |
| 89 TestMockTimeTaskRunner::TestOrderedPendingTask::TestOrderedPendingTask() |
| 90 : ordinal(0) { |
| 91 } |
| 92 |
| 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) { |
| 102 } |
| 103 |
| 104 TestMockTimeTaskRunner::TestOrderedPendingTask::~TestOrderedPendingTask() { |
| 105 } |
| 106 |
| 70 // TestMockTimeTaskRunner ----------------------------------------------------- | 107 // TestMockTimeTaskRunner ----------------------------------------------------- |
| 71 | 108 |
| 72 bool TestMockTimeTaskRunner::TemporalOrder::operator()( | 109 bool TestMockTimeTaskRunner::TemporalOrder::operator()( |
| 73 const TestPendingTask& first_task, | 110 const TestOrderedPendingTask& first_task, |
| 74 const TestPendingTask& second_task) const { | 111 const TestOrderedPendingTask& second_task) const { |
| 112 if (first_task.GetTimeToRun() == second_task.GetTimeToRun()) |
| 113 return first_task.ordinal > second_task.ordinal; |
| 75 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); | 114 return first_task.GetTimeToRun() > second_task.GetTimeToRun(); |
| 76 } | 115 } |
| 77 | 116 |
| 78 TestMockTimeTaskRunner::TestMockTimeTaskRunner() : now_(Time::UnixEpoch()) { | 117 TestMockTimeTaskRunner::TestMockTimeTaskRunner() |
| 118 : now_(Time::UnixEpoch()), next_task_ordinal_(0) { |
| 79 } | 119 } |
| 80 | 120 |
| 81 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { | 121 TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { |
| 82 } | 122 } |
| 83 | 123 |
| 84 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { | 124 void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 DCHECK_GE(delta, TimeDelta()); | 126 DCHECK_GE(delta, TimeDelta()); |
| 87 | 127 |
| 88 const TimeTicks original_now_ticks = now_ticks_; | 128 const TimeTicks original_now_ticks = now_ticks_; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 185 |
| 146 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { | 186 bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
| 147 return thread_checker_.CalledOnValidThread(); | 187 return thread_checker_.CalledOnValidThread(); |
| 148 } | 188 } |
| 149 | 189 |
| 150 bool TestMockTimeTaskRunner::PostDelayedTask( | 190 bool TestMockTimeTaskRunner::PostDelayedTask( |
| 151 const tracked_objects::Location& from_here, | 191 const tracked_objects::Location& from_here, |
| 152 const Closure& task, | 192 const Closure& task, |
| 153 TimeDelta delay) { | 193 TimeDelta delay) { |
| 154 AutoLock scoped_lock(tasks_lock_); | 194 AutoLock scoped_lock(tasks_lock_); |
| 155 tasks_.push(TestPendingTask(from_here, task, now_ticks_, delay, | 195 tasks_.push(TestOrderedPendingTask(from_here, task, now_ticks_, delay, |
| 156 TestPendingTask::NESTABLE)); | 196 next_task_ordinal_++, |
| 197 TestPendingTask::NESTABLE)); |
| 157 return true; | 198 return true; |
| 158 } | 199 } |
| 159 | 200 |
| 160 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( | 201 bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( |
| 161 const tracked_objects::Location& from_here, | 202 const tracked_objects::Location& from_here, |
| 162 const Closure& task, | 203 const Closure& task, |
| 163 TimeDelta delay) { | 204 TimeDelta delay) { |
| 164 return PostDelayedTask(from_here, task, delay); | 205 return PostDelayedTask(from_here, task, delay); |
| 165 } | 206 } |
| 166 | 207 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 if (!tasks_.empty() && | 254 if (!tasks_.empty() && |
| 214 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { | 255 (tasks_.top().GetTimeToRun() - reference) <= max_delta) { |
| 215 *next_task = tasks_.top(); | 256 *next_task = tasks_.top(); |
| 216 tasks_.pop(); | 257 tasks_.pop(); |
| 217 return true; | 258 return true; |
| 218 } | 259 } |
| 219 return false; | 260 return false; |
| 220 } | 261 } |
| 221 | 262 |
| 222 } // namespace base | 263 } // namespace base |
| OLD | NEW |