Implement self.queueMicrotask in Workers
[WebKit-https.git] / LayoutTests / http / wpt / workers / queue-microtask.any.js
1 promise_test(() => {
2     var counter = 0;
3     self.queueMicrotask(() => {
4         assert_equals(counter++, 1);
5         self.queueMicrotask(() => {
6             assert_equals(counter++, 2);
7         });
8     });
9     var promise = new Promise((resolve, reject) => {
10         setTimeout(() => {
11             assert_equals(counter++, 3);
12             resolve();
13         }, 0);
14     });
15     assert_equals(counter++, 0);
16     return promise;
17 }, `Queued microtasks should be drained before executing macrotasks`);
18
19 promise_test(() => {
20     return new Promise((resolve, reject) => {
21         self.queueMicrotask(function () {
22             try {
23                 assert_equals(arguments.length, 0);
24                 assert_equals(this, self);
25                 self.queueMicrotask(function () {
26                     try {
27                         assert_equals(this, self);
28                         self.queueMicrotask(function () {
29                             'use strict';
30                             try {
31                                 assert_equals(this, undefined);
32                                 resolve();
33                             } catch (e) {
34                                 reject(e);
35                             }
36                         });
37                     } catch (e) {
38                         reject(e);
39                     }
40                 });
41             } catch (e) {
42                 reject(e);
43             }
44         });
45     });
46 }, `queueMicrotask's callback has zero arguments and self as |this|`);
47
48 promise_test(() => {
49     return new Promise((resolve ,reject) => {
50         var counter = 0;
51         Promise.resolve().then(() => {
52             assert_equals(counter++, 1);
53             self.queueMicrotask(() => {
54                 assert_equals(counter++, 3);
55                 resolve();
56             });
57         });
58         self.queueMicrotask(() => {
59             assert_equals(counter++, 2);
60         });
61         assert_equals(counter++, 0);
62     });
63 }, `queueMicrotask and Promise uses the same Microtask queue`);
64
65 test(() => {
66     assert_throws(new TypeError, () => {
67         self.queueMicrotask();
68     });
69     assert_throws(new TypeError, () => {
70         self.queueMicrotask(null);
71     });
72     assert_throws(new TypeError, () => {
73         self.queueMicrotask(undefined);
74     });
75     assert_throws(new TypeError, () => {
76         self.queueMicrotask(42);
77     });
78     assert_throws(new TypeError, () => {
79         self.queueMicrotask("42");
80     });
81     assert_throws(new TypeError, () => {
82         self.queueMicrotask(true);
83     });
84     assert_throws(new TypeError, () => {
85         self.queueMicrotask(Symbol("42"));
86     });
87     assert_throws(new TypeError, () => {
88         self.queueMicrotask({});
89     });
90     assert_throws(new TypeError, () => {
91         self.queueMicrotask({ handleEvent() { } });
92     });
93 }, `queueMicrotask should reject non-function arguments`);