test_promise_xrays.html 10 KB

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