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 CC_TEST_LAYER_TREE_TEST_COMMON_H_
6 #define CC_TEST_LAYER_TREE_TEST_COMMON_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/threading/thread.h"
10 #include "cc/layer_tree_host.h"
11 #include "cc/layer_tree_host_impl.h"
12 #include "cc/thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebAnimationDelegate.h"
19 class LayerTreeHostClient
;
20 class LayerTreeHostImpl
;
22 // Used by test stubs to notify the test when something interesting happens.
23 class TestHooks
: public WebKit::WebAnimationDelegate
{
25 virtual void beginCommitOnThread(LayerTreeHostImpl
*) { }
26 virtual void commitCompleteOnThread(LayerTreeHostImpl
*) { }
27 virtual bool prepareToDrawOnThread(
28 LayerTreeHostImpl
*, LayerTreeHostImpl::FrameData
&, bool result
);
29 virtual void drawLayersOnThread(LayerTreeHostImpl
*) { }
30 virtual void animateLayers(LayerTreeHostImpl
*, base::TimeTicks monotonicTime
, bool hasUnfinishedAnimation
) { }
31 virtual void willAnimateLayers(LayerTreeHostImpl
*, base::TimeTicks monotonicTime
) { }
32 virtual void applyScrollAndScale(gfx::Vector2d
, float) { }
33 virtual void animate(base::TimeTicks monotonicTime
) { }
34 virtual void layout() { }
35 virtual void didRecreateOutputSurface(bool succeeded
) { }
36 virtual void didAddAnimation() { }
37 virtual void didCommit() { }
38 virtual void didCommitAndDrawFrame() { }
39 virtual void scheduleComposite() { }
40 virtual void didDeferCommit() { }
41 virtual bool canActivatePendingTree();
43 // Implementation of WebAnimationDelegate
44 virtual void notifyAnimationStarted(double time
) OVERRIDE
{ }
45 virtual void notifyAnimationFinished(double time
) OVERRIDE
{ }
47 virtual scoped_ptr
<OutputSurface
> createOutputSurface();
53 class MockLayerImplTreeHostClient
: public LayerTreeHostClient
{
56 // The ThreadedTests runs with the main loop running. It instantiates a single MockLayerTreeHost and associated
57 // MockLayerTreeHostImpl/ThreadedMockLayerTreeHostClient.
59 // beginTest() is called once the main message loop is running and the layer tree host is initialized.
61 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to ThreadedTest methods of similar names.
62 // To track the commit process, override these functions.
64 // The test continues until someone calls endTest. endTest can be called on any thread, but be aware that
65 // ending the test is an asynchronous process.
66 class ThreadedTest
: public testing::Test
, public TestHooks
{
68 virtual ~ThreadedTest();
70 virtual void afterTest() = 0;
71 virtual void beginTest() = 0;
72 virtual void setupTree();
75 void endTestAfterDelay(int delayMilliseconds
);
77 void postAddAnimationToMainThread(Layer
*);
78 void postAddInstantAnimationToMainThread();
79 void postSetNeedsCommitToMainThread();
80 void postAcquireLayerTextures();
81 void postSetNeedsRedrawToMainThread();
82 void postSetVisibleToMainThread(bool visible
);
87 LayerTreeHost
* layerTreeHost() { return m_layerTreeHost
.get(); }
92 virtual void initializeSettings(LayerTreeSettings
&) { }
94 virtual void scheduleComposite() OVERRIDE
;
98 void dispatchAddInstantAnimation();
99 void dispatchAddAnimation(Layer
*);
100 void dispatchSetNeedsCommit();
101 void dispatchAcquireLayerTextures();
102 void dispatchSetNeedsRedraw();
103 void dispatchSetVisible(bool);
104 void dispatchComposite();
105 void dispatchDidAddAnimation();
107 virtual void runTest(bool threaded
);
109 Thread
* implThread() { return proxy() ? proxy()->implThread() : 0; }
110 Proxy
* proxy() const { return m_layerTreeHost
? m_layerTreeHost
->proxy() : 0; }
112 LayerTreeSettings m_settings
;
113 scoped_ptr
<MockLayerImplTreeHostClient
> m_client
;
114 scoped_ptr
<LayerTreeHost
> m_layerTreeHost
;
118 bool m_endWhenBeginReturns
;
123 scoped_ptr
<Thread
> m_mainCCThread
;
124 scoped_ptr
<base::Thread
> m_implThread
;
125 base::CancelableClosure m_timeout
;
126 base::WeakPtr
<ThreadedTest
> m_mainThreadWeakPtr
;
127 base::WeakPtrFactory
<ThreadedTest
> m_weakFactory
;
130 class ThreadedTestThreadOnly
: public ThreadedTest
{
132 void runTestThreaded()
134 ThreadedTest::runTest(true);
138 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
139 class MockLayerTreeHostImpl
: public LayerTreeHostImpl
{
141 static scoped_ptr
<MockLayerTreeHostImpl
> create(TestHooks
*, const LayerTreeSettings
&, LayerTreeHostImplClient
*, Proxy
*);
143 virtual void beginCommit() OVERRIDE
;
144 virtual void commitComplete() OVERRIDE
;
145 virtual bool prepareToDraw(FrameData
&) OVERRIDE
;
146 virtual void drawLayers(FrameData
&) OVERRIDE
;
147 virtual void activatePendingTreeIfNeeded() OVERRIDE
;
149 // Make these public.
150 typedef std::vector
<LayerImpl
*> LayerList
;
153 virtual void animateLayers(base::TimeTicks monotonicTime
, base::Time wallClockTime
) OVERRIDE
;
154 virtual base::TimeDelta
lowFrequencyAnimationInterval() const OVERRIDE
;
157 MockLayerTreeHostImpl(TestHooks
*, const LayerTreeSettings
&, LayerTreeHostImplClient
*, Proxy
*);
159 TestHooks
* m_testHooks
;
164 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
165 TEST_F(TEST_FIXTURE_NAME, runSingleThread) \
170 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
171 TEST_F(TEST_FIXTURE_NAME, runMultiThread) \
176 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
177 SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
178 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
180 #endif // CC_TEST_LAYER_TREE_TEST_COMMON_H_