| 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.CssSpriteAnimationTest'); |
| 16 |
goog.setTestOnly('goog.fx.CssSpriteAnimationTest'); |
| 17 |
|
| 18 |
goog.require('goog.fx.CssSpriteAnimation'); |
| 19 |
goog.require('goog.math.Box'); |
| 20 |
goog.require('goog.math.Size'); |
| 21 |
goog.require('goog.testing.MockClock'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
|
| 24 |
var el; |
| 25 |
var size; |
| 26 |
var box; |
| 27 |
var time = 1000; |
| 28 |
var anim, clock; |
| 29 |
|
| 30 |
function setUpPage() { |
| 31 |
clock = new goog.testing.MockClock(true); |
| 32 |
el = document.getElementById('test'); |
| 33 |
size = new goog.math.Size(10, 10); |
| 34 |
box = new goog.math.Box(0, 10, 100, 0); |
| 35 |
} |
| 36 |
|
| 37 |
function tearDownPage() { |
| 38 |
clock.dispose(); |
| 39 |
} |
| 40 |
|
| 41 |
function tearDown() { |
| 42 |
anim.clearSpritePosition(); |
| 43 |
anim.dispose(); |
| 44 |
} |
| 45 |
|
| 46 |
function assertBackgroundPosition(x, y) { |
| 47 |
if (typeof el.style.backgroundPositionX != 'undefined') { |
| 48 |
assertEquals(x + 'px', el.style.backgroundPositionX); |
| 49 |
assertEquals(y + 'px', el.style.backgroundPositionY); |
| 50 |
} else { |
| 51 |
var bgPos = el.style.backgroundPosition; |
| 52 |
var message = 'Expected <' + x + 'px ' + y + 'px>, found <' + bgPos + '>'; |
| 53 |
if (x == y) { |
| 54 |
// when x and y are the same the browser sometimes collapse the prop |
| 55 |
assertTrue( |
| 56 |
message, |
| 57 |
bgPos == x || // in case of 0 without a unit |
| 58 |
bgPos == x + 'px' || bgPos == x + ' ' + y || |
| 59 |
bgPos == x + 'px ' + y + 'px'); |
| 60 |
} else { |
| 61 |
assertTrue( |
| 62 |
message, bgPos == x + ' ' + y || bgPos == x + 'px ' + y || |
| 63 |
bgPos == x + ' ' + y + 'px' || bgPos == x + 'px ' + y + 'px'); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
function testAnimation() { |
| 69 |
anim = new goog.fx.CssSpriteAnimation(el, size, box, time); |
| 70 |
anim.play(); |
| 71 |
|
| 72 |
assertBackgroundPosition(0, 0); |
| 73 |
|
| 74 |
clock.tick(5); |
| 75 |
assertBackgroundPosition(0, 0); |
| 76 |
|
| 77 |
clock.tick(95); |
| 78 |
assertBackgroundPosition(0, -10); |
| 79 |
|
| 80 |
clock.tick(100); |
| 81 |
assertBackgroundPosition(0, -20); |
| 82 |
|
| 83 |
clock.tick(300); |
| 84 |
assertBackgroundPosition(0, -50); |
| 85 |
|
| 86 |
clock.tick(400); |
| 87 |
assertBackgroundPosition(0, -90); |
| 88 |
|
| 89 |
// loop around to starting position |
| 90 |
clock.tick(100); |
| 91 |
assertBackgroundPosition(0, 0); |
| 92 |
|
| 93 |
assertTrue(anim.isPlaying()); |
| 94 |
assertFalse(anim.isStopped()); |
| 95 |
|
| 96 |
clock.tick(100); |
| 97 |
assertBackgroundPosition(0, -10); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
function testAnimation_disableLoop() { |
| 102 |
anim = new goog.fx.CssSpriteAnimation( |
| 103 |
el, size, box, time, undefined, true /* opt_disableLoop */); |
| 104 |
anim.play(); |
| 105 |
|
| 106 |
assertBackgroundPosition(0, 0); |
| 107 |
|
| 108 |
clock.tick(5); |
| 109 |
assertBackgroundPosition(0, 0); |
| 110 |
|
| 111 |
clock.tick(95); |
| 112 |
assertBackgroundPosition(0, -10); |
| 113 |
|
| 114 |
clock.tick(100); |
| 115 |
assertBackgroundPosition(0, -20); |
| 116 |
|
| 117 |
clock.tick(300); |
| 118 |
assertBackgroundPosition(0, -50); |
| 119 |
|
| 120 |
clock.tick(400); |
| 121 |
assertBackgroundPosition(0, -90); |
| 122 |
|
| 123 |
// loop around to starting position |
| 124 |
clock.tick(100); |
| 125 |
assertBackgroundPosition(0, -90); |
| 126 |
|
| 127 |
assertTrue(anim.isStopped()); |
| 128 |
assertFalse(anim.isPlaying()); |
| 129 |
|
| 130 |
clock.tick(100); |
| 131 |
assertBackgroundPosition(0, -90); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
function testClearSpritePosition() { |
| 136 |
anim = new goog.fx.CssSpriteAnimation(el, size, box, time); |
| 137 |
anim.play(); |
| 138 |
|
| 139 |
assertBackgroundPosition(0, 0); |
| 140 |
|
| 141 |
clock.tick(100); |
| 142 |
assertBackgroundPosition(0, -10); |
| 143 |
anim.clearSpritePosition(); |
| 144 |
|
| 145 |
if (typeof el.style.backgroundPositionX != 'undefined') { |
| 146 |
assertEquals('', el.style.backgroundPositionX); |
| 147 |
assertEquals('', el.style.backgroundPositionY); |
| 148 |
} |
| 149 |
|
| 150 |
assertEquals('', el.style.backgroundPosition); |
| 151 |
} |