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.
5 #include "base/test/test_simple_task_runner.h"
7 #include "base/logging.h"
11 TestSimpleTaskRunner::TestSimpleTaskRunner() {}
13 TestSimpleTaskRunner::~TestSimpleTaskRunner() {
14 DCHECK(thread_checker_
.CalledOnValidThread());
17 bool TestSimpleTaskRunner::PostDelayedTask(
18 const tracked_objects::Location
& from_here
,
21 DCHECK(thread_checker_
.CalledOnValidThread());
22 pending_tasks_
.push_back(
23 TestPendingTask(from_here
, task
, TimeTicks(), delay
,
24 TestPendingTask::NESTABLE
));
28 bool TestSimpleTaskRunner::PostNonNestableDelayedTask(
29 const tracked_objects::Location
& from_here
,
32 DCHECK(thread_checker_
.CalledOnValidThread());
33 pending_tasks_
.push_back(
34 TestPendingTask(from_here
, task
, TimeTicks(), delay
,
35 TestPendingTask::NON_NESTABLE
));
39 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const {
40 DCHECK(thread_checker_
.CalledOnValidThread());
44 const std::deque
<TestPendingTask
>&
45 TestSimpleTaskRunner::GetPendingTasks() const {
46 DCHECK(thread_checker_
.CalledOnValidThread());
47 return pending_tasks_
;
50 bool TestSimpleTaskRunner::HasPendingTask() const {
51 DCHECK(thread_checker_
.CalledOnValidThread());
52 return !pending_tasks_
.empty();
55 base::TimeDelta
TestSimpleTaskRunner::NextPendingTaskDelay() const {
56 DCHECK(thread_checker_
.CalledOnValidThread());
57 return pending_tasks_
.front().GetTimeToRun() - base::TimeTicks();
60 void TestSimpleTaskRunner::ClearPendingTasks() {
61 DCHECK(thread_checker_
.CalledOnValidThread());
62 pending_tasks_
.clear();
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
) {
76 void TestSimpleTaskRunner::RunUntilIdle() {
77 while (!pending_tasks_
.empty()) {