Don't consider a Bluetooth adapter present until it has an address.
[chromium-blink-merge.git] / cc / CCThreadedTest.h
blob55840ed99d257f1c6908fda054f81f4d9fe47c88
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 CCThreadedTest_h
6 #define CCThreadedTest_h
8 #include "CCLayerTreeHost.h"
9 #include "CCLayerTreeHostImpl.h"
10 #include "CCScopedThreadProxy.h"
11 #include "CompositorFakeWebGraphicsContext3D.h"
12 #include <gtest/gtest.h>
13 #include <public/WebAnimationDelegate.h>
14 #include <public/WebThread.h>
16 namespace WebCore {
17 class CCLayerImpl;
18 class CCLayerTreeHost;
19 class CCLayerTreeHostClient;
20 class CCLayerTreeHostImpl;
21 class GraphicsContext3D;
24 namespace WebKitTests {
26 // Used by test stubs to notify the test when something interesting happens.
27 class TestHooks : public WebKit::WebAnimationDelegate {
28 public:
29 virtual void beginCommitOnCCThread(WebCore::CCLayerTreeHostImpl*) { }
30 virtual void commitCompleteOnCCThread(WebCore::CCLayerTreeHostImpl*) { }
31 virtual bool prepareToDrawOnCCThread(WebCore::CCLayerTreeHostImpl*) { return true; }
32 virtual void drawLayersOnCCThread(WebCore::CCLayerTreeHostImpl*) { }
33 virtual void animateLayers(WebCore::CCLayerTreeHostImpl*, double monotonicTime) { }
34 virtual void willAnimateLayers(WebCore::CCLayerTreeHostImpl*, double monotonicTime) { }
35 virtual void applyScrollAndScale(const WebCore::IntSize&, float) { }
36 virtual void animate(double monotonicTime) { }
37 virtual void layout() { }
38 virtual void didRecreateOutputSurface(bool succeeded) { }
39 virtual void didAddAnimation() { }
40 virtual void didCommit() { }
41 virtual void didCommitAndDrawFrame() { }
42 virtual void scheduleComposite() { }
44 // Implementation of WebAnimationDelegate
45 virtual void notifyAnimationStarted(double time) OVERRIDE { }
46 virtual void notifyAnimationFinished(double time) OVERRIDE { }
48 virtual PassOwnPtr<WebKit::WebCompositorOutputSurface> createOutputSurface();
51 class TimeoutTask;
52 class BeginTask;
53 class EndTestTask;
55 class MockCCLayerTreeHostClient : public WebCore::CCLayerTreeHostClient {
58 // The CCThreadedTests runs with the main loop running. It instantiates a single MockLayerTreeHost and associated
59 // MockLayerTreeHostImpl/MockLayerTreeHostClient.
61 // beginTest() is called once the main message loop is running and the layer tree host is initialized.
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.
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.
68 class CCThreadedTest : public testing::Test, public TestHooks {
69 public:
70 virtual void afterTest() = 0;
71 virtual void beginTest() = 0;
73 void endTest();
74 void endTestAfterDelay(int delayMilliseconds);
76 void postSetNeedsAnimateToMainThread();
77 void postAddAnimationToMainThread();
78 void postAddInstantAnimationToMainThread();
79 void postSetNeedsCommitToMainThread();
80 void postAcquireLayerTextures();
81 void postSetNeedsRedrawToMainThread();
82 void postSetNeedsAnimateAndCommitToMainThread();
83 void postSetVisibleToMainThread(bool visible);
84 void postDidAddAnimationToMainThread();
86 void doBeginTest();
87 void timeout();
89 void clearTimeout() { m_timeoutTask = 0; }
90 void clearEndTestTask() { m_endTestTask = 0; }
92 WebCore::CCLayerTreeHost* layerTreeHost() { return m_layerTreeHost.get(); }
94 protected:
95 CCThreadedTest();
97 virtual void initializeSettings(WebCore::CCLayerTreeSettings&) { }
99 virtual void scheduleComposite();
101 static void onEndTest(void* self);
103 static void dispatchSetNeedsAnimate(void* self);
104 static void dispatchAddInstantAnimation(void* self);
105 static void dispatchAddAnimation(void* self);
106 static void dispatchSetNeedsAnimateAndCommit(void* self);
107 static void dispatchSetNeedsCommit(void* self);
108 static void dispatchAcquireLayerTextures(void* self);
109 static void dispatchSetNeedsRedraw(void* self);
110 static void dispatchSetVisible(void* self);
111 static void dispatchSetInvisible(void* self);
112 static void dispatchComposite(void* self);
113 static void dispatchDidAddAnimation(void* self);
115 virtual void runTest(bool threaded);
116 WebKit::WebThread* webThread() const { return m_webThread.get(); }
118 WebCore::CCLayerTreeSettings m_settings;
119 OwnPtr<MockCCLayerTreeHostClient> m_client;
120 OwnPtr<WebCore::CCLayerTreeHost> m_layerTreeHost;
122 private:
123 bool m_beginning;
124 bool m_endWhenBeginReturns;
125 bool m_timedOut;
126 bool m_finished;
127 bool m_scheduled;
128 bool m_started;
130 OwnPtr<WebKit::WebThread> m_webThread;
131 RefPtr<WebCore::CCScopedThreadProxy> m_mainThreadProxy;
132 TimeoutTask* m_timeoutTask;
133 BeginTask* m_beginTask;
134 EndTestTask* m_endTestTask;
137 class CCThreadedTestThreadOnly : public CCThreadedTest {
138 public:
139 void runTestThreaded()
141 CCThreadedTest::runTest(true);
145 // Adapts CCLayerTreeHostImpl for test. Runs real code, then invokes test hooks.
146 class MockLayerTreeHostImpl : public WebCore::CCLayerTreeHostImpl {
147 public:
148 static PassOwnPtr<MockLayerTreeHostImpl> create(TestHooks*, const WebCore::CCLayerTreeSettings&, WebCore::CCLayerTreeHostImplClient*);
150 virtual void beginCommit();
151 virtual void commitComplete();
152 virtual bool prepareToDraw(FrameData&);
153 virtual void drawLayers(const FrameData&);
155 // Make these public.
156 typedef Vector<WebCore::CCLayerImpl*> CCLayerList;
157 using CCLayerTreeHostImpl::calculateRenderSurfaceLayerList;
159 protected:
160 virtual void animateLayers(double monotonicTime, double wallClockTime);
161 virtual double lowFrequencyAnimationInterval() const;
163 private:
164 MockLayerTreeHostImpl(TestHooks*, const WebCore::CCLayerTreeSettings&, WebCore::CCLayerTreeHostImplClient*);
166 TestHooks* m_testHooks;
169 class CompositorFakeWebGraphicsContext3DWithTextureTracking : public WebKit::CompositorFakeWebGraphicsContext3D {
170 public:
171 static PassOwnPtr<CompositorFakeWebGraphicsContext3DWithTextureTracking> create(Attributes);
173 virtual WebKit::WebGLId createTexture();
175 virtual void deleteTexture(WebKit::WebGLId texture);
177 virtual void bindTexture(WebKit::WGC3Denum target, WebKit::WebGLId texture);
179 int numTextures() const;
180 int texture(int texture) const;
181 void resetTextures();
183 int numUsedTextures() const;
184 bool usedTexture(int texture) const;
185 void resetUsedTextures();
187 private:
188 explicit CompositorFakeWebGraphicsContext3DWithTextureTracking(Attributes attrs);
190 Vector<WebKit::WebGLId> m_textures;
191 HashSet<WebKit::WebGLId, DefaultHash<WebKit::WebGLId>::Hash, WTF::UnsignedWithZeroKeyHashTraits<WebKit::WebGLId> > m_usedTextures;
194 } // namespace WebKitTests
196 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
197 TEST_F(TEST_FIXTURE_NAME, runSingleThread) \
199 runTest(false); \
201 TEST_F(TEST_FIXTURE_NAME, runMultiThread) \
203 runTest(true); \
206 #endif // CCThreadedTest_h