MediaCodecPlayer implementation - stage 6 (respect audio permission)
[chromium-blink-merge.git] / cc / test / ordered_simple_task_runner.h
blob91ea4be75cdc67f99e607ff90af27bb3cf2171c8
1 // Copyright 2014 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 CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_
6 #define CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_
8 #include <limits>
9 #include <set>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/logging.h"
15 #include "base/test/simple_test_tick_clock.h"
16 #include "base/test/test_simple_task_runner.h"
17 #include "base/trace_event/trace_event.h"
19 namespace cc {
21 // Subclass of TestPendingTask which has a unique ID for every task, supports
22 // being used inside a std::set and has debug tracing support.
23 class TestOrderablePendingTask : public base::TestPendingTask {
24 public:
25 TestOrderablePendingTask();
26 TestOrderablePendingTask(const tracked_objects::Location& location,
27 const base::Closure& task,
28 base::TimeTicks post_time,
29 base::TimeDelta delay,
30 TestNestability nestability);
31 ~TestOrderablePendingTask();
33 // operators needed by std::set and comparison
34 bool operator==(const TestOrderablePendingTask& other) const;
35 bool operator<(const TestOrderablePendingTask& other) const;
37 // base::trace_event tracing functionality
38 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValue() const;
39 void AsValueInto(base::trace_event::TracedValue* state) const;
41 private:
42 static size_t task_id_counter;
43 const size_t task_id_;
46 // This runs pending tasks based on task's post_time + delay.
47 // We should not execute a delayed task sooner than some of the queued tasks
48 // which don't have a delay even though it is queued early.
49 class OrderedSimpleTaskRunner : public base::SingleThreadTaskRunner {
50 public:
51 OrderedSimpleTaskRunner(base::SimpleTestTickClock* now_src, bool advance_now);
53 // base::TestSimpleTaskRunner implementation:
54 bool PostDelayedTask(const tracked_objects::Location& from_here,
55 const base::Closure& task,
56 base::TimeDelta delay) override;
57 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
58 const base::Closure& task,
59 base::TimeDelta delay) override;
61 bool RunsTasksOnCurrentThread() const override;
63 static base::TimeTicks AbsoluteMaxNow();
65 // Set a maximum number of tasks to run at once. Useful as a timeout to
66 // prevent infinite task loops.
67 static const size_t kAbsoluteMaxTasks;
68 void SetRunTaskLimit(size_t max_tasks) { max_tasks_ = max_tasks; }
69 void ClearRunTaskLimit() { max_tasks_ = kAbsoluteMaxTasks; }
71 // Allow task runner to advance now when running tasks.
72 void SetAutoAdvanceNowToPendingTasks(bool advance_now) {
73 advance_now_ = advance_now;
76 size_t NumPendingTasks() const;
77 bool HasPendingTasks() const;
78 base::TimeTicks NextTaskTime();
79 base::TimeDelta DelayToNextTaskTime();
81 // Run tasks while the callback returns true or too many tasks have been run.
82 // Returns true if there are still pending tasks left.
83 bool RunTasksWhile(base::Callback<bool(void)> condition);
85 // Run tasks while *all* of the callbacks return true or too many tasks have
86 // been run. Exits on the *first* condition which returns false, skipping
87 // calling all remaining conditions. Conditions can have side effects,
88 // including modifying the task queue.
89 // Returns true if there are still pending tasks left.
90 bool RunTasksWhile(const std::vector<base::Callback<bool(void)>>& conditions);
92 // Convenience functions to run tasks with common conditions.
94 // Run tasks which existed at the start of this call.
95 // Return code indicates tasks still exist to run.
96 bool RunPendingTasks();
97 // Keep running tasks until no tasks are left.
98 // Return code indicates tasks still exist to run which also indicates if
99 // runner reached idle.
100 bool RunUntilIdle();
101 // Keep running tasks until given time period.
102 // Return code indicates tasks still exist to run.
103 bool RunUntilTime(base::TimeTicks time);
104 bool RunForPeriod(base::TimeDelta period);
106 // base::trace_event tracing functionality
107 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValue() const;
108 virtual void AsValueInto(base::trace_event::TracedValue* state) const;
110 // Common conditions to run for, exposed publicly to allow external users to
111 // use their own combinations.
112 // -------------------------------------------------------------------------
114 // Keep running until the given number of tasks have run.
115 // You generally shouldn't use this check as it will cause your tests to fail
116 // when code is changed adding a new task. It is useful as a "timeout" type
117 // solution.
118 base::Callback<bool(void)> TaskRunCountBelow(size_t max_tasks);
120 // Keep running until a task which didn't exist initially would run.
121 base::Callback<bool(void)> TaskExistedInitially();
123 // Stop running tasks when NextTaskTime() >= stop_at
124 base::Callback<bool(void)> NowBefore(base::TimeTicks stop_at);
126 // Advance Now() to the next task to run.
127 base::Callback<bool(void)> AdvanceNow();
129 protected:
130 static bool TaskRunCountBelowCallback(size_t max_tasks, size_t* task_run);
131 bool TaskExistedInitiallyCallback(
132 const std::set<TestOrderablePendingTask>& existing_tasks);
133 bool NowBeforeCallback(base::TimeTicks stop_at);
134 bool AdvanceNowCallback();
136 ~OrderedSimpleTaskRunner() override;
138 base::ThreadChecker thread_checker_;
140 bool advance_now_;
141 // Not owned.
142 base::SimpleTestTickClock* now_src_;
144 size_t max_tasks_;
146 bool inside_run_tasks_until_;
147 std::set<TestOrderablePendingTask> pending_tasks_;
149 private:
150 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner);
153 } // namespace cc
155 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_