Add self.queueMicrotask(f) on DOMWindow
[WebKit-https.git] / LayoutTests / js / dom / queue-microtask-window.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>self.queueMicrotask works in Window</title>
5 <script src="../../resources/testharness.js"></script>
6 <script src="../../resources/testharnessreport.js"></script>
7 </head>
8 <body>
9 <iframe></iframe>
10 <script>
11 promise_test(() => {
12     var counter = 0;
13     self.queueMicrotask(() => {
14         assert_equals(counter++, 1);
15         self.queueMicrotask(() => {
16             assert_equals(counter++, 2);
17         });
18     });
19     var promise = new Promise((resolve, reject) => {
20         setTimeout(() => {
21             assert_equals(counter++, 3);
22             resolve();
23         }, 0);
24     });
25     assert_equals(counter++, 0);
26     return promise;
27 }, `Queued microtasks should be drained before executing macrotasks`);
28
29 promise_test(() => {
30     return new Promise((resolve, reject) => {
31         self.queueMicrotask(function () {
32             try {
33                 assert_equals(arguments.length, 0);
34                 assert_equals(this, self);
35                 assert_equals(frames.length, 1);
36                 assert_not_equals(frames[0], self);
37                 var iframe = frames[0];
38                 iframe.queueMicrotask(function () {
39                     try {
40                         assert_equals(this, self);
41                         assert_not_equals(this, iframe);
42                         self.queueMicrotask(function () {
43                             'use strict';
44                             try {
45                                 assert_equals(this, undefined);
46                                 resolve();
47                             } catch (e) {
48                                 reject(e);
49                             }
50                         });
51                     } catch (e) {
52                         reject(e);
53                     }
54                 });
55             } catch (e) {
56                 reject(e);
57             }
58         });
59     });
60 }, `queueMicrotask's callback has zero arguments and self as |this|`);
61
62 promise_test(() => {
63     return new Promise((resolve ,reject) => {
64         var counter = 0;
65         Promise.resolve().then(() => {
66             assert_equals(counter++, 1);
67             self.queueMicrotask(() => {
68                 assert_equals(counter++, 3);
69                 resolve();
70             });
71         });
72         self.queueMicrotask(() => {
73             assert_equals(counter++, 2);
74         });
75         assert_equals(counter++, 0);
76     });
77 }, `queueMicrotask and Promise uses the same Microtask queue`);
78
79 test(() => {
80     assert_throws(new TypeError, () => {
81         self.queueMicrotask();
82     });
83     assert_throws(new TypeError, () => {
84         self.queueMicrotask(null);
85     });
86     assert_throws(new TypeError, () => {
87         self.queueMicrotask(undefined);
88     });
89     assert_throws(new TypeError, () => {
90         self.queueMicrotask(42);
91     });
92     assert_throws(new TypeError, () => {
93         self.queueMicrotask("42");
94     });
95     assert_throws(new TypeError, () => {
96         self.queueMicrotask(true);
97     });
98     assert_throws(new TypeError, () => {
99         self.queueMicrotask(Symbol("42"));
100     });
101     assert_throws(new TypeError, () => {
102         self.queueMicrotask({});
103     });
104     assert_throws(new TypeError, () => {
105         self.queueMicrotask({ handleEvent() { } });
106     });
107 }, `queueMicrotask should reject non-function arguments`);
108 </script>
109 </body>
110 </html>