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