3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/tvcm/utils.html">
8 <link rel="import" href="/tvcm/ui/animation_controller.html">
12 tvcm.unittest.testSuite(function() {
13 function SimpleAnimation(options) {
14 this.stopTime = options.stopTime;
16 this.startCalled = false;
17 this.didStopEarlyCalled = false;
18 this.wasTakenOver = false;
22 SimpleAnimation.prototype = {
23 __proto__: tvcm.ui.Animation.prototype,
25 canTakeOverFor: function(existingAnimation) {
29 takeOverFor: function(existingAnimation, newStartTimestamp, target) {
30 throw new Error('Not implemented');
33 start: function(timestamp, target) {
34 this.startCalled = true;
37 didStopEarly: function(timestamp, target, willBeTakenOver) {
38 this.didStopEarlyCalled = true;
39 this.wasTakenOver = willBeTakenOver;
43 * @return {boolean} true if the animation is finished.
45 tick: function(timestamp, target) {
47 return timestamp >= this.stopTime;
51 test('cancel', function() {
54 cloneAnimationState: function() { return {x: this.x}; }
57 var controller = new tvcm.ui.AnimationController();
58 controller.target = target;
60 var animation = new SimpleAnimation({stopTime: 100});
61 controller.queueAnimation(animation);
63 tvcm.forcePendingRAFTasksToRun(0);
64 assertEquals(1, animation.tickCount);
65 controller.cancelActiveAnimation();
66 assertFalse(controller.hasActiveAnimation);
67 assertTrue(animation.didStopEarlyCalled);
70 test('simple', function() {
73 cloneAnimationState: function() { return {x: this.x}; }
76 var controller = new tvcm.ui.AnimationController();
77 controller.target = target;
79 var animation = new SimpleAnimation({stopTime: 100});
80 controller.queueAnimation(animation);
82 tvcm.forcePendingRAFTasksToRun(0);
83 assertEquals(1, animation.tickCount);
84 assertTrue(controller.hasActiveAnimation);
86 tvcm.forcePendingRAFTasksToRun(100);
87 assertEquals(2, animation.tickCount);
88 assertFalse(controller.hasActiveAnimation);
91 test('queueTwo', function() {
92 // Clear all pending rafs so if something is lingering it will blow up here.
93 tvcm.forcePendingRAFTasksToRun(0);
97 cloneAnimationState: function() { return {x: this.x}; }
100 var controller = new tvcm.ui.AnimationController();
101 controller.target = target;
103 var a1 = new SimpleAnimation({stopTime: 100});
104 var a2 = new SimpleAnimation({stopTime: 100});
105 controller.queueAnimation(a1, 0);
106 assertTrue(a1.startCalled);
107 controller.queueAnimation(a2, 50);
108 assertTrue(a1.didStopEarlyCalled);
109 assertTrue(a2.startCalled);
111 tvcm.forcePendingRAFTasksToRun(150);
112 assertFalse(controller.hasActiveAnimation);
113 assertTrue(a2.tickCount > 0);
119 function AnimationThatCanTakeOverForSimpleAnimation() {
120 this.takeOverForAnimation = undefined;
123 AnimationThatCanTakeOverForSimpleAnimation.prototype = {
124 __proto__: tvcm.ui.Animation.prototype,
127 canTakeOverFor: function(existingAnimation) {
128 return existingAnimation instanceof SimpleAnimation;
131 takeOverFor: function(existingAnimation, newStartTimestamp, target) {
132 this.takeOverForAnimation = existingAnimation;
135 start: function(timestamp, target) {
136 this.startCalled = true;
140 test('takeOver', function() {
143 cloneAnimationState: function() { return {x: this.x}; }
146 var controller = new tvcm.ui.AnimationController();
147 controller.target = target;
149 var a1 = new SimpleAnimation({stopTime: 100});
150 var a2 = new AnimationThatCanTakeOverForSimpleAnimation();
151 controller.queueAnimation(a1, 0);
152 assertTrue(a1.startCalled);
153 assertEquals(0, a1.tickCount);
154 controller.queueAnimation(a2, 10);
155 assertTrue(a1.didStopEarlyCalled);
156 assertTrue(a1.wasTakenOver);
157 assertEquals(1, a1.tickCount);
159 assertEquals(a2.takeOverForAnimation, a1);
160 assertTrue(a2.startCalled);
162 controller.cancelActiveAnimation();
163 assertFalse(controller.hasActiveAnimation);