OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 if (self.importScripts) { |
| 4 importScripts('/resources/testharness.js'); |
| 5 } |
| 6 |
| 7 setup({ |
| 8 allow_uncaught_exception: true |
| 9 }); |
| 10 |
| 11 // |
| 12 // Straightforward unhandledrejection tests |
| 13 // |
| 14 async_test(function(t) { |
| 15 var e = new Error(); |
| 16 var p; |
| 17 |
| 18 onUnhandledSucceed(t, e, function() { return p; }); |
| 19 |
| 20 p = Promise.reject(e); |
| 21 }, 'unhandledrejection: from Promise.reject'); |
| 22 |
| 23 async_test(function(t) { |
| 24 var e = new Error(); |
| 25 var p; |
| 26 |
| 27 onUnhandledSucceed(t, e, function() { return p; }); |
| 28 |
| 29 p = new Promise(function(_, reject) { |
| 30 reject(e); |
| 31 }); |
| 32 }, 'unhandledrejection: from a synchronous rejection in new Promise'); |
| 33 |
| 34 async_test(function(t) { |
| 35 var e = new Error(); |
| 36 var p; |
| 37 |
| 38 onUnhandledSucceed(t, e, function() { return p; }); |
| 39 |
| 40 p = new Promise(function(_, reject) { |
| 41 postMessageTask(function() { |
| 42 reject(e); |
| 43 }); |
| 44 }); |
| 45 }, 'unhandledrejection: from a task-delayed rejection'); |
| 46 |
| 47 async_test(function(t) { |
| 48 var e = new Error(); |
| 49 var p; |
| 50 |
| 51 onUnhandledSucceed(t, e, function() { return p; }); |
| 52 |
| 53 p = new Promise(function(_, reject) { |
| 54 setTimeout(function() { |
| 55 reject(e); |
| 56 }, 1); |
| 57 }); |
| 58 }, 'unhandledrejection: from a setTimeout-delayed rejection'); |
| 59 |
| 60 async_test(function(t) { |
| 61 var e = new Error(); |
| 62 var e2 = new Error(); |
| 63 var promise2; |
| 64 |
| 65 onUnhandledSucceed(t, e2, function() { return promise2; }); |
| 66 |
| 67 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 68 promise2 = Promise.reject(e).then(unreached, function(reason) { |
| 69 t.step(function() { |
| 70 assert_equals(reason, e); |
| 71 }); |
| 72 throw e2; |
| 73 }); |
| 74 }, 'unhandledrejection: from a throw in a rejection handler chained off of Promi
se.reject'); |
| 75 |
| 76 async_test(function(t) { |
| 77 var e = new Error(); |
| 78 var e2 = new Error(); |
| 79 var promise2; |
| 80 |
| 81 onUnhandledSucceed(t, e2, function() { return promise2; }); |
| 82 |
| 83 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 84 promise2 = new Promise(function(_, reject) { |
| 85 setTimeout(function() { |
| 86 reject(e); |
| 87 }, 1); |
| 88 }).then(unreached, function(reason) { |
| 89 t.step(function() { |
| 90 assert_equals(reason, e); |
| 91 }); |
| 92 throw e2; |
| 93 }); |
| 94 }, 'unhandledrejection: from a throw in a rejection handler chained off of a set
Timeout-delayed rejection'); |
| 95 |
| 96 async_test(function(t) { |
| 97 var e = new Error(); |
| 98 var e2 = new Error(); |
| 99 var promise2; |
| 100 |
| 101 onUnhandledSucceed(t, e2, function() { return promise2; }); |
| 102 |
| 103 var promise = new Promise(function(_, reject) { |
| 104 setTimeout(function() { |
| 105 reject(e); |
| 106 mutationObserverMicrotask(function() { |
| 107 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 108 promise2 = promise.then(unreached, function(reason) { |
| 109 t.step(function() { |
| 110 assert_equals(reason, e); |
| 111 }); |
| 112 throw e2; |
| 113 }); |
| 114 }); |
| 115 }, 1); |
| 116 }); |
| 117 }, 'unhandledrejection: from a throw in a rejection handler attached one microta
sk after a setTimeout-delayed rejection'); |
| 118 |
| 119 async_test(function(t) { |
| 120 var e = new Error(); |
| 121 var p; |
| 122 |
| 123 onUnhandledSucceed(t, e, function() { return p; }); |
| 124 |
| 125 p = Promise.resolve().then(function() { |
| 126 return Promise.reject(e); |
| 127 }); |
| 128 }, 'unhandledrejection: from returning a Promise.reject-created rejection in a f
ulfillment handler'); |
| 129 |
| 130 async_test(function(t) { |
| 131 var e = new Error(); |
| 132 var p; |
| 133 |
| 134 onUnhandledSucceed(t, e, function() { return p; }); |
| 135 |
| 136 p = Promise.resolve().then(function() { |
| 137 throw e; |
| 138 }); |
| 139 }, 'unhandledrejection: from a throw in a fulfillment handler'); |
| 140 |
| 141 async_test(function(t) { |
| 142 var e = new Error(); |
| 143 var p; |
| 144 |
| 145 onUnhandledSucceed(t, e, function() { return p; }); |
| 146 |
| 147 p = Promise.resolve().then(function() { |
| 148 return new Promise(function(_, reject) { |
| 149 setTimeout(function() { |
| 150 reject(e); |
| 151 }, 1); |
| 152 }); |
| 153 }); |
| 154 }, 'unhandledrejection: from returning a setTimeout-delayed rejection in a fulfi
llment handler'); |
| 155 |
| 156 async_test(function(t) { |
| 157 var e = new Error(); |
| 158 var p; |
| 159 |
| 160 onUnhandledSucceed(t, e, function() { return p; }); |
| 161 |
| 162 p = Promise.all([Promise.reject(e)]); |
| 163 }, 'unhandledrejection: from Promise.reject, indirected through Promise.all'); |
| 164 |
| 165 // |
| 166 // Negative unhandledrejection/rejectionhandled tests with immediate attachment |
| 167 // |
| 168 |
| 169 async_test(function(t) { |
| 170 var e = new Error(); |
| 171 var p; |
| 172 |
| 173 onUnhandledFail(t, function() { return p; }); |
| 174 |
| 175 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 176 p = Promise.reject(e).then(unreached, function() {}); |
| 177 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono
usly to a promise from Promise.reject'); |
| 178 |
| 179 async_test(function(t) { |
| 180 var e = new Error(); |
| 181 var p; |
| 182 |
| 183 onUnhandledFail(t, function() { return p; }); |
| 184 |
| 185 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 186 p = Promise.all([Promise.reject(e)]).then(unreached, function() {}); |
| 187 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono
usly to a promise from ' + |
| 188 'Promise.reject, indirecting through Promise.all'); |
| 189 |
| 190 async_test(function(t) { |
| 191 var e = new Error(); |
| 192 var p; |
| 193 |
| 194 onUnhandledFail(t, function() { return p; }); |
| 195 |
| 196 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 197 p = new Promise(function(_, reject) { |
| 198 reject(e); |
| 199 }).then(unreached, function() {}); |
| 200 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono
usly to a synchronously-rejected ' + |
| 201 'promise created with new Promise'); |
| 202 |
| 203 async_test(function(t) { |
| 204 var e = new Error(); |
| 205 var p; |
| 206 |
| 207 onUnhandledFail(t, function() { return p; }); |
| 208 |
| 209 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 210 p = Promise.resolve().then(function() { |
| 211 throw e; |
| 212 }).then(unreached, function(reason) { |
| 213 t.step(function() { |
| 214 assert_equals(reason, e); |
| 215 }); |
| 216 }); |
| 217 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono
usly to a promise created from ' + |
| 218 'throwing in a fulfillment handler'); |
| 219 |
| 220 async_test(function(t) { |
| 221 var e = new Error(); |
| 222 var p; |
| 223 |
| 224 onUnhandledFail(t, function() { return p; }); |
| 225 |
| 226 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 227 p = Promise.resolve().then(function() { |
| 228 return Promise.reject(e); |
| 229 }).then(unreached, function(reason) { |
| 230 t.step(function() { |
| 231 assert_equals(reason, e); |
| 232 }); |
| 233 }); |
| 234 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono
usly to a promise created from ' + |
| 235 'returning a Promise.reject-created promise in a fulfillment handler'); |
| 236 |
| 237 async_test(function(t) { |
| 238 var e = new Error(); |
| 239 var p; |
| 240 |
| 241 onUnhandledFail(t, function() { return p; }); |
| 242 |
| 243 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 244 p = Promise.resolve().then(function() { |
| 245 return new Promise(function(_, reject) { |
| 246 setTimeout(function() { |
| 247 reject(e); |
| 248 }, 1); |
| 249 }); |
| 250 }).then(unreached, function(reason) { |
| 251 t.step(function() { |
| 252 assert_equals(reason, e); |
| 253 }); |
| 254 }); |
| 255 }, 'no unhandledrejection/rejectionhandled: rejection handler attached synchrono
usly to a promise created from ' + |
| 256 'returning a setTimeout-delayed rejection in a fulfillment handler'); |
| 257 |
| 258 async_test(function(t) { |
| 259 var e = new Error(); |
| 260 var p; |
| 261 |
| 262 onUnhandledFail(t, function() { return p; }); |
| 263 |
| 264 postMessageTask(function() { |
| 265 p = Promise.resolve().then(function() { |
| 266 return Promise.reject(e); |
| 267 }) |
| 268 .catch(function() {}); |
| 269 }); |
| 270 }, 'no unhandledrejection/rejectionhandled: all inside a queued task, a rejectio
n handler attached synchronously to ' + |
| 271 'a promise created from returning a Promise.reject-created promise in a fulfi
llment handler'); |
| 272 |
| 273 // |
| 274 // Negative unhandledrejection/rejectionhandled tests with microtask-delayed att
achment |
| 275 // |
| 276 |
| 277 async_test(function(t) { |
| 278 var e = new Error(); |
| 279 var p; |
| 280 |
| 281 onUnhandledFail(t, function() { return p; }); |
| 282 |
| 283 p = Promise.reject(e); |
| 284 mutationObserverMicrotask(function() { |
| 285 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 286 p.then(unreached, function() {}); |
| 287 }); |
| 288 }, 'delayed handling: a microtask delay before attaching a handler prevents both
events (Promise.reject-created ' + |
| 289 'promise)'); |
| 290 |
| 291 async_test(function(t) { |
| 292 var e = new Error(); |
| 293 var p; |
| 294 |
| 295 onUnhandledFail(t, function() { return p; }); |
| 296 |
| 297 p = new Promise(function(_, reject) { |
| 298 reject(e); |
| 299 }); |
| 300 mutationObserverMicrotask(function() { |
| 301 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 302 p.then(unreached, function() {}); |
| 303 }); |
| 304 }, 'delayed handling: a microtask delay before attaching a handler prevents both
events (immediately-rejected new ' + |
| 305 'Promise-created promise)'); |
| 306 |
| 307 async_test(function(t) { |
| 308 var e = new Error(); |
| 309 var p1; |
| 310 var p2; |
| 311 |
| 312 onUnhandledFail(t, function() { return p1; }); |
| 313 onUnhandledFail(t, function() { return p2; }); |
| 314 |
| 315 p1 = new Promise(function(_, reject) { |
| 316 mutationObserverMicrotask(function() { |
| 317 reject(e); |
| 318 }); |
| 319 }); |
| 320 p2 = Promise.all([p1]); |
| 321 mutationObserverMicrotask(function() { |
| 322 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 323 p2.then(unreached, function() {}); |
| 324 }); |
| 325 }, 'delayed handling: a microtask delay before attaching the handler, and before
rejecting the promise, indirected ' + |
| 326 'through Promise.all'); |
| 327 |
| 328 // |
| 329 // Negative unhandledrejection/rejectionhandled tests with nested-microtask-dela
yed attachment |
| 330 // |
| 331 |
| 332 async_test(function(t) { |
| 333 var e = new Error(); |
| 334 var p; |
| 335 |
| 336 onUnhandledFail(t, function() { return p; }); |
| 337 |
| 338 p = Promise.reject(e); |
| 339 mutationObserverMicrotask(function() { |
| 340 Promise.resolve().then(function() { |
| 341 mutationObserverMicrotask(function() { |
| 342 Promise.resolve().then(function() { |
| 343 p.catch(function() {}); |
| 344 }); |
| 345 }); |
| 346 }); |
| 347 }); |
| 348 }, 'microtask nesting: attaching a handler inside a combination of mutationObser
verMicrotask + promise microtasks'); |
| 349 |
| 350 async_test(function(t) { |
| 351 var e = new Error(); |
| 352 var p; |
| 353 |
| 354 onUnhandledFail(t, function() { return p; }); |
| 355 |
| 356 postMessageTask(function() { |
| 357 p = Promise.reject(e); |
| 358 mutationObserverMicrotask(function() { |
| 359 Promise.resolve().then(function() { |
| 360 mutationObserverMicrotask(function() { |
| 361 Promise.resolve().then(function() { |
| 362 p.catch(function() {}); |
| 363 }); |
| 364 }); |
| 365 }); |
| 366 }); |
| 367 }); |
| 368 }, 'microtask nesting: attaching a handler inside a combination of mutationObser
verMicrotask + promise microtasks, ' + |
| 369 'all inside a postMessageTask'); |
| 370 |
| 371 async_test(function(t) { |
| 372 var e = new Error(); |
| 373 var p; |
| 374 |
| 375 onUnhandledFail(t, function() { return p; }); |
| 376 |
| 377 setTimeout(function() { |
| 378 p = Promise.reject(e); |
| 379 mutationObserverMicrotask(function() { |
| 380 Promise.resolve().then(function() { |
| 381 mutationObserverMicrotask(function() { |
| 382 Promise.resolve().then(function() { |
| 383 p.catch(function() {}); |
| 384 }); |
| 385 }); |
| 386 }); |
| 387 }); |
| 388 }, 0); |
| 389 }, 'microtask nesting: attaching a handler inside a combination of mutationObser
verMicrotask + promise microtasks, ' + |
| 390 'all inside a setTimeout'); |
| 391 |
| 392 async_test(function(t) { |
| 393 var e = new Error(); |
| 394 var p; |
| 395 |
| 396 onUnhandledFail(t, function() { return p; }); |
| 397 |
| 398 p = Promise.reject(e); |
| 399 Promise.resolve().then(function() { |
| 400 mutationObserverMicrotask(function() { |
| 401 Promise.resolve().then(function() { |
| 402 mutationObserverMicrotask(function() { |
| 403 p.catch(function() {}); |
| 404 }); |
| 405 }); |
| 406 }); |
| 407 }); |
| 408 }, 'microtask nesting: attaching a handler inside a combination of promise micro
tasks + mutationObserverMicrotask'); |
| 409 |
| 410 async_test(function(t) { |
| 411 var e = new Error(); |
| 412 var p; |
| 413 |
| 414 onUnhandledFail(t, function() { return p; }); |
| 415 |
| 416 postMessageTask(function() { |
| 417 p = Promise.reject(e); |
| 418 Promise.resolve().then(function() { |
| 419 mutationObserverMicrotask(function() { |
| 420 Promise.resolve().then(function() { |
| 421 mutationObserverMicrotask(function() { |
| 422 p.catch(function() {}); |
| 423 }); |
| 424 }); |
| 425 }); |
| 426 }); |
| 427 }); |
| 428 }, 'microtask nesting: attaching a handler inside a combination of promise micro
tasks + mutationObserverMicrotask, ' + |
| 429 'all inside a postMessageTask'); |
| 430 |
| 431 async_test(function(t) { |
| 432 var e = new Error(); |
| 433 var p; |
| 434 |
| 435 onUnhandledFail(t, function() { return p; }); |
| 436 |
| 437 setTimeout(function() { |
| 438 p = Promise.reject(e); |
| 439 Promise.resolve().then(function() { |
| 440 mutationObserverMicrotask(function() { |
| 441 Promise.resolve().then(function() { |
| 442 mutationObserverMicrotask(function() { |
| 443 p.catch(function() {}); |
| 444 }); |
| 445 }); |
| 446 }); |
| 447 }); |
| 448 }, 0); |
| 449 }, 'microtask nesting: attaching a handler inside a combination of promise micro
tasks + mutationObserverMicrotask, ' + |
| 450 'all inside a setTimeout'); |
| 451 |
| 452 |
| 453 // For workers, postMessageTask() involves posting tasks to other threads, so |
| 454 // the following tests don't work there. |
| 455 |
| 456 if ('document' in self) { |
| 457 // |
| 458 // Negative unhandledrejection/rejectionhandled tests with task-delayed attach
ment |
| 459 // |
| 460 |
| 461 async_test(function(t) { |
| 462 var e = new Error(); |
| 463 var p; |
| 464 |
| 465 onUnhandledFail(t, function() { return p; }); |
| 466 |
| 467 var _reject; |
| 468 p = new Promise(function(_, reject) { |
| 469 _reject = reject; |
| 470 }); |
| 471 _reject(e); |
| 472 postMessageTask(function() { |
| 473 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 474 p.then(unreached, function() {}); |
| 475 }); |
| 476 }, 'delayed handling: a task delay before attaching a handler prevents unhandl
edrejection'); |
| 477 |
| 478 async_test(function(t) { |
| 479 var e = new Error(); |
| 480 var p; |
| 481 |
| 482 onUnhandledFail(t, function() { return p; }); |
| 483 |
| 484 p = Promise.reject(e); |
| 485 postMessageTask(function() { |
| 486 Promise.resolve().then(function() { |
| 487 p.catch(function() {}); |
| 488 }); |
| 489 }); |
| 490 }, 'delayed handling: postMessageTask after promise creation/rejection, plus p
romise microtasks, is not too late to ' + |
| 491 'attach a rejection handler'); |
| 492 |
| 493 async_test(function(t) { |
| 494 var e = new Error(); |
| 495 var p; |
| 496 |
| 497 onUnhandledFail(t, function() { return p; }); |
| 498 |
| 499 postMessageTask(function() { |
| 500 Promise.resolve().then(function() { |
| 501 Promise.resolve().then(function() { |
| 502 Promise.resolve().then(function() { |
| 503 Promise.resolve().then(function() { |
| 504 p.catch(function() {}); |
| 505 }); |
| 506 }); |
| 507 }); |
| 508 }); |
| 509 }); |
| 510 p = Promise.reject(e); |
| 511 }, 'delayed handling: postMessageTask before promise creation/rejection, plus
many promise microtasks, is not too ' + |
| 512 'late to attach a rejection handler'); |
| 513 |
| 514 async_test(function(t) { |
| 515 var e = new Error(); |
| 516 var p; |
| 517 |
| 518 onUnhandledFail(t, function() { return p; }); |
| 519 |
| 520 p = Promise.reject(e); |
| 521 postMessageTask(function() { |
| 522 Promise.resolve().then(function() { |
| 523 Promise.resolve().then(function() { |
| 524 Promise.resolve().then(function() { |
| 525 Promise.resolve().then(function() { |
| 526 p.catch(function() {}); |
| 527 }); |
| 528 }); |
| 529 }); |
| 530 }); |
| 531 }); |
| 532 }, 'delayed handling: postMessageTask after promise creation/rejection, plus m
any promise microtasks, is not too ' + |
| 533 'late to attach a rejection handler'); |
| 534 } |
| 535 |
| 536 // |
| 537 // Positive unhandledrejection/rejectionhandled tests with delayed attachment |
| 538 // |
| 539 |
| 540 async_test(function(t) { |
| 541 var e = new Error(); |
| 542 var p; |
| 543 |
| 544 onUnhandledSucceed(t, e, function() { return p; }); |
| 545 |
| 546 var _reject; |
| 547 p = new Promise(function(_, reject) { |
| 548 _reject = reject; |
| 549 }); |
| 550 _reject(e); |
| 551 postMessageTask(function() { |
| 552 postMessageTask(function() { |
| 553 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 554 p.then(unreached, function() {}); |
| 555 }); |
| 556 }); |
| 557 }, 'delayed handling: a nested-task delay before attaching a handler causes unha
ndledrejection'); |
| 558 |
| 559 async_test(function(t) { |
| 560 var e = new Error(); |
| 561 var p; |
| 562 |
| 563 onUnhandledSucceed(t, e, function() { return p; }); |
| 564 |
| 565 p = Promise.reject(e); |
| 566 postMessageTask(function() { |
| 567 postMessageTask(function() { |
| 568 Promise.resolve().then(function() { |
| 569 p.catch(function() {}); |
| 570 }); |
| 571 }); |
| 572 }); |
| 573 }, 'delayed handling: a nested-postMessageTask after promise creation/rejection,
plus promise microtasks, is too ' + |
| 574 'late to attach a rejection handler'); |
| 575 |
| 576 async_test(function(t) { |
| 577 var e = new Error(); |
| 578 var p; |
| 579 |
| 580 onUnhandledSucceed(t, e, function() { return p; }); |
| 581 |
| 582 postMessageTask(function() { |
| 583 postMessageTask(function() { |
| 584 Promise.resolve().then(function() { |
| 585 Promise.resolve().then(function() { |
| 586 Promise.resolve().then(function() { |
| 587 Promise.resolve().then(function() { |
| 588 p.catch(function() {}); |
| 589 }); |
| 590 }); |
| 591 }); |
| 592 }); |
| 593 }); |
| 594 }); |
| 595 p = Promise.reject(e); |
| 596 }, 'delayed handling: a nested-postMessageTask before promise creation/rejection
, plus many promise microtasks, is ' + |
| 597 'too late to attach a rejection handler'); |
| 598 |
| 599 async_test(function(t) { |
| 600 var e = new Error(); |
| 601 var p; |
| 602 |
| 603 onUnhandledSucceed(t, e, function() { return p; }); |
| 604 |
| 605 p = Promise.reject(e); |
| 606 postMessageTask(function() { |
| 607 postMessageTask(function() { |
| 608 Promise.resolve().then(function() { |
| 609 Promise.resolve().then(function() { |
| 610 Promise.resolve().then(function() { |
| 611 Promise.resolve().then(function() { |
| 612 p.catch(function() {}); |
| 613 }); |
| 614 }); |
| 615 }); |
| 616 }); |
| 617 }); |
| 618 }); |
| 619 }, 'delayed handling: a nested-postMessageTask after promise creation/rejection,
plus many promise microtasks, is ' + |
| 620 'too late to attach a rejection handler'); |
| 621 |
| 622 async_test(function(t) { |
| 623 var unhandledPromises = []; |
| 624 var unhandledReasons = []; |
| 625 var e = new Error(); |
| 626 var p; |
| 627 |
| 628 var unhandled = function(ev) { |
| 629 if (ev.promise === p) { |
| 630 t.step(function() { |
| 631 unhandledPromises.push(ev.promise); |
| 632 unhandledReasons.push(ev.reason); |
| 633 }); |
| 634 } |
| 635 }; |
| 636 var handled = function(ev) { |
| 637 if (ev.promise === p) { |
| 638 t.step(function() { |
| 639 assert_array_equals(unhandledPromises, [p]); |
| 640 assert_array_equals(unhandledReasons, [e]); |
| 641 assert_equals(ev.promise, p); |
| 642 assert_equals(ev.reason, e); |
| 643 }); |
| 644 } |
| 645 }; |
| 646 addEventListener('unhandledrejection', unhandled); |
| 647 addEventListener('rejectionhandled', handled); |
| 648 ensureCleanup(t, unhandled, handled); |
| 649 |
| 650 p = new Promise(function() { |
| 651 throw e; |
| 652 }); |
| 653 setTimeout(function() { |
| 654 var unreached = t.unreached_func('promise should not be fulfilled'); |
| 655 p.then(unreached, function(reason) { |
| 656 assert_equals(reason, e); |
| 657 setTimeout(function() { t.done(); }, 10); |
| 658 }); |
| 659 }, 10); |
| 660 }, 'delayed handling: delaying handling by setTimeout(,10) will cause both event
s to fire'); |
| 661 |
| 662 // |
| 663 // Miscellaneous tests about integration with the rest of the platform |
| 664 // |
| 665 |
| 666 async_test(function(t) { |
| 667 var e = new Error(); |
| 668 var l = function(ev) { |
| 669 var order = []; |
| 670 mutationObserverMicrotask(function() { |
| 671 order.push(1); |
| 672 }); |
| 673 setTimeout(function() { |
| 674 order.push(2); |
| 675 t.step(function() { |
| 676 assert_array_equals(order, [1, 2]); |
| 677 }); |
| 678 t.done(); |
| 679 }, 1); |
| 680 }; |
| 681 addEventListener('unhandledrejection', l); |
| 682 ensureCleanup(t, l); |
| 683 Promise.reject(e); |
| 684 }, 'mutationObserverMicrotask vs. postMessageTask ordering is not disturbed insi
de unhandledrejection events'); |
| 685 |
| 686 // For workers, postMessageTask() involves posting tasks to other threads, so |
| 687 // the following tests don't work there. |
| 688 |
| 689 if ('document' in self) { |
| 690 |
| 691 // For the next two see https://github.com/domenic/unhandled-rejections-browse
r-spec/issues/2#issuecomment-121121695 |
| 692 // and the following comments. |
| 693 |
| 694 async_test(function(t) { |
| 695 var sequenceOfEvents = []; |
| 696 |
| 697 addEventListener('unhandledrejection', l); |
| 698 ensureCleanup(t, l); |
| 699 |
| 700 var p1 = Promise.reject(); |
| 701 var p2; |
| 702 postMessageTask(function() { |
| 703 p2 = Promise.reject(); |
| 704 postMessageTask(function() { |
| 705 sequenceOfEvents.push('postMessageTask'); |
| 706 checkSequence(); |
| 707 }); |
| 708 }); |
| 709 |
| 710 function l(ev) { |
| 711 if (ev.promise === p1 || ev.promise === p2) { |
| 712 sequenceOfEvents.push(ev.promise); |
| 713 checkSequence(); |
| 714 } |
| 715 } |
| 716 |
| 717 function checkSequence() { |
| 718 if (sequenceOfEvents.length === 3) { |
| 719 t.step(function() { |
| 720 assert_array_equals(sequenceOfEvents, [p1, 'postMessageTask', p2]); |
| 721 }); |
| 722 t.done(); |
| 723 } |
| 724 } |
| 725 }, 'postMessageTask ordering vs. the task queued for unhandled rejection notif
ication (1)'); |
| 726 |
| 727 async_test(function(t) { |
| 728 var sequenceOfEvents = []; |
| 729 |
| 730 addEventListener('unhandledrejection', l); |
| 731 ensureCleanup(t, l); |
| 732 |
| 733 var p2; |
| 734 postMessageTask(function() { |
| 735 p2 = Promise.reject(); |
| 736 postMessageTask(function() { |
| 737 sequenceOfEvents.push('postMessageTask'); |
| 738 checkSequence(); |
| 739 }); |
| 740 }); |
| 741 |
| 742 function l(ev) { |
| 743 if (ev.promise == p2) { |
| 744 sequenceOfEvents.push(ev.promise); |
| 745 checkSequence(); |
| 746 } |
| 747 } |
| 748 |
| 749 function checkSequence() { |
| 750 if (sequenceOfEvents.length === 2) { |
| 751 t.step(function() { |
| 752 assert_array_equals(sequenceOfEvents, ['postMessageTask', p2]); |
| 753 }); |
| 754 t.done(); |
| 755 } |
| 756 } |
| 757 }, 'postMessageTask ordering vs. the task queued for unhandled rejection notif
ication (2)'); |
| 758 |
| 759 async_test(function(t) { |
| 760 var sequenceOfEvents = []; |
| 761 |
| 762 |
| 763 addEventListener('unhandledrejection', unhandled); |
| 764 addEventListener('rejectionhandled', handled); |
| 765 ensureCleanup(t, unhandled, handled); |
| 766 |
| 767 var p = Promise.reject(); |
| 768 |
| 769 setTimeout(function() { |
| 770 postMessageTask(function() { |
| 771 sequenceOfEvents.push('task before catch'); |
| 772 checkSequence(); |
| 773 }); |
| 774 |
| 775 p.catch(function() { |
| 776 sequenceOfEvents.push('catch'); |
| 777 checkSequence(); |
| 778 }); |
| 779 |
| 780 postMessageTask(function() { |
| 781 sequenceOfEvents.push('task after catch'); |
| 782 checkSequence(); |
| 783 }); |
| 784 |
| 785 sequenceOfEvents.push('after catch'); |
| 786 checkSequence(); |
| 787 }, 10); |
| 788 |
| 789 function unhandled(ev) { |
| 790 if (ev.promise === p) { |
| 791 sequenceOfEvents.push('unhandled'); |
| 792 checkSequence(); |
| 793 } |
| 794 } |
| 795 |
| 796 function handled(ev) { |
| 797 if (ev.promise === p) { |
| 798 sequenceOfEvents.push('handled'); |
| 799 checkSequence(); |
| 800 } |
| 801 } |
| 802 |
| 803 function checkSequence() { |
| 804 if (sequenceOfEvents.length === 6) { |
| 805 t.step(function() { |
| 806 assert_array_equals(sequenceOfEvents, |
| 807 ['unhandled', 'after catch', 'catch', 'task before catch', 'handled'
, 'task after catch']); |
| 808 }); |
| 809 t.done(); |
| 810 } |
| 811 } |
| 812 }, 'rejectionhandled is dispatched from a queued task, and not immediately'); |
| 813 } |
| 814 |
| 815 // |
| 816 // HELPERS |
| 817 // |
| 818 |
| 819 var globalPostMessageCounter = 0; |
| 820 |
| 821 function postMessageTask(f) { |
| 822 if ('document' in self) { |
| 823 var message = 'abusingpostmessageforfunandprofit' + globalPostMessageCounter
; |
| 824 globalPostMessageCounter++; |
| 825 var l = function(ev) { |
| 826 if (ev.data === message) { |
| 827 removeEventListener('message', l); |
| 828 f(); |
| 829 } |
| 830 }; |
| 831 addEventListener('message', l); |
| 832 postMessage(message, '*'); |
| 833 } else { |
| 834 var channel = new MessageChannel(); |
| 835 channel.port1.onmessage = function() { channel.port1.close(); f(); }; |
| 836 channel.port2.postMessage('abusingpostmessageforfunandprofit'); |
| 837 channel.port2.close(); |
| 838 } |
| 839 } |
| 840 |
| 841 function mutationObserverMicrotask(f) { |
| 842 if ('document' in self) { |
| 843 var observer = new MutationObserver(function() { f(); }); |
| 844 var node = document.createTextNode(''); |
| 845 observer.observe(node, { characterData: true }); |
| 846 node.data = 'foo'; |
| 847 } else { |
| 848 // We don't have mutation observers on workers, so just post a promise-based |
| 849 // microtask. |
| 850 Promise.resolve().then(function() { f(); }); |
| 851 } |
| 852 } |
| 853 |
| 854 function onUnhandledSucceed(t, expectedReason, expectedPromiseGetter) { |
| 855 var l = function(ev) { |
| 856 if (ev.promise === expectedPromiseGetter()) { |
| 857 t.step(function() { |
| 858 assert_equals(ev.reason, expectedReason); |
| 859 assert_equals(ev.promise, expectedPromiseGetter()); |
| 860 }); |
| 861 t.done(); |
| 862 } |
| 863 }; |
| 864 addEventListener('unhandledrejection', l); |
| 865 ensureCleanup(t, l); |
| 866 } |
| 867 |
| 868 function onUnhandledFail(t, expectedPromiseGetter) { |
| 869 var unhandled = function(evt) { |
| 870 if (evt.promise === expectedPromiseGetter()) { |
| 871 t.step(function() { |
| 872 assert_unreached('unhandledrejection event is not supposed to be trigger
ed'); |
| 873 }); |
| 874 } |
| 875 }; |
| 876 var handled = function(evt) { |
| 877 if (evt.promise === expectedPromiseGetter()) { |
| 878 t.step(function() { |
| 879 assert_unreached('rejectionhandled event is not supposed to be triggered
'); |
| 880 }); |
| 881 } |
| 882 }; |
| 883 addEventListener('unhandledrejection', unhandled); |
| 884 addEventListener('rejectionhandled', handled); |
| 885 ensureCleanup(t, unhandled, handled); |
| 886 setTimeout(function() { |
| 887 t.done(); |
| 888 }, 10); |
| 889 } |
| 890 |
| 891 function ensureCleanup(t, unhandled, handled) { |
| 892 t.add_cleanup(function() { |
| 893 if (unhandled) |
| 894 removeEventListener('unhandledrejection', unhandled); |
| 895 if (handled) |
| 896 removeEventListener('rejectionhandled', handled); |
| 897 }); |
| 898 } |
| 899 |
| 900 done(); |
OLD | NEW |