OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "ui/gfx/compositor/layer_animator.h" |
| 6 |
| 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/gfx/rect.h" |
| 13 #include "ui/gfx/transform.h" |
| 14 #include "ui/gfx/compositor/layer_animation_delegate.h" |
| 15 #include "ui/gfx/compositor/layer_animation_element.h" |
| 16 #include "ui/gfx/compositor/layer_animation_sequence.h" |
| 17 #include "ui/gfx/compositor/test_utils.h" |
| 18 #include "ui/gfx/compositor/test_layer_animation_delegate.h" |
| 19 |
| 20 namespace ui { |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Checks that setting a property on an implicit animator causes an animation to |
| 25 // happen. |
| 26 TEST(LayerAnimatorTest, ImplicitAnimation) { |
| 27 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateImplicitAnimator()); |
| 28 AnimationContainerElement* element = animator.get(); |
| 29 animator->set_disable_timer_for_test(true); |
| 30 TestLayerAnimationDelegate delegate; |
| 31 animator->SetDelegate(&delegate); |
| 32 base::TimeTicks now = base::TimeTicks::Now(); |
| 33 animator->SetOpacity(0.5); |
| 34 EXPECT_TRUE(animator->is_animating()); |
| 35 element->Step(now + base::TimeDelta::FromSeconds(1)); |
| 36 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); |
| 37 } |
| 38 |
| 39 // Checks that if the animator is a default animator, that implicit animations |
| 40 // are not started. |
| 41 TEST(LayerAnimatorTest, NoImplicitAnimation) { |
| 42 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 43 animator->set_disable_timer_for_test(true); |
| 44 TestLayerAnimationDelegate delegate; |
| 45 animator->SetDelegate(&delegate); |
| 46 base::TimeTicks now = base::TimeTicks::Now(); |
| 47 animator->SetOpacity(0.5); |
| 48 EXPECT_FALSE(animator->is_animating()); |
| 49 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); |
| 50 } |
| 51 |
| 52 // Checks that StopAnimatingProperty stops animation for that property, and also |
| 53 // skips the stopped animation to the end. |
| 54 TEST(LayerAnimatorTest, StopAnimatingProperty) { |
| 55 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateImplicitAnimator()); |
| 56 animator->set_disable_timer_for_test(true); |
| 57 TestLayerAnimationDelegate delegate; |
| 58 animator->SetDelegate(&delegate); |
| 59 base::TimeTicks now = base::TimeTicks::Now(); |
| 60 double target_opacity(0.5); |
| 61 gfx::Rect target_bounds(0, 0, 50, 50); |
| 62 animator->SetOpacity(target_opacity); |
| 63 animator->SetBounds(target_bounds); |
| 64 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY); |
| 65 EXPECT_TRUE(animator->is_animating()); |
| 66 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); |
| 67 animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS); |
| 68 EXPECT_FALSE(animator->is_animating()); |
| 69 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); |
| 70 } |
| 71 |
| 72 // Checks that multiple running animation for separate properties can be stopped |
| 73 // simultaneously and that all animations are advanced to their target values. |
| 74 TEST(LayerAnimatorTest, StopAnimating) { |
| 75 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateImplicitAnimator()); |
| 76 animator->set_disable_timer_for_test(true); |
| 77 TestLayerAnimationDelegate delegate; |
| 78 animator->SetDelegate(&delegate); |
| 79 base::TimeTicks now = base::TimeTicks::Now(); |
| 80 double target_opacity(0.5); |
| 81 gfx::Rect target_bounds(0, 0, 50, 50); |
| 82 animator->SetOpacity(target_opacity); |
| 83 animator->SetBounds(target_bounds); |
| 84 EXPECT_TRUE(animator->is_animating()); |
| 85 animator->StopAnimating(); |
| 86 EXPECT_FALSE(animator->is_animating()); |
| 87 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); |
| 88 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); |
| 89 } |
| 90 |
| 91 // Schedule an animation that can run immediately. This is the trivial case and |
| 92 // should result in the animation being started immediately. |
| 93 TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) { |
| 94 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 95 AnimationContainerElement* element = animator.get(); |
| 96 animator->set_disable_timer_for_test(true); |
| 97 TestLayerAnimationDelegate delegate; |
| 98 animator->SetDelegate(&delegate); |
| 99 |
| 100 double start_opacity(0.0); |
| 101 double middle_opacity(0.5); |
| 102 double target_opacity(1.0); |
| 103 |
| 104 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 105 |
| 106 delegate.SetOpacityFromAnimation(start_opacity); |
| 107 |
| 108 animator->ScheduleAnimation( |
| 109 new LayerAnimationSequence( |
| 110 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 111 |
| 112 EXPECT_TRUE(animator->is_animating()); |
| 113 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 114 |
| 115 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 116 |
| 117 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); |
| 118 |
| 119 EXPECT_TRUE(animator->is_animating()); |
| 120 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 121 |
| 122 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 123 |
| 124 EXPECT_FALSE(animator->is_animating()); |
| 125 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 126 } |
| 127 |
| 128 // Schedule two animations on separate properties. Both animations should |
| 129 // start immediately and should progress in lock step. |
| 130 TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) { |
| 131 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 132 AnimationContainerElement* element = animator.get(); |
| 133 animator->set_disable_timer_for_test(true); |
| 134 TestLayerAnimationDelegate delegate; |
| 135 animator->SetDelegate(&delegate); |
| 136 |
| 137 double start_opacity(0.0); |
| 138 double middle_opacity(0.5); |
| 139 double target_opacity(1.0); |
| 140 |
| 141 gfx::Rect start_bounds, target_bounds, middle_bounds; |
| 142 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); |
| 143 start_bounds.set_x(-90); |
| 144 target_bounds.set_x(90); |
| 145 |
| 146 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 147 |
| 148 delegate.SetOpacityFromAnimation(start_opacity); |
| 149 delegate.SetBoundsFromAnimation(start_bounds); |
| 150 |
| 151 animator->ScheduleAnimation( |
| 152 new LayerAnimationSequence( |
| 153 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 154 |
| 155 animator->ScheduleAnimation( |
| 156 new LayerAnimationSequence( |
| 157 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); |
| 158 |
| 159 EXPECT_TRUE(animator->is_animating()); |
| 160 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 161 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); |
| 162 |
| 163 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 164 |
| 165 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); |
| 166 |
| 167 EXPECT_TRUE(animator->is_animating()); |
| 168 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 169 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds); |
| 170 |
| 171 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 172 |
| 173 EXPECT_FALSE(animator->is_animating()); |
| 174 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 175 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); |
| 176 } |
| 177 |
| 178 // Schedule two animations on the same property. In this case, the two |
| 179 // animations should run one after another. |
| 180 TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) { |
| 181 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 182 AnimationContainerElement* element = animator.get(); |
| 183 animator->set_disable_timer_for_test(true); |
| 184 TestLayerAnimationDelegate delegate; |
| 185 animator->SetDelegate(&delegate); |
| 186 |
| 187 double start_opacity(0.0); |
| 188 double middle_opacity(0.5); |
| 189 double target_opacity(1.0); |
| 190 |
| 191 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 192 |
| 193 delegate.SetOpacityFromAnimation(start_opacity); |
| 194 |
| 195 animator->ScheduleAnimation( |
| 196 new LayerAnimationSequence( |
| 197 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 198 |
| 199 animator->ScheduleAnimation( |
| 200 new LayerAnimationSequence( |
| 201 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); |
| 202 |
| 203 EXPECT_TRUE(animator->is_animating()); |
| 204 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 205 |
| 206 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 207 |
| 208 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); |
| 209 |
| 210 EXPECT_TRUE(animator->is_animating()); |
| 211 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 212 |
| 213 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 214 |
| 215 EXPECT_TRUE(animator->is_animating()); |
| 216 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 217 |
| 218 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); |
| 219 |
| 220 EXPECT_TRUE(animator->is_animating()); |
| 221 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 222 |
| 223 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); |
| 224 |
| 225 EXPECT_FALSE(animator->is_animating()); |
| 226 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 227 } |
| 228 |
| 229 // Schedule [{o}, {o,b}, {b}] and ensure that {b} doesn't run right away. That |
| 230 // is, ensure that all animations targetting a particular property are run in |
| 231 // order. |
| 232 TEST(LayerAnimatorTest, ScheduleBlockedAnimation) { |
| 233 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 234 AnimationContainerElement* element = animator.get(); |
| 235 animator->set_disable_timer_for_test(true); |
| 236 TestLayerAnimationDelegate delegate; |
| 237 animator->SetDelegate(&delegate); |
| 238 |
| 239 double start_opacity(0.0); |
| 240 double middle_opacity(0.5); |
| 241 double target_opacity(1.0); |
| 242 |
| 243 gfx::Rect start_bounds, target_bounds, middle_bounds; |
| 244 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); |
| 245 start_bounds.set_x(-90); |
| 246 target_bounds.set_x(90); |
| 247 |
| 248 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 249 |
| 250 delegate.SetOpacityFromAnimation(start_opacity); |
| 251 delegate.SetBoundsFromAnimation(start_bounds); |
| 252 |
| 253 animator->ScheduleAnimation( |
| 254 new LayerAnimationSequence( |
| 255 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 256 |
| 257 scoped_ptr<LayerAnimationSequence> bounds_and_opacity( |
| 258 new LayerAnimationSequence( |
| 259 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); |
| 260 |
| 261 bounds_and_opacity->AddElement( |
| 262 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)); |
| 263 |
| 264 animator->ScheduleAnimation(bounds_and_opacity.release()); |
| 265 |
| 266 animator->ScheduleAnimation( |
| 267 new LayerAnimationSequence( |
| 268 LayerAnimationElement::CreateBoundsElement(start_bounds, delta))); |
| 269 |
| 270 EXPECT_TRUE(animator->is_animating()); |
| 271 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 272 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); |
| 273 |
| 274 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 275 |
| 276 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); |
| 277 |
| 278 EXPECT_TRUE(animator->is_animating()); |
| 279 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 280 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); |
| 281 |
| 282 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 283 |
| 284 EXPECT_TRUE(animator->is_animating()); |
| 285 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 286 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); |
| 287 |
| 288 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); |
| 289 |
| 290 EXPECT_TRUE(animator->is_animating()); |
| 291 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 292 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); |
| 293 |
| 294 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000)); |
| 295 |
| 296 EXPECT_TRUE(animator->is_animating()); |
| 297 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 298 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); |
| 299 |
| 300 element->Step(start_time + base::TimeDelta::FromMilliseconds(4000)); |
| 301 |
| 302 EXPECT_FALSE(animator->is_animating()); |
| 303 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 304 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); |
| 305 } |
| 306 |
| 307 // Schedule {o} and then schedule {o} and {b} together. In this case, since |
| 308 // ScheduleTogether is being used, the bounds animation should not start until |
| 309 // the second opacity animation starts. |
| 310 TEST(LayerAnimatorTest, ScheduleTogether) { |
| 311 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 312 AnimationContainerElement* element = animator.get(); |
| 313 animator->set_disable_timer_for_test(true); |
| 314 TestLayerAnimationDelegate delegate; |
| 315 animator->SetDelegate(&delegate); |
| 316 |
| 317 double start_opacity(0.0); |
| 318 double target_opacity(1.0); |
| 319 |
| 320 gfx::Rect start_bounds, target_bounds, middle_bounds; |
| 321 start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50); |
| 322 start_bounds.set_x(-90); |
| 323 target_bounds.set_x(90); |
| 324 |
| 325 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 326 |
| 327 delegate.SetOpacityFromAnimation(start_opacity); |
| 328 delegate.SetBoundsFromAnimation(start_bounds); |
| 329 |
| 330 animator->ScheduleAnimation( |
| 331 new LayerAnimationSequence( |
| 332 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 333 |
| 334 std::vector<LayerAnimationSequence*> sequences; |
| 335 sequences.push_back(new LayerAnimationSequence( |
| 336 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); |
| 337 sequences.push_back(new LayerAnimationSequence( |
| 338 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); |
| 339 |
| 340 animator->ScheduleTogether(sequences); |
| 341 |
| 342 EXPECT_TRUE(animator->is_animating()); |
| 343 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 344 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); |
| 345 |
| 346 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 347 |
| 348 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 349 |
| 350 EXPECT_TRUE(animator->is_animating()); |
| 351 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 352 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); |
| 353 |
| 354 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); |
| 355 |
| 356 EXPECT_FALSE(animator->is_animating()); |
| 357 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 358 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); |
| 359 } |
| 360 |
| 361 // Start animation (that can run immediately). This is the trivial case (see |
| 362 // the trival case for ScheduleAnimation). |
| 363 TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) { |
| 364 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 365 AnimationContainerElement* element = animator.get(); |
| 366 animator->set_disable_timer_for_test(true); |
| 367 TestLayerAnimationDelegate delegate; |
| 368 animator->SetDelegate(&delegate); |
| 369 |
| 370 double start_opacity(0.0); |
| 371 double middle_opacity(0.5); |
| 372 double target_opacity(1.0); |
| 373 |
| 374 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 375 |
| 376 delegate.SetOpacityFromAnimation(start_opacity); |
| 377 |
| 378 animator->StartAnimation( |
| 379 new LayerAnimationSequence( |
| 380 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 381 |
| 382 EXPECT_TRUE(animator->is_animating()); |
| 383 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 384 |
| 385 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 386 |
| 387 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); |
| 388 |
| 389 EXPECT_TRUE(animator->is_animating()); |
| 390 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 391 |
| 392 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 393 |
| 394 EXPECT_FALSE(animator->is_animating()); |
| 395 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 396 } |
| 397 |
| 398 // Preempt by immediately setting new target. |
| 399 TEST(LayerAnimatorTest, PreemptBySettingNewTarget) { |
| 400 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 401 animator->set_disable_timer_for_test(true); |
| 402 TestLayerAnimationDelegate delegate; |
| 403 animator->SetDelegate(&delegate); |
| 404 |
| 405 double start_opacity(0.0); |
| 406 double target_opacity(1.0); |
| 407 |
| 408 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 409 |
| 410 delegate.SetOpacityFromAnimation(start_opacity); |
| 411 |
| 412 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET); |
| 413 |
| 414 animator->StartAnimation( |
| 415 new LayerAnimationSequence( |
| 416 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 417 |
| 418 animator->StartAnimation( |
| 419 new LayerAnimationSequence( |
| 420 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); |
| 421 |
| 422 EXPECT_FALSE(animator->is_animating()); |
| 423 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 424 } |
| 425 |
| 426 // Preempt by animating to new target. |
| 427 TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) { |
| 428 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 429 AnimationContainerElement* element = animator.get(); |
| 430 animator->set_disable_timer_for_test(true); |
| 431 TestLayerAnimationDelegate delegate; |
| 432 animator->SetDelegate(&delegate); |
| 433 |
| 434 double start_opacity(0.0); |
| 435 double middle_opacity(0.5); |
| 436 double target_opacity(1.0); |
| 437 |
| 438 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 439 |
| 440 delegate.SetOpacityFromAnimation(start_opacity); |
| 441 |
| 442 animator->set_preemption_strategy( |
| 443 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 444 |
| 445 animator->StartAnimation( |
| 446 new LayerAnimationSequence( |
| 447 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 448 |
| 449 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 450 |
| 451 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); |
| 452 |
| 453 animator->StartAnimation( |
| 454 new LayerAnimationSequence( |
| 455 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); |
| 456 |
| 457 EXPECT_TRUE(animator->is_animating()); |
| 458 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 459 |
| 460 animator->StartAnimation( |
| 461 new LayerAnimationSequence( |
| 462 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); |
| 463 |
| 464 EXPECT_TRUE(animator->is_animating()); |
| 465 |
| 466 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 467 |
| 468 EXPECT_TRUE(animator->is_animating()); |
| 469 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), |
| 470 0.5 * (start_opacity + middle_opacity)); |
| 471 |
| 472 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); |
| 473 |
| 474 EXPECT_FALSE(animator->is_animating()); |
| 475 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 476 } |
| 477 |
| 478 // Preempt by enqueuing the new animation. |
| 479 TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) { |
| 480 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 481 AnimationContainerElement* element = animator.get(); |
| 482 animator->set_disable_timer_for_test(true); |
| 483 TestLayerAnimationDelegate delegate; |
| 484 animator->SetDelegate(&delegate); |
| 485 |
| 486 double start_opacity(0.0); |
| 487 double middle_opacity(0.5); |
| 488 double target_opacity(1.0); |
| 489 |
| 490 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 491 |
| 492 delegate.SetOpacityFromAnimation(start_opacity); |
| 493 |
| 494 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); |
| 495 |
| 496 animator->StartAnimation( |
| 497 new LayerAnimationSequence( |
| 498 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 499 |
| 500 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 501 |
| 502 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); |
| 503 |
| 504 animator->StartAnimation( |
| 505 new LayerAnimationSequence( |
| 506 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); |
| 507 |
| 508 EXPECT_TRUE(animator->is_animating()); |
| 509 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 510 |
| 511 EXPECT_TRUE(animator->is_animating()); |
| 512 |
| 513 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 514 |
| 515 EXPECT_TRUE(animator->is_animating()); |
| 516 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 517 |
| 518 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); |
| 519 |
| 520 EXPECT_TRUE(animator->is_animating()); |
| 521 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 522 |
| 523 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); |
| 524 |
| 525 EXPECT_FALSE(animator->is_animating()); |
| 526 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 527 } |
| 528 |
| 529 // Start an animation when there are sequences waiting in the queue. In this |
| 530 // case, all pending and running animations should be finished, and the new |
| 531 // animation started. |
| 532 TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) { |
| 533 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 534 AnimationContainerElement* element = animator.get(); |
| 535 animator->set_disable_timer_for_test(true); |
| 536 TestLayerAnimationDelegate delegate; |
| 537 animator->SetDelegate(&delegate); |
| 538 |
| 539 double start_opacity(0.0); |
| 540 double middle_opacity(0.5); |
| 541 double target_opacity(1.0); |
| 542 |
| 543 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 544 |
| 545 delegate.SetOpacityFromAnimation(start_opacity); |
| 546 |
| 547 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
| 548 |
| 549 animator->StartAnimation( |
| 550 new LayerAnimationSequence( |
| 551 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 552 |
| 553 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 554 |
| 555 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); |
| 556 |
| 557 animator->StartAnimation( |
| 558 new LayerAnimationSequence( |
| 559 LayerAnimationElement::CreateOpacityElement(middle_opacity, delta))); |
| 560 |
| 561 // Queue should now have two animations. Starting a third should replace the |
| 562 // second. |
| 563 animator->StartAnimation( |
| 564 new LayerAnimationSequence( |
| 565 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); |
| 566 |
| 567 EXPECT_TRUE(animator->is_animating()); |
| 568 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 569 |
| 570 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 571 |
| 572 EXPECT_TRUE(animator->is_animating()); |
| 573 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 574 |
| 575 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); |
| 576 |
| 577 EXPECT_TRUE(animator->is_animating()); |
| 578 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); |
| 579 |
| 580 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); |
| 581 |
| 582 EXPECT_FALSE(animator->is_animating()); |
| 583 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 584 } |
| 585 |
| 586 // Test that cyclic sequences continue to animate. |
| 587 TEST(LayerAnimatorTest, CyclicSequences) { |
| 588 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); |
| 589 AnimationContainerElement* element = animator.get(); |
| 590 animator->set_disable_timer_for_test(true); |
| 591 TestLayerAnimationDelegate delegate; |
| 592 animator->SetDelegate(&delegate); |
| 593 |
| 594 double start_opacity(0.0); |
| 595 double target_opacity(1.0); |
| 596 |
| 597 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 598 |
| 599 delegate.SetOpacityFromAnimation(start_opacity); |
| 600 |
| 601 scoped_ptr<LayerAnimationSequence> sequence( |
| 602 new LayerAnimationSequence( |
| 603 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); |
| 604 |
| 605 sequence->AddElement( |
| 606 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); |
| 607 |
| 608 sequence->set_is_cyclic(true); |
| 609 |
| 610 animator->StartAnimation(sequence.release()); |
| 611 |
| 612 base::TimeTicks start_time = animator->get_last_step_time_for_test(); |
| 613 |
| 614 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); |
| 615 |
| 616 EXPECT_TRUE(animator->is_animating()); |
| 617 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 618 |
| 619 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); |
| 620 |
| 621 EXPECT_TRUE(animator->is_animating()); |
| 622 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 623 |
| 624 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000)); |
| 625 |
| 626 EXPECT_TRUE(animator->is_animating()); |
| 627 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 628 |
| 629 // Skip ahead by a lot. |
| 630 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000000000000)); |
| 631 |
| 632 EXPECT_TRUE(animator->is_animating()); |
| 633 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); |
| 634 |
| 635 // Skip ahead by a lot. |
| 636 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000000001000)); |
| 637 |
| 638 EXPECT_TRUE(animator->is_animating()); |
| 639 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); |
| 640 |
| 641 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY); |
| 642 |
| 643 EXPECT_FALSE(animator->is_animating()); |
| 644 } |
| 645 |
| 646 } // namespace |
| 647 |
| 648 } // namespace ui |
OLD | NEW |