- add sources.
[platform/framework/web/crosswalk.git] / src / cc / layers / layer_iterator_unittest.cc
1 // Copyright 2012 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 #include "cc/layers/layer_iterator.h"
6
7 #include <vector>
8
9 #include "cc/layers/layer.h"
10 #include "cc/test/fake_layer_tree_host.h"
11 #include "cc/trees/layer_tree_host_common.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/transform.h"
15
16 using ::testing::Mock;
17 using ::testing::_;
18 using ::testing::AtLeast;
19 using ::testing::AnyNumber;
20
21 namespace cc {
22 namespace {
23
24 class TestLayer : public Layer {
25  public:
26   static scoped_refptr<TestLayer> Create() {
27     return make_scoped_refptr(new TestLayer());
28   }
29
30   int count_representing_target_surface_;
31   int count_representing_contributing_surface_;
32   int count_representing_itself_;
33
34   virtual bool DrawsContent() const OVERRIDE { return draws_content_; }
35   void set_draws_content(bool draws_content) { draws_content_ = draws_content; }
36
37  private:
38   TestLayer() : Layer(), draws_content_(true) {
39     SetBounds(gfx::Size(100, 100));
40     SetPosition(gfx::Point());
41     SetAnchorPoint(gfx::Point());
42   }
43   virtual ~TestLayer() {}
44
45   bool draws_content_;
46 };
47
48 #define EXPECT_COUNT(layer, target, contrib, itself)                           \
49   EXPECT_EQ(target, layer->count_representing_target_surface_);                \
50   EXPECT_EQ(contrib, layer->count_representing_contributing_surface_);         \
51   EXPECT_EQ(itself, layer->count_representing_itself_);
52
53 typedef LayerIterator<Layer,
54                       RenderSurfaceLayerList,
55                       RenderSurface,
56                       LayerIteratorActions::FrontToBack> FrontToBack;
57
58 void ResetCounts(RenderSurfaceLayerList* render_surface_layer_list) {
59   for (unsigned surface_index = 0;
60        surface_index < render_surface_layer_list->size();
61        ++surface_index) {
62     TestLayer* render_surface_layer = static_cast<TestLayer*>(
63         render_surface_layer_list->at(surface_index));
64     RenderSurface* render_surface = render_surface_layer->render_surface();
65
66     render_surface_layer->count_representing_target_surface_ = -1;
67     render_surface_layer->count_representing_contributing_surface_ = -1;
68     render_surface_layer->count_representing_itself_ = -1;
69
70     for (unsigned layer_index = 0;
71          layer_index < render_surface->layer_list().size();
72          ++layer_index) {
73       TestLayer* layer = static_cast<TestLayer*>(
74           render_surface->layer_list().at(layer_index));
75
76       layer->count_representing_target_surface_ = -1;
77       layer->count_representing_contributing_surface_ = -1;
78       layer->count_representing_itself_ = -1;
79     }
80   }
81 }
82
83 void IterateFrontToBack(
84     RenderSurfaceLayerList* render_surface_layer_list) {
85   ResetCounts(render_surface_layer_list);
86   int count = 0;
87   for (FrontToBack it = FrontToBack::Begin(render_surface_layer_list);
88        it != FrontToBack::End(render_surface_layer_list);
89        ++it, ++count) {
90     TestLayer* layer = static_cast<TestLayer*>(*it);
91     if (it.represents_target_render_surface())
92       layer->count_representing_target_surface_ = count;
93     if (it.represents_contributing_render_surface())
94       layer->count_representing_contributing_surface_ = count;
95     if (it.represents_itself())
96       layer->count_representing_itself_ = count;
97   }
98 }
99
100 TEST(LayerIteratorTest, EmptyTree) {
101   RenderSurfaceLayerList render_surface_layer_list;
102
103   IterateFrontToBack(&render_surface_layer_list);
104 }
105
106 TEST(LayerIteratorTest, SimpleTree) {
107   scoped_refptr<TestLayer> root_layer = TestLayer::Create();
108   scoped_refptr<TestLayer> first = TestLayer::Create();
109   scoped_refptr<TestLayer> second = TestLayer::Create();
110   scoped_refptr<TestLayer> third = TestLayer::Create();
111   scoped_refptr<TestLayer> fourth = TestLayer::Create();
112
113   root_layer->AddChild(first);
114   root_layer->AddChild(second);
115   root_layer->AddChild(third);
116   root_layer->AddChild(fourth);
117
118   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
119   host->SetRootLayer(root_layer);
120
121   RenderSurfaceLayerList render_surface_layer_list;
122   LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
123       root_layer.get(), root_layer->bounds(), &render_surface_layer_list);
124   LayerTreeHostCommon::CalculateDrawProperties(&inputs);
125
126   IterateFrontToBack(&render_surface_layer_list);
127   EXPECT_COUNT(root_layer, 5, -1, 4);
128   EXPECT_COUNT(first, -1, -1, 3);
129   EXPECT_COUNT(second, -1, -1, 2);
130   EXPECT_COUNT(third, -1, -1, 1);
131   EXPECT_COUNT(fourth, -1, -1, 0);
132 }
133
134 TEST(LayerIteratorTest, ComplexTree) {
135   scoped_refptr<TestLayer> root_layer = TestLayer::Create();
136   scoped_refptr<TestLayer> root1 = TestLayer::Create();
137   scoped_refptr<TestLayer> root2 = TestLayer::Create();
138   scoped_refptr<TestLayer> root3 = TestLayer::Create();
139   scoped_refptr<TestLayer> root21 = TestLayer::Create();
140   scoped_refptr<TestLayer> root22 = TestLayer::Create();
141   scoped_refptr<TestLayer> root23 = TestLayer::Create();
142   scoped_refptr<TestLayer> root221 = TestLayer::Create();
143   scoped_refptr<TestLayer> root231 = TestLayer::Create();
144
145   root_layer->AddChild(root1);
146   root_layer->AddChild(root2);
147   root_layer->AddChild(root3);
148   root2->AddChild(root21);
149   root2->AddChild(root22);
150   root2->AddChild(root23);
151   root22->AddChild(root221);
152   root23->AddChild(root231);
153
154   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
155   host->SetRootLayer(root_layer);
156
157   RenderSurfaceLayerList render_surface_layer_list;
158   LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
159       root_layer.get(), root_layer->bounds(), &render_surface_layer_list);
160   LayerTreeHostCommon::CalculateDrawProperties(&inputs);
161
162   IterateFrontToBack(&render_surface_layer_list);
163   EXPECT_COUNT(root_layer, 9, -1, 8);
164   EXPECT_COUNT(root1, -1, -1, 7);
165   EXPECT_COUNT(root2, -1, -1, 6);
166   EXPECT_COUNT(root21, -1, -1, 5);
167   EXPECT_COUNT(root22, -1, -1, 4);
168   EXPECT_COUNT(root221, -1, -1, 3);
169   EXPECT_COUNT(root23, -1, -1, 2);
170   EXPECT_COUNT(root231, -1, -1, 1);
171   EXPECT_COUNT(root3, -1, -1, 0);
172 }
173
174 TEST(LayerIteratorTest, ComplexTreeMultiSurface) {
175   scoped_refptr<TestLayer> root_layer = TestLayer::Create();
176   scoped_refptr<TestLayer> root1 = TestLayer::Create();
177   scoped_refptr<TestLayer> root2 = TestLayer::Create();
178   scoped_refptr<TestLayer> root3 = TestLayer::Create();
179   scoped_refptr<TestLayer> root21 = TestLayer::Create();
180   scoped_refptr<TestLayer> root22 = TestLayer::Create();
181   scoped_refptr<TestLayer> root23 = TestLayer::Create();
182   scoped_refptr<TestLayer> root221 = TestLayer::Create();
183   scoped_refptr<TestLayer> root231 = TestLayer::Create();
184
185   root_layer->AddChild(root1);
186   root_layer->AddChild(root2);
187   root_layer->AddChild(root3);
188   root2->set_draws_content(false);
189   root2->SetOpacity(0.5f);
190   root2->SetForceRenderSurface(true);  // Force the layer to own a new surface.
191   root2->AddChild(root21);
192   root2->AddChild(root22);
193   root2->AddChild(root23);
194   root22->SetOpacity(0.5f);
195   root22->AddChild(root221);
196   root23->SetOpacity(0.5f);
197   root23->AddChild(root231);
198
199   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
200   host->SetRootLayer(root_layer);
201
202   RenderSurfaceLayerList render_surface_layer_list;
203   LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
204       root_layer.get(), root_layer->bounds(), &render_surface_layer_list);
205   LayerTreeHostCommon::CalculateDrawProperties(&inputs);
206
207   IterateFrontToBack(&render_surface_layer_list);
208   EXPECT_COUNT(root_layer, 14, -1, 13);
209   EXPECT_COUNT(root1, -1, -1, 12);
210   EXPECT_COUNT(root2, 10, 11, -1);
211   EXPECT_COUNT(root21, -1, -1, 9);
212   EXPECT_COUNT(root22, 7, 8, 6);
213   EXPECT_COUNT(root221, -1, -1, 5);
214   EXPECT_COUNT(root23, 3, 4, 2);
215   EXPECT_COUNT(root231, -1, -1, 1);
216   EXPECT_COUNT(root3, -1, -1, 0);
217 }
218
219 }  // namespace
220 }  // namespace cc