OLD | NEW |
---|---|
(Empty) | |
1 'use strict'; | |
2 if (self.importScripts) { | |
3 self.importScripts('../../../resources/testharness.js'); | |
4 } | |
5 // | |
domenic
2015/06/16 15:42:36
You killed all my nice separating newlines :(
jochen (gone - plz use gerrit)
2015/06/16 18:08:18
I blame this on paste... I'll put them back in
| |
6 // Straightforward unhandledrejection tests | |
7 // | |
8 async_test(function(t) { | |
9 var e = new Error(); | |
10 var p; | |
11 onUnhandledSucceed(t, e, function() { return p; }); | |
12 p = Promise.reject(e); | |
13 }, 'unhandledrejection: from Promise.reject'); | |
14 async_test(function(t) { | |
15 var e = new Error(); | |
16 var p; | |
17 onUnhandledSucceed(t, e, function() { return p; }); | |
18 p = new Promise(function(_, reject) { | |
19 reject(e); | |
20 }); | |
21 }, 'unhandledrejection: from a synchronous rejection in new Promise'); | |
22 async_test(function(t) { | |
23 var e = new Error(); | |
24 var p; | |
25 onUnhandledSucceed(t, e, function() { return p; }); | |
26 p = new Promise(function(_, reject) { | |
27 postMessageTask(function() { | |
28 reject(e); | |
29 }); | |
30 }); | |
31 }, 'unhandledrejection: from a task-delayed rejection'); | |
32 async_test(function(t) { | |
33 var e = new Error(); | |
34 var p; | |
35 onUnhandledSucceed(t, e, function() { return p; }); | |
36 p = new Promise(function(_, reject) { | |
37 setTimeout(function() { | |
38 reject(e); | |
39 }, 1); | |
40 }); | |
41 }, 'unhandledrejection: from a setTimeout-delayed rejection'); | |
42 async_test(function(t) { | |
43 var e = new Error(); | |
44 var e2 = new Error(); | |
45 var promise2; | |
46 onUnhandledSucceed(t, e2, function() { return promise2; }); | |
47 var unreached = t.unreached_func('promise should not be fulfilled'); | |
48 promise2 = Promise.reject(e).then(unreached, function(reason) { | |
49 t.step(function() { | |
50 assert_equals(reason, e); | |
51 }); | |
52 throw e2; | |
53 }); | |
54 }, 'unhandledrejection: from a throw in a rejection handler chained off of Promi se.reject'); | |
55 async_test(function(t) { | |
56 var e = new Error(); | |
57 var e2 = new Error(); | |
58 var promise2; | |
59 onUnhandledSucceed(t, e2, function() { return promise2; }); | |
60 var unreached = t.unreached_func('promise should not be fulfilled'); | |
61 promise2 = new Promise(function(_, reject) { | |
62 setTimeout(function() { | |
63 reject(e); | |
64 }, 1); | |
65 }).then(unreached, function(reason) { | |
66 t.step(function() { | |
67 assert_equals(reason, e); | |
68 }); | |
69 throw e2; | |
70 }); | |
71 }, 'unhandledrejection: from a throw in a rejection handler chained off of a set Timeout-delayed rejection'); | |
72 async_test(function(t) { | |
73 var e = new Error(); | |
74 var e2 = new Error(); | |
75 var promise2; | |
76 onUnhandledSucceed(t, e2, function() { return promise2; }); | |
77 var promise = new Promise(function(_, reject) { | |
78 setTimeout(function() { | |
79 reject(e); | |
80 postMicroTask(function() { | |
81 var unreached = t.unreached_func('promise should not be fulfilled'); | |
82 promise2 = promise.then(unreached, function(reason) { | |
83 t.step(function() { | |
84 assert_equals(reason, e); | |
85 }); | |
86 throw e2; | |
87 }); | |
88 }); | |
89 }, 1); | |
90 }); | |
91 }, 'unhandledrejection: from a throw in a rejection handler attached one microta sk after a setTimeout-delayed rejection'); | |
92 async_test(function(t) { | |
93 var e = new Error(); | |
94 var p; | |
95 onUnhandledSucceed(t, e, function() { return p; }); | |
96 p = Promise.resolve().then(function() { | |
97 return Promise.reject(e); | |
98 }); | |
99 }, 'unhandledrejection: from returning a Promise.reject-created rejection in a f ulfillment handler'); | |
100 async_test(function(t) { | |
101 var e = new Error(); | |
102 var p; | |
103 onUnhandledSucceed(t, e, function() { return p; }); | |
104 p = Promise.resolve().then(function() { | |
105 throw e; | |
106 }); | |
107 }, 'unhandledrejection: from a throw in a fulfillment handler'); | |
108 async_test(function(t) { | |
109 var e = new Error(); | |
110 var p; | |
111 onUnhandledSucceed(t, e, function() { return p; }); | |
112 p = Promise.resolve().then(function() { | |
113 return new Promise(function(_, reject) { | |
114 setTimeout(function() { | |
115 reject(e); | |
116 }, 1); | |
117 }); | |
118 }); | |
119 }, 'unhandledrejection: from returning a setTimeout-delayed rejection in a fulfi llment handler'); | |
120 async_test(function(t) { | |
121 var e = new Error(); | |
122 var p; | |
123 onUnhandledSucceed(t, e, function() { return p; }); | |
124 p = Promise.all([Promise.reject(e)]); | |
125 }, 'unhandledrejection: from Promise.reject, indirected through Promise.all'); | |
126 // | |
127 // Negative unhandledrejection/rejectionhandled tests with immediate attachment | |
128 // | |
129 async_test(function(t) { | |
130 var e = new Error(); | |
131 var p; | |
132 onUnhandledFail(t, function() { return p; }); | |
133 var unreached = t.unreached_func('promise should not be fulfilled'); | |
134 p = Promise.reject(e).then(unreached, function() {}); | |
135 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono usly to a promise from Promise.reject'); | |
136 async_test(function(t) { | |
137 var e = new Error(); | |
138 var p; | |
139 onUnhandledFail(t, function() { return p; }); | |
140 var unreached = t.unreached_func('promise should not be fulfilled'); | |
141 p = Promise.all([Promise.reject(e)]).then(unreached, function() {}); | |
142 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono usly to a promise from ' + | |
143 'Promise.reject, indirecting through Promise.all'); | |
144 async_test(function(t) { | |
145 var e = new Error(); | |
146 var p; | |
147 onUnhandledFail(t, function() { return p; }); | |
148 var unreached = t.unreached_func('promise should not be fulfilled'); | |
149 p = new Promise(function(_, reject) { | |
150 reject(e); | |
151 }).then(unreached, function() {}); | |
152 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono usly to a synchronously-rejected ' + | |
153 'promise created with new Promise'); | |
154 async_test(function(t) { | |
155 var e = new Error(); | |
156 var p; | |
157 onUnhandledFail(t, function() { return p; }); | |
158 var unreached = t.unreached_func('promise should not be fulfilled'); | |
159 p = Promise.resolve().then(function() { | |
160 throw e; | |
161 }).then(unreached, function(reason) { | |
162 t.step(function() { | |
163 assert_equals(reason, e); | |
164 }); | |
165 }); | |
166 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono usly to a promise created from ' + | |
167 'throwing in a fulfillment handler'); | |
168 async_test(function(t) { | |
169 var e = new Error(); | |
170 var p; | |
171 onUnhandledFail(t, function() { return p; }); | |
172 var unreached = t.unreached_func('promise should not be fulfilled'); | |
173 p = Promise.resolve().then(function() { | |
174 return Promise.reject(e); | |
175 }).then(unreached, function(reason) { | |
176 t.step(function() { | |
177 assert_equals(reason, e); | |
178 }); | |
179 }); | |
180 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono usly to a promise created from ' + | |
181 'returning a Promise.reject-created promise in a fulfillment handler'); | |
182 async_test(function(t) { | |
183 var e = new Error(); | |
184 var p; | |
185 onUnhandledFail(t, function() { return p; }); | |
186 var unreached = t.unreached_func('promise should not be fulfilled'); | |
187 p = Promise.resolve().then(function() { | |
188 return new Promise(function(_, reject) { | |
189 setTimeout(function() { | |
190 reject(e); | |
191 }, 1); | |
192 }); | |
193 }).then(unreached, function(reason) { | |
194 t.step(function() { | |
195 assert_equals(reason, e); | |
196 }); | |
197 }); | |
198 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono usly to a promise created from ' + | |
199 'returning a setTimeout-delayed rejection in a fulfillment handler'); | |
200 async_test(function(t) { | |
201 var e = new Error(); | |
202 var p; | |
203 onUnhandledFail(t, function() { return p; }); | |
204 postMessageTask(function() { | |
205 p = Promise.resolve().then(function() { | |
206 return Promise.reject(e); | |
207 }) | |
208 .catch(function() {}); | |
209 }); | |
210 }, 'no unhandledrejection/rejectionhandled: all inside a queued task, a rejectio n handler attached synchronously to ' + | |
211 'a promise created from returning a Promise.reject-created promise in a fulfi llment handler'); | |
212 // | |
213 // Negative unhandledrejection/rejectionhandled tests with delayed attachment | |
214 // | |
215 async_test(function(t) { | |
216 var e = new Error(); | |
217 var p; | |
218 onUnhandledFail(t, function() { return p; }); | |
219 p = Promise.reject(e); | |
220 postMicroTask(function() { | |
221 var unreached = t.unreached_func('promise should not be fulfilled'); | |
222 p.then(unreached, function() {}); | |
223 }); | |
224 }, 'delayed handling: a microtask delay before attaching a handler prevents both events (Promise.reject-created ' + | |
225 'promise)'); | |
226 async_test(function(t) { | |
227 var e = new Error(); | |
228 var p; | |
229 onUnhandledFail(t, function() { return p; }); | |
230 p = new Promise(function(_, reject) { | |
231 reject(e); | |
232 }); | |
233 postMicroTask(function() { | |
234 var unreached = t.unreached_func('promise should not be fulfilled'); | |
235 p.then(unreached, function() {}); | |
236 }); | |
237 }, 'delayed handling: a microtask delay before attaching a handler prevents both events (immediately-rejected new ' + | |
238 'Promise-created promise)'); | |
239 async_test(function(t) { | |
240 var e = new Error(); | |
241 var p1; | |
242 var p2; | |
243 onUnhandledFail(t, function() { return p1; }); | |
244 onUnhandledFail(t, function() { return p2; }); | |
245 p1 = new Promise(function(_, reject) { | |
246 postMicroTask(function() { | |
247 reject(e); | |
248 }); | |
249 }); | |
250 p2 = Promise.all([p1]); | |
251 postMicroTask(function() { | |
252 var unreached = t.unreached_func('promise should not be fulfilled'); | |
253 p2.then(unreached, function() {}); | |
254 }); | |
255 }, 'delayed handling: a microtask delay before attaching the handler, and before rejecting the promise, indirected ' + | |
256 'through Promise.all'); | |
257 // | |
258 // Positive unhandledrejection/rejectionhandled tests with delayed attachment | |
259 // | |
260 async_test(function(t) { | |
261 var e = new Error(); | |
262 var p; | |
263 onUnhandledSucceed(t, e, function() { return p; }); | |
264 var _reject; | |
265 p = new Promise(function(_, reject) { | |
266 _reject = reject; | |
267 }); | |
268 _reject(e); | |
269 postMessageTask(function() { | |
270 var unreached = t.unreached_func('promise should not be fulfilled'); | |
271 p.then(unreached, function() {}); | |
272 }); | |
273 }, 'delayed handling: a task delay before attaching a handler does not prevent u nhandledrejection'); | |
274 async_test(function(t) { | |
275 var unhandledPromises = []; | |
276 var unhandledReasons = []; | |
277 var e = new Error(); | |
278 var p; | |
279 var unhandled = function(ev) { | |
280 if (ev.promise === p) { | |
281 t.step(function() { | |
282 unhandledPromises.push(ev.promise); | |
283 unhandledReasons.push(ev.reason); | |
284 }); | |
285 } | |
286 }; | |
287 var handled = function(ev) { | |
288 if (ev.promise === p) { | |
289 t.step(function() { | |
290 assert_array_equals(unhandledPromises, [p]); | |
291 assert_array_equals(unhandledReasons, [e]); | |
292 assert_equals(ev.promise, p); | |
293 assert_equals(ev.reason, e); | |
294 }); | |
295 } | |
296 }; | |
297 addEventListener('unhandledrejection', unhandled); | |
298 addEventListener('rejectionhandled', handled); | |
299 ensureCleanup(t, unhandled, handled); | |
300 p = new Promise(function() { | |
301 throw e; | |
302 }); | |
303 setTimeout(function() { | |
304 var unreached = t.unreached_func('promise should not be fulfilled'); | |
305 p.then(unreached, function(reason) { | |
306 assert_equals(reason, e); | |
307 setTimeout(function() { t.done(); }, 10); | |
308 }); | |
309 }, 10); | |
310 }, 'delayed handling: delaying handling by setTimeout(,10) will cause both event s to fire'); | |
311 async_test(function(t) { | |
312 var e = new Error(); | |
313 var p; | |
314 onUnhandledSucceed(t, e, function() { return p; }); | |
315 p = Promise.reject(e); | |
316 postMessageTask(function() { | |
317 Promise.resolve().then(function() { | |
318 p.catch(function() {}); | |
319 }); | |
320 }); | |
321 }, 'delayed handling: postMessageTask after promise creation/rejection, plus pro mise microtasks, is too late to ' + | |
322 'attach a rejection handler'); | |
323 async_test(function(t) { | |
324 var e = new Error(); | |
325 var p; | |
326 onUnhandledSucceed(t, e, function() { return p; }); | |
327 postMessageTask(function() { | |
328 Promise.resolve().then(function() { | |
329 Promise.resolve().then(function() { | |
330 Promise.resolve().then(function() { | |
331 Promise.resolve().then(function() { | |
332 p.catch(function() {}); | |
333 }); | |
334 }); | |
335 }); | |
336 }); | |
337 }); | |
338 p = Promise.reject(e); | |
339 }, 'delayed handling: postMessageTask before promise creation/rejection, plus ma ny promise microtasks, is too late ' + | |
340 'to attach a rejection handler'); | |
341 async_test(function(t) { | |
342 var e = new Error(); | |
343 var p; | |
344 onUnhandledSucceed(t, e, function() { return p; }); | |
345 p = Promise.reject(e); | |
346 postMessageTask(function() { | |
347 Promise.resolve().then(function() { | |
348 Promise.resolve().then(function() { | |
349 Promise.resolve().then(function() { | |
350 Promise.resolve().then(function() { | |
351 p.catch(function() {}); | |
352 }); | |
353 }); | |
354 }); | |
355 }); | |
356 }); | |
357 }, 'delayed handling: postMessageTask after promise creation/rejection, plus man y promise microtasks, is too late ' + | |
358 'to attach a rejection handler'); | |
359 // | |
360 // Miscellaneous tests about integration with the rest of the platform | |
361 // | |
362 async_test(function(t) { | |
363 var e = new Error(); | |
364 var l = function(ev) { | |
365 var order = []; | |
366 postMicroTask(function() { | |
367 order.push(1); | |
368 }); | |
369 setTimeout(function() { | |
370 order.push(2); | |
371 t.step(function() { | |
372 assert_array_equals(order, [1, 2]); | |
373 }); | |
374 t.done(); | |
375 }, 1); | |
376 }; | |
377 addEventListener('unhandledrejection', l); | |
378 ensureCleanup(t, l); | |
379 Promise.reject(e); | |
380 }, 'postMicroTask vs. postMessageTask ordering is not disturbed inside unhandled rejection events'); | |
381 // | |
382 // HELPERS | |
383 // | |
384 function postMessageTask(f) { | |
385 if ('document' in self) { | |
386 var l = function() { | |
387 removeEventListener('message', l); | |
388 f(); | |
389 }; | |
390 addEventListener('message', l); | |
391 postMessage('abusingpostmessageforfunandprofit', '*'); | |
392 } else { | |
393 setTimeout(function() { f(); }, 0); | |
domenic
2015/06/16 15:42:36
Should use MessageChannel in workers. https://gith
jochen (gone - plz use gerrit)
2015/06/17 07:57:53
done
| |
394 } | |
395 } | |
396 function postMicroTask(f) { | |
397 Promise.resolve().then(function() { f(); }); | |
domenic
2015/06/16 15:42:36
Kind of the point of the microtask tests is to tes
jochen (gone - plz use gerrit)
2015/06/16 18:08:18
i'll put them back.
from an implementation point
| |
398 } | |
399 function onUnhandledSucceed(t, expectedReason, expectedPromiseGetter) { | |
400 var l = function(ev) { | |
401 if (ev.promise === expectedPromiseGetter()) { | |
domenic
2015/06/16 15:42:36
This if seems bad. If they mismatch, then the test
jochen (gone - plz use gerrit)
2015/06/16 18:08:18
without this, none of the tests will work, as they
| |
402 t.step(function() { | |
403 assert_equals(ev.reason, expectedReason); | |
404 assert_equals(ev.promise, expectedPromiseGetter()); | |
405 }); | |
406 t.done(); | |
407 } | |
408 }; | |
409 addEventListener('unhandledrejection', l); | |
410 ensureCleanup(t, l); | |
411 } | |
412 function onUnhandledFail(t, expectedPromiseGetter) { | |
413 var unhandled = function(evt) { | |
414 if (evt.promise === expectedPromiseGetter()) { | |
415 t.unreached_func('unhandledrejection event is not supposed to be triggered '); | |
416 } | |
417 }; | |
418 var handled = function(evt) { | |
419 if (evt.promise === expectedPromiseGetter()) { | |
420 t.unreached_func('rejectionhandled event is not supposed to be triggered') ; | |
421 } | |
422 }; | |
423 addEventListener('unhandledrejection', unhandled); | |
424 addEventListener('rejectionhandled', handled); | |
425 ensureCleanup(t, unhandled, handled); | |
426 setTimeout(function() { | |
427 t.done(); | |
428 }, 10); | |
429 } | |
430 function ensureCleanup(t, unhandled, handled) { | |
431 t.add_cleanup(function() { | |
432 if (unhandled) | |
433 removeEventListener('unhandledrejection', unhandled); | |
434 if (handled) | |
435 removeEventListener('rejectionhandled', handled); | |
436 }); | |
437 } | |
438 | |
439 done(); | |
OLD | NEW |