Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / ui / animation_controller_test.html
1 <!DOCTYPE html>
2 <!--
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.
6 -->
7 <link rel="import" href="/tvcm/utils.html">
8 <link rel="import" href="/tvcm/ui/animation_controller.html">
9 <script>
10 'use strict';
11
12 tvcm.unittest.testSuite(function() {
13   function SimpleAnimation(options) {
14     this.stopTime = options.stopTime;
15
16     this.startCalled = false;
17     this.didStopEarlyCalled = false;
18     this.wasTakenOver = false;
19     this.tickCount = 0;
20   }
21
22   SimpleAnimation.prototype = {
23     __proto__: tvcm.ui.Animation.prototype,
24
25     canTakeOverFor: function(existingAnimation) {
26       return false;
27     },
28
29     takeOverFor: function(existingAnimation, newStartTimestamp, target) {
30       throw new Error('Not implemented');
31     },
32
33     start: function(timestamp, target) {
34       this.startCalled = true;
35     },
36
37     didStopEarly: function(timestamp, target, willBeTakenOver) {
38       this.didStopEarlyCalled = true;
39       this.wasTakenOver = willBeTakenOver;
40     },
41
42     /**
43      * @return {boolean} true if the animation is finished.
44      */
45     tick: function(timestamp, target) {
46       this.tickCount++;
47       return timestamp >= this.stopTime;
48     }
49   };
50
51   test('cancel', function() {
52     var target = {
53       x: 0,
54       cloneAnimationState: function() { return {x: this.x}; }
55     };
56
57     var controller = new tvcm.ui.AnimationController();
58     controller.target = target;
59
60     var animation = new SimpleAnimation({stopTime: 100});
61     controller.queueAnimation(animation);
62
63     tvcm.forcePendingRAFTasksToRun(0);
64     assertEquals(1, animation.tickCount);
65     controller.cancelActiveAnimation();
66     assertFalse(controller.hasActiveAnimation);
67     assertTrue(animation.didStopEarlyCalled);
68   });
69
70   test('simple', function() {
71     var target = {
72       x: 0,
73       cloneAnimationState: function() { return {x: this.x}; }
74     };
75
76     var controller = new tvcm.ui.AnimationController();
77     controller.target = target;
78
79     var animation = new SimpleAnimation({stopTime: 100});
80     controller.queueAnimation(animation);
81
82     tvcm.forcePendingRAFTasksToRun(0);
83     assertEquals(1, animation.tickCount);
84     assertTrue(controller.hasActiveAnimation);
85
86     tvcm.forcePendingRAFTasksToRun(100);
87     assertEquals(2, animation.tickCount);
88     assertFalse(controller.hasActiveAnimation);
89   });
90
91   test('queueTwo', function() {
92     // Clear all pending rafs so if something is lingering it will blow up here.
93     tvcm.forcePendingRAFTasksToRun(0);
94
95     var target = {
96       x: 0,
97       cloneAnimationState: function() { return {x: this.x}; }
98     };
99
100     var controller = new tvcm.ui.AnimationController();
101     controller.target = target;
102
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);
110
111     tvcm.forcePendingRAFTasksToRun(150);
112     assertFalse(controller.hasActiveAnimation);
113     assertTrue(a2.tickCount > 0);
114   });
115
116   /**
117    * @constructor
118    */
119   function AnimationThatCanTakeOverForSimpleAnimation() {
120     this.takeOverForAnimation = undefined;
121   }
122
123   AnimationThatCanTakeOverForSimpleAnimation.prototype = {
124     __proto__: tvcm.ui.Animation.prototype,
125
126
127     canTakeOverFor: function(existingAnimation) {
128       return existingAnimation instanceof SimpleAnimation;
129     },
130
131     takeOverFor: function(existingAnimation, newStartTimestamp, target) {
132       this.takeOverForAnimation = existingAnimation;
133     },
134
135     start: function(timestamp, target) {
136       this.startCalled = true;
137     }
138   };
139
140   test('takeOver', function() {
141     var target = {
142       x: 0,
143       cloneAnimationState: function() { return {x: this.x}; }
144     };
145
146     var controller = new tvcm.ui.AnimationController();
147     controller.target = target;
148
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);
158
159     assertEquals(a2.takeOverForAnimation, a1);
160     assertTrue(a2.startCalled);
161
162     controller.cancelActiveAnimation();
163     assertFalse(controller.hasActiveAnimation);
164   });
165 });
166 </script>