1 // Copyright (c) 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.
5 #include "ui/compositor/layer_animation_element.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/compositor/layer_animation_delegate.h"
13 #include "ui/compositor/test/test_layer_animation_delegate.h"
14 #include "ui/compositor/test/test_utils.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/transform.h"
22 // Check that the transformation element progresses the delegate as expected and
23 // that the element can be reused after it completes.
24 TEST(LayerAnimationElementTest
, TransformElement
) {
25 TestLayerAnimationDelegate delegate
;
26 gfx::Transform start_transform
, target_transform
, middle_transform
;
27 start_transform
.Rotate(-30.0);
28 target_transform
.Rotate(30.0);
29 base::TimeTicks start_time
;
30 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
32 scoped_ptr
<LayerAnimationElement
> element(
33 LayerAnimationElement::CreateTransformElement(target_transform
, delta
));
35 for (int i
= 0; i
< 2; ++i
) {
37 element
->set_start_time(start_time
);
38 delegate
.SetTransformFromAnimation(start_transform
);
39 element
->Progress(start_time
, &delegate
);
40 CheckApproximatelyEqual(start_transform
,
41 delegate
.GetTransformForAnimation());
42 element
->Progress(start_time
+ delta
/2, &delegate
);
43 CheckApproximatelyEqual(middle_transform
,
44 delegate
.GetTransformForAnimation());
45 element
->Progress(start_time
+ delta
, &delegate
);
46 CheckApproximatelyEqual(target_transform
,
47 delegate
.GetTransformForAnimation());
50 LayerAnimationElement::TargetValue
target_value(&delegate
);
51 element
->GetTargetValue(&target_value
);
52 CheckApproximatelyEqual(target_transform
, target_value
.transform
);
54 base::TimeDelta element_duration
;
55 EXPECT_TRUE(element
->IsFinished(start_time
+ delta
, &element_duration
));
56 EXPECT_EQ(delta
, element_duration
);
59 // Check that the bounds element progresses the delegate as expected and
60 // that the element can be reused after it completes.
61 TEST(LayerAnimationElementTest
, BoundsElement
) {
62 TestLayerAnimationDelegate delegate
;
63 gfx::Rect start
, target
, middle
;
64 start
= target
= middle
= gfx::Rect(0, 0, 50, 50);
67 base::TimeTicks start_time
;
68 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
70 scoped_ptr
<LayerAnimationElement
> element(
71 LayerAnimationElement::CreateBoundsElement(target
, delta
));
73 for (int i
= 0; i
< 2; ++i
) {
75 element
->set_start_time(start_time
);
76 delegate
.SetBoundsFromAnimation(start
);
77 element
->Progress(start_time
, &delegate
);
78 CheckApproximatelyEqual(start
, delegate
.GetBoundsForAnimation());
79 element
->Progress(start_time
+ delta
/2, &delegate
);
80 CheckApproximatelyEqual(middle
, delegate
.GetBoundsForAnimation());
81 element
->Progress(start_time
+ delta
, &delegate
);
82 CheckApproximatelyEqual(target
, delegate
.GetBoundsForAnimation());
85 LayerAnimationElement::TargetValue
target_value(&delegate
);
86 element
->GetTargetValue(&target_value
);
87 CheckApproximatelyEqual(target
, target_value
.bounds
);
89 base::TimeDelta element_duration
;
90 EXPECT_TRUE(element
->IsFinished(start_time
+ delta
, &element_duration
));
91 EXPECT_EQ(delta
, element_duration
);
94 // Check that the opacity element progresses the delegate as expected and
95 // that the element can be reused after it completes.
96 TEST(LayerAnimationElementTest
, OpacityElement
) {
97 TestLayerAnimationDelegate delegate
;
101 base::TimeTicks start_time
;
102 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
103 scoped_ptr
<LayerAnimationElement
> element(
104 LayerAnimationElement::CreateOpacityElement(target
, delta
));
106 for (int i
= 0; i
< 2; ++i
) {
108 element
->set_start_time(start_time
);
109 delegate
.SetOpacityFromAnimation(start
);
110 element
->Progress(start_time
, &delegate
);
111 EXPECT_FLOAT_EQ(start
, delegate
.GetOpacityForAnimation());
112 element
->Progress(start_time
+ delta
/2, &delegate
);
113 EXPECT_FLOAT_EQ(middle
, delegate
.GetOpacityForAnimation());
114 element
->Progress(start_time
+ delta
, &delegate
);
115 EXPECT_FLOAT_EQ(target
, delegate
.GetOpacityForAnimation());
118 LayerAnimationElement::TargetValue
target_value(&delegate
);
119 element
->GetTargetValue(&target_value
);
120 EXPECT_FLOAT_EQ(target
, target_value
.opacity
);
122 base::TimeDelta element_duration
;
123 EXPECT_TRUE(element
->IsFinished(start_time
+ delta
, &element_duration
));
124 EXPECT_EQ(delta
, element_duration
);
127 // Check that the visibility element progresses the delegate as expected and
128 // that the element can be reused after it completes.
129 TEST(LayerAnimationElementTest
, VisibilityElement
) {
130 TestLayerAnimationDelegate delegate
;
133 base::TimeTicks start_time
;
134 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
135 scoped_ptr
<LayerAnimationElement
> element(
136 LayerAnimationElement::CreateVisibilityElement(target
, delta
));
138 for (int i
= 0; i
< 2; ++i
) {
140 element
->set_start_time(start_time
);
141 delegate
.SetVisibilityFromAnimation(start
);
142 element
->Progress(start_time
, &delegate
);
143 EXPECT_TRUE(delegate
.GetVisibilityForAnimation());
144 element
->Progress(start_time
+ delta
/2, &delegate
);
145 EXPECT_TRUE(delegate
.GetVisibilityForAnimation());
146 element
->Progress(start_time
+ delta
, &delegate
);
147 EXPECT_FALSE(delegate
.GetVisibilityForAnimation());
150 LayerAnimationElement::TargetValue
target_value(&delegate
);
151 element
->GetTargetValue(&target_value
);
152 EXPECT_FALSE(target_value
.visibility
);
154 base::TimeDelta element_duration
;
155 EXPECT_TRUE(element
->IsFinished(start_time
+ delta
, &element_duration
));
156 EXPECT_EQ(delta
, element_duration
);
159 // Check that the Brightness element progresses the delegate as expected and
160 // that the element can be reused after it completes.
161 TEST(LayerAnimationElementTest
, BrightnessElement
) {
162 TestLayerAnimationDelegate delegate
;
166 base::TimeTicks start_time
;
167 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
168 scoped_ptr
<LayerAnimationElement
> element(
169 LayerAnimationElement::CreateBrightnessElement(target
, delta
));
171 for (int i
= 0; i
< 2; ++i
) {
173 element
->set_start_time(start_time
);
174 delegate
.SetBrightnessFromAnimation(start
);
175 element
->Progress(start_time
, &delegate
);
176 EXPECT_FLOAT_EQ(start
, delegate
.GetBrightnessForAnimation());
177 element
->Progress(start_time
+ delta
/2, &delegate
);
178 EXPECT_FLOAT_EQ(middle
, delegate
.GetBrightnessForAnimation());
179 element
->Progress(start_time
+ delta
, &delegate
);
180 EXPECT_FLOAT_EQ(target
, delegate
.GetBrightnessForAnimation());
183 LayerAnimationElement::TargetValue
target_value(&delegate
);
184 element
->GetTargetValue(&target_value
);
185 EXPECT_FLOAT_EQ(target
, target_value
.brightness
);
187 base::TimeDelta element_duration
;
188 EXPECT_TRUE(element
->IsFinished(start_time
+ delta
, &element_duration
));
189 EXPECT_EQ(delta
, element_duration
);
192 // Check that the Grayscale element progresses the delegate as expected and
193 // that the element can be reused after it completes.
194 TEST(LayerAnimationElementTest
, GrayscaleElement
) {
195 TestLayerAnimationDelegate delegate
;
199 base::TimeTicks start_time
;
200 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
201 scoped_ptr
<LayerAnimationElement
> element(
202 LayerAnimationElement::CreateGrayscaleElement(target
, delta
));
204 for (int i
= 0; i
< 2; ++i
) {
206 element
->set_start_time(start_time
);
207 delegate
.SetGrayscaleFromAnimation(start
);
208 element
->Progress(start_time
, &delegate
);
209 EXPECT_FLOAT_EQ(start
, delegate
.GetGrayscaleForAnimation());
210 element
->Progress(start_time
+ delta
/2, &delegate
);
211 EXPECT_FLOAT_EQ(middle
, delegate
.GetGrayscaleForAnimation());
212 element
->Progress(start_time
+ delta
, &delegate
);
213 EXPECT_FLOAT_EQ(target
, delegate
.GetGrayscaleForAnimation());
216 LayerAnimationElement::TargetValue
target_value(&delegate
);
217 element
->GetTargetValue(&target_value
);
218 EXPECT_FLOAT_EQ(target
, target_value
.grayscale
);
220 base::TimeDelta element_duration
;
221 EXPECT_TRUE(element
->IsFinished(start_time
+ delta
, &element_duration
));
222 EXPECT_EQ(delta
, element_duration
);
225 // Check that the pause element progresses the delegate as expected and
226 // that the element can be reused after it completes.
227 TEST(LayerAnimationElementTest
, PauseElement
) {
228 LayerAnimationElement::AnimatableProperties properties
;
229 properties
.insert(LayerAnimationElement::TRANSFORM
);
230 properties
.insert(LayerAnimationElement::BOUNDS
);
231 properties
.insert(LayerAnimationElement::OPACITY
);
232 properties
.insert(LayerAnimationElement::BRIGHTNESS
);
233 properties
.insert(LayerAnimationElement::GRAYSCALE
);
234 base::TimeTicks start_time
;
235 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
237 scoped_ptr
<LayerAnimationElement
> element(
238 LayerAnimationElement::CreatePauseElement(properties
, delta
));
240 TestLayerAnimationDelegate delegate
;
241 TestLayerAnimationDelegate copy
= delegate
;
244 element
->set_start_time(start_time
);
245 element
->Progress(start_time
+ delta
, &delegate
);
247 // Nothing should have changed.
248 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(),
249 copy
.GetBoundsForAnimation());
250 CheckApproximatelyEqual(delegate
.GetTransformForAnimation(),
251 copy
.GetTransformForAnimation());
252 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(),
253 copy
.GetOpacityForAnimation());
254 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(),
255 copy
.GetBrightnessForAnimation());
256 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(),
257 copy
.GetGrayscaleForAnimation());
259 // Pause should last for |delta|.
260 base::TimeDelta element_duration
;
261 EXPECT_TRUE(element
->IsFinished(start_time
+ delta
, &element_duration
));
262 EXPECT_EQ(delta
, element_duration
);