dom/promise/tests/test_promise_utils.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/promise/tests/test_promise_utils.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,319 @@
     1.4 +<!--
     1.5 +  Any copyright is dedicated to the Public Domain.
     1.6 +  http://creativecommons.org/publicdomain/zero/1.0/
     1.7 +-->
     1.8 +<html>
     1.9 +<head>
    1.10 +  <title>Test for Promise.all, Promise.race</title>
    1.11 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.13 +</head>
    1.14 +<body>
    1.15 +<p id="display"></p>
    1.16 +<div id="content" style="display: none">
    1.17 +
    1.18 +</div>
    1.19 +<pre id="test">
    1.20 +<script type="application/javascript"><!--
    1.21 +
    1.22 +function promiseUtilitiesDefined() {
    1.23 +  ok(Promise.all, "Promise.all must be defined when Promise is enabled.");
    1.24 +  ok(Promise.race, "Promise.race must be defined when Promise is enabled.");
    1.25 +  runTest();
    1.26 +}
    1.27 +
    1.28 +function promiseAllEmptyArray() {
    1.29 +  var p = Promise.all([]);
    1.30 +  ok(p instanceof Promise, "Return value of Promise.all should be a Promise.");
    1.31 +  p.then(function(values) {
    1.32 +    ok(Array.isArray(values), "Resolved value should be an array.");
    1.33 +    is(values.length, 0, "Resolved array length should match iterable's length.");
    1.34 +    runTest();
    1.35 +  }, function() {
    1.36 +    ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
    1.37 +    runTest();
    1.38 +  });
    1.39 +}
    1.40 +
    1.41 +function promiseAllArray() {
    1.42 +  var p = Promise.all([1, new Date(), Promise.resolve("firefox")]);
    1.43 +  ok(p instanceof Promise, "Return value of Promise.all should be a Promise.");
    1.44 +  p.then(function(values) {
    1.45 +    ok(Array.isArray(values), "Resolved value should be an array.");
    1.46 +    is(values.length, 3, "Resolved array length should match iterable's length.");
    1.47 +    is(values[0], 1, "Array values should match.");
    1.48 +    ok(values[1] instanceof Date, "Array values should match.");
    1.49 +    is(values[2], "firefox", "Array values should match.");
    1.50 +    runTest();
    1.51 +  }, function() {
    1.52 +    ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
    1.53 +    runTest();
    1.54 +  });
    1.55 +}
    1.56 +
    1.57 +function promiseAllIterable() {
    1.58 +  function* promiseGen() {
    1.59 +    var i = 3;
    1.60 +    while (--i) {
    1.61 +      yield Promise.resolve(i);
    1.62 +    }
    1.63 +
    1.64 +    yield new Promise(function(resolve) {
    1.65 +      setTimeout(resolve, 10);
    1.66 +    });
    1.67 +  }
    1.68 +
    1.69 +  Promise.all(promiseGen()).then(function(values) {
    1.70 +    is(values.length, 3, "Resolved array length should match iterable's length.");
    1.71 +    is(values[0], 2, "Array values should match.");
    1.72 +    is(values[1], 1, "Array values should match.");
    1.73 +    is(values[2], undefined, "Array values should match.");
    1.74 +    runTest();
    1.75 +  }, function(e) {
    1.76 +    ok(false, "Promise.all shouldn't fail when an iterable is passed.");
    1.77 +    runTest();
    1.78 +  });
    1.79 +}
    1.80 +
    1.81 +function promiseAllWaitsForAllPromises() {
    1.82 +  var arr = [
    1.83 +    new Promise(function(resolve) {
    1.84 +      setTimeout(resolve.bind(undefined, 1), 50);
    1.85 +    }),
    1.86 +    new Promise(function(resolve) {
    1.87 +      setTimeout(resolve.bind(undefined, 2), 10);
    1.88 +    }),
    1.89 +    new Promise(function(resolve) {
    1.90 +      setTimeout(resolve.bind(undefined, new Promise(function(resolve2) {
    1.91 +        resolve2(3);
    1.92 +      })), 10);
    1.93 +    }),
    1.94 +    new Promise(function(resolve) {
    1.95 +      setTimeout(resolve.bind(undefined, 4), 20);
    1.96 +    })
    1.97 +  ];
    1.98 +
    1.99 +  var p = Promise.all(arr);
   1.100 +  p.then(function(values) {
   1.101 +    ok(Array.isArray(values), "Resolved value should be an array.");
   1.102 +    is(values.length, 4, "Resolved array length should match iterable's length.");
   1.103 +    is(values[0], 1, "Array values should match.");
   1.104 +    is(values[1], 2, "Array values should match.");
   1.105 +    is(values[2], 3, "Array values should match.");
   1.106 +    is(values[3], 4, "Array values should match.");
   1.107 +    runTest();
   1.108 +  }, function() {
   1.109 +    ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
   1.110 +    runTest();
   1.111 +  });
   1.112 +}
   1.113 +
   1.114 +function promiseAllRejectFails() {
   1.115 +  var arr = [
   1.116 +    new Promise(function(resolve) {
   1.117 +      setTimeout(resolve.bind(undefined, 1), 50);
   1.118 +    }),
   1.119 +    new Promise(function(resolve, reject) {
   1.120 +      setTimeout(reject.bind(undefined, 2), 10);
   1.121 +    }),
   1.122 +    new Promise(function(resolve) {
   1.123 +      setTimeout(resolve.bind(undefined, 3), 10);
   1.124 +    }),
   1.125 +    new Promise(function(resolve) {
   1.126 +      setTimeout(resolve.bind(undefined, 4), 20);
   1.127 +    })
   1.128 +  ];
   1.129 +
   1.130 +  var p = Promise.all(arr);
   1.131 +  p.then(function(values) {
   1.132 +    ok(false, "Promise.all shouldn't resolve when iterable has rejected Promises.");
   1.133 +    runTest();
   1.134 +  }, function(e) {
   1.135 +    ok(true, "Promise.all should reject when iterable has rejected Promises.");
   1.136 +    is(e, 2, "Rejection value should match.");
   1.137 +    runTest();
   1.138 +  });
   1.139 +}
   1.140 +
   1.141 +function promiseAllCastError() {
   1.142 +  var p = Promise.all([Promise.resolve(2), { then: function() { foo(); } }]);
   1.143 +  ok(p instanceof Promise, "Should cast to a Promise.");
   1.144 +  p.then(function(v) {
   1.145 +    ok(false, "promiseAllCastError: should've rejected.");
   1.146 +    runTest();
   1.147 +  }, function(e) {
   1.148 +    ok(e instanceof ReferenceError, "promiseCastThenableError");
   1.149 +    runTest();
   1.150 +  });
   1.151 +}
   1.152 +
   1.153 +// Check that the resolved array is enumerable.
   1.154 +function promiseAllEnumerable() {
   1.155 +  var p = Promise.all([1, new Date(), Promise.resolve("firefox")]);
   1.156 +  p.then(function(v) {
   1.157 +    var count = 0;
   1.158 +    for (key in v) {
   1.159 +      ++count;
   1.160 +      ok(v[key] === 1 || v[key] instanceof Date || v[key] === "firefox",
   1.161 +         "Enumerated properties don't match.");
   1.162 +    }
   1.163 +    is(count, 3, "Resolved array from Promise.all should be enumerable");
   1.164 +    runTest();
   1.165 +  }, function(e) {
   1.166 +    ok(false, "promiseAllEnumerable: should've resolved.");
   1.167 +    runTest();
   1.168 +  });
   1.169 +}
   1.170 +
   1.171 +function promiseRaceEmpty() {
   1.172 +  var p = Promise.race([]);
   1.173 +  ok(p instanceof Promise, "Should return a Promise.");
   1.174 +  p.then(function() {
   1.175 +    ok(false, "Should not resolve");
   1.176 +  }, function() {
   1.177 +    ok(false, "Should not reject");
   1.178 +  });
   1.179 +  // Per spec, An empty race never resolves or rejects.
   1.180 +  setTimeout(function() {
   1.181 +    ok(true);
   1.182 +    runTest();
   1.183 +  }, 50);
   1.184 +}
   1.185 +
   1.186 +function promiseRaceValuesArray() {
   1.187 +  var p = Promise.race([true, new Date(), 3]);
   1.188 +  ok(p instanceof Promise, "Should return a Promise.");
   1.189 +  p.then(function(winner) {
   1.190 +    is(winner, true, "First value should win.");
   1.191 +    runTest();
   1.192 +  }, function(err) {
   1.193 +    ok(false, "Should not fail " + err + ".");
   1.194 +    runTest();
   1.195 +  });
   1.196 +}
   1.197 +
   1.198 +function promiseRacePromiseArray() {
   1.199 +  function timeoutPromise(n) {
   1.200 +    return new Promise(function(resolve) {
   1.201 +      setTimeout(function() {
   1.202 +        resolve(n);
   1.203 +      }, n);
   1.204 +    });
   1.205 +  }
   1.206 +
   1.207 +  var arr = [
   1.208 +    new Promise(function(resolve) {
   1.209 +      resolve("first");
   1.210 +    }),
   1.211 +    Promise.resolve("second"),
   1.212 +    new Promise(function() {}),
   1.213 +    new Promise(function(resolve) {
   1.214 +      setTimeout(function() {
   1.215 +        setTimeout(function() {
   1.216 +          resolve("fourth");
   1.217 +        }, 0);
   1.218 +      }, 0);
   1.219 +    }),
   1.220 +  ];
   1.221 +
   1.222 +  var p = Promise.race(arr);
   1.223 +  p.then(function(winner) {
   1.224 +    is(winner, "first", "First queued resolution should win the race.");
   1.225 +    runTest();
   1.226 +  });
   1.227 +}
   1.228 +
   1.229 +function promiseRaceIterable() {
   1.230 +  function* participants() {
   1.231 +    yield new Promise(function(resolve) {
   1.232 +      setTimeout(resolve, 10, 10);
   1.233 +    });
   1.234 +    yield new Promise(function(resolve) {
   1.235 +      setTimeout(resolve, 20, 20);
   1.236 +    });
   1.237 +  }
   1.238 +
   1.239 +  Promise.race(participants()).then(function(winner) {
   1.240 +    is(winner, 10, "Winner should be the one that finished earlier.");
   1.241 +    runTest();
   1.242 +  }, function(e) {
   1.243 +    ok(false, "Promise.race shouldn't throw when an iterable is passed!");
   1.244 +    runTest();
   1.245 +  });
   1.246 +}
   1.247 +
   1.248 +function promiseRaceReject() {
   1.249 +  var p = Promise.race([
   1.250 +    Promise.reject(new Error("Fail bad!")),
   1.251 +    new Promise(function(resolve) {
   1.252 +      setTimeout(resolve, 0);
   1.253 +    })
   1.254 +  ]);
   1.255 +
   1.256 +  p.then(function() {
   1.257 +    ok(false, "Should not resolve when winning Promise rejected.");
   1.258 +    runTest();
   1.259 +  }, function(e) {
   1.260 +    ok(true, "Should be rejected");
   1.261 +    ok(e instanceof Error, "Should reject with Error.");
   1.262 +    ok(e.message == "Fail bad!", "Message should match.");
   1.263 +    runTest();
   1.264 +  });
   1.265 +}
   1.266 +
   1.267 +function promiseRaceThrow() {
   1.268 +  var p = Promise.race([
   1.269 +    new Promise(function(resolve) {
   1.270 +      nonExistent();
   1.271 +    }),
   1.272 +    new Promise(function(resolve) {
   1.273 +      setTimeout(resolve, 0);
   1.274 +    })
   1.275 +  ]);
   1.276 +
   1.277 +  p.then(function() {
   1.278 +    ok(false, "Should not resolve when winning Promise had an error.");
   1.279 +    runTest();
   1.280 +  }, function(e) {
   1.281 +    ok(true, "Should be rejected");
   1.282 +    ok(e instanceof ReferenceError, "Should reject with ReferenceError for function nonExistent().");
   1.283 +    runTest();
   1.284 +  });
   1.285 +}
   1.286 +
   1.287 +var tests = [
   1.288 +              promiseUtilitiesDefined,
   1.289 +              promiseAllEmptyArray,
   1.290 +              promiseAllArray,
   1.291 +              promiseAllIterable,
   1.292 +              promiseAllWaitsForAllPromises,
   1.293 +              promiseAllRejectFails,
   1.294 +              promiseAllCastError,
   1.295 +              promiseAllEnumerable,
   1.296 +
   1.297 +              promiseRaceEmpty,
   1.298 +              promiseRaceValuesArray,
   1.299 +              promiseRacePromiseArray,
   1.300 +              promiseRaceIterable,
   1.301 +              promiseRaceReject,
   1.302 +              promiseRaceThrow,
   1.303 +            ];
   1.304 +
   1.305 +function runTest() {
   1.306 +  if (!tests.length) {
   1.307 +    SimpleTest.finish();
   1.308 +    return;
   1.309 +  }
   1.310 +
   1.311 +  var test = tests.shift();
   1.312 +  test();
   1.313 +}
   1.314 +
   1.315 +SimpleTest.waitForExplicitFinish();
   1.316 +runTest();
   1.317 +// -->
   1.318 +</script>
   1.319 +</pre>
   1.320 +</body>
   1.321 +</html>
   1.322 +

mercurial