1 // Copyright (c) 2013 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.
7 base.require('base.utils');
8 base.require('ui.animation_controller');
10 base.unittest.testSuite('ui.animation_controller', function() {
11 function SimpleAnimation(options) {
12 this.stopTime = options.stopTime;
14 this.startCalled = false;
15 this.didStopEarlyCalled = false;
16 this.wasTakenOver = false;
20 SimpleAnimation.prototype = {
21 __proto__: ui.Animation.prototype,
23 canTakeOverFor: function(existingAnimation) {
27 takeOverFor: function(existingAnimation, newStartTimestamp, target) {
28 throw new Error('Not implemented');
31 start: function(timestamp, target) {
32 this.startCalled = true;
35 didStopEarly: function(timestamp, target, willBeTakenOver) {
36 this.didStopEarlyCalled = true;
37 this.wasTakenOver = willBeTakenOver;
41 * @return {boolean} true if the animation is finished.
43 tick: function(timestamp, target) {
45 return timestamp >= this.stopTime;
49 test('cancel', function() {
52 cloneAnimationState: function() { return {x: this.x}; }
55 var controller = new ui.AnimationController();
56 controller.target = target;
58 var animation = new SimpleAnimation({stopTime: 100});
59 controller.queueAnimation(animation);
61 base.forcePendingRAFTasksToRun(0);
62 assertEquals(1, animation.tickCount);
63 controller.cancelActiveAnimation();
64 assertFalse(controller.hasActiveAnimation);
65 assertTrue(animation.didStopEarlyCalled);
68 test('simple', function() {
71 cloneAnimationState: function() { return {x: this.x}; }
74 var controller = new ui.AnimationController();
75 controller.target = target;
77 var animation = new SimpleAnimation({stopTime: 100});
78 controller.queueAnimation(animation);
80 base.forcePendingRAFTasksToRun(0);
81 assertEquals(1, animation.tickCount);
82 assertTrue(controller.hasActiveAnimation);
84 base.forcePendingRAFTasksToRun(100);
85 assertEquals(2, animation.tickCount);
86 assertFalse(controller.hasActiveAnimation);
89 test('queueTwo', function() {
90 // Clear all pending rafs so if something is lingering it will blow up here.
91 base.forcePendingRAFTasksToRun(0);
95 cloneAnimationState: function() { return {x: this.x}; }
98 var controller = new ui.AnimationController();
99 controller.target = target;
101 var a1 = new SimpleAnimation({stopTime: 100});
102 var a2 = new SimpleAnimation({stopTime: 100});
103 controller.queueAnimation(a1, 0);
104 assertTrue(a1.startCalled);
105 controller.queueAnimation(a2, 50);
106 assertTrue(a1.didStopEarlyCalled);
107 assertTrue(a2.startCalled);
109 base.forcePendingRAFTasksToRun(150);
110 assertFalse(controller.hasActiveAnimation);
111 assertTrue(a2.tickCount > 0);
117 function AnimationThatCanTakeOverForSimpleAnimation() {
118 this.takeOverForAnimation = undefined;
121 AnimationThatCanTakeOverForSimpleAnimation.prototype = {
122 __proto__: ui.Animation.prototype,
125 canTakeOverFor: function(existingAnimation) {
126 return existingAnimation instanceof SimpleAnimation;
129 takeOverFor: function(existingAnimation, newStartTimestamp, target) {
130 this.takeOverForAnimation = existingAnimation;
133 start: function(timestamp, target) {
134 this.startCalled = true;
138 test('takeOver', function() {
141 cloneAnimationState: function() { return {x: this.x}; }
144 var controller = new ui.AnimationController();
145 controller.target = target;
147 var a1 = new SimpleAnimation({stopTime: 100});
148 var a2 = new AnimationThatCanTakeOverForSimpleAnimation();
149 controller.queueAnimation(a1, 0);
150 assertTrue(a1.startCalled);
151 assertEquals(0, a1.tickCount);
152 controller.queueAnimation(a2, 10);
153 assertTrue(a1.didStopEarlyCalled);
154 assertTrue(a1.wasTakenOver);
155 assertEquals(1, a1.tickCount);
157 assertEquals(a2.takeOverForAnimation, a1);
158 assertTrue(a2.startCalled);
160 controller.cancelActiveAnimation();
161 assertFalse(controller.hasActiveAnimation);