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_sequence.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/compositor/layer_animation_delegate.h"
13 #include "ui/compositor/layer_animation_element.h"
14 #include "ui/compositor/test/test_layer_animation_delegate.h"
15 #include "ui/compositor/test/test_layer_animation_observer.h"
16 #include "ui/compositor/test/test_utils.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/transform.h"
24 // Check that the sequence behaves sanely when it contains no elements.
25 TEST(LayerAnimationSequenceTest
, NoElement
) {
26 LayerAnimationSequence sequence
;
27 base::TimeTicks start_time
;
28 start_time
+= base::TimeDelta::FromSeconds(1);
29 sequence
.set_start_time(start_time
);
30 EXPECT_TRUE(sequence
.IsFinished(start_time
));
31 EXPECT_TRUE(sequence
.properties().size() == 0);
32 LayerAnimationElement::AnimatableProperties properties
;
33 EXPECT_FALSE(sequence
.HasConflictingProperty(properties
));
36 // Check that the sequences progresses the delegate as expected when it contains
37 // a single non-threaded element.
38 TEST(LayerAnimationSequenceTest
, SingleElement
) {
39 LayerAnimationSequence sequence
;
40 TestLayerAnimationDelegate delegate
;
44 base::TimeTicks start_time
;
45 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
47 LayerAnimationElement::CreateBrightnessElement(target
, delta
));
49 for (int i
= 0; i
< 2; ++i
) {
51 sequence
.set_start_time(start_time
);
52 delegate
.SetBrightnessFromAnimation(start
);
53 sequence
.Start(&delegate
);
54 sequence
.Progress(start_time
, &delegate
);
55 EXPECT_FLOAT_EQ(start
, delegate
.GetBrightnessForAnimation());
56 sequence
.Progress(start_time
+ base::TimeDelta::FromMilliseconds(500),
58 EXPECT_FLOAT_EQ(middle
, delegate
.GetBrightnessForAnimation());
59 EXPECT_TRUE(sequence
.IsFinished(start_time
+ delta
));
60 sequence
.Progress(start_time
+ base::TimeDelta::FromMilliseconds(1000),
62 EXPECT_FLOAT_EQ(target
, delegate
.GetBrightnessForAnimation());
65 EXPECT_TRUE(sequence
.properties().size() == 1);
66 EXPECT_TRUE(sequence
.properties().find(LayerAnimationElement::BRIGHTNESS
) !=
67 sequence
.properties().end());
70 // Check that the sequences progresses the delegate as expected when it contains
71 // a single threaded element.
72 TEST(LayerAnimationSequenceTest
, SingleThreadedElement
) {
73 LayerAnimationSequence sequence
;
74 TestLayerAnimationDelegate delegate
;
78 base::TimeTicks start_time
;
79 base::TimeTicks effective_start
;
80 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
82 LayerAnimationElement::CreateOpacityElement(target
, delta
));
84 for (int i
= 0; i
< 2; ++i
) {
85 int starting_group_id
= 1;
86 sequence
.set_animation_group_id(starting_group_id
);
87 start_time
= effective_start
+ delta
;
88 sequence
.set_start_time(start_time
);
89 delegate
.SetOpacityFromAnimation(start
);
90 sequence
.Start(&delegate
);
91 sequence
.Progress(start_time
, &delegate
);
92 EXPECT_FLOAT_EQ(start
, sequence
.last_progressed_fraction());
93 effective_start
= start_time
+ delta
;
94 sequence
.OnThreadedAnimationStarted(cc::AnimationEvent(
95 cc::AnimationEvent::Started
,
97 sequence
.animation_group_id(),
98 cc::Animation::Opacity
,
99 (effective_start
- base::TimeTicks()).InSecondsF()));
100 sequence
.Progress(effective_start
+ delta
/2, &delegate
);
101 EXPECT_FLOAT_EQ(middle
, sequence
.last_progressed_fraction());
102 EXPECT_TRUE(sequence
.IsFinished(effective_start
+ delta
));
103 sequence
.Progress(effective_start
+ delta
, &delegate
);
104 EXPECT_FLOAT_EQ(target
, sequence
.last_progressed_fraction());
105 EXPECT_FLOAT_EQ(target
, delegate
.GetOpacityForAnimation());
108 EXPECT_TRUE(sequence
.properties().size() == 1);
109 EXPECT_TRUE(sequence
.properties().find(LayerAnimationElement::OPACITY
) !=
110 sequence
.properties().end());
113 // Check that the sequences progresses the delegate as expected when it contains
114 // multiple elements. Note, see the layer animator tests for cyclic sequences.
115 TEST(LayerAnimationSequenceTest
, MultipleElement
) {
116 LayerAnimationSequence sequence
;
117 TestLayerAnimationDelegate delegate
;
118 float start_opacity
= 0.0f
;
119 float target_opacity
= 1.0f
;
120 base::TimeTicks start_time
;
121 base::TimeTicks opacity_effective_start
;
122 base::TimeTicks transform_effective_start
;
123 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
125 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
));
127 // Pause bounds for a second.
128 LayerAnimationElement::AnimatableProperties properties
;
129 properties
.insert(LayerAnimationElement::BOUNDS
);
132 LayerAnimationElement::CreatePauseElement(properties
, delta
));
134 gfx::Transform start_transform
, target_transform
, middle_transform
;
135 start_transform
.Rotate(-30.0);
136 target_transform
.Rotate(30.0);
139 LayerAnimationElement::CreateTransformElement(target_transform
, delta
));
141 for (int i
= 0; i
< 2; ++i
) {
142 int starting_group_id
= 1;
143 sequence
.set_animation_group_id(starting_group_id
);
144 start_time
= opacity_effective_start
+ 4 * delta
;
145 sequence
.set_start_time(start_time
);
146 delegate
.SetOpacityFromAnimation(start_opacity
);
147 delegate
.SetTransformFromAnimation(start_transform
);
149 sequence
.Start(&delegate
);
150 sequence
.Progress(start_time
, &delegate
);
151 EXPECT_FLOAT_EQ(0.0, sequence
.last_progressed_fraction());
152 opacity_effective_start
= start_time
+ delta
;
153 EXPECT_EQ(starting_group_id
, sequence
.animation_group_id());
154 sequence
.OnThreadedAnimationStarted(cc::AnimationEvent(
155 cc::AnimationEvent::Started
,
157 sequence
.animation_group_id(),
158 cc::Animation::Opacity
,
159 (opacity_effective_start
- base::TimeTicks()).InSecondsF()));
160 sequence
.Progress(opacity_effective_start
+ delta
/2, &delegate
);
161 EXPECT_FLOAT_EQ(0.5, sequence
.last_progressed_fraction());
162 sequence
.Progress(opacity_effective_start
+ delta
, &delegate
);
163 EXPECT_FLOAT_EQ(target_opacity
, delegate
.GetOpacityForAnimation());
165 // Now at the start of the pause.
166 EXPECT_FLOAT_EQ(0.0, sequence
.last_progressed_fraction());
167 TestLayerAnimationDelegate copy
= delegate
;
169 // In the middle of the pause -- nothing should have changed.
170 sequence
.Progress(opacity_effective_start
+ delta
+ delta
/2,
172 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(),
173 copy
.GetBoundsForAnimation());
174 CheckApproximatelyEqual(delegate
.GetTransformForAnimation(),
175 copy
.GetTransformForAnimation());
176 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(),
177 copy
.GetOpacityForAnimation());
179 sequence
.Progress(opacity_effective_start
+ 2 * delta
, &delegate
);
180 CheckApproximatelyEqual(start_transform
,
181 delegate
.GetTransformForAnimation());
182 EXPECT_FLOAT_EQ(0.0, sequence
.last_progressed_fraction());
183 transform_effective_start
= opacity_effective_start
+ 3 * delta
;
184 EXPECT_NE(starting_group_id
, sequence
.animation_group_id());
185 sequence
.OnThreadedAnimationStarted(cc::AnimationEvent(
186 cc::AnimationEvent::Started
,
188 sequence
.animation_group_id(),
189 cc::Animation::Transform
,
190 (transform_effective_start
- base::TimeTicks()).InSecondsF()));
191 sequence
.Progress(transform_effective_start
+ delta
/2, &delegate
);
192 EXPECT_FLOAT_EQ(0.5, sequence
.last_progressed_fraction());
193 EXPECT_TRUE(sequence
.IsFinished(transform_effective_start
+ delta
));
194 sequence
.Progress(transform_effective_start
+ delta
, &delegate
);
195 CheckApproximatelyEqual(target_transform
,
196 delegate
.GetTransformForAnimation());
199 EXPECT_TRUE(sequence
.properties().size() == 3);
200 EXPECT_TRUE(sequence
.properties().find(LayerAnimationElement::OPACITY
) !=
201 sequence
.properties().end());
202 EXPECT_TRUE(sequence
.properties().find(LayerAnimationElement::TRANSFORM
) !=
203 sequence
.properties().end());
204 EXPECT_TRUE(sequence
.properties().find(LayerAnimationElement::BOUNDS
) !=
205 sequence
.properties().end());
208 // Check that a sequence can still be aborted if it has cycled many times.
209 TEST(LayerAnimationSequenceTest
, AbortingCyclicSequence
) {
210 LayerAnimationSequence sequence
;
211 TestLayerAnimationDelegate delegate
;
212 float start_brightness
= 0.0f
;
213 float target_brightness
= 1.0f
;
214 base::TimeTicks start_time
;
215 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
217 LayerAnimationElement::CreateBrightnessElement(target_brightness
, delta
));
220 LayerAnimationElement::CreateBrightnessElement(start_brightness
, delta
));
222 sequence
.set_is_cyclic(true);
224 delegate
.SetBrightnessFromAnimation(start_brightness
);
227 sequence
.set_start_time(start_time
);
228 sequence
.Start(&delegate
);
229 sequence
.Progress(start_time
+ base::TimeDelta::FromMilliseconds(101000),
231 EXPECT_FLOAT_EQ(target_brightness
, delegate
.GetBrightnessForAnimation());
232 sequence
.Abort(&delegate
);
234 // Should be able to reuse the sequence after aborting.
235 delegate
.SetBrightnessFromAnimation(start_brightness
);
236 start_time
+= base::TimeDelta::FromMilliseconds(101000);
237 sequence
.set_start_time(start_time
);
238 sequence
.Progress(start_time
+ base::TimeDelta::FromMilliseconds(100000),
240 EXPECT_FLOAT_EQ(start_brightness
, delegate
.GetBrightnessForAnimation());
243 // Check that a sequence can be 'fast-forwarded' to the end and the target set.
244 // Also check that this has no effect if the sequence is cyclic.
245 TEST(LayerAnimationSequenceTest
, SetTarget
) {
246 LayerAnimationSequence sequence
;
247 TestLayerAnimationDelegate delegate
;
248 float start_opacity
= 0.0f
;
249 float target_opacity
= 1.0f
;
250 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
252 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
));
254 LayerAnimationElement::TargetValue
target_value(&delegate
);
255 target_value
.opacity
= start_opacity
;
256 sequence
.GetTargetValue(&target_value
);
257 EXPECT_FLOAT_EQ(target_opacity
, target_value
.opacity
);
259 sequence
.set_is_cyclic(true);
260 target_value
.opacity
= start_opacity
;
261 sequence
.GetTargetValue(&target_value
);
262 EXPECT_FLOAT_EQ(start_opacity
, target_value
.opacity
);
265 TEST(LayerAnimationSequenceTest
, AddObserver
) {
266 base::TimeTicks start_time
;
267 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
268 LayerAnimationSequence sequence
;
270 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
271 for (int i
= 0; i
< 2; ++i
) {
273 sequence
.set_start_time(start_time
);
274 TestLayerAnimationObserver observer
;
275 TestLayerAnimationDelegate delegate
;
276 sequence
.AddObserver(&observer
);
277 EXPECT_TRUE(!observer
.last_ended_sequence());
278 sequence
.Progress(start_time
+ delta
, &delegate
);
279 EXPECT_EQ(observer
.last_ended_sequence(), &sequence
);
280 sequence
.RemoveObserver(&observer
);