OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_TEST_LAYER_TREE_TEST_COMMON_H_ | 5 #ifndef CC_TEST_LAYER_TREE_TEST_COMMON_H_ |
6 #define CC_TEST_LAYER_TREE_TEST_COMMON_H_ | 6 #define CC_TEST_LAYER_TREE_TEST_COMMON_H_ |
7 | 7 |
8 #include "base/hash_tables.h" | 8 #include "base/hash_tables.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "cc/layer_tree_host.h" | 11 #include "cc/layer_tree_host.h" |
12 #include "cc/layer_tree_host_impl.h" | 12 #include "cc/layer_tree_host_impl.h" |
13 #include "cc/scoped_thread_proxy.h" | 13 #include "cc/scoped_thread_proxy.h" |
14 #include "cc/test/compositor_fake_web_graphics_context_3d.h" | 14 #include "cc/test/compositor_fake_web_graphics_context_3d.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include <public/WebAnimationDelegate.h> | 16 #include <public/WebAnimationDelegate.h> |
17 | 17 |
18 namespace cc { | 18 namespace cc { |
19 class LayerImpl; | 19 class LayerImpl; |
20 class LayerTreeHost; | 20 class LayerTreeHost; |
21 class LayerTreeHostClient; | 21 class LayerTreeHostClient; |
22 class LayerTreeHostImpl; | 22 class LayerTreeHostImpl; |
23 class Thread; | 23 class Thread; |
24 } | |
25 | 24 |
26 namespace cc { | |
27 | 25 |
28 // Used by test stubs to notify the test when something interesting happens. | 26 // Used by test stubs to notify the test when something interesting happens. |
29 class TestHooks : public WebKit::WebAnimationDelegate { | 27 class TestHooks : public WebKit::WebAnimationDelegate { |
30 public: | 28 public: |
31 virtual void beginCommitOnThread(cc::LayerTreeHostImpl*) { } | 29 virtual void beginCommitOnThread(LayerTreeHostImpl*) { } |
32 virtual void commitCompleteOnThread(cc::LayerTreeHostImpl*) { } | 30 virtual void commitCompleteOnThread(LayerTreeHostImpl*) { } |
33 virtual bool prepareToDrawOnThread(cc::LayerTreeHostImpl*); | 31 virtual bool prepareToDrawOnThread(LayerTreeHostImpl*); |
34 virtual void drawLayersOnThread(cc::LayerTreeHostImpl*) { } | 32 virtual void drawLayersOnThread(LayerTreeHostImpl*) { } |
35 virtual void animateLayers(cc::LayerTreeHostImpl*, base::TimeTicks monotonic
Time) { } | 33 virtual void animateLayers(LayerTreeHostImpl*, base::TimeTicks monotonicTime
) { } |
36 virtual void willAnimateLayers(cc::LayerTreeHostImpl*, base::TimeTicks monot
onicTime) { } | 34 virtual void willAnimateLayers(LayerTreeHostImpl*, base::TimeTicks monotonic
Time) { } |
37 virtual void applyScrollAndScale(gfx::Vector2d, float) { } | 35 virtual void applyScrollAndScale(gfx::Vector2d, float) { } |
38 virtual void animate(base::TimeTicks monotonicTime) { } | 36 virtual void animate(base::TimeTicks monotonicTime) { } |
39 virtual void layout() { } | 37 virtual void layout() { } |
40 virtual void didRecreateOutputSurface(bool succeeded) { } | 38 virtual void didRecreateOutputSurface(bool succeeded) { } |
41 virtual void didAddAnimation() { } | 39 virtual void didAddAnimation() { } |
42 virtual void didCommit() { } | 40 virtual void didCommit() { } |
43 virtual void didCommitAndDrawFrame() { } | 41 virtual void didCommitAndDrawFrame() { } |
44 virtual void scheduleComposite() { } | 42 virtual void scheduleComposite() { } |
45 virtual void didDeferCommit() { } | 43 virtual void didDeferCommit() { } |
46 | 44 |
47 // Implementation of WebAnimationDelegate | 45 // Implementation of WebAnimationDelegate |
48 virtual void notifyAnimationStarted(double time) OVERRIDE { } | 46 virtual void notifyAnimationStarted(double time) OVERRIDE { } |
49 virtual void notifyAnimationFinished(double time) OVERRIDE { } | 47 virtual void notifyAnimationFinished(double time) OVERRIDE { } |
50 | 48 |
51 virtual scoped_ptr<OutputSurface> createOutputSurface(); | 49 virtual scoped_ptr<OutputSurface> createOutputSurface(); |
52 }; | 50 }; |
53 | 51 |
54 class TimeoutTask; | 52 class TimeoutTask; |
55 class BeginTask; | 53 class BeginTask; |
56 | 54 |
57 class MockLayerImplTreeHostClient : public cc::LayerTreeHostClient { | 55 class MockLayerImplTreeHostClient : public LayerTreeHostClient { |
58 }; | 56 }; |
59 | 57 |
60 // The ThreadedTests runs with the main loop running. It instantiates a single M
ockLayerTreeHost and associated | 58 // The ThreadedTests runs with the main loop running. It instantiates a single M
ockLayerTreeHost and associated |
61 // MockLayerTreeHostImpl/ThreadedMockLayerTreeHostClient. | 59 // MockLayerTreeHostImpl/ThreadedMockLayerTreeHostClient. |
62 // | 60 // |
63 // beginTest() is called once the main message loop is running and the layer tre
e host is initialized. | 61 // beginTest() is called once the main message loop is running and the layer tre
e host is initialized. |
64 // | 62 // |
65 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to Thread
edTest methods of similar names. | 63 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to Thread
edTest methods of similar names. |
66 // To track the commit process, override these functions. | 64 // To track the commit process, override these functions. |
67 // | 65 // |
68 // The test continues until someone calls endTest. endTest can be called on any
thread, but be aware that | 66 // The test continues until someone calls endTest. endTest can be called on any
thread, but be aware that |
69 // ending the test is an asynchronous process. | 67 // ending the test is an asynchronous process. |
70 class ThreadedTest : public testing::Test, public TestHooks { | 68 class ThreadedTest : public testing::Test, public TestHooks { |
71 public: | 69 public: |
72 virtual ~ThreadedTest(); | 70 virtual ~ThreadedTest(); |
73 | 71 |
74 virtual void afterTest() = 0; | 72 virtual void afterTest() = 0; |
75 virtual void beginTest() = 0; | 73 virtual void beginTest() = 0; |
76 | 74 |
77 void endTest(); | 75 void endTest(); |
78 void endTestAfterDelay(int delayMilliseconds); | 76 void endTestAfterDelay(int delayMilliseconds); |
79 | 77 |
80 void postSetNeedsAnimateToMainThread(); | 78 void postSetNeedsAnimateToMainThread(); |
81 void postAddAnimationToMainThread(cc::Layer*); | 79 void postAddAnimationToMainThread(Layer*); |
82 void postAddInstantAnimationToMainThread(); | 80 void postAddInstantAnimationToMainThread(); |
83 void postSetNeedsCommitToMainThread(); | 81 void postSetNeedsCommitToMainThread(); |
84 void postAcquireLayerTextures(); | 82 void postAcquireLayerTextures(); |
85 void postSetNeedsRedrawToMainThread(); | 83 void postSetNeedsRedrawToMainThread(); |
86 void postSetVisibleToMainThread(bool visible); | 84 void postSetVisibleToMainThread(bool visible); |
87 void postDidAddAnimationToMainThread(); | 85 void postDidAddAnimationToMainThread(); |
88 | 86 |
89 void doBeginTest(); | 87 void doBeginTest(); |
90 void timeout(); | 88 void timeout(); |
91 | 89 |
92 cc::LayerTreeHost* layerTreeHost() { return m_layerTreeHost.get(); } | 90 LayerTreeHost* layerTreeHost() { return m_layerTreeHost.get(); } |
93 | 91 |
94 protected: | 92 protected: |
95 ThreadedTest(); | 93 ThreadedTest(); |
96 | 94 |
97 virtual void initializeSettings(cc::LayerTreeSettings&) { } | 95 virtual void initializeSettings(LayerTreeSettings&) { } |
98 | 96 |
99 virtual void scheduleComposite() OVERRIDE; | 97 virtual void scheduleComposite() OVERRIDE; |
100 | 98 |
101 void realEndTest(); | 99 void realEndTest(); |
102 | 100 |
103 void dispatchSetNeedsAnimate(); | 101 void dispatchSetNeedsAnimate(); |
104 void dispatchAddInstantAnimation(); | 102 void dispatchAddInstantAnimation(); |
105 void dispatchAddAnimation(cc::Layer*); | 103 void dispatchAddAnimation(Layer*); |
106 void dispatchSetNeedsCommit(); | 104 void dispatchSetNeedsCommit(); |
107 void dispatchAcquireLayerTextures(); | 105 void dispatchAcquireLayerTextures(); |
108 void dispatchSetNeedsRedraw(); | 106 void dispatchSetNeedsRedraw(); |
109 void dispatchSetVisible(bool); | 107 void dispatchSetVisible(bool); |
110 void dispatchComposite(); | 108 void dispatchComposite(); |
111 void dispatchDidAddAnimation(); | 109 void dispatchDidAddAnimation(); |
112 | 110 |
113 virtual void runTest(bool threaded); | 111 virtual void runTest(bool threaded); |
114 | 112 |
115 cc::Thread* implThread() { return proxy() ? proxy()->implThread() : 0; } | 113 Thread* implThread() { return proxy() ? proxy()->implThread() : 0; } |
116 cc::Proxy* proxy() const { return m_layerTreeHost ? m_layerTreeHost->proxy()
: 0; } | 114 Proxy* proxy() const { return m_layerTreeHost ? m_layerTreeHost->proxy() : 0
; } |
117 | 115 |
118 cc::LayerTreeSettings m_settings; | 116 LayerTreeSettings m_settings; |
119 scoped_ptr<MockLayerImplTreeHostClient> m_client; | 117 scoped_ptr<MockLayerImplTreeHostClient> m_client; |
120 scoped_ptr<cc::LayerTreeHost> m_layerTreeHost; | 118 scoped_ptr<LayerTreeHost> m_layerTreeHost; |
121 | 119 |
122 protected: | 120 protected: |
123 scoped_refptr<cc::ScopedThreadProxy> m_mainThreadProxy; | 121 scoped_refptr<ScopedThreadProxy> m_mainThreadProxy; |
124 | 122 |
125 private: | 123 private: |
126 bool m_beginning; | 124 bool m_beginning; |
127 bool m_endWhenBeginReturns; | 125 bool m_endWhenBeginReturns; |
128 bool m_timedOut; | 126 bool m_timedOut; |
129 bool m_finished; | 127 bool m_finished; |
130 bool m_scheduled; | 128 bool m_scheduled; |
131 bool m_started; | 129 bool m_started; |
132 | 130 |
133 scoped_ptr<cc::Thread> m_mainCCThread; | 131 scoped_ptr<Thread> m_mainCCThread; |
134 scoped_ptr<base::Thread> m_implThread; | 132 scoped_ptr<base::Thread> m_implThread; |
135 base::CancelableClosure m_timeout; | 133 base::CancelableClosure m_timeout; |
136 }; | 134 }; |
137 | 135 |
138 class ThreadedTestThreadOnly : public ThreadedTest { | 136 class ThreadedTestThreadOnly : public ThreadedTest { |
139 public: | 137 public: |
140 void runTestThreaded() | 138 void runTestThreaded() |
141 { | 139 { |
142 ThreadedTest::runTest(true); | 140 ThreadedTest::runTest(true); |
143 } | 141 } |
144 }; | 142 }; |
145 | 143 |
146 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks. | 144 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks. |
147 class MockLayerTreeHostImpl : public cc::LayerTreeHostImpl { | 145 class MockLayerTreeHostImpl : public LayerTreeHostImpl { |
148 public: | 146 public: |
149 static scoped_ptr<MockLayerTreeHostImpl> create(TestHooks*, const cc::LayerT
reeSettings&, cc::LayerTreeHostImplClient*, cc::Proxy*); | 147 static scoped_ptr<MockLayerTreeHostImpl> create(TestHooks*, const LayerTreeS
ettings&, LayerTreeHostImplClient*, Proxy*); |
150 | 148 |
151 virtual void beginCommit() OVERRIDE; | 149 virtual void beginCommit() OVERRIDE; |
152 virtual void commitComplete() OVERRIDE; | 150 virtual void commitComplete() OVERRIDE; |
153 virtual bool prepareToDraw(FrameData&) OVERRIDE; | 151 virtual bool prepareToDraw(FrameData&) OVERRIDE; |
154 virtual void drawLayers(FrameData&) OVERRIDE; | 152 virtual void drawLayers(FrameData&) OVERRIDE; |
155 | 153 |
156 // Make these public. | 154 // Make these public. |
157 typedef std::vector<cc::LayerImpl*> LayerList; | 155 typedef std::vector<LayerImpl*> LayerList; |
158 using LayerTreeHostImpl::calculateRenderSurfaceLayerList; | 156 using LayerTreeHostImpl::calculateRenderSurfaceLayerList; |
159 | 157 |
160 protected: | 158 protected: |
161 virtual void animateLayers(base::TimeTicks monotonicTime, base::Time wallClo
ckTime) OVERRIDE; | 159 virtual void animateLayers(base::TimeTicks monotonicTime, base::Time wallClo
ckTime) OVERRIDE; |
162 virtual base::TimeDelta lowFrequencyAnimationInterval() const OVERRIDE; | 160 virtual base::TimeDelta lowFrequencyAnimationInterval() const OVERRIDE; |
163 | 161 |
164 private: | 162 private: |
165 MockLayerTreeHostImpl(TestHooks*, const cc::LayerTreeSettings&, cc::LayerTre
eHostImplClient*, cc::Proxy*); | 163 MockLayerTreeHostImpl(TestHooks*, const LayerTreeSettings&, LayerTreeHostImp
lClient*, Proxy*); |
166 | 164 |
167 TestHooks* m_testHooks; | 165 TestHooks* m_testHooks; |
168 }; | 166 }; |
169 | 167 |
170 class CompositorFakeWebGraphicsContext3DWithTextureTracking : public WebKit::Com
positorFakeWebGraphicsContext3D { | 168 class CompositorFakeWebGraphicsContext3DWithTextureTracking : public CompositorF
akeWebGraphicsContext3D { |
171 public: | 169 public: |
172 static scoped_ptr<CompositorFakeWebGraphicsContext3DWithTextureTracking> cre
ate(Attributes); | 170 static scoped_ptr<CompositorFakeWebGraphicsContext3DWithTextureTracking> cre
ate(Attributes); |
173 virtual ~CompositorFakeWebGraphicsContext3DWithTextureTracking(); | 171 virtual ~CompositorFakeWebGraphicsContext3DWithTextureTracking(); |
174 | 172 |
175 virtual WebKit::WebGLId createTexture(); | 173 virtual WebKit::WebGLId createTexture(); |
176 | 174 |
177 virtual void deleteTexture(WebKit::WebGLId texture); | 175 virtual void deleteTexture(WebKit::WebGLId texture); |
178 | 176 |
179 virtual void bindTexture(WebKit::WGC3Denum target, WebKit::WebGLId texture); | 177 virtual void bindTexture(WebKit::WGC3Denum target, WebKit::WebGLId texture); |
180 | 178 |
181 int numTextures() const; | 179 int numTextures() const; |
182 int texture(int texture) const; | 180 int texture(int texture) const; |
183 void resetTextures(); | 181 void resetTextures(); |
184 | 182 |
185 int numUsedTextures() const; | 183 int numUsedTextures() const; |
186 bool usedTexture(int texture) const; | 184 bool usedTexture(int texture) const; |
187 void resetUsedTextures(); | 185 void resetUsedTextures(); |
188 | 186 |
189 private: | 187 private: |
190 explicit CompositorFakeWebGraphicsContext3DWithTextureTracking(Attributes at
trs); | 188 explicit CompositorFakeWebGraphicsContext3DWithTextureTracking(Attributes at
trs); |
191 | 189 |
192 std::vector<WebKit::WebGLId> m_textures; | 190 std::vector<WebKit::WebGLId> m_textures; |
193 base::hash_set<WebKit::WebGLId> m_usedTextures; | 191 base::hash_set<WebKit::WebGLId> m_usedTextures; |
194 }; | 192 }; |
195 | 193 |
196 } // namespace WebKitTests | 194 } // namespace cc |
197 | 195 |
198 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \ | 196 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \ |
199 TEST_F(TEST_FIXTURE_NAME, runSingleThread) \ | 197 TEST_F(TEST_FIXTURE_NAME, runSingleThread) \ |
200 { \ | 198 { \ |
201 runTest(false); \ | 199 runTest(false); \ |
202 } \ | 200 } \ |
203 TEST_F(TEST_FIXTURE_NAME, runMultiThread) \ | 201 TEST_F(TEST_FIXTURE_NAME, runMultiThread) \ |
204 { \ | 202 { \ |
205 runTest(true); \ | 203 runTest(true); \ |
206 } | 204 } |
207 | 205 |
208 #endif // CC_TEST_LAYER_TREE_TEST_COMMON_H_ | 206 #endif // CC_TEST_LAYER_TREE_TEST_COMMON_H_ |
OLD | NEW |