| OLD | NEW |
| (Empty) |
| 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/gfx/compositor/layer_animator.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/time.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "ui/gfx/rect.h" | |
| 13 #include "ui/gfx/transform.h" | |
| 14 #include "ui/gfx/compositor/layer_animation_delegate.h" | |
| 15 #include "ui/gfx/compositor/layer_animation_element.h" | |
| 16 #include "ui/gfx/compositor/layer_animation_sequence.h" | |
| 17 #include "ui/gfx/compositor/scoped_layer_animation_settings.h" | |
| 18 #include "ui/gfx/compositor/test/test_layer_animation_delegate.h" | |
| 19 #include "ui/gfx/compositor/test/test_layer_animation_observer.h" | |
| 20 #include "ui/gfx/compositor/test/test_utils.h" | |
| 21 | |
| 22 namespace ui { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class TestImplicitAnimationObserver : public ImplicitAnimationObserver { | |
| 27 public: | |
| 28 explicit TestImplicitAnimationObserver(bool notify_when_animator_destructed) | |
| 29 : animations_completed_(false), | |
| 30 notify_when_animator_destructed_(notify_when_animator_destructed) { | |
| 31 } | |
| 32 | |
| 33 bool animations_completed() const { return animations_completed_; } | |
| 34 void set_animations_completed(bool completed) { | |
| 35 animations_completed_ = completed; | |
| 36 } | |
| 37 | |
| 38 private: | |
| 39 // ImplicitAnimationObserver implementation | |
| 40 virtual void OnImplicitAnimationsCompleted() OVERRIDE { | |
| 41 animations_completed_ = true; | |
| 42 } | |
| 43 | |
| 44 virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE { | |
| 45 return notify_when_animator_destructed_; | |
| 46 } | |
| 47 | |
| 48 bool animations_completed_; | |
| 49 bool notify_when_animator_destructed_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver); | |
| 52 }; | |
| 53 | |
| 54 // When notified that an animation has ended, stops all other animations. | |
| 55 class DeletingLayerAnimationObserver : public LayerAnimationObserver { | |
| 56 public: | |
| 57 DeletingLayerAnimationObserver(LayerAnimator* animator, | |
| 58 LayerAnimationSequence* sequence) | |
| 59 : animator_(animator), | |
| 60 sequence_(sequence) { | |
| 61 } | |
| 62 | |
| 63 virtual void OnLayerAnimationEnded(LayerAnimationSequence* sequence) { | |
| 64 animator_->StopAnimating(); | |
| 65 } | |
| 66 | |
| 67 virtual void OnLayerAnimationAborted(LayerAnimationSequence* sequence) {} | |
| 68 | |
| 69 virtual void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) {} | |
| 70 | |
| 71 private: | |
| 72 LayerAnimator* animator_; | |
| 73 LayerAnimationSequence* sequence_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver); | |
| 76 }; | |
| 77 | |
| 78 class TestLayerAnimator : public LayerAnimator { | |
| 79 public: | |
| 80 TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)) {} | |
| 81 virtual ~TestLayerAnimator() {} | |
| 82 | |
| 83 protected: | |
| 84 virtual bool ProgressAnimation(LayerAnimationSequence* sequence, | |
| 85 base::TimeDelta delta) OVERRIDE { | |
| 86 EXPECT_TRUE(HasAnimation(sequence)); | |
| 87 return LayerAnimator::ProgressAnimation(sequence, delta); | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator); | |
| 92 }; | |
| 93 | |
| 94 // The test layer animation sequence updates a live instances count when it is | |
| 95 // created and destroyed. | |
| 96 class TestLayerAnimationSequence : public LayerAnimationSequence { | |
| 97 public: | |
| 98 TestLayerAnimationSequence(LayerAnimationElement* element, | |
| 99 int* num_live_instances) | |
| 100 : LayerAnimationSequence(element), | |
| 101 num_live_instances_(num_live_instances) { | |
| 102 (*num_live_instances_)++; | |
| 103 } | |
| 104 | |
| 105 virtual ~TestLayerAnimationSequence() { | |
| 106 (*num_live_instances_)--; | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 int* num_live_instances_; | |
| 111 | |
| 112 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence); | |
| 113 }; | |
| 114 | |
| 115 } // namespace | |
| 116 | |
| 117 // Checks that setting a property on an implicit animator causes an animation to | |
| 118 // happen. | |
| 119 TEST(LayerAnimatorTest, ImplicitAnimation) { | |
| 120 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateImplicitAnimator()); | |
| 121 AnimationContainerElement* element = animator.get(); | |
| 122 animator->set_disable_timer_for_test(true); | |
| 123 TestLayerAnimationDelegate delegate; | |
| 124 animator->SetDelegate(&delegate); | |
| 125 base::TimeTicks now = base::TimeTicks::Now(); | |
| 126 animator->SetOpacity(0.5); | |
| 127 EXPECT_TRUE(animator->is_animating()); | |
| 128 element->Step(now + base::TimeDelta::FromSeconds(1)); | |
| 129 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); | |
| 130 } | |
| 131 | |
| 132 // Checks that if the animator is a default animator, that implicit animations | |
| 133 // are not started. | |
| 134 TEST(LayerAnimatorTest, NoImplicitAnimation) { | |
| 135 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 136 animator->set_disable_timer_for_test(true); | |
| 137 TestLayerAnimationDelegate delegate; | |
| 138 animator->SetDelegate(&delegate); | |
| 139 animator->SetOpacity(0.5); | |
| 140 EXPECT_FALSE(animator->is_animating()); | |
| 141 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); | |
| 142 } | |
| 143 | |
| 144 // Checks that StopAnimatingProperty stops animation for that property, and also | |
| 145 // skips the stopped animation to the end. | |
| 146 TEST(LayerAnimatorTest, StopAnimatingProperty) { | |
| 147 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateImplicitAnimator()); | |
| 148 animator->set_disable_timer_for_test(true); | |
| 149 TestLayerAnimationDelegate delegate; | |
| 150 animator->SetDelegate(&delegate); | |
| 151 double target_opacity(0.5); | |
| 152 gfx::Rect target_bounds(0, 0, 50, 50); | |
| 153 animator->SetOpacity(target_opacity); | |
| 154 animator->SetBounds(target_bounds); | |
| 155 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY); | |
| 156 EXPECT_TRUE(animator->is_animating()); | |
| 157 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); | |
| 158 animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS); | |
| 159 EXPECT_FALSE(animator->is_animating()); | |
| 160 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
| 161 } | |
| 162 | |
| 163 // Checks that multiple running animation for separate properties can be stopped | |
| 164 // simultaneously and that all animations are advanced to their target values. | |
| 165 TEST(LayerAnimatorTest, StopAnimating) { | |
| 166 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateImplicitAnimator()); | |
| 167 animator->set_disable_timer_for_test(true); | |
| 168 TestLayerAnimationDelegate delegate; | |
| 169 animator->SetDelegate(&delegate); | |
| 170 double target_opacity(0.5); | |
| 171 gfx::Rect target_bounds(0, 0, 50, 50); | |
| 172 animator->SetOpacity(target_opacity); | |
| 173 animator->SetBounds(target_bounds); | |
| 174 EXPECT_TRUE(animator->is_animating()); | |
| 175 animator->StopAnimating(); | |
| 176 EXPECT_FALSE(animator->is_animating()); | |
| 177 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); | |
| 178 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
| 179 } | |
| 180 | |
| 181 // Schedule an animation that can run immediately. This is the trivial case and | |
| 182 // should result in the animation being started immediately. | |
| 183 TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) { | |
| 184 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 185 AnimationContainerElement* element = animator.get(); | |
| 186 animator->set_disable_timer_for_test(true); | |
| 187 TestLayerAnimationDelegate delegate; | |
| 188 animator->SetDelegate(&delegate); | |
| 189 | |
| 190 double start_opacity(0.0); | |
| 191 double middle_opacity(0.5); | |
| 192 double target_opacity(1.0); | |
| 193 | |
| 194 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 195 | |
| 196 delegate.SetOpacityFromAnimation(start_opacity); | |
| 197 | |
| 198 animator->ScheduleAnimation( | |
| 199 new LayerAnimationSequence( | |
| 200 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 201 | |
| 202 EXPECT_TRUE(animator->is_animating()); | |
| 203 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 204 | |
| 205 base::TimeTicks start_time = animator->last_step_time(); | |
| 206 | |
| 207 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 208 | |
| 209 EXPECT_TRUE(animator->is_animating()); | |
| 210 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 211 | |
| 212 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 213 | |
| 214 EXPECT_FALSE(animator->is_animating()); | |
| 215 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 216 } | |
| 217 | |
| 218 // Schedule two animations on separate properties. Both animations should | |
| 219 // start immediately and should progress in lock step. | |
| 220 TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) { | |
| 221 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 222 AnimationContainerElement* element = animator.get(); | |
| 223 animator->set_disable_timer_for_test(true); | |
| 224 TestLayerAnimationDelegate delegate; | |
| 225 animator->SetDelegate(&delegate); | |
| 226 | |
| 227 double start_opacity(0.0); | |
| 228 double middle_opacity(0.5); | |
| 229 double target_opacity(1.0); | |
| 230 | |
| 231 gfx::Rect start_bounds, target_bounds, middle_bounds; | |
| 232 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); | |
| 233 start_bounds.set_x(-90); | |
| 234 target_bounds.set_x(90); | |
| 235 | |
| 236 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 237 | |
| 238 delegate.SetOpacityFromAnimation(start_opacity); | |
| 239 delegate.SetBoundsFromAnimation(start_bounds); | |
| 240 | |
| 241 animator->ScheduleAnimation( | |
| 242 new LayerAnimationSequence( | |
| 243 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 244 | |
| 245 animator->ScheduleAnimation( | |
| 246 new LayerAnimationSequence( | |
| 247 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); | |
| 248 | |
| 249 EXPECT_TRUE(animator->is_animating()); | |
| 250 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 251 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
| 252 | |
| 253 base::TimeTicks start_time = animator->last_step_time(); | |
| 254 | |
| 255 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 256 | |
| 257 EXPECT_TRUE(animator->is_animating()); | |
| 258 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 259 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds); | |
| 260 | |
| 261 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 262 | |
| 263 EXPECT_FALSE(animator->is_animating()); | |
| 264 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 265 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
| 266 } | |
| 267 | |
| 268 // Schedule two animations on the same property. In this case, the two | |
| 269 // animations should run one after another. | |
| 270 TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) { | |
| 271 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 272 AnimationContainerElement* element = animator.get(); | |
| 273 animator->set_disable_timer_for_test(true); | |
| 274 TestLayerAnimationDelegate delegate; | |
| 275 animator->SetDelegate(&delegate); | |
| 276 | |
| 277 double start_opacity(0.0); | |
| 278 double middle_opacity(0.5); | |
| 279 double target_opacity(1.0); | |
| 280 | |
| 281 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 282 | |
| 283 delegate.SetOpacityFromAnimation(start_opacity); | |
| 284 | |
| 285 animator->ScheduleAnimation( | |
| 286 new LayerAnimationSequence( | |
| 287 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 288 | |
| 289 animator->ScheduleAnimation( | |
| 290 new LayerAnimationSequence( | |
| 291 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
| 292 | |
| 293 EXPECT_TRUE(animator->is_animating()); | |
| 294 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 295 | |
| 296 base::TimeTicks start_time = animator->last_step_time(); | |
| 297 | |
| 298 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 299 | |
| 300 EXPECT_TRUE(animator->is_animating()); | |
| 301 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 302 | |
| 303 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 304 | |
| 305 EXPECT_TRUE(animator->is_animating()); | |
| 306 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 307 | |
| 308 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); | |
| 309 | |
| 310 EXPECT_TRUE(animator->is_animating()); | |
| 311 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 312 | |
| 313 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
| 314 | |
| 315 EXPECT_FALSE(animator->is_animating()); | |
| 316 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 317 } | |
| 318 | |
| 319 // Schedule [{o}, {o,b}, {b}] and ensure that {b} doesn't run right away. That | |
| 320 // is, ensure that all animations targetting a particular property are run in | |
| 321 // order. | |
| 322 TEST(LayerAnimatorTest, ScheduleBlockedAnimation) { | |
| 323 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 324 AnimationContainerElement* element = animator.get(); | |
| 325 animator->set_disable_timer_for_test(true); | |
| 326 TestLayerAnimationDelegate delegate; | |
| 327 animator->SetDelegate(&delegate); | |
| 328 | |
| 329 double start_opacity(0.0); | |
| 330 double middle_opacity(0.5); | |
| 331 double target_opacity(1.0); | |
| 332 | |
| 333 gfx::Rect start_bounds, target_bounds, middle_bounds; | |
| 334 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); | |
| 335 start_bounds.set_x(-90); | |
| 336 target_bounds.set_x(90); | |
| 337 | |
| 338 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 339 | |
| 340 delegate.SetOpacityFromAnimation(start_opacity); | |
| 341 delegate.SetBoundsFromAnimation(start_bounds); | |
| 342 | |
| 343 animator->ScheduleAnimation( | |
| 344 new LayerAnimationSequence( | |
| 345 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 346 | |
| 347 scoped_ptr<LayerAnimationSequence> bounds_and_opacity( | |
| 348 new LayerAnimationSequence( | |
| 349 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
| 350 | |
| 351 bounds_and_opacity->AddElement( | |
| 352 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)); | |
| 353 | |
| 354 animator->ScheduleAnimation(bounds_and_opacity.release()); | |
| 355 | |
| 356 animator->ScheduleAnimation( | |
| 357 new LayerAnimationSequence( | |
| 358 LayerAnimationElement::CreateBoundsElement(start_bounds, delta))); | |
| 359 | |
| 360 EXPECT_TRUE(animator->is_animating()); | |
| 361 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 362 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
| 363 | |
| 364 base::TimeTicks start_time = animator->last_step_time(); | |
| 365 | |
| 366 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 367 | |
| 368 EXPECT_TRUE(animator->is_animating()); | |
| 369 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 370 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
| 371 | |
| 372 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 373 | |
| 374 EXPECT_TRUE(animator->is_animating()); | |
| 375 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 376 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
| 377 | |
| 378 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
| 379 | |
| 380 EXPECT_TRUE(animator->is_animating()); | |
| 381 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 382 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
| 383 | |
| 384 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000)); | |
| 385 | |
| 386 EXPECT_TRUE(animator->is_animating()); | |
| 387 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 388 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
| 389 | |
| 390 element->Step(start_time + base::TimeDelta::FromMilliseconds(4000)); | |
| 391 | |
| 392 EXPECT_FALSE(animator->is_animating()); | |
| 393 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 394 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
| 395 } | |
| 396 | |
| 397 // Schedule {o} and then schedule {o} and {b} together. In this case, since | |
| 398 // ScheduleTogether is being used, the bounds animation should not start until | |
| 399 // the second opacity animation starts. | |
| 400 TEST(LayerAnimatorTest, ScheduleTogether) { | |
| 401 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 402 AnimationContainerElement* element = animator.get(); | |
| 403 animator->set_disable_timer_for_test(true); | |
| 404 TestLayerAnimationDelegate delegate; | |
| 405 animator->SetDelegate(&delegate); | |
| 406 | |
| 407 double start_opacity(0.0); | |
| 408 double target_opacity(1.0); | |
| 409 | |
| 410 gfx::Rect start_bounds, target_bounds, middle_bounds; | |
| 411 start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50); | |
| 412 start_bounds.set_x(-90); | |
| 413 target_bounds.set_x(90); | |
| 414 | |
| 415 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 416 | |
| 417 delegate.SetOpacityFromAnimation(start_opacity); | |
| 418 delegate.SetBoundsFromAnimation(start_bounds); | |
| 419 | |
| 420 animator->ScheduleAnimation( | |
| 421 new LayerAnimationSequence( | |
| 422 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 423 | |
| 424 std::vector<LayerAnimationSequence*> sequences; | |
| 425 sequences.push_back(new LayerAnimationSequence( | |
| 426 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
| 427 sequences.push_back(new LayerAnimationSequence( | |
| 428 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); | |
| 429 | |
| 430 animator->ScheduleTogether(sequences); | |
| 431 | |
| 432 EXPECT_TRUE(animator->is_animating()); | |
| 433 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 434 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
| 435 | |
| 436 base::TimeTicks start_time = animator->last_step_time(); | |
| 437 | |
| 438 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 439 | |
| 440 EXPECT_TRUE(animator->is_animating()); | |
| 441 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 442 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
| 443 | |
| 444 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
| 445 | |
| 446 EXPECT_FALSE(animator->is_animating()); | |
| 447 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 448 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
| 449 } | |
| 450 | |
| 451 // Start animation (that can run immediately). This is the trivial case (see | |
| 452 // the trival case for ScheduleAnimation). | |
| 453 TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) { | |
| 454 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 455 AnimationContainerElement* element = animator.get(); | |
| 456 animator->set_disable_timer_for_test(true); | |
| 457 TestLayerAnimationDelegate delegate; | |
| 458 animator->SetDelegate(&delegate); | |
| 459 | |
| 460 double start_opacity(0.0); | |
| 461 double middle_opacity(0.5); | |
| 462 double target_opacity(1.0); | |
| 463 | |
| 464 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 465 | |
| 466 delegate.SetOpacityFromAnimation(start_opacity); | |
| 467 | |
| 468 animator->StartAnimation( | |
| 469 new LayerAnimationSequence( | |
| 470 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 471 | |
| 472 EXPECT_TRUE(animator->is_animating()); | |
| 473 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 474 | |
| 475 base::TimeTicks start_time = animator->last_step_time(); | |
| 476 | |
| 477 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 478 | |
| 479 EXPECT_TRUE(animator->is_animating()); | |
| 480 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 481 | |
| 482 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 483 | |
| 484 EXPECT_FALSE(animator->is_animating()); | |
| 485 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 486 } | |
| 487 | |
| 488 // Preempt by immediately setting new target. | |
| 489 TEST(LayerAnimatorTest, PreemptBySettingNewTarget) { | |
| 490 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 491 animator->set_disable_timer_for_test(true); | |
| 492 TestLayerAnimationDelegate delegate; | |
| 493 animator->SetDelegate(&delegate); | |
| 494 | |
| 495 double start_opacity(0.0); | |
| 496 double target_opacity(1.0); | |
| 497 | |
| 498 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 499 | |
| 500 delegate.SetOpacityFromAnimation(start_opacity); | |
| 501 | |
| 502 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET); | |
| 503 | |
| 504 animator->StartAnimation( | |
| 505 new LayerAnimationSequence( | |
| 506 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 507 | |
| 508 animator->StartAnimation( | |
| 509 new LayerAnimationSequence( | |
| 510 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
| 511 | |
| 512 EXPECT_FALSE(animator->is_animating()); | |
| 513 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 514 } | |
| 515 | |
| 516 // Preempt by animating to new target. | |
| 517 TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) { | |
| 518 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 519 AnimationContainerElement* element = animator.get(); | |
| 520 animator->set_disable_timer_for_test(true); | |
| 521 TestLayerAnimationDelegate delegate; | |
| 522 animator->SetDelegate(&delegate); | |
| 523 | |
| 524 double start_opacity(0.0); | |
| 525 double middle_opacity(0.5); | |
| 526 double target_opacity(1.0); | |
| 527 | |
| 528 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 529 | |
| 530 delegate.SetOpacityFromAnimation(start_opacity); | |
| 531 | |
| 532 animator->set_preemption_strategy( | |
| 533 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 534 | |
| 535 animator->StartAnimation( | |
| 536 new LayerAnimationSequence( | |
| 537 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 538 | |
| 539 base::TimeTicks start_time = animator->last_step_time(); | |
| 540 | |
| 541 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 542 | |
| 543 animator->StartAnimation( | |
| 544 new LayerAnimationSequence( | |
| 545 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
| 546 | |
| 547 EXPECT_TRUE(animator->is_animating()); | |
| 548 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 549 | |
| 550 animator->StartAnimation( | |
| 551 new LayerAnimationSequence( | |
| 552 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
| 553 | |
| 554 EXPECT_TRUE(animator->is_animating()); | |
| 555 | |
| 556 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 557 | |
| 558 EXPECT_TRUE(animator->is_animating()); | |
| 559 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | |
| 560 0.5 * (start_opacity + middle_opacity)); | |
| 561 | |
| 562 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); | |
| 563 | |
| 564 EXPECT_FALSE(animator->is_animating()); | |
| 565 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 566 } | |
| 567 | |
| 568 // Preempt by enqueuing the new animation. | |
| 569 TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) { | |
| 570 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 571 AnimationContainerElement* element = animator.get(); | |
| 572 animator->set_disable_timer_for_test(true); | |
| 573 TestLayerAnimationDelegate delegate; | |
| 574 animator->SetDelegate(&delegate); | |
| 575 | |
| 576 double start_opacity(0.0); | |
| 577 double middle_opacity(0.5); | |
| 578 double target_opacity(1.0); | |
| 579 | |
| 580 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 581 | |
| 582 delegate.SetOpacityFromAnimation(start_opacity); | |
| 583 | |
| 584 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); | |
| 585 | |
| 586 animator->StartAnimation( | |
| 587 new LayerAnimationSequence( | |
| 588 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 589 | |
| 590 base::TimeTicks start_time = animator->last_step_time(); | |
| 591 | |
| 592 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 593 | |
| 594 animator->StartAnimation( | |
| 595 new LayerAnimationSequence( | |
| 596 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
| 597 | |
| 598 EXPECT_TRUE(animator->is_animating()); | |
| 599 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 600 | |
| 601 EXPECT_TRUE(animator->is_animating()); | |
| 602 | |
| 603 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 604 | |
| 605 EXPECT_TRUE(animator->is_animating()); | |
| 606 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 607 | |
| 608 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); | |
| 609 | |
| 610 EXPECT_TRUE(animator->is_animating()); | |
| 611 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 612 | |
| 613 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
| 614 | |
| 615 EXPECT_FALSE(animator->is_animating()); | |
| 616 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 617 } | |
| 618 | |
| 619 // Start an animation when there are sequences waiting in the queue. In this | |
| 620 // case, all pending and running animations should be finished, and the new | |
| 621 // animation started. | |
| 622 TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) { | |
| 623 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 624 AnimationContainerElement* element = animator.get(); | |
| 625 animator->set_disable_timer_for_test(true); | |
| 626 TestLayerAnimationDelegate delegate; | |
| 627 animator->SetDelegate(&delegate); | |
| 628 | |
| 629 double start_opacity(0.0); | |
| 630 double middle_opacity(0.5); | |
| 631 double target_opacity(1.0); | |
| 632 | |
| 633 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 634 | |
| 635 delegate.SetOpacityFromAnimation(start_opacity); | |
| 636 | |
| 637 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | |
| 638 | |
| 639 animator->StartAnimation( | |
| 640 new LayerAnimationSequence( | |
| 641 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 642 | |
| 643 base::TimeTicks start_time = animator->last_step_time(); | |
| 644 | |
| 645 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 646 | |
| 647 animator->StartAnimation( | |
| 648 new LayerAnimationSequence( | |
| 649 LayerAnimationElement::CreateOpacityElement(middle_opacity, delta))); | |
| 650 | |
| 651 // Queue should now have two animations. Starting a third should replace the | |
| 652 // second. | |
| 653 animator->StartAnimation( | |
| 654 new LayerAnimationSequence( | |
| 655 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
| 656 | |
| 657 EXPECT_TRUE(animator->is_animating()); | |
| 658 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 659 | |
| 660 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 661 | |
| 662 EXPECT_TRUE(animator->is_animating()); | |
| 663 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 664 | |
| 665 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); | |
| 666 | |
| 667 EXPECT_TRUE(animator->is_animating()); | |
| 668 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
| 669 | |
| 670 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
| 671 | |
| 672 EXPECT_FALSE(animator->is_animating()); | |
| 673 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 674 } | |
| 675 | |
| 676 // Test that cyclic sequences continue to animate. | |
| 677 TEST(LayerAnimatorTest, CyclicSequences) { | |
| 678 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 679 AnimationContainerElement* element = animator.get(); | |
| 680 animator->set_disable_timer_for_test(true); | |
| 681 TestLayerAnimationDelegate delegate; | |
| 682 animator->SetDelegate(&delegate); | |
| 683 | |
| 684 double start_opacity(0.0); | |
| 685 double target_opacity(1.0); | |
| 686 | |
| 687 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 688 | |
| 689 delegate.SetOpacityFromAnimation(start_opacity); | |
| 690 | |
| 691 scoped_ptr<LayerAnimationSequence> sequence( | |
| 692 new LayerAnimationSequence( | |
| 693 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 694 | |
| 695 sequence->AddElement( | |
| 696 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); | |
| 697 | |
| 698 sequence->set_is_cyclic(true); | |
| 699 | |
| 700 animator->StartAnimation(sequence.release()); | |
| 701 | |
| 702 base::TimeTicks start_time = animator->last_step_time(); | |
| 703 | |
| 704 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 705 | |
| 706 EXPECT_TRUE(animator->is_animating()); | |
| 707 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 708 | |
| 709 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
| 710 | |
| 711 EXPECT_TRUE(animator->is_animating()); | |
| 712 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 713 | |
| 714 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000)); | |
| 715 | |
| 716 EXPECT_TRUE(animator->is_animating()); | |
| 717 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 718 | |
| 719 // Skip ahead by a lot. | |
| 720 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000000000)); | |
| 721 | |
| 722 EXPECT_TRUE(animator->is_animating()); | |
| 723 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
| 724 | |
| 725 // Skip ahead by a lot. | |
| 726 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000001000)); | |
| 727 | |
| 728 EXPECT_TRUE(animator->is_animating()); | |
| 729 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
| 730 | |
| 731 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY); | |
| 732 | |
| 733 EXPECT_FALSE(animator->is_animating()); | |
| 734 } | |
| 735 | |
| 736 TEST(LayerAnimatorTest, AddObserverExplicit) { | |
| 737 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 738 AnimationContainerElement* element = animator.get(); | |
| 739 animator->set_disable_timer_for_test(true); | |
| 740 TestLayerAnimationObserver observer; | |
| 741 TestLayerAnimationDelegate delegate; | |
| 742 animator->SetDelegate(&delegate); | |
| 743 animator->AddObserver(&observer); | |
| 744 observer.set_requires_notification_when_animator_destroyed(true); | |
| 745 | |
| 746 EXPECT_TRUE(!observer.last_ended_sequence()); | |
| 747 | |
| 748 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 749 | |
| 750 delegate.SetOpacityFromAnimation(0.0f); | |
| 751 | |
| 752 LayerAnimationSequence* sequence = new LayerAnimationSequence( | |
| 753 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | |
| 754 | |
| 755 animator->StartAnimation(sequence); | |
| 756 | |
| 757 EXPECT_EQ(observer.last_scheduled_sequence(), sequence); | |
| 758 | |
| 759 base::TimeTicks start_time = animator->last_step_time(); | |
| 760 | |
| 761 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 762 | |
| 763 EXPECT_EQ(observer.last_ended_sequence(), sequence); | |
| 764 | |
| 765 // |sequence| has been destroyed. Recreate it to test abort. | |
| 766 sequence = new LayerAnimationSequence( | |
| 767 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | |
| 768 | |
| 769 animator->StartAnimation(sequence); | |
| 770 | |
| 771 animator.reset(); | |
| 772 | |
| 773 EXPECT_EQ(observer.last_aborted_sequence(), sequence); | |
| 774 } | |
| 775 | |
| 776 // Tests that an observer added to a scoped settings object is still notified | |
| 777 // when the object goes out of scope. | |
| 778 TEST(LayerAnimatorTest, ImplicitAnimationObservers) { | |
| 779 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 780 AnimationContainerElement* element = animator.get(); | |
| 781 animator->set_disable_timer_for_test(true); | |
| 782 TestImplicitAnimationObserver observer(false); | |
| 783 TestLayerAnimationDelegate delegate; | |
| 784 animator->SetDelegate(&delegate); | |
| 785 | |
| 786 EXPECT_FALSE(observer.animations_completed()); | |
| 787 animator->SetOpacity(1.0f); | |
| 788 | |
| 789 { | |
| 790 ScopedLayerAnimationSettings settings(animator.get()); | |
| 791 settings.AddObserver(&observer); | |
| 792 animator->SetOpacity(0.0f); | |
| 793 } | |
| 794 | |
| 795 EXPECT_FALSE(observer.animations_completed()); | |
| 796 base::TimeTicks start_time = animator->last_step_time(); | |
| 797 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 798 EXPECT_TRUE(observer.animations_completed()); | |
| 799 EXPECT_FLOAT_EQ(0.0f, delegate.GetOpacityForAnimation()); | |
| 800 } | |
| 801 | |
| 802 // Tests that an observer added to a scoped settings object is still notified | |
| 803 // when the object goes out of scope due to the animation being interrupted. | |
| 804 TEST(LayerAnimatorTest, InterruptedImplicitAnimationObservers) { | |
| 805 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 806 animator->set_disable_timer_for_test(true); | |
| 807 TestImplicitAnimationObserver observer(false); | |
| 808 TestLayerAnimationDelegate delegate; | |
| 809 animator->SetDelegate(&delegate); | |
| 810 | |
| 811 EXPECT_FALSE(observer.animations_completed()); | |
| 812 animator->SetOpacity(1.0f); | |
| 813 | |
| 814 { | |
| 815 ScopedLayerAnimationSettings settings(animator.get()); | |
| 816 settings.AddObserver(&observer); | |
| 817 animator->SetOpacity(0.0f); | |
| 818 } | |
| 819 | |
| 820 EXPECT_FALSE(observer.animations_completed()); | |
| 821 // This should interrupt the implicit animation causing the observer to be | |
| 822 // notified immediately. | |
| 823 animator->SetOpacity(1.0f); | |
| 824 EXPECT_TRUE(observer.animations_completed()); | |
| 825 EXPECT_FLOAT_EQ(1.0f, delegate.GetOpacityForAnimation()); | |
| 826 } | |
| 827 | |
| 828 // Tests that an observer added to a scoped settings object is not notified | |
| 829 // when the animator is destroyed unless explicitly requested. | |
| 830 TEST(LayerAnimatorTest, ImplicitObserversAtAnimatorDestruction) { | |
| 831 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 832 animator->set_disable_timer_for_test(true); | |
| 833 TestImplicitAnimationObserver observer_notify(true); | |
| 834 TestImplicitAnimationObserver observer_do_not_notify(false); | |
| 835 TestLayerAnimationDelegate delegate; | |
| 836 animator->SetDelegate(&delegate); | |
| 837 | |
| 838 EXPECT_FALSE(observer_notify.animations_completed()); | |
| 839 EXPECT_FALSE(observer_do_not_notify.animations_completed()); | |
| 840 | |
| 841 animator->SetOpacity(1.0f); | |
| 842 | |
| 843 { | |
| 844 ScopedLayerAnimationSettings settings(animator.get()); | |
| 845 settings.AddObserver(&observer_notify); | |
| 846 settings.AddObserver(&observer_do_not_notify); | |
| 847 animator->SetOpacity(0.0f); | |
| 848 } | |
| 849 | |
| 850 EXPECT_FALSE(observer_notify.animations_completed()); | |
| 851 EXPECT_FALSE(observer_do_not_notify.animations_completed()); | |
| 852 animator.reset(NULL); | |
| 853 EXPECT_TRUE(observer_notify.animations_completed()); | |
| 854 EXPECT_FALSE(observer_do_not_notify.animations_completed()); | |
| 855 } | |
| 856 | |
| 857 | |
| 858 TEST(LayerAnimatorTest, RemoveObserverShouldRemoveFromSequences) { | |
| 859 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 860 AnimationContainerElement* element = animator.get(); | |
| 861 animator->set_disable_timer_for_test(true); | |
| 862 TestLayerAnimationObserver observer; | |
| 863 TestLayerAnimationObserver removed_observer; | |
| 864 TestLayerAnimationDelegate delegate; | |
| 865 animator->SetDelegate(&delegate); | |
| 866 | |
| 867 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 868 | |
| 869 LayerAnimationSequence* sequence = new LayerAnimationSequence( | |
| 870 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | |
| 871 | |
| 872 sequence->AddObserver(&observer); | |
| 873 sequence->AddObserver(&removed_observer); | |
| 874 | |
| 875 animator->StartAnimation(sequence); | |
| 876 | |
| 877 EXPECT_EQ(observer.last_scheduled_sequence(), sequence); | |
| 878 EXPECT_TRUE(!observer.last_ended_sequence()); | |
| 879 EXPECT_EQ(removed_observer.last_scheduled_sequence(), sequence); | |
| 880 EXPECT_TRUE(!removed_observer.last_ended_sequence()); | |
| 881 | |
| 882 // This should stop the observer from observing sequence. | |
| 883 animator->RemoveObserver(&removed_observer); | |
| 884 | |
| 885 base::TimeTicks start_time = animator->last_step_time(); | |
| 886 | |
| 887 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 888 | |
| 889 EXPECT_EQ(observer.last_ended_sequence(), sequence); | |
| 890 EXPECT_TRUE(!removed_observer.last_ended_sequence()); | |
| 891 } | |
| 892 | |
| 893 TEST(LayerAnimatorTest, ObserverReleasedBeforeAnimationSequenceEnds) { | |
| 894 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 895 animator->set_disable_timer_for_test(true); | |
| 896 | |
| 897 scoped_ptr<TestLayerAnimationObserver> observer( | |
| 898 new TestLayerAnimationObserver); | |
| 899 TestLayerAnimationDelegate delegate; | |
| 900 animator->SetDelegate(&delegate); | |
| 901 animator->AddObserver(observer.get()); | |
| 902 | |
| 903 delegate.SetOpacityFromAnimation(0.0f); | |
| 904 | |
| 905 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 906 LayerAnimationSequence* sequence = new LayerAnimationSequence( | |
| 907 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | |
| 908 | |
| 909 animator->StartAnimation(sequence); | |
| 910 | |
| 911 // |observer| should be attached to |sequence|. | |
| 912 EXPECT_EQ(static_cast<size_t>(1), sequence->observers_.size()); | |
| 913 | |
| 914 // Now, release |observer| | |
| 915 observer.reset(); | |
| 916 | |
| 917 // And |sequence| should no longer be attached to |observer|. | |
| 918 EXPECT_EQ(static_cast<size_t>(0), sequence->observers_.size()); | |
| 919 } | |
| 920 | |
| 921 TEST(LayerAnimatorTest, ObserverAttachedAfterAnimationStarted) { | |
| 922 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 923 AnimationContainerElement* element = animator.get(); | |
| 924 animator->set_disable_timer_for_test(true); | |
| 925 | |
| 926 TestImplicitAnimationObserver observer(false); | |
| 927 TestLayerAnimationDelegate delegate; | |
| 928 animator->SetDelegate(&delegate); | |
| 929 | |
| 930 delegate.SetOpacityFromAnimation(0.0f); | |
| 931 | |
| 932 { | |
| 933 ScopedLayerAnimationSettings setter(animator.get()); | |
| 934 | |
| 935 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 936 LayerAnimationSequence* sequence = new LayerAnimationSequence( | |
| 937 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | |
| 938 | |
| 939 animator->StartAnimation(sequence); | |
| 940 base::TimeTicks start_time = animator->last_step_time(); | |
| 941 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 942 | |
| 943 setter.AddObserver(&observer); | |
| 944 | |
| 945 // Start observing an in-flight animation. | |
| 946 sequence->AddObserver(&observer); | |
| 947 | |
| 948 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
| 949 } | |
| 950 | |
| 951 EXPECT_TRUE(observer.animations_completed()); | |
| 952 } | |
| 953 | |
| 954 TEST(LayerAnimatorTest, ObserverDetachedBeforeAnimationFinished) { | |
| 955 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 956 AnimationContainerElement* element = animator.get(); | |
| 957 animator->set_disable_timer_for_test(true); | |
| 958 | |
| 959 TestImplicitAnimationObserver observer(false); | |
| 960 TestLayerAnimationDelegate delegate; | |
| 961 animator->SetDelegate(&delegate); | |
| 962 | |
| 963 delegate.SetOpacityFromAnimation(0.0f); | |
| 964 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 965 LayerAnimationSequence* sequence = new LayerAnimationSequence( | |
| 966 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | |
| 967 | |
| 968 { | |
| 969 ScopedLayerAnimationSettings setter(animator.get()); | |
| 970 setter.AddObserver(&observer); | |
| 971 | |
| 972 animator->StartAnimation(sequence); | |
| 973 base::TimeTicks start_time = animator->last_step_time(); | |
| 974 element->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
| 975 } | |
| 976 | |
| 977 EXPECT_FALSE(observer.animations_completed()); | |
| 978 | |
| 979 // Stop observing an in-flight animation. | |
| 980 sequence->RemoveObserver(&observer); | |
| 981 | |
| 982 EXPECT_TRUE(observer.animations_completed()); | |
| 983 } | |
| 984 | |
| 985 // This checks that if an animation is deleted due to a callback, that the | |
| 986 // animator does not try to use the deleted animation. For example, if we have | |
| 987 // two running animations, and the first finishes and the resulting callback | |
| 988 // causes the second to be deleted, we should not attempt to animate the second | |
| 989 // animation. | |
| 990 TEST(LayerAnimatorTest, ObserverDeletesAnimations) { | |
| 991 LayerAnimator::set_disable_animations_for_test(false); | |
| 992 scoped_ptr<LayerAnimator> animator(new TestLayerAnimator()); | |
| 993 AnimationContainerElement* element = animator.get(); | |
| 994 animator->set_disable_timer_for_test(true); | |
| 995 TestLayerAnimationDelegate delegate; | |
| 996 animator->SetDelegate(&delegate); | |
| 997 | |
| 998 double start_opacity(0.0); | |
| 999 double target_opacity(1.0); | |
| 1000 | |
| 1001 gfx::Rect start_bounds(0, 0, 50, 50); | |
| 1002 gfx::Rect target_bounds(5, 5, 5, 5); | |
| 1003 | |
| 1004 delegate.SetOpacityFromAnimation(start_opacity); | |
| 1005 delegate.SetBoundsFromAnimation(start_bounds); | |
| 1006 | |
| 1007 base::TimeDelta opacity_delta = base::TimeDelta::FromSeconds(1); | |
| 1008 base::TimeDelta halfway_delta = base::TimeDelta::FromSeconds(2); | |
| 1009 base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(3); | |
| 1010 | |
| 1011 LayerAnimationSequence* to_delete = new LayerAnimationSequence( | |
| 1012 LayerAnimationElement::CreateBoundsElement(target_bounds, bounds_delta)); | |
| 1013 | |
| 1014 scoped_ptr<DeletingLayerAnimationObserver> observer( | |
| 1015 new DeletingLayerAnimationObserver(animator.get(), to_delete)); | |
| 1016 | |
| 1017 animator->AddObserver(observer.get()); | |
| 1018 | |
| 1019 animator->StartAnimation( | |
| 1020 new LayerAnimationSequence( | |
| 1021 LayerAnimationElement::CreateOpacityElement( | |
| 1022 target_opacity, opacity_delta))); | |
| 1023 | |
| 1024 animator->StartAnimation(to_delete); | |
| 1025 | |
| 1026 base::TimeTicks start_time = animator->last_step_time(); | |
| 1027 element->Step(start_time + halfway_delta); | |
| 1028 | |
| 1029 animator->RemoveObserver(observer.get()); | |
| 1030 } | |
| 1031 | |
| 1032 // Check that setting a property during an animation with a default animator | |
| 1033 // cancels the original animation. | |
| 1034 TEST(LayerAnimatorTest, SettingPropertyDuringAnAnimation) { | |
| 1035 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 1036 animator->set_disable_timer_for_test(true); | |
| 1037 TestLayerAnimationDelegate delegate; | |
| 1038 animator->SetDelegate(&delegate); | |
| 1039 | |
| 1040 double start_opacity(0.0); | |
| 1041 double target_opacity(1.0); | |
| 1042 | |
| 1043 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 1044 | |
| 1045 delegate.SetOpacityFromAnimation(start_opacity); | |
| 1046 | |
| 1047 scoped_ptr<LayerAnimationSequence> sequence( | |
| 1048 new LayerAnimationSequence( | |
| 1049 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
| 1050 | |
| 1051 animator->StartAnimation(sequence.release()); | |
| 1052 | |
| 1053 animator->SetOpacity(0.5); | |
| 1054 | |
| 1055 EXPECT_FALSE(animator->is_animating()); | |
| 1056 EXPECT_EQ(0.5, animator->GetTargetOpacity()); | |
| 1057 } | |
| 1058 | |
| 1059 // Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the | |
| 1060 // second sequence to be leaked. | |
| 1061 TEST(LayerAnimatorTest, ImmediatelySettingNewTargetDoesNotLeak) { | |
| 1062 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 1063 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET); | |
| 1064 animator->set_disable_timer_for_test(true); | |
| 1065 TestLayerAnimationDelegate delegate; | |
| 1066 animator->SetDelegate(&delegate); | |
| 1067 | |
| 1068 gfx::Rect start_bounds(0, 0, 50, 50); | |
| 1069 gfx::Rect middle_bounds(10, 10, 100, 100); | |
| 1070 gfx::Rect target_bounds(5, 5, 5, 5); | |
| 1071 | |
| 1072 delegate.SetBoundsFromAnimation(start_bounds); | |
| 1073 | |
| 1074 { | |
| 1075 // start an implicit bounds animation. | |
| 1076 ScopedLayerAnimationSettings settings(animator.get()); | |
| 1077 animator->SetBounds(middle_bounds); | |
| 1078 } | |
| 1079 | |
| 1080 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); | |
| 1081 | |
| 1082 int num_live_instances = 0; | |
| 1083 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
| 1084 scoped_ptr<TestLayerAnimationSequence> sequence( | |
| 1085 new TestLayerAnimationSequence( | |
| 1086 LayerAnimationElement::CreateBoundsElement(target_bounds, delta), | |
| 1087 &num_live_instances)); | |
| 1088 | |
| 1089 EXPECT_EQ(1, num_live_instances); | |
| 1090 | |
| 1091 // This should interrupt the running sequence causing us to immediately set | |
| 1092 // the target value. The sequence should alse be destructed. | |
| 1093 animator->StartAnimation(sequence.release()); | |
| 1094 | |
| 1095 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); | |
| 1096 EXPECT_EQ(0, num_live_instances); | |
| 1097 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
| 1098 } | |
| 1099 | |
| 1100 // Verifies GetTargetOpacity() works when multiple sequences are scheduled. | |
| 1101 TEST(LayerAnimatorTest, GetTargetOpacity) { | |
| 1102 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
| 1103 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); | |
| 1104 animator->set_disable_timer_for_test(true); | |
| 1105 TestLayerAnimationDelegate delegate; | |
| 1106 animator->SetDelegate(&delegate); | |
| 1107 | |
| 1108 delegate.SetOpacityFromAnimation(0.0); | |
| 1109 | |
| 1110 { | |
| 1111 ScopedLayerAnimationSettings settings(animator.get()); | |
| 1112 animator->SetOpacity(0.5); | |
| 1113 EXPECT_EQ(0.5, animator->GetTargetOpacity()); | |
| 1114 | |
| 1115 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1. | |
| 1116 animator->SetOpacity(1.0); | |
| 1117 EXPECT_EQ(1.0, animator->GetTargetOpacity()); | |
| 1118 } | |
| 1119 } | |
| 1120 | |
| 1121 } // namespace ui | |
| OLD | NEW |