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 DidBeginImplFrameOnThread(LayerTreeHostImpl
* host_impl
,
40 const BeginFrameArgs
& args
) {}
41 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl
* host_impl
,
43 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
44 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
45 virtual void WillActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
46 virtual void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
47 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
49 virtual DrawSwapReadbackResult::DrawResult
PrepareToDrawOnThread(
50 LayerTreeHostImpl
* host_impl
,
51 LayerTreeHostImpl::FrameData
* frame_data
,
52 DrawSwapReadbackResult::DrawResult draw_result
);
53 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
54 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
55 virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
56 virtual void UpdateVisibleTilesOnThread(LayerTreeHostImpl
* host_impl
) {}
57 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
58 base::TimeTicks monotonic_time
) {}
59 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
60 bool has_unfinished_animation
) {}
61 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
62 base::TimeTicks monotonic_time
) {}
63 virtual void ApplyScrollAndScale(const gfx::Vector2d
& scroll_delta
,
65 virtual void Animate(base::TimeTicks monotonic_time
) {}
66 virtual void WillBeginMainFrame() {}
67 virtual void DidBeginMainFrame() {}
68 virtual void Layout() {}
69 virtual void DidInitializeOutputSurface(bool succeeded
) {}
70 virtual void DidFailToInitializeOutputSurface() {}
71 virtual void DidAddAnimation() {}
72 virtual void WillCommit() {}
73 virtual void DidCommit() {}
74 virtual void DidCommitAndDrawFrame() {}
75 virtual void DidCompleteSwapBuffers() {}
76 virtual void ScheduleComposite() {}
77 virtual void ScheduleAnimation() {}
78 virtual void DidDeferCommit() {}
79 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
81 virtual base::TimeDelta
LowFrequencyAnimationInterval() const;
83 // Implementation of AnimationDelegate:
84 virtual void NotifyAnimationStarted(base::TimeTicks monotonic_time
,
85 Animation::TargetProperty target_property
)
87 virtual void NotifyAnimationFinished(
88 base::TimeTicks monotonic_time
,
89 Animation::TargetProperty target_property
) OVERRIDE
{}
91 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface(bool fallback
) = 0;
92 virtual scoped_refptr
<ContextProvider
> OffscreenContextProvider() = 0;
96 class LayerTreeHostClientForTesting
;
99 // The LayerTreeTests runs with the main loop running. It instantiates a single
100 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
101 // LayerTreeHostClientForTesting.
103 // BeginTest() is called once the main message loop is running and the layer
104 // tree host is initialized.
106 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
107 // LayerTreeTest methods of similar names. To track the commit process, override
110 // The test continues until someone calls EndTest. EndTest can be called on any
111 // thread, but be aware that ending the test is an asynchronous process.
112 class LayerTreeTest
: public testing::Test
, public TestHooks
{
114 virtual ~LayerTreeTest();
116 virtual void EndTest();
117 void EndTestAfterDelay(int delay_milliseconds
);
119 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
120 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
121 void PostAddLongAnimationToMainThread(Layer
* layer_to_receive_animation
);
122 void PostSetNeedsCommitToMainThread();
123 void PostSetNeedsUpdateLayersToMainThread();
124 void PostReadbackToMainThread();
125 void PostAcquireLayerTextures();
126 void PostSetNeedsRedrawToMainThread();
127 void PostSetNeedsRedrawRectToMainThread(const gfx::Rect
& damage_rect
);
128 void PostSetVisibleToMainThread(bool visible
);
129 void PostSetNextCommitForcesRedrawToMainThread();
137 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
139 virtual void ScheduleComposite() OVERRIDE
;
143 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
144 double animation_duration
);
145 void DispatchSetNeedsCommit();
146 void DispatchSetNeedsUpdateLayers();
147 void DispatchReadback();
148 void DispatchAcquireLayerTextures();
149 void DispatchSetNeedsRedraw();
150 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
151 void DispatchSetVisible(bool visible
);
152 void DispatchSetNextCommitForcesRedraw();
153 void DispatchComposite();
154 void DispatchDidAddAnimation();
156 virtual void AfterTest() = 0;
157 virtual void WillBeginTest();
158 virtual void BeginTest() = 0;
159 virtual void SetupTree();
161 virtual void RunTest(bool threaded
,
162 bool delegating_renderer
,
163 bool impl_side_painting
);
164 virtual void RunTestWithImplSidePainting();
166 bool HasImplThread() { return proxy() ? proxy()->HasImplThread() : false; }
167 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
169 return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
170 : main_task_runner_
.get();
172 base::SingleThreadTaskRunner
* MainThreadTaskRunner() {
173 return main_task_runner_
.get();
175 Proxy
* proxy() const {
176 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
179 bool TestEnded() const { return ended_
; }
181 LayerTreeHost
* layer_tree_host() { return layer_tree_host_
.get(); }
182 bool delegating_renderer() const { return delegating_renderer_
; }
183 FakeOutputSurface
* output_surface() { return output_surface_
; }
184 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
186 void DestroyLayerTreeHost();
188 // Override this for pixel tests, where you need a real output surface.
189 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface(bool fallback
) OVERRIDE
;
190 // Override this for unit tests, which should not produce pixel output.
191 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface(bool fallback
);
193 virtual scoped_refptr
<ContextProvider
> OffscreenContextProvider() OVERRIDE
;
195 TestWebGraphicsContext3D
* TestContext();
199 LayerTreeSettings settings_
;
200 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
201 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
202 FakeOutputSurface
* output_surface_
;
205 bool end_when_begin_returns_
;
208 bool schedule_when_set_visible_true_
;
211 bool delegating_renderer_
;
213 int timeout_seconds_
;
215 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
216 scoped_ptr
<base::Thread
> impl_thread_
;
217 base::CancelableClosure timeout_
;
218 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
219 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
220 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
225 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
226 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer) { \
227 RunTest(false, false, false); \
229 class SingleThreadDirectNeedsSemicolon##TEST_FIXTURE_NAME {}
231 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
232 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) { \
233 RunTest(false, true, false); \
235 class SingleThreadDelegatingNeedsSemicolon##TEST_FIXTURE_NAME {}
237 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
238 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
239 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
241 #define MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
242 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_MainThreadPaint) { \
243 RunTest(true, false, false); \
246 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
247 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
248 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_ImplSidePaint) { \
249 RunTest(true, false, true); \
251 class MultiThreadDirectNeedsSemicolon##TEST_FIXTURE_NAME {}
253 #define MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
254 TEST_F(TEST_FIXTURE_NAME, \
255 RunMultiThread_DelegatingRenderer_MainThreadPaint) { \
256 RunTest(true, true, false); \
259 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
260 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) TEST_F( \
261 TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer_ImplSidePaint) { \
262 RunTest(true, true, true); \
264 class MultiThreadDelegatingNeedsSemicolon##TEST_FIXTURE_NAME {}
266 #define MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
267 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
268 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
270 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
271 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
272 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
274 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F( \
276 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
277 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
279 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
280 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
281 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
283 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F( \
285 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
286 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
288 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
289 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
290 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
292 #define SINGLE_AND_MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
293 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
294 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
296 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
297 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
298 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
300 #endif // CC_TEST_LAYER_TREE_TEST_H_