OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/compositor/layer_animation_sequence.h" | 5 #include "ui/compositor/layer_animation_sequence.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 base::TimeTicks start_time; | 27 base::TimeTicks start_time; |
28 start_time += base::TimeDelta::FromSeconds(1); | 28 start_time += base::TimeDelta::FromSeconds(1); |
29 sequence.set_start_time(start_time); | 29 sequence.set_start_time(start_time); |
30 EXPECT_TRUE(sequence.IsFinished(start_time)); | 30 EXPECT_TRUE(sequence.IsFinished(start_time)); |
31 EXPECT_TRUE(sequence.properties().size() == 0); | 31 EXPECT_TRUE(sequence.properties().size() == 0); |
32 LayerAnimationElement::AnimatableProperties properties; | 32 LayerAnimationElement::AnimatableProperties properties; |
33 EXPECT_FALSE(sequence.HasCommonProperty(properties)); | 33 EXPECT_FALSE(sequence.HasCommonProperty(properties)); |
34 } | 34 } |
35 | 35 |
36 // Check that the sequences progresses the delegate as expected when it contains | 36 // Check that the sequences progresses the delegate as expected when it contains |
37 // a single element. | 37 // a single non-threaded element. |
38 TEST(LayerAnimationSequenceTest, SingleElement) { | 38 TEST(LayerAnimationSequenceTest, SingleElement) { |
39 LayerAnimationSequence sequence; | 39 LayerAnimationSequence sequence; |
40 TestLayerAnimationDelegate delegate; | 40 TestLayerAnimationDelegate delegate; |
41 float start = 0.0f; | 41 float start = 0.0f; |
42 float middle = 0.5f; | 42 float middle = 0.5f; |
43 float target = 1.0f; | 43 float target = 1.0f; |
44 base::TimeTicks start_time; | 44 base::TimeTicks start_time; |
45 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 45 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
46 sequence.AddElement( | 46 sequence.AddElement( |
| 47 LayerAnimationElement::CreateBrightnessElement(target, delta)); |
| 48 |
| 49 for (int i = 0; i < 2; ++i) { |
| 50 start_time += delta; |
| 51 sequence.set_start_time(start_time); |
| 52 delegate.SetBrightnessFromAnimation(start); |
| 53 sequence.Progress(start_time, &delegate); |
| 54 EXPECT_FLOAT_EQ(start, delegate.GetBrightnessForAnimation()); |
| 55 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500), |
| 56 &delegate); |
| 57 EXPECT_FLOAT_EQ(middle, delegate.GetBrightnessForAnimation()); |
| 58 EXPECT_TRUE(sequence.IsFinished(start_time + delta)); |
| 59 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000), |
| 60 &delegate); |
| 61 EXPECT_FLOAT_EQ(target, delegate.GetBrightnessForAnimation()); |
| 62 } |
| 63 |
| 64 EXPECT_TRUE(sequence.properties().size() == 1); |
| 65 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BRIGHTNESS) != |
| 66 sequence.properties().end()); |
| 67 } |
| 68 |
| 69 // Check that the sequences progresses the delegate as expected when it contains |
| 70 // a single threaded element. |
| 71 TEST(LayerAnimationSequenceTest, SingleThreadedElement) { |
| 72 LayerAnimationSequence sequence; |
| 73 TestLayerAnimationDelegate delegate; |
| 74 float start = 0.0f; |
| 75 float middle = 0.5f; |
| 76 float target = 1.0f; |
| 77 base::TimeTicks start_time; |
| 78 base::TimeTicks effective_start; |
| 79 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 80 sequence.AddElement( |
47 LayerAnimationElement::CreateOpacityElement(target, delta)); | 81 LayerAnimationElement::CreateOpacityElement(target, delta)); |
48 | 82 |
49 for (int i = 0; i < 2; ++i) { | 83 for (int i = 0; i < 2; ++i) { |
50 start_time += delta; | 84 int group_id = 1; |
| 85 sequence.set_animation_group_id(group_id); |
| 86 start_time = effective_start + delta; |
51 sequence.set_start_time(start_time); | 87 sequence.set_start_time(start_time); |
52 delegate.SetOpacityFromAnimation(start); | 88 delegate.SetOpacityFromAnimation(start); |
53 sequence.Progress(start_time, &delegate); | 89 sequence.Progress(start_time, &delegate); |
54 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); | 90 EXPECT_FLOAT_EQ(start, sequence.last_progressed_fraction()); |
55 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500), | 91 effective_start = start_time + delta; |
56 &delegate); | 92 sequence.OnThreadedAnimationStarted(cc::AnimationEvent( |
57 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); | 93 cc::AnimationEvent::Started, |
58 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000), | 94 0, |
59 &delegate); | 95 group_id, |
| 96 cc::Animation::Opacity, |
| 97 (effective_start - base::TimeTicks()).InSecondsF())); |
| 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()); |
60 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); | 103 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); |
61 } | 104 } |
62 | 105 |
63 EXPECT_TRUE(sequence.properties().size() == 1); | 106 EXPECT_TRUE(sequence.properties().size() == 1); |
64 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | 107 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != |
65 sequence.properties().end()); | 108 sequence.properties().end()); |
66 EXPECT_TRUE(sequence.IsFinished(start_time + delta)); | |
67 } | 109 } |
68 | 110 |
69 // Check that the sequences progresses the delegate as expected when it contains | 111 // Check that the sequences progresses the delegate as expected when it contains |
70 // multiple elements. Note, see the layer animator tests for cyclic sequences. | 112 // multiple elements. Note, see the layer animator tests for cyclic sequences. |
71 TEST(LayerAnimationSequenceTest, MultipleElement) { | 113 TEST(LayerAnimationSequenceTest, MultipleElement) { |
72 LayerAnimationSequence sequence; | 114 LayerAnimationSequence sequence; |
73 TestLayerAnimationDelegate delegate; | 115 TestLayerAnimationDelegate delegate; |
74 float start_opacity = 0.0f; | 116 float start_opacity = 0.0f; |
75 float middle_opacity = 0.5f; | |
76 float target_opacity = 1.0f; | 117 float target_opacity = 1.0f; |
77 base::TimeTicks start_time; | 118 base::TimeTicks start_time; |
| 119 base::TimeTicks effective_start; |
78 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 120 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
79 sequence.AddElement( | 121 sequence.AddElement( |
80 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | 122 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); |
81 | 123 |
82 // Pause bounds for a second. | 124 // Pause bounds for a second. |
83 LayerAnimationElement::AnimatableProperties properties; | 125 LayerAnimationElement::AnimatableProperties properties; |
84 properties.insert(LayerAnimationElement::BOUNDS); | 126 properties.insert(LayerAnimationElement::BOUNDS); |
85 | 127 |
86 sequence.AddElement( | 128 sequence.AddElement( |
87 LayerAnimationElement::CreatePauseElement(properties, delta)); | 129 LayerAnimationElement::CreatePauseElement(properties, delta)); |
88 | 130 |
89 gfx::Transform start_transform, target_transform, middle_transform; | 131 gfx::Transform start_transform, target_transform, middle_transform; |
90 start_transform.Rotate(-30.0); | 132 start_transform.Rotate(-30.0); |
91 target_transform.Rotate(30.0); | 133 target_transform.Rotate(30.0); |
92 | 134 |
93 sequence.AddElement( | 135 sequence.AddElement( |
94 LayerAnimationElement::CreateTransformElement(target_transform, delta)); | 136 LayerAnimationElement::CreateTransformElement(target_transform, delta)); |
95 | 137 |
96 for (int i = 0; i < 2; ++i) { | 138 for (int i = 0; i < 2; ++i) { |
97 start_time += delta + delta + delta; | 139 int group_id = 1; |
| 140 sequence.set_animation_group_id(group_id); |
| 141 start_time = effective_start + 3 * delta; |
98 sequence.set_start_time(start_time); | 142 sequence.set_start_time(start_time); |
99 delegate.SetOpacityFromAnimation(start_opacity); | 143 delegate.SetOpacityFromAnimation(start_opacity); |
100 delegate.SetTransformFromAnimation(start_transform); | 144 delegate.SetTransformFromAnimation(start_transform); |
101 | 145 |
102 sequence.Progress(start_time, &delegate); | 146 sequence.Progress(start_time, &delegate); |
103 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | 147 EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction()); |
104 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500), | 148 effective_start = start_time + delta; |
105 &delegate); | 149 sequence.OnThreadedAnimationStarted(cc::AnimationEvent( |
106 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation()); | 150 cc::AnimationEvent::Started, |
107 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000), | 151 0, |
108 &delegate); | 152 group_id, |
| 153 cc::Animation::Opacity, |
| 154 (effective_start - base::TimeTicks()).InSecondsF())); |
| 155 sequence.Progress(effective_start + delta/2, &delegate); |
| 156 EXPECT_FLOAT_EQ(0.5, sequence.last_progressed_fraction()); |
| 157 sequence.Progress(effective_start + delta, &delegate); |
109 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | 158 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); |
| 159 |
| 160 // Now at the start of the pause. |
| 161 EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction()); |
110 TestLayerAnimationDelegate copy = delegate; | 162 TestLayerAnimationDelegate copy = delegate; |
111 | 163 |
112 // In the middle of the pause -- nothing should have changed. | 164 // In the middle of the pause -- nothing should have changed. |
113 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1500), | 165 sequence.Progress(effective_start + delta + delta/2, |
114 &delegate); | 166 &delegate); |
115 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), | 167 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), |
116 copy.GetBoundsForAnimation()); | 168 copy.GetBoundsForAnimation()); |
117 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), | 169 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), |
118 copy.GetTransformForAnimation()); | 170 copy.GetTransformForAnimation()); |
119 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | 171 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), |
120 copy.GetOpacityForAnimation()); | 172 copy.GetOpacityForAnimation()); |
121 | 173 |
122 | 174 |
123 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(2000), | 175 sequence.Progress(effective_start + 2 * delta, &delegate); |
124 &delegate); | |
125 CheckApproximatelyEqual(start_transform, | 176 CheckApproximatelyEqual(start_transform, |
126 delegate.GetTransformForAnimation()); | 177 delegate.GetTransformForAnimation()); |
127 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(2500), | 178 sequence.Progress(effective_start + 2 * delta + delta/2, &delegate); |
128 &delegate); | |
129 CheckApproximatelyEqual(middle_transform, | 179 CheckApproximatelyEqual(middle_transform, |
130 delegate.GetTransformForAnimation()); | 180 delegate.GetTransformForAnimation()); |
131 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(3000), | 181 EXPECT_TRUE(sequence.IsFinished(effective_start + 3 * delta)); |
132 &delegate); | 182 sequence.Progress(effective_start + 3 * delta, &delegate); |
133 CheckApproximatelyEqual(target_transform, | 183 CheckApproximatelyEqual(target_transform, |
134 delegate.GetTransformForAnimation()); | 184 delegate.GetTransformForAnimation()); |
135 } | 185 } |
136 | 186 |
137 EXPECT_TRUE(sequence.properties().size() == 3); | 187 EXPECT_TRUE(sequence.properties().size() == 3); |
138 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | 188 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != |
139 sequence.properties().end()); | 189 sequence.properties().end()); |
140 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != | 190 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != |
141 sequence.properties().end()); | 191 sequence.properties().end()); |
142 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != | 192 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != |
143 sequence.properties().end()); | 193 sequence.properties().end()); |
144 EXPECT_TRUE(sequence.IsFinished(start_time + delta + delta + delta)); | |
145 } | 194 } |
146 | 195 |
147 // Check that a sequence can still be aborted if it has cycled many times. | 196 // Check that a sequence can still be aborted if it has cycled many times. |
148 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { | 197 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { |
149 LayerAnimationSequence sequence; | 198 LayerAnimationSequence sequence; |
150 TestLayerAnimationDelegate delegate; | 199 TestLayerAnimationDelegate delegate; |
151 float start_opacity = 0.0f; | 200 float start_brightness = 0.0f; |
152 float target_opacity = 1.0f; | 201 float target_brightness = 1.0f; |
153 base::TimeTicks start_time; | 202 base::TimeTicks start_time; |
154 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 203 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
155 sequence.AddElement( | 204 sequence.AddElement( |
156 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | 205 LayerAnimationElement::CreateBrightnessElement(target_brightness, delta)); |
157 | 206 |
158 sequence.AddElement( | 207 sequence.AddElement( |
159 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); | 208 LayerAnimationElement::CreateBrightnessElement(start_brightness, delta)); |
160 | 209 |
161 sequence.set_is_cyclic(true); | 210 sequence.set_is_cyclic(true); |
162 | 211 |
163 delegate.SetOpacityFromAnimation(start_opacity); | 212 delegate.SetBrightnessFromAnimation(start_brightness); |
164 | 213 |
165 start_time += delta; | 214 start_time += delta; |
166 sequence.set_start_time(start_time); | 215 sequence.set_start_time(start_time); |
167 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(101000), | 216 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(101000), |
168 &delegate); | 217 &delegate); |
169 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | 218 EXPECT_FLOAT_EQ(target_brightness, delegate.GetBrightnessForAnimation()); |
170 sequence.Abort(); | 219 sequence.Abort(&delegate); |
171 | 220 |
172 // Should be able to reuse the sequence after aborting. | 221 // Should be able to reuse the sequence after aborting. |
173 delegate.SetOpacityFromAnimation(start_opacity); | 222 delegate.SetBrightnessFromAnimation(start_brightness); |
174 start_time += base::TimeDelta::FromMilliseconds(101000); | 223 start_time += base::TimeDelta::FromMilliseconds(101000); |
175 sequence.set_start_time(start_time); | 224 sequence.set_start_time(start_time); |
176 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(100000), | 225 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(100000), |
177 &delegate); | 226 &delegate); |
178 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | 227 EXPECT_FLOAT_EQ(start_brightness, delegate.GetBrightnessForAnimation()); |
179 } | 228 } |
180 | 229 |
181 // Check that a sequence can be 'fast-forwarded' to the end and the target set. | 230 // Check that a sequence can be 'fast-forwarded' to the end and the target set. |
182 // Also check that this has no effect if the sequence is cyclic. | 231 // Also check that this has no effect if the sequence is cyclic. |
183 TEST(LayerAnimationSequenceTest, SetTarget) { | 232 TEST(LayerAnimationSequenceTest, SetTarget) { |
184 LayerAnimationSequence sequence; | 233 LayerAnimationSequence sequence; |
185 TestLayerAnimationDelegate delegate; | 234 TestLayerAnimationDelegate delegate; |
186 float start_opacity = 0.0f; | 235 float start_opacity = 0.0f; |
187 float target_opacity = 1.0f; | 236 float target_opacity = 1.0f; |
188 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 237 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
189 sequence.AddElement( | 238 sequence.AddElement( |
190 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | 239 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); |
191 | 240 |
192 LayerAnimationElement::TargetValue target_value(&delegate); | 241 LayerAnimationElement::TargetValue target_value(&delegate); |
193 target_value.opacity = start_opacity; | 242 target_value.opacity = start_opacity; |
194 sequence.GetTargetValue(&target_value); | 243 sequence.GetTargetValue(&target_value); |
195 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity); | 244 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity); |
196 | 245 |
197 sequence.set_is_cyclic(true); | 246 sequence.set_is_cyclic(true); |
198 target_value.opacity = start_opacity; | 247 target_value.opacity = start_opacity; |
199 sequence.GetTargetValue(&target_value); | 248 sequence.GetTargetValue(&target_value); |
200 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity); | 249 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity); |
201 } | 250 } |
202 | 251 |
203 TEST(LayerAnimationSequenceTest, AddObserver) { | 252 TEST(LayerAnimationSequenceTest, AddObserver) { |
204 base::TimeTicks start_time; | 253 base::TimeTicks start_time; |
205 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 254 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
206 LayerAnimationSequence sequence; | 255 LayerAnimationSequence sequence; |
207 sequence.AddElement( | 256 sequence.AddElement( |
208 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | 257 LayerAnimationElement::CreateBrightnessElement(1.0f, delta)); |
209 for (int i = 0; i < 2; ++i) { | 258 for (int i = 0; i < 2; ++i) { |
210 start_time += delta; | 259 start_time += delta; |
211 sequence.set_start_time(start_time); | 260 sequence.set_start_time(start_time); |
212 TestLayerAnimationObserver observer; | 261 TestLayerAnimationObserver observer; |
213 TestLayerAnimationDelegate delegate; | 262 TestLayerAnimationDelegate delegate; |
214 sequence.AddObserver(&observer); | 263 sequence.AddObserver(&observer); |
215 EXPECT_TRUE(!observer.last_ended_sequence()); | 264 EXPECT_TRUE(!observer.last_ended_sequence()); |
216 sequence.Progress(start_time + delta, &delegate); | 265 sequence.Progress(start_time + delta, &delegate); |
217 EXPECT_EQ(observer.last_ended_sequence(), &sequence); | 266 EXPECT_EQ(observer.last_ended_sequence(), &sequence); |
218 sequence.RemoveObserver(&observer); | 267 sequence.RemoveObserver(&observer); |
219 } | 268 } |
220 } | 269 } |
221 | 270 |
222 } // namespace | 271 } // namespace |
223 | 272 |
224 } // namespace ui | 273 } // namespace ui |
OLD | NEW |