• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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_simple_task_runner.h"
6 
7 #include "base/logging.h"
8 
9 namespace base {
10 
TestSimpleTaskRunner()11 TestSimpleTaskRunner::TestSimpleTaskRunner() {}
12 
~TestSimpleTaskRunner()13 TestSimpleTaskRunner::~TestSimpleTaskRunner() {
14   DCHECK(thread_checker_.CalledOnValidThread());
15 }
16 
PostDelayedTask(const tracked_objects::Location & from_here,const Closure & task,TimeDelta delay)17 bool TestSimpleTaskRunner::PostDelayedTask(
18     const tracked_objects::Location& from_here,
19     const Closure& task,
20     TimeDelta delay) {
21   DCHECK(thread_checker_.CalledOnValidThread());
22   pending_tasks_.push_back(
23       TestPendingTask(from_here, task, TimeTicks(), delay,
24                       TestPendingTask::NESTABLE));
25   return true;
26 }
27 
PostNonNestableDelayedTask(const tracked_objects::Location & from_here,const Closure & task,TimeDelta delay)28 bool TestSimpleTaskRunner::PostNonNestableDelayedTask(
29     const tracked_objects::Location& from_here,
30     const Closure& task,
31     TimeDelta delay) {
32   DCHECK(thread_checker_.CalledOnValidThread());
33   pending_tasks_.push_back(
34       TestPendingTask(from_here, task, TimeTicks(), delay,
35                       TestPendingTask::NON_NESTABLE));
36   return true;
37 }
38 
RunsTasksOnCurrentThread() const39 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const {
40   DCHECK(thread_checker_.CalledOnValidThread());
41   return true;
42 }
43 
44 const std::deque<TestPendingTask>&
GetPendingTasks() const45 TestSimpleTaskRunner::GetPendingTasks() const {
46   DCHECK(thread_checker_.CalledOnValidThread());
47   return pending_tasks_;
48 }
49 
HasPendingTask() const50 bool TestSimpleTaskRunner::HasPendingTask() const {
51   DCHECK(thread_checker_.CalledOnValidThread());
52   return !pending_tasks_.empty();
53 }
54 
NextPendingTaskDelay() const55 base::TimeDelta TestSimpleTaskRunner::NextPendingTaskDelay() const {
56   DCHECK(thread_checker_.CalledOnValidThread());
57   return pending_tasks_.front().GetTimeToRun() - base::TimeTicks();
58 }
59 
ClearPendingTasks()60 void TestSimpleTaskRunner::ClearPendingTasks() {
61   DCHECK(thread_checker_.CalledOnValidThread());
62   pending_tasks_.clear();
63 }
64 
RunPendingTasks()65 void TestSimpleTaskRunner::RunPendingTasks() {
66   DCHECK(thread_checker_.CalledOnValidThread());
67   // Swap with a local variable to avoid re-entrancy problems.
68   std::deque<TestPendingTask> tasks_to_run;
69   tasks_to_run.swap(pending_tasks_);
70   for (std::deque<TestPendingTask>::iterator it = tasks_to_run.begin();
71        it != tasks_to_run.end(); ++it) {
72     it->task.Run();
73   }
74 }
75 
RunUntilIdle()76 void TestSimpleTaskRunner::RunUntilIdle() {
77   while (!pending_tasks_.empty()) {
78     RunPendingTasks();
79   }
80 }
81 
82 }  // namespace base
83