Move VISUAL_STATE promise to activation
[chromium-blink-merge.git] / ui / compositor / layer_animation_sequence_unittest.cc
blobc416fabc7c1ac8a74e9fcb2e6f919b2358b987e6
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"
20 namespace ui {
22 namespace {
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;
42 float start = 0.0f;
43 float middle = 0.5f;
44 float target = 1.0f;
45 base::TimeTicks start_time;
46 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
47 sequence.AddElement(
48 LayerAnimationElement::CreateBrightnessElement(target, delta));
50 for (int i = 0; i < 2; ++i) {
51 start_time += delta;
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),
58 &delegate);
59 EXPECT_FLOAT_EQ(middle, delegate.GetBrightnessForAnimation());
60 EXPECT_TRUE(sequence.IsFinished(start_time + delta));
61 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000),
62 &delegate);
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;
76 float start = 0.0f;
77 float middle = 0.5f;
78 float target = 1.0f;
79 base::TimeTicks start_time;
80 base::TimeTicks effective_start;
81 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
82 sequence.AddElement(
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);
122 sequence.AddElement(
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);
133 sequence.AddElement(
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,
163 &delegate);
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());
188 EXPECT_EQ(
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);
203 sequence.AddElement(
204 LayerAnimationElement::CreateBrightnessElement(target_brightness, delta));
206 sequence.AddElement(
207 LayerAnimationElement::CreateBrightnessElement(start_brightness, delta));
209 sequence.set_is_cyclic(true);
211 delegate.SetBrightnessFromAnimation(start_brightness);
213 start_time += delta;
214 sequence.set_start_time(start_time);
215 sequence.Start(&delegate);
216 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(101000),
217 &delegate);
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),
226 &delegate);
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);
238 sequence.AddElement(
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;
256 sequence.AddElement(
257 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
258 for (int i = 0; i < 2; ++i) {
259 start_time += delta;
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);
271 } // namespace
273 } // namespace ui