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