Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / cc / layers / layer_utils_unittest.cc
blob3a8d99c2e06b8230f9a0752e90a78c236a5331d9
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.
5 #include "cc/layers/layer_utils.h"
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 "cc/test/test_shared_bitmap_manager.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/box_f.h"
15 #include "ui/gfx/test/gfx_util.h"
17 namespace cc {
18 namespace {
20 float diagonal(float width, float height) {
21 return std::sqrt(width * width + height * height);
24 class LayerUtilsGetAnimationBoundsTest : public testing::Test {
25 public:
26 LayerUtilsGetAnimationBoundsTest()
27 : host_impl_(&proxy_, &shared_bitmap_manager_),
28 root_(CreateThreeNodeTree(host_impl_)),
29 parent_(root_->children()[0]),
30 child_(parent_->children()[0]) {}
32 LayerImpl* root() { return root_.get(); }
33 LayerImpl* parent() { return parent_; }
34 LayerImpl* child() { return child_; }
36 private:
37 static scoped_ptr<LayerImpl> CreateThreeNodeTree(
38 LayerTreeHostImpl& host_impl) {
39 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
40 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 2));
41 root->children()[0]
42 ->AddChild(LayerImpl::Create(host_impl.active_tree(), 3));
43 return root.Pass();
46 FakeImplProxy proxy_;
47 TestSharedBitmapManager shared_bitmap_manager_;
48 FakeLayerTreeHostImpl host_impl_;
49 scoped_ptr<LayerImpl> root_;
50 LayerImpl* parent_;
51 LayerImpl* child_;
54 TEST_F(LayerUtilsGetAnimationBoundsTest, ScaleRoot) {
55 double duration = 1.0;
57 TransformOperations start;
58 start.AppendScale(1.f, 1.f, 1.f);
59 TransformOperations end;
60 end.AppendScale(2.f, 2.f, 1.f);
61 AddAnimatedTransformToLayer(root(), duration, start, end);
63 root()->SetPosition(gfx::PointF());
64 parent()->SetPosition(gfx::PointF());
65 parent()->SetBounds(gfx::Size(350, 200));
67 child()->SetDrawsContent(true);
68 child()->draw_properties().screen_space_transform_is_animating = true;
69 child()->SetPosition(gfx::PointF(150.f, 50.f));
70 child()->SetBounds(gfx::Size(100, 200));
72 gfx::BoxF box;
73 bool success = LayerUtils::GetAnimationBounds(*child(), &box);
74 EXPECT_TRUE(success);
75 gfx::BoxF expected(150.f, 50.f, 0.f, 350.f, 450.f, 0.f);
76 EXPECT_BOXF_EQ(expected, box);
79 TEST_F(LayerUtilsGetAnimationBoundsTest, TranslateParentLayer) {
80 double duration = 1.0;
82 TransformOperations start;
83 start.AppendTranslate(0.f, 0.f, 0.f);
84 TransformOperations end;
85 end.AppendTranslate(50.f, 50.f, 0.f);
86 AddAnimatedTransformToLayer(parent(), duration, start, end);
88 parent()->SetBounds(gfx::Size(350, 200));
90 child()->SetDrawsContent(true);
91 child()->draw_properties().screen_space_transform_is_animating = true;
92 child()->SetPosition(gfx::PointF(150.f, 50.f));
93 child()->SetBounds(gfx::Size(100, 200));
95 gfx::BoxF box;
96 bool success = LayerUtils::GetAnimationBounds(*child(), &box);
97 EXPECT_TRUE(success);
98 gfx::BoxF expected(150.f, 50.f, 0.f, 150.f, 250.f, 0.f);
99 EXPECT_BOXF_EQ(expected, box);
102 TEST_F(LayerUtilsGetAnimationBoundsTest, TranslateChildLayer) {
103 double duration = 1.0;
105 TransformOperations start;
106 start.AppendTranslate(0.f, 0.f, 0.f);
107 TransformOperations end;
108 end.AppendTranslate(50.f, 50.f, 0.f);
109 AddAnimatedTransformToLayer(child(), duration, start, end);
111 parent()->SetBounds(gfx::Size(350, 200));
113 child()->SetDrawsContent(true);
114 child()->draw_properties().screen_space_transform_is_animating = true;
115 child()->SetPosition(gfx::PointF(150.f, 50.f));
116 child()->SetBounds(gfx::Size(100, 200));
118 gfx::BoxF box;
119 bool success = LayerUtils::GetAnimationBounds(*child(), &box);
120 EXPECT_TRUE(success);
121 gfx::BoxF expected(150.f, 50.f, 0.f, 150.f, 250.f, 0.f);
122 EXPECT_BOXF_EQ(expected, box);
125 TEST_F(LayerUtilsGetAnimationBoundsTest, TranslateBothLayers) {
126 double duration = 1.0;
128 TransformOperations start;
129 start.AppendTranslate(0.f, 0.f, 0.f);
130 TransformOperations child_end;
131 child_end.AppendTranslate(50.f, 0.f, 0.f);
132 AddAnimatedTransformToLayer(parent(), duration, start, child_end);
134 TransformOperations grand_child_end;
135 grand_child_end.AppendTranslate(0.f, 50.f, 0.f);
136 AddAnimatedTransformToLayer(child(), duration, start, grand_child_end);
138 parent()->SetBounds(gfx::Size(350, 200));
140 child()->SetDrawsContent(true);
141 child()->draw_properties().screen_space_transform_is_animating = true;
142 child()->SetPosition(gfx::PointF(150.f, 50.f));
143 child()->SetBounds(gfx::Size(100, 200));
145 gfx::BoxF box;
146 bool success = LayerUtils::GetAnimationBounds(*child(), &box);
147 EXPECT_TRUE(success);
148 gfx::BoxF expected(150.f, 50.f, 0.f, 150.f, 250.f, 0.f);
149 EXPECT_BOXF_EQ(expected, box);
152 TEST_F(LayerUtilsGetAnimationBoundsTest, RotateXNoPerspective) {
153 double duration = 1.0;
155 TransformOperations start;
156 start.AppendRotate(1.f, 0.f, 0.f, 0.f);
157 TransformOperations end;
158 end.AppendRotate(1.f, 0.f, 0.f, 90.f);
159 AddAnimatedTransformToLayer(child(), duration, start, end);
161 parent()->SetBounds(gfx::Size(350, 200));
163 gfx::Size bounds(100, 100);
164 child()->SetDrawsContent(true);
165 child()->draw_properties().screen_space_transform_is_animating = true;
166 child()->SetPosition(gfx::PointF(150.f, 50.f));
167 child()->SetBounds(bounds);
169 gfx::BoxF box;
170 bool success = LayerUtils::GetAnimationBounds(*child(), &box);
171 EXPECT_TRUE(success);
172 gfx::BoxF expected(150.f, 50.f, -50.f, 100.f, 100.f, 100.f);
173 EXPECT_BOXF_EQ(expected, box);
176 TEST_F(LayerUtilsGetAnimationBoundsTest, RotateXWithPerspective) {
177 double duration = 1.0;
179 TransformOperations start;
180 start.AppendRotate(1.f, 0.f, 0.f, 0.f);
181 TransformOperations end;
182 end.AppendRotate(1.f, 0.f, 0.f, 90.f);
183 AddAnimatedTransformToLayer(child(), duration, start, end);
185 // Make the anchor point not the default 0.5 value and line up with the
186 // child center to make the math easier.
187 parent()->SetAnchorPoint(gfx::PointF(0.375f, 0.375f));
188 parent()->SetBounds(gfx::Size(400, 400));
190 gfx::Transform perspective;
191 perspective.ApplyPerspectiveDepth(100.f);
192 parent()->SetTransform(perspective);
194 gfx::Size bounds(100, 100);
195 child()->SetDrawsContent(true);
196 child()->draw_properties().screen_space_transform_is_animating = true;
197 child()->SetPosition(gfx::PointF(100.f, 100.f));
198 child()->SetBounds(bounds);
200 gfx::BoxF box;
201 bool success = LayerUtils::GetAnimationBounds(*child(), &box);
202 EXPECT_TRUE(success);
203 gfx::BoxF expected(50.f, 50.f, -33.333336f, 200.f, 200.f, 133.333344f);
204 EXPECT_BOXF_EQ(expected, box);
207 TEST_F(LayerUtilsGetAnimationBoundsTest, RotateZ) {
208 double duration = 1.0;
210 TransformOperations start;
211 start.AppendRotate(0.f, 0.f, 1.f, 0.f);
212 TransformOperations end;
213 end.AppendRotate(0.f, 0.f, 1.f, 90.f);
214 AddAnimatedTransformToLayer(child(), duration, start, end);
216 parent()->SetBounds(gfx::Size(350, 200));
218 gfx::Size bounds(100, 100);
219 child()->SetDrawsContent(true);
220 child()->draw_properties().screen_space_transform_is_animating = true;
221 child()->SetPosition(gfx::PointF(150.f, 50.f));
222 child()->SetBounds(bounds);
224 gfx::BoxF box;
225 bool success = LayerUtils::GetAnimationBounds(*child(), &box);
226 EXPECT_TRUE(success);
227 float diag = diagonal(bounds.width(), bounds.height());
228 gfx::BoxF expected(150.f + 0.5f * (bounds.width() - diag),
229 50.f + 0.5f * (bounds.height() - diag),
230 0.f,
231 diag,
232 diag,
233 0.f);
234 EXPECT_BOXF_EQ(expected, box);
237 TEST_F(LayerUtilsGetAnimationBoundsTest, MismatchedTransforms) {
238 double duration = 1.0;
240 TransformOperations start;
241 start.AppendTranslate(5, 6, 7);
242 TransformOperations end;
243 end.AppendRotate(0.f, 0.f, 1.f, 90.f);
244 AddAnimatedTransformToLayer(child(), duration, start, end);
246 parent()->SetBounds(gfx::Size(350, 200));
248 gfx::Size bounds(100, 100);
249 child()->SetDrawsContent(true);
250 child()->draw_properties().screen_space_transform_is_animating = true;
251 child()->SetPosition(gfx::PointF(150.f, 50.f));
252 child()->SetBounds(bounds);
254 gfx::BoxF box;
255 bool success = LayerUtils::GetAnimationBounds(*child(), &box);
256 EXPECT_FALSE(success);
259 } // namespace
260 } // namespace cc