Bug 1736292 [wpt PR 31290] - BroadcastChannel: Add WPTs for detached iframes, closed...
[gecko.git] / testing / web-platform / tests / webmessaging / broadcastchannel / workers.html
blob16fea069309f5738e51305a0bd53bd1413e86f11
1 <!DOCTYPE html>
2 <meta charset=utf-8>
3 <script src="/resources/testharness.js"></script>
4 <script src="/resources/testharnessreport.js"></script>
5 <script>
7 async_test(t => {
8 let c1 = new BroadcastChannel('worker');
9 let c2 = new BroadcastChannel('worker');
10 let events = [];
12 c1.onmessage = e => events.push(e);
13 c2.onmessage = e => events.push(e);
15 let doneCount = 0;
16 c2.addEventListener('message', t.step_func(e => {
17 if (e.data == 'from worker') {
18 c2.postMessage('from c2');
19 c1.postMessage('done');
20 } else if (e.data == 'done') {
21 assert_equals(events.length, 4);
22 assert_equals(events[0].data, 'from worker');
23 assert_equals(events[0].target, c1);
24 assert_equals(events[1].data, 'from worker');
25 assert_equals(events[1].target, c2);
26 assert_equals(events[2].data, 'from c2');
27 assert_equals(events[3].data, 'done');
28 if (++doneCount == 2) t.done();
30 }));
32 let worker = new Worker('resources/worker.js');
33 worker.onmessage = t.step_func(e => {
34 assert_array_equals(e.data, ['from c2', 'done']);
35 if (++doneCount == 2) t.done();
36 });
37 worker.postMessage({channel: 'worker'});
39 }, 'BroadcastChannel works in workers');
41 async_test(t => {
42 let c1 = new BroadcastChannel('shared worker');
43 let c2 = new BroadcastChannel('shared worker');
44 let events = [];
46 c1.onmessage = e => events.push(e);
47 c2.onmessage = e => events.push(e);
49 let doneCount = 0;
50 c2.addEventListener('message', t.step_func(e => {
51 if (e.data == 'from worker') {
52 c2.postMessage('from c2');
53 c1.postMessage('done');
54 } else if (e.data == 'done') {
55 assert_equals(events.length, 4);
56 assert_equals(events[0].data, 'from worker');
57 assert_equals(events[0].target, c1);
58 assert_equals(events[1].data, 'from worker');
59 assert_equals(events[1].target, c2);
60 assert_equals(events[2].data, 'from c2');
61 assert_equals(events[3].data, 'done');
62 if (++doneCount == 2) t.done();
64 }));
66 let worker = new SharedWorker('resources/worker.js');
67 worker.port.onmessage = t.step_func(e => {
68 assert_array_equals(e.data, ['from c2', 'done']);
69 if (++doneCount == 2) t.done();
70 });
71 worker.port.postMessage({channel: 'shared worker'});
73 }, 'BroadcastChannel works in shared workers');
75 async_test(t => {
76 let c = new BroadcastChannel('worker-close');
77 let events = [];
79 c.onmessage = e => events.push('c1: ' + e.data);
81 let worker = new Worker('resources/worker.js');
82 worker.onmessage = t.step_func(e => {
83 assert_array_equals(events,
84 ['c1: from worker', 'c2: ready', 'c2: echo'],
85 'messages in document');
86 assert_array_equals(e.data, ['done'], 'messages in worker');
87 t.done();
88 });
89 worker.onmessagerror =
90 t.unreached_func('Worker\'s onmessageerror handler called');
92 c.addEventListener('message', e => {
93 if (e.data == 'from worker') {
94 c.close();
95 if (self.gc) self.gc();
96 window.setTimeout(() => {
97 let c2 = new BroadcastChannel('worker-close');
98 c2.onmessage = e => {
99 events.push('c2: ' + e.data);
100 if (e.data === 'ready') {
101 worker.postMessage({ping: 'echo'});
102 } else {
103 c2.postMessage('done');
104 c2.close();
107 // For some implementations there may be a race condition between
108 // when the BroadcastChannel instance above is created / ready to
109 // receive messages and when the worker calls postMessage on it's
110 // BroadcastChannel instance. To avoid this, confirm that our
111 // instance can receive a message before indicating to the other
112 // thread that we are ready. For more details, see:
113 // https://github.com/whatwg/html/issues/7267
114 let c3 = new BroadcastChannel('worker-close');
115 c3.postMessage('ready');
116 c3.close();
117 }, 1);
121 worker.postMessage({channel: 'worker-close'});
122 t.add_cleanup(() => worker.terminate());
124 }, 'Closing and re-opening a channel works.');
126 </script>