Index: base/test/test_mock_time_task_runner.cc |
diff --git a/base/test/test_mock_time_task_runner.cc b/base/test/test_mock_time_task_runner.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..379022f77272a251310de082c5b061b6b0a7b1d9 |
--- /dev/null |
+++ b/base/test/test_mock_time_task_runner.cc |
@@ -0,0 +1,157 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/test/test_mock_time_task_runner.h" |
+ |
+#include "base/callback.h" |
bartfab (slow)
2015/01/08 14:14:34
Nit: Do you actually need this? You do not seem to
engedy
2015/01/08 16:41:25
Apparently not. Removed.
|
+ |
+namespace base { |
+ |
+namespace { |
+ |
+// TickClock that always returns the then-current mock time of |task_runner| as |
+// the current time. |
+class CorrespondingMockTickClock : public TickClock { |
bartfab (slow)
2015/01/08 14:14:34
Nit: I am not sure "Corresponding" is a useful par
engedy
2015/01/08 16:41:25
Removed that part. Done.
|
+ public: |
+ explicit CorrespondingMockTickClock( |
+ scoped_refptr<const TestMockTimeTaskRunner> task_runner); |
bartfab (slow)
2015/01/08 14:14:34
Nit: #include "base/memory/ref_counted.h"
engedy
2015/01/08 16:41:25
Done.
|
+ virtual ~CorrespondingMockTickClock(); |
+ |
+ // TickClock: |
+ virtual TimeTicks NowTicks() override; |
+ |
+ private: |
+ scoped_refptr<const TestMockTimeTaskRunner> task_runner_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CorrespondingMockTickClock); |
+}; |
+ |
+CorrespondingMockTickClock::CorrespondingMockTickClock( |
+ scoped_refptr<const TestMockTimeTaskRunner> task_runner) |
+ : task_runner_(task_runner) { |
+} |
+ |
+CorrespondingMockTickClock::~CorrespondingMockTickClock() { |
+} |
+ |
+TimeTicks CorrespondingMockTickClock::NowTicks() { |
+ return task_runner_->GetCurrentMockTime(); |
+} |
+ |
+} // namespace |
+ |
+bool TestMockTimeTaskRunner::TemporalOrder::operator()( |
+ const TestPendingTask& first_task, |
+ const TestPendingTask& second_task) const { |
+ return first_task.GetTimeToRun() > second_task.GetTimeToRun(); |
+} |
+ |
+TestMockTimeTaskRunner::TestMockTimeTaskRunner() { |
+} |
+ |
+TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { |
+} |
+ |
+void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
bartfab (slow)
2015/01/08 14:14:34
Nit: #include "base/logging.h"
engedy
2015/01/08 16:41:25
Done.
|
+ |
+ OnBeforeSelectingTask(); |
+ |
+ base::TimeTicks original_now = now_; |
bartfab (slow)
2015/01/08 14:14:34
Nit: const.
engedy
2015/01/08 16:41:25
Done.
|
+ TestPendingTask task_info; |
+ while (DequeueNextTask(original_now, delta, &task_info)) { |
+ if (task_info.GetTimeToRun() - now_ > base::TimeDelta()) { |
+ now_ = task_info.GetTimeToRun(); |
+ OnAfterTimePasses(); |
+ } |
+ |
+ task_info.task.Run(); |
+ |
+ OnAfterRunningTask(); |
+ OnBeforeSelectingTask(); |
+ } |
+ |
+ if (!delta.is_max() && now_ - original_now < delta) { |
+ now_ = original_now + delta; |
+ OnAfterTimePasses(); |
+ } |
+} |
+ |
+void TestMockTimeTaskRunner::RunUntilIdle() { |
+ FastForwardBy(TimeDelta()); |
+} |
+ |
+void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { |
+ FastForwardBy(TimeDelta::Max()); |
+} |
+ |
+TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ return now_; |
+} |
+ |
+scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ return make_scoped_ptr(new CorrespondingMockTickClock(this)); |
+} |
+ |
+bool TestMockTimeTaskRunner::HasPendingTask() const { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ return !tasks_.empty(); |
+} |
+ |
+TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_; |
+} |
+ |
+bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
bartfab (slow)
2015/01/08 14:14:34
Nit: You could actually trivially make this method
engedy
2015/01/08 16:41:25
Nice. Done.
|
+ return true; |
+} |
+ |
+bool TestMockTimeTaskRunner::PostDelayedTask( |
+ const tracked_objects::Location& from_here, |
+ const Closure& task, |
+ TimeDelta delay) { |
+ base::AutoLock scoped_lock(tasks_lock_); |
+ tasks_.push( |
+ TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE)); |
+ return true; |
+} |
+ |
+bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( |
+ const tracked_objects::Location& from_here, |
+ const Closure& task, |
+ TimeDelta delay) { |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+void TestMockTimeTaskRunner::OnBeforeSelectingTask() { |
+ // Empty default implementation. |
+} |
+ |
+void TestMockTimeTaskRunner::OnAfterTimePasses() { |
+ // Empty default implementation. |
+} |
+ |
+void TestMockTimeTaskRunner::OnAfterRunningTask() { |
+ // Empty default implementation. |
+} |
+ |
+bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference, |
+ const base::TimeDelta& max_delta, |
+ TestPendingTask* next_task) { |
+ base::AutoLock scoped_lock(tasks_lock_); |
+ if (!tasks_.empty() && |
+ (tasks_.top().GetTimeToRun() - reference) <= max_delta) { |
+ *next_task = tasks_.top(); |
+ tasks_.pop(); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+} // namespace base |