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"
15 namespace Webkit
{ class WebGraphicsContext3D
; }
18 class FakeLayerTreeHostClient
;
19 class FakeOutputSurface
;
22 class LayerTreeHostClient
;
23 class LayerTreeHostImpl
;
24 class TestContextProvider
;
26 // Used by test stubs to notify the test when something interesting happens.
27 class TestHooks
: public AnimationDelegate
{
32 void ReadSettings(const LayerTreeSettings
& settings
);
34 virtual void WillBeginImplFrameOnThread(LayerTreeHostImpl
* host_impl
,
35 const BeginFrameArgs
& args
) {}
36 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
37 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
38 virtual void WillActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
39 virtual void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
40 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
42 virtual bool PrepareToDrawOnThread(LayerTreeHostImpl
* host_impl
,
43 LayerTreeHostImpl::FrameData
* frame_data
,
45 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
46 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
47 virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
48 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
49 base::TimeTicks monotonic_time
) {}
50 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
51 bool has_unfinished_animation
) {}
52 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
53 base::TimeTicks monotonic_time
) {}
54 virtual void ApplyScrollAndScale(gfx::Vector2d scroll_delta
,
56 virtual void Animate(base::TimeTicks monotonic_time
) {}
57 virtual void WillBeginFrame() {}
58 virtual void DidBeginFrame() {}
59 virtual void Layout() {}
60 virtual void DidInitializeOutputSurface(bool succeeded
) {}
61 virtual void DidFailToInitializeOutputSurface() {}
62 virtual void DidAddAnimation() {}
63 virtual void WillCommit() {}
64 virtual void DidCommit() {}
65 virtual void DidCommitAndDrawFrame() {}
66 virtual void DidCompleteSwapBuffers() {}
67 virtual void ScheduleComposite() {}
68 virtual void DidDeferCommit() {}
69 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
71 virtual base::TimeDelta
LowFrequencyAnimationInterval() const;
73 // Implementation of AnimationDelegate:
74 virtual void NotifyAnimationStarted(double time
) OVERRIDE
{}
75 virtual void NotifyAnimationFinished(double time
) OVERRIDE
{}
77 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface(bool fallback
) = 0;
78 virtual scoped_refptr
<cc::ContextProvider
>
79 OffscreenContextProviderForMainThread() = 0;
80 virtual scoped_refptr
<cc::ContextProvider
>
81 OffscreenContextProviderForCompositorThread() = 0;
85 class LayerTreeHostClientForTesting
;
88 // The LayerTreeTests runs with the main loop running. It instantiates a single
89 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
90 // LayerTreeHostClientForTesting.
92 // BeginTest() is called once the main message loop is running and the layer
93 // tree host is initialized.
95 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
96 // LayerTreeTest methods of similar names. To track the commit process, override
99 // The test continues until someone calls EndTest. EndTest can be called on any
100 // thread, but be aware that ending the test is an asynchronous process.
101 class LayerTreeTest
: public testing::Test
, public TestHooks
{
103 virtual ~LayerTreeTest();
105 virtual void AfterTest() = 0;
106 virtual void BeginTest() = 0;
107 virtual void SetupTree();
109 virtual void EndTest();
110 void EndTestAfterDelay(int delay_milliseconds
);
112 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
113 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
114 void PostSetNeedsCommitToMainThread();
115 void PostAcquireLayerTextures();
116 void PostSetNeedsRedrawToMainThread();
117 void PostSetNeedsRedrawRectToMainThread(gfx::Rect damage_rect
);
118 void PostSetVisibleToMainThread(bool visible
);
126 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
128 virtual void ScheduleComposite() OVERRIDE
;
132 virtual void DispatchAddInstantAnimation(Layer
* layer_to_receive_animation
);
133 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
);
134 void DispatchSetNeedsCommit();
135 void DispatchAcquireLayerTextures();
136 void DispatchSetNeedsRedraw();
137 void DispatchSetNeedsRedrawRect(gfx::Rect damage_rect
);
138 void DispatchSetVisible(bool visible
);
139 void DispatchComposite();
140 void DispatchDidAddAnimation();
142 virtual void RunTest(bool threaded
,
143 bool delegating_renderer
,
144 bool impl_side_painting
);
146 bool HasImplThread() { return proxy() ? proxy()->HasImplThread() : false; }
147 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
148 return proxy() ? proxy()->ImplThreadTaskRunner() : NULL
;
150 Proxy
* proxy() const {
151 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
154 bool TestEnded() const { return ended_
; }
156 LayerTreeHost
* layer_tree_host() { return layer_tree_host_
.get(); }
157 bool delegating_renderer() const { return delegating_renderer_
; }
158 FakeOutputSurface
* output_surface() { return output_surface_
; }
160 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface(bool fallback
) OVERRIDE
;
161 virtual scoped_refptr
<cc::ContextProvider
>
162 OffscreenContextProviderForMainThread() OVERRIDE
;
163 virtual scoped_refptr
<cc::ContextProvider
>
164 OffscreenContextProviderForCompositorThread() OVERRIDE
;
167 LayerTreeSettings settings_
;
168 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
169 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
170 FakeOutputSurface
* output_surface_
;
173 bool end_when_begin_returns_
;
176 bool schedule_when_set_visible_true_
;
179 bool delegating_renderer_
;
181 int timeout_seconds_
;
183 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
184 scoped_ptr
<base::Thread
> impl_thread_
;
185 base::CancelableClosure timeout_
;
186 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
187 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
188 scoped_refptr
<TestContextProvider
> main_thread_contexts_
;
189 scoped_refptr
<TestContextProvider
> compositor_thread_contexts_
;
194 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
195 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer) { \
196 RunTest(false, false, false); \
198 class SingleThreadDirectNeedsSemicolon##TEST_FIXTURE_NAME {}
200 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
201 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) { \
202 RunTest(false, true, false); \
204 class SingleThreadDelegatingNeedsSemicolon##TEST_FIXTURE_NAME {}
206 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
207 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
208 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
210 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
211 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_MainThreadPaint) { \
212 RunTest(true, false, false); \
214 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_ImplSidePaint) { \
215 RunTest(true, false, true); \
217 class MultiThreadDirectNeedsSemicolon##TEST_FIXTURE_NAME {}
219 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
220 TEST_F(TEST_FIXTURE_NAME, \
221 RunMultiThread_DelegatingRenderer_MainThreadPaint) { \
222 RunTest(true, true, false); \
224 TEST_F(TEST_FIXTURE_NAME, \
225 RunMultiThread_DelegatingRenderer_ImplSidePaint) { \
226 RunTest(true, true, true); \
228 class MultiThreadDelegatingNeedsSemicolon##TEST_FIXTURE_NAME {}
230 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
231 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
232 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
234 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
235 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
236 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
238 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
239 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
240 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
242 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
243 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
244 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
246 #endif // CC_TEST_LAYER_TREE_TEST_H_