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 FakeTimeSourceClient
: public TimeSourceClient
{
24 FakeTimeSourceClient() : tick_called_(false) {}
25 void Reset() { tick_called_
= false; }
26 bool TickCalled() const { return tick_called_
; }
28 // TimeSourceClient implementation.
29 void OnTimerTick() override
;
35 DISALLOW_COPY_AND_ASSIGN(FakeTimeSourceClient
);
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 SetDrawDurationEstimate(base::TimeDelta duration
);
170 void SetBeginMainFrameToCommitDurationEstimate(base::TimeDelta duration
);
171 void SetCommitToActivateDurationEstimate(base::TimeDelta duration
);
173 base::TimeDelta
DrawDurationEstimate() const override
;
174 base::TimeDelta
BeginMainFrameToCommitDurationEstimate() const override
;
175 base::TimeDelta
CommitToActivateDurationEstimate() const override
;
178 FakeCompositorTimingHistory(scoped_ptr
<RenderingStatsInstrumentation
>
179 rendering_stats_instrumentation_owned
);
181 scoped_ptr
<RenderingStatsInstrumentation
>
182 rendering_stats_instrumentation_owned_
;
184 base::TimeDelta draw_duration_
;
185 base::TimeDelta begin_main_frame_to_commit_duration_
;
186 base::TimeDelta commit_to_activate_duration_
;
189 DISALLOW_COPY_AND_ASSIGN(FakeCompositorTimingHistory
);
192 class TestScheduler
: public Scheduler
{
194 static scoped_ptr
<TestScheduler
> Create(
195 base::SimpleTestTickClock
* now_src
,
196 SchedulerClient
* client
,
197 const SchedulerSettings
& scheduler_settings
,
198 int layer_tree_host_id
,
199 OrderedSimpleTaskRunner
* task_runner
,
200 BeginFrameSource
* external_frame_source
,
201 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history
);
203 // Extra test helper functionality
204 bool IsBeginRetroFrameArgsEmpty() const {
205 return begin_retro_frame_args_
.empty();
208 bool CanStart() const { return state_machine_
.CanStartForTesting(); }
210 BeginFrameSource
& frame_source() { return *frame_source_
; }
211 bool FrameProductionThrottled() { return throttle_frame_production_
; }
213 ~TestScheduler() override
;
215 base::TimeDelta
BeginImplFrameInterval() {
216 return begin_impl_frame_tracker_
.Interval();
220 // Overridden from Scheduler.
221 base::TimeTicks
Now() const override
;
225 base::SimpleTestTickClock
* now_src
,
226 SchedulerClient
* client
,
227 const SchedulerSettings
& scheduler_settings
,
228 int layer_tree_host_id
,
229 OrderedSimpleTaskRunner
* task_runner
,
230 BeginFrameSource
* external_frame_source
,
231 scoped_ptr
<TestSyntheticBeginFrameSource
> synthetic_frame_source
,
232 scoped_ptr
<TestBackToBackBeginFrameSource
> unthrottled_frame_source
,
233 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history
);
235 base::SimpleTestTickClock
* now_src_
;
237 DISALLOW_COPY_AND_ASSIGN(TestScheduler
);
242 #endif // CC_TEST_SCHEDULER_TEST_COMMON_H_