blob: d1638a3770898d191d1196c9da892d92b2c76d98 [file] [log] [blame]
[email protected]c79f1472012-10-12 19:40:561// 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.
4
5#ifndef CCThreadedTest_h
6#define CCThreadedTest_h
7
8#include "CCLayerTreeHost.h"
9#include "CCLayerTreeHostImpl.h"
[email protected]1c0088f2012-10-17 00:29:3010#include "CCScopedThreadProxy.h"
[email protected]c79f1472012-10-12 19:40:5611#include "base/hash_tables.h"
[email protected]101441ce2012-10-16 01:45:0312#include "cc/test/compositor_fake_web_graphics_context_3d.h"
[email protected]c79f1472012-10-12 19:40:5613#include "testing/gtest/include/gtest/gtest.h"
14#include <public/WebAnimationDelegate.h>
15#include <public/WebThread.h>
16
17namespace cc {
18class CCLayerImpl;
19class CCLayerTreeHost;
20class CCLayerTreeHostClient;
21class CCLayerTreeHostImpl;
22class GraphicsContext3D;
23}
24
25namespace WebKitTests {
26
27// Used by test stubs to notify the test when something interesting happens.
28class TestHooks : public WebKit::WebAnimationDelegate {
29public:
30 virtual void beginCommitOnCCThread(cc::CCLayerTreeHostImpl*) { }
31 virtual void commitCompleteOnCCThread(cc::CCLayerTreeHostImpl*) { }
32 virtual bool prepareToDrawOnCCThread(cc::CCLayerTreeHostImpl*);
33 virtual void drawLayersOnCCThread(cc::CCLayerTreeHostImpl*) { }
34 virtual void animateLayers(cc::CCLayerTreeHostImpl*, double monotonicTime) { }
35 virtual void willAnimateLayers(cc::CCLayerTreeHostImpl*, double monotonicTime) { }
36 virtual void applyScrollAndScale(const cc::IntSize&, float) { }
37 virtual void animate(double monotonicTime) { }
38 virtual void layout() { }
39 virtual void didRecreateOutputSurface(bool succeeded) { }
40 virtual void didAddAnimation() { }
41 virtual void didCommit() { }
42 virtual void didCommitAndDrawFrame() { }
43 virtual void scheduleComposite() { }
44
45 // Implementation of WebAnimationDelegate
46 virtual void notifyAnimationStarted(double time) OVERRIDE { }
47 virtual void notifyAnimationFinished(double time) OVERRIDE { }
48
49 virtual scoped_ptr<WebKit::WebCompositorOutputSurface> createOutputSurface();
50};
51
52class TimeoutTask;
53class BeginTask;
54
55class MockCCLayerTreeHostClient : public cc::CCLayerTreeHostClient {
56};
57
58// The CCThreadedTests runs with the main loop running. It instantiates a single MockLayerTreeHost and associated
59// MockLayerTreeHostImpl/MockLayerTreeHostClient.
60//
61// beginTest() is called once the main message loop is running and the layer tree host is initialized.
62//
63// Key stages of the drawing loop, e.g. drawing or commiting, redirect to CCThreadedTest methods of similar names.
64// To track the commit process, override these functions.
65//
66// The test continues until someone calls endTest. endTest can be called on any thread, but be aware that
67// ending the test is an asynchronous process.
68class CCThreadedTest : public testing::Test, public TestHooks {
69public:
70 virtual ~CCThreadedTest();
71
72 virtual void afterTest() = 0;
73 virtual void beginTest() = 0;
74
75 void endTest();
76 void endTestAfterDelay(int delayMilliseconds);
77
78 void postSetNeedsAnimateToMainThread();
79 void postAddAnimationToMainThread();
80 void postAddInstantAnimationToMainThread();
81 void postSetNeedsCommitToMainThread();
82 void postAcquireLayerTextures();
83 void postSetNeedsRedrawToMainThread();
84 void postSetNeedsAnimateAndCommitToMainThread();
85 void postSetVisibleToMainThread(bool visible);
86 void postDidAddAnimationToMainThread();
87
88 void doBeginTest();
89 void timeout();
90
91 void clearTimeout() { m_timeoutTask = 0; }
92
93 cc::CCLayerTreeHost* layerTreeHost() { return m_layerTreeHost.get(); }
94
95protected:
96 CCThreadedTest();
97
98 virtual void initializeSettings(cc::CCLayerTreeSettings&) { }
99
100 virtual void scheduleComposite() OVERRIDE;
101
102 void realEndTest();
103
104 void dispatchSetNeedsAnimate();
105 void dispatchAddInstantAnimation();
106 void dispatchAddAnimation();
107 void dispatchSetNeedsAnimateAndCommit();
108 void dispatchSetNeedsCommit();
109 void dispatchAcquireLayerTextures();
110 void dispatchSetNeedsRedraw();
111 void dispatchSetVisible(bool);
112 void dispatchComposite();
113 void dispatchDidAddAnimation();
114
115 virtual void runTest(bool threaded);
116 WebKit::WebThread* webThread() const { return m_webThread.get(); }
117
118 cc::CCLayerTreeSettings m_settings;
119 OwnPtr<MockCCLayerTreeHostClient> m_client;
120 scoped_ptr<cc::CCLayerTreeHost> m_layerTreeHost;
121
122protected:
123 RefPtr<cc::CCScopedThreadProxy> m_mainThreadProxy;
124
125private:
126 bool m_beginning;
127 bool m_endWhenBeginReturns;
128 bool m_timedOut;
129 bool m_finished;
130 bool m_scheduled;
131 bool m_started;
132
133 OwnPtr<WebKit::WebThread> m_webThread;
134 TimeoutTask* m_timeoutTask;
135 BeginTask* m_beginTask;
136};
137
138class CCThreadedTestThreadOnly : public CCThreadedTest {
139public:
140 void runTestThreaded()
141 {
142 CCThreadedTest::runTest(true);
143 }
144};
145
146// Adapts CCLayerTreeHostImpl for test. Runs real code, then invokes test hooks.
147class MockLayerTreeHostImpl : public cc::CCLayerTreeHostImpl {
148public:
149 static scoped_ptr<MockLayerTreeHostImpl> create(TestHooks*, const cc::CCLayerTreeSettings&, cc::CCLayerTreeHostImplClient*);
150
151 virtual void beginCommit() OVERRIDE;
152 virtual void commitComplete() OVERRIDE;
153 virtual bool prepareToDraw(FrameData&) OVERRIDE;
154 virtual void drawLayers(const FrameData&) OVERRIDE;
155
156 // Make these public.
157 typedef std::vector<cc::CCLayerImpl*> CCLayerList;
158 using CCLayerTreeHostImpl::calculateRenderSurfaceLayerList;
159
160protected:
161 virtual void animateLayers(double monotonicTime, double wallClockTime) OVERRIDE;
162 virtual base::TimeDelta lowFrequencyAnimationInterval() const OVERRIDE;
163
164private:
165 MockLayerTreeHostImpl(TestHooks*, const cc::CCLayerTreeSettings&, cc::CCLayerTreeHostImplClient*);
166
167 TestHooks* m_testHooks;
168};
169
170class CompositorFakeWebGraphicsContext3DWithTextureTracking : public WebKit::CompositorFakeWebGraphicsContext3D {
171public:
172 static PassOwnPtr<CompositorFakeWebGraphicsContext3DWithTextureTracking> create(Attributes);
173 virtual ~CompositorFakeWebGraphicsContext3DWithTextureTracking();
174
175 virtual WebKit::WebGLId createTexture();
176
177 virtual void deleteTexture(WebKit::WebGLId texture);
178
179 virtual void bindTexture(WebKit::WGC3Denum target, WebKit::WebGLId texture);
180
181 int numTextures() const;
182 int texture(int texture) const;
183 void resetTextures();
184
185 int numUsedTextures() const;
186 bool usedTexture(int texture) const;
187 void resetUsedTextures();
188
189private:
190 explicit CompositorFakeWebGraphicsContext3DWithTextureTracking(Attributes attrs);
191
192 Vector<WebKit::WebGLId> m_textures;
193 base::hash_set<WebKit::WebGLId> m_usedTextures;
194};
195
196} // namespace WebKitTests
197
198#define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
199 TEST_F(TEST_FIXTURE_NAME, runSingleThread) \
200 { \
201 runTest(false); \
202 } \
203 TEST_F(TEST_FIXTURE_NAME, runMultiThread) \
204 { \
205 runTest(true); \
206 }
207
208#endif // CCThreadedTest_h