Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / cc / layers / layer_utils_unittest.cc
1 // Copyright 2014 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_utils.h"
6
7 #include "cc/animation/transform_operations.h"
8 #include "cc/layers/layer_impl.h"
9 #include "cc/test/animation_test_common.h"
10 #include "cc/test/fake_impl_proxy.h"
11 #include "cc/test/fake_layer_tree_host_impl.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/box_f.h"
14 #include "ui/gfx/test/gfx_util.h"
15
16 namespace cc {
17 namespace {
18
19 float diagonal(float width, float height) {
20   return std::sqrt(width * width + height * height);
21 }
22
23 class LayerUtilsGetAnimationBoundsTest : public testing::Test {
24  public:
25   LayerUtilsGetAnimationBoundsTest()
26       : host_impl_(&proxy_),
27         root_(CreateThreeNodeTree(host_impl_)),
28         parent_(root_->children()[0]),
29         child_(parent_->children()[0]) {}
30
31   LayerImpl* root() { return root_.get(); }
32   LayerImpl* parent() { return parent_; }
33   LayerImpl* child() { return child_; }
34
35  private:
36   static scoped_ptr<LayerImpl> CreateThreeNodeTree(
37       LayerTreeHostImpl& host_impl) {
38     scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
39     root->AddChild(LayerImpl::Create(host_impl.active_tree(), 2));
40     root->children()[0]
41         ->AddChild(LayerImpl::Create(host_impl.active_tree(), 3));
42     return root.Pass();
43   }
44
45   FakeImplProxy proxy_;
46   FakeLayerTreeHostImpl host_impl_;
47   scoped_ptr<LayerImpl> root_;
48   LayerImpl* parent_;
49   LayerImpl* child_;
50 };
51
52 TEST_F(LayerUtilsGetAnimationBoundsTest, ScaleRoot) {
53   double duration = 1.0;
54
55   TransformOperations start;
56   start.AppendScale(1.f, 1.f, 1.f);
57   TransformOperations end;
58   end.AppendScale(2.f, 2.f, 1.f);
59   AddAnimatedTransformToLayer(root(), duration, start, end);
60
61   root()->SetPosition(gfx::PointF());
62   parent()->SetPosition(gfx::PointF());
63   parent()->SetBounds(gfx::Size(350, 200));
64
65   child()->SetDrawsContent(true);
66   child()->draw_properties().screen_space_transform_is_animating = true;
67   child()->SetPosition(gfx::PointF(150.f, 50.f));
68   child()->SetBounds(gfx::Size(100, 200));
69
70   gfx::BoxF box;
71   bool success = LayerUtils::GetAnimationBounds(*child(), &box);
72   EXPECT_TRUE(success);
73   gfx::BoxF expected(150.f, 50.f, 0.f, 350.f, 450.f, 0.f);
74   EXPECT_BOXF_EQ(expected, box);
75 }
76
77 TEST_F(LayerUtilsGetAnimationBoundsTest, TranslateParentLayer) {
78   double duration = 1.0;
79
80   TransformOperations start;
81   start.AppendTranslate(0.f, 0.f, 0.f);
82   TransformOperations end;
83   end.AppendTranslate(50.f, 50.f, 0.f);
84   AddAnimatedTransformToLayer(parent(), duration, start, end);
85
86   parent()->SetBounds(gfx::Size(350, 200));
87
88   child()->SetDrawsContent(true);
89   child()->draw_properties().screen_space_transform_is_animating = true;
90   child()->SetPosition(gfx::PointF(150.f, 50.f));
91   child()->SetBounds(gfx::Size(100, 200));
92
93   gfx::BoxF box;
94   bool success = LayerUtils::GetAnimationBounds(*child(), &box);
95   EXPECT_TRUE(success);
96   gfx::BoxF expected(150.f, 50.f, 0.f, 150.f, 250.f, 0.f);
97   EXPECT_BOXF_EQ(expected, box);
98 }
99
100 TEST_F(LayerUtilsGetAnimationBoundsTest, TranslateChildLayer) {
101   double duration = 1.0;
102
103   TransformOperations start;
104   start.AppendTranslate(0.f, 0.f, 0.f);
105   TransformOperations end;
106   end.AppendTranslate(50.f, 50.f, 0.f);
107   AddAnimatedTransformToLayer(child(), duration, start, end);
108
109   parent()->SetBounds(gfx::Size(350, 200));
110
111   child()->SetDrawsContent(true);
112   child()->draw_properties().screen_space_transform_is_animating = true;
113   child()->SetPosition(gfx::PointF(150.f, 50.f));
114   child()->SetBounds(gfx::Size(100, 200));
115
116   gfx::BoxF box;
117   bool success = LayerUtils::GetAnimationBounds(*child(), &box);
118   EXPECT_TRUE(success);
119   gfx::BoxF expected(150.f, 50.f, 0.f, 150.f, 250.f, 0.f);
120   EXPECT_BOXF_EQ(expected, box);
121 }
122
123 TEST_F(LayerUtilsGetAnimationBoundsTest, TranslateBothLayers) {
124   double duration = 1.0;
125
126   TransformOperations start;
127   start.AppendTranslate(0.f, 0.f, 0.f);
128   TransformOperations child_end;
129   child_end.AppendTranslate(50.f, 0.f, 0.f);
130   AddAnimatedTransformToLayer(parent(), duration, start, child_end);
131
132   TransformOperations grand_child_end;
133   grand_child_end.AppendTranslate(0.f, 50.f, 0.f);
134   AddAnimatedTransformToLayer(child(), duration, start, grand_child_end);
135
136   parent()->SetBounds(gfx::Size(350, 200));
137
138   child()->SetDrawsContent(true);
139   child()->draw_properties().screen_space_transform_is_animating = true;
140   child()->SetPosition(gfx::PointF(150.f, 50.f));
141   child()->SetBounds(gfx::Size(100, 200));
142
143   gfx::BoxF box;
144   bool success = LayerUtils::GetAnimationBounds(*child(), &box);
145   EXPECT_TRUE(success);
146   gfx::BoxF expected(150.f, 50.f, 0.f, 150.f, 250.f, 0.f);
147   EXPECT_BOXF_EQ(expected, box);
148 }
149
150 TEST_F(LayerUtilsGetAnimationBoundsTest, RotateXNoPerspective) {
151   double duration = 1.0;
152
153   TransformOperations start;
154   start.AppendRotate(1.f, 0.f, 0.f, 0.f);
155   TransformOperations end;
156   end.AppendRotate(1.f, 0.f, 0.f, 90.f);
157   AddAnimatedTransformToLayer(child(), duration, start, end);
158
159   parent()->SetBounds(gfx::Size(350, 200));
160
161   gfx::Size bounds(100, 100);
162   child()->SetDrawsContent(true);
163   child()->draw_properties().screen_space_transform_is_animating = true;
164   child()->SetPosition(gfx::PointF(150.f, 50.f));
165   child()->SetBounds(bounds);
166
167   gfx::BoxF box;
168   bool success = LayerUtils::GetAnimationBounds(*child(), &box);
169   EXPECT_TRUE(success);
170   gfx::BoxF expected(150.f, 50.f, -50.f, 100.f, 100.f, 100.f);
171   EXPECT_BOXF_EQ(expected, box);
172 }
173
174 TEST_F(LayerUtilsGetAnimationBoundsTest, RotateXWithPerspective) {
175   double duration = 1.0;
176
177   TransformOperations start;
178   start.AppendRotate(1.f, 0.f, 0.f, 0.f);
179   TransformOperations end;
180   end.AppendRotate(1.f, 0.f, 0.f, 90.f);
181   AddAnimatedTransformToLayer(child(), duration, start, end);
182
183   // Make the anchor point not the default 0.5 value and line up with the
184   // child center to make the math easier.
185   parent()->SetAnchorPoint(gfx::PointF(0.375f, 0.375f));
186   parent()->SetBounds(gfx::Size(400, 400));
187
188   gfx::Transform perspective;
189   perspective.ApplyPerspectiveDepth(100.f);
190   parent()->SetSublayerTransform(perspective);
191
192   gfx::Size bounds(100, 100);
193   child()->SetDrawsContent(true);
194   child()->draw_properties().screen_space_transform_is_animating = true;
195   child()->SetPosition(gfx::PointF(100.f, 100.f));
196   child()->SetBounds(bounds);
197
198   gfx::BoxF box;
199   bool success = LayerUtils::GetAnimationBounds(*child(), &box);
200   EXPECT_TRUE(success);
201   gfx::BoxF expected(50.f, 50.f, -33.333336f, 200.f, 200.f, 133.333344f);
202   EXPECT_BOXF_EQ(expected, box);
203 }
204
205 TEST_F(LayerUtilsGetAnimationBoundsTest, RotateZ) {
206   double duration = 1.0;
207
208   TransformOperations start;
209   start.AppendRotate(0.f, 0.f, 1.f, 0.f);
210   TransformOperations end;
211   end.AppendRotate(0.f, 0.f, 1.f, 90.f);
212   AddAnimatedTransformToLayer(child(), duration, start, end);
213
214   parent()->SetBounds(gfx::Size(350, 200));
215
216   gfx::Size bounds(100, 100);
217   child()->SetDrawsContent(true);
218   child()->draw_properties().screen_space_transform_is_animating = true;
219   child()->SetPosition(gfx::PointF(150.f, 50.f));
220   child()->SetBounds(bounds);
221
222   gfx::BoxF box;
223   bool success = LayerUtils::GetAnimationBounds(*child(), &box);
224   EXPECT_TRUE(success);
225   float diag = diagonal(bounds.width(), bounds.height());
226   gfx::BoxF expected(150.f + 0.5f * (bounds.width() - diag),
227                      50.f + 0.5f * (bounds.height() - diag),
228                      0.f,
229                      diag,
230                      diag,
231                      0.f);
232   EXPECT_BOXF_EQ(expected, box);
233 }
234
235 TEST_F(LayerUtilsGetAnimationBoundsTest, MismatchedTransforms) {
236   double duration = 1.0;
237
238   TransformOperations start;
239   start.AppendTranslate(5, 6, 7);
240   TransformOperations end;
241   end.AppendRotate(0.f, 0.f, 1.f, 90.f);
242   AddAnimatedTransformToLayer(child(), duration, start, end);
243
244   parent()->SetBounds(gfx::Size(350, 200));
245
246   gfx::Size bounds(100, 100);
247   child()->SetDrawsContent(true);
248   child()->draw_properties().screen_space_transform_is_animating = true;
249   child()->SetPosition(gfx::PointF(150.f, 50.f));
250   child()->SetBounds(bounds);
251
252   gfx::BoxF box;
253   bool success = LayerUtils::GetAnimationBounds(*child(), &box);
254   EXPECT_FALSE(success);
255 }
256
257 }  // namespace
258 }  // namespace cc