summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webmessaging/broadcastchannel/basics.any.js
blob: eec09d65a3aba27feb44bd330e0b5bf9dfdd96a2 (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
test(function() {
  assert_throws_js(
      TypeError,
      () => BroadcastChannel(""),
      "Calling BroadcastChannel constructor without 'new' must throw"
    );
}, "BroadcastChannel constructor called as normal function");

async_test(t => {
    let c1 = new BroadcastChannel('eventType');
    let c2 = new BroadcastChannel('eventType');

    c2.onmessage = t.step_func(e => {
        assert_true(e instanceof MessageEvent);
        assert_equals(e.target, c2);
        assert_equals(e.type, 'message');
        assert_equals(e.origin, location.origin, 'origin');
        assert_equals(e.data, 'hello world');
        assert_equals(e.source, null, 'source');
        t.done();
      });
    c1.postMessage('hello world');
  }, 'postMessage results in correct event');

async_test(t => {
    let c1 = new BroadcastChannel('order');
    let c2 = new BroadcastChannel('order');
    let c3 = new BroadcastChannel('order');

    let events = [];
    let doneCount = 0;
    let handler = t.step_func(e => {
      events.push(e);
      if (e.data == 'done') {
        doneCount++;
        if (doneCount == 2) {
          assert_equals(events.length, 6);
          assert_equals(events[0].target, c2, 'target for event 0');
          assert_equals(events[0].data, 'from c1');
          assert_equals(events[1].target, c3, 'target for event 1');
          assert_equals(events[1].data, 'from c1');
          assert_equals(events[2].target, c1, 'target for event 2');
          assert_equals(events[2].data, 'from c3');
          assert_equals(events[3].target, c2, 'target for event 3');
          assert_equals(events[3].data, 'from c3');
          assert_equals(events[4].target, c1, 'target for event 4');
          assert_equals(events[4].data, 'done');
          assert_equals(events[5].target, c3, 'target for event 5');
          assert_equals(events[5].data, 'done');
          t.done();
        }
      }
    });
    c1.onmessage = handler;
    c2.onmessage = handler;
    c3.onmessage = handler;

    c1.postMessage('from c1');
    c3.postMessage('from c3');
    c2.postMessage('done');
  }, 'messages are delivered in port creation order');

async_test(t => {
    let c1 = new BroadcastChannel('closed');
    let c2 = new BroadcastChannel('closed');
    let c3 = new BroadcastChannel('closed');

    c2.onmessage = t.unreached_func();
    c2.close();
    c3.onmessage = t.step_func(() => t.done());
    c1.postMessage('test');
  }, 'messages aren\'t delivered to a closed port');

 async_test(t => {
    let c1 = new BroadcastChannel('closed');
    let c2 = new BroadcastChannel('closed');
    let c3 = new BroadcastChannel('closed');

    c2.onmessage = t.unreached_func();
    c3.onmessage = t.step_func(() => t.done());
    c1.postMessage('test');
    c2.close();
}, 'messages aren\'t delivered to a port closed after calling postMessage.');

async_test(t => {
    let c1 = new BroadcastChannel('create-in-onmessage');
    let c2 = new BroadcastChannel('create-in-onmessage');

    c2.onmessage = t.step_func(e => {
        assert_equals(e.data, 'first');
        c2.close();
        let c3 = new BroadcastChannel('create-in-onmessage');
        c3.onmessage = t.step_func(event => {
            assert_equals(event.data, 'done');
            t.done();
          });
        c1.postMessage('done');
      });
    c1.postMessage('first');
    c2.postMessage('second');
  }, 'closing and creating channels during message delivery works correctly');

async_test(t => {
    let c1 = new BroadcastChannel('close-in-onmessage');
    let c2 = new BroadcastChannel('close-in-onmessage');
    let c3 = new BroadcastChannel('close-in-onmessage');
    let events = [];
    c1.onmessage = e => events.push('c1: ' + e.data);
    c2.onmessage = e => events.push('c2: ' + e.data);
    c3.onmessage = e => events.push('c3: ' + e.data);

    // c2 closes itself when it receives the first message
    c2.addEventListener('message', e => {
        c2.close();
      });

    c3.addEventListener('message', t.step_func(e => {
        if (e.data == 'done') {
          assert_array_equals(events, [
              'c2: first',
              'c3: first',
              'c3: done']);
          t.done();
        }
      }));
    c1.postMessage('first');
    c1.postMessage('done');
  }, 'Closing a channel in onmessage prevents already queued tasks from firing onmessage events');