1 // Copyright 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 "cc/animation/layer_animation_controller.h" 6 7 #include "cc/animation/animation.h" 8 #include "cc/animation/animation_curve.h" 9 #include "cc/animation/animation_delegate.h" 10 #include "cc/animation/keyframed_animation_curve.h" 11 #include "cc/animation/transform_operations.h" 12 #include "cc/test/animation_test_common.h" 13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "ui/gfx/transform.h" 16 17 namespace cc { 18 namespace { 19 20 void ExpectTranslateX(double translate_x, const gfx::Transform& matrix) { 21 EXPECT_FLOAT_EQ(translate_x, matrix.matrix().getDouble(0, 3)); } 22 23 scoped_ptr<Animation> CreateAnimation(scoped_ptr<AnimationCurve> curve, 24 int id, 25 Animation::TargetProperty property) { 26 return Animation::Create(curve.Pass(), 0, id, property); 27 } 28 29 TEST(LayerAnimationControllerTest, SyncNewAnimation) { 30 FakeLayerAnimationValueObserver dummy_impl; 31 scoped_refptr<LayerAnimationController> controller_impl( 32 LayerAnimationController::Create(0)); 33 controller_impl->AddValueObserver(&dummy_impl); 34 FakeLayerAnimationValueObserver dummy; 35 scoped_refptr<LayerAnimationController> controller( 36 LayerAnimationController::Create(0)); 37 controller->AddValueObserver(&dummy); 38 39 EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity)); 40 41 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false); 42 int group_id = controller->GetAnimation(Animation::Opacity)->group(); 43 44 controller->PushAnimationUpdatesTo(controller_impl.get()); 45 46 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)); 47 EXPECT_EQ(Animation::WaitingForTargetAvailability, 48 controller_impl->GetAnimation(group_id, 49 Animation::Opacity)->run_state()); 50 } 51 52 // If an animation is started on the impl thread before it is ticked on the main 53 // thread, we must be sure to respect the synchronized start time. 54 TEST(LayerAnimationControllerTest, DoNotClobberStartTimes) { 55 FakeLayerAnimationValueObserver dummy_impl; 56 scoped_refptr<LayerAnimationController> controller_impl( 57 LayerAnimationController::Create(0)); 58 controller_impl->AddValueObserver(&dummy_impl); 59 FakeLayerAnimationValueObserver dummy; 60 scoped_refptr<LayerAnimationController> controller( 61 LayerAnimationController::Create(0)); 62 controller->AddValueObserver(&dummy); 63 64 EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity)); 65 66 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false); 67 int group_id = controller->GetAnimation(Animation::Opacity)->group(); 68 69 controller->PushAnimationUpdatesTo(controller_impl.get()); 70 71 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)); 72 EXPECT_EQ(Animation::WaitingForTargetAvailability, 73 controller_impl->GetAnimation(group_id, 74 Animation::Opacity)->run_state()); 75 76 AnimationEventsVector events; 77 controller_impl->Animate(1.0); 78 controller_impl->UpdateState(true, &events); 79 80 // Synchronize the start times. 81 EXPECT_EQ(1u, events.size()); 82 controller->NotifyAnimationStarted(events[0], 0.0); 83 EXPECT_EQ(controller->GetAnimation(group_id, 84 Animation::Opacity)->start_time(), 85 controller_impl->GetAnimation(group_id, 86 Animation::Opacity)->start_time()); 87 88 // Start the animation on the main thread. Should not affect the start time. 89 controller->Animate(1.5); 90 controller->UpdateState(true, NULL); 91 EXPECT_EQ(controller->GetAnimation(group_id, 92 Animation::Opacity)->start_time(), 93 controller_impl->GetAnimation(group_id, 94 Animation::Opacity)->start_time()); 95 } 96 97 TEST(LayerAnimationControllerTest, SyncPauseAndResume) { 98 FakeLayerAnimationValueObserver dummy_impl; 99 scoped_refptr<LayerAnimationController> controller_impl( 100 LayerAnimationController::Create(0)); 101 controller_impl->AddValueObserver(&dummy_impl); 102 FakeLayerAnimationValueObserver dummy; 103 scoped_refptr<LayerAnimationController> controller( 104 LayerAnimationController::Create(0)); 105 controller->AddValueObserver(&dummy); 106 107 EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity)); 108 109 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false); 110 int group_id = controller->GetAnimation(Animation::Opacity)->group(); 111 112 controller->PushAnimationUpdatesTo(controller_impl.get()); 113 114 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)); 115 EXPECT_EQ(Animation::WaitingForTargetAvailability, 116 controller_impl->GetAnimation(group_id, 117 Animation::Opacity)->run_state()); 118 119 // Start the animations on each controller. 120 AnimationEventsVector events; 121 controller_impl->Animate(0.0); 122 controller_impl->UpdateState(true, &events); 123 controller->Animate(0.0); 124 controller->UpdateState(true, NULL); 125 EXPECT_EQ(Animation::Running, 126 controller_impl->GetAnimation(group_id, 127 Animation::Opacity)->run_state()); 128 EXPECT_EQ(Animation::Running, 129 controller->GetAnimation(group_id, 130 Animation::Opacity)->run_state()); 131 132 // Pause the main-thread animation. 133 controller->SuspendAnimations(1.0); 134 EXPECT_EQ(Animation::Paused, 135 controller->GetAnimation(group_id, 136 Animation::Opacity)->run_state()); 137 138 // The pause run state change should make it to the impl thread controller. 139 controller->PushAnimationUpdatesTo(controller_impl.get()); 140 EXPECT_EQ(Animation::Paused, 141 controller_impl->GetAnimation(group_id, 142 Animation::Opacity)->run_state()); 143 144 // Resume the main-thread animation. 145 controller->ResumeAnimations(2.0); 146 EXPECT_EQ(Animation::Running, 147 controller->GetAnimation(group_id, 148 Animation::Opacity)->run_state()); 149 150 // The pause run state change should make it to the impl thread controller. 151 controller->PushAnimationUpdatesTo(controller_impl.get()); 152 EXPECT_EQ(Animation::Running, 153 controller_impl->GetAnimation(group_id, 154 Animation::Opacity)->run_state()); 155 } 156 157 TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) { 158 FakeLayerAnimationValueObserver dummy_impl; 159 scoped_refptr<LayerAnimationController> controller_impl( 160 LayerAnimationController::Create(0)); 161 controller_impl->AddValueObserver(&dummy_impl); 162 FakeLayerAnimationValueObserver dummy; 163 scoped_refptr<LayerAnimationController> controller( 164 LayerAnimationController::Create(0)); 165 controller->AddValueObserver(&dummy); 166 167 EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity)); 168 169 int animation_id = 170 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false); 171 int group_id = controller->GetAnimation(Animation::Opacity)->group(); 172 173 controller->PushAnimationUpdatesTo(controller_impl.get()); 174 175 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)); 176 EXPECT_EQ(Animation::WaitingForTargetAvailability, 177 controller_impl->GetAnimation(group_id, 178 Animation::Opacity)->run_state()); 179 180 // Notify main thread controller that the animation has started. 181 AnimationEvent animation_started_event( 182 AnimationEvent::Started, 0, group_id, Animation::Opacity, 0); 183 controller->NotifyAnimationStarted(animation_started_event, 0.0); 184 185 // Force animation to complete on impl thread. 186 controller_impl->RemoveAnimation(animation_id); 187 188 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)); 189 190 controller->PushAnimationUpdatesTo(controller_impl.get()); 191 192 // Even though the main thread has a 'new' animation, it should not be pushed 193 // because the animation has already completed on the impl thread. 194 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)); 195 } 196 197 // Ensure that a finished animation is eventually deleted by both the 198 // main-thread and the impl-thread controllers. 199 TEST(LayerAnimationControllerTest, AnimationsAreDeleted) { 200 FakeLayerAnimationValueObserver dummy; 201 FakeLayerAnimationValueObserver dummy_impl; 202 scoped_ptr<AnimationEventsVector> events( 203 make_scoped_ptr(new AnimationEventsVector)); 204 scoped_refptr<LayerAnimationController> controller( 205 LayerAnimationController::Create(0)); 206 scoped_refptr<LayerAnimationController> controller_impl( 207 LayerAnimationController::Create(0)); 208 controller->AddValueObserver(&dummy); 209 controller_impl->AddValueObserver(&dummy_impl); 210 211 AddOpacityTransitionToController(controller.get(), 1.0, 0.0f, 1.0f, false); 212 controller->Animate(0.0); 213 controller->UpdateState(true, NULL); 214 controller->PushAnimationUpdatesTo(controller_impl.get()); 215 216 controller_impl->Animate(0.5); 217 controller_impl->UpdateState(true, events.get()); 218 219 // There should be a Started event for the animation. 220 EXPECT_EQ(1u, events->size()); 221 EXPECT_EQ(AnimationEvent::Started, (*events)[0].type); 222 controller->NotifyAnimationStarted((*events)[0], 0.0); 223 224 controller->Animate(1.0); 225 controller->UpdateState(true, NULL); 226 227 events.reset(new AnimationEventsVector); 228 controller_impl->Animate(2.0); 229 controller_impl->UpdateState(true, events.get()); 230 231 // There should be a Finished event for the animation. 232 EXPECT_EQ(1u, events->size()); 233 EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type); 234 235 // Neither controller should have deleted the animation yet. 236 EXPECT_TRUE(controller->GetAnimation(Animation::Opacity)); 237 EXPECT_TRUE(controller_impl->GetAnimation(Animation::Opacity)); 238 239 controller->NotifyAnimationFinished((*events)[0], 0.0); 240 241 controller->Animate(3.0); 242 controller->UpdateState(true, NULL); 243 244 controller->PushAnimationUpdatesTo(controller_impl.get()); 245 246 // Both controllers should now have deleted the animation. 247 EXPECT_FALSE(controller->has_any_animation()); 248 EXPECT_FALSE(controller_impl->has_any_animation()); 249 } 250 251 // Tests that transitioning opacity from 0 to 1 works as expected. 252 253 static const AnimationEvent* GetMostRecentPropertyUpdateEvent( 254 const AnimationEventsVector* events) { 255 const AnimationEvent* event = 0; 256 for (size_t i = 0; i < events->size(); ++i) 257 if ((*events)[i].type == AnimationEvent::PropertyUpdate) 258 event = &(*events)[i]; 259 260 return event; 261 } 262 263 TEST(LayerAnimationControllerTest, TrivialTransition) { 264 scoped_ptr<AnimationEventsVector> events( 265 make_scoped_ptr(new AnimationEventsVector)); 266 FakeLayerAnimationValueObserver dummy; 267 scoped_refptr<LayerAnimationController> controller( 268 LayerAnimationController::Create(0)); 269 controller->AddValueObserver(&dummy); 270 271 scoped_ptr<Animation> to_add(CreateAnimation( 272 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 273 1, 274 Animation::Opacity)); 275 276 controller->AddAnimation(to_add.Pass()); 277 controller->Animate(0.0); 278 controller->UpdateState(true, events.get()); 279 EXPECT_TRUE(controller->HasActiveAnimation()); 280 EXPECT_EQ(0.f, dummy.opacity()); 281 // A non-impl-only animation should not generate property updates. 282 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get()); 283 EXPECT_FALSE(event); 284 controller->Animate(1.0); 285 controller->UpdateState(true, events.get()); 286 EXPECT_EQ(1.f, dummy.opacity()); 287 EXPECT_FALSE(controller->HasActiveAnimation()); 288 event = GetMostRecentPropertyUpdateEvent(events.get()); 289 EXPECT_FALSE(event); 290 } 291 292 TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) { 293 scoped_ptr<AnimationEventsVector> events( 294 make_scoped_ptr(new AnimationEventsVector)); 295 FakeLayerAnimationValueObserver dummy_impl; 296 scoped_refptr<LayerAnimationController> controller_impl( 297 LayerAnimationController::Create(0)); 298 controller_impl->AddValueObserver(&dummy_impl); 299 300 scoped_ptr<Animation> to_add(CreateAnimation( 301 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 302 1, 303 Animation::Opacity)); 304 to_add->set_is_impl_only(true); 305 306 controller_impl->AddAnimation(to_add.Pass()); 307 controller_impl->Animate(0.0); 308 controller_impl->UpdateState(true, events.get()); 309 EXPECT_TRUE(controller_impl->HasActiveAnimation()); 310 EXPECT_EQ(0.f, dummy_impl.opacity()); 311 EXPECT_EQ(2u, events->size()); 312 const AnimationEvent* start_opacity_event = 313 GetMostRecentPropertyUpdateEvent(events.get()); 314 EXPECT_EQ(0.f, start_opacity_event->opacity); 315 316 controller_impl->Animate(1.0); 317 controller_impl->UpdateState(true, events.get()); 318 EXPECT_EQ(1.f, dummy_impl.opacity()); 319 EXPECT_FALSE(controller_impl->HasActiveAnimation()); 320 EXPECT_EQ(4u, events->size()); 321 const AnimationEvent* end_opacity_event = 322 GetMostRecentPropertyUpdateEvent(events.get()); 323 EXPECT_EQ(1.f, end_opacity_event->opacity); 324 } 325 326 TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) { 327 scoped_ptr<AnimationEventsVector> events( 328 make_scoped_ptr(new AnimationEventsVector)); 329 FakeLayerAnimationValueObserver dummy_impl; 330 scoped_refptr<LayerAnimationController> controller_impl( 331 LayerAnimationController::Create(0)); 332 controller_impl->AddValueObserver(&dummy_impl); 333 334 // Choose different values for x and y to avoid coincidental values in the 335 // observed transforms. 336 const float delta_x = 3; 337 const float delta_y = 4; 338 339 scoped_ptr<KeyframedTransformAnimationCurve> curve( 340 KeyframedTransformAnimationCurve::Create()); 341 342 // Create simple Transform animation. 343 TransformOperations operations; 344 curve->AddKeyframe(TransformKeyframe::Create( 345 0, operations, scoped_ptr<cc::TimingFunction>())); 346 operations.AppendTranslate(delta_x, delta_y, 0); 347 curve->AddKeyframe(TransformKeyframe::Create( 348 1, operations, scoped_ptr<cc::TimingFunction>())); 349 350 scoped_ptr<Animation> animation(Animation::Create( 351 curve.PassAs<AnimationCurve>(), 1, 0, Animation::Transform)); 352 animation->set_is_impl_only(true); 353 controller_impl->AddAnimation(animation.Pass()); 354 355 // Run animation. 356 controller_impl->Animate(0.0); 357 controller_impl->UpdateState(true, events.get()); 358 EXPECT_TRUE(controller_impl->HasActiveAnimation()); 359 EXPECT_EQ(gfx::Transform(), dummy_impl.transform()); 360 EXPECT_EQ(2u, events->size()); 361 const AnimationEvent* start_transform_event = 362 GetMostRecentPropertyUpdateEvent(events.get()); 363 ASSERT_TRUE(start_transform_event); 364 EXPECT_EQ(gfx::Transform(), start_transform_event->transform); 365 EXPECT_TRUE(start_transform_event->is_impl_only); 366 367 gfx::Transform expected_transform; 368 expected_transform.Translate(delta_x, delta_y); 369 370 controller_impl->Animate(1.0); 371 controller_impl->UpdateState(true, events.get()); 372 EXPECT_EQ(expected_transform, dummy_impl.transform()); 373 EXPECT_FALSE(controller_impl->HasActiveAnimation()); 374 EXPECT_EQ(4u, events->size()); 375 const AnimationEvent* end_transform_event = 376 GetMostRecentPropertyUpdateEvent(events.get()); 377 EXPECT_EQ(expected_transform, end_transform_event->transform); 378 EXPECT_TRUE(end_transform_event->is_impl_only); 379 } 380 381 class FakeAnimationDelegate : public AnimationDelegate { 382 public: 383 FakeAnimationDelegate() 384 : started_(false), 385 finished_(false) {} 386 387 virtual void NotifyAnimationStarted(double time) OVERRIDE { 388 started_ = true; 389 } 390 391 virtual void NotifyAnimationFinished(double time) OVERRIDE { 392 finished_ = true; 393 } 394 395 bool started() { return started_; } 396 397 bool finished() { return finished_; } 398 399 private: 400 bool started_; 401 bool finished_; 402 }; 403 404 // Tests that impl-only animations lead to start and finished notifications 405 // being sent to the main thread controller's animation delegate. 406 TEST(LayerAnimationControllerTest, 407 NotificationsForImplOnlyAnimationsAreSentToMainThreadDelegate) { 408 FakeLayerAnimationValueObserver dummy_impl; 409 scoped_refptr<LayerAnimationController> controller_impl( 410 LayerAnimationController::Create(0)); 411 controller_impl->AddValueObserver(&dummy_impl); 412 scoped_ptr<AnimationEventsVector> events( 413 make_scoped_ptr(new AnimationEventsVector)); 414 FakeLayerAnimationValueObserver dummy; 415 scoped_refptr<LayerAnimationController> controller( 416 LayerAnimationController::Create(0)); 417 controller->AddValueObserver(&dummy); 418 FakeAnimationDelegate delegate; 419 controller->set_layer_animation_delegate(&delegate); 420 421 scoped_ptr<Animation> to_add(CreateAnimation( 422 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 423 1, 424 Animation::Opacity)); 425 to_add->set_is_impl_only(true); 426 controller_impl->AddAnimation(to_add.Pass()); 427 428 controller_impl->Animate(0.0); 429 controller_impl->UpdateState(true, events.get()); 430 431 // We should receive 2 events (a started notification and a property update). 432 EXPECT_EQ(2u, events->size()); 433 EXPECT_EQ(AnimationEvent::Started, (*events)[0].type); 434 EXPECT_TRUE((*events)[0].is_impl_only); 435 EXPECT_EQ(AnimationEvent::PropertyUpdate, (*events)[1].type); 436 EXPECT_TRUE((*events)[1].is_impl_only); 437 438 // Passing on the start event to the main thread controller should cause the 439 // delegate to get notified. 440 EXPECT_FALSE(delegate.started()); 441 controller->NotifyAnimationStarted((*events)[0], 0.0); 442 EXPECT_TRUE(delegate.started()); 443 444 events.reset(new AnimationEventsVector); 445 controller_impl->Animate(1.0); 446 controller_impl->UpdateState(true, events.get()); 447 448 // We should receive 2 events (a finished notification and a property update). 449 EXPECT_EQ(2u, events->size()); 450 EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type); 451 EXPECT_TRUE((*events)[0].is_impl_only); 452 EXPECT_EQ(AnimationEvent::PropertyUpdate, (*events)[1].type); 453 EXPECT_TRUE((*events)[1].is_impl_only); 454 455 // Passing on the finished event to the main thread controller should cause 456 // the delegate to get notified. 457 EXPECT_FALSE(delegate.finished()); 458 controller->NotifyAnimationFinished((*events)[0], 0.0); 459 EXPECT_TRUE(delegate.finished()); 460 } 461 462 // Tests animations that are waiting for a synchronized start time do not 463 // finish. 464 TEST(LayerAnimationControllerTest, 465 AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) { 466 scoped_ptr<AnimationEventsVector> events( 467 make_scoped_ptr(new AnimationEventsVector)); 468 FakeLayerAnimationValueObserver dummy; 469 scoped_refptr<LayerAnimationController> controller( 470 LayerAnimationController::Create(0)); 471 controller->AddValueObserver(&dummy); 472 473 scoped_ptr<Animation> to_add(CreateAnimation( 474 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 475 1, 476 Animation::Opacity)); 477 to_add->set_needs_synchronized_start_time(true); 478 479 // We should pause at the first keyframe indefinitely waiting for that 480 // animation to start. 481 controller->AddAnimation(to_add.Pass()); 482 controller->Animate(0.0); 483 controller->UpdateState(true, events.get()); 484 EXPECT_TRUE(controller->HasActiveAnimation()); 485 EXPECT_EQ(0.f, dummy.opacity()); 486 controller->Animate(1.0); 487 controller->UpdateState(true, events.get()); 488 EXPECT_TRUE(controller->HasActiveAnimation()); 489 EXPECT_EQ(0.f, dummy.opacity()); 490 controller->Animate(2.0); 491 controller->UpdateState(true, events.get()); 492 EXPECT_TRUE(controller->HasActiveAnimation()); 493 EXPECT_EQ(0.f, dummy.opacity()); 494 495 // Send the synchronized start time. 496 controller->NotifyAnimationStarted( 497 AnimationEvent(AnimationEvent::Started, 0, 1, Animation::Opacity, 2), 498 0.0); 499 controller->Animate(5.0); 500 controller->UpdateState(true, events.get()); 501 EXPECT_EQ(1.f, dummy.opacity()); 502 EXPECT_FALSE(controller->HasActiveAnimation()); 503 } 504 505 // Tests that two queued animations affecting the same property run in sequence. 506 TEST(LayerAnimationControllerTest, TrivialQueuing) { 507 scoped_ptr<AnimationEventsVector> events( 508 make_scoped_ptr(new AnimationEventsVector)); 509 FakeLayerAnimationValueObserver dummy; 510 scoped_refptr<LayerAnimationController> controller( 511 LayerAnimationController::Create(0)); 512 controller->AddValueObserver(&dummy); 513 514 controller->AddAnimation(CreateAnimation( 515 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 516 1, 517 Animation::Opacity)); 518 controller->AddAnimation(CreateAnimation( 519 scoped_ptr<AnimationCurve>( 520 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(), 521 2, 522 Animation::Opacity)); 523 524 controller->Animate(0.0); 525 controller->UpdateState(true, events.get()); 526 EXPECT_TRUE(controller->HasActiveAnimation()); 527 EXPECT_EQ(0.f, dummy.opacity()); 528 controller->Animate(1.0); 529 controller->UpdateState(true, events.get()); 530 EXPECT_TRUE(controller->HasActiveAnimation()); 531 EXPECT_EQ(1.f, dummy.opacity()); 532 controller->Animate(2.0); 533 controller->UpdateState(true, events.get()); 534 EXPECT_EQ(0.5f, dummy.opacity()); 535 EXPECT_FALSE(controller->HasActiveAnimation()); 536 } 537 538 // Tests interrupting a transition with another transition. 539 TEST(LayerAnimationControllerTest, Interrupt) { 540 scoped_ptr<AnimationEventsVector> events( 541 make_scoped_ptr(new AnimationEventsVector)); 542 FakeLayerAnimationValueObserver dummy; 543 scoped_refptr<LayerAnimationController> controller( 544 LayerAnimationController::Create(0)); 545 controller->AddValueObserver(&dummy); 546 controller->AddAnimation(CreateAnimation( 547 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 548 1, 549 Animation::Opacity)); 550 controller->Animate(0.0); 551 controller->UpdateState(true, events.get()); 552 EXPECT_TRUE(controller->HasActiveAnimation()); 553 EXPECT_EQ(0.f, dummy.opacity()); 554 555 scoped_ptr<Animation> to_add(CreateAnimation( 556 scoped_ptr<AnimationCurve>( 557 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(), 558 2, 559 Animation::Opacity)); 560 to_add->SetRunState(Animation::WaitingForNextTick, 0); 561 controller->AddAnimation(to_add.Pass()); 562 563 // Since the animation was in the WaitingForNextTick state, it should start 564 // right in this call to animate. 565 controller->Animate(0.5); 566 controller->UpdateState(true, events.get()); 567 EXPECT_TRUE(controller->HasActiveAnimation()); 568 EXPECT_EQ(1.f, dummy.opacity()); 569 controller->Animate(1.5); 570 controller->UpdateState(true, events.get()); 571 EXPECT_EQ(0.5f, dummy.opacity()); 572 EXPECT_FALSE(controller->HasActiveAnimation()); 573 } 574 575 // Tests scheduling two animations to run together when only one property is 576 // free. 577 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) { 578 scoped_ptr<AnimationEventsVector> events( 579 make_scoped_ptr(new AnimationEventsVector)); 580 FakeLayerAnimationValueObserver dummy; 581 scoped_refptr<LayerAnimationController> controller( 582 LayerAnimationController::Create(0)); 583 controller->AddValueObserver(&dummy); 584 585 controller->AddAnimation(CreateAnimation( 586 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 587 1, 588 Animation::Transform)); 589 controller->AddAnimation(CreateAnimation( 590 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 591 2, 592 Animation::Transform)); 593 controller->AddAnimation(CreateAnimation( 594 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 595 2, 596 Animation::Opacity)); 597 598 controller->Animate(0.0); 599 controller->UpdateState(true, events.get()); 600 EXPECT_EQ(0.f, dummy.opacity()); 601 EXPECT_TRUE(controller->HasActiveAnimation()); 602 controller->Animate(1.0); 603 controller->UpdateState(true, events.get()); 604 // Should not have started the float transition yet. 605 EXPECT_TRUE(controller->HasActiveAnimation()); 606 EXPECT_EQ(0.f, dummy.opacity()); 607 // The float animation should have started at time 1 and should be done. 608 controller->Animate(2.0); 609 controller->UpdateState(true, events.get()); 610 EXPECT_EQ(1.f, dummy.opacity()); 611 EXPECT_FALSE(controller->HasActiveAnimation()); 612 } 613 614 // Tests scheduling two animations to run together with different lengths and 615 // another animation queued to start when the shorter animation finishes (should 616 // wait for both to finish). 617 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) { 618 scoped_ptr<AnimationEventsVector> events( 619 make_scoped_ptr(new AnimationEventsVector)); 620 FakeLayerAnimationValueObserver dummy; 621 scoped_refptr<LayerAnimationController> controller( 622 LayerAnimationController::Create(0)); 623 controller->AddValueObserver(&dummy); 624 625 controller->AddAnimation(CreateAnimation( 626 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(), 627 1, 628 Animation::Transform)); 629 controller->AddAnimation(CreateAnimation( 630 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 631 1, 632 Animation::Opacity)); 633 controller->AddAnimation(CreateAnimation( 634 scoped_ptr<AnimationCurve>( 635 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(), 636 2, 637 Animation::Opacity)); 638 639 // Animations with id 1 should both start now. 640 controller->Animate(0.0); 641 controller->UpdateState(true, events.get()); 642 EXPECT_TRUE(controller->HasActiveAnimation()); 643 EXPECT_EQ(0.f, dummy.opacity()); 644 // The opacity animation should have finished at time 1, but the group 645 // of animations with id 1 don't finish until time 2 because of the length 646 // of the transform animation. 647 controller->Animate(2.0); 648 controller->UpdateState(true, events.get()); 649 // Should not have started the float transition yet. 650 EXPECT_TRUE(controller->HasActiveAnimation()); 651 EXPECT_EQ(1.f, dummy.opacity()); 652 653 // The second opacity animation should start at time 2 and should be done by 654 // time 3. 655 controller->Animate(3.0); 656 controller->UpdateState(true, events.get()); 657 EXPECT_EQ(0.5f, dummy.opacity()); 658 EXPECT_FALSE(controller->HasActiveAnimation()); 659 } 660 661 // Tests scheduling an animation to start in the future. 662 TEST(LayerAnimationControllerTest, ScheduleAnimation) { 663 scoped_ptr<AnimationEventsVector> events( 664 make_scoped_ptr(new AnimationEventsVector)); 665 FakeLayerAnimationValueObserver dummy; 666 scoped_refptr<LayerAnimationController> controller( 667 LayerAnimationController::Create(0)); 668 controller->AddValueObserver(&dummy); 669 670 scoped_ptr<Animation> to_add(CreateAnimation( 671 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 672 1, 673 Animation::Opacity)); 674 to_add->SetRunState(Animation::WaitingForStartTime, 0); 675 to_add->set_start_time(1.f); 676 controller->AddAnimation(to_add.Pass()); 677 678 controller->Animate(0.0); 679 controller->UpdateState(true, events.get()); 680 EXPECT_TRUE(controller->HasActiveAnimation()); 681 EXPECT_EQ(0.f, dummy.opacity()); 682 controller->Animate(1.0); 683 controller->UpdateState(true, events.get()); 684 EXPECT_TRUE(controller->HasActiveAnimation()); 685 EXPECT_EQ(0.f, dummy.opacity()); 686 controller->Animate(2.0); 687 controller->UpdateState(true, events.get()); 688 EXPECT_EQ(1.f, dummy.opacity()); 689 EXPECT_FALSE(controller->HasActiveAnimation()); 690 } 691 692 // Tests scheduling an animation to start in the future that's interrupting a 693 // running animation. 694 TEST(LayerAnimationControllerTest, 695 ScheduledAnimationInterruptsRunningAnimation) { 696 scoped_ptr<AnimationEventsVector> events( 697 make_scoped_ptr(new AnimationEventsVector)); 698 FakeLayerAnimationValueObserver dummy; 699 scoped_refptr<LayerAnimationController> controller( 700 LayerAnimationController::Create(0)); 701 controller->AddValueObserver(&dummy); 702 703 controller->AddAnimation(CreateAnimation( 704 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(), 705 1, 706 Animation::Opacity)); 707 708 scoped_ptr<Animation> to_add(CreateAnimation( 709 scoped_ptr<AnimationCurve>( 710 new FakeFloatTransition(1.0, 0.5f, 0.f)).Pass(), 711 2, 712 Animation::Opacity)); 713 to_add->SetRunState(Animation::WaitingForStartTime, 0); 714 to_add->set_start_time(1.f); 715 controller->AddAnimation(to_add.Pass()); 716 717 // First 2s opacity transition should start immediately. 718 controller->Animate(0.0); 719 controller->UpdateState(true, events.get()); 720 EXPECT_TRUE(controller->HasActiveAnimation()); 721 EXPECT_EQ(0.f, dummy.opacity()); 722 controller->Animate(0.5); 723 controller->UpdateState(true, events.get()); 724 EXPECT_TRUE(controller->HasActiveAnimation()); 725 EXPECT_EQ(0.25f, dummy.opacity()); 726 controller->Animate(1.0); 727 controller->UpdateState(true, events.get()); 728 EXPECT_TRUE(controller->HasActiveAnimation()); 729 EXPECT_EQ(0.5f, dummy.opacity()); 730 controller->Animate(2.0); 731 controller->UpdateState(true, events.get()); 732 EXPECT_EQ(0.f, dummy.opacity()); 733 EXPECT_FALSE(controller->HasActiveAnimation()); 734 } 735 736 // Tests scheduling an animation to start in the future that interrupts a 737 // running animation and there is yet another animation queued to start later. 738 TEST(LayerAnimationControllerTest, 739 ScheduledAnimationInterruptsRunningAnimationWithAnimInQueue) { 740 scoped_ptr<AnimationEventsVector> events( 741 make_scoped_ptr(new AnimationEventsVector)); 742 FakeLayerAnimationValueObserver dummy; 743 scoped_refptr<LayerAnimationController> controller( 744 LayerAnimationController::Create(0)); 745 controller->AddValueObserver(&dummy); 746 747 controller->AddAnimation(CreateAnimation( 748 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(), 749 1, 750 Animation::Opacity)); 751 752 scoped_ptr<Animation> to_add(CreateAnimation( 753 scoped_ptr<AnimationCurve>( 754 new FakeFloatTransition(2.0, 0.5f, 0.f)).Pass(), 755 2, 756 Animation::Opacity)); 757 to_add->SetRunState(Animation::WaitingForStartTime, 0); 758 to_add->set_start_time(1.f); 759 controller->AddAnimation(to_add.Pass()); 760 761 controller->AddAnimation(CreateAnimation( 762 scoped_ptr<AnimationCurve>( 763 new FakeFloatTransition(1.0, 0.f, 0.75f)).Pass(), 764 3, 765 Animation::Opacity)); 766 767 // First 2s opacity transition should start immediately. 768 controller->Animate(0.0); 769 controller->UpdateState(true, events.get()); 770 EXPECT_TRUE(controller->HasActiveAnimation()); 771 EXPECT_EQ(0.f, dummy.opacity()); 772 controller->Animate(0.5); 773 controller->UpdateState(true, events.get()); 774 EXPECT_TRUE(controller->HasActiveAnimation()); 775 EXPECT_EQ(0.25f, dummy.opacity()); 776 EXPECT_TRUE(controller->HasActiveAnimation()); 777 controller->Animate(1.0); 778 controller->UpdateState(true, events.get()); 779 EXPECT_TRUE(controller->HasActiveAnimation()); 780 EXPECT_EQ(0.5f, dummy.opacity()); 781 controller->Animate(3.0); 782 controller->UpdateState(true, events.get()); 783 EXPECT_TRUE(controller->HasActiveAnimation()); 784 EXPECT_EQ(0.f, dummy.opacity()); 785 controller->Animate(4.0); 786 controller->UpdateState(true, events.get()); 787 EXPECT_EQ(0.75f, dummy.opacity()); 788 EXPECT_FALSE(controller->HasActiveAnimation()); 789 } 790 791 // Test that a looping animation loops and for the correct number of iterations. 792 TEST(LayerAnimationControllerTest, TrivialLooping) { 793 scoped_ptr<AnimationEventsVector> events( 794 make_scoped_ptr(new AnimationEventsVector)); 795 FakeLayerAnimationValueObserver dummy; 796 scoped_refptr<LayerAnimationController> controller( 797 LayerAnimationController::Create(0)); 798 controller->AddValueObserver(&dummy); 799 800 scoped_ptr<Animation> to_add(CreateAnimation( 801 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 802 1, 803 Animation::Opacity)); 804 to_add->set_iterations(3); 805 controller->AddAnimation(to_add.Pass()); 806 807 controller->Animate(0.0); 808 controller->UpdateState(true, events.get()); 809 EXPECT_TRUE(controller->HasActiveAnimation()); 810 EXPECT_EQ(0.f, dummy.opacity()); 811 controller->Animate(1.25); 812 controller->UpdateState(true, events.get()); 813 EXPECT_TRUE(controller->HasActiveAnimation()); 814 EXPECT_EQ(0.25f, dummy.opacity()); 815 controller->Animate(1.75); 816 controller->UpdateState(true, events.get()); 817 EXPECT_TRUE(controller->HasActiveAnimation()); 818 EXPECT_EQ(0.75f, dummy.opacity()); 819 controller->Animate(2.25); 820 controller->UpdateState(true, events.get()); 821 EXPECT_TRUE(controller->HasActiveAnimation()); 822 EXPECT_EQ(0.25f, dummy.opacity()); 823 controller->Animate(2.75); 824 controller->UpdateState(true, events.get()); 825 EXPECT_TRUE(controller->HasActiveAnimation()); 826 EXPECT_EQ(0.75f, dummy.opacity()); 827 controller->Animate(3.0); 828 controller->UpdateState(true, events.get()); 829 EXPECT_FALSE(controller->HasActiveAnimation()); 830 EXPECT_EQ(1.f, dummy.opacity()); 831 832 // Just be extra sure. 833 controller->Animate(4.0); 834 controller->UpdateState(true, events.get()); 835 EXPECT_EQ(1.f, dummy.opacity()); 836 } 837 838 // Test that an infinitely looping animation does indeed go until aborted. 839 TEST(LayerAnimationControllerTest, InfiniteLooping) { 840 scoped_ptr<AnimationEventsVector> events( 841 make_scoped_ptr(new AnimationEventsVector)); 842 FakeLayerAnimationValueObserver dummy; 843 scoped_refptr<LayerAnimationController> controller( 844 LayerAnimationController::Create(0)); 845 controller->AddValueObserver(&dummy); 846 847 const int id = 1; 848 scoped_ptr<Animation> to_add(CreateAnimation( 849 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 850 id, 851 Animation::Opacity)); 852 to_add->set_iterations(-1); 853 controller->AddAnimation(to_add.Pass()); 854 855 controller->Animate(0.0); 856 controller->UpdateState(true, events.get()); 857 EXPECT_TRUE(controller->HasActiveAnimation()); 858 EXPECT_EQ(0.f, dummy.opacity()); 859 controller->Animate(1.25); 860 controller->UpdateState(true, events.get()); 861 EXPECT_TRUE(controller->HasActiveAnimation()); 862 EXPECT_EQ(0.25f, dummy.opacity()); 863 controller->Animate(1.75); 864 controller->UpdateState(true, events.get()); 865 EXPECT_TRUE(controller->HasActiveAnimation()); 866 EXPECT_EQ(0.75f, dummy.opacity()); 867 868 controller->Animate(1073741824.25); 869 controller->UpdateState(true, events.get()); 870 EXPECT_TRUE(controller->HasActiveAnimation()); 871 EXPECT_EQ(0.25f, dummy.opacity()); 872 controller->Animate(1073741824.75); 873 controller->UpdateState(true, events.get()); 874 EXPECT_TRUE(controller->HasActiveAnimation()); 875 EXPECT_EQ(0.75f, dummy.opacity()); 876 877 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity)); 878 controller->GetAnimation(id, Animation::Opacity)->SetRunState( 879 Animation::Aborted, 0.75); 880 EXPECT_FALSE(controller->HasActiveAnimation()); 881 EXPECT_EQ(0.75f, dummy.opacity()); 882 } 883 884 // Test that pausing and resuming work as expected. 885 TEST(LayerAnimationControllerTest, PauseResume) { 886 scoped_ptr<AnimationEventsVector> events( 887 make_scoped_ptr(new AnimationEventsVector)); 888 FakeLayerAnimationValueObserver dummy; 889 scoped_refptr<LayerAnimationController> controller( 890 LayerAnimationController::Create(0)); 891 controller->AddValueObserver(&dummy); 892 893 const int id = 1; 894 controller->AddAnimation(CreateAnimation( 895 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 896 id, 897 Animation::Opacity)); 898 899 controller->Animate(0.0); 900 controller->UpdateState(true, events.get()); 901 EXPECT_TRUE(controller->HasActiveAnimation()); 902 EXPECT_EQ(0.f, dummy.opacity()); 903 controller->Animate(0.5); 904 controller->UpdateState(true, events.get()); 905 EXPECT_TRUE(controller->HasActiveAnimation()); 906 EXPECT_EQ(0.5f, dummy.opacity()); 907 908 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity)); 909 controller->GetAnimation(id, Animation::Opacity)->SetRunState( 910 Animation::Paused, 0.5); 911 912 controller->Animate(1024); 913 controller->UpdateState(true, events.get()); 914 EXPECT_TRUE(controller->HasActiveAnimation()); 915 EXPECT_EQ(0.5f, dummy.opacity()); 916 917 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity)); 918 controller->GetAnimation(id, Animation::Opacity)->SetRunState( 919 Animation::Running, 1024); 920 921 controller->Animate(1024.25); 922 controller->UpdateState(true, events.get()); 923 EXPECT_TRUE(controller->HasActiveAnimation()); 924 EXPECT_EQ(0.75f, dummy.opacity()); 925 controller->Animate(1024.5); 926 controller->UpdateState(true, events.get()); 927 EXPECT_FALSE(controller->HasActiveAnimation()); 928 EXPECT_EQ(1.f, dummy.opacity()); 929 } 930 931 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) { 932 scoped_ptr<AnimationEventsVector> events( 933 make_scoped_ptr(new AnimationEventsVector)); 934 FakeLayerAnimationValueObserver dummy; 935 scoped_refptr<LayerAnimationController> controller( 936 LayerAnimationController::Create(0)); 937 controller->AddValueObserver(&dummy); 938 939 const int id = 1; 940 controller->AddAnimation(CreateAnimation( 941 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 942 id, 943 Animation::Transform)); 944 controller->AddAnimation(CreateAnimation( 945 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(), 946 id, 947 Animation::Opacity)); 948 controller->AddAnimation(CreateAnimation( 949 scoped_ptr<AnimationCurve>( 950 new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(), 951 2, 952 Animation::Opacity)); 953 954 controller->Animate(0.0); 955 controller->UpdateState(true, events.get()); 956 EXPECT_TRUE(controller->HasActiveAnimation()); 957 EXPECT_EQ(0.f, dummy.opacity()); 958 controller->Animate(1.0); 959 controller->UpdateState(true, events.get()); 960 EXPECT_TRUE(controller->HasActiveAnimation()); 961 EXPECT_EQ(0.5f, dummy.opacity()); 962 963 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity)); 964 controller->GetAnimation(id, Animation::Opacity)->SetRunState( 965 Animation::Aborted, 1); 966 controller->Animate(1.0); 967 controller->UpdateState(true, events.get()); 968 EXPECT_TRUE(controller->HasActiveAnimation()); 969 EXPECT_EQ(1.f, dummy.opacity()); 970 controller->Animate(2.0); 971 controller->UpdateState(true, events.get()); 972 EXPECT_TRUE(!controller->HasActiveAnimation()); 973 EXPECT_EQ(0.75f, dummy.opacity()); 974 } 975 976 TEST(LayerAnimationControllerTest, ForceSyncWhenSynchronizedStartTimeNeeded) { 977 FakeLayerAnimationValueObserver dummy_impl; 978 scoped_refptr<LayerAnimationController> controller_impl( 979 LayerAnimationController::Create(0)); 980 controller_impl->AddValueObserver(&dummy_impl); 981 scoped_ptr<AnimationEventsVector> events( 982 make_scoped_ptr(new AnimationEventsVector)); 983 FakeLayerAnimationValueObserver dummy; 984 scoped_refptr<LayerAnimationController> controller( 985 LayerAnimationController::Create(0)); 986 controller->AddValueObserver(&dummy); 987 988 scoped_ptr<Animation> to_add(CreateAnimation( 989 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(), 990 0, 991 Animation::Opacity)); 992 to_add->set_needs_synchronized_start_time(true); 993 controller->AddAnimation(to_add.Pass()); 994 995 controller->Animate(0.0); 996 controller->UpdateState(true, events.get()); 997 EXPECT_TRUE(controller->HasActiveAnimation()); 998 Animation* active_animation = controller->GetAnimation(0, Animation::Opacity); 999 EXPECT_TRUE(active_animation); 1000 EXPECT_TRUE(active_animation->needs_synchronized_start_time()); 1001 1002 controller->set_force_sync(); 1003 1004 controller->PushAnimationUpdatesTo(controller_impl.get()); 1005 1006 active_animation = controller_impl->GetAnimation(0, Animation::Opacity); 1007 EXPECT_TRUE(active_animation); 1008 EXPECT_EQ(Animation::WaitingForTargetAvailability, 1009 active_animation->run_state()); 1010 } 1011 1012 // Tests that skipping a call to UpdateState works as expected. 1013 TEST(LayerAnimationControllerTest, SkipUpdateState) { 1014 scoped_ptr<AnimationEventsVector> events( 1015 make_scoped_ptr(new AnimationEventsVector)); 1016 FakeLayerAnimationValueObserver dummy; 1017 scoped_refptr<LayerAnimationController> controller( 1018 LayerAnimationController::Create(0)); 1019 controller->AddValueObserver(&dummy); 1020 1021 controller->AddAnimation(CreateAnimation( 1022 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 1023 1, 1024 Animation::Transform)); 1025 1026 controller->Animate(0.0); 1027 controller->UpdateState(true, events.get()); 1028 1029 controller->AddAnimation(CreateAnimation( 1030 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), 1031 2, 1032 Animation::Opacity)); 1033 1034 // Animate but don't UpdateState. 1035 controller->Animate(1.0); 1036 1037 controller->Animate(2.0); 1038 events.reset(new AnimationEventsVector); 1039 controller->UpdateState(true, events.get()); 1040 1041 // Should have one Started event and one Finished event. 1042 EXPECT_EQ(2u, events->size()); 1043 EXPECT_NE((*events)[0].type, (*events)[1].type); 1044 1045 // The float transition should still be at its starting point. 1046 EXPECT_TRUE(controller->HasActiveAnimation()); 1047 EXPECT_EQ(0.f, dummy.opacity()); 1048 1049 controller->Animate(3.0); 1050 controller->UpdateState(true, events.get()); 1051 1052 // The float tranisition should now be done. 1053 EXPECT_EQ(1.f, dummy.opacity()); 1054 EXPECT_FALSE(controller->HasActiveAnimation()); 1055 } 1056 1057 // Tests that an animation controller with only an inactive observer gets ticked 1058 // but doesn't progress animations past the Starting state. 1059 TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) { 1060 scoped_ptr<AnimationEventsVector> events( 1061 make_scoped_ptr(new AnimationEventsVector)); 1062 FakeLayerAnimationValueObserver dummy; 1063 FakeInactiveLayerAnimationValueObserver inactive_dummy; 1064 scoped_refptr<LayerAnimationController> controller( 1065 LayerAnimationController::Create(0)); 1066 1067 const int id = 1; 1068 controller->AddAnimation(CreateAnimation(scoped_ptr<AnimationCurve>( 1069 new FakeFloatTransition(1.0, 0.5f, 1.f)).Pass(), 1070 id, 1071 Animation::Opacity)); 1072 1073 // Without an observer, the animation shouldn't progress to the Starting 1074 // state. 1075 controller->Animate(0.0); 1076 controller->UpdateState(true, events.get()); 1077 EXPECT_EQ(0u, events->size()); 1078 EXPECT_EQ(Animation::WaitingForTargetAvailability, 1079 controller->GetAnimation(id, Animation::Opacity)->run_state()); 1080 1081 controller->AddValueObserver(&inactive_dummy); 1082 1083 // With only an inactive observer, the animation should progress to the 1084 // Starting state and get ticked at its starting point, but should not 1085 // progress to Running. 1086 controller->Animate(1.0); 1087 controller->UpdateState(true, events.get()); 1088 EXPECT_EQ(0u, events->size()); 1089 EXPECT_EQ(Animation::Starting, 1090 controller->GetAnimation(id, Animation::Opacity)->run_state()); 1091 EXPECT_EQ(0.5f, inactive_dummy.opacity()); 1092 1093 // Even when already in the Starting state, the animation should stay 1094 // there, and shouldn't be ticked past its starting point. 1095 controller->Animate(2.0); 1096 controller->UpdateState(true, events.get()); 1097 EXPECT_EQ(0u, events->size()); 1098 EXPECT_EQ(Animation::Starting, 1099 controller->GetAnimation(id, Animation::Opacity)->run_state()); 1100 EXPECT_EQ(0.5f, inactive_dummy.opacity()); 1101 1102 controller->AddValueObserver(&dummy); 1103 1104 // Now that an active observer has been added, the animation should still 1105 // initially tick at its starting point, but should now progress to Running. 1106 controller->Animate(3.0); 1107 controller->UpdateState(true, events.get()); 1108 EXPECT_EQ(1u, events->size()); 1109 EXPECT_EQ(Animation::Running, 1110 controller->GetAnimation(id, Animation::Opacity)->run_state()); 1111 EXPECT_EQ(0.5f, inactive_dummy.opacity()); 1112 EXPECT_EQ(0.5f, dummy.opacity()); 1113 1114 // The animation should now tick past its starting point. 1115 controller->Animate(3.5); 1116 EXPECT_NE(0.5f, inactive_dummy.opacity()); 1117 EXPECT_NE(0.5f, dummy.opacity()); 1118 } 1119 1120 } // namespace 1121 } // namespace cc 1122