Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Unified Diff: base/test/test_mock_time_task_runner.cc

Issue 823143004: Add TestMockTimeTaskRunner in base/test. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update year in copyright notice. Rebase. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/test/test_mock_time_task_runner.h ('k') | chrome/browser/chromeos/session_length_limiter_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..169839124d9e29f7ea60ab4f356020ebcb5fb0f0
--- /dev/null
+++ b/base/test/test_mock_time_task_runner.cc
@@ -0,0 +1,157 @@
+// Copyright 2015 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/logging.h"
+#include "base/memory/ref_counted.h"
+
+namespace base {
+
+namespace {
+
+// TickClock that always returns the then-current mock time of |task_runner| as
+// the current time.
+class MockTickClock : public TickClock {
+ public:
+ explicit MockTickClock(
+ scoped_refptr<const TestMockTimeTaskRunner> task_runner);
+ ~MockTickClock() override;
+
+ // TickClock:
+ TimeTicks NowTicks() override;
+
+ private:
+ scoped_refptr<const TestMockTimeTaskRunner> task_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockTickClock);
+};
+
+MockTickClock::MockTickClock(
+ scoped_refptr<const TestMockTimeTaskRunner> task_runner)
+ : task_runner_(task_runner) {
+}
+
+MockTickClock::~MockTickClock() {
+}
+
+TimeTicks MockTickClock::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());
+
+ OnBeforeSelectingTask();
+
+ const base::TimeTicks original_now = now_;
+ TestPendingTask task_info;
+ while (DequeueNextTask(original_now, delta, &task_info)) {
+ if (task_info.GetTimeToRun() - now_ > base::TimeDelta()) {
+ now_ = task_info.GetTimeToRun();
+ OnAfterTimePassed();
+ }
+
+ task_info.task.Run();
+
+ OnAfterTaskRun();
+ OnBeforeSelectingTask();
+ }
+
+ if (!delta.is_max() && now_ - original_now < delta) {
+ now_ = original_now + delta;
+ OnAfterTimePassed();
+ }
+}
+
+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 MockTickClock(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 {
+ return thread_checker_.CalledOnValidThread();
+}
+
+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::OnAfterTimePassed() {
+ // Empty default implementation.
+}
+
+void TestMockTimeTaskRunner::OnAfterTaskRun() {
+ // 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
« no previous file with comments | « base/test/test_mock_time_task_runner.h ('k') | chrome/browser/chromeos/session_length_limiter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698