| 1 |
// Copyright 2011 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.AnimationTest'); |
| 16 |
goog.setTestOnly('goog.fx.AnimationTest'); |
| 17 |
|
| 18 |
goog.require('goog.events'); |
| 19 |
goog.require('goog.fx.Animation'); |
| 20 |
goog.require('goog.testing.MockClock'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
var clock; |
| 24 |
|
| 25 |
function setUpPage() { |
| 26 |
clock = new goog.testing.MockClock(true); |
| 27 |
} |
| 28 |
|
| 29 |
function tearDownPage() { |
| 30 |
clock.dispose(); |
| 31 |
} |
| 32 |
|
| 33 |
function testPauseLogic() { |
| 34 |
var anim = new goog.fx.Animation([], [], 3000); |
| 35 |
var nFrames = 0; |
| 36 |
goog.events.listen(anim, goog.fx.Animation.EventType.ANIMATE, function(e) { |
| 37 |
assertRoughlyEquals(e.progress, progress, 1e-6); |
| 38 |
nFrames++; |
| 39 |
}); |
| 40 |
goog.events.listen( |
| 41 |
anim, goog.fx.Animation.EventType.END, function(e) { nFrames++; }); |
| 42 |
var nSteps = 10; |
| 43 |
for (var i = 0; i < nSteps; i++) { |
| 44 |
progress = i / (nSteps - 1); |
| 45 |
anim.setProgress(progress); |
| 46 |
anim.play(); |
| 47 |
anim.pause(); |
| 48 |
} |
| 49 |
assertEquals(nSteps, nFrames); |
| 50 |
} |
| 51 |
|
| 52 |
function testPauseOffset() { |
| 53 |
var anim = new goog.fx.Animation([0], [1000], 1000); |
| 54 |
anim.play(); |
| 55 |
|
| 56 |
assertEquals(0, anim.coords[0]); |
| 57 |
assertRoughlyEquals(0, anim.progress, 1e-4); |
| 58 |
|
| 59 |
clock.tick(300); |
| 60 |
|
| 61 |
assertEquals(300, anim.coords[0]); |
| 62 |
assertRoughlyEquals(0.3, anim.progress, 1e-4); |
| 63 |
|
| 64 |
anim.pause(); |
| 65 |
|
| 66 |
clock.tick(400); |
| 67 |
|
| 68 |
assertEquals(300, anim.coords[0]); |
| 69 |
assertRoughlyEquals(0.3, anim.progress, 1e-4); |
| 70 |
|
| 71 |
anim.play(); |
| 72 |
|
| 73 |
assertEquals(300, anim.coords[0]); |
| 74 |
assertRoughlyEquals(0.3, anim.progress, 1e-4); |
| 75 |
|
| 76 |
clock.tick(400); |
| 77 |
|
| 78 |
assertEquals(700, anim.coords[0]); |
| 79 |
assertRoughlyEquals(0.7, anim.progress, 1e-4); |
| 80 |
|
| 81 |
anim.pause(); |
| 82 |
|
| 83 |
clock.tick(300); |
| 84 |
|
| 85 |
assertEquals(700, anim.coords[0]); |
| 86 |
assertRoughlyEquals(0.7, anim.progress, 1e-4); |
| 87 |
|
| 88 |
anim.play(); |
| 89 |
|
| 90 |
var lastPlay = goog.now(); |
| 91 |
|
| 92 |
assertEquals(700, anim.coords[0]); |
| 93 |
assertRoughlyEquals(0.7, anim.progress, 1e-4); |
| 94 |
|
| 95 |
clock.tick(300); |
| 96 |
|
| 97 |
assertEquals(1000, anim.coords[0]); |
| 98 |
assertRoughlyEquals(1, anim.progress, 1e-4); |
| 99 |
assertEquals(goog.fx.Animation.State.STOPPED, anim.getStateInternal()); |
| 100 |
} |
| 101 |
|
| 102 |
function testClockReset() { |
| 103 |
var anim = new goog.fx.Animation([0], [1000], 1000); |
| 104 |
anim.play(); |
| 105 |
|
| 106 |
assertEquals(0, anim.coords[0]); |
| 107 |
assertRoughlyEquals(0, anim.progress, 1e-4); |
| 108 |
|
| 109 |
// Possible when clock is reset. |
| 110 |
clock.tick(-200000); |
| 111 |
anim.pause(); |
| 112 |
anim.play(); |
| 113 |
|
| 114 |
assertEquals(0, anim.coords[0]); |
| 115 |
assertRoughlyEquals(0, anim.progress, 1e-4); |
| 116 |
|
| 117 |
// Animation shoud still only last a second. |
| 118 |
clock.tick(900); |
| 119 |
anim.pause(); |
| 120 |
anim.play(); |
| 121 |
|
| 122 |
assertEquals(900, anim.coords[0]); |
| 123 |
assertRoughlyEquals(0.9, anim.progress, 1e-4); |
| 124 |
} |
| 125 |
|
| 126 |
function testSetProgress() { |
| 127 |
var anim = new goog.fx.Animation([0], [1000], 3000); |
| 128 |
var nFrames = 0; |
| 129 |
anim.play(); |
| 130 |
anim.setProgress(0.5); |
| 131 |
goog.events.listen(anim, goog.fx.Animation.EventType.ANIMATE, function(e) { |
| 132 |
assertEquals(500, e.coords[0]); |
| 133 |
assertRoughlyEquals(0.5, e.progress, 1e-4); |
| 134 |
nFrames++; |
| 135 |
}); |
| 136 |
anim.cycle(goog.now()); |
| 137 |
anim.stop(); |
| 138 |
assertEquals(1, nFrames); |
| 139 |
} |