summaryrefslogtreecommitdiffstats
path: root/cc/CCThreadedTest.h
blob: 61a08b505aade8983e0fafec600b4f5031e5979b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CCThreadedTest_h
#define CCThreadedTest_h

#include "CCLayerTreeHost.h"
#include "CCLayerTreeHostImpl.h"
#include "CCScopedThreadProxy.h"
#include "CompositorFakeWebGraphicsContext3D.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <public/WebAnimationDelegate.h>
#include <public/WebThread.h>

namespace cc {
class CCLayerImpl;
class CCLayerTreeHost;
class CCLayerTreeHostClient;
class CCLayerTreeHostImpl;
class GraphicsContext3D;
}

namespace WebKitTests {

// Used by test stubs to notify the test when something interesting happens.
class TestHooks : public WebKit::WebAnimationDelegate {
public:
    virtual void beginCommitOnCCThread(cc::CCLayerTreeHostImpl*) { }
    virtual void commitCompleteOnCCThread(cc::CCLayerTreeHostImpl*) { }
    virtual bool prepareToDrawOnCCThread(cc::CCLayerTreeHostImpl*);
    virtual void drawLayersOnCCThread(cc::CCLayerTreeHostImpl*) { }
    virtual void animateLayers(cc::CCLayerTreeHostImpl*, double monotonicTime) { }
    virtual void willAnimateLayers(cc::CCLayerTreeHostImpl*, double monotonicTime) { }
    virtual void applyScrollAndScale(const cc::IntSize&, float) { }
    virtual void animate(double monotonicTime) { }
    virtual void layout() { }
    virtual void didRecreateOutputSurface(bool succeeded) { }
    virtual void didAddAnimation() { }
    virtual void didCommit() { }
    virtual void didCommitAndDrawFrame() { }
    virtual void scheduleComposite() { }

    // Implementation of WebAnimationDelegate
    virtual void notifyAnimationStarted(double time) OVERRIDE { }
    virtual void notifyAnimationFinished(double time) OVERRIDE { }

    virtual PassOwnPtr<WebKit::WebCompositorOutputSurface> createOutputSurface();
};

class TimeoutTask;
class BeginTask;

class MockCCLayerTreeHostClient : public cc::CCLayerTreeHostClient {
};

// The CCThreadedTests runs with the main loop running. It instantiates a single MockLayerTreeHost and associated
// MockLayerTreeHostImpl/MockLayerTreeHostClient.
//
// beginTest() is called once the main message loop is running and the layer tree host is initialized.
//
// Key stages of the drawing loop, e.g. drawing or commiting, redirect to CCThreadedTest methods of similar names.
// To track the commit process, override these functions.
//
// The test continues until someone calls endTest. endTest can be called on any thread, but be aware that
// ending the test is an asynchronous process.
class CCThreadedTest : public testing::Test, public TestHooks {
public:
    virtual ~CCThreadedTest();

    virtual void afterTest() = 0;
    virtual void beginTest() = 0;

    void endTest();
    void endTestAfterDelay(int delayMilliseconds);

    void postSetNeedsAnimateToMainThread();
    void postAddAnimationToMainThread();
    void postAddInstantAnimationToMainThread();
    void postSetNeedsCommitToMainThread();
    void postAcquireLayerTextures();
    void postSetNeedsRedrawToMainThread();
    void postSetNeedsAnimateAndCommitToMainThread();
    void postSetVisibleToMainThread(bool visible);
    void postDidAddAnimationToMainThread();

    void doBeginTest();
    void timeout();

    void clearTimeout() { m_timeoutTask = 0; }

    cc::CCLayerTreeHost* layerTreeHost() { return m_layerTreeHost.get(); }

protected:
    CCThreadedTest();

    virtual void initializeSettings(cc::CCLayerTreeSettings&) { }

    virtual void scheduleComposite() OVERRIDE;

    void realEndTest();

    void dispatchSetNeedsAnimate();
    void dispatchAddInstantAnimation();
    void dispatchAddAnimation();
    void dispatchSetNeedsAnimateAndCommit();
    void dispatchSetNeedsCommit();
    void dispatchAcquireLayerTextures();
    void dispatchSetNeedsRedraw();
    void dispatchSetVisible(bool);
    void dispatchComposite();
    void dispatchDidAddAnimation();

    virtual void runTest(bool threaded);
    WebKit::WebThread* webThread() const { return m_webThread.get(); }

    cc::CCLayerTreeSettings m_settings;
    OwnPtr<MockCCLayerTreeHostClient> m_client;
    OwnPtr<cc::CCLayerTreeHost> m_layerTreeHost;

protected:
    RefPtr<cc::CCScopedThreadProxy> m_mainThreadProxy;

private:
    bool m_beginning;
    bool m_endWhenBeginReturns;
    bool m_timedOut;
    bool m_finished;
    bool m_scheduled;
    bool m_started;

    OwnPtr<WebKit::WebThread> m_webThread;
    TimeoutTask* m_timeoutTask;
    BeginTask* m_beginTask;
};

class CCThreadedTestThreadOnly : public CCThreadedTest {
public:
    void runTestThreaded()
    {
        CCThreadedTest::runTest(true);
    }
};

// Adapts CCLayerTreeHostImpl for test. Runs real code, then invokes test hooks.
class MockLayerTreeHostImpl : public cc::CCLayerTreeHostImpl {
public:
    static PassOwnPtr<MockLayerTreeHostImpl> create(TestHooks*, const cc::CCLayerTreeSettings&, cc::CCLayerTreeHostImplClient*);

    virtual void beginCommit() OVERRIDE;
    virtual void commitComplete() OVERRIDE;
    virtual bool prepareToDraw(FrameData&) OVERRIDE;
    virtual void drawLayers(const FrameData&) OVERRIDE;

    // Make these public.
    typedef Vector<cc::CCLayerImpl*> CCLayerList;
    using CCLayerTreeHostImpl::calculateRenderSurfaceLayerList;

protected:
    virtual void animateLayers(double monotonicTime, double wallClockTime) OVERRIDE;
    virtual base::TimeDelta lowFrequencyAnimationInterval() const OVERRIDE;

private:
    MockLayerTreeHostImpl(TestHooks*, const cc::CCLayerTreeSettings&, cc::CCLayerTreeHostImplClient*);

    TestHooks* m_testHooks;
};

class CompositorFakeWebGraphicsContext3DWithTextureTracking : public WebKit::CompositorFakeWebGraphicsContext3D {
public:
    static PassOwnPtr<CompositorFakeWebGraphicsContext3DWithTextureTracking> create(Attributes);
    virtual ~CompositorFakeWebGraphicsContext3DWithTextureTracking();

    virtual WebKit::WebGLId createTexture();

    virtual void deleteTexture(WebKit::WebGLId texture);

    virtual void bindTexture(WebKit::WGC3Denum target, WebKit::WebGLId texture);

    int numTextures() const;
    int texture(int texture) const;
    void resetTextures();

    int numUsedTextures() const;
    bool usedTexture(int texture) const;
    void resetUsedTextures();

private:
    explicit CompositorFakeWebGraphicsContext3DWithTextureTracking(Attributes attrs);

    Vector<WebKit::WebGLId> m_textures;
    HashSet<WebKit::WebGLId, DefaultHash<WebKit::WebGLId>::Hash, WTF::UnsignedWithZeroKeyHashTraits<WebKit::WebGLId> > m_usedTextures;
};

} // namespace WebKitTests

#define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
    TEST_F(TEST_FIXTURE_NAME, runSingleThread)            \
    {                                                     \
        runTest(false);                                   \
    }                                                     \
    TEST_F(TEST_FIXTURE_NAME, runMultiThread)             \
    {                                                     \
        runTest(true);                                    \
    }

#endif // CCThreadedTest_h