1 // Copyright 2011 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_SCHEDULER_TEST_COMMON_H_
6 #define CC_TEST_SCHEDULER_TEST_COMMON_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "cc/scheduler/compositor_timing_history.h"
14 #include "cc/scheduler/scheduler.h"
15 #include "cc/test/ordered_simple_task_runner.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class RenderingStatsInstrumentation
;
22 class FakeDelayBasedTimeSourceClient
: public DelayBasedTimeSourceClient
{
24 FakeDelayBasedTimeSourceClient() : tick_called_(false) {}
25 void Reset() { tick_called_
= false; }
26 bool TickCalled() const { return tick_called_
; }
28 // DelayBasedTimeSourceClient implementation.
29 void OnTimerTick() override
;
35 DISALLOW_COPY_AND_ASSIGN(FakeDelayBasedTimeSourceClient
);
38 class FakeDelayBasedTimeSource
: public DelayBasedTimeSource
{
40 static scoped_ptr
<FakeDelayBasedTimeSource
> Create(
41 base::TimeDelta interval
,
42 base::SingleThreadTaskRunner
* task_runner
) {
43 return make_scoped_ptr(new FakeDelayBasedTimeSource(interval
, task_runner
));
46 ~FakeDelayBasedTimeSource() override
{}
48 void SetNow(base::TimeTicks time
) { now_
= time
; }
49 base::TimeTicks
Now() const override
;
52 FakeDelayBasedTimeSource(base::TimeDelta interval
,
53 base::SingleThreadTaskRunner
* task_runner
)
54 : DelayBasedTimeSource(interval
, task_runner
) {}
59 DISALLOW_COPY_AND_ASSIGN(FakeDelayBasedTimeSource
);
62 class TestDelayBasedTimeSource
: public DelayBasedTimeSource
{
64 static scoped_ptr
<TestDelayBasedTimeSource
> Create(
65 base::SimpleTestTickClock
* now_src
,
66 base::TimeDelta interval
,
67 OrderedSimpleTaskRunner
* task_runner
) {
68 return make_scoped_ptr(
69 new TestDelayBasedTimeSource(now_src
, interval
, task_runner
));
72 ~TestDelayBasedTimeSource() override
;
75 TestDelayBasedTimeSource(base::SimpleTestTickClock
* now_src
,
76 base::TimeDelta interval
,
77 OrderedSimpleTaskRunner
* task_runner
);
79 // Overridden from DelayBasedTimeSource
80 base::TimeTicks
Now() const override
;
81 std::string
TypeString() const override
;
84 base::SimpleTestTickClock
* now_src_
;
87 DISALLOW_COPY_AND_ASSIGN(TestDelayBasedTimeSource
);
90 class FakeBeginFrameSource
: public BeginFrameSourceBase
{
92 FakeBeginFrameSource() : remaining_frames_(false) {}
93 ~FakeBeginFrameSource() override
{}
95 BeginFrameObserver
* GetObserver() { return observer_
; }
97 BeginFrameArgs
TestLastUsedBeginFrameArgs() {
99 return observer_
->LastUsedBeginFrameArgs();
101 return BeginFrameArgs();
104 void TestOnBeginFrame(const BeginFrameArgs
& args
) {
105 return CallOnBeginFrame(args
);
109 void DidFinishFrame(size_t remaining_frames
) override
;
110 void AsValueInto(base::trace_event::TracedValue
* dict
) const override
;
113 bool remaining_frames_
;
115 DISALLOW_COPY_AND_ASSIGN(FakeBeginFrameSource
);
118 class TestBackToBackBeginFrameSource
: public BackToBackBeginFrameSource
{
120 ~TestBackToBackBeginFrameSource() override
;
122 static scoped_ptr
<TestBackToBackBeginFrameSource
> Create(
123 base::SimpleTestTickClock
* now_src
,
124 base::SingleThreadTaskRunner
* task_runner
) {
125 return make_scoped_ptr(
126 new TestBackToBackBeginFrameSource(now_src
, task_runner
));
130 TestBackToBackBeginFrameSource(base::SimpleTestTickClock
* now_src
,
131 base::SingleThreadTaskRunner
* task_runner
);
133 base::TimeTicks
Now() override
;
135 base::SimpleTestTickClock
* now_src_
;
138 DISALLOW_COPY_AND_ASSIGN(TestBackToBackBeginFrameSource
);
141 class TestSyntheticBeginFrameSource
: public SyntheticBeginFrameSource
{
143 ~TestSyntheticBeginFrameSource() override
;
145 static scoped_ptr
<TestSyntheticBeginFrameSource
> Create(
146 base::SimpleTestTickClock
* now_src
,
147 OrderedSimpleTaskRunner
* task_runner
,
148 base::TimeDelta initial_interval
) {
149 scoped_ptr
<TestDelayBasedTimeSource
> time_source
=
150 TestDelayBasedTimeSource::Create(now_src
, initial_interval
,
152 return make_scoped_ptr(
153 new TestSyntheticBeginFrameSource(time_source
.Pass()));
157 explicit TestSyntheticBeginFrameSource(
158 scoped_ptr
<DelayBasedTimeSource
> time_source
);
161 DISALLOW_COPY_AND_ASSIGN(TestSyntheticBeginFrameSource
);
164 class FakeCompositorTimingHistory
: public CompositorTimingHistory
{
166 static scoped_ptr
<FakeCompositorTimingHistory
> Create();
167 ~FakeCompositorTimingHistory() override
;
169 void SetAllEstimatesTo(base::TimeDelta duration
);
171 void SetBeginMainFrameToCommitDurationEstimate(base::TimeDelta duration
);
172 void SetCommitToReadyToActivateDurationEstimate(base::TimeDelta duration
);
173 void SetPrepareTilesDurationEstimate(base::TimeDelta duration
);
174 void SetActivateDurationEstimate(base::TimeDelta duration
);
175 void SetDrawDurationEstimate(base::TimeDelta duration
);
177 base::TimeDelta
BeginMainFrameToCommitDurationEstimate() const override
;
178 base::TimeDelta
CommitToReadyToActivateDurationEstimate() const override
;
179 base::TimeDelta
PrepareTilesDurationEstimate() const override
;
180 base::TimeDelta
ActivateDurationEstimate() const override
;
181 base::TimeDelta
DrawDurationEstimate() const override
;
184 FakeCompositorTimingHistory(scoped_ptr
<RenderingStatsInstrumentation
>
185 rendering_stats_instrumentation_owned
);
187 scoped_ptr
<RenderingStatsInstrumentation
>
188 rendering_stats_instrumentation_owned_
;
190 base::TimeDelta begin_main_frame_to_commit_duration_
;
191 base::TimeDelta commit_to_ready_to_activate_duration_
;
192 base::TimeDelta prepare_tiles_duration_
;
193 base::TimeDelta activate_duration_
;
194 base::TimeDelta draw_duration_
;
197 DISALLOW_COPY_AND_ASSIGN(FakeCompositorTimingHistory
);
200 class TestScheduler
: public Scheduler
{
202 static scoped_ptr
<TestScheduler
> Create(
203 base::SimpleTestTickClock
* now_src
,
204 SchedulerClient
* client
,
205 const SchedulerSettings
& scheduler_settings
,
206 int layer_tree_host_id
,
207 OrderedSimpleTaskRunner
* task_runner
,
208 BeginFrameSource
* external_frame_source
,
209 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history
);
211 // Extra test helper functionality
212 bool IsBeginRetroFrameArgsEmpty() const {
213 return begin_retro_frame_args_
.empty();
216 bool SwapThrottled() const { return state_machine_
.SwapThrottled(); }
218 bool CanStart() const { return state_machine_
.CanStartForTesting(); }
220 bool NeedsBeginMainFrame() const {
221 return state_machine_
.needs_begin_main_frame();
224 BeginFrameSource
& frame_source() { return *frame_source_
; }
225 bool FrameProductionThrottled() { return throttle_frame_production_
; }
227 bool MainThreadMissedLastDeadline() const {
228 return state_machine_
.main_thread_missed_last_deadline();
231 ~TestScheduler() override
;
233 base::TimeDelta
BeginImplFrameInterval() {
234 return begin_impl_frame_tracker_
.Interval();
238 // Overridden from Scheduler.
239 base::TimeTicks
Now() const override
;
243 base::SimpleTestTickClock
* now_src
,
244 SchedulerClient
* client
,
245 const SchedulerSettings
& scheduler_settings
,
246 int layer_tree_host_id
,
247 OrderedSimpleTaskRunner
* task_runner
,
248 BeginFrameSource
* external_frame_source
,
249 scoped_ptr
<TestSyntheticBeginFrameSource
> synthetic_frame_source
,
250 scoped_ptr
<TestBackToBackBeginFrameSource
> unthrottled_frame_source
,
251 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history
);
253 base::SimpleTestTickClock
* now_src_
;
255 DISALLOW_COPY_AND_ASSIGN(TestScheduler
);
260 #endif // CC_TEST_SCHEDULER_TEST_COMMON_H_