nexttick_test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.nextTickTest');
  15. goog.setTestOnly('goog.async.nextTickTest');
  16. goog.require('goog.Promise');
  17. goog.require('goog.Timer');
  18. goog.require('goog.async.nextTick');
  19. goog.require('goog.debug.ErrorHandler');
  20. goog.require('goog.debug.entryPointRegistry');
  21. goog.require('goog.dom');
  22. goog.require('goog.dom.TagName');
  23. goog.require('goog.labs.userAgent.browser');
  24. goog.require('goog.testing.MockClock');
  25. goog.require('goog.testing.PropertyReplacer');
  26. goog.require('goog.testing.jsunit');
  27. var clock;
  28. var propertyReplacer = new goog.testing.PropertyReplacer();
  29. function setUp() {
  30. clock = null;
  31. }
  32. function tearDown() {
  33. if (clock) {
  34. clock.uninstall();
  35. }
  36. // Unset the cached setImmediate_ behavior so it's re-evaluated for each test.
  37. goog.async.nextTick.setImmediate_ = undefined;
  38. propertyReplacer.reset();
  39. }
  40. function testNextTick() {
  41. return new goog.Promise(function(resolve, reject) {
  42. var c = 0;
  43. var max = 100;
  44. var async = true;
  45. var counterStep = function(i) {
  46. async = false;
  47. assertEquals('Order correct', i, c);
  48. c++;
  49. if (c === max) {
  50. resolve();
  51. }
  52. };
  53. for (var i = 0; i < max; i++) {
  54. goog.async.nextTick(goog.partial(counterStep, i));
  55. }
  56. assertTrue(async);
  57. });
  58. }
  59. function testNextTickSetImmediate() {
  60. return new goog.Promise(function(resolve, reject) {
  61. var c = 0;
  62. var max = 100;
  63. var async = true;
  64. var counterStep = function(i) {
  65. async = false;
  66. assertEquals('Order correct', i, c);
  67. c++;
  68. if (c === max) {
  69. resolve();
  70. }
  71. };
  72. for (var i = 0; i < max; i++) {
  73. goog.async.nextTick(
  74. goog.partial(counterStep, i), undefined,
  75. /* opt_useSetImmediate */ true);
  76. }
  77. assertTrue(async);
  78. });
  79. }
  80. function testNextTickContext() {
  81. return new goog.Promise(function(resolve, reject) {
  82. var context = {};
  83. var c = 0;
  84. var max = 10;
  85. var async = true;
  86. var counterStep = function(i) {
  87. async = false;
  88. assertEquals('Order correct', i, c);
  89. assertEquals(context, this);
  90. c++;
  91. if (c === max) {
  92. resolve();
  93. }
  94. };
  95. for (var i = 0; i < max; i++) {
  96. goog.async.nextTick(goog.partial(counterStep, i), context);
  97. }
  98. assertTrue(async);
  99. });
  100. }
  101. function testNextTickMockClock() {
  102. clock = new goog.testing.MockClock(true);
  103. var result = '';
  104. goog.async.nextTick(function() { result += 'a'; });
  105. goog.async.nextTick(function() { result += 'b'; });
  106. goog.async.nextTick(function() { result += 'c'; });
  107. assertEquals('', result);
  108. clock.tick(0);
  109. assertEquals('abc', result);
  110. }
  111. function testNextTickDoesntSwallowError() {
  112. return new goog.Promise(function(resolve, reject) {
  113. var sentinel = 'sentinel';
  114. propertyReplacer.replace(window, 'onerror', function(e) {
  115. e = '' + e;
  116. // Don't test for contents in IE7, which does not preserve the exception
  117. // message.
  118. if (e.indexOf('Exception thrown and not caught') == -1) {
  119. assertContains(sentinel, e);
  120. }
  121. resolve();
  122. return false;
  123. });
  124. goog.async.nextTick(function() { throw sentinel; });
  125. });
  126. }
  127. function testNextTickProtectEntryPoint() {
  128. return new goog.Promise(function(resolve, reject) {
  129. var errorHandlerCallbackCalled = false;
  130. var errorHandler = new goog.debug.ErrorHandler(function() {
  131. errorHandlerCallbackCalled = true;
  132. });
  133. // MS Edge will always use goog.global.setImmediate, so ensure we get
  134. // to setImmediate_ here. See useSetImmediate_ implementation for details on
  135. // Edge special casing.
  136. propertyReplacer.set(
  137. goog.async.nextTick, 'useSetImmediate_', function() { return false; });
  138. // This is only testing wrapping the callback with the protected entry
  139. // point, so it's okay to replace this function with a fake.
  140. propertyReplacer.set(goog.async.nextTick, 'setImmediate_', function(cb) {
  141. try {
  142. cb();
  143. fail('The callback should have thrown an error.');
  144. } catch (e) {
  145. assertTrue(errorHandlerCallbackCalled);
  146. assertTrue(e instanceof goog.debug.ErrorHandler.ProtectedFunctionError);
  147. } finally {
  148. // Restore setImmediate so it doesn't interfere with Promise behavior.
  149. propertyReplacer.reset();
  150. }
  151. resolve();
  152. });
  153. goog.debug.entryPointRegistry.monitorAll(errorHandler);
  154. goog.async.nextTick(function() {
  155. throw Error('This should be caught by the protected function.');
  156. });
  157. });
  158. }
  159. function testNextTick_notStarvedBySetTimeout() {
  160. // This test will timeout when affected by
  161. // http://codeforhire.com/2013/09/21/setimmediate-and-messagechannel-broken-on-internet-explorer-10/
  162. // This test would fail without the fix introduced in cl/72472221
  163. // It keeps scheduling 0 timeouts and a single nextTick. If the nextTick
  164. // ever fires, the IE specific problem does not occur.
  165. var timeout;
  166. function busy() {
  167. timeout = setTimeout(function() { busy(); }, 0);
  168. }
  169. busy();
  170. return new goog.Promise(function(resolve, reject) {
  171. goog.async.nextTick(function() {
  172. if (timeout) {
  173. clearTimeout(timeout);
  174. }
  175. resolve();
  176. });
  177. });
  178. }
  179. /**
  180. * Test a scenario in which the iframe used by the postMessage polyfill gets a
  181. * message that does not have match what is expected. In this case, the polyfill
  182. * should not try to invoke a callback (which would result in an error because
  183. * there would be no callbacks in the linked list).
  184. */
  185. function testPostMessagePolyfillDoesNotPumpCallbackQueueIfMessageIsIncorrect() {
  186. // EDGE/IE does not use the postMessage polyfill.
  187. if (goog.labs.userAgent.browser.isIE() ||
  188. goog.labs.userAgent.browser.isEdge()) {
  189. return;
  190. }
  191. // Force postMessage polyfill for setImmediate.
  192. propertyReplacer.set(window, 'setImmediate', undefined);
  193. propertyReplacer.set(window, 'MessageChannel', undefined);
  194. var callbackCalled = false;
  195. goog.async.nextTick(function() { callbackCalled = true; });
  196. var frame = goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME)[0];
  197. frame.contentWindow.postMessage(
  198. 'bogus message', window.location.protocol + '//' + window.location.host);
  199. var error = null;
  200. frame.contentWindow.onerror = function(e) { error = e; };
  201. return goog.Timer.promise(3)
  202. .then(function() {
  203. assert('Callback should have been called.', callbackCalled);
  204. assertNull('An unexpected error was thrown.', error);
  205. })
  206. .thenAlways(function() { goog.dom.removeNode(frame); });
  207. }
  208. function testBehaviorOnPagesWithOverriddenWindowConstructor() {
  209. propertyReplacer.set(goog.global, 'Window', {});
  210. testNextTick();
  211. testNextTickSetImmediate();
  212. testNextTickMockClock();
  213. }