Upstream version 11.40.277.0
[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   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   }
42   ~TestLayer() override {}
43
44   bool draws_content_;
45 };
46
47 #define EXPECT_COUNT(layer, target, contrib, itself)                           \
48   EXPECT_EQ(target, layer->count_representing_target_surface_);                \
49   EXPECT_EQ(contrib, layer->count_representing_contributing_surface_);         \
50   EXPECT_EQ(itself, layer->count_representing_itself_);
51
52 typedef LayerIterator<Layer> FrontToBack;
53
54 void ResetCounts(RenderSurfaceLayerList* render_surface_layer_list) {
55   for (unsigned surface_index = 0;
56        surface_index < render_surface_layer_list->size();
57        ++surface_index) {
58     TestLayer* render_surface_layer = static_cast<TestLayer*>(
59         render_surface_layer_list->at(surface_index));
60     RenderSurface* render_surface = render_surface_layer->render_surface();
61
62     render_surface_layer->count_representing_target_surface_ = -1;
63     render_surface_layer->count_representing_contributing_surface_ = -1;
64     render_surface_layer->count_representing_itself_ = -1;
65
66     for (unsigned layer_index = 0;
67          layer_index < render_surface->layer_list().size();
68          ++layer_index) {
69       TestLayer* layer = static_cast<TestLayer*>(
70           render_surface->layer_list().at(layer_index).get());
71
72       layer->count_representing_target_surface_ = -1;
73       layer->count_representing_contributing_surface_ = -1;
74       layer->count_representing_itself_ = -1;
75     }
76   }
77 }
78
79 void IterateFrontToBack(
80     RenderSurfaceLayerList* render_surface_layer_list) {
81   ResetCounts(render_surface_layer_list);
82   int count = 0;
83   for (FrontToBack it = FrontToBack::Begin(render_surface_layer_list);
84        it != FrontToBack::End(render_surface_layer_list);
85        ++it, ++count) {
86     TestLayer* layer = static_cast<TestLayer*>(*it);
87     if (it.represents_target_render_surface())
88       layer->count_representing_target_surface_ = count;
89     if (it.represents_contributing_render_surface())
90       layer->count_representing_contributing_surface_ = count;
91     if (it.represents_itself())
92       layer->count_representing_itself_ = count;
93   }
94 }
95
96 TEST(LayerIteratorTest, EmptyTree) {
97   RenderSurfaceLayerList render_surface_layer_list;
98
99   IterateFrontToBack(&render_surface_layer_list);
100 }
101
102 TEST(LayerIteratorTest, SimpleTree) {
103   scoped_refptr<TestLayer> root_layer = TestLayer::Create();
104   scoped_refptr<TestLayer> first = TestLayer::Create();
105   scoped_refptr<TestLayer> second = TestLayer::Create();
106   scoped_refptr<TestLayer> third = TestLayer::Create();
107   scoped_refptr<TestLayer> fourth = TestLayer::Create();
108
109   root_layer->AddChild(first);
110   root_layer->AddChild(second);
111   root_layer->AddChild(third);
112   root_layer->AddChild(fourth);
113
114   FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
115   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
116   host->SetRootLayer(root_layer);
117
118   RenderSurfaceLayerList render_surface_layer_list;
119   LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
120       root_layer.get(), root_layer->bounds(), &render_surface_layer_list);
121   LayerTreeHostCommon::CalculateDrawProperties(&inputs);
122
123   IterateFrontToBack(&render_surface_layer_list);
124   EXPECT_COUNT(root_layer, 5, -1, 4);
125   EXPECT_COUNT(first, -1, -1, 3);
126   EXPECT_COUNT(second, -1, -1, 2);
127   EXPECT_COUNT(third, -1, -1, 1);
128   EXPECT_COUNT(fourth, -1, -1, 0);
129 }
130
131 TEST(LayerIteratorTest, ComplexTree) {
132   scoped_refptr<TestLayer> root_layer = TestLayer::Create();
133   scoped_refptr<TestLayer> root1 = TestLayer::Create();
134   scoped_refptr<TestLayer> root2 = TestLayer::Create();
135   scoped_refptr<TestLayer> root3 = TestLayer::Create();
136   scoped_refptr<TestLayer> root21 = TestLayer::Create();
137   scoped_refptr<TestLayer> root22 = TestLayer::Create();
138   scoped_refptr<TestLayer> root23 = TestLayer::Create();
139   scoped_refptr<TestLayer> root221 = TestLayer::Create();
140   scoped_refptr<TestLayer> root231 = TestLayer::Create();
141
142   root_layer->AddChild(root1);
143   root_layer->AddChild(root2);
144   root_layer->AddChild(root3);
145   root2->AddChild(root21);
146   root2->AddChild(root22);
147   root2->AddChild(root23);
148   root22->AddChild(root221);
149   root23->AddChild(root231);
150
151   FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
152   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
153   host->SetRootLayer(root_layer);
154
155   RenderSurfaceLayerList render_surface_layer_list;
156   LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
157       root_layer.get(), root_layer->bounds(), &render_surface_layer_list);
158   LayerTreeHostCommon::CalculateDrawProperties(&inputs);
159
160   IterateFrontToBack(&render_surface_layer_list);
161   EXPECT_COUNT(root_layer, 9, -1, 8);
162   EXPECT_COUNT(root1, -1, -1, 7);
163   EXPECT_COUNT(root2, -1, -1, 6);
164   EXPECT_COUNT(root21, -1, -1, 5);
165   EXPECT_COUNT(root22, -1, -1, 4);
166   EXPECT_COUNT(root221, -1, -1, 3);
167   EXPECT_COUNT(root23, -1, -1, 2);
168   EXPECT_COUNT(root231, -1, -1, 1);
169   EXPECT_COUNT(root3, -1, -1, 0);
170 }
171
172 TEST(LayerIteratorTest, ComplexTreeMultiSurface) {
173   scoped_refptr<TestLayer> root_layer = TestLayer::Create();
174   scoped_refptr<TestLayer> root1 = TestLayer::Create();
175   scoped_refptr<TestLayer> root2 = TestLayer::Create();
176   scoped_refptr<TestLayer> root3 = TestLayer::Create();
177   scoped_refptr<TestLayer> root21 = TestLayer::Create();
178   scoped_refptr<TestLayer> root22 = TestLayer::Create();
179   scoped_refptr<TestLayer> root23 = TestLayer::Create();
180   scoped_refptr<TestLayer> root221 = TestLayer::Create();
181   scoped_refptr<TestLayer> root231 = TestLayer::Create();
182
183   root_layer->AddChild(root1);
184   root_layer->AddChild(root2);
185   root_layer->AddChild(root3);
186   root2->set_draws_content(false);
187   root2->SetOpacity(0.5f);
188   root2->SetForceRenderSurface(true);  // Force the layer to own a new surface.
189   root2->AddChild(root21);
190   root2->AddChild(root22);
191   root2->AddChild(root23);
192   root22->SetOpacity(0.5f);
193   root22->AddChild(root221);
194   root23->SetOpacity(0.5f);
195   root23->AddChild(root231);
196
197   FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
198   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
199   host->SetRootLayer(root_layer);
200
201   RenderSurfaceLayerList render_surface_layer_list;
202   LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
203       root_layer.get(), root_layer->bounds(), &render_surface_layer_list);
204   LayerTreeHostCommon::CalculateDrawProperties(&inputs);
205
206   IterateFrontToBack(&render_surface_layer_list);
207   EXPECT_COUNT(root_layer, 14, -1, 13);
208   EXPECT_COUNT(root1, -1, -1, 12);
209   EXPECT_COUNT(root2, 10, 11, -1);
210   EXPECT_COUNT(root21, -1, -1, 9);
211   EXPECT_COUNT(root22, 7, 8, 6);
212   EXPECT_COUNT(root221, -1, -1, 5);
213   EXPECT_COUNT(root23, 3, 4, 2);
214   EXPECT_COUNT(root231, -1, -1, 1);
215   EXPECT_COUNT(root3, -1, -1, 0);
216 }
217
218 }  // namespace
219 }  // namespace cc