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/base/thread.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"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebAnimationDelegate.h"
17 class WebGraphicsContext3D
;
21 class FakeLayerTreeHostClient
;
24 class LayerTreeHostClient
;
25 class LayerTreeHostImpl
;
27 // Used by test stubs to notify the test when something interesting happens.
28 class TestHooks
: public WebKit::WebAnimationDelegate
{
33 void ReadSettings(const LayerTreeSettings
& settings
);
35 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
36 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
37 virtual void TreeActivatedOnThread(LayerTreeHostImpl
* host_impl
) {}
38 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
40 virtual bool PrepareToDrawOnThread(LayerTreeHostImpl
* host_impl
,
41 LayerTreeHostImpl::FrameData
* frame_data
,
43 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
44 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
45 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
46 base::TimeTicks monotonic_time
) {}
47 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
48 bool has_unfinished_animation
) {}
49 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
50 base::TimeTicks monotonic_time
) {}
51 virtual void ApplyScrollAndScale(gfx::Vector2d scroll_delta
,
53 virtual void Animate(base::TimeTicks monotonic_time
) {}
54 virtual void Layout() {}
55 virtual void DidRecreateOutputSurface(bool succeeded
) {}
56 virtual void WillRetryRecreateOutputSurface() {}
57 virtual void DidAddAnimation() {}
58 virtual void DidCommit() {}
59 virtual void DidCommitAndDrawFrame() {}
60 virtual void ScheduleComposite() {}
61 virtual void DidDeferCommit() {}
62 virtual bool CanActivatePendingTree();
63 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
66 // Implementation of WebAnimationDelegate
67 virtual void notifyAnimationStarted(double time
) OVERRIDE
{}
68 virtual void notifyAnimationFinished(double time
) OVERRIDE
{}
70 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface();
72 virtual scoped_refptr
<cc::ContextProvider
>
73 OffscreenContextProviderForMainThread();
74 virtual scoped_refptr
<cc::ContextProvider
>
75 OffscreenContextProviderForCompositorThread();
78 scoped_ptr
<FakeLayerTreeHostClient
> fake_client_
;
82 class LayerTreeHostClientForTesting
;
85 // The LayerTreeTests runs with the main loop running. It instantiates a single
86 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
87 // LayerTreeHostClientForTesting.
89 // BeginTest() is called once the main message loop is running and the layer
90 // tree host is initialized.
92 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
93 // LayerTreeTest methods of similar names. To track the commit process, override
96 // The test continues until someone calls EndTest. EndTest can be called on any
97 // thread, but be aware that ending the test is an asynchronous process.
98 class LayerTreeTest
: public testing::Test
, public TestHooks
{
100 virtual ~LayerTreeTest();
102 virtual void AfterTest() = 0;
103 virtual void BeginTest() = 0;
104 virtual void SetupTree();
107 void EndTestAfterDelay(int delay_milliseconds
);
109 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
110 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
111 void PostSetNeedsCommitToMainThread();
112 void PostAcquireLayerTextures();
113 void PostSetNeedsRedrawToMainThread();
114 void PostSetNeedsRedrawRectToMainThread(gfx::Rect damage_rect
);
115 void PostSetVisibleToMainThread(bool visible
);
123 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
125 virtual void ScheduleComposite() OVERRIDE
;
129 virtual void DispatchAddInstantAnimation(Layer
* layer_to_receive_animation
);
130 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
);
131 void DispatchSetNeedsCommit();
132 void DispatchAcquireLayerTextures();
133 void DispatchSetNeedsRedraw();
134 void DispatchSetNeedsRedrawRect(gfx::Rect damage_rect
);
135 void DispatchSetVisible(bool visible
);
136 void DispatchComposite();
137 void DispatchDidAddAnimation();
139 virtual void RunTest(bool threaded
);
141 Thread
* ImplThread() { return proxy() ? proxy()->ImplThread() : NULL
; }
142 Proxy
* proxy() const {
143 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
146 bool TestEnded() const { return ended_
; }
148 LayerTreeHost
* layer_tree_host() { return layer_tree_host_
.get(); }
151 LayerTreeSettings settings_
;
152 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
153 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
156 bool end_when_begin_returns_
;
159 bool schedule_when_set_visible_true_
;
163 int timeout_seconds_
;
165 scoped_ptr
<Thread
> main_ccthread_
;
166 scoped_ptr
<base::Thread
> impl_thread_
;
167 base::CancelableClosure timeout_
;
168 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
169 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
174 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
175 TEST_F(TEST_FIXTURE_NAME, RunSingleThread) { \
178 class SingleThreadNeedsSemicolon##TEST_FIXTURE_NAME {}
181 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
182 TEST_F(TEST_FIXTURE_NAME, RunMultiThread) { \
185 class MultiThreadNeedsSemicolon##TEST_FIXTURE_NAME {}
187 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
188 SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME); \
189 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
191 #endif // CC_TEST_LAYER_TREE_TEST_H_