animationdelay_test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2012 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.module('goog.async.AnimationDelayTest');
  15. goog.setTestOnly('goog.async.AnimationDelayTest');
  16. var AnimationDelay = goog.require('goog.async.AnimationDelay');
  17. var Promise = goog.require('goog.Promise');
  18. var PropertyReplacer = goog.require('goog.testing.PropertyReplacer');
  19. var Timer = goog.require('goog.Timer');
  20. var jsunit = goog.require('goog.testing.jsunit');
  21. var testSuite = goog.require('goog.testing.testSuite');
  22. var TEST_DELAY = 50;
  23. var stubs = new PropertyReplacer();
  24. testSuite({
  25. tearDown: function() { stubs.reset(); },
  26. testStart: function() {
  27. var resolver = Promise.withResolver();
  28. var start = goog.now();
  29. var delay = new AnimationDelay(function(end) {
  30. assertNotNull(resolver); // fail if called multiple times
  31. resolver.resolve();
  32. resolver = null;
  33. });
  34. delay.start();
  35. return resolver.promise;
  36. },
  37. testStop: function() {
  38. var resolver = Promise.withResolver();
  39. var start = goog.now();
  40. var delay = new AnimationDelay(function(end) { resolver.reject(); });
  41. delay.start();
  42. delay.stop();
  43. return Timer.promise(TEST_DELAY).then(function() {
  44. resolver.resolve();
  45. return resolver.promise;
  46. });
  47. },
  48. testAlwaysUseGoogNowForHandlerTimestamp: function() {
  49. var resolver = Promise.withResolver();
  50. var expectedValue = 12345.1;
  51. stubs.set(goog, 'now', function() { return expectedValue; });
  52. var delay = new AnimationDelay(function(timestamp) {
  53. assertEquals(expectedValue, timestamp);
  54. resolver.resolve();
  55. });
  56. delay.start();
  57. return resolver.promise;
  58. },
  59. testStartIfActive: function() {
  60. var delay = new AnimationDelay(goog.nullFunction);
  61. delay.start();
  62. var startWasCalled = false;
  63. stubs.set(AnimationDelay.prototype, 'start', function() {
  64. startWasCalled = true;
  65. });
  66. delay.startIfNotActive();
  67. assertEquals(startWasCalled, false);
  68. delay.stop();
  69. delay.startIfNotActive();
  70. assertEquals(startWasCalled, true);
  71. }
  72. });