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