blob: 7481b6deda2a8127ad8fb8790f8dc7ae5975fc35 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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#ifndef BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
6#define BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
7
8#include <deque>
9
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"
15
16namespace base {
17
18class TimeDelta;
19
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.
25//
26// TestSimpleTaskRunner has the following properties which make it simple:
27//
28// - It is non-thread safe; all member functions must be called on
29// the same thread.
30// - Tasks are simply stored in a queue in FIFO order, ignoring delay
31// and nestability.
32// - Tasks aren't guaranteed to be destroyed immediately after
33// they're run.
34//
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).
38//
39// If you need more complicated properties, consider using this class
40// as a template for writing a test TaskRunner implementation using
41// TestPendingTask.
42//
43// Note that, like any TaskRunner, TestSimpleTaskRunner is
44// ref-counted.
45class TestSimpleTaskRunner : public SingleThreadTaskRunner {
46 public:
47 TestSimpleTaskRunner();
48
49 // SingleThreadTaskRunner implementation.
James Robinsone1b30cf2014-10-21 12:25:40 -070050 bool PostDelayedTask(const tracked_objects::Location& from_here,
51 const Closure& task,
52 TimeDelta delay) override;
53 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
54 const Closure& task,
55 TimeDelta delay) override;
James Robinson646469d2014-10-03 15:33:28 -070056
James Robinsone1b30cf2014-10-21 12:25:40 -070057 bool RunsTasksOnCurrentThread() const override;
James Robinson646469d2014-10-03 15:33:28 -070058
59 const std::deque<TestPendingTask>& GetPendingTasks() const;
60 bool HasPendingTask() const;
61 base::TimeDelta NextPendingTaskDelay() const;
62
63 // Clears the queue of pending tasks without running them.
64 void ClearPendingTasks();
65
66 // Runs each current pending task in order and clears the queue.
67 // Any tasks posted by the tasks are not run.
68 virtual void RunPendingTasks();
69
70 // Runs pending tasks until the queue is empty.
71 void RunUntilIdle();
72
73 protected:
James Robinsone1b30cf2014-10-21 12:25:40 -070074 ~TestSimpleTaskRunner() override;
James Robinson646469d2014-10-03 15:33:28 -070075
76 std::deque<TestPendingTask> pending_tasks_;
77 ThreadChecker thread_checker_;
78
79 private:
80 DISALLOW_COPY_AND_ASSIGN(TestSimpleTaskRunner);
81};
82
83} // namespace base
84
85#endif // BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_