layer_tree_test.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#ifndef CC_TEST_LAYER_TREE_TEST_COMMON_H_
6#define CC_TEST_LAYER_TREE_TEST_COMMON_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/threading/thread.h"
10#include "cc/base/thread.h"
11#include "cc/trees/layer_tree_host.h"
12#include "cc/trees/layer_tree_host_impl.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "third_party/WebKit/Source/Platform/chromium/public/WebAnimationDelegate.h"
15
16namespace Webkit {
17class WebGraphicsContext3D;
18}
19
20namespace cc {
21class FakeLayerTreeHostClient;
22class LayerImpl;
23class LayerTreeHost;
24class LayerTreeHostClient;
25class LayerTreeHostImpl;
26
27// Used by test stubs to notify the test when something interesting happens.
28class TestHooks : public WebKit::WebAnimationDelegate {
29 public:
30  TestHooks();
31  virtual ~TestHooks();
32
33  virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) {}
34  virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) {}
35  virtual void TreeActivatedOnThread(LayerTreeHostImpl* host_impl) {}
36  virtual void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
37                                           bool success) {}
38  virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
39                                     LayerTreeHostImpl::FrameData* frame_data,
40                                     bool result);
41  virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) {}
42  virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) {}
43  virtual void AnimateLayers(LayerTreeHostImpl* host_impl,
44                             base::TimeTicks monotonic_time) {}
45  virtual void UpdateAnimationState(LayerTreeHostImpl* host_impl,
46                                    bool has_unfinished_animation) {}
47  virtual void WillAnimateLayers(LayerTreeHostImpl* host_impl,
48                                 base::TimeTicks monotonic_time) {}
49  virtual void ApplyScrollAndScale(gfx::Vector2d scroll_delta,
50                                   float scale) {}
51  virtual void Animate(base::TimeTicks monotonic_time) {}
52  virtual void Layout() {}
53  virtual void DidRecreateOutputSurface(bool succeeded) {}
54  virtual void WillRetryRecreateOutputSurface() {}
55  virtual void DidAddAnimation() {}
56  virtual void DidCommit() {}
57  virtual void DidCommitAndDrawFrame() {}
58  virtual void ScheduleComposite() {}
59  virtual void DidDeferCommit() {}
60  virtual bool CanActivatePendingTree();
61  virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl* host_impl,
62                                       bool visible) {}
63
64  // Implementation of WebAnimationDelegate
65  virtual void notifyAnimationStarted(double time) OVERRIDE {}
66  virtual void notifyAnimationFinished(double time) OVERRIDE {}
67
68  virtual scoped_ptr<OutputSurface> CreateOutputSurface();
69
70  virtual scoped_refptr<cc::ContextProvider>
71      OffscreenContextProviderForMainThread();
72  virtual scoped_refptr<cc::ContextProvider>
73      OffscreenContextProviderForCompositorThread();
74
75 private:
76  scoped_ptr<FakeLayerTreeHostClient> fake_client_;
77};
78
79class BeginTask;
80class LayerTreeHostClientForTesting;
81class TimeoutTask;
82
83// The LayerTreeTests runs with the main loop running. It instantiates a single
84// LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
85// LayerTreeHostClientForTesting.
86//
87// BeginTest() is called once the main message loop is running and the layer
88// tree host is initialized.
89//
90// Key stages of the drawing loop, e.g. drawing or commiting, redirect to
91// LayerTreeTest methods of similar names. To track the commit process, override
92// these functions.
93//
94// The test continues until someone calls EndTest. EndTest can be called on any
95// thread, but be aware that ending the test is an asynchronous process.
96class LayerTreeTest : public testing::Test, public TestHooks {
97 public:
98  virtual ~LayerTreeTest();
99
100  virtual void AfterTest() = 0;
101  virtual void BeginTest() = 0;
102  virtual void SetupTree();
103
104  void EndTest();
105  void EndTestAfterDelay(int delay_milliseconds);
106
107  void PostAddAnimationToMainThread(Layer* layer_to_receive_animation);
108  void PostAddInstantAnimationToMainThread();
109  void PostSetNeedsCommitToMainThread();
110  void PostAcquireLayerTextures();
111  void PostSetNeedsRedrawToMainThread();
112  void PostSetVisibleToMainThread(bool visible);
113
114  void DoBeginTest();
115  void Timeout();
116
117 protected:
118  LayerTreeTest();
119
120  virtual void InitializeSettings(LayerTreeSettings* settings) {}
121
122  virtual void ScheduleComposite() OVERRIDE;
123
124  void RealEndTest();
125
126  void DispatchAddInstantAnimation();
127  void DispatchAddAnimation(Layer* layer_to_receive_animation);
128  void DispatchSetNeedsCommit();
129  void DispatchAcquireLayerTextures();
130  void DispatchSetNeedsRedraw();
131  void DispatchSetVisible(bool visible);
132  void DispatchComposite();
133  void DispatchDidAddAnimation();
134
135  virtual void RunTest(bool threaded);
136
137  Thread* ImplThread() { return proxy() ? proxy()->ImplThread() : NULL; }
138  Proxy* proxy() const {
139    return layer_tree_host_ ? layer_tree_host_->proxy() : NULL;
140  }
141
142  bool TestEnded() const { return ended_; }
143
144  LayerTreeHost* layer_tree_host() { return layer_tree_host_.get(); }
145
146 private:
147  LayerTreeSettings settings_;
148  scoped_ptr<LayerTreeHostClientForTesting> client_;
149  scoped_ptr<LayerTreeHost> layer_tree_host_;
150
151  bool beginning_;
152  bool end_when_begin_returns_;
153  bool timed_out_;
154  bool scheduled_;
155  bool schedule_when_set_visible_true_;
156  bool started_;
157  bool ended_;
158
159  scoped_ptr<Thread> main_ccthread_;
160  scoped_ptr<base::Thread> impl_thread_;
161  base::CancelableClosure timeout_;
162  base::WeakPtr<LayerTreeTest> main_thread_weak_ptr_;
163  base::WeakPtrFactory<LayerTreeTest> weak_factory_;
164};
165
166}  // namespace cc
167
168#define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
169  TEST_F(TEST_FIXTURE_NAME, RunSingleThread) {  \
170    RunTest(false);                             \
171  }
172
173#define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)  \
174  TEST_F(TEST_FIXTURE_NAME, RunMultiThread) {   \
175    RunTest(true);                              \
176  }
177
178#define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
179  SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME)                 \
180  MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
181
182#endif  // CC_TEST_LAYER_TREE_TEST_COMMON_H_
183