cssspriteanimation_test.js 3.8 KB

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