tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_promise_xrays.html (10195B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=1170760
      5 -->
      6 <head>
      7  <meta charset="utf-8">
      8  <title>Test for Bug 1170760</title>
      9  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
     11  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
     12 </head>
     13 <body>
     14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1170760">Mozilla Bug 1170760</a>
     15 <p id="display"></p>
     16 <div id="content" style="display: none">
     17 <iframe id="t" src="http://example.org/chrome/dom/promise/tests/file_promise_xrays.html"></iframe>
     18 </div>
     19 
     20 <pre id="test">
     21 <script type="application/javascript">
     22 
     23 var win = $("t").contentWindow;
     24 
     25 /** Test for Bug 1170760 */
     26 SimpleTest.waitForExplicitFinish();
     27 
     28 function testLoadComplete() {
     29  is(win.location.href, $("t").src, "Should have loaded the right thing");
     30  nextTest();
     31 }
     32 
     33 function testHaveXray() {
     34  is(typeof win.Promise.race, "function", "Should see a race() function");
     35  var exception;
     36  try {
     37    win.Promise.wrappedJSObject.race;
     38  } catch (e) {
     39    exception = e;
     40  }
     41  is(exception, "Getting race", "Should have thrown the right exception");
     42  is(win.wrappedJSObject.setupThrew, false, "Setup should not have thrown");
     43  nextTest();
     44 }
     45 
     46 function testConstructor1() {
     47  var p = new win.Promise(function(resolve) { resolve(win.Promise.resolve(5)); });
     48  p.then(
     49    function(arg) {
     50      is(arg, 5, "Content Promise constructor resolved with content promise should work");
     51    },
     52    function() {
     53      ok(false, "Content Promise constructor resolved with content promise should not fail");
     54    }
     55  ).then(nextTest);
     56 }
     57 
     58 function testConstructor2() {
     59  var p = new win.Promise(function(resolve) { resolve(Promise.resolve(5)); });
     60  p.then(
     61    function(arg) {
     62      is(arg, 5, "Content Promise constructor resolved with chrome promise should work");
     63    },
     64    function() {
     65      ok(false, "Content Promise constructor resolved with chrome promise should not fail");
     66    }
     67  ).then(nextTest);
     68 }
     69 
     70 function testRace1() {
     71  var p = win.Promise.race(new win.Array(1, 2));
     72  p.then(
     73    function(arg) {
     74      ok(arg == 1 || arg == 2,
     75         "Should get the right value when racing content-side array");
     76    },
     77    function(e) {
     78      ok(false, "testRace1 threw exception: " + e);
     79    }
     80  ).then(nextTest);
     81 }
     82 
     83 function testRace2() {
     84  var p = win.Promise.race(
     85    [win.Promise.resolve(1), win.Promise.resolve(2)]);
     86  p.then(
     87    function(arg) {
     88      ok(arg == 1 || arg == 2,
     89         "Should get the right value when racing content-side array of explicit Promises");
     90    },
     91    function(e) {
     92      ok(false, "testRace2 threw exception: " + e);
     93    }
     94  ).then(nextTest);
     95 }
     96 
     97 function testRace3() {
     98  // This works with a chrome-side array because we do the iteration
     99  // while still in the Xray compartment.
    100  var p = win.Promise.race([1, 2]);
    101  p.then(
    102    function(arg) {
    103      ok(arg == 1 || arg == 2,
    104         "Should get the right value when racing chrome-side array");
    105    },
    106    function(e) {
    107      ok(false, "testRace3 threw exception: " + e);
    108    }
    109  ).then(nextTest);
    110 }
    111 
    112 function testRace4() {
    113  // This works with both content-side and chrome-side Promises because we want
    114  // it to and go to some lengths to make it work.
    115  var p = win.Promise.race([Promise.resolve(1), win.Promise.resolve(2)]);
    116  p.then(
    117    function(arg) {
    118      ok(arg == 1 || arg == 2,
    119         "Should get the right value when racing chrome-side promises");
    120    },
    121    function(e) {
    122      ok(false, "testRace4 threw exception: " + e);
    123    }
    124  ).then(nextTest);
    125 }
    126 
    127 function testAll1() {
    128  var p = win.Promise.all(new win.Array(1, 2));
    129  p.then(
    130    function(arg) {
    131      ok(arg instanceof win.Array, "Should get an Array from Promise.all (1)");
    132      is(arg[0], 1, "First entry of Promise.all return value should be correct (1)");
    133      is(arg[1], 2, "Second entry of Promise.all return value should be correct (1)");
    134    },
    135    function(e) {
    136      ok(false, "testAll1 threw exception: " + e);
    137    }
    138  ).then(nextTest);
    139 }
    140 
    141 function testAll2() {
    142  var p = win.Promise.all(
    143    [win.Promise.resolve(1), win.Promise.resolve(2)]);
    144  p.then(
    145    function(arg) {
    146      ok(arg instanceof win.Array, "Should get an Array from Promise.all (2)");
    147      is(arg[0], 1, "First entry of Promise.all return value should be correct (2)");
    148      is(arg[1], 2, "Second entry of Promise.all return value should be correct (2)");
    149    },
    150    function(e) {
    151      ok(false, "testAll2 threw exception: " + e);
    152    }
    153  ).then(nextTest);
    154 }
    155 
    156 function testAll3() {
    157  // This works with a chrome-side array because we do the iteration
    158  // while still in the Xray compartment.
    159  var p = win.Promise.all([1, 2]);
    160  p.then(
    161    function(arg) {
    162      ok(arg instanceof win.Array, "Should get an Array from Promise.all (3)");
    163      is(arg[0], 1, "First entry of Promise.all return value should be correct (3)");
    164      is(arg[1], 2, "Second entry of Promise.all return value should be correct (3)");
    165    },
    166    function(e) {
    167      ok(false, "testAll3 threw exception: " + e);
    168    }
    169  ).then(nextTest);
    170 }
    171 
    172 function testAll4() {
    173  // This works with both content-side and chrome-side Promises because we want
    174  // it to and go to some lengths to make it work.
    175  var p = win.Promise.all([Promise.resolve(1), win.Promise.resolve(2)]);
    176  p.then(
    177    function(arg) {
    178      ok(arg instanceof win.Array, "Should get an Array from Promise.all (4)");
    179      is(arg[0], 1, "First entry of Promise.all return value should be correct (4)");
    180      is(arg[1], 2, "Second entry of Promise.all return value should be correct (4)");
    181    },
    182    function(e) {
    183      ok(false, "testAll4 threw exception: " + e);
    184    }
    185  ).then(nextTest);
    186 }
    187 
    188 function testAll5() {
    189  var p = win.Promise.all(new win.Array());
    190  p.then(
    191    function(arg) {
    192      ok(arg instanceof win.Array, "Should get an Array from Promise.all (5)");
    193    },
    194    function(e) {
    195      ok(false, "testAll5 threw exception: " + e);
    196    }
    197  ).then(nextTest);
    198 }
    199 
    200 function testResolve1() {
    201  var p = win.Promise.resolve(5);
    202  ok(p instanceof win.Promise, "Promise.resolve should return a promise");
    203  p.then(
    204    function(arg) {
    205      is(arg, 5, "Should get correct Promise.resolve value");
    206    },
    207    function(e) {
    208      ok(false, "testAll5 threw exception: " + e);
    209    }
    210  ).then(nextTest);
    211 }
    212 
    213 function testResolve2() {
    214  var p = win.Promise.resolve(5);
    215  var q = win.Promise.resolve(p);
    216  is(q, p, "Promise.resolve should just pass through Promise values");
    217  nextTest();
    218 }
    219 
    220 function testResolve3() {
    221  var p = win.Promise.resolve(Promise.resolve(5));
    222  p.then(
    223    function(arg) {
    224      is(arg, 5, "Promise.resolve with chrome Promise should work");
    225    },
    226    function() {
    227      ok(false, "Promise.resolve with chrome Promise should not fail");
    228    }
    229  ).then(nextTest);
    230 }
    231 
    232 function testResolve4() {
    233  var p = new win.Promise(() => {});
    234  Cu.getJSTestingFunctions().resolvePromise(p, 42);
    235  p.then(
    236    function(arg) {
    237      is(arg, 42, "Resolving an Xray to a promise with TestingFunctions resolvePromise should work");
    238    },
    239    function() {
    240      ok(false, "Resolving an Xray to a promise with TestingFunctions resolvePromise should not fail");
    241    }
    242  ).then(nextTest);
    243 }
    244 
    245 function testReject1() {
    246  var p = win.Promise.reject(5);
    247  ok(p instanceof win.Promise, "Promise.reject should return a promise");
    248  p.then(
    249    function() {
    250      ok(false, "Promise should be rejected");
    251    },
    252    function(e) {
    253      is(e, 5, "Should get correct Promise.reject value");
    254    }
    255  ).then(nextTest);
    256 }
    257 
    258 function testReject2() {
    259  var p = new win.Promise(() => {});
    260  Cu.getJSTestingFunctions().rejectPromise(p, 42);
    261  p.then(
    262    function() {
    263      ok(false, "Rejecting an Xray to a promise with TestingFunctions rejectPromise should trigger catch handler");
    264    },
    265    function(e) {
    266      is(e, 42, "Rejecting an Xray to a promise with TestingFunctions rejectPromise should work");
    267    }
    268  ).then(nextTest);
    269 }
    270 
    271 function testThen1() {
    272  var p = win.Promise.resolve(5);
    273  var q = p.then((x) => x * x);
    274  ok(q instanceof win.Promise,
    275     "Promise.then should return a promise from the right global");
    276  q.then(
    277    function(arg) {
    278      is(arg, 25, "Promise.then should work");
    279    },
    280    function() {
    281      ok(false, "Promise.then should not fail");
    282    }
    283  ).then(nextTest);
    284 }
    285 
    286 function testThen2() {
    287  var p = win.Promise.resolve(5);
    288  var q = p.then((x) => Promise.resolve(x * x));
    289  ok(q instanceof win.Promise,
    290     "Promise.then should return a promise from the right global");
    291  q.then(
    292    function(arg) {
    293      is(arg, 25, "Promise.then resolved with chrome promise should work");
    294    },
    295    function() {
    296      ok(false, "Promise.then resolved with chrome promise should not fail");
    297    }
    298  ).then(nextTest);
    299 }
    300 
    301 function testCatch1() {
    302  var p = win.Promise.reject(5);
    303  ok(p instanceof win.Promise, "Promise.reject should return a promise");
    304  var q = p.catch((x) => x * x);
    305  ok(q instanceof win.Promise,
    306     "Promise.catch should return a promise from the right global");
    307  q.then(
    308    function(arg) {
    309      is(arg, 25, "Promise.catch should work");
    310    },
    311    function() {
    312      ok(false, "Promise.catch should not fail");
    313    }
    314  ).then(nextTest);
    315 }
    316 
    317 function testToStringTag1() {
    318  is(win.Promise.prototype[Symbol.toStringTag], "Promise", "@@toStringTag was incorrect");
    319  var p = win.Promise.resolve();
    320  is(String(p), "[object Promise]", "String() result was incorrect");
    321  is(p.toString(), "[object Promise]", "toString result was incorrect");
    322  is(Object.prototype.toString.call(p), "[object Promise]", "second toString result was incorrect");
    323  nextTest();
    324 }
    325 
    326 var tests = [
    327  testLoadComplete,
    328  testHaveXray,
    329  testConstructor1,
    330  testConstructor2,
    331  testRace1,
    332  testRace2,
    333  testRace3,
    334  testRace4,
    335  testAll1,
    336  testAll2,
    337  testAll3,
    338  testAll4,
    339  testAll5,
    340  testResolve1,
    341  testResolve2,
    342  testResolve3,
    343  testResolve4,
    344  testReject1,
    345  testReject2,
    346  testThen1,
    347  testThen2,
    348  testCatch1,
    349  testToStringTag1,
    350 ];
    351 
    352 function nextTest() {
    353  if (!tests.length) {
    354    SimpleTest.finish();
    355    return;
    356  }
    357  tests.shift()();
    358 }
    359 
    360 addLoadEvent(nextTest);
    361 
    362 </script>
    363 </pre>
    364 </body>
    365 </html>