summaryrefslogtreecommitdiffstats
path: root/dom/promise/tests/test_promise_xrays.html
blob: 55398167ecb000e4031b8e23dc4d279efda67e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1170760
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1170760</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1170760">Mozilla Bug 1170760</a>
<p id="display"></p>
<div id="content" style="display: none">
<iframe id="t" src="http://example.org/chrome/dom/promise/tests/file_promise_xrays.html"></iframe>
</div>

<pre id="test">
<script type="application/javascript">

var win = $("t").contentWindow;

/** Test for Bug 1170760 **/
SimpleTest.waitForExplicitFinish();

function testLoadComplete() {
  is(win.location.href, $("t").src, "Should have loaded the right thing");
  nextTest();
}

function testHaveXray() {
  is(typeof win.Promise.race, "function", "Should see a race() function");
  var exception;
  try {
    win.Promise.wrappedJSObject.race;
  } catch (e) {
    exception = e;
  }
  is(exception, "Getting race", "Should have thrown the right exception");
  is(win.wrappedJSObject.setupThrew, false, "Setup should not have thrown");
  nextTest();
}

function testConstructor1() {
  var p = new win.Promise(function(resolve, reject) { resolve(win.Promise.resolve(5)); });
  p.then(
    function(arg) {
      is(arg, 5, "Content Promise constructor resolved with content promise should work");
    },
    function(e) {
      ok(false, "Content Promise constructor resolved with content promise should not fail");
    }
  ).then(nextTest);
}

function testConstructor2() {
  var p = new win.Promise(function(resolve, reject) { resolve(Promise.resolve(5)); });
  p.then(
    function(arg) {
      is(arg, 5, "Content Promise constructor resolved with chrome promise should work");
    },
    function(e) {
      ok(false, "Content Promise constructor resolved with chrome promise should not fail");
    }
  ).then(nextTest);
}

function testRace1() {
  var p = win.Promise.race(new win.Array(1, 2));
  p.then(
    function(arg) {
      ok(arg == 1 || arg == 2,
         "Should get the right value when racing content-side array");
    },
    function(e) {
      ok(false, "testRace1 threw exception: " + e);
    }
  ).then(nextTest);
}

function testRace2() {
  var p = win.Promise.race(
    new Array(win.Promise.resolve(1), win.Promise.resolve(2)));
  p.then(
    function(arg) {
      ok(arg == 1 || arg == 2,
         "Should get the right value when racing content-side array of explicit Promises");
    },
    function(e) {
      ok(false, "testRace2 threw exception: " + e);
    }
  ).then(nextTest);
}

function testRace3() {
  // This works with a chrome-side array because we do the iteration
  // while still in the Xray compartment.
  var p = win.Promise.race([1, 2]);
  p.then(
    function(arg) {
      ok(arg == 1 || arg == 2,
         "Should get the right value when racing chrome-side array");
    },
    function(e) {
      ok(false, "testRace3 threw exception: " + e);
    }
  ).then(nextTest);
}

function testRace4() {
  // This works with both content-side and chrome-side Promises because we want
  // it to and go to some lengths to make it work.
  var p = win.Promise.race([Promise.resolve(1), win.Promise.resolve(2)]);
  p.then(
    function(arg) {
      ok(arg == 1 || arg == 2,
         "Should get the right value when racing chrome-side promises");
    },
    function(e) {
      ok(false, "testRace4 threw exception: " + e);
    }
  ).then(nextTest);
}

function testAll1() {
  var p = win.Promise.all(new win.Array(1, 2));
  p.then(
    function(arg) {
      ok(arg instanceof win.Array, "Should get an Array from Promise.all (1)");
      is(arg[0], 1, "First entry of Promise.all return value should be correct (1)");
      is(arg[1], 2, "Second entry of Promise.all return value should be correct (1)");
    },
    function(e) {
      ok(false, "testAll1 threw exception: " + e);
    }
  ).then(nextTest);
}

function testAll2() {
  var p = win.Promise.all(
    new Array(win.Promise.resolve(1), win.Promise.resolve(2)));
  p.then(
    function(arg) {
      ok(arg instanceof win.Array, "Should get an Array from Promise.all (2)");
      is(arg[0], 1, "First entry of Promise.all return value should be correct (2)");
      is(arg[1], 2, "Second entry of Promise.all return value should be correct (2)");
    },
    function(e) {
      ok(false, "testAll2 threw exception: " + e);
    }
  ).then(nextTest);
}

function testAll3() {
  // This works with a chrome-side array because we do the iteration
  // while still in the Xray compartment.
  var p = win.Promise.all([1, 2]);
  p.then(
    function(arg) {
      ok(arg instanceof win.Array, "Should get an Array from Promise.all (3)");
      is(arg[0], 1, "First entry of Promise.all return value should be correct (3)");
      is(arg[1], 2, "Second entry of Promise.all return value should be correct (3)");
    },
    function(e) {
      ok(false, "testAll3 threw exception: " + e);
    }
  ).then(nextTest);
}

function testAll4() {
  // This works with both content-side and chrome-side Promises because we want
  // it to and go to some lengths to make it work.
  var p = win.Promise.all([Promise.resolve(1), win.Promise.resolve(2)]);
  p.then(
    function(arg) {
      ok(arg instanceof win.Array, "Should get an Array from Promise.all (4)");
      is(arg[0], 1, "First entry of Promise.all return value should be correct (4)");
      is(arg[1], 2, "Second entry of Promise.all return value should be correct (4)");
    },
    function(e) {
      ok(false, "testAll4 threw exception: " + e);
    }
  ).then(nextTest);
}

function testAll5() {
  var p = win.Promise.all(new win.Array());
  p.then(
    function(arg) {
      ok(arg instanceof win.Array, "Should get an Array from Promise.all (5)");
    },
    function(e) {
      ok(false, "testAll5 threw exception: " + e);
    }
  ).then(nextTest);
}

function testResolve1() {
  var p = win.Promise.resolve(5);
  ok(p instanceof win.Promise, "Promise.resolve should return a promise");
  p.then(
    function(arg) {
      is(arg, 5, "Should get correct Promise.resolve value");
    },
    function(e) {
      ok(false, "testAll5 threw exception: " + e);
    }
  ).then(nextTest);
}

function testResolve2() {
  var p = win.Promise.resolve(5);
  var q = win.Promise.resolve(p);
  is(q, p, "Promise.resolve should just pass through Promise values");
  nextTest();
}

function testResolve3() {
  var p = win.Promise.resolve(Promise.resolve(5));
  p.then(
    function(arg) {
      is(arg, 5, "Promise.resolve with chrome Promise should work");
    },
    function(e) {
      ok(false, "Promise.resolve with chrome Promise should not fail");
    }
  ).then(nextTest);
}

function testResolve4() {
  var p = new win.Promise((res, rej) => {});
  Components.utils.getJSTestingFunctions().resolvePromise(p, 42);
  p.then(
    function(arg) {
      is(arg, 42, "Resolving an Xray to a promise with TestingFunctions resolvePromise should work");
    },
    function(e) {
      ok(false, "Resolving an Xray to a promise with TestingFunctions resolvePromise should not fail");
    }
  ).then(nextTest);
}

function testReject1() {
  var p = win.Promise.reject(5);
  ok(p instanceof win.Promise, "Promise.reject should return a promise");
  p.then(
    function(arg) {
      ok(false, "Promise should be rejected");
    },
    function(e) {
      is(e, 5, "Should get correct Promise.reject value");
    }
  ).then(nextTest);
}

function testReject2() {
  var p = new win.Promise((res, rej) => {});
  Components.utils.getJSTestingFunctions().rejectPromise(p, 42);
  p.then(
    function(arg) {
      ok(false, "Rejecting an Xray to a promise with TestingFunctions rejectPromise should trigger catch handler");
    },
    function(e) {
      is(e, 42, "Rejecting an Xray to a promise with TestingFunctions rejectPromise should work");
    }
  ).then(nextTest);
}

function testThen1() {
  var p = win.Promise.resolve(5);
  var q = p.then((x) => x*x);
  ok(q instanceof win.Promise,
     "Promise.then should return a promise from the right global");
  q.then(
    function(arg) {
      is(arg, 25, "Promise.then should work");
    },
    function(e) {
      ok(false, "Promise.then should not fail");
    }
  ).then(nextTest);
}

function testThen2() {
  var p = win.Promise.resolve(5);
  var q = p.then((x) => Promise.resolve(x*x));
  ok(q instanceof win.Promise,
     "Promise.then should return a promise from the right global");
  q.then(
    function(arg) {
      is(arg, 25, "Promise.then resolved with chrome promise should work");
    },
    function(e) {
      ok(false, "Promise.then resolved with chrome promise should not fail");
    }
  ).then(nextTest);
}

function testCatch1() {
  var p = win.Promise.reject(5);
  ok(p instanceof win.Promise, "Promise.reject should return a promise");
  var q = p.catch((x) => x*x);
  ok(q instanceof win.Promise,
     "Promise.catch should return a promise from the right global");
  q.then(
    function(arg) {
      is(arg, 25, "Promise.catch should work");
    },
    function(e) {
      ok(false, "Promise.catch should not fail");
    }
  ).then(nextTest);
}

function testToStringTag1() {
  is(win.Promise.prototype[Symbol.toStringTag], "Promise", "@@toStringTag was incorrect");
  var p = win.Promise.resolve();
  is(String(p), "[object Promise]", "String() result was incorrect");
  is(p.toString(), "[object Promise]", "toString result was incorrect");
  is(Object.prototype.toString.call(p), "[object Promise]", "second toString result was incorrect");
  nextTest();
}

var tests = [
  testLoadComplete,
  testHaveXray,
  testConstructor1,
  testConstructor2,
  testRace1,
  testRace2,
  testRace3,
  testRace4,
  testAll1,
  testAll2,
  testAll3,
  testAll4,
  testAll5,
  testResolve1,
  testResolve2,
  testResolve3,
  testResolve4,
  testReject1,
  testReject2,
  testThen1,
  testThen2,
  testCatch1,
  testToStringTag1,
];

function nextTest() {
  if (tests.length == 0) {
    SimpleTest.finish();
    return;
  }
  tests.shift()();
}

addLoadEvent(nextTest);

</script>
</pre>
</body>
</html>