animation_test.js 3.4 KB

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