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_LAYER_TREE_TEST_H_
6 #define CC_TEST_LAYER_TREE_TEST_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/threading/thread.h"
10 #include "cc/animation/animation_delegate.h"
11 #include "cc/trees/layer_tree_host.h"
12 #include "cc/trees/layer_tree_host_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 class WebGraphicsContext3D
;
20 class FakeLayerTreeHostClient
;
21 class FakeOutputSurface
;
24 class LayerTreeHostClient
;
25 class LayerTreeHostImpl
;
26 class TestContextProvider
;
27 class TestWebGraphicsContext3D
;
29 // Used by test stubs to notify the test when something interesting happens.
30 class TestHooks
: public AnimationDelegate
{
35 void ReadSettings(const LayerTreeSettings
& settings
);
37 virtual void WillBeginImplFrameOnThread(LayerTreeHostImpl
* host_impl
,
38 const BeginFrameArgs
& args
) {}
39 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl
* host_impl
,
41 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
42 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
43 virtual void WillActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
44 virtual void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
45 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
47 virtual DrawResult
PrepareToDrawOnThread(
48 LayerTreeHostImpl
* host_impl
,
49 LayerTreeHostImpl::FrameData
* frame_data
,
50 DrawResult draw_result
);
51 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
52 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
53 virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
54 virtual void UpdateVisibleTilesOnThread(LayerTreeHostImpl
* host_impl
) {}
55 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
56 base::TimeTicks monotonic_time
) {}
57 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
58 bool has_unfinished_animation
) {}
59 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
60 base::TimeTicks monotonic_time
) {}
61 virtual void ApplyViewportDeltas(const gfx::Vector2d
& scroll_delta
,
63 float top_controls_delta
) {}
64 virtual void BeginMainFrame(const BeginFrameArgs
& args
) {}
65 virtual void WillBeginMainFrame() {}
66 virtual void DidBeginMainFrame() {}
67 virtual void Layout() {}
68 virtual void DidInitializeOutputSurface() {}
69 virtual void DidFailToInitializeOutputSurface() {}
70 virtual void DidAddAnimation() {}
71 virtual void WillCommit() {}
72 virtual void DidCommit() {}
73 virtual void DidCommitAndDrawFrame() {}
74 virtual void DidCompleteSwapBuffers() {}
75 virtual void DidDeferCommit() {}
76 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
78 virtual base::TimeDelta
LowFrequencyAnimationInterval() const;
80 // Hooks for SchedulerClient.
81 virtual void ScheduledActionWillSendBeginMainFrame() {}
82 virtual void ScheduledActionSendBeginMainFrame() {}
83 virtual void ScheduledActionDrawAndSwapIfPossible() {}
84 virtual void ScheduledActionAnimate() {}
85 virtual void ScheduledActionCommit() {}
86 virtual void ScheduledActionBeginOutputSurfaceCreation() {}
88 // Implementation of AnimationDelegate:
89 virtual void NotifyAnimationStarted(base::TimeTicks monotonic_time
,
90 Animation::TargetProperty target_property
)
92 virtual void NotifyAnimationFinished(
93 base::TimeTicks monotonic_time
,
94 Animation::TargetProperty target_property
) OVERRIDE
{}
96 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface(bool fallback
) = 0;
100 class LayerTreeHostClientForTesting
;
103 // The LayerTreeTests runs with the main loop running. It instantiates a single
104 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
105 // LayerTreeHostClientForTesting.
107 // BeginTest() is called once the main message loop is running and the layer
108 // tree host is initialized.
110 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
111 // LayerTreeTest methods of similar names. To track the commit process, override
114 // The test continues until someone calls EndTest. EndTest can be called on any
115 // thread, but be aware that ending the test is an asynchronous process.
116 class LayerTreeTest
: public testing::Test
, public TestHooks
{
118 virtual ~LayerTreeTest();
120 virtual void EndTest();
121 void EndTestAfterDelay(int delay_milliseconds
);
123 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
124 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
125 void PostAddLongAnimationToMainThread(Layer
* layer_to_receive_animation
);
126 void PostSetNeedsCommitToMainThread();
127 void PostSetNeedsUpdateLayersToMainThread();
128 void PostSetNeedsRedrawToMainThread();
129 void PostSetNeedsRedrawRectToMainThread(const gfx::Rect
& damage_rect
);
130 void PostSetVisibleToMainThread(bool visible
);
131 void PostSetNextCommitForcesRedrawToMainThread();
139 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
143 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
144 double animation_duration
);
145 void DispatchSetNeedsCommit();
146 void DispatchSetNeedsUpdateLayers();
147 void DispatchSetNeedsRedraw();
148 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
149 void DispatchSetVisible(bool visible
);
150 void DispatchSetNextCommitForcesRedraw();
151 void DispatchDidAddAnimation();
153 virtual void AfterTest() = 0;
154 virtual void WillBeginTest();
155 virtual void BeginTest() = 0;
156 virtual void SetupTree();
158 virtual void RunTest(bool threaded
,
159 bool delegating_renderer
,
160 bool impl_side_painting
);
161 virtual void RunTestWithImplSidePainting();
163 bool HasImplThread() { return proxy() ? proxy()->HasImplThread() : false; }
164 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
166 return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
167 : main_task_runner_
.get();
169 base::SingleThreadTaskRunner
* MainThreadTaskRunner() {
170 return main_task_runner_
.get();
172 Proxy
* proxy() const {
173 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
176 bool TestEnded() const { return ended_
; }
178 LayerTreeHost
* layer_tree_host() { return layer_tree_host_
.get(); }
179 bool delegating_renderer() const { return delegating_renderer_
; }
180 FakeOutputSurface
* output_surface() { return output_surface_
; }
181 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
183 void DestroyLayerTreeHost();
185 // Override this for pixel tests, where you need a real output surface.
186 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface(bool fallback
) OVERRIDE
;
187 // Override this for unit tests, which should not produce pixel output.
188 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface(bool fallback
);
190 TestWebGraphicsContext3D
* TestContext();
194 LayerTreeSettings settings_
;
195 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
196 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
197 FakeOutputSurface
* output_surface_
;
200 bool end_when_begin_returns_
;
205 bool delegating_renderer_
;
207 int timeout_seconds_
;
209 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
210 scoped_ptr
<base::Thread
> impl_thread_
;
211 base::CancelableClosure timeout_
;
212 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
213 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
214 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
219 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
220 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer) { \
221 RunTest(false, false, false); \
223 class SingleThreadDirectNeedsSemicolon##TEST_FIXTURE_NAME {}
225 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
226 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) { \
227 RunTest(false, true, false); \
229 class SingleThreadDelegatingNeedsSemicolon##TEST_FIXTURE_NAME {}
231 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
232 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
233 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
235 #define MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
236 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_MainThreadPaint) { \
237 RunTest(true, false, false); \
240 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
241 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
242 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_ImplSidePaint) { \
243 RunTest(true, false, true); \
245 class MultiThreadDirectNeedsSemicolon##TEST_FIXTURE_NAME {}
247 #define MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
248 TEST_F(TEST_FIXTURE_NAME, \
249 RunMultiThread_DelegatingRenderer_MainThreadPaint) { \
250 RunTest(true, true, false); \
253 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
254 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) TEST_F( \
255 TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer_ImplSidePaint) { \
256 RunTest(true, true, true); \
258 class MultiThreadDelegatingNeedsSemicolon##TEST_FIXTURE_NAME {}
260 #define MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
261 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
262 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
264 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
265 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
266 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
268 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F( \
270 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
271 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
273 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
274 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
275 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
277 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F( \
279 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
280 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
282 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
283 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
284 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
286 #define SINGLE_AND_MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
287 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
288 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
290 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
291 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
292 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
294 #endif // CC_TEST_LAYER_TREE_TEST_H_