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/delay_based_time_source.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 FakeTimeSourceClient
: public TimeSourceClient
{
22 FakeTimeSourceClient() : tick_called_(false) {}
23 void Reset() { tick_called_
= false; }
24 bool TickCalled() const { return tick_called_
; }
26 // TimeSourceClient implementation.
27 void OnTimerTick() override
;
33 class FakeDelayBasedTimeSource
: public DelayBasedTimeSource
{
35 static scoped_refptr
<FakeDelayBasedTimeSource
> Create(
36 base::TimeDelta interval
, base::SingleThreadTaskRunner
* task_runner
) {
37 return make_scoped_refptr(new FakeDelayBasedTimeSource(interval
,
41 void SetNow(base::TimeTicks time
) { now_
= time
; }
42 base::TimeTicks
Now() const override
;
45 FakeDelayBasedTimeSource(base::TimeDelta interval
,
46 base::SingleThreadTaskRunner
* task_runner
)
47 : DelayBasedTimeSource(interval
, task_runner
) {}
48 ~FakeDelayBasedTimeSource() override
{}
53 class TestDelayBasedTimeSource
: public DelayBasedTimeSource
{
55 static scoped_refptr
<TestDelayBasedTimeSource
> Create(
56 scoped_refptr
<TestNowSource
> now_src
,
57 base::TimeDelta interval
,
58 OrderedSimpleTaskRunner
* task_runner
) {
59 return make_scoped_refptr(
60 new TestDelayBasedTimeSource(now_src
, interval
, task_runner
));
64 TestDelayBasedTimeSource(scoped_refptr
<TestNowSource
> now_src
,
65 base::TimeDelta interval
,
66 OrderedSimpleTaskRunner
* task_runner
);
68 // Overridden from DelayBasedTimeSource
69 ~TestDelayBasedTimeSource() override
;
70 base::TimeTicks
Now() const override
;
71 std::string
TypeString() const override
;
73 scoped_refptr
<TestNowSource
> now_src_
;
76 struct FakeBeginFrameSource
: public BeginFrameSourceMixIn
{
77 bool remaining_frames_
= false;
79 BeginFrameObserver
* GetObserver() { return observer_
; }
81 BeginFrameArgs
TestLastUsedBeginFrameArgs() {
83 return observer_
->LastUsedBeginFrameArgs();
85 return BeginFrameArgs();
87 void TestOnBeginFrame(const BeginFrameArgs
& args
) {
88 return CallOnBeginFrame(args
);
92 void DidFinishFrame(size_t remaining_frames
) override
;
93 void AsValueInto(base::trace_event::TracedValue
* dict
) const override
;
95 ~FakeBeginFrameSource() override
{}
98 class TestBackToBackBeginFrameSource
: public BackToBackBeginFrameSource
{
100 ~TestBackToBackBeginFrameSource() override
;
102 static scoped_ptr
<TestBackToBackBeginFrameSource
> Create(
103 scoped_refptr
<TestNowSource
> now_src
,
104 base::SingleThreadTaskRunner
* task_runner
) {
105 return make_scoped_ptr(
106 new TestBackToBackBeginFrameSource(now_src
, task_runner
));
110 TestBackToBackBeginFrameSource(scoped_refptr
<TestNowSource
> now_src
,
111 base::SingleThreadTaskRunner
* task_runner
);
113 base::TimeTicks
Now() override
;
115 scoped_refptr
<TestNowSource
> now_src_
;
118 class TestSyntheticBeginFrameSource
: public SyntheticBeginFrameSource
{
120 ~TestSyntheticBeginFrameSource() override
;
122 static scoped_ptr
<TestSyntheticBeginFrameSource
> Create(
123 scoped_refptr
<TestNowSource
> now_src
,
124 OrderedSimpleTaskRunner
* task_runner
,
125 base::TimeDelta initial_interval
) {
126 return make_scoped_ptr(
127 new TestSyntheticBeginFrameSource(TestDelayBasedTimeSource::Create(
128 now_src
, initial_interval
, task_runner
)));
132 TestSyntheticBeginFrameSource(
133 scoped_refptr
<DelayBasedTimeSource
> time_source
);
137 class TestSchedulerFrameSourcesConstructor
138 : public SchedulerFrameSourcesConstructor
{
140 ~TestSchedulerFrameSourcesConstructor() override
;
143 BeginFrameSource
* ConstructPrimaryFrameSource(Scheduler
* scheduler
) override
;
144 BeginFrameSource
* ConstructUnthrottledFrameSource(
145 Scheduler
* scheduler
) override
;
147 OrderedSimpleTaskRunner
* test_task_runner_
;
148 TestNowSource
* now_src_
;
151 explicit TestSchedulerFrameSourcesConstructor(
152 OrderedSimpleTaskRunner
* test_task_runner
,
153 TestNowSource
* now_src
);
154 friend class TestScheduler
;
157 class TestScheduler
: public Scheduler
{
159 static scoped_ptr
<TestScheduler
> Create(
160 scoped_refptr
<TestNowSource
> now_src
,
161 SchedulerClient
* client
,
162 const SchedulerSettings
& scheduler_settings
,
163 int layer_tree_host_id
,
164 const scoped_refptr
<OrderedSimpleTaskRunner
>& task_runner
,
165 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
166 TestSchedulerFrameSourcesConstructor
frame_sources_constructor(
167 task_runner
.get(), now_src
.get());
168 return make_scoped_ptr(new TestScheduler(
174 &frame_sources_constructor
,
175 external_begin_frame_source
.Pass()));
178 // Extra test helper functionality
179 bool IsBeginRetroFrameArgsEmpty() const {
180 return begin_retro_frame_args_
.empty();
183 bool CanStart() const { return state_machine_
.CanStartForTesting(); }
185 BeginFrameSource
& frame_source() { return *frame_source_
; }
186 bool FrameProductionThrottled() { return throttle_frame_production_
; }
187 BeginFrameArgs
begin_impl_frame_args() { return begin_impl_frame_args_
; }
189 ~TestScheduler() override
;
191 void NotifyReadyToCommitThenActivateIfNeeded() {
192 NotifyReadyToCommit();
193 if (settings_
.impl_side_painting
) {
194 NotifyReadyToActivate();
199 // Overridden from Scheduler.
200 base::TimeTicks
Now() const override
;
204 scoped_refptr
<TestNowSource
> now_src
,
205 SchedulerClient
* client
,
206 const SchedulerSettings
& scheduler_settings
,
207 int layer_tree_host_id
,
208 const scoped_refptr
<OrderedSimpleTaskRunner
>& test_task_runner
,
209 TestSchedulerFrameSourcesConstructor
* frame_sources_constructor
,
210 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
212 scoped_refptr
<TestNowSource
> now_src_
;
217 #endif // CC_TEST_SCHEDULER_TEST_COMMON_H_