ozone: fix HDPMLegacy - do the PF after overlays, also clear old overlays
[chromium-blink-merge.git] / base / test / test_mock_time_task_runner.h
blob3a0a215c7c967fcbaa4d25209f6497fd6251465c
1 // Copyright 2015 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_MOCK_TIME_TASK_RUNNER_H_
6 #define BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_
8 #include <queue>
9 #include <vector>
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h"
15 #include "base/test/test_pending_task.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/time/tick_clock.h"
18 #include "base/time/time.h"
20 namespace base {
22 // Runs pending tasks in the order of the tasks' post time + delay, and keeps
23 // track of a mock (virtual) tick clock time that can be fast-forwarded.
25 // TestMockTimeTaskRunner has the following properties:
27 // - Methods RunsTasksOnCurrentThread() and Post[Delayed]Task() can be called
28 // from any thread, but the rest of the methods must be called on the same
29 // thread the TaskRunner was created on.
30 // - It allows for reentrancy, in that it handles the running of tasks that in
31 // turn call back into it (e.g., to post more tasks).
32 // - Tasks are stored in a priority queue, and executed in the increasing
33 // order of post time + delay.
34 // - Non-nestable tasks are not supported.
35 // - Tasks aren't guaranteed to be destroyed immediately after they're run.
37 // This is a slightly more sophisticated version of TestSimpleTaskRunner, in
38 // that it supports running delayed tasks in the correct temporal order.
39 class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner {
40 public:
41 TestMockTimeTaskRunner();
43 // Fast-forwards virtual time by |delta|, causing all tasks with a remaining
44 // delay less than or equal to |delta| to be executed.
45 void FastForwardBy(base::TimeDelta delta);
47 // Fast-forwards virtual time just until all tasks are executed.
48 void FastForwardUntilNoTasksRemain();
50 // Executes all tasks that have no remaining delay. Tasks with a remaining
51 // delay greater than zero will remain enqueued, and no virtual time will
52 // elapse.
53 void RunUntilIdle();
55 // Returns the current virtual time.
56 TimeTicks GetCurrentMockTime() const;
58 // Returns a TickClock that uses the mock time of |this| as its time source.
59 // The returned TickClock will hold a reference to |this|.
60 scoped_ptr<TickClock> GetMockTickClock() const;
62 bool HasPendingTask() const;
63 TimeDelta NextPendingTaskDelay() const;
65 // SingleThreadTaskRunner:
66 bool RunsTasksOnCurrentThread() const override;
67 bool PostDelayedTask(const tracked_objects::Location& from_here,
68 const base::Closure& task,
69 TimeDelta delay) override;
70 bool PostNonNestableDelayedTask(
71 const tracked_objects::Location& from_here,
72 const base::Closure& task,
73 TimeDelta delay) override;
75 protected:
76 ~TestMockTimeTaskRunner() override;
78 // Called before the next task to run is selected, so that subclasses have a
79 // last chance to make sure all tasks are posted.
80 virtual void OnBeforeSelectingTask();
82 // Called after the current mock time has been incremented so that subclasses
83 // can react to the passing of time.
84 virtual void OnAfterTimePassed();
86 // Called after each task is run so that subclasses may perform additional
87 // activities, e.g., pump additional task runners.
88 virtual void OnAfterTaskRun();
90 private:
91 // Predicate that defines a strict weak temporal ordering of tasks.
92 class TemporalOrder {
93 public:
94 bool operator()(const TestPendingTask& first_task,
95 const TestPendingTask& second_task) const;
98 typedef std::priority_queue<TestPendingTask,
99 std::vector<TestPendingTask>,
100 TemporalOrder> TaskPriorityQueue;
102 // Returns the |next_task| to run if there is any with a running time that is
103 // at most |reference| + |max_delta|. This additional complexity is required
104 // so that |max_delta| == TimeDelta::Max() can be supported.
105 bool DequeueNextTask(const base::TimeTicks& reference,
106 const base::TimeDelta& max_delta,
107 TestPendingTask* next_task);
109 base::ThreadChecker thread_checker_;
110 base::TimeTicks now_;
112 // Temporally ordered heap of pending tasks. Must only be accessed while the
113 // |tasks_lock_| is held.
114 TaskPriorityQueue tasks_;
115 base::Lock tasks_lock_;
117 DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner);
120 } // namespace base
122 #endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_