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/geometry/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_EQ(static_cast<LayerAnimationElement::AnimatableProperties
>(
32 LayerAnimationElement::UNKNOWN
),
33 sequence
.properties());
34 EXPECT_FALSE(sequence
.HasConflictingProperty(LayerAnimationElement::UNKNOWN
));
37 // Check that the sequences progresses the delegate as expected when it contains
38 // a single non-threaded element.
39 TEST(LayerAnimationSequenceTest
, SingleElement
) {
40 LayerAnimationSequence sequence
;
41 TestLayerAnimationDelegate delegate
;
45 base::TimeTicks start_time
;
46 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
48 LayerAnimationElement::CreateBrightnessElement(target
, delta
));
50 for (int i
= 0; i
< 2; ++i
) {
52 sequence
.set_start_time(start_time
);
53 delegate
.SetBrightnessFromAnimation(start
);
54 sequence
.Start(&delegate
);
55 sequence
.Progress(start_time
, &delegate
);
56 EXPECT_FLOAT_EQ(start
, delegate
.GetBrightnessForAnimation());
57 sequence
.Progress(start_time
+ base::TimeDelta::FromMilliseconds(500),
59 EXPECT_FLOAT_EQ(middle
, delegate
.GetBrightnessForAnimation());
60 EXPECT_TRUE(sequence
.IsFinished(start_time
+ delta
));
61 sequence
.Progress(start_time
+ base::TimeDelta::FromMilliseconds(1000),
63 EXPECT_FLOAT_EQ(target
, delegate
.GetBrightnessForAnimation());
66 EXPECT_EQ(static_cast<LayerAnimationElement::AnimatableProperties
>(
67 LayerAnimationElement::BRIGHTNESS
),
68 sequence
.properties());
71 // Check that the sequences progresses the delegate as expected when it contains
72 // a single threaded element.
73 TEST(LayerAnimationSequenceTest
, SingleThreadedElement
) {
74 LayerAnimationSequence sequence
;
75 TestLayerAnimationDelegate delegate
;
79 base::TimeTicks start_time
;
80 base::TimeTicks effective_start
;
81 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
83 LayerAnimationElement::CreateOpacityElement(target
, delta
));
85 for (int i
= 0; i
< 2; ++i
) {
86 int starting_group_id
= 1;
87 sequence
.set_animation_group_id(starting_group_id
);
88 start_time
= effective_start
+ delta
;
89 sequence
.set_start_time(start_time
);
90 delegate
.SetOpacityFromAnimation(start
);
91 sequence
.Start(&delegate
);
92 sequence
.Progress(start_time
, &delegate
);
93 EXPECT_FLOAT_EQ(start
, sequence
.last_progressed_fraction());
94 effective_start
= start_time
+ delta
;
95 sequence
.OnThreadedAnimationStarted(cc::AnimationEvent(
96 cc::AnimationEvent::STARTED
, 0, sequence
.animation_group_id(),
97 cc::Animation::OPACITY
, effective_start
));
98 sequence
.Progress(effective_start
+ delta
/2, &delegate
);
99 EXPECT_FLOAT_EQ(middle
, sequence
.last_progressed_fraction());
100 EXPECT_TRUE(sequence
.IsFinished(effective_start
+ delta
));
101 sequence
.Progress(effective_start
+ delta
, &delegate
);
102 EXPECT_FLOAT_EQ(target
, sequence
.last_progressed_fraction());
103 EXPECT_FLOAT_EQ(target
, delegate
.GetOpacityForAnimation());
106 EXPECT_EQ(static_cast<LayerAnimationElement::AnimatableProperties
>(
107 LayerAnimationElement::OPACITY
),
108 sequence
.properties());
111 // Check that the sequences progresses the delegate as expected when it contains
112 // multiple elements. Note, see the layer animator tests for cyclic sequences.
113 TEST(LayerAnimationSequenceTest
, MultipleElement
) {
114 LayerAnimationSequence sequence
;
115 TestLayerAnimationDelegate delegate
;
116 float start_opacity
= 0.0f
;
117 float target_opacity
= 1.0f
;
118 base::TimeTicks start_time
;
119 base::TimeTicks opacity_effective_start
;
120 base::TimeTicks transform_effective_start
;
121 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
123 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
));
125 // Pause bounds for a second.
126 sequence
.AddElement(LayerAnimationElement::CreatePauseElement(
127 LayerAnimationElement::BOUNDS
, delta
));
129 gfx::Transform start_transform
, target_transform
, middle_transform
;
130 start_transform
.Rotate(-30.0);
131 target_transform
.Rotate(30.0);
134 LayerAnimationElement::CreateTransformElement(target_transform
, delta
));
136 for (int i
= 0; i
< 2; ++i
) {
137 int starting_group_id
= 1;
138 sequence
.set_animation_group_id(starting_group_id
);
139 start_time
= opacity_effective_start
+ 4 * delta
;
140 sequence
.set_start_time(start_time
);
141 delegate
.SetOpacityFromAnimation(start_opacity
);
142 delegate
.SetTransformFromAnimation(start_transform
);
144 sequence
.Start(&delegate
);
145 sequence
.Progress(start_time
, &delegate
);
146 EXPECT_FLOAT_EQ(0.0, sequence
.last_progressed_fraction());
147 opacity_effective_start
= start_time
+ delta
;
148 EXPECT_EQ(starting_group_id
, sequence
.animation_group_id());
149 sequence
.OnThreadedAnimationStarted(cc::AnimationEvent(
150 cc::AnimationEvent::STARTED
, 0, sequence
.animation_group_id(),
151 cc::Animation::OPACITY
, opacity_effective_start
));
152 sequence
.Progress(opacity_effective_start
+ delta
/2, &delegate
);
153 EXPECT_FLOAT_EQ(0.5, sequence
.last_progressed_fraction());
154 sequence
.Progress(opacity_effective_start
+ delta
, &delegate
);
155 EXPECT_FLOAT_EQ(target_opacity
, delegate
.GetOpacityForAnimation());
157 // Now at the start of the pause.
158 EXPECT_FLOAT_EQ(0.0, sequence
.last_progressed_fraction());
159 TestLayerAnimationDelegate copy
= delegate
;
161 // In the middle of the pause -- nothing should have changed.
162 sequence
.Progress(opacity_effective_start
+ delta
+ delta
/2,
164 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(),
165 copy
.GetBoundsForAnimation());
166 CheckApproximatelyEqual(delegate
.GetTransformForAnimation(),
167 copy
.GetTransformForAnimation());
168 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(),
169 copy
.GetOpacityForAnimation());
171 sequence
.Progress(opacity_effective_start
+ 2 * delta
, &delegate
);
172 CheckApproximatelyEqual(start_transform
,
173 delegate
.GetTransformForAnimation());
174 EXPECT_FLOAT_EQ(0.0, sequence
.last_progressed_fraction());
175 transform_effective_start
= opacity_effective_start
+ 3 * delta
;
176 EXPECT_NE(starting_group_id
, sequence
.animation_group_id());
177 sequence
.OnThreadedAnimationStarted(cc::AnimationEvent(
178 cc::AnimationEvent::STARTED
, 0, sequence
.animation_group_id(),
179 cc::Animation::TRANSFORM
, transform_effective_start
));
180 sequence
.Progress(transform_effective_start
+ delta
/2, &delegate
);
181 EXPECT_FLOAT_EQ(0.5, sequence
.last_progressed_fraction());
182 EXPECT_TRUE(sequence
.IsFinished(transform_effective_start
+ delta
));
183 sequence
.Progress(transform_effective_start
+ delta
, &delegate
);
184 CheckApproximatelyEqual(target_transform
,
185 delegate
.GetTransformForAnimation());
189 static_cast<LayerAnimationElement::AnimatableProperties
>(
190 LayerAnimationElement::OPACITY
| LayerAnimationElement::TRANSFORM
|
191 LayerAnimationElement::BOUNDS
),
192 sequence
.properties());
195 // Check that a sequence can still be aborted if it has cycled many times.
196 TEST(LayerAnimationSequenceTest
, AbortingCyclicSequence
) {
197 LayerAnimationSequence sequence
;
198 TestLayerAnimationDelegate delegate
;
199 float start_brightness
= 0.0f
;
200 float target_brightness
= 1.0f
;
201 base::TimeTicks start_time
;
202 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
204 LayerAnimationElement::CreateBrightnessElement(target_brightness
, delta
));
207 LayerAnimationElement::CreateBrightnessElement(start_brightness
, delta
));
209 sequence
.set_is_cyclic(true);
211 delegate
.SetBrightnessFromAnimation(start_brightness
);
214 sequence
.set_start_time(start_time
);
215 sequence
.Start(&delegate
);
216 sequence
.Progress(start_time
+ base::TimeDelta::FromMilliseconds(101000),
218 EXPECT_FLOAT_EQ(target_brightness
, delegate
.GetBrightnessForAnimation());
219 sequence
.Abort(&delegate
);
221 // Should be able to reuse the sequence after aborting.
222 delegate
.SetBrightnessFromAnimation(start_brightness
);
223 start_time
+= base::TimeDelta::FromMilliseconds(101000);
224 sequence
.set_start_time(start_time
);
225 sequence
.Progress(start_time
+ base::TimeDelta::FromMilliseconds(100000),
227 EXPECT_FLOAT_EQ(start_brightness
, delegate
.GetBrightnessForAnimation());
230 // Check that a sequence can be 'fast-forwarded' to the end and the target set.
231 // Also check that this has no effect if the sequence is cyclic.
232 TEST(LayerAnimationSequenceTest
, SetTarget
) {
233 LayerAnimationSequence sequence
;
234 TestLayerAnimationDelegate delegate
;
235 float start_opacity
= 0.0f
;
236 float target_opacity
= 1.0f
;
237 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
239 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
));
241 LayerAnimationElement::TargetValue
target_value(&delegate
);
242 target_value
.opacity
= start_opacity
;
243 sequence
.GetTargetValue(&target_value
);
244 EXPECT_FLOAT_EQ(target_opacity
, target_value
.opacity
);
246 sequence
.set_is_cyclic(true);
247 target_value
.opacity
= start_opacity
;
248 sequence
.GetTargetValue(&target_value
);
249 EXPECT_FLOAT_EQ(start_opacity
, target_value
.opacity
);
252 TEST(LayerAnimationSequenceTest
, AddObserver
) {
253 base::TimeTicks start_time
;
254 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
255 LayerAnimationSequence sequence
;
257 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
258 for (int i
= 0; i
< 2; ++i
) {
260 sequence
.set_start_time(start_time
);
261 TestLayerAnimationObserver observer
;
262 TestLayerAnimationDelegate delegate
;
263 sequence
.AddObserver(&observer
);
264 EXPECT_TRUE(!observer
.last_ended_sequence());
265 sequence
.Progress(start_time
+ delta
, &delegate
);
266 EXPECT_EQ(observer
.last_ended_sequence(), &sequence
);
267 sequence
.RemoveObserver(&observer
);