aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/fx/anim/anim_test.html
blob: aa13fd4cf8944c2b280d2a5a84e2ce2397ad9e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
  Author: arv@google.com (Erik Arvidsson)
  Author: steineldar@google.com (Stein Eldar Johnsen)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.fx</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.Timer');
  goog.require('goog.fx.Animation');
  goog.require('goog.fx.anim');
  goog.require('goog.object');
  goog.require('goog.testing.MockClock');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.recordFunction');
  goog.require('goog.userAgent');
</script>
<style>

</style>
</head>
<body>
  <script>

var clock, replacer;

function setUpPage() {
  clock = new goog.testing.MockClock(true);
}

function tearDownPage() {
  clock.dispose();
}

function setUp() {
  replacer = new goog.testing.PropertyReplacer();
}

function tearDown() {
  replacer.reset();
  goog.fx.anim.tearDown();
}

function testDelayWithMocks() {
  goog.fx.anim.setAnimationWindow(null);
  registerAndUnregisterAnimationWithMocks(goog.async.Delay);
}

function testAnimationDelayWithMocks() {
  goog.fx.anim.setAnimationWindow(window);
  registerAndUnregisterAnimationWithMocks(goog.async.AnimationDelay);
}

/**
 * @param {!Function} delayType The constructor for Delay or AnimationDelay.
 *     The methods will be mocked out.
 */
function registerAndUnregisterAnimationWithMocks(delayType) {
  var timerCount = 0;

  replacer.set(delayType.prototype, 'start', function() {
    timerCount++;
  });
  replacer.set(delayType.prototype, 'stop', function() {
    timerCount--;
  });
  replacer.set(delayType.prototype, 'isActive', function() {
    return timerCount > 0;
  });

  var forbiddenDelayType = delayType == goog.async.AnimationDelay ?
      goog.async.Delay : goog.async.AnimationDelay;
  replacer.set(forbiddenDelayType.prototype,
      'start', goog.functions.error());
  replacer.set(forbiddenDelayType.prototype,
      'stop', goog.functions.error());
  replacer.set(forbiddenDelayType.prototype,
      'isActive', goog.functions.error());

  var anim = new goog.fx.Animation([0], [1], 1000);
  var anim2 = new goog.fx.Animation([0], [1], 1000);

  goog.fx.anim.registerAnimation(anim);

  assertTrue('Should contain the animation',
             goog.object.containsValue(goog.fx.anim.activeAnimations_,
                                       anim));
  assertEquals('Should have called start once', 1, timerCount);

  goog.fx.anim.registerAnimation(anim2);

  assertEquals('Should not have called start again', 1, timerCount);

  // Add anim again.
  goog.fx.anim.registerAnimation(anim);
  assertTrue('Should contain the animation',
             goog.object.containsValue(goog.fx.anim.activeAnimations_,
                                       anim));
  assertEquals('Should not have called start again', 1, timerCount);

  goog.fx.anim.unregisterAnimation(anim);
  assertFalse('Should not contain the animation',
              goog.object.containsValue(goog.fx.anim.activeAnimations_,
                                        anim));
  assertEquals('clearTimeout should not have been called', 1, timerCount);

  goog.fx.anim.unregisterAnimation(anim2);
  assertEquals('There should be no remaining timers', 0, timerCount);

  // Make sure we don't trigger setTimeout or setInterval.
  clock.tick(1000);
  goog.fx.anim.cycleAnimations_(goog.now());

  assertEquals('There should be no remaining timers', 0, timerCount);

  anim.dispose();
  anim2.dispose();
}

function testRegisterAndUnregisterAnimationWithRequestAnimationFrameGecko() {
  // Only FF4 onwards support requestAnimationFrame.
  if (!goog.userAgent.GECKO || !goog.userAgent.isVersion('2.0')) {
    return;
  }

  goog.fx.anim.setAnimationWindow(window);

  var anim = new goog.fx.Animation([0], [1], 1000);
  var anim2 = new goog.fx.Animation([0], [1], 1000);

  goog.fx.anim.registerAnimation(anim);

  assertTrue('Should contain the animation',
             goog.object.containsValue(goog.fx.anim.activeAnimations_,
                                       anim));

  assertEquals('Should have listen to MozBeforePaint once', 1,
      goog.events.getListeners(window, 'MozBeforePaint', false).length);

  goog.fx.anim.registerAnimation(anim2);

  assertEquals('Should not add more listener for MozBeforePaint', 1,
      goog.events.getListeners(window, 'MozBeforePaint', false).length);

  // Add anim again.
  goog.fx.anim.registerAnimation(anim);
  assertTrue('Should contain the animation',
             goog.object.containsValue(goog.fx.anim.activeAnimations_,
                                       anim));
  assertEquals('Should not add more listener for MozBeforePaint', 1,
      goog.events.getListeners(window, 'MozBeforePaint', false).length);

  goog.fx.anim.unregisterAnimation(anim);
  assertFalse('Should not contain the animation',
              goog.object.containsValue(goog.fx.anim.activeAnimations_,
                                        anim));
  assertEquals('Should not clear listener for MozBeforePaint yet', 1,
      goog.events.getListeners(window, 'MozBeforePaint', false).length);

  goog.fx.anim.unregisterAnimation(anim2);
  assertEquals('There should be no more listener for MozBeforePaint', 0,
      goog.events.getListeners(window, 'MozBeforePaint', false).length);

  anim.dispose();
  anim2.dispose();

  goog.fx.anim.setAnimationWindow(null);
}

function testRegisterUnregisterAnimation() {
  var anim = new goog.fx.Animation([0], [1], 1000);

  goog.fx.anim.registerAnimation(anim);

  assertTrue('There should be an active timer',
      goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive());
  assertEquals('There should be an active animations',
      1, goog.object.getCount(goog.fx.anim.activeAnimations_));

  goog.fx.anim.unregisterAnimation(anim);

  assertTrue('There should be no active animations',
      goog.object.isEmpty(goog.fx.anim.activeAnimations_));
  assertFalse('There should be no active timer',
      goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive());

  anim.dispose();
}

function testCycleWithMockClock() {
  goog.fx.anim.setAnimationWindow(null);
  var anim = new goog.fx.Animation([0], [1], 1000);
  anim.onAnimationFrame = goog.testing.recordFunction();

  goog.fx.anim.registerAnimation(anim);
  clock.tick(goog.fx.anim.TIMEOUT);

  assertEquals(1, anim.onAnimationFrame.getCallCount());
}

function testCycleWithMockClockAndAnimationWindow() {
  goog.fx.anim.setAnimationWindow(window);
  var anim = new goog.fx.Animation([0], [1], 1000);
  anim.onAnimationFrame = goog.testing.recordFunction();

  goog.fx.anim.registerAnimation(anim);
  clock.tick(goog.fx.anim.TIMEOUT);

  assertEquals(1, anim.onAnimationFrame.getCallCount());
}

</script>
</body>
</html>