dom/workers/test/promise_worker.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/test/promise_worker.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,715 @@
     1.4 +function ok(a, msg) {
     1.5 +  dump("OK: " + !!a + "  =>  " + a + " " + msg + "\n");
     1.6 +  postMessage({type: 'status', status: !!a, msg: a + ": " + msg });
     1.7 +}
     1.8 +
     1.9 +function is(a, b, msg) {
    1.10 +  dump("IS: " + (a===b) + "  =>  " + a + " | " + b + " " + msg + "\n");
    1.11 +  postMessage({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg });
    1.12 +}
    1.13 +
    1.14 +function isnot(a, b, msg) {
    1.15 +  dump("ISNOT: " + (a!==b) + "  =>  " + a + " | " + b + " " + msg + "\n");
    1.16 +  postMessage({type: 'status', status: a !== b, msg: a + " !== " + b + ": " + msg });
    1.17 +}
    1.18 +
    1.19 +function promiseResolve() {
    1.20 +  ok(Promise, "Promise object should exist");
    1.21 +
    1.22 +  var promise = new Promise(function(resolve, reject) {
    1.23 +    ok(resolve, "Promise.resolve exists");
    1.24 +    ok(reject, "Promise.reject exists");
    1.25 +
    1.26 +    resolve(42);
    1.27 +  }).then(function(what) {
    1.28 +    ok(true, "Then - resolveCb has been called");
    1.29 +    is(what, 42, "ResolveCb received 42");
    1.30 +    runTest();
    1.31 +  }, function() {
    1.32 +    ok(false, "Then - rejectCb has been called");
    1.33 +    runTest();
    1.34 +  });
    1.35 +}
    1.36 +
    1.37 +function promiseResolveNoArg() {
    1.38 +  var promise = new Promise(function(resolve, reject) {
    1.39 +    ok(resolve, "Promise.resolve exists");
    1.40 +    ok(reject, "Promise.reject exists");
    1.41 +
    1.42 +    resolve();
    1.43 +  }).then(function(what) {
    1.44 +    ok(true, "Then - resolveCb has been called");
    1.45 +    is(what, undefined, "ResolveCb received undefined");
    1.46 +    runTest();
    1.47 +  }, function() {
    1.48 +    ok(false, "Then - rejectCb has been called");
    1.49 +    runTest();
    1.50 +  });
    1.51 +}
    1.52 +
    1.53 +function promiseRejectNoHandler() {
    1.54 +  // This test only checks that the code that reports unhandled errors in the
    1.55 +  // Promises implementation does not crash or leak.
    1.56 +  var promise = new Promise(function(res, rej) {
    1.57 +    noSuchMethod();
    1.58 +  });
    1.59 +  runTest();
    1.60 +}
    1.61 +
    1.62 +function promiseReject() {
    1.63 +  var promise = new Promise(function(resolve, reject) {
    1.64 +    reject(42);
    1.65 +  }).then(function(what) {
    1.66 +    ok(false, "Then - resolveCb has been called");
    1.67 +    runTest();
    1.68 +  }, function(what) {
    1.69 +    ok(true, "Then - rejectCb has been called");
    1.70 +    is(what, 42, "RejectCb received 42");
    1.71 +    runTest();
    1.72 +  });
    1.73 +}
    1.74 +
    1.75 +function promiseRejectNoArg() {
    1.76 +  var promise = new Promise(function(resolve, reject) {
    1.77 +    reject();
    1.78 +  }).then(function(what) {
    1.79 +    ok(false, "Then - resolveCb has been called");
    1.80 +    runTest();
    1.81 +  }, function(what) {
    1.82 +    ok(true, "Then - rejectCb has been called");
    1.83 +    is(what, undefined, "RejectCb received undefined");
    1.84 +    runTest();
    1.85 +  });
    1.86 +}
    1.87 +
    1.88 +function promiseException() {
    1.89 +  var promise = new Promise(function(resolve, reject) {
    1.90 +    throw 42;
    1.91 +  }).then(function(what) {
    1.92 +    ok(false, "Then - resolveCb has been called");
    1.93 +    runTest();
    1.94 +  }, function(what) {
    1.95 +    ok(true, "Then - rejectCb has been called");
    1.96 +    is(what, 42, "RejectCb received 42");
    1.97 +    runTest();
    1.98 +  });
    1.99 +}
   1.100 +
   1.101 +function promiseAsync() {
   1.102 +  var global = "foo";
   1.103 +  var f = new Promise(function(r1, r2) {
   1.104 +    is(global, "foo", "Global should be foo");
   1.105 +    r1(42);
   1.106 +    is(global, "foo", "Global should still be foo");
   1.107 +    setTimeout(function() {
   1.108 +      is(global, "bar", "Global should still be bar!");
   1.109 +      runTest();
   1.110 +    }, 0);
   1.111 +  }).then(function() {
   1.112 +    global = "bar";
   1.113 +  });
   1.114 +  is(global, "foo", "Global should still be foo (2)");
   1.115 +}
   1.116 +
   1.117 +function promiseDoubleThen() {
   1.118 +  var steps = 0;
   1.119 +  var promise = new Promise(function(r1, r2) {
   1.120 +    r1(42);
   1.121 +  });
   1.122 +
   1.123 +  promise.then(function(what) {
   1.124 +    ok(true, "Then.resolve has been called");
   1.125 +    is(what, 42, "Value == 42");
   1.126 +    steps++;
   1.127 +  }, function(what) {
   1.128 +    ok(false, "Then.reject has been called");
   1.129 +  });
   1.130 +
   1.131 +  promise.then(function(what) {
   1.132 +    ok(true, "Then.resolve has been called");
   1.133 +    is(steps, 1, "Then.resolve - step == 1");
   1.134 +    is(what, 42, "Value == 42");
   1.135 +    runTest();
   1.136 +  }, function(what) {
   1.137 +    ok(false, "Then.reject has been called");
   1.138 +  });
   1.139 +}
   1.140 +
   1.141 +function promiseThenException() {
   1.142 +  var promise = new Promise(function(resolve, reject) {
   1.143 +    resolve(42);
   1.144 +  });
   1.145 +
   1.146 +  promise.then(function(what) {
   1.147 +    ok(true, "Then.resolve has been called");
   1.148 +    throw "booh";
   1.149 +  }).catch(function(e) {
   1.150 +    ok(true, "Catch has been called!");
   1.151 +    runTest();
   1.152 +  });
   1.153 +}
   1.154 +
   1.155 +function promiseThenCatchThen() {
   1.156 +  var promise = new Promise(function(resolve, reject) {
   1.157 +    resolve(42);
   1.158 +  });
   1.159 +
   1.160 +  var promise2 = promise.then(function(what) {
   1.161 +    ok(true, "Then.resolve has been called");
   1.162 +    is(what, 42, "Value == 42");
   1.163 +    return what + 1;
   1.164 +  }, function(what) {
   1.165 +    ok(false, "Then.reject has been called");
   1.166 +  });
   1.167 +
   1.168 +  isnot(promise, promise2, "These 2 promise objs are different");
   1.169 +
   1.170 +  promise2.then(function(what) {
   1.171 +    ok(true, "Then.resolve has been called");
   1.172 +    is(what, 43, "Value == 43");
   1.173 +    return what + 1;
   1.174 +  }, function(what) {
   1.175 +    ok(false, "Then.reject has been called");
   1.176 +  }).catch(function() {
   1.177 +    ok(false, "Catch has been called");
   1.178 +  }).then(function(what) {
   1.179 +    ok(true, "Then.resolve has been called");
   1.180 +    is(what, 44, "Value == 44");
   1.181 +    runTest();
   1.182 +  }, function(what) {
   1.183 +    ok(false, "Then.reject has been called");
   1.184 +  });
   1.185 +}
   1.186 +
   1.187 +function promiseRejectThenCatchThen() {
   1.188 +  var promise = new Promise(function(resolve, reject) {
   1.189 +    reject(42);
   1.190 +  });
   1.191 +
   1.192 +  var promise2 = promise.then(function(what) {
   1.193 +    ok(false, "Then.resolve has been called");
   1.194 +  }, function(what) {
   1.195 +    ok(true, "Then.reject has been called");
   1.196 +    is(what, 42, "Value == 42");
   1.197 +    return what + 1;
   1.198 +  });
   1.199 +
   1.200 +  isnot(promise, promise2, "These 2 promise objs are different");
   1.201 +
   1.202 +  promise2.then(function(what) {
   1.203 +    ok(true, "Then.resolve has been called");
   1.204 +    is(what, 43, "Value == 43");
   1.205 +    return what+1;
   1.206 +  }).catch(function(what) {
   1.207 +    ok(false, "Catch has been called");
   1.208 +  }).then(function(what) {
   1.209 +    ok(true, "Then.resolve has been called");
   1.210 +    is(what, 44, "Value == 44");
   1.211 +    runTest();
   1.212 +  });
   1.213 +}
   1.214 +
   1.215 +function promiseRejectThenCatchThen2() {
   1.216 +  var promise = new Promise(function(resolve, reject) {
   1.217 +    reject(42);
   1.218 +  });
   1.219 +
   1.220 +  promise.then(function(what) {
   1.221 +    ok(true, "Then.resolve has been called");
   1.222 +    is(what, 42, "Value == 42");
   1.223 +    return what+1;
   1.224 +  }).catch(function(what) {
   1.225 +    is(what, 42, "Value == 42");
   1.226 +    ok(true, "Catch has been called");
   1.227 +    return what+1;
   1.228 +  }).then(function(what) {
   1.229 +    ok(true, "Then.resolve has been called");
   1.230 +    is(what, 43, "Value == 43");
   1.231 +    runTest();
   1.232 +  });
   1.233 +}
   1.234 +
   1.235 +function promiseRejectThenCatchExceptionThen() {
   1.236 +  var promise = new Promise(function(resolve, reject) {
   1.237 +    reject(42);
   1.238 +  });
   1.239 +
   1.240 +  promise.then(function(what) {
   1.241 +    ok(false, "Then.resolve has been called");
   1.242 +  }, function(what) {
   1.243 +    ok(true, "Then.reject has been called");
   1.244 +    is(what, 42, "Value == 42");
   1.245 +    throw(what + 1);
   1.246 +  }).catch(function(what) {
   1.247 +    ok(true, "Catch has been called");
   1.248 +    is(what, 43, "Value == 43");
   1.249 +    return what + 1;
   1.250 +  }).then(function(what) {
   1.251 +    ok(true, "Then.resolve has been called");
   1.252 +    is(what, 44, "Value == 44");
   1.253 +    runTest();
   1.254 +  });
   1.255 +}
   1.256 +
   1.257 +function promiseThenCatchOrderingResolve() {
   1.258 +  var global = 0;
   1.259 +  var f = new Promise(function(r1, r2) {
   1.260 +    r1(42);
   1.261 +  });
   1.262 +
   1.263 +  f.then(function() {
   1.264 +    f.then(function() {
   1.265 +      global++;
   1.266 +    });
   1.267 +    f.catch(function() {
   1.268 +      global++;
   1.269 +    });
   1.270 +    f.then(function() {
   1.271 +      global++;
   1.272 +    });
   1.273 +    setTimeout(function() {
   1.274 +      is(global, 2, "Many steps... should return 2");
   1.275 +      runTest();
   1.276 +    }, 0);
   1.277 +  });
   1.278 +}
   1.279 +
   1.280 +function promiseThenCatchOrderingReject() {
   1.281 +  var global = 0;
   1.282 +  var f = new Promise(function(r1, r2) {
   1.283 +    r2(42);
   1.284 +  })
   1.285 +
   1.286 +  f.then(function() {}, function() {
   1.287 +    f.then(function() {
   1.288 +      global++;
   1.289 +    });
   1.290 +    f.catch(function() {
   1.291 +      global++;
   1.292 +    });
   1.293 +    f.then(function() {}, function() {
   1.294 +      global++;
   1.295 +    });
   1.296 +    setTimeout(function() {
   1.297 +      is(global, 2, "Many steps... should return 2");
   1.298 +      runTest();
   1.299 +    }, 0);
   1.300 +  });
   1.301 +}
   1.302 +
   1.303 +function promiseThenNoArg() {
   1.304 +  var promise = new Promise(function(resolve, reject) {
   1.305 +    resolve(42);
   1.306 +  });
   1.307 +
   1.308 +  var clone = promise.then();
   1.309 +  isnot(promise, clone, "These 2 promise objs are different");
   1.310 +  promise.then(function(v) {
   1.311 +    clone.then(function(cv) {
   1.312 +      is(v, cv, "Both resolve to the same value");
   1.313 +      runTest();
   1.314 +    });
   1.315 +  });
   1.316 +}
   1.317 +
   1.318 +function promiseThenUndefinedResolveFunction() {
   1.319 +  var promise = new Promise(function(resolve, reject) {
   1.320 +    reject(42);
   1.321 +  });
   1.322 +
   1.323 +  try {
   1.324 +    promise.then(undefined, function(v) {
   1.325 +      is(v, 42, "Promise rejected with 42");
   1.326 +      runTest();
   1.327 +    });
   1.328 +  } catch (e) {
   1.329 +    ok(false, "then should not throw on undefined resolve function");
   1.330 +  }
   1.331 +}
   1.332 +
   1.333 +function promiseThenNullResolveFunction() {
   1.334 +  var promise = new Promise(function(resolve, reject) {
   1.335 +    reject(42);
   1.336 +  });
   1.337 +
   1.338 +  try {
   1.339 +    promise.then(null, function(v) {
   1.340 +      is(v, 42, "Promise rejected with 42");
   1.341 +      runTest();
   1.342 +    });
   1.343 +  } catch (e) {
   1.344 +    ok(false, "then should not throw on null resolve function");
   1.345 +  }
   1.346 +}
   1.347 +
   1.348 +function promiseCatchNoArg() {
   1.349 +  var promise = new Promise(function(resolve, reject) {
   1.350 +    reject(42);
   1.351 +  });
   1.352 +
   1.353 +  var clone = promise.catch();
   1.354 +  isnot(promise, clone, "These 2 promise objs are different");
   1.355 +  promise.catch(function(v) {
   1.356 +    clone.catch(function(cv) {
   1.357 +      is(v, cv, "Both reject to the same value");
   1.358 +      runTest();
   1.359 +    });
   1.360 +  });
   1.361 +}
   1.362 +
   1.363 +function promiseNestedPromise() {
   1.364 +  new Promise(function(resolve, reject) {
   1.365 +    resolve(new Promise(function(resolve, reject) {
   1.366 +      ok(true, "Nested promise is executed");
   1.367 +      resolve(42);
   1.368 +    }));
   1.369 +  }).then(function(value) {
   1.370 +    is(value, 42, "Nested promise is executed and then == 42");
   1.371 +    runTest();
   1.372 +  });
   1.373 +}
   1.374 +
   1.375 +function promiseNestedNestedPromise() {
   1.376 +  new Promise(function(resolve, reject) {
   1.377 +    resolve(new Promise(function(resolve, reject) {
   1.378 +      ok(true, "Nested promise is executed");
   1.379 +      resolve(42);
   1.380 +    }).then(function(what) { return what+1; }));
   1.381 +  }).then(function(value) {
   1.382 +    is(value, 43, "Nested promise is executed and then == 43");
   1.383 +    runTest();
   1.384 +  });
   1.385 +}
   1.386 +
   1.387 +function promiseWrongNestedPromise() {
   1.388 +  new Promise(function(resolve, reject) {
   1.389 +    resolve(new Promise(function(r, r2) {
   1.390 +      ok(true, "Nested promise is executed");
   1.391 +      r(42);
   1.392 +    }));
   1.393 +    reject(42);
   1.394 +  }).then(function(value) {
   1.395 +    is(value, 42, "Nested promise is executed and then == 42");
   1.396 +    runTest();
   1.397 +  }, function(value) {
   1.398 +     ok(false, "This is wrong");
   1.399 +  });
   1.400 +}
   1.401 +
   1.402 +function promiseLoop() {
   1.403 +  new Promise(function(resolve, reject) {
   1.404 +    resolve(new Promise(function(r1, r2) {
   1.405 +      ok(true, "Nested promise is executed");
   1.406 +      r1(new Promise(function(r1, r2) {
   1.407 +        ok(true, "Nested nested promise is executed");
   1.408 +        r1(42);
   1.409 +      }));
   1.410 +    }));
   1.411 +  }).then(function(value) {
   1.412 +    is(value, 42, "Nested nested promise is executed and then == 42");
   1.413 +    runTest();
   1.414 +  }, function(value) {
   1.415 +     ok(false, "This is wrong");
   1.416 +  });
   1.417 +}
   1.418 +
   1.419 +function promiseStaticReject() {
   1.420 +  var promise = Promise.reject(42).then(function(what) {
   1.421 +    ok(false, "This should not be called");
   1.422 +  }, function(what) {
   1.423 +    is(what, 42, "Value == 42");
   1.424 +    runTest();
   1.425 +  });
   1.426 +}
   1.427 +
   1.428 +function promiseStaticResolve() {
   1.429 +  var promise = Promise.resolve(42).then(function(what) {
   1.430 +    is(what, 42, "Value == 42");
   1.431 +    runTest();
   1.432 +  }, function() {
   1.433 +    ok(false, "This should not be called");
   1.434 +  });
   1.435 +}
   1.436 +
   1.437 +function promiseResolveNestedPromise() {
   1.438 +  var promise = Promise.resolve(new Promise(function(r, r2) {
   1.439 +    ok(true, "Nested promise is executed");
   1.440 +    r(42);
   1.441 +  }, function() {
   1.442 +    ok(false, "This should not be called");
   1.443 +  })).then(function(what) {
   1.444 +    is(what, 42, "Value == 42");
   1.445 +    runTest();
   1.446 +  }, function() {
   1.447 +    ok(false, "This should not be called");
   1.448 +  });
   1.449 +}
   1.450 +
   1.451 +function promiseRejectNoHandler() {
   1.452 +  // This test only checks that the code that reports unhandled errors in the
   1.453 +  // Promises implementation does not crash or leak.
   1.454 +  var promise = new Promise(function(res, rej) {
   1.455 +    noSuchMethod();
   1.456 +  });
   1.457 +  runTest();
   1.458 +}
   1.459 +
   1.460 +function promiseUtilitiesDefined() {
   1.461 +  ok(Promise.all, "Promise.all must be defined when Promise is enabled.");
   1.462 +  ok(Promise.race, "Promise.race must be defined when Promise is enabled.");
   1.463 +  runTest();
   1.464 +}
   1.465 +
   1.466 +function promiseAllArray() {
   1.467 +  var p = Promise.all([1, new Date(), Promise.resolve("firefox")]);
   1.468 +  ok(p instanceof Promise, "Return value of Promise.all should be a Promise.");
   1.469 +  p.then(function(values) {
   1.470 +    ok(Array.isArray(values), "Resolved value should be an array.");
   1.471 +    is(values.length, 3, "Resolved array length should match iterable's length.");
   1.472 +    is(values[0], 1, "Array values should match.");
   1.473 +    ok(values[1] instanceof Date, "Array values should match.");
   1.474 +    is(values[2], "firefox", "Array values should match.");
   1.475 +    runTest();
   1.476 +  }, function() {
   1.477 +    ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
   1.478 +    runTest();
   1.479 +  });
   1.480 +}
   1.481 +
   1.482 +function promiseAllWaitsForAllPromises() {
   1.483 +  var arr = [
   1.484 +    new Promise(function(resolve) {
   1.485 +      setTimeout(resolve.bind(undefined, 1), 50);
   1.486 +    }),
   1.487 +    new Promise(function(resolve) {
   1.488 +      setTimeout(resolve.bind(undefined, 2), 10);
   1.489 +    }),
   1.490 +    new Promise(function(resolve) {
   1.491 +      setTimeout(resolve.bind(undefined, new Promise(function(resolve2) {
   1.492 +        resolve2(3);
   1.493 +      })), 10);
   1.494 +    }),
   1.495 +    new Promise(function(resolve) {
   1.496 +      setTimeout(resolve.bind(undefined, 4), 20);
   1.497 +    })
   1.498 +  ];
   1.499 +
   1.500 +  var p = Promise.all(arr);
   1.501 +  p.then(function(values) {
   1.502 +    ok(Array.isArray(values), "Resolved value should be an array.");
   1.503 +    is(values.length, 4, "Resolved array length should match iterable's length.");
   1.504 +    is(values[0], 1, "Array values should match.");
   1.505 +    is(values[1], 2, "Array values should match.");
   1.506 +    is(values[2], 3, "Array values should match.");
   1.507 +    is(values[3], 4, "Array values should match.");
   1.508 +    runTest();
   1.509 +  }, function() {
   1.510 +    ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
   1.511 +    runTest();
   1.512 +  });
   1.513 +}
   1.514 +
   1.515 +function promiseAllRejectFails() {
   1.516 +  var arr = [
   1.517 +    new Promise(function(resolve) {
   1.518 +      setTimeout(resolve.bind(undefined, 1), 50);
   1.519 +    }),
   1.520 +    new Promise(function(resolve, reject) {
   1.521 +      setTimeout(reject.bind(undefined, 2), 10);
   1.522 +    }),
   1.523 +    new Promise(function(resolve) {
   1.524 +      setTimeout(resolve.bind(undefined, 3), 10);
   1.525 +    }),
   1.526 +    new Promise(function(resolve) {
   1.527 +      setTimeout(resolve.bind(undefined, 4), 20);
   1.528 +    })
   1.529 +  ];
   1.530 +
   1.531 +  var p = Promise.all(arr);
   1.532 +  p.then(function(values) {
   1.533 +    ok(false, "Promise.all shouldn't resolve when iterable has rejected Promises.");
   1.534 +    runTest();
   1.535 +  }, function(e) {
   1.536 +    ok(true, "Promise.all should reject when iterable has rejected Promises.");
   1.537 +    is(e, 2, "Rejection value should match.");
   1.538 +    runTest();
   1.539 +  });
   1.540 +}
   1.541 +
   1.542 +function promiseRaceEmpty() {
   1.543 +  var p = Promise.race([]);
   1.544 +  ok(p instanceof Promise, "Should return a Promise.");
   1.545 +  // An empty race never resolves!
   1.546 +  runTest();
   1.547 +}
   1.548 +
   1.549 +function promiseRaceValuesArray() {
   1.550 +  var p = Promise.race([true, new Date(), 3]);
   1.551 +  ok(p instanceof Promise, "Should return a Promise.");
   1.552 +  p.then(function(winner) {
   1.553 +    is(winner, true, "First value should win.");
   1.554 +    runTest();
   1.555 +  }, function(err) {
   1.556 +    ok(false, "Should not fail " + err + ".");
   1.557 +    runTest();
   1.558 +  });
   1.559 +}
   1.560 +
   1.561 +function promiseRacePromiseArray() {
   1.562 +  var arr = [
   1.563 +    new Promise(function(resolve) {
   1.564 +      resolve("first");
   1.565 +    }),
   1.566 +    Promise.resolve("second"),
   1.567 +    new Promise(function() {}),
   1.568 +    new Promise(function(resolve) {
   1.569 +      setTimeout(function() {
   1.570 +        setTimeout(function() {
   1.571 +          resolve("fourth");
   1.572 +        }, 0);
   1.573 +      }, 0);
   1.574 +    }),
   1.575 +  ];
   1.576 +
   1.577 +  var p = Promise.race(arr);
   1.578 +  p.then(function(winner) {
   1.579 +    is(winner, "first", "First queued resolution should win the race.");
   1.580 +    runTest();
   1.581 +  });
   1.582 +}
   1.583 +
   1.584 +function promiseRaceReject() {
   1.585 +  var p = Promise.race([
   1.586 +    Promise.reject(new Error("Fail bad!")),
   1.587 +    new Promise(function(resolve) {
   1.588 +      setTimeout(resolve, 0);
   1.589 +    })
   1.590 +  ]);
   1.591 +
   1.592 +  p.then(function() {
   1.593 +    ok(false, "Should not resolve when winning Promise rejected.");
   1.594 +    runTest();
   1.595 +  }, function(e) {
   1.596 +    ok(true, "Should be rejected");
   1.597 +    ok(e instanceof Error, "Should reject with Error.");
   1.598 +    ok(e.message == "Fail bad!", "Message should match.");
   1.599 +    runTest();
   1.600 +  });
   1.601 +}
   1.602 +
   1.603 +function promiseRaceThrow() {
   1.604 +  var p = Promise.race([
   1.605 +    new Promise(function(resolve) {
   1.606 +      nonExistent();
   1.607 +    }),
   1.608 +    new Promise(function(resolve) {
   1.609 +      setTimeout(resolve, 0);
   1.610 +    })
   1.611 +  ]);
   1.612 +
   1.613 +  p.then(function() {
   1.614 +    ok(false, "Should not resolve when winning Promise had an error.");
   1.615 +    runTest();
   1.616 +  }, function(e) {
   1.617 +    ok(true, "Should be rejected");
   1.618 +    ok(e instanceof ReferenceError, "Should reject with ReferenceError for function nonExistent().");
   1.619 +    runTest();
   1.620 +  });
   1.621 +}
   1.622 +
   1.623 +function promiseResolveArray() {
   1.624 +  var p = Promise.resolve([1,2,3]);
   1.625 +  ok(p instanceof Promise, "Should return a Promise.");
   1.626 +  p.then(function(v) {
   1.627 +    ok(Array.isArray(v), "Resolved value should be an Array");
   1.628 +    is(v.length, 3, "Length should match");
   1.629 +    is(v[0], 1, "Resolved value should match original");
   1.630 +    is(v[1], 2, "Resolved value should match original");
   1.631 +    is(v[2], 3, "Resolved value should match original");
   1.632 +    runTest();
   1.633 +  });
   1.634 +}
   1.635 +
   1.636 +function promiseResolveThenable() {
   1.637 +  var p = Promise.resolve({ then: function(onFulfill, onReject) { onFulfill(2); } });
   1.638 +  ok(p instanceof Promise, "Should cast to a Promise.");
   1.639 +  p.then(function(v) {
   1.640 +    is(v, 2, "Should resolve to 2.");
   1.641 +    runTest();
   1.642 +  }, function(e) {
   1.643 +    ok(false, "promiseResolveThenable should've resolved");
   1.644 +    runTest();
   1.645 +  });
   1.646 +}
   1.647 +
   1.648 +function promiseResolvePromise() {
   1.649 +  var original = Promise.resolve(true);
   1.650 +  var cast = Promise.resolve(original);
   1.651 +
   1.652 +  ok(cast instanceof Promise, "Should cast to a Promise.");
   1.653 +  is(cast, original, "Should return original Promise.");
   1.654 +  cast.then(function(v) {
   1.655 +    is(v, true, "Should resolve to true.");
   1.656 +    runTest();
   1.657 +  });
   1.658 +}
   1.659 +
   1.660 +var tests = [
   1.661 +    promiseResolve,
   1.662 +    promiseReject,
   1.663 +    promiseException,
   1.664 +    promiseAsync,
   1.665 +    promiseDoubleThen,
   1.666 +    promiseThenException,
   1.667 +    promiseThenCatchThen,
   1.668 +    promiseRejectThenCatchThen,
   1.669 +    promiseRejectThenCatchThen2,
   1.670 +    promiseRejectThenCatchExceptionThen,
   1.671 +    promiseThenCatchOrderingResolve,
   1.672 +    promiseThenCatchOrderingReject,
   1.673 +    promiseNestedPromise,
   1.674 +    promiseNestedNestedPromise,
   1.675 +    promiseWrongNestedPromise,
   1.676 +    promiseLoop,
   1.677 +    promiseStaticReject,
   1.678 +    promiseStaticResolve,
   1.679 +    promiseResolveNestedPromise,
   1.680 +    promiseResolveNoArg,
   1.681 +    promiseRejectNoArg,
   1.682 +
   1.683 +    promiseThenNoArg,
   1.684 +    promiseThenUndefinedResolveFunction,
   1.685 +    promiseThenNullResolveFunction,
   1.686 +    promiseCatchNoArg,
   1.687 +    promiseRejectNoHandler,
   1.688 +
   1.689 +    promiseUtilitiesDefined,
   1.690 +
   1.691 +    promiseAllArray,
   1.692 +    promiseAllWaitsForAllPromises,
   1.693 +    promiseAllRejectFails,
   1.694 +
   1.695 +    promiseRaceEmpty,
   1.696 +    promiseRaceValuesArray,
   1.697 +    promiseRacePromiseArray,
   1.698 +    promiseRaceReject,
   1.699 +    promiseRaceThrow,
   1.700 +
   1.701 +    promiseResolveArray,
   1.702 +    promiseResolveThenable,
   1.703 +    promiseResolvePromise,
   1.704 +];
   1.705 +
   1.706 +function runTest() {
   1.707 +  if (!tests.length) {
   1.708 +    postMessage({ type: 'finish' });
   1.709 +    return;
   1.710 +  }
   1.711 +
   1.712 +  var test = tests.shift();
   1.713 +  test();
   1.714 +}
   1.715 +
   1.716 +onmessage = function() {
   1.717 +  runTest();
   1.718 +}

mercurial