Debian Codegraph @

closure-library/closure/goog/async/nexttick_test.js

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