run_test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2013 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.async.runTest');
  15. goog.require('goog.async.run');
  16. goog.require('goog.testing.MockClock');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('goog.testing.recordFunction');
  19. goog.setTestOnly('goog.async.runTest');
  20. var mockClock;
  21. var futureCallback1, futureCallback2;
  22. function setUpPage() {
  23. mockClock = new goog.testing.MockClock();
  24. mockClock.install();
  25. }
  26. function setUp() {
  27. mockClock.reset();
  28. futureCallback1 = new goog.testing.recordFunction();
  29. futureCallback2 = new goog.testing.recordFunction();
  30. }
  31. function tearDown() {
  32. futureCallback1 = null;
  33. futureCallback2 = null;
  34. }
  35. function tearDownPage() {
  36. mockClock.uninstall();
  37. goog.dispose(mockClock);
  38. }
  39. function testCalledAsync() {
  40. goog.async.run(futureCallback1);
  41. goog.async.run(futureCallback2);
  42. assertEquals(0, futureCallback1.getCallCount());
  43. assertEquals(0, futureCallback2.getCallCount());
  44. // but the callbacks are scheduled...
  45. mockClock.tick();
  46. // and called.
  47. assertEquals(1, futureCallback1.getCallCount());
  48. assertEquals(1, futureCallback2.getCallCount());
  49. // and aren't called a second time.
  50. assertEquals(1, futureCallback1.getCallCount());
  51. assertEquals(1, futureCallback2.getCallCount());
  52. }
  53. function testSequenceCalledInOrder() {
  54. futureCallback1 = new goog.testing.recordFunction(function() {
  55. // called before futureCallback2
  56. assertEquals(0, futureCallback2.getCallCount());
  57. });
  58. futureCallback2 = new goog.testing.recordFunction(function() {
  59. // called after futureCallback1
  60. assertEquals(1, futureCallback1.getCallCount());
  61. });
  62. goog.async.run(futureCallback1);
  63. goog.async.run(futureCallback2);
  64. // goog.async.run doesn't call the top callback immediately.
  65. assertEquals(0, futureCallback1.getCallCount());
  66. // but the callbacks are scheduled...
  67. mockClock.tick();
  68. // and called during the same "tick".
  69. assertEquals(1, futureCallback1.getCallCount());
  70. assertEquals(1, futureCallback2.getCallCount());
  71. }
  72. function testSequenceScheduledTwice() {
  73. goog.async.run(futureCallback1);
  74. goog.async.run(futureCallback1);
  75. // goog.async.run doesn't call the top callback immediately.
  76. assertEquals(0, futureCallback1.getCallCount());
  77. // but the callbacks are scheduled...
  78. mockClock.tick();
  79. // and called twice during the same "tick".
  80. assertEquals(2, futureCallback1.getCallCount());
  81. }
  82. function testSequenceCalledSync() {
  83. futureCallback1 = new goog.testing.recordFunction(function() {
  84. goog.async.run(futureCallback2);
  85. // goog.async.run doesn't call the inner callback immediately.
  86. assertEquals(0, futureCallback2.getCallCount());
  87. });
  88. goog.async.run(futureCallback1);
  89. // goog.async.run doesn't call the top callback immediately.
  90. assertEquals(0, futureCallback1.getCallCount());
  91. // but the callbacks are scheduled...
  92. mockClock.tick();
  93. // and called during the same "tick".
  94. assertEquals(1, futureCallback1.getCallCount());
  95. assertEquals(1, futureCallback2.getCallCount());
  96. }
  97. function testScope() {
  98. var aScope = {};
  99. goog.async.run(futureCallback1);
  100. goog.async.run(futureCallback2, aScope);
  101. // the callbacks are scheduled...
  102. mockClock.tick();
  103. // and called.
  104. assertEquals(1, futureCallback1.getCallCount());
  105. assertEquals(1, futureCallback2.getCallCount());
  106. // and get the correct scope.
  107. var last1 = futureCallback1.popLastCall();
  108. assertEquals(0, last1.getArguments().length);
  109. assertEquals(goog.global, last1.getThis());
  110. var last2 = futureCallback2.popLastCall();
  111. assertEquals(0, last2.getArguments().length);
  112. assertEquals(aScope, last2.getThis());
  113. }