dom/workers/test/promise_worker.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 function ok(a, msg) {
michael@0 2 dump("OK: " + !!a + " => " + a + " " + msg + "\n");
michael@0 3 postMessage({type: 'status', status: !!a, msg: a + ": " + msg });
michael@0 4 }
michael@0 5
michael@0 6 function is(a, b, msg) {
michael@0 7 dump("IS: " + (a===b) + " => " + a + " | " + b + " " + msg + "\n");
michael@0 8 postMessage({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg });
michael@0 9 }
michael@0 10
michael@0 11 function isnot(a, b, msg) {
michael@0 12 dump("ISNOT: " + (a!==b) + " => " + a + " | " + b + " " + msg + "\n");
michael@0 13 postMessage({type: 'status', status: a !== b, msg: a + " !== " + b + ": " + msg });
michael@0 14 }
michael@0 15
michael@0 16 function promiseResolve() {
michael@0 17 ok(Promise, "Promise object should exist");
michael@0 18
michael@0 19 var promise = new Promise(function(resolve, reject) {
michael@0 20 ok(resolve, "Promise.resolve exists");
michael@0 21 ok(reject, "Promise.reject exists");
michael@0 22
michael@0 23 resolve(42);
michael@0 24 }).then(function(what) {
michael@0 25 ok(true, "Then - resolveCb has been called");
michael@0 26 is(what, 42, "ResolveCb received 42");
michael@0 27 runTest();
michael@0 28 }, function() {
michael@0 29 ok(false, "Then - rejectCb has been called");
michael@0 30 runTest();
michael@0 31 });
michael@0 32 }
michael@0 33
michael@0 34 function promiseResolveNoArg() {
michael@0 35 var promise = new Promise(function(resolve, reject) {
michael@0 36 ok(resolve, "Promise.resolve exists");
michael@0 37 ok(reject, "Promise.reject exists");
michael@0 38
michael@0 39 resolve();
michael@0 40 }).then(function(what) {
michael@0 41 ok(true, "Then - resolveCb has been called");
michael@0 42 is(what, undefined, "ResolveCb received undefined");
michael@0 43 runTest();
michael@0 44 }, function() {
michael@0 45 ok(false, "Then - rejectCb has been called");
michael@0 46 runTest();
michael@0 47 });
michael@0 48 }
michael@0 49
michael@0 50 function promiseRejectNoHandler() {
michael@0 51 // This test only checks that the code that reports unhandled errors in the
michael@0 52 // Promises implementation does not crash or leak.
michael@0 53 var promise = new Promise(function(res, rej) {
michael@0 54 noSuchMethod();
michael@0 55 });
michael@0 56 runTest();
michael@0 57 }
michael@0 58
michael@0 59 function promiseReject() {
michael@0 60 var promise = new Promise(function(resolve, reject) {
michael@0 61 reject(42);
michael@0 62 }).then(function(what) {
michael@0 63 ok(false, "Then - resolveCb has been called");
michael@0 64 runTest();
michael@0 65 }, function(what) {
michael@0 66 ok(true, "Then - rejectCb has been called");
michael@0 67 is(what, 42, "RejectCb received 42");
michael@0 68 runTest();
michael@0 69 });
michael@0 70 }
michael@0 71
michael@0 72 function promiseRejectNoArg() {
michael@0 73 var promise = new Promise(function(resolve, reject) {
michael@0 74 reject();
michael@0 75 }).then(function(what) {
michael@0 76 ok(false, "Then - resolveCb has been called");
michael@0 77 runTest();
michael@0 78 }, function(what) {
michael@0 79 ok(true, "Then - rejectCb has been called");
michael@0 80 is(what, undefined, "RejectCb received undefined");
michael@0 81 runTest();
michael@0 82 });
michael@0 83 }
michael@0 84
michael@0 85 function promiseException() {
michael@0 86 var promise = new Promise(function(resolve, reject) {
michael@0 87 throw 42;
michael@0 88 }).then(function(what) {
michael@0 89 ok(false, "Then - resolveCb has been called");
michael@0 90 runTest();
michael@0 91 }, function(what) {
michael@0 92 ok(true, "Then - rejectCb has been called");
michael@0 93 is(what, 42, "RejectCb received 42");
michael@0 94 runTest();
michael@0 95 });
michael@0 96 }
michael@0 97
michael@0 98 function promiseAsync() {
michael@0 99 var global = "foo";
michael@0 100 var f = new Promise(function(r1, r2) {
michael@0 101 is(global, "foo", "Global should be foo");
michael@0 102 r1(42);
michael@0 103 is(global, "foo", "Global should still be foo");
michael@0 104 setTimeout(function() {
michael@0 105 is(global, "bar", "Global should still be bar!");
michael@0 106 runTest();
michael@0 107 }, 0);
michael@0 108 }).then(function() {
michael@0 109 global = "bar";
michael@0 110 });
michael@0 111 is(global, "foo", "Global should still be foo (2)");
michael@0 112 }
michael@0 113
michael@0 114 function promiseDoubleThen() {
michael@0 115 var steps = 0;
michael@0 116 var promise = new Promise(function(r1, r2) {
michael@0 117 r1(42);
michael@0 118 });
michael@0 119
michael@0 120 promise.then(function(what) {
michael@0 121 ok(true, "Then.resolve has been called");
michael@0 122 is(what, 42, "Value == 42");
michael@0 123 steps++;
michael@0 124 }, function(what) {
michael@0 125 ok(false, "Then.reject has been called");
michael@0 126 });
michael@0 127
michael@0 128 promise.then(function(what) {
michael@0 129 ok(true, "Then.resolve has been called");
michael@0 130 is(steps, 1, "Then.resolve - step == 1");
michael@0 131 is(what, 42, "Value == 42");
michael@0 132 runTest();
michael@0 133 }, function(what) {
michael@0 134 ok(false, "Then.reject has been called");
michael@0 135 });
michael@0 136 }
michael@0 137
michael@0 138 function promiseThenException() {
michael@0 139 var promise = new Promise(function(resolve, reject) {
michael@0 140 resolve(42);
michael@0 141 });
michael@0 142
michael@0 143 promise.then(function(what) {
michael@0 144 ok(true, "Then.resolve has been called");
michael@0 145 throw "booh";
michael@0 146 }).catch(function(e) {
michael@0 147 ok(true, "Catch has been called!");
michael@0 148 runTest();
michael@0 149 });
michael@0 150 }
michael@0 151
michael@0 152 function promiseThenCatchThen() {
michael@0 153 var promise = new Promise(function(resolve, reject) {
michael@0 154 resolve(42);
michael@0 155 });
michael@0 156
michael@0 157 var promise2 = promise.then(function(what) {
michael@0 158 ok(true, "Then.resolve has been called");
michael@0 159 is(what, 42, "Value == 42");
michael@0 160 return what + 1;
michael@0 161 }, function(what) {
michael@0 162 ok(false, "Then.reject has been called");
michael@0 163 });
michael@0 164
michael@0 165 isnot(promise, promise2, "These 2 promise objs are different");
michael@0 166
michael@0 167 promise2.then(function(what) {
michael@0 168 ok(true, "Then.resolve has been called");
michael@0 169 is(what, 43, "Value == 43");
michael@0 170 return what + 1;
michael@0 171 }, function(what) {
michael@0 172 ok(false, "Then.reject has been called");
michael@0 173 }).catch(function() {
michael@0 174 ok(false, "Catch has been called");
michael@0 175 }).then(function(what) {
michael@0 176 ok(true, "Then.resolve has been called");
michael@0 177 is(what, 44, "Value == 44");
michael@0 178 runTest();
michael@0 179 }, function(what) {
michael@0 180 ok(false, "Then.reject has been called");
michael@0 181 });
michael@0 182 }
michael@0 183
michael@0 184 function promiseRejectThenCatchThen() {
michael@0 185 var promise = new Promise(function(resolve, reject) {
michael@0 186 reject(42);
michael@0 187 });
michael@0 188
michael@0 189 var promise2 = promise.then(function(what) {
michael@0 190 ok(false, "Then.resolve has been called");
michael@0 191 }, function(what) {
michael@0 192 ok(true, "Then.reject has been called");
michael@0 193 is(what, 42, "Value == 42");
michael@0 194 return what + 1;
michael@0 195 });
michael@0 196
michael@0 197 isnot(promise, promise2, "These 2 promise objs are different");
michael@0 198
michael@0 199 promise2.then(function(what) {
michael@0 200 ok(true, "Then.resolve has been called");
michael@0 201 is(what, 43, "Value == 43");
michael@0 202 return what+1;
michael@0 203 }).catch(function(what) {
michael@0 204 ok(false, "Catch has been called");
michael@0 205 }).then(function(what) {
michael@0 206 ok(true, "Then.resolve has been called");
michael@0 207 is(what, 44, "Value == 44");
michael@0 208 runTest();
michael@0 209 });
michael@0 210 }
michael@0 211
michael@0 212 function promiseRejectThenCatchThen2() {
michael@0 213 var promise = new Promise(function(resolve, reject) {
michael@0 214 reject(42);
michael@0 215 });
michael@0 216
michael@0 217 promise.then(function(what) {
michael@0 218 ok(true, "Then.resolve has been called");
michael@0 219 is(what, 42, "Value == 42");
michael@0 220 return what+1;
michael@0 221 }).catch(function(what) {
michael@0 222 is(what, 42, "Value == 42");
michael@0 223 ok(true, "Catch has been called");
michael@0 224 return what+1;
michael@0 225 }).then(function(what) {
michael@0 226 ok(true, "Then.resolve has been called");
michael@0 227 is(what, 43, "Value == 43");
michael@0 228 runTest();
michael@0 229 });
michael@0 230 }
michael@0 231
michael@0 232 function promiseRejectThenCatchExceptionThen() {
michael@0 233 var promise = new Promise(function(resolve, reject) {
michael@0 234 reject(42);
michael@0 235 });
michael@0 236
michael@0 237 promise.then(function(what) {
michael@0 238 ok(false, "Then.resolve has been called");
michael@0 239 }, function(what) {
michael@0 240 ok(true, "Then.reject has been called");
michael@0 241 is(what, 42, "Value == 42");
michael@0 242 throw(what + 1);
michael@0 243 }).catch(function(what) {
michael@0 244 ok(true, "Catch has been called");
michael@0 245 is(what, 43, "Value == 43");
michael@0 246 return what + 1;
michael@0 247 }).then(function(what) {
michael@0 248 ok(true, "Then.resolve has been called");
michael@0 249 is(what, 44, "Value == 44");
michael@0 250 runTest();
michael@0 251 });
michael@0 252 }
michael@0 253
michael@0 254 function promiseThenCatchOrderingResolve() {
michael@0 255 var global = 0;
michael@0 256 var f = new Promise(function(r1, r2) {
michael@0 257 r1(42);
michael@0 258 });
michael@0 259
michael@0 260 f.then(function() {
michael@0 261 f.then(function() {
michael@0 262 global++;
michael@0 263 });
michael@0 264 f.catch(function() {
michael@0 265 global++;
michael@0 266 });
michael@0 267 f.then(function() {
michael@0 268 global++;
michael@0 269 });
michael@0 270 setTimeout(function() {
michael@0 271 is(global, 2, "Many steps... should return 2");
michael@0 272 runTest();
michael@0 273 }, 0);
michael@0 274 });
michael@0 275 }
michael@0 276
michael@0 277 function promiseThenCatchOrderingReject() {
michael@0 278 var global = 0;
michael@0 279 var f = new Promise(function(r1, r2) {
michael@0 280 r2(42);
michael@0 281 })
michael@0 282
michael@0 283 f.then(function() {}, function() {
michael@0 284 f.then(function() {
michael@0 285 global++;
michael@0 286 });
michael@0 287 f.catch(function() {
michael@0 288 global++;
michael@0 289 });
michael@0 290 f.then(function() {}, function() {
michael@0 291 global++;
michael@0 292 });
michael@0 293 setTimeout(function() {
michael@0 294 is(global, 2, "Many steps... should return 2");
michael@0 295 runTest();
michael@0 296 }, 0);
michael@0 297 });
michael@0 298 }
michael@0 299
michael@0 300 function promiseThenNoArg() {
michael@0 301 var promise = new Promise(function(resolve, reject) {
michael@0 302 resolve(42);
michael@0 303 });
michael@0 304
michael@0 305 var clone = promise.then();
michael@0 306 isnot(promise, clone, "These 2 promise objs are different");
michael@0 307 promise.then(function(v) {
michael@0 308 clone.then(function(cv) {
michael@0 309 is(v, cv, "Both resolve to the same value");
michael@0 310 runTest();
michael@0 311 });
michael@0 312 });
michael@0 313 }
michael@0 314
michael@0 315 function promiseThenUndefinedResolveFunction() {
michael@0 316 var promise = new Promise(function(resolve, reject) {
michael@0 317 reject(42);
michael@0 318 });
michael@0 319
michael@0 320 try {
michael@0 321 promise.then(undefined, function(v) {
michael@0 322 is(v, 42, "Promise rejected with 42");
michael@0 323 runTest();
michael@0 324 });
michael@0 325 } catch (e) {
michael@0 326 ok(false, "then should not throw on undefined resolve function");
michael@0 327 }
michael@0 328 }
michael@0 329
michael@0 330 function promiseThenNullResolveFunction() {
michael@0 331 var promise = new Promise(function(resolve, reject) {
michael@0 332 reject(42);
michael@0 333 });
michael@0 334
michael@0 335 try {
michael@0 336 promise.then(null, function(v) {
michael@0 337 is(v, 42, "Promise rejected with 42");
michael@0 338 runTest();
michael@0 339 });
michael@0 340 } catch (e) {
michael@0 341 ok(false, "then should not throw on null resolve function");
michael@0 342 }
michael@0 343 }
michael@0 344
michael@0 345 function promiseCatchNoArg() {
michael@0 346 var promise = new Promise(function(resolve, reject) {
michael@0 347 reject(42);
michael@0 348 });
michael@0 349
michael@0 350 var clone = promise.catch();
michael@0 351 isnot(promise, clone, "These 2 promise objs are different");
michael@0 352 promise.catch(function(v) {
michael@0 353 clone.catch(function(cv) {
michael@0 354 is(v, cv, "Both reject to the same value");
michael@0 355 runTest();
michael@0 356 });
michael@0 357 });
michael@0 358 }
michael@0 359
michael@0 360 function promiseNestedPromise() {
michael@0 361 new Promise(function(resolve, reject) {
michael@0 362 resolve(new Promise(function(resolve, reject) {
michael@0 363 ok(true, "Nested promise is executed");
michael@0 364 resolve(42);
michael@0 365 }));
michael@0 366 }).then(function(value) {
michael@0 367 is(value, 42, "Nested promise is executed and then == 42");
michael@0 368 runTest();
michael@0 369 });
michael@0 370 }
michael@0 371
michael@0 372 function promiseNestedNestedPromise() {
michael@0 373 new Promise(function(resolve, reject) {
michael@0 374 resolve(new Promise(function(resolve, reject) {
michael@0 375 ok(true, "Nested promise is executed");
michael@0 376 resolve(42);
michael@0 377 }).then(function(what) { return what+1; }));
michael@0 378 }).then(function(value) {
michael@0 379 is(value, 43, "Nested promise is executed and then == 43");
michael@0 380 runTest();
michael@0 381 });
michael@0 382 }
michael@0 383
michael@0 384 function promiseWrongNestedPromise() {
michael@0 385 new Promise(function(resolve, reject) {
michael@0 386 resolve(new Promise(function(r, r2) {
michael@0 387 ok(true, "Nested promise is executed");
michael@0 388 r(42);
michael@0 389 }));
michael@0 390 reject(42);
michael@0 391 }).then(function(value) {
michael@0 392 is(value, 42, "Nested promise is executed and then == 42");
michael@0 393 runTest();
michael@0 394 }, function(value) {
michael@0 395 ok(false, "This is wrong");
michael@0 396 });
michael@0 397 }
michael@0 398
michael@0 399 function promiseLoop() {
michael@0 400 new Promise(function(resolve, reject) {
michael@0 401 resolve(new Promise(function(r1, r2) {
michael@0 402 ok(true, "Nested promise is executed");
michael@0 403 r1(new Promise(function(r1, r2) {
michael@0 404 ok(true, "Nested nested promise is executed");
michael@0 405 r1(42);
michael@0 406 }));
michael@0 407 }));
michael@0 408 }).then(function(value) {
michael@0 409 is(value, 42, "Nested nested promise is executed and then == 42");
michael@0 410 runTest();
michael@0 411 }, function(value) {
michael@0 412 ok(false, "This is wrong");
michael@0 413 });
michael@0 414 }
michael@0 415
michael@0 416 function promiseStaticReject() {
michael@0 417 var promise = Promise.reject(42).then(function(what) {
michael@0 418 ok(false, "This should not be called");
michael@0 419 }, function(what) {
michael@0 420 is(what, 42, "Value == 42");
michael@0 421 runTest();
michael@0 422 });
michael@0 423 }
michael@0 424
michael@0 425 function promiseStaticResolve() {
michael@0 426 var promise = Promise.resolve(42).then(function(what) {
michael@0 427 is(what, 42, "Value == 42");
michael@0 428 runTest();
michael@0 429 }, function() {
michael@0 430 ok(false, "This should not be called");
michael@0 431 });
michael@0 432 }
michael@0 433
michael@0 434 function promiseResolveNestedPromise() {
michael@0 435 var promise = Promise.resolve(new Promise(function(r, r2) {
michael@0 436 ok(true, "Nested promise is executed");
michael@0 437 r(42);
michael@0 438 }, function() {
michael@0 439 ok(false, "This should not be called");
michael@0 440 })).then(function(what) {
michael@0 441 is(what, 42, "Value == 42");
michael@0 442 runTest();
michael@0 443 }, function() {
michael@0 444 ok(false, "This should not be called");
michael@0 445 });
michael@0 446 }
michael@0 447
michael@0 448 function promiseRejectNoHandler() {
michael@0 449 // This test only checks that the code that reports unhandled errors in the
michael@0 450 // Promises implementation does not crash or leak.
michael@0 451 var promise = new Promise(function(res, rej) {
michael@0 452 noSuchMethod();
michael@0 453 });
michael@0 454 runTest();
michael@0 455 }
michael@0 456
michael@0 457 function promiseUtilitiesDefined() {
michael@0 458 ok(Promise.all, "Promise.all must be defined when Promise is enabled.");
michael@0 459 ok(Promise.race, "Promise.race must be defined when Promise is enabled.");
michael@0 460 runTest();
michael@0 461 }
michael@0 462
michael@0 463 function promiseAllArray() {
michael@0 464 var p = Promise.all([1, new Date(), Promise.resolve("firefox")]);
michael@0 465 ok(p instanceof Promise, "Return value of Promise.all should be a Promise.");
michael@0 466 p.then(function(values) {
michael@0 467 ok(Array.isArray(values), "Resolved value should be an array.");
michael@0 468 is(values.length, 3, "Resolved array length should match iterable's length.");
michael@0 469 is(values[0], 1, "Array values should match.");
michael@0 470 ok(values[1] instanceof Date, "Array values should match.");
michael@0 471 is(values[2], "firefox", "Array values should match.");
michael@0 472 runTest();
michael@0 473 }, function() {
michael@0 474 ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
michael@0 475 runTest();
michael@0 476 });
michael@0 477 }
michael@0 478
michael@0 479 function promiseAllWaitsForAllPromises() {
michael@0 480 var arr = [
michael@0 481 new Promise(function(resolve) {
michael@0 482 setTimeout(resolve.bind(undefined, 1), 50);
michael@0 483 }),
michael@0 484 new Promise(function(resolve) {
michael@0 485 setTimeout(resolve.bind(undefined, 2), 10);
michael@0 486 }),
michael@0 487 new Promise(function(resolve) {
michael@0 488 setTimeout(resolve.bind(undefined, new Promise(function(resolve2) {
michael@0 489 resolve2(3);
michael@0 490 })), 10);
michael@0 491 }),
michael@0 492 new Promise(function(resolve) {
michael@0 493 setTimeout(resolve.bind(undefined, 4), 20);
michael@0 494 })
michael@0 495 ];
michael@0 496
michael@0 497 var p = Promise.all(arr);
michael@0 498 p.then(function(values) {
michael@0 499 ok(Array.isArray(values), "Resolved value should be an array.");
michael@0 500 is(values.length, 4, "Resolved array length should match iterable's length.");
michael@0 501 is(values[0], 1, "Array values should match.");
michael@0 502 is(values[1], 2, "Array values should match.");
michael@0 503 is(values[2], 3, "Array values should match.");
michael@0 504 is(values[3], 4, "Array values should match.");
michael@0 505 runTest();
michael@0 506 }, function() {
michael@0 507 ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
michael@0 508 runTest();
michael@0 509 });
michael@0 510 }
michael@0 511
michael@0 512 function promiseAllRejectFails() {
michael@0 513 var arr = [
michael@0 514 new Promise(function(resolve) {
michael@0 515 setTimeout(resolve.bind(undefined, 1), 50);
michael@0 516 }),
michael@0 517 new Promise(function(resolve, reject) {
michael@0 518 setTimeout(reject.bind(undefined, 2), 10);
michael@0 519 }),
michael@0 520 new Promise(function(resolve) {
michael@0 521 setTimeout(resolve.bind(undefined, 3), 10);
michael@0 522 }),
michael@0 523 new Promise(function(resolve) {
michael@0 524 setTimeout(resolve.bind(undefined, 4), 20);
michael@0 525 })
michael@0 526 ];
michael@0 527
michael@0 528 var p = Promise.all(arr);
michael@0 529 p.then(function(values) {
michael@0 530 ok(false, "Promise.all shouldn't resolve when iterable has rejected Promises.");
michael@0 531 runTest();
michael@0 532 }, function(e) {
michael@0 533 ok(true, "Promise.all should reject when iterable has rejected Promises.");
michael@0 534 is(e, 2, "Rejection value should match.");
michael@0 535 runTest();
michael@0 536 });
michael@0 537 }
michael@0 538
michael@0 539 function promiseRaceEmpty() {
michael@0 540 var p = Promise.race([]);
michael@0 541 ok(p instanceof Promise, "Should return a Promise.");
michael@0 542 // An empty race never resolves!
michael@0 543 runTest();
michael@0 544 }
michael@0 545
michael@0 546 function promiseRaceValuesArray() {
michael@0 547 var p = Promise.race([true, new Date(), 3]);
michael@0 548 ok(p instanceof Promise, "Should return a Promise.");
michael@0 549 p.then(function(winner) {
michael@0 550 is(winner, true, "First value should win.");
michael@0 551 runTest();
michael@0 552 }, function(err) {
michael@0 553 ok(false, "Should not fail " + err + ".");
michael@0 554 runTest();
michael@0 555 });
michael@0 556 }
michael@0 557
michael@0 558 function promiseRacePromiseArray() {
michael@0 559 var arr = [
michael@0 560 new Promise(function(resolve) {
michael@0 561 resolve("first");
michael@0 562 }),
michael@0 563 Promise.resolve("second"),
michael@0 564 new Promise(function() {}),
michael@0 565 new Promise(function(resolve) {
michael@0 566 setTimeout(function() {
michael@0 567 setTimeout(function() {
michael@0 568 resolve("fourth");
michael@0 569 }, 0);
michael@0 570 }, 0);
michael@0 571 }),
michael@0 572 ];
michael@0 573
michael@0 574 var p = Promise.race(arr);
michael@0 575 p.then(function(winner) {
michael@0 576 is(winner, "first", "First queued resolution should win the race.");
michael@0 577 runTest();
michael@0 578 });
michael@0 579 }
michael@0 580
michael@0 581 function promiseRaceReject() {
michael@0 582 var p = Promise.race([
michael@0 583 Promise.reject(new Error("Fail bad!")),
michael@0 584 new Promise(function(resolve) {
michael@0 585 setTimeout(resolve, 0);
michael@0 586 })
michael@0 587 ]);
michael@0 588
michael@0 589 p.then(function() {
michael@0 590 ok(false, "Should not resolve when winning Promise rejected.");
michael@0 591 runTest();
michael@0 592 }, function(e) {
michael@0 593 ok(true, "Should be rejected");
michael@0 594 ok(e instanceof Error, "Should reject with Error.");
michael@0 595 ok(e.message == "Fail bad!", "Message should match.");
michael@0 596 runTest();
michael@0 597 });
michael@0 598 }
michael@0 599
michael@0 600 function promiseRaceThrow() {
michael@0 601 var p = Promise.race([
michael@0 602 new Promise(function(resolve) {
michael@0 603 nonExistent();
michael@0 604 }),
michael@0 605 new Promise(function(resolve) {
michael@0 606 setTimeout(resolve, 0);
michael@0 607 })
michael@0 608 ]);
michael@0 609
michael@0 610 p.then(function() {
michael@0 611 ok(false, "Should not resolve when winning Promise had an error.");
michael@0 612 runTest();
michael@0 613 }, function(e) {
michael@0 614 ok(true, "Should be rejected");
michael@0 615 ok(e instanceof ReferenceError, "Should reject with ReferenceError for function nonExistent().");
michael@0 616 runTest();
michael@0 617 });
michael@0 618 }
michael@0 619
michael@0 620 function promiseResolveArray() {
michael@0 621 var p = Promise.resolve([1,2,3]);
michael@0 622 ok(p instanceof Promise, "Should return a Promise.");
michael@0 623 p.then(function(v) {
michael@0 624 ok(Array.isArray(v), "Resolved value should be an Array");
michael@0 625 is(v.length, 3, "Length should match");
michael@0 626 is(v[0], 1, "Resolved value should match original");
michael@0 627 is(v[1], 2, "Resolved value should match original");
michael@0 628 is(v[2], 3, "Resolved value should match original");
michael@0 629 runTest();
michael@0 630 });
michael@0 631 }
michael@0 632
michael@0 633 function promiseResolveThenable() {
michael@0 634 var p = Promise.resolve({ then: function(onFulfill, onReject) { onFulfill(2); } });
michael@0 635 ok(p instanceof Promise, "Should cast to a Promise.");
michael@0 636 p.then(function(v) {
michael@0 637 is(v, 2, "Should resolve to 2.");
michael@0 638 runTest();
michael@0 639 }, function(e) {
michael@0 640 ok(false, "promiseResolveThenable should've resolved");
michael@0 641 runTest();
michael@0 642 });
michael@0 643 }
michael@0 644
michael@0 645 function promiseResolvePromise() {
michael@0 646 var original = Promise.resolve(true);
michael@0 647 var cast = Promise.resolve(original);
michael@0 648
michael@0 649 ok(cast instanceof Promise, "Should cast to a Promise.");
michael@0 650 is(cast, original, "Should return original Promise.");
michael@0 651 cast.then(function(v) {
michael@0 652 is(v, true, "Should resolve to true.");
michael@0 653 runTest();
michael@0 654 });
michael@0 655 }
michael@0 656
michael@0 657 var tests = [
michael@0 658 promiseResolve,
michael@0 659 promiseReject,
michael@0 660 promiseException,
michael@0 661 promiseAsync,
michael@0 662 promiseDoubleThen,
michael@0 663 promiseThenException,
michael@0 664 promiseThenCatchThen,
michael@0 665 promiseRejectThenCatchThen,
michael@0 666 promiseRejectThenCatchThen2,
michael@0 667 promiseRejectThenCatchExceptionThen,
michael@0 668 promiseThenCatchOrderingResolve,
michael@0 669 promiseThenCatchOrderingReject,
michael@0 670 promiseNestedPromise,
michael@0 671 promiseNestedNestedPromise,
michael@0 672 promiseWrongNestedPromise,
michael@0 673 promiseLoop,
michael@0 674 promiseStaticReject,
michael@0 675 promiseStaticResolve,
michael@0 676 promiseResolveNestedPromise,
michael@0 677 promiseResolveNoArg,
michael@0 678 promiseRejectNoArg,
michael@0 679
michael@0 680 promiseThenNoArg,
michael@0 681 promiseThenUndefinedResolveFunction,
michael@0 682 promiseThenNullResolveFunction,
michael@0 683 promiseCatchNoArg,
michael@0 684 promiseRejectNoHandler,
michael@0 685
michael@0 686 promiseUtilitiesDefined,
michael@0 687
michael@0 688 promiseAllArray,
michael@0 689 promiseAllWaitsForAllPromises,
michael@0 690 promiseAllRejectFails,
michael@0 691
michael@0 692 promiseRaceEmpty,
michael@0 693 promiseRaceValuesArray,
michael@0 694 promiseRacePromiseArray,
michael@0 695 promiseRaceReject,
michael@0 696 promiseRaceThrow,
michael@0 697
michael@0 698 promiseResolveArray,
michael@0 699 promiseResolveThenable,
michael@0 700 promiseResolvePromise,
michael@0 701 ];
michael@0 702
michael@0 703 function runTest() {
michael@0 704 if (!tests.length) {
michael@0 705 postMessage({ type: 'finish' });
michael@0 706 return;
michael@0 707 }
michael@0 708
michael@0 709 var test = tests.shift();
michael@0 710 test();
michael@0 711 }
michael@0 712
michael@0 713 onmessage = function() {
michael@0 714 runTest();
michael@0 715 }

mercurial