| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.fx.animTest'); |
| 16 |
goog.setTestOnly('goog.fx.animTest'); |
| 17 |
|
| 18 |
goog.require('goog.async.AnimationDelay'); |
| 19 |
goog.require('goog.async.Delay'); |
| 20 |
goog.require('goog.events'); |
| 21 |
goog.require('goog.functions'); |
| 22 |
goog.require('goog.fx.Animation'); |
| 23 |
goog.require('goog.fx.anim'); |
| 24 |
goog.require('goog.object'); |
| 25 |
goog.require('goog.testing.MockClock'); |
| 26 |
goog.require('goog.testing.PropertyReplacer'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
goog.require('goog.testing.recordFunction'); |
| 29 |
goog.require('goog.userAgent'); |
| 30 |
|
| 31 |
var clock, replacer; |
| 32 |
|
| 33 |
function setUpPage() { |
| 34 |
clock = new goog.testing.MockClock(true); |
| 35 |
} |
| 36 |
|
| 37 |
function tearDownPage() { |
| 38 |
clock.dispose(); |
| 39 |
} |
| 40 |
|
| 41 |
function setUp() { |
| 42 |
replacer = new goog.testing.PropertyReplacer(); |
| 43 |
} |
| 44 |
|
| 45 |
function tearDown() { |
| 46 |
replacer.reset(); |
| 47 |
goog.fx.anim.tearDown(); |
| 48 |
} |
| 49 |
|
| 50 |
function testDelayWithMocks() { |
| 51 |
goog.fx.anim.setAnimationWindow(null); |
| 52 |
registerAndUnregisterAnimationWithMocks(goog.async.Delay); |
| 53 |
} |
| 54 |
|
| 55 |
function testAnimationDelayWithMocks() { |
| 56 |
goog.fx.anim.setAnimationWindow(window); |
| 57 |
registerAndUnregisterAnimationWithMocks(goog.async.AnimationDelay); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* @param {!Function} delayType The constructor for Delay or AnimationDelay. |
| 63 |
* The methods will be mocked out. |
| 64 |
*/ |
| 65 |
function registerAndUnregisterAnimationWithMocks(delayType) { |
| 66 |
var timerCount = 0; |
| 67 |
|
| 68 |
replacer.set(delayType.prototype, 'start', function() { timerCount++; }); |
| 69 |
replacer.set(delayType.prototype, 'stop', function() { timerCount--; }); |
| 70 |
replacer.set( |
| 71 |
delayType.prototype, 'isActive', function() { return timerCount > 0; }); |
| 72 |
|
| 73 |
var forbiddenDelayType = delayType == goog.async.AnimationDelay ? |
| 74 |
goog.async.Delay : |
| 75 |
goog.async.AnimationDelay; |
| 76 |
replacer.set(forbiddenDelayType.prototype, 'start', goog.functions.error()); |
| 77 |
replacer.set(forbiddenDelayType.prototype, 'stop', goog.functions.error()); |
| 78 |
replacer.set( |
| 79 |
forbiddenDelayType.prototype, 'isActive', goog.functions.error()); |
| 80 |
|
| 81 |
var anim = new goog.fx.Animation([0], [1], 1000); |
| 82 |
var anim2 = new goog.fx.Animation([0], [1], 1000); |
| 83 |
|
| 84 |
goog.fx.anim.registerAnimation(anim); |
| 85 |
|
| 86 |
assertTrue( |
| 87 |
'Should contain the animation', |
| 88 |
goog.object.containsValue(goog.fx.anim.activeAnimations_, anim)); |
| 89 |
assertEquals('Should have called start once', 1, timerCount); |
| 90 |
|
| 91 |
goog.fx.anim.registerAnimation(anim2); |
| 92 |
|
| 93 |
assertEquals('Should not have called start again', 1, timerCount); |
| 94 |
|
| 95 |
// Add anim again. |
| 96 |
goog.fx.anim.registerAnimation(anim); |
| 97 |
assertTrue( |
| 98 |
'Should contain the animation', |
| 99 |
goog.object.containsValue(goog.fx.anim.activeAnimations_, anim)); |
| 100 |
assertEquals('Should not have called start again', 1, timerCount); |
| 101 |
|
| 102 |
goog.fx.anim.unregisterAnimation(anim); |
| 103 |
assertFalse( |
| 104 |
'Should not contain the animation', |
| 105 |
goog.object.containsValue(goog.fx.anim.activeAnimations_, anim)); |
| 106 |
assertEquals('clearTimeout should not have been called', 1, timerCount); |
| 107 |
|
| 108 |
goog.fx.anim.unregisterAnimation(anim2); |
| 109 |
assertEquals('There should be no remaining timers', 0, timerCount); |
| 110 |
|
| 111 |
// Make sure we don't trigger setTimeout or setInterval. |
| 112 |
clock.tick(1000); |
| 113 |
goog.fx.anim.cycleAnimations_(goog.now()); |
| 114 |
|
| 115 |
assertEquals('There should be no remaining timers', 0, timerCount); |
| 116 |
|
| 117 |
anim.dispose(); |
| 118 |
anim2.dispose(); |
| 119 |
} |
| 120 |
|
| 121 |
function testRegisterAndUnregisterAnimationWithRequestAnimationFrameGecko() { |
| 122 |
// Only FF4 onwards support requestAnimationFrame. |
| 123 |
if (!goog.userAgent.GECKO || !goog.userAgent.isVersionOrHigher('2.0') || |
| 124 |
goog.userAgent.isVersionOrHigher('17')) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
goog.fx.anim.setAnimationWindow(window); |
| 129 |
|
| 130 |
var anim = new goog.fx.Animation([0], [1], 1000); |
| 131 |
var anim2 = new goog.fx.Animation([0], [1], 1000); |
| 132 |
|
| 133 |
goog.fx.anim.registerAnimation(anim); |
| 134 |
|
| 135 |
assertTrue( |
| 136 |
'Should contain the animation', |
| 137 |
goog.object.containsValue(goog.fx.anim.activeAnimations_, anim)); |
| 138 |
|
| 139 |
assertEquals( |
| 140 |
'Should have listen to MozBeforePaint once', 1, |
| 141 |
goog.events.getListeners(window, 'MozBeforePaint', false).length); |
| 142 |
|
| 143 |
goog.fx.anim.registerAnimation(anim2); |
| 144 |
|
| 145 |
assertEquals( |
| 146 |
'Should not add more listener for MozBeforePaint', 1, |
| 147 |
goog.events.getListeners(window, 'MozBeforePaint', false).length); |
| 148 |
|
| 149 |
// Add anim again. |
| 150 |
goog.fx.anim.registerAnimation(anim); |
| 151 |
assertTrue( |
| 152 |
'Should contain the animation', |
| 153 |
goog.object.containsValue(goog.fx.anim.activeAnimations_, anim)); |
| 154 |
assertEquals( |
| 155 |
'Should not add more listener for MozBeforePaint', 1, |
| 156 |
goog.events.getListeners(window, 'MozBeforePaint', false).length); |
| 157 |
|
| 158 |
goog.fx.anim.unregisterAnimation(anim); |
| 159 |
assertFalse( |
| 160 |
'Should not contain the animation', |
| 161 |
goog.object.containsValue(goog.fx.anim.activeAnimations_, anim)); |
| 162 |
assertEquals( |
| 163 |
'Should not clear listener for MozBeforePaint yet', 1, |
| 164 |
goog.events.getListeners(window, 'MozBeforePaint', false).length); |
| 165 |
|
| 166 |
goog.fx.anim.unregisterAnimation(anim2); |
| 167 |
assertEquals( |
| 168 |
'There should be no more listener for MozBeforePaint', 0, |
| 169 |
goog.events.getListeners(window, 'MozBeforePaint', false).length); |
| 170 |
|
| 171 |
anim.dispose(); |
| 172 |
anim2.dispose(); |
| 173 |
|
| 174 |
goog.fx.anim.setAnimationWindow(null); |
| 175 |
} |
| 176 |
|
| 177 |
function testRegisterUnregisterAnimation() { |
| 178 |
var anim = new goog.fx.Animation([0], [1], 1000); |
| 179 |
|
| 180 |
goog.fx.anim.registerAnimation(anim); |
| 181 |
|
| 182 |
assertTrue( |
| 183 |
'There should be an active timer', |
| 184 |
goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive()); |
| 185 |
assertEquals( |
| 186 |
'There should be an active animations', 1, |
| 187 |
goog.object.getCount(goog.fx.anim.activeAnimations_)); |
| 188 |
|
| 189 |
goog.fx.anim.unregisterAnimation(anim); |
| 190 |
|
| 191 |
assertTrue( |
| 192 |
'There should be no active animations', |
| 193 |
goog.object.isEmpty(goog.fx.anim.activeAnimations_)); |
| 194 |
assertFalse( |
| 195 |
'There should be no active timer', |
| 196 |
goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive()); |
| 197 |
|
| 198 |
anim.dispose(); |
| 199 |
} |
| 200 |
|
| 201 |
function testCycleWithMockClock() { |
| 202 |
goog.fx.anim.setAnimationWindow(null); |
| 203 |
var anim = new goog.fx.Animation([0], [1], 1000); |
| 204 |
anim.onAnimationFrame = goog.testing.recordFunction(); |
| 205 |
|
| 206 |
goog.fx.anim.registerAnimation(anim); |
| 207 |
clock.tick(goog.fx.anim.TIMEOUT); |
| 208 |
|
| 209 |
assertEquals(1, anim.onAnimationFrame.getCallCount()); |
| 210 |
} |
| 211 |
|
| 212 |
function testCycleWithMockClockAndAnimationWindow() { |
| 213 |
goog.fx.anim.setAnimationWindow(window); |
| 214 |
var anim = new goog.fx.Animation([0], [1], 1000); |
| 215 |
anim.onAnimationFrame = goog.testing.recordFunction(); |
| 216 |
|
| 217 |
goog.fx.anim.registerAnimation(anim); |
| 218 |
clock.tick(goog.fx.anim.TIMEOUT); |
| 219 |
|
| 220 |
assertEquals(1, anim.onAnimationFrame.getCallCount()); |
| 221 |
} |