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
208
|
// 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 CC_TEST_LAYER_TREE_TEST_COMMON_H_
#define CC_TEST_LAYER_TREE_TEST_COMMON_H_
#include "base/memory/ref_counted.h"
#include "base/threading/thread.h"
#include "cc/base/thread.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebAnimationDelegate.h"
namespace Webkit {
class WebGraphicsContext3D;
}
namespace cc {
class FakeLayerImplTreeHostClient;
class LayerImpl;
class LayerTreeHost;
class LayerTreeHostClient;
class LayerTreeHostImpl;
// Used by test stubs to notify the test when something interesting happens.
class TestHooks : public WebKit::WebAnimationDelegate {
public:
TestHooks();
virtual ~TestHooks();
virtual void beginCommitOnThread(LayerTreeHostImpl*) { }
virtual void commitCompleteOnThread(LayerTreeHostImpl*) { }
virtual void treeActivatedOnThread(LayerTreeHostImpl*) { }
virtual void initializedRendererOnThread(LayerTreeHostImpl*, bool success) { }
virtual bool prepareToDrawOnThread(
LayerTreeHostImpl*, LayerTreeHostImpl::FrameData*, bool result);
virtual void drawLayersOnThread(LayerTreeHostImpl*) { }
virtual void swapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) { }
virtual void animateLayers(LayerTreeHostImpl*, base::TimeTicks monotonicTime) { }
virtual void updateAnimationState(LayerTreeHostImpl*, bool hasUnfinishedAnimation) { }
virtual void willAnimateLayers(LayerTreeHostImpl*, base::TimeTicks monotonicTime) { }
virtual void applyScrollAndScale(gfx::Vector2d, float) { }
virtual void animate(base::TimeTicks monotonicTime) { }
virtual void layout() { }
virtual void didRecreateOutputSurface(bool succeeded) { }
virtual void willRetryRecreateOutputSurface() { }
virtual void didAddAnimation() { }
virtual void didCommit() { }
virtual void didCommitAndDrawFrame() { }
virtual void scheduleComposite() { }
virtual void didDeferCommit() { }
virtual bool canActivatePendingTree();
virtual void didSetVisibleOnImplTree(LayerTreeHostImpl*, bool visible) { }
// Implementation of WebAnimationDelegate
virtual void notifyAnimationStarted(double time) OVERRIDE { }
virtual void notifyAnimationFinished(double time) OVERRIDE { }
virtual scoped_ptr<OutputSurface> createOutputSurface();
virtual scoped_refptr<cc::ContextProvider> OffscreenContextProviderForMainThread();
virtual scoped_refptr<cc::ContextProvider> OffscreenContextProviderForCompositorThread();
private:
scoped_ptr<FakeLayerImplTreeHostClient> m_fakeClient;
};
class TimeoutTask;
class BeginTask;
class MockLayerImplTreeHostClient : public LayerTreeHostClient {
};
// The ThreadedTests runs with the main loop running. It instantiates a single MockLayerTreeHost and associated
// MockLayerTreeHostImpl/ThreadedMockLayerTreeHostClient.
//
// 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 ThreadedTest 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 ThreadedTest : public testing::Test, public TestHooks {
public:
virtual ~ThreadedTest();
virtual void afterTest() = 0;
virtual void beginTest() = 0;
virtual void setupTree();
void endTest();
void endTestAfterDelay(int delayMilliseconds);
void postAddAnimationToMainThread(Layer*);
void postAddInstantAnimationToMainThread();
void postSetNeedsCommitToMainThread();
void postAcquireLayerTextures();
void postSetNeedsRedrawToMainThread();
void postSetVisibleToMainThread(bool visible);
void doBeginTest();
void timeout();
LayerTreeHost* layerTreeHost() { return m_layerTreeHost.get(); }
protected:
ThreadedTest();
virtual void initializeSettings(LayerTreeSettings&) { }
virtual void scheduleComposite() OVERRIDE;
void realEndTest();
void dispatchAddInstantAnimation();
void dispatchAddAnimation(Layer*);
void dispatchSetNeedsCommit();
void dispatchAcquireLayerTextures();
void dispatchSetNeedsRedraw();
void dispatchSetVisible(bool);
void dispatchComposite();
void dispatchDidAddAnimation();
virtual void runTest(bool threaded);
Thread* ImplThread() { return proxy() ? proxy()->ImplThread() : 0; }
Proxy* proxy() const { return m_layerTreeHost ? m_layerTreeHost->proxy() : 0; }
LayerTreeSettings m_settings;
scoped_ptr<MockLayerImplTreeHostClient> m_client;
scoped_ptr<LayerTreeHost> m_layerTreeHost;
bool testEnded() const { return m_ended; }
private:
bool m_beginning;
bool m_endWhenBeginReturns;
bool m_timedOut;
bool m_scheduled;
bool m_scheduleWhenSetVisibleTrue;
bool m_started;
bool m_ended;
scoped_ptr<Thread> m_mainCCThread;
scoped_ptr<base::Thread> m_implThread;
base::CancelableClosure m_timeout;
base::WeakPtr<ThreadedTest> m_mainThreadWeakPtr;
base::WeakPtrFactory<ThreadedTest> m_weakFactory;
};
class ThreadedTestThreadOnly : public ThreadedTest {
public:
void runTestThreaded()
{
ThreadedTest::runTest(true);
}
};
// Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
class MockLayerTreeHostImpl : public LayerTreeHostImpl {
public:
static scoped_ptr<MockLayerTreeHostImpl> create(TestHooks*, const LayerTreeSettings&, LayerTreeHostImplClient*, Proxy*);
virtual void BeginCommit() OVERRIDE;
virtual void CommitComplete() OVERRIDE;
virtual bool PrepareToDraw(FrameData* frame) OVERRIDE;
virtual void DrawLayers(FrameData* frame, base::TimeTicks frameBeginTime) OVERRIDE;
virtual bool SwapBuffers() OVERRIDE;
virtual bool ActivatePendingTreeIfNeeded() OVERRIDE;
virtual bool InitializeRenderer(scoped_ptr<OutputSurface> outputSurface) OVERRIDE;
virtual void SetVisible(bool visible) OVERRIDE;
// Make these public.
typedef std::vector<LayerImpl*> LayerList;
protected:
virtual void AnimateLayers(base::TimeTicks monotonicTime, base::Time wallClockTime) OVERRIDE;
virtual void UpdateAnimationState() OVERRIDE;
virtual base::TimeDelta LowFrequencyAnimationInterval() const OVERRIDE;
private:
MockLayerTreeHostImpl(TestHooks*, const LayerTreeSettings&, LayerTreeHostImplClient*, Proxy*);
TestHooks* m_testHooks;
};
} // namespace cc
#define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
TEST_F(TEST_FIXTURE_NAME, runSingleThread) \
{ \
runTest(false); \
}
#define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
TEST_F(TEST_FIXTURE_NAME, runMultiThread) \
{ \
runTest(true); \
}
#define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
#endif // CC_TEST_LAYER_TREE_TEST_COMMON_H_
|