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 #ifndef BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
6 #define BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/test/test_pending_task.h"
14 #include "base/threading/thread_checker.h"
20 // TestSimpleTaskRunner is a simple TaskRunner implementation that can
21 // be used for testing. It implements SingleThreadTaskRunner as that
22 // interface implements SequencedTaskRunner, which in turn implements
23 // TaskRunner, so TestSimpleTaskRunner can be passed in to a function
24 // that accepts any *TaskRunner object.
26 // TestSimpleTaskRunner has the following properties which make it simple:
28 // - It is non-thread safe; all member functions must be called on
30 // - Tasks are simply stored in a queue in FIFO order, ignoring delay
32 // - Tasks aren't guaranteed to be destroyed immediately after
35 // However, TestSimpleTaskRunner allows for reentrancy, in that it
36 // handles the running of tasks that in turn call back into itself
37 // (e.g., to post more tasks).
39 // If you need more complicated properties, consider using this class
40 // as a template for writing a test TaskRunner implementation using
43 // Note that, like any TaskRunner, TestSimpleTaskRunner is
45 class TestSimpleTaskRunner
: public SingleThreadTaskRunner
{
47 TestSimpleTaskRunner();
49 // SingleThreadTaskRunner implementation.
50 virtual bool PostDelayedTask(const tracked_objects::Location
& from_here
,
52 TimeDelta delay
) OVERRIDE
;
53 virtual bool PostNonNestableDelayedTask(
54 const tracked_objects::Location
& from_here
,
56 TimeDelta delay
) OVERRIDE
;
58 virtual bool RunsTasksOnCurrentThread() const OVERRIDE
;
60 const std::deque
<TestPendingTask
>& GetPendingTasks() const;
61 bool HasPendingTask() const;
62 base::TimeDelta
NextPendingTaskDelay() const;
64 // Clears the queue of pending tasks without running them.
65 void ClearPendingTasks();
67 // Runs each current pending task in order and clears the queue.
68 // Any tasks posted by the tasks are not run.
69 virtual void RunPendingTasks();
71 // Runs pending tasks until the queue is empty.
75 virtual ~TestSimpleTaskRunner();
77 std::deque
<TestPendingTask
> pending_tasks_
;
78 ThreadChecker thread_checker_
;
81 DISALLOW_COPY_AND_ASSIGN(TestSimpleTaskRunner
);
86 #endif // BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_