layer_animator_unittest.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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. 4 5#include "ui/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/strings/stringprintf.h" 11#include "base/time/time.h" 12#include "testing/gtest/include/gtest/gtest.h" 13#include "ui/compositor/layer.h" 14#include "ui/compositor/layer_animation_delegate.h" 15#include "ui/compositor/layer_animation_element.h" 16#include "ui/compositor/layer_animation_sequence.h" 17#include "ui/compositor/scoped_animation_duration_scale_mode.h" 18#include "ui/compositor/scoped_layer_animation_settings.h" 19#include "ui/compositor/test/layer_animator_test_controller.h" 20#include "ui/compositor/test/test_layer_animation_delegate.h" 21#include "ui/compositor/test/test_layer_animation_observer.h" 22#include "ui/compositor/test/test_utils.h" 23#include "ui/gfx/rect.h" 24#include "ui/gfx/transform.h" 25 26using gfx::AnimationContainerElement; 27 28namespace ui { 29 30namespace { 31 32// Converts |color| to a string. Each component of the color is separated by a 33// space and the order if A R G B. 34std::string ColorToString(SkColor color) { 35 return base::StringPrintf("%d %d %d %d", SkColorGetA(color), 36 SkColorGetR(color), SkColorGetG(color), 37 SkColorGetB(color)); 38} 39 40// Creates vector with two LayerAnimationSequences, based on |first| and 41// |second| layer animation elements. 42std::vector<LayerAnimationSequence*> CreateMultiSequence( 43 LayerAnimationElement* first, 44 LayerAnimationElement* second) { 45 LayerAnimationSequence* first_sequence = new LayerAnimationSequence(); 46 first_sequence->AddElement(first); 47 LayerAnimationSequence* second_sequence = new LayerAnimationSequence(); 48 second_sequence->AddElement(second); 49 50 std::vector<ui::LayerAnimationSequence*> animations; 51 animations.push_back(first_sequence); 52 animations.push_back(second_sequence); 53 return animations; 54} 55 56class TestImplicitAnimationObserver : public ImplicitAnimationObserver { 57 public: 58 explicit TestImplicitAnimationObserver(bool notify_when_animator_destructed) 59 : animations_completed_(false), 60 notify_when_animator_destructed_(notify_when_animator_destructed) { 61 } 62 63 bool animations_completed() const { return animations_completed_; } 64 void set_animations_completed(bool completed) { 65 animations_completed_ = completed; 66 } 67 68 bool WasAnimationAbortedForProperty( 69 LayerAnimationElement::AnimatableProperty property) const { 70 return ImplicitAnimationObserver::WasAnimationAbortedForProperty(property); 71 } 72 73 bool WasAnimationCompletedForProperty( 74 LayerAnimationElement::AnimatableProperty property) const { 75 return ImplicitAnimationObserver::WasAnimationCompletedForProperty( 76 property); 77 } 78 79 private: 80 // ImplicitAnimationObserver implementation 81 virtual void OnImplicitAnimationsCompleted() OVERRIDE { 82 animations_completed_ = true; 83 } 84 85 virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE { 86 return notify_when_animator_destructed_; 87 } 88 89 bool animations_completed_; 90 bool notify_when_animator_destructed_; 91 92 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver); 93}; 94 95// When notified that an animation has ended, stops all other animations. 96class DeletingLayerAnimationObserver : public LayerAnimationObserver { 97 public: 98 DeletingLayerAnimationObserver(LayerAnimator* animator) 99 : animator_(animator) { 100 } 101 102 virtual void OnLayerAnimationEnded( 103 LayerAnimationSequence* sequence) OVERRIDE { 104 animator_->StopAnimating(); 105 } 106 107 virtual void OnLayerAnimationAborted( 108 LayerAnimationSequence* sequence) OVERRIDE { 109 animator_->StopAnimating(); 110 } 111 112 virtual void OnLayerAnimationScheduled( 113 LayerAnimationSequence* sequence) OVERRIDE { 114 } 115 116 private: 117 LayerAnimator* animator_; 118 119 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver); 120}; 121 122class TestLayerAnimator : public LayerAnimator { 123 public: 124 TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)) {} 125 126 protected: 127 virtual ~TestLayerAnimator() {} 128 129 virtual void ProgressAnimation(LayerAnimationSequence* sequence, 130 base::TimeTicks now) OVERRIDE { 131 EXPECT_TRUE(HasAnimation(sequence)); 132 LayerAnimator::ProgressAnimation(sequence, now); 133 } 134 135 private: 136 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator); 137}; 138 139// The test layer animation sequence updates a live instances count when it is 140// created and destroyed. 141class TestLayerAnimationSequence : public LayerAnimationSequence { 142 public: 143 TestLayerAnimationSequence(LayerAnimationElement* element, 144 int* num_live_instances) 145 : LayerAnimationSequence(element), 146 num_live_instances_(num_live_instances) { 147 (*num_live_instances_)++; 148 } 149 150 virtual ~TestLayerAnimationSequence() { 151 (*num_live_instances_)--; 152 } 153 154 private: 155 int* num_live_instances_; 156 157 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence); 158}; 159 160} // namespace 161 162// Checks that setting a property on an implicit animator causes an animation to 163// happen. 164TEST(LayerAnimatorTest, ImplicitAnimation) { 165 scoped_refptr<LayerAnimator> animator( 166 LayerAnimator::CreateImplicitAnimator()); 167 AnimationContainerElement* element = animator.get(); 168 animator->set_disable_timer_for_test(true); 169 TestLayerAnimationDelegate delegate; 170 animator->SetDelegate(&delegate); 171 base::TimeTicks now = base::TimeTicks::Now(); 172 animator->SetBrightness(0.5); 173 EXPECT_TRUE(animator->is_animating()); 174 element->Step(now + base::TimeDelta::FromSeconds(1)); 175 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5); 176} 177 178// Checks that if the animator is a default animator, that implicit animations 179// are not started. 180TEST(LayerAnimatorTest, NoImplicitAnimation) { 181 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 182 animator->set_disable_timer_for_test(true); 183 TestLayerAnimationDelegate delegate; 184 animator->SetDelegate(&delegate); 185 animator->SetBrightness(0.5); 186 EXPECT_FALSE(animator->is_animating()); 187 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5); 188} 189 190// Checks that StopAnimatingProperty stops animation for that property, and also 191// skips the stopped animation to the end. 192TEST(LayerAnimatorTest, StopAnimatingProperty) { 193 scoped_refptr<LayerAnimator> animator( 194 LayerAnimator::CreateImplicitAnimator()); 195 animator->set_disable_timer_for_test(true); 196 TestLayerAnimationDelegate delegate; 197 animator->SetDelegate(&delegate); 198 double target_opacity(0.5); 199 gfx::Rect target_bounds(0, 0, 50, 50); 200 animator->SetOpacity(target_opacity); 201 animator->SetBounds(target_bounds); 202 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY); 203 EXPECT_TRUE(animator->is_animating()); 204 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); 205 animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS); 206 EXPECT_FALSE(animator->is_animating()); 207 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); 208} 209 210// Checks that multiple running animation for separate properties can be stopped 211// simultaneously and that all animations are advanced to their target values. 212TEST(LayerAnimatorTest, StopAnimating) { 213 scoped_refptr<LayerAnimator> animator( 214 LayerAnimator::CreateImplicitAnimator()); 215 animator->set_disable_timer_for_test(true); 216 TestLayerAnimationDelegate delegate; 217 animator->SetDelegate(&delegate); 218 double target_opacity(0.5); 219 gfx::Rect target_bounds(0, 0, 50, 50); 220 animator->SetOpacity(target_opacity); 221 animator->SetBounds(target_bounds); 222 EXPECT_TRUE(animator->is_animating()); 223 animator->StopAnimating(); 224 EXPECT_FALSE(animator->is_animating()); 225 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); 226 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); 227} 228 229// Checks that multiple running animation for separate properties can be stopped 230// simultaneously and that all animations are advanced to their target values. 231TEST(LayerAnimatorTest, AbortAllAnimations) { 232 scoped_refptr<LayerAnimator> animator( 233 LayerAnimator::CreateImplicitAnimator()); 234 animator->set_disable_timer_for_test(true); 235 TestLayerAnimationDelegate delegate; 236 double initial_opacity(1.0); 237 gfx::Rect initial_bounds(0, 0, 10, 10); 238 delegate.SetOpacityFromAnimation(initial_opacity); 239 delegate.SetBoundsFromAnimation(initial_bounds); 240 animator->SetDelegate(&delegate); 241 double target_opacity(0.5); 242 gfx::Rect target_bounds(0, 0, 50, 50); 243 animator->SetOpacity(target_opacity); 244 animator->SetBounds(target_bounds); 245 EXPECT_TRUE(animator->is_animating()); 246 animator->AbortAllAnimations(); 247 EXPECT_FALSE(animator->is_animating()); 248 EXPECT_FLOAT_EQ(initial_opacity, delegate.GetOpacityForAnimation()); 249 CheckApproximatelyEqual(initial_bounds, delegate.GetBoundsForAnimation()); 250} 251 252// Schedule a non-threaded animation that can run immediately. This is the 253// trivial case and should result in the animation being started immediately. 254TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) { 255 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 256 AnimationContainerElement* element = animator.get(); 257 animator->set_disable_timer_for_test(true); 258 TestLayerAnimationDelegate delegate; 259 animator->SetDelegate(&delegate); 260 261 double start_brightness(0.0); 262 double middle_brightness(0.5); 263 double target_brightness(1.0); 264 265 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 266 267 delegate.SetBrightnessFromAnimation(start_brightness); 268 269 animator->ScheduleAnimation( 270 new LayerAnimationSequence( 271 LayerAnimationElement::CreateBrightnessElement(target_brightness, 272 delta))); 273 274 EXPECT_TRUE(animator->is_animating()); 275 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 276 277 base::TimeTicks start_time = animator->last_step_time(); 278 279 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 280 281 EXPECT_TRUE(animator->is_animating()); 282 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 283 284 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 285 286 EXPECT_FALSE(animator->is_animating()); 287 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 288} 289 290// Schedule a threaded animation that can run immediately. 291TEST(LayerAnimatorTest, ScheduleThreadedAnimationThatCanRunImmediately) { 292 double epsilon = 0.00001; 293 LayerAnimatorTestController test_controller( 294 LayerAnimator::CreateDefaultAnimator()); 295 AnimationContainerElement* element = test_controller.animator(); 296 test_controller.animator()->set_disable_timer_for_test(true); 297 TestLayerAnimationDelegate delegate; 298 test_controller.animator()->SetDelegate(&delegate); 299 300 double start_opacity(0.0); 301 double target_opacity(1.0); 302 303 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 304 305 delegate.SetOpacityFromAnimation(start_opacity); 306 307 test_controller.animator()->ScheduleAnimation( 308 new LayerAnimationSequence( 309 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); 310 311 EXPECT_TRUE(test_controller.animator()->is_animating()); 312 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 313 314 base::TimeTicks start_time = test_controller.animator()->last_step_time(); 315 base::TimeTicks effective_start = start_time + delta; 316 317 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 318 cc::AnimationEvent::Started, 319 0, 320 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 321 animation_group_id(), 322 cc::Animation::Opacity, 323 (effective_start - base::TimeTicks()).InSecondsF())); 324 325 element->Step(effective_start + delta/2); 326 327 EXPECT_TRUE(test_controller.animator()->is_animating()); 328 EXPECT_NEAR( 329 0.5, 330 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 331 last_progressed_fraction(), 332 epsilon); 333 334 element->Step(effective_start + delta); 335 336 EXPECT_FALSE(test_controller.animator()->is_animating()); 337 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); 338} 339 340// Schedule two non-threaded animations on separate properties. Both animations 341// should start immediately and should progress in lock step. 342TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) { 343 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 344 AnimationContainerElement* element = animator.get(); 345 animator->set_disable_timer_for_test(true); 346 TestLayerAnimationDelegate delegate; 347 animator->SetDelegate(&delegate); 348 349 double start_brightness(0.0); 350 double middle_brightness(0.5); 351 double target_brightness(1.0); 352 353 gfx::Rect start_bounds, target_bounds, middle_bounds; 354 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); 355 start_bounds.set_x(-90); 356 target_bounds.set_x(90); 357 358 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 359 360 delegate.SetBrightnessFromAnimation(start_brightness); 361 delegate.SetBoundsFromAnimation(start_bounds); 362 363 animator->ScheduleAnimation( 364 new LayerAnimationSequence( 365 LayerAnimationElement::CreateBrightnessElement(target_brightness, 366 delta))); 367 368 animator->ScheduleAnimation( 369 new LayerAnimationSequence( 370 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); 371 372 EXPECT_TRUE(animator->is_animating()); 373 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 374 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 375 376 base::TimeTicks start_time = animator->last_step_time(); 377 378 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 379 380 EXPECT_TRUE(animator->is_animating()); 381 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 382 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds); 383 384 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 385 386 EXPECT_FALSE(animator->is_animating()); 387 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 388 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); 389} 390 391// Schedule a threaded and a non-threaded animation on separate properties. Both 392// animations should progress in lock step. 393TEST(LayerAnimatorTest, ScheduleThreadedAndNonThreadedAnimations) { 394 double epsilon = 0.00001; 395 LayerAnimatorTestController test_controller( 396 LayerAnimator::CreateDefaultAnimator()); 397 AnimationContainerElement* element = test_controller.animator(); 398 test_controller.animator()->set_disable_timer_for_test(true); 399 TestLayerAnimationDelegate delegate; 400 test_controller.animator()->SetDelegate(&delegate); 401 402 double start_opacity(0.0); 403 double target_opacity(1.0); 404 405 gfx::Rect start_bounds, target_bounds, middle_bounds; 406 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); 407 start_bounds.set_x(-90); 408 target_bounds.set_x(90); 409 410 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 411 412 delegate.SetOpacityFromAnimation(start_opacity); 413 delegate.SetBoundsFromAnimation(start_bounds); 414 415 std::vector<LayerAnimationSequence*> animations; 416 animations.push_back( 417 new LayerAnimationSequence( 418 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); 419 420 animations.push_back( 421 new LayerAnimationSequence( 422 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); 423 424 test_controller.animator()->ScheduleTogether(animations); 425 426 EXPECT_TRUE(test_controller.animator()->is_animating()); 427 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 428 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 429 430 base::TimeTicks start_time = test_controller.animator()->last_step_time(); 431 base::TimeTicks effective_start = start_time + delta; 432 433 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 434 cc::AnimationEvent::Started, 435 0, 436 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 437 animation_group_id(), 438 cc::Animation::Opacity, 439 (effective_start - base::TimeTicks()).InSecondsF())); 440 441 element->Step(effective_start + delta/2); 442 443 EXPECT_TRUE(test_controller.animator()->is_animating()); 444 EXPECT_NEAR( 445 0.5, 446 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 447 last_progressed_fraction(), 448 epsilon); 449 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds); 450 451 element->Step(effective_start + delta); 452 453 EXPECT_FALSE(test_controller.animator()->is_animating()); 454 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); 455 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); 456} 457 458// Schedule two animations on the same property. In this case, the two 459// animations should run one after another. 460TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) { 461 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 462 AnimationContainerElement* element = animator.get(); 463 animator->set_disable_timer_for_test(true); 464 TestLayerAnimationDelegate delegate; 465 animator->SetDelegate(&delegate); 466 467 double start_brightness(0.0); 468 double middle_brightness(0.5); 469 double target_brightness(1.0); 470 471 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 472 473 delegate.SetBrightnessFromAnimation(start_brightness); 474 475 animator->ScheduleAnimation( 476 new LayerAnimationSequence( 477 LayerAnimationElement::CreateBrightnessElement(target_brightness, 478 delta))); 479 480 animator->ScheduleAnimation( 481 new LayerAnimationSequence( 482 LayerAnimationElement::CreateBrightnessElement(start_brightness, 483 delta))); 484 485 EXPECT_TRUE(animator->is_animating()); 486 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 487 488 base::TimeTicks start_time = animator->last_step_time(); 489 490 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 491 492 EXPECT_TRUE(animator->is_animating()); 493 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 494 495 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 496 497 EXPECT_TRUE(animator->is_animating()); 498 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 499 500 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); 501 502 EXPECT_TRUE(animator->is_animating()); 503 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 504 505 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); 506 507 EXPECT_FALSE(animator->is_animating()); 508 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 509} 510 511// Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That 512// is, ensure that all animations targetting a particular property are run in 513// order. 514TEST(LayerAnimatorTest, ScheduleBlockedAnimation) { 515 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 516 AnimationContainerElement* element = animator.get(); 517 animator->set_disable_timer_for_test(true); 518 TestLayerAnimationDelegate delegate; 519 animator->SetDelegate(&delegate); 520 521 double start_grayscale(0.0); 522 double middle_grayscale(0.5); 523 double target_grayscale(1.0); 524 525 gfx::Rect start_bounds, target_bounds, middle_bounds; 526 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); 527 start_bounds.set_x(-90); 528 target_bounds.set_x(90); 529 530 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 531 532 delegate.SetGrayscaleFromAnimation(start_grayscale); 533 delegate.SetBoundsFromAnimation(start_bounds); 534 535 animator->ScheduleAnimation( 536 new LayerAnimationSequence( 537 LayerAnimationElement::CreateGrayscaleElement(target_grayscale, 538 delta))); 539 540 scoped_ptr<LayerAnimationSequence> bounds_and_grayscale( 541 new LayerAnimationSequence( 542 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, 543 delta))); 544 545 bounds_and_grayscale->AddElement( 546 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)); 547 548 animator->ScheduleAnimation(bounds_and_grayscale.release()); 549 550 animator->ScheduleAnimation( 551 new LayerAnimationSequence( 552 LayerAnimationElement::CreateBoundsElement(start_bounds, delta))); 553 554 EXPECT_TRUE(animator->is_animating()); 555 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 556 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 557 558 base::TimeTicks start_time = animator->last_step_time(); 559 560 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 561 562 EXPECT_TRUE(animator->is_animating()); 563 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale); 564 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 565 566 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 567 568 EXPECT_TRUE(animator->is_animating()); 569 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale); 570 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 571 572 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); 573 574 EXPECT_TRUE(animator->is_animating()); 575 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 576 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 577 578 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000)); 579 580 EXPECT_TRUE(animator->is_animating()); 581 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 582 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); 583 584 element->Step(start_time + base::TimeDelta::FromMilliseconds(4000)); 585 586 EXPECT_FALSE(animator->is_animating()); 587 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 588 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 589} 590 591// Schedule {g} and then schedule {g} and {b} together. In this case, since 592// ScheduleTogether is being used, the bounds animation should not start until 593// the second grayscale animation starts. 594TEST(LayerAnimatorTest, ScheduleTogether) { 595 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 596 AnimationContainerElement* element = animator.get(); 597 animator->set_disable_timer_for_test(true); 598 TestLayerAnimationDelegate delegate; 599 animator->SetDelegate(&delegate); 600 601 double start_grayscale(0.0); 602 double target_grayscale(1.0); 603 604 gfx::Rect start_bounds, target_bounds, middle_bounds; 605 start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50); 606 start_bounds.set_x(-90); 607 target_bounds.set_x(90); 608 609 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 610 611 delegate.SetGrayscaleFromAnimation(start_grayscale); 612 delegate.SetBoundsFromAnimation(start_bounds); 613 614 animator->ScheduleAnimation( 615 new LayerAnimationSequence( 616 LayerAnimationElement::CreateGrayscaleElement(target_grayscale, 617 delta))); 618 619 std::vector<LayerAnimationSequence*> sequences; 620 sequences.push_back(new LayerAnimationSequence( 621 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta))); 622 sequences.push_back(new LayerAnimationSequence( 623 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); 624 625 animator->ScheduleTogether(sequences); 626 627 EXPECT_TRUE(animator->is_animating()); 628 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 629 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 630 631 base::TimeTicks start_time = animator->last_step_time(); 632 633 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 634 635 EXPECT_TRUE(animator->is_animating()); 636 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale); 637 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); 638 639 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); 640 641 EXPECT_FALSE(animator->is_animating()); 642 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 643 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); 644} 645 646// Start non-threaded animation (that can run immediately). This is the trivial 647// case (see the trival case for ScheduleAnimation). 648TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) { 649 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 650 AnimationContainerElement* element = animator.get(); 651 animator->set_disable_timer_for_test(true); 652 TestLayerAnimationDelegate delegate; 653 animator->SetDelegate(&delegate); 654 655 double start_brightness(0.0); 656 double middle_brightness(0.5); 657 double target_brightness(1.0); 658 659 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 660 661 delegate.SetBrightnessFromAnimation(start_brightness); 662 663 animator->StartAnimation( 664 new LayerAnimationSequence( 665 LayerAnimationElement::CreateBrightnessElement(target_brightness, 666 delta))); 667 668 EXPECT_TRUE(animator->is_animating()); 669 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 670 671 base::TimeTicks start_time = animator->last_step_time(); 672 673 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 674 675 EXPECT_TRUE(animator->is_animating()); 676 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 677 678 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 679 680 EXPECT_FALSE(animator->is_animating()); 681 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 682} 683 684// Start threaded animation (that can run immediately). 685TEST(LayerAnimatorTest, StartThreadedAnimationThatCanRunImmediately) { 686 double epsilon = 0.00001; 687 LayerAnimatorTestController test_controller( 688 LayerAnimator::CreateDefaultAnimator()); 689 AnimationContainerElement* element = test_controller.animator(); 690 test_controller.animator()->set_disable_timer_for_test(true); 691 TestLayerAnimationDelegate delegate; 692 test_controller.animator()->SetDelegate(&delegate); 693 694 double start_opacity(0.0); 695 double target_opacity(1.0); 696 697 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 698 699 delegate.SetOpacityFromAnimation(start_opacity); 700 701 test_controller.animator()->StartAnimation( 702 new LayerAnimationSequence( 703 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); 704 705 EXPECT_TRUE(test_controller.animator()->is_animating()); 706 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 707 708 base::TimeTicks start_time = test_controller.animator()->last_step_time(); 709 base::TimeTicks effective_start = start_time + delta; 710 711 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 712 cc::AnimationEvent::Started, 713 0, 714 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 715 animation_group_id(), 716 cc::Animation::Opacity, 717 (effective_start - base::TimeTicks()).InSecondsF())); 718 719 element->Step(effective_start + delta/2); 720 721 EXPECT_TRUE(test_controller.animator()->is_animating()); 722 EXPECT_NEAR( 723 0.5, 724 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 725 last_progressed_fraction(), 726 epsilon); 727 728 element->Step(effective_start + delta); 729 EXPECT_FALSE(test_controller.animator()->is_animating()); 730 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); 731} 732 733// Preempt by immediately setting new target. 734TEST(LayerAnimatorTest, PreemptBySettingNewTarget) { 735 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 736 animator->set_disable_timer_for_test(true); 737 TestLayerAnimationDelegate delegate; 738 animator->SetDelegate(&delegate); 739 740 double start_opacity(0.0); 741 double target_opacity(1.0); 742 743 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 744 745 delegate.SetOpacityFromAnimation(start_opacity); 746 747 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET); 748 749 animator->StartAnimation( 750 new LayerAnimationSequence( 751 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); 752 753 animator->StartAnimation( 754 new LayerAnimationSequence( 755 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); 756 757 EXPECT_FALSE(animator->is_animating()); 758 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 759} 760 761// Preempt by animating to new target, with a non-threaded animation. 762TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) { 763 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 764 AnimationContainerElement* element = animator.get(); 765 animator->set_disable_timer_for_test(true); 766 TestLayerAnimationDelegate delegate; 767 animator->SetDelegate(&delegate); 768 769 double start_brightness(0.0); 770 double middle_brightness(0.5); 771 double target_brightness(1.0); 772 773 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 774 775 delegate.SetBrightnessFromAnimation(start_brightness); 776 777 animator->set_preemption_strategy( 778 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 779 780 animator->StartAnimation( 781 new LayerAnimationSequence( 782 LayerAnimationElement::CreateBrightnessElement(target_brightness, 783 delta))); 784 785 base::TimeTicks start_time = animator->last_step_time(); 786 787 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 788 789 animator->StartAnimation( 790 new LayerAnimationSequence( 791 LayerAnimationElement::CreateBrightnessElement(start_brightness, 792 delta))); 793 794 EXPECT_TRUE(animator->is_animating()); 795 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 796 797 animator->StartAnimation( 798 new LayerAnimationSequence( 799 LayerAnimationElement::CreateBrightnessElement(start_brightness, 800 delta))); 801 802 EXPECT_TRUE(animator->is_animating()); 803 804 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 805 806 EXPECT_TRUE(animator->is_animating()); 807 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 808 0.5 * (start_brightness + middle_brightness)); 809 810 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); 811 812 EXPECT_FALSE(animator->is_animating()); 813 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 814} 815 816// Preempt by animating to new target, with a threaded animation. 817TEST(LayerAnimatorTest, PreemptThreadedByImmediatelyAnimatingToNewTarget) { 818 double epsilon = 0.00001; 819 LayerAnimatorTestController test_controller( 820 LayerAnimator::CreateDefaultAnimator()); 821 AnimationContainerElement* element = test_controller.animator(); 822 test_controller.animator()->set_disable_timer_for_test(true); 823 TestLayerAnimationDelegate delegate; 824 test_controller.animator()->SetDelegate(&delegate); 825 826 double start_opacity(0.0); 827 double middle_opacity(0.5); 828 double target_opacity(1.0); 829 830 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 831 832 delegate.SetOpacityFromAnimation(start_opacity); 833 834 test_controller.animator()->set_preemption_strategy( 835 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 836 837 test_controller.animator()->StartAnimation( 838 new LayerAnimationSequence( 839 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); 840 841 base::TimeTicks start_time = test_controller.animator()->last_step_time(); 842 base::TimeTicks effective_start = start_time + delta; 843 844 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 845 cc::AnimationEvent::Started, 846 0, 847 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 848 animation_group_id(), 849 cc::Animation::Opacity, 850 (effective_start - base::TimeTicks()).InSecondsF())); 851 852 element->Step(effective_start + delta/2); 853 854 test_controller.animator()->StartAnimation( 855 new LayerAnimationSequence( 856 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); 857 858 EXPECT_TRUE(test_controller.animator()->is_animating()); 859 EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon); 860 861 test_controller.animator()->StartAnimation( 862 new LayerAnimationSequence( 863 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); 864 865 EXPECT_TRUE(test_controller.animator()->is_animating()); 866 867 base::TimeTicks second_effective_start = effective_start + delta; 868 869 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 870 cc::AnimationEvent::Started, 871 0, 872 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 873 animation_group_id(), 874 cc::Animation::Opacity, 875 (second_effective_start - base::TimeTicks()).InSecondsF())); 876 877 element->Step(second_effective_start + delta/2); 878 879 EXPECT_TRUE(test_controller.animator()->is_animating()); 880 EXPECT_NEAR( 881 0.5, 882 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 883 last_progressed_fraction(), 884 epsilon); 885 886 element->Step(second_effective_start + delta); 887 888 EXPECT_FALSE(test_controller.animator()->is_animating()); 889 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 890} 891 892// Preempt by enqueuing the new animation. 893TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) { 894 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 895 AnimationContainerElement* element = animator.get(); 896 animator->set_disable_timer_for_test(true); 897 TestLayerAnimationDelegate delegate; 898 animator->SetDelegate(&delegate); 899 900 double start_brightness(0.0); 901 double middle_brightness(0.5); 902 double target_brightness(1.0); 903 904 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 905 906 delegate.SetBrightnessFromAnimation(start_brightness); 907 908 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); 909 910 animator->StartAnimation( 911 new LayerAnimationSequence( 912 LayerAnimationElement::CreateBrightnessElement(target_brightness, 913 delta))); 914 915 base::TimeTicks start_time = animator->last_step_time(); 916 917 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 918 919 animator->StartAnimation( 920 new LayerAnimationSequence( 921 LayerAnimationElement::CreateBrightnessElement(start_brightness, 922 delta))); 923 924 EXPECT_TRUE(animator->is_animating()); 925 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 926 927 EXPECT_TRUE(animator->is_animating()); 928 929 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 930 931 EXPECT_TRUE(animator->is_animating()); 932 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 933 934 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); 935 936 EXPECT_TRUE(animator->is_animating()); 937 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 938 939 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); 940 941 EXPECT_FALSE(animator->is_animating()); 942 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 943} 944 945// Start an animation when there are sequences waiting in the queue. In this 946// case, all pending and running animations should be finished, and the new 947// animation started. 948TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) { 949 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 950 AnimationContainerElement* element = animator.get(); 951 animator->set_disable_timer_for_test(true); 952 TestLayerAnimationDelegate delegate; 953 animator->SetDelegate(&delegate); 954 955 double start_brightness(0.0); 956 double middle_brightness(0.5); 957 double target_brightness(1.0); 958 959 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 960 961 delegate.SetBrightnessFromAnimation(start_brightness); 962 963 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS); 964 965 animator->StartAnimation( 966 new LayerAnimationSequence( 967 LayerAnimationElement::CreateBrightnessElement(target_brightness, 968 delta))); 969 970 base::TimeTicks start_time = animator->last_step_time(); 971 972 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 973 974 animator->StartAnimation( 975 new LayerAnimationSequence( 976 LayerAnimationElement::CreateBrightnessElement(middle_brightness, 977 delta))); 978 979 // Queue should now have two animations. Starting a third should replace the 980 // second. 981 animator->StartAnimation( 982 new LayerAnimationSequence( 983 LayerAnimationElement::CreateBrightnessElement(start_brightness, 984 delta))); 985 986 EXPECT_TRUE(animator->is_animating()); 987 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 988 989 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 990 991 EXPECT_TRUE(animator->is_animating()); 992 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 993 994 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); 995 996 EXPECT_TRUE(animator->is_animating()); 997 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 998 999 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); 1000 1001 EXPECT_FALSE(animator->is_animating()); 1002 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 1003} 1004 1005TEST(LayerAnimatorTest, StartTogetherSetsLastStepTime) { 1006 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1007 animator->set_disable_timer_for_test(true); 1008 TestLayerAnimationDelegate delegate; 1009 animator->SetDelegate(&delegate); 1010 1011 double start_grayscale(0.0); 1012 double target_grayscale(1.0); 1013 double start_brightness(0.1); 1014 double target_brightness(0.9); 1015 1016 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1017 1018 delegate.SetGrayscaleFromAnimation(start_grayscale); 1019 delegate.SetBrightnessFromAnimation(start_brightness); 1020 1021 animator->set_preemption_strategy( 1022 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 1023 1024 animator->set_last_step_time(base::TimeTicks()); 1025 1026 animator->StartTogether( 1027 CreateMultiSequence( 1028 LayerAnimationElement::CreateGrayscaleElement(target_grayscale, 1029 delta), 1030 LayerAnimationElement::CreateBrightnessElement(target_brightness, 1031 delta) 1032 )); 1033 1034 // If last step time was not set correctly, the resulting delta should be 1035 // miniscule (fractions of a millisecond). If set correctly, then the delta 1036 // should be enormous. Arbitrarily choosing 1 minute as the threshold, 1037 // though a much smaller value would probably have sufficed. 1038 delta = base::TimeTicks::Now() - animator->last_step_time(); 1039 EXPECT_GT(60.0, delta.InSecondsF()); 1040} 1041 1042//------------------------------------------------------- 1043// Preempt by immediately setting new target. 1044TEST(LayerAnimatorTest, MultiPreemptBySettingNewTarget) { 1045 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1046 animator->set_disable_timer_for_test(true); 1047 TestLayerAnimationDelegate delegate; 1048 animator->SetDelegate(&delegate); 1049 1050 double start_opacity(0.0); 1051 double target_opacity(1.0); 1052 double start_brightness(0.1); 1053 double target_brightness(0.9); 1054 1055 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1056 1057 delegate.SetOpacityFromAnimation(start_opacity); 1058 delegate.SetBrightnessFromAnimation(start_brightness); 1059 1060 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET); 1061 1062 animator->StartTogether( 1063 CreateMultiSequence( 1064 LayerAnimationElement::CreateOpacityElement(target_opacity, delta), 1065 LayerAnimationElement::CreateBrightnessElement(target_brightness, 1066 delta) 1067 )); 1068 1069 animator->StartTogether( 1070 CreateMultiSequence( 1071 LayerAnimationElement::CreateOpacityElement(start_opacity, delta), 1072 LayerAnimationElement::CreateBrightnessElement(start_brightness, 1073 delta) 1074 )); 1075 1076 EXPECT_FALSE(animator->is_animating()); 1077 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 1078 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 1079} 1080 1081// Preempt by animating to new target. 1082TEST(LayerAnimatorTest, MultiPreemptByImmediatelyAnimatingToNewTarget) { 1083 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1084 AnimationContainerElement* element = animator.get(); 1085 animator->set_disable_timer_for_test(true); 1086 TestLayerAnimationDelegate delegate; 1087 animator->SetDelegate(&delegate); 1088 1089 double start_grayscale(0.0); 1090 double middle_grayscale(0.5); 1091 double target_grayscale(1.0); 1092 1093 double start_brightness(0.1); 1094 double middle_brightness(0.2); 1095 double target_brightness(0.3); 1096 1097 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1098 1099 delegate.SetGrayscaleFromAnimation(start_grayscale); 1100 delegate.SetBrightnessFromAnimation(start_brightness); 1101 1102 animator->set_preemption_strategy( 1103 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 1104 1105 animator->StartTogether( 1106 CreateMultiSequence( 1107 LayerAnimationElement::CreateGrayscaleElement(target_grayscale, 1108 delta), 1109 LayerAnimationElement::CreateBrightnessElement(target_brightness, 1110 delta) 1111 )); 1112 1113 base::TimeTicks start_time = animator->last_step_time(); 1114 1115 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 1116 1117 animator->StartTogether( 1118 CreateMultiSequence( 1119 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta), 1120 LayerAnimationElement::CreateBrightnessElement(start_brightness, 1121 delta))); 1122 1123 EXPECT_TRUE(animator->is_animating()); 1124 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale); 1125 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 1126 1127 animator->StartTogether( 1128 CreateMultiSequence( 1129 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta), 1130 LayerAnimationElement::CreateBrightnessElement(start_brightness, 1131 delta))); 1132 1133 EXPECT_TRUE(animator->is_animating()); 1134 1135 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 1136 1137 EXPECT_TRUE(animator->is_animating()); 1138 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), 1139 0.5 * (start_grayscale + middle_grayscale)); 1140 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 1141 0.5 * (start_brightness + middle_brightness)); 1142 1143 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); 1144 1145 EXPECT_FALSE(animator->is_animating()); 1146 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 1147 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 1148} 1149 1150// Preempt a threaded animation by animating to new target. 1151TEST(LayerAnimatorTest, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget) { 1152 double epsilon = 0.00001; 1153 LayerAnimatorTestController test_controller( 1154 LayerAnimator::CreateDefaultAnimator()); 1155 AnimationContainerElement* element = test_controller.animator(); 1156 test_controller.animator()->set_disable_timer_for_test(true); 1157 TestLayerAnimationDelegate delegate; 1158 test_controller.animator()->SetDelegate(&delegate); 1159 1160 double start_opacity(0.0); 1161 double middle_opacity(0.5); 1162 double target_opacity(1.0); 1163 1164 double start_brightness(0.1); 1165 double middle_brightness(0.2); 1166 double target_brightness(0.3); 1167 1168 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1169 1170 delegate.SetOpacityFromAnimation(start_opacity); 1171 delegate.SetBrightnessFromAnimation(start_brightness); 1172 1173 test_controller.animator()->set_preemption_strategy( 1174 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 1175 1176 test_controller.animator()->StartTogether( 1177 CreateMultiSequence( 1178 LayerAnimationElement::CreateOpacityElement(target_opacity, delta), 1179 LayerAnimationElement::CreateBrightnessElement(target_brightness, 1180 delta) 1181 )); 1182 1183 base::TimeTicks start_time = test_controller.animator()->last_step_time(); 1184 base::TimeTicks effective_start = start_time + delta; 1185 1186 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 1187 cc::AnimationEvent::Started, 1188 0, 1189 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 1190 animation_group_id(), 1191 cc::Animation::Opacity, 1192 (effective_start - base::TimeTicks()).InSecondsF())); 1193 1194 element->Step(effective_start + delta/2); 1195 1196 test_controller.animator()->StartTogether( 1197 CreateMultiSequence( 1198 LayerAnimationElement::CreateOpacityElement(start_opacity, delta), 1199 LayerAnimationElement::CreateBrightnessElement(start_brightness, 1200 delta))); 1201 1202 EXPECT_TRUE(test_controller.animator()->is_animating()); 1203 EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon); 1204 EXPECT_NEAR(delegate.GetBrightnessForAnimation(), middle_brightness, epsilon); 1205 1206 test_controller.animator()->StartTogether( 1207 CreateMultiSequence( 1208 LayerAnimationElement::CreateOpacityElement(start_opacity, delta), 1209 LayerAnimationElement::CreateBrightnessElement(start_brightness, 1210 delta))); 1211 1212 EXPECT_TRUE(test_controller.animator()->is_animating()); 1213 1214 base::TimeTicks second_effective_start = effective_start + delta; 1215 1216 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 1217 cc::AnimationEvent::Started, 1218 0, 1219 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 1220 animation_group_id(), 1221 cc::Animation::Opacity, 1222 (second_effective_start - base::TimeTicks()).InSecondsF())); 1223 1224 element->Step(second_effective_start + delta/2); 1225 1226 EXPECT_TRUE(test_controller.animator()->is_animating()); 1227 EXPECT_NEAR( 1228 0.5, 1229 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 1230 last_progressed_fraction(), 1231 epsilon); 1232 EXPECT_NEAR(delegate.GetBrightnessForAnimation(), 1233 0.5 * (start_brightness + middle_brightness), 1234 epsilon); 1235 1236 element->Step(second_effective_start + delta); 1237 1238 EXPECT_FALSE(test_controller.animator()->is_animating()); 1239 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 1240 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 1241} 1242 1243// Preempt by enqueuing the new animation. 1244TEST(LayerAnimatorTest, MultiPreemptEnqueueNewAnimation) { 1245 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1246 AnimationContainerElement* element = animator.get(); 1247 animator->set_disable_timer_for_test(true); 1248 TestLayerAnimationDelegate delegate; 1249 animator->SetDelegate(&delegate); 1250 1251 double start_grayscale(0.0); 1252 double middle_grayscale(0.5); 1253 double target_grayscale(1.0); 1254 1255 double start_brightness(0.1); 1256 double middle_brightness(0.2); 1257 double target_brightness(0.3); 1258 1259 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1260 1261 delegate.SetGrayscaleFromAnimation(start_grayscale); 1262 delegate.SetBrightnessFromAnimation(start_brightness); 1263 1264 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); 1265 1266 animator->StartTogether( 1267 CreateMultiSequence( 1268 LayerAnimationElement::CreateGrayscaleElement(target_grayscale, 1269 delta), 1270 LayerAnimationElement::CreateBrightnessElement(target_brightness, 1271 delta))); 1272 1273 base::TimeTicks start_time = animator->last_step_time(); 1274 1275 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 1276 1277 animator->StartTogether( 1278 CreateMultiSequence( 1279 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta), 1280 LayerAnimationElement::CreateBrightnessElement(start_brightness, 1281 delta))); 1282 1283 EXPECT_TRUE(animator->is_animating()); 1284 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale); 1285 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 1286 1287 EXPECT_TRUE(animator->is_animating()); 1288 1289 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 1290 1291 EXPECT_TRUE(animator->is_animating()); 1292 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale); 1293 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 1294 1295 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); 1296 1297 EXPECT_TRUE(animator->is_animating()); 1298 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale); 1299 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 1300 1301 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); 1302 1303 EXPECT_FALSE(animator->is_animating()); 1304 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 1305 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 1306} 1307 1308// Start an animation when there are sequences waiting in the queue. In this 1309// case, all pending and running animations should be finished, and the new 1310// animation started. 1311TEST(LayerAnimatorTest, MultiPreemptByReplacingQueuedAnimations) { 1312 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1313 AnimationContainerElement* element = animator.get(); 1314 animator->set_disable_timer_for_test(true); 1315 TestLayerAnimationDelegate delegate; 1316 animator->SetDelegate(&delegate); 1317 1318 double start_grayscale(0.0); 1319 double middle_grayscale(0.5); 1320 double target_grayscale(1.0); 1321 1322 double start_brightness(0.1); 1323 double middle_brightness(0.2); 1324 double target_brightness(0.3); 1325 1326 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1327 1328 delegate.SetGrayscaleFromAnimation(start_grayscale); 1329 delegate.SetBrightnessFromAnimation(start_brightness); 1330 1331 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS); 1332 1333 animator->StartTogether( 1334 CreateMultiSequence( 1335 LayerAnimationElement::CreateGrayscaleElement(target_grayscale, 1336 delta), 1337 LayerAnimationElement::CreateBrightnessElement(target_brightness, 1338 delta))); 1339 1340 base::TimeTicks start_time = animator->last_step_time(); 1341 1342 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 1343 1344 animator->StartTogether( 1345 CreateMultiSequence( 1346 LayerAnimationElement::CreateGrayscaleElement(middle_grayscale, 1347 delta), 1348 LayerAnimationElement::CreateBrightnessElement(middle_brightness, 1349 delta))); 1350 1351 // Queue should now have two animations. Starting a third should replace the 1352 // second. 1353 animator->StartTogether( 1354 CreateMultiSequence( 1355 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta), 1356 LayerAnimationElement::CreateBrightnessElement(start_brightness, 1357 delta))); 1358 1359 EXPECT_TRUE(animator->is_animating()); 1360 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale); 1361 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 1362 1363 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 1364 1365 EXPECT_TRUE(animator->is_animating()); 1366 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale); 1367 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 1368 1369 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); 1370 1371 EXPECT_TRUE(animator->is_animating()); 1372 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale); 1373 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness); 1374 1375 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); 1376 1377 EXPECT_FALSE(animator->is_animating()); 1378 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale); 1379 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 1380} 1381//------------------------------------------------------- 1382// Test that non-threaded cyclic sequences continue to animate. 1383TEST(LayerAnimatorTest, CyclicSequences) { 1384 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1385 AnimationContainerElement* element = animator.get(); 1386 animator->set_disable_timer_for_test(true); 1387 TestLayerAnimationDelegate delegate; 1388 animator->SetDelegate(&delegate); 1389 1390 double start_brightness(0.0); 1391 double target_brightness(1.0); 1392 1393 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1394 1395 delegate.SetBrightnessFromAnimation(start_brightness); 1396 1397 scoped_ptr<LayerAnimationSequence> sequence( 1398 new LayerAnimationSequence( 1399 LayerAnimationElement::CreateBrightnessElement(target_brightness, 1400 delta))); 1401 1402 sequence->AddElement( 1403 LayerAnimationElement::CreateBrightnessElement(start_brightness, delta)); 1404 1405 sequence->set_is_cyclic(true); 1406 1407 animator->StartAnimation(sequence.release()); 1408 1409 base::TimeTicks start_time = animator->last_step_time(); 1410 1411 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 1412 1413 EXPECT_TRUE(animator->is_animating()); 1414 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 1415 1416 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); 1417 1418 EXPECT_TRUE(animator->is_animating()); 1419 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 1420 1421 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000)); 1422 1423 EXPECT_TRUE(animator->is_animating()); 1424 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 1425 1426 // Skip ahead by a lot. 1427 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000000000)); 1428 1429 EXPECT_TRUE(animator->is_animating()); 1430 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness); 1431 1432 // Skip ahead by a lot. 1433 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000001000)); 1434 1435 EXPECT_TRUE(animator->is_animating()); 1436 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness); 1437 1438 animator->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS); 1439 1440 EXPECT_FALSE(animator->is_animating()); 1441} 1442 1443// Test that threaded cyclic sequences continue to animate. 1444TEST(LayerAnimatorTest, ThreadedCyclicSequences) { 1445 LayerAnimatorTestController test_controller( 1446 LayerAnimator::CreateDefaultAnimator()); 1447 AnimationContainerElement* element = test_controller.animator(); 1448 test_controller.animator()->set_disable_timer_for_test(true); 1449 TestLayerAnimationDelegate delegate; 1450 test_controller.animator()->SetDelegate(&delegate); 1451 1452 double start_opacity(0.0); 1453 double target_opacity(1.0); 1454 1455 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1456 1457 delegate.SetOpacityFromAnimation(start_opacity); 1458 1459 scoped_ptr<LayerAnimationSequence> sequence( 1460 new LayerAnimationSequence( 1461 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); 1462 1463 sequence->AddElement( 1464 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); 1465 1466 sequence->set_is_cyclic(true); 1467 1468 test_controller.animator()->StartAnimation(sequence.release()); 1469 1470 base::TimeTicks start_time = test_controller.animator()->last_step_time(); 1471 base::TimeTicks effective_start = start_time + delta; 1472 1473 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 1474 cc::AnimationEvent::Started, 1475 0, 1476 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 1477 animation_group_id(), 1478 cc::Animation::Opacity, 1479 (effective_start - base::TimeTicks()).InSecondsF())); 1480 1481 element->Step(effective_start + delta); 1482 EXPECT_TRUE(test_controller.animator()->is_animating()); 1483 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); 1484 1485 base::TimeTicks second_effective_start = effective_start + 2 * delta; 1486 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 1487 cc::AnimationEvent::Started, 1488 0, 1489 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 1490 animation_group_id(), 1491 cc::Animation::Opacity, 1492 (second_effective_start - base::TimeTicks()).InSecondsF())); 1493 1494 element->Step(second_effective_start + delta); 1495 1496 EXPECT_TRUE(test_controller.animator()->is_animating()); 1497 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 1498 1499 base::TimeTicks third_effective_start = second_effective_start + 2 * delta; 1500 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 1501 cc::AnimationEvent::Started, 1502 0, 1503 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 1504 animation_group_id(), 1505 cc::Animation::Opacity, 1506 (third_effective_start - base::TimeTicks()).InSecondsF())); 1507 1508 element->Step(third_effective_start + delta); 1509 EXPECT_TRUE(test_controller.animator()->is_animating()); 1510 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); 1511 1512 base::TimeTicks fourth_effective_start = third_effective_start + 2 * delta; 1513 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 1514 cc::AnimationEvent::Started, 1515 0, 1516 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 1517 animation_group_id(), 1518 cc::Animation::Opacity, 1519 (fourth_effective_start - base::TimeTicks()).InSecondsF())); 1520 1521 // Skip ahead by a lot. 1522 element->Step(fourth_effective_start + 1000 * delta); 1523 1524 EXPECT_TRUE(test_controller.animator()->is_animating()); 1525 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); 1526 1527 base::TimeTicks fifth_effective_start = fourth_effective_start + 1001 * delta; 1528 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent( 1529 cc::AnimationEvent::Started, 1530 0, 1531 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)-> 1532 animation_group_id(), 1533 cc::Animation::Opacity, 1534 (fifth_effective_start - base::TimeTicks()).InSecondsF())); 1535 1536 // Skip ahead by a lot. 1537 element->Step(fifth_effective_start + 999 * delta); 1538 1539 EXPECT_TRUE(test_controller.animator()->is_animating()); 1540 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); 1541 1542 test_controller.animator()->StopAnimatingProperty( 1543 LayerAnimationElement::OPACITY); 1544 1545 EXPECT_FALSE(test_controller.animator()->is_animating()); 1546} 1547 1548TEST(LayerAnimatorTest, AddObserverExplicit) { 1549 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1550 AnimationContainerElement* element = animator.get(); 1551 animator->set_disable_timer_for_test(true); 1552 TestLayerAnimationObserver observer; 1553 TestLayerAnimationDelegate delegate; 1554 animator->SetDelegate(&delegate); 1555 animator->AddObserver(&observer); 1556 observer.set_requires_notification_when_animator_destroyed(true); 1557 1558 EXPECT_TRUE(!observer.last_ended_sequence()); 1559 1560 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1561 1562 delegate.SetBrightnessFromAnimation(0.0f); 1563 1564 LayerAnimationSequence* sequence = new LayerAnimationSequence( 1565 LayerAnimationElement::CreateBrightnessElement(1.0f, delta)); 1566 1567 animator->StartAnimation(sequence); 1568 1569 EXPECT_EQ(observer.last_scheduled_sequence(), sequence); 1570 1571 base::TimeTicks start_time = animator->last_step_time(); 1572 1573 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 1574 1575 EXPECT_EQ(observer.last_ended_sequence(), sequence); 1576 1577 // |sequence| has been destroyed. Recreate it to test abort. 1578 sequence = new LayerAnimationSequence( 1579 LayerAnimationElement::CreateBrightnessElement(1.0f, delta)); 1580 1581 animator->StartAnimation(sequence); 1582 1583 animator = NULL; 1584 1585 EXPECT_EQ(observer.last_aborted_sequence(), sequence); 1586} 1587 1588// Tests that an observer added to a scoped settings object is still notified 1589// when the object goes out of scope. 1590TEST(LayerAnimatorTest, ImplicitAnimationObservers) { 1591 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1592 AnimationContainerElement* element = animator.get(); 1593 animator->set_disable_timer_for_test(true); 1594 TestImplicitAnimationObserver observer(false); 1595 TestLayerAnimationDelegate delegate; 1596 animator->SetDelegate(&delegate); 1597 1598 EXPECT_FALSE(observer.animations_completed()); 1599 animator->SetBrightness(1.0f); 1600 1601 { 1602 ScopedLayerAnimationSettings settings(animator.get()); 1603 settings.AddObserver(&observer); 1604 animator->SetBrightness(0.0f); 1605 } 1606 1607 EXPECT_FALSE(observer.animations_completed()); 1608 base::TimeTicks start_time = animator->last_step_time(); 1609 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 1610 EXPECT_TRUE(observer.animations_completed()); 1611 EXPECT_TRUE(observer.WasAnimationCompletedForProperty( 1612 LayerAnimationElement::BRIGHTNESS)); 1613 EXPECT_FLOAT_EQ(0.0f, delegate.GetBrightnessForAnimation()); 1614} 1615 1616// Tests that an observer added to a scoped settings object is still notified 1617// when the object goes out of scope due to the animation being interrupted. 1618TEST(LayerAnimatorTest, InterruptedImplicitAnimationObservers) { 1619 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1620 animator->set_disable_timer_for_test(true); 1621 TestImplicitAnimationObserver observer(false); 1622 TestLayerAnimationDelegate delegate; 1623 animator->SetDelegate(&delegate); 1624 1625 EXPECT_FALSE(observer.animations_completed()); 1626 animator->SetBrightness(1.0f); 1627 1628 { 1629 ScopedLayerAnimationSettings settings(animator.get()); 1630 settings.AddObserver(&observer); 1631 animator->SetBrightness(0.0f); 1632 } 1633 1634 EXPECT_FALSE(observer.animations_completed()); 1635 // This should interrupt the implicit animation causing the observer to be 1636 // notified immediately. 1637 animator->SetBrightness(1.0f); 1638 EXPECT_TRUE(observer.animations_completed()); 1639 EXPECT_TRUE(observer.WasAnimationCompletedForProperty( 1640 LayerAnimationElement::BRIGHTNESS)); 1641 EXPECT_FLOAT_EQ(1.0f, delegate.GetBrightnessForAnimation()); 1642} 1643 1644// Tests that an observer added to a scoped settings object is not notified 1645// when the animator is destroyed unless explicitly requested. 1646TEST(LayerAnimatorTest, ImplicitObserversAtAnimatorDestruction) { 1647 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1648 animator->set_disable_timer_for_test(true); 1649 TestImplicitAnimationObserver observer_notify(true); 1650 TestImplicitAnimationObserver observer_do_not_notify(false); 1651 TestLayerAnimationDelegate delegate; 1652 animator->SetDelegate(&delegate); 1653 1654 EXPECT_FALSE(observer_notify.animations_completed()); 1655 EXPECT_FALSE(observer_do_not_notify.animations_completed()); 1656 1657 animator->SetBrightness(1.0f); 1658 1659 { 1660 ScopedLayerAnimationSettings settings(animator.get()); 1661 settings.AddObserver(&observer_notify); 1662 settings.AddObserver(&observer_do_not_notify); 1663 animator->SetBrightness(0.0f); 1664 } 1665 1666 EXPECT_FALSE(observer_notify.animations_completed()); 1667 EXPECT_FALSE(observer_do_not_notify.animations_completed()); 1668 animator = NULL; 1669 EXPECT_TRUE(observer_notify.animations_completed()); 1670 EXPECT_TRUE(observer_notify.WasAnimationAbortedForProperty( 1671 LayerAnimationElement::BRIGHTNESS)); 1672 EXPECT_FALSE(observer_do_not_notify.animations_completed()); 1673} 1674 1675TEST(LayerAnimatorTest, AbortedAnimationStatusInImplicitObservers) { 1676 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1677 animator->set_disable_timer_for_test(true); 1678 TestImplicitAnimationObserver observer(false); 1679 TestLayerAnimationDelegate delegate; 1680 animator->SetDelegate(&delegate); 1681 1682 EXPECT_FALSE(observer.animations_completed()); 1683 animator->SetBrightness(1.0f); 1684 1685 { 1686 ScopedLayerAnimationSettings settings(animator.get()); 1687 settings.AddObserver(&observer); 1688 animator->SetBrightness(0.0f); 1689 } 1690 EXPECT_FALSE(observer.animations_completed()); 1691 1692 animator->AbortAllAnimations(); 1693 EXPECT_TRUE(observer.animations_completed()); 1694 EXPECT_TRUE(observer.WasAnimationAbortedForProperty( 1695 LayerAnimationElement::BRIGHTNESS)); 1696 EXPECT_FALSE(observer.WasAnimationAbortedForProperty( 1697 LayerAnimationElement::OPACITY)); 1698 1699 observer.set_animations_completed(false); 1700 { 1701 ScopedLayerAnimationSettings settings(animator.get()); 1702 settings.AddObserver(&observer); 1703 animator->SetOpacity(0.0f); 1704 } 1705 EXPECT_FALSE(observer.animations_completed()); 1706 1707 animator->AbortAllAnimations(); 1708 EXPECT_TRUE(observer.animations_completed()); 1709 EXPECT_TRUE(observer.WasAnimationAbortedForProperty( 1710 LayerAnimationElement::BRIGHTNESS)); 1711 EXPECT_TRUE(observer.WasAnimationAbortedForProperty( 1712 LayerAnimationElement::OPACITY)); 1713} 1714 1715TEST(LayerAnimatorTest, RemoveObserverShouldRemoveFromSequences) { 1716 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1717 AnimationContainerElement* element = animator.get(); 1718 animator->set_disable_timer_for_test(true); 1719 TestLayerAnimationObserver observer; 1720 TestLayerAnimationObserver removed_observer; 1721 TestLayerAnimationDelegate delegate; 1722 animator->SetDelegate(&delegate); 1723 1724 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1725 1726 LayerAnimationSequence* sequence = new LayerAnimationSequence( 1727 LayerAnimationElement::CreateBrightnessElement(1.0f, delta)); 1728 1729 sequence->AddObserver(&observer); 1730 sequence->AddObserver(&removed_observer); 1731 1732 animator->StartAnimation(sequence); 1733 1734 EXPECT_EQ(observer.last_scheduled_sequence(), sequence); 1735 EXPECT_TRUE(!observer.last_ended_sequence()); 1736 EXPECT_EQ(removed_observer.last_scheduled_sequence(), sequence); 1737 EXPECT_TRUE(!removed_observer.last_ended_sequence()); 1738 1739 // This should stop the observer from observing sequence. 1740 animator->RemoveObserver(&removed_observer); 1741 1742 base::TimeTicks start_time = animator->last_step_time(); 1743 1744 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 1745 1746 EXPECT_EQ(observer.last_ended_sequence(), sequence); 1747 EXPECT_TRUE(!removed_observer.last_ended_sequence()); 1748} 1749 1750TEST(LayerAnimatorTest, ObserverReleasedBeforeAnimationSequenceEnds) { 1751 TestLayerAnimationDelegate delegate; 1752 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1753 animator->set_disable_timer_for_test(true); 1754 1755 scoped_ptr<TestLayerAnimationObserver> observer( 1756 new TestLayerAnimationObserver); 1757 animator->SetDelegate(&delegate); 1758 animator->AddObserver(observer.get()); 1759 1760 delegate.SetOpacityFromAnimation(0.0f); 1761 1762 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1763 LayerAnimationSequence* sequence = new LayerAnimationSequence( 1764 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); 1765 1766 animator->StartAnimation(sequence); 1767 1768 // |observer| should be attached to |sequence|. 1769 EXPECT_TRUE(sequence->observers_.might_have_observers()); 1770 1771 // Now, release |observer| 1772 observer.reset(); 1773 1774 // And |sequence| should no longer be attached to |observer|. 1775 EXPECT_FALSE(sequence->observers_.might_have_observers()); 1776} 1777 1778TEST(LayerAnimatorTest, ObserverAttachedAfterAnimationStarted) { 1779 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1780 AnimationContainerElement* element = animator.get(); 1781 animator->set_disable_timer_for_test(true); 1782 1783 TestImplicitAnimationObserver observer(false); 1784 TestLayerAnimationDelegate delegate; 1785 animator->SetDelegate(&delegate); 1786 1787 delegate.SetBrightnessFromAnimation(0.0f); 1788 1789 { 1790 ScopedLayerAnimationSettings setter(animator.get()); 1791 1792 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1793 LayerAnimationSequence* sequence = new LayerAnimationSequence( 1794 LayerAnimationElement::CreateBrightnessElement(1.0f, delta)); 1795 1796 animator->StartAnimation(sequence); 1797 base::TimeTicks start_time = animator->last_step_time(); 1798 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 1799 1800 setter.AddObserver(&observer); 1801 1802 // Start observing an in-flight animation. 1803 sequence->AddObserver(&observer); 1804 1805 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 1806 } 1807 1808 EXPECT_TRUE(observer.animations_completed()); 1809 EXPECT_TRUE(observer.WasAnimationCompletedForProperty( 1810 LayerAnimationElement::BRIGHTNESS)); 1811} 1812 1813TEST(LayerAnimatorTest, ObserverDetachedBeforeAnimationFinished) { 1814 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1815 AnimationContainerElement* element = animator.get(); 1816 animator->set_disable_timer_for_test(true); 1817 1818 TestImplicitAnimationObserver observer(false); 1819 TestLayerAnimationDelegate delegate; 1820 animator->SetDelegate(&delegate); 1821 1822 delegate.SetBrightnessFromAnimation(0.0f); 1823 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1824 LayerAnimationSequence* sequence = new LayerAnimationSequence( 1825 LayerAnimationElement::CreateBrightnessElement(1.0f, delta)); 1826 1827 { 1828 ScopedLayerAnimationSettings setter(animator.get()); 1829 setter.AddObserver(&observer); 1830 1831 animator->StartAnimation(sequence); 1832 base::TimeTicks start_time = animator->last_step_time(); 1833 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 1834 } 1835 1836 EXPECT_FALSE(observer.animations_completed()); 1837 1838 // Stop observing an in-flight animation. 1839 sequence->RemoveObserver(&observer); 1840 1841 EXPECT_TRUE(observer.animations_completed()); 1842 1843 // The animation didn't complete, and neither was it aborted. 1844 EXPECT_FALSE(observer.WasAnimationCompletedForProperty( 1845 LayerAnimationElement::BRIGHTNESS)); 1846 EXPECT_FALSE(observer.WasAnimationAbortedForProperty( 1847 LayerAnimationElement::BRIGHTNESS)); 1848} 1849 1850// This checks that if an animation is deleted due to a callback, that the 1851// animator does not try to use the deleted animation. For example, if we have 1852// two running animations, and the first finishes and the resulting callback 1853// causes the second to be deleted, we should not attempt to animate the second 1854// animation. 1855TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnEnd) { 1856 ScopedAnimationDurationScaleMode normal_duration_mode( 1857 ScopedAnimationDurationScaleMode::NORMAL_DURATION); 1858 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator()); 1859 AnimationContainerElement* element = animator.get(); 1860 animator->set_disable_timer_for_test(true); 1861 TestLayerAnimationDelegate delegate; 1862 animator->SetDelegate(&delegate); 1863 1864 double start_brightness(0.0); 1865 double target_brightness(1.0); 1866 1867 gfx::Rect start_bounds(0, 0, 50, 50); 1868 gfx::Rect target_bounds(5, 5, 5, 5); 1869 1870 delegate.SetBrightnessFromAnimation(start_brightness); 1871 delegate.SetBoundsFromAnimation(start_bounds); 1872 1873 base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1); 1874 base::TimeDelta halfway_delta = base::TimeDelta::FromSeconds(2); 1875 base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(3); 1876 1877 scoped_ptr<DeletingLayerAnimationObserver> observer( 1878 new DeletingLayerAnimationObserver(animator.get())); 1879 1880 animator->AddObserver(observer.get()); 1881 1882 animator->StartAnimation( 1883 new LayerAnimationSequence( 1884 LayerAnimationElement::CreateBrightnessElement( 1885 target_brightness, brightness_delta))); 1886 1887 animator->StartAnimation(new LayerAnimationSequence( 1888 LayerAnimationElement::CreateBoundsElement( 1889 target_bounds, bounds_delta))); 1890 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); 1891 1892 base::TimeTicks start_time = animator->last_step_time(); 1893 element->Step(start_time + halfway_delta); 1894 1895 // Completing the brightness animation should have stopped the bounds 1896 // animation. 1897 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); 1898 1899 animator->RemoveObserver(observer.get()); 1900} 1901 1902// Similar to the ObserverDeletesAnimationsOnEnd test above except that it 1903// tests the behavior when the OnLayerAnimationAborted() callback causes 1904// all of the animator's other animations to be deleted. 1905TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnAbort) { 1906 ScopedAnimationDurationScaleMode normal_duration_mode( 1907 ScopedAnimationDurationScaleMode::NORMAL_DURATION); 1908 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator()); 1909 animator->set_disable_timer_for_test(true); 1910 TestLayerAnimationDelegate delegate; 1911 animator->SetDelegate(&delegate); 1912 1913 double start_brightness(0.0); 1914 double target_brightness(1.0); 1915 gfx::Rect start_bounds(0, 0, 50, 50); 1916 gfx::Rect target_bounds(5, 5, 5, 5); 1917 base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1); 1918 base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(2); 1919 1920 delegate.SetBrightnessFromAnimation(start_brightness); 1921 delegate.SetBoundsFromAnimation(start_bounds); 1922 1923 scoped_ptr<DeletingLayerAnimationObserver> observer( 1924 new DeletingLayerAnimationObserver(animator.get())); 1925 animator->AddObserver(observer.get()); 1926 1927 animator->StartAnimation( 1928 new LayerAnimationSequence( 1929 LayerAnimationElement::CreateBrightnessElement( 1930 target_brightness, brightness_delta))); 1931 animator->StartAnimation(new LayerAnimationSequence( 1932 LayerAnimationElement::CreateBoundsElement( 1933 target_bounds, bounds_delta))); 1934 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); 1935 1936 animator->set_preemption_strategy( 1937 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 1938 animator->StartAnimation( 1939 new LayerAnimationSequence( 1940 LayerAnimationElement::CreateBrightnessElement( 1941 target_brightness, brightness_delta))); 1942 1943 // Starting the second brightness animation should have aborted the initial 1944 // brightness animation. |observer| should have stopped the bounds animation 1945 // as a result. 1946 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); 1947 1948 animator->RemoveObserver(observer.get()); 1949} 1950 1951// Check that setting a property during an animation with a default animator 1952// cancels the original animation. 1953TEST(LayerAnimatorTest, SettingPropertyDuringAnAnimation) { 1954 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1955 animator->set_disable_timer_for_test(true); 1956 TestLayerAnimationDelegate delegate; 1957 animator->SetDelegate(&delegate); 1958 1959 double start_opacity(0.0); 1960 double target_opacity(1.0); 1961 1962 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 1963 1964 delegate.SetOpacityFromAnimation(start_opacity); 1965 1966 scoped_ptr<LayerAnimationSequence> sequence( 1967 new LayerAnimationSequence( 1968 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); 1969 1970 animator->StartAnimation(sequence.release()); 1971 1972 animator->SetOpacity(0.5); 1973 1974 EXPECT_FALSE(animator->is_animating()); 1975 EXPECT_EQ(0.5, animator->GetTargetOpacity()); 1976} 1977 1978// Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the 1979// second sequence to be leaked. 1980TEST(LayerAnimatorTest, ImmediatelySettingNewTargetDoesNotLeak) { 1981 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 1982 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET); 1983 animator->set_disable_timer_for_test(true); 1984 TestLayerAnimationDelegate delegate; 1985 animator->SetDelegate(&delegate); 1986 1987 gfx::Rect start_bounds(0, 0, 50, 50); 1988 gfx::Rect middle_bounds(10, 10, 100, 100); 1989 gfx::Rect target_bounds(5, 5, 5, 5); 1990 1991 delegate.SetBoundsFromAnimation(start_bounds); 1992 1993 { 1994 // start an implicit bounds animation. 1995 ScopedLayerAnimationSettings settings(animator.get()); 1996 animator->SetBounds(middle_bounds); 1997 } 1998 1999 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); 2000 2001 int num_live_instances = 0; 2002 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 2003 scoped_ptr<TestLayerAnimationSequence> sequence( 2004 new TestLayerAnimationSequence( 2005 LayerAnimationElement::CreateBoundsElement(target_bounds, delta), 2006 &num_live_instances)); 2007 2008 EXPECT_EQ(1, num_live_instances); 2009 2010 // This should interrupt the running sequence causing us to immediately set 2011 // the target value. The sequence should alse be destructed. 2012 animator->StartAnimation(sequence.release()); 2013 2014 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); 2015 EXPECT_EQ(0, num_live_instances); 2016 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); 2017} 2018 2019// Verifies GetTargetOpacity() works when multiple sequences are scheduled. 2020TEST(LayerAnimatorTest, GetTargetOpacity) { 2021 TestLayerAnimationDelegate delegate; 2022 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 2023 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); 2024 animator->set_disable_timer_for_test(true); 2025 animator->SetDelegate(&delegate); 2026 2027 delegate.SetOpacityFromAnimation(0.0); 2028 2029 { 2030 ScopedLayerAnimationSettings settings(animator.get()); 2031 animator->SetOpacity(0.5); 2032 EXPECT_EQ(0.5, animator->GetTargetOpacity()); 2033 2034 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1. 2035 animator->SetOpacity(1.0); 2036 EXPECT_EQ(1.0, animator->GetTargetOpacity()); 2037 } 2038} 2039 2040// Verifies GetTargetBrightness() works when multiple sequences are scheduled. 2041TEST(LayerAnimatorTest, GetTargetBrightness) { 2042 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 2043 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); 2044 animator->set_disable_timer_for_test(true); 2045 TestLayerAnimationDelegate delegate; 2046 animator->SetDelegate(&delegate); 2047 2048 delegate.SetBrightnessFromAnimation(0.0); 2049 2050 { 2051 ScopedLayerAnimationSettings settings(animator.get()); 2052 animator->SetBrightness(0.5); 2053 EXPECT_EQ(0.5, animator->GetTargetBrightness()); 2054 2055 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1. 2056 animator->SetBrightness(1.0); 2057 EXPECT_EQ(1.0, animator->GetTargetBrightness()); 2058 } 2059} 2060 2061// Verifies GetTargetGrayscale() works when multiple sequences are scheduled. 2062TEST(LayerAnimatorTest, GetTargetGrayscale) { 2063 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 2064 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); 2065 animator->set_disable_timer_for_test(true); 2066 TestLayerAnimationDelegate delegate; 2067 animator->SetDelegate(&delegate); 2068 2069 delegate.SetGrayscaleFromAnimation(0.0); 2070 2071 { 2072 ScopedLayerAnimationSettings settings(animator.get()); 2073 animator->SetGrayscale(0.5); 2074 EXPECT_EQ(0.5, animator->GetTargetGrayscale()); 2075 2076 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1. 2077 animator->SetGrayscale(1.0); 2078 EXPECT_EQ(1.0, animator->GetTargetGrayscale()); 2079 } 2080} 2081 2082// Verifies color property is modified appropriately. 2083TEST(LayerAnimatorTest, Color) { 2084 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 2085 AnimationContainerElement* element = animator.get(); 2086 animator->set_disable_timer_for_test(true); 2087 TestLayerAnimationDelegate delegate; 2088 animator->SetDelegate(&delegate); 2089 2090 SkColor start_color = SkColorSetARGB( 0, 20, 40, 60); 2091 SkColor middle_color = SkColorSetARGB(127, 30, 60, 100); 2092 SkColor target_color = SkColorSetARGB(254, 40, 80, 140); 2093 2094 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 2095 2096 delegate.SetColorFromAnimation(start_color); 2097 2098 animator->ScheduleAnimation( 2099 new LayerAnimationSequence( 2100 LayerAnimationElement::CreateColorElement(target_color, delta))); 2101 2102 EXPECT_TRUE(animator->is_animating()); 2103 EXPECT_EQ(ColorToString(start_color), 2104 ColorToString(delegate.GetColorForAnimation())); 2105 2106 base::TimeTicks start_time = animator->last_step_time(); 2107 2108 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); 2109 2110 EXPECT_TRUE(animator->is_animating()); 2111 EXPECT_EQ(ColorToString(middle_color), 2112 ColorToString(delegate.GetColorForAnimation())); 2113 2114 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); 2115 2116 EXPECT_FALSE(animator->is_animating()); 2117 EXPECT_EQ(ColorToString(target_color), 2118 ColorToString(delegate.GetColorForAnimation())); 2119} 2120 2121// Verifies SchedulePauseForProperties(). 2122TEST(LayerAnimatorTest, SchedulePauseForProperties) { 2123 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 2124 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); 2125 animator->SchedulePauseForProperties(base::TimeDelta::FromMilliseconds(100), 2126 LayerAnimationElement::TRANSFORM, 2127 LayerAnimationElement::BOUNDS, -1); 2128 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::TRANSFORM)); 2129 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); 2130 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::OPACITY)); 2131} 2132 2133 2134class AnimatorOwner { 2135public: 2136 AnimatorOwner() 2137 : animator_(LayerAnimator::CreateDefaultAnimator()) { 2138 } 2139 2140 LayerAnimator* animator() { return animator_.get(); } 2141 2142private: 2143 scoped_refptr<LayerAnimator> animator_; 2144 2145 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner); 2146}; 2147 2148class DeletingObserver : public LayerAnimationObserver { 2149public: 2150 DeletingObserver(bool* was_deleted) 2151 : animator_owner_(new AnimatorOwner), 2152 delete_on_animation_ended_(false), 2153 delete_on_animation_aborted_(false), 2154 delete_on_animation_scheduled_(false), 2155 was_deleted_(was_deleted) { 2156 animator()->AddObserver(this); 2157 } 2158 2159 virtual ~DeletingObserver() { 2160 animator()->RemoveObserver(this); 2161 *was_deleted_ = true; 2162 } 2163 2164 LayerAnimator* animator() { return animator_owner_->animator(); } 2165 2166 bool delete_on_animation_ended() const { 2167 return delete_on_animation_ended_; 2168 } 2169 void set_delete_on_animation_ended(bool enabled) { 2170 delete_on_animation_ended_ = enabled; 2171 } 2172 2173 bool delete_on_animation_aborted() const { 2174 return delete_on_animation_aborted_; 2175 } 2176 void set_delete_on_animation_aborted(bool enabled) { 2177 delete_on_animation_aborted_ = enabled; 2178 } 2179 2180 bool delete_on_animation_scheduled() const { 2181 return delete_on_animation_scheduled_; 2182 } 2183 void set_delete_on_animation_scheduled(bool enabled) { 2184 delete_on_animation_scheduled_ = enabled; 2185 } 2186 2187 // LayerAnimationObserver implementation. 2188 virtual void OnLayerAnimationEnded( 2189 LayerAnimationSequence* sequence) OVERRIDE { 2190 if (delete_on_animation_ended_) 2191 delete this; 2192 } 2193 2194 virtual void OnLayerAnimationAborted( 2195 LayerAnimationSequence* sequence) OVERRIDE { 2196 if (delete_on_animation_aborted_) 2197 delete this; 2198 } 2199 2200 virtual void OnLayerAnimationScheduled( 2201 LayerAnimationSequence* sequence) OVERRIDE { 2202 if (delete_on_animation_scheduled_) 2203 delete this; 2204 } 2205 2206private: 2207 scoped_ptr<AnimatorOwner> animator_owner_; 2208 bool delete_on_animation_ended_; 2209 bool delete_on_animation_aborted_; 2210 bool delete_on_animation_scheduled_; 2211 bool* was_deleted_; 2212 2213 DISALLOW_COPY_AND_ASSIGN(DeletingObserver); 2214}; 2215 2216TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterFinishingAnimation) { 2217 bool observer_was_deleted = false; 2218 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted); 2219 observer->set_delete_on_animation_ended(true); 2220 observer->set_delete_on_animation_aborted(true); 2221 LayerAnimator* animator = observer->animator(); 2222 AnimationContainerElement* element = observer->animator(); 2223 animator->set_disable_timer_for_test(true); 2224 TestLayerAnimationDelegate delegate; 2225 animator->SetDelegate(&delegate); 2226 2227 delegate.SetBrightnessFromAnimation(0.0f); 2228 2229 gfx::Rect start_bounds(0, 0, 50, 50); 2230 gfx::Rect target_bounds(10, 10, 100, 100); 2231 2232 delegate.SetBoundsFromAnimation(start_bounds); 2233 2234 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 2235 LayerAnimationSequence* brightness_sequence = new LayerAnimationSequence( 2236 LayerAnimationElement::CreateBrightnessElement(1.0f, delta)); 2237 animator->StartAnimation(brightness_sequence); 2238 2239 delta = base::TimeDelta::FromSeconds(2); 2240 LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence( 2241 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)); 2242 animator->StartAnimation(bounds_sequence); 2243 2244 base::TimeTicks start_time = animator->last_step_time(); 2245 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); 2246 2247 EXPECT_TRUE(observer_was_deleted); 2248} 2249 2250TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterStoppingAnimating) { 2251 bool observer_was_deleted = false; 2252 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted); 2253 observer->set_delete_on_animation_ended(true); 2254 observer->set_delete_on_animation_aborted(true); 2255 LayerAnimator* animator = observer->animator(); 2256 animator->set_disable_timer_for_test(true); 2257 TestLayerAnimationDelegate delegate; 2258 animator->SetDelegate(&delegate); 2259 2260 delegate.SetOpacityFromAnimation(0.0f); 2261 2262 gfx::Rect start_bounds(0, 0, 50, 50); 2263 gfx::Rect target_bounds(10, 10, 100, 100); 2264 2265 delegate.SetBoundsFromAnimation(start_bounds); 2266 2267 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 2268 LayerAnimationSequence* opacity_sequence = new LayerAnimationSequence( 2269 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); 2270 animator->StartAnimation(opacity_sequence); 2271 2272 delta = base::TimeDelta::FromSeconds(2); 2273 LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence( 2274 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)); 2275 animator->StartAnimation(bounds_sequence); 2276 2277 animator->StopAnimating(); 2278 2279 EXPECT_TRUE(observer_was_deleted); 2280} 2281 2282TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterScheduling) { 2283 bool observer_was_deleted = false; 2284 TestLayerAnimationDelegate delegate; 2285 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted); 2286 observer->set_delete_on_animation_scheduled(true); 2287 LayerAnimator* animator = observer->animator(); 2288 animator->set_disable_timer_for_test(true); 2289 animator->SetDelegate(&delegate); 2290 2291 delegate.SetOpacityFromAnimation(0.0f); 2292 2293 gfx::Rect start_bounds(0, 0, 50, 50); 2294 gfx::Rect target_bounds(10, 10, 100, 100); 2295 2296 delegate.SetBoundsFromAnimation(start_bounds); 2297 2298 std::vector<LayerAnimationSequence*> to_start; 2299 2300 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 2301 to_start.push_back(new LayerAnimationSequence( 2302 LayerAnimationElement::CreateOpacityElement(1.0f, delta))); 2303 2304 delta = base::TimeDelta::FromSeconds(2); 2305 to_start.push_back(new LayerAnimationSequence( 2306 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); 2307 2308 animator->ScheduleTogether(to_start); 2309 2310 EXPECT_TRUE(observer_was_deleted); 2311} 2312 2313TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterAborted) { 2314 bool observer_was_deleted = false; 2315 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted); 2316 TestLayerAnimationDelegate delegate; 2317 observer->set_delete_on_animation_aborted(true); 2318 LayerAnimator* animator = observer->animator(); 2319 animator->set_preemption_strategy( 2320 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 2321 animator->set_disable_timer_for_test(true); 2322 animator->SetDelegate(&delegate); 2323 2324 delegate.SetOpacityFromAnimation(0.0f); 2325 2326 gfx::Rect start_bounds(0, 0, 50, 50); 2327 gfx::Rect target_bounds(10, 10, 100, 100); 2328 2329 delegate.SetBoundsFromAnimation(start_bounds); 2330 2331 std::vector<LayerAnimationSequence*> to_start; 2332 2333 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 2334 to_start.push_back(new LayerAnimationSequence( 2335 LayerAnimationElement::CreateOpacityElement(1.0f, delta))); 2336 2337 delta = base::TimeDelta::FromSeconds(2); 2338 to_start.push_back(new LayerAnimationSequence( 2339 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); 2340 2341 animator->ScheduleTogether(to_start); 2342 2343 EXPECT_FALSE(observer_was_deleted); 2344 2345 animator->StartAnimation(new LayerAnimationSequence( 2346 LayerAnimationElement::CreateOpacityElement(1.0f, delta))); 2347 2348 EXPECT_TRUE(observer_was_deleted); 2349} 2350 2351 2352TEST(LayerAnimatorTest, TestSetterRespectEnqueueStrategy) { 2353 TestLayerAnimationDelegate delegate; 2354 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); 2355 animator->set_disable_timer_for_test(true); 2356 2357 animator->SetDelegate(&delegate); 2358 2359 float start_opacity = 0.0f; 2360 float target_opacity = 1.0f; 2361 float magic_opacity = 0.123f; 2362 2363 delegate.SetOpacityFromAnimation(start_opacity); 2364 2365 ScopedLayerAnimationSettings settings(animator.get()); 2366 settings.SetPreemptionStrategy( 2367 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 2368 settings.SetTransitionDuration(base::TimeDelta::FromSeconds(1)); 2369 animator->SetOpacity(target_opacity); 2370 2371 EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation()); 2372 2373 settings.SetPreemptionStrategy( 2374 LayerAnimator::ENQUEUE_NEW_ANIMATION); 2375 settings.SetTransitionDuration(base::TimeDelta()); 2376 animator->SetOpacity(magic_opacity); 2377 2378 EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation()); 2379} 2380 2381TEST(LayerAnimatorTest, TestScopedCounterAnimation) { 2382 Layer parent, child; 2383 parent.Add(&child); 2384 2385 gfx::Transform parent_begin, parent_end; 2386 2387 parent_end.Scale3d(2.0, 0.5, 1.0); 2388 2389 // Parent animates from identity to the end value. The counter animation will 2390 // start at the end value and animate back to identity. 2391 gfx::Transform child_begin(parent_end); 2392 2393 child.SetTransform(child_begin); 2394 parent.SetTransform(parent_begin); 2395 2396 EXPECT_FALSE(child.GetAnimator()->is_animating()); 2397 2398 ScopedLayerAnimationSettings settings(parent.GetAnimator()); 2399 settings.SetInverselyAnimatedBaseLayer(&parent); 2400 settings.AddInverselyAnimatedLayer(&child); 2401 2402 parent.SetTransform(parent_end); 2403 2404 EXPECT_TRUE(child.GetAnimator()->is_animating()); 2405 EXPECT_TRUE(child.GetTargetTransform().IsIdentity()) 2406 << child.GetTargetTransform().ToString(); 2407 2408} 2409 2410} // namespace ui 2411