summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webmessaging/broadcastchannel/ordering.html
blob: 2d521b9e0ccf94feb42fa9acc09343314f625a87 (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
<!DOCTYPE html>
<meta charset=utf-8>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<!-- Pull in the with_iframe helper function from the service worker tests -->
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<body>
<script>

const BC0_FIRST_MSG = 'from BC0 - first';
const BC1_FIRST_MSG = 'from BC1 - first';
const BC2_FIRST_MSG = 'from BC2 - first';
const BC3_FIRST_MSG = 'from BC3 - first';
const BC0_SECOND_MSG = 'from BC0 - second';
const BC1_SECOND_MSG = 'from BC1 - second';
const BC2_SECOND_MSG = 'from BC2 - second';
const BC3_SECOND_MSG = 'done';
const BC0_TARGET_NAME = 'BC1';
const BC1_TARGET_NAME = 'BC1';
const BC2_TARGET_NAME = 'BC2';
const BC3_TARGET_NAME = 'BC3';
const MULTI_FRAME_ORDERING_TEST_CHANNEL_NAME = 'multi-frame-order';

const bc0 = new BroadcastChannel(MULTI_FRAME_ORDERING_TEST_CHANNEL_NAME);
const messages = [];

function logReceivedMessage(targetname, e) {
  messages.push({'target': targetname, 'data': e.data});
}

function postMessagesToChannel() {
  return new Promise((resolve) => {
    bc0.postMessage(BC0_FIRST_MSG);
    bc0.postMessage(BC0_SECOND_MSG);
    resolve();
  });
}

// Expected flow of messages between the BroadcastChannel objects (based on
// the requirement that messages get delivered to BroadcastChannel objects
// "in creation order, oldest first") and comments describing the actions
// taken in response to each event
const EXPECTED_RESULTS = [
  // -> BC0 sends two messages, BC1 and BC2 are connected to the channel

  {'data': BC0_FIRST_MSG, 'target': BC1_TARGET_NAME},
  // -> BC1 Creates BC3 and sends first message

  {'data': BC0_FIRST_MSG, 'target': BC2_TARGET_NAME},
  // -> BC2 sends two messages

  // BC3 isn't expected to receive the messages sent before it was created, so
  // no corresponding entries here for messages from BC0.

  {'data': BC0_SECOND_MSG, 'target': BC1_TARGET_NAME},
  // -> BC1 sends second message

  {'data': BC0_SECOND_MSG, 'target': BC2_TARGET_NAME},
  // -> BC2 closes

  {'data': BC1_FIRST_MSG, 'target': BC0_TARGET_NAME},

  {'data': BC1_FIRST_MSG, 'target': BC3_TARGET_NAME},
  // -> BC3 sends first message

  {'data': BC2_FIRST_MSG, 'target': BC0_TARGET_NAME},

  {'data': BC2_FIRST_MSG, 'target': BC1_TARGET_NAME},
  // -> BC1 closes

  {'data': BC2_FIRST_MSG, 'target': BC3_TARGET_NAME},
  // -> BC3 sends second message

  {'data': BC2_SECOND_MSG, 'target': BC0_TARGET_NAME},

  {'data': BC2_SECOND_MSG, 'target': BC3_TARGET_NAME},
  // -> BC3 closes

  {'data': BC1_SECOND_MSG, 'target': BC0_TARGET_NAME},

  {'data': BC3_FIRST_MSG, 'target': BC0_TARGET_NAME},

  {'data': BC3_SECOND_MSG, 'target': BC0_TARGET_NAME},
];

function testCompletion(t) {
  return new Promise((resolve) => {
    bc0.onmessage = t.step_func(e => {
      logReceivedMessage(BC0_TARGET_NAME, e);
      if (e.data == BC3_SECOND_MSG) {
        assert_equals(messages.length, EXPECTED_RESULTS.length);
        for(var i = 0; i < messages.length; i++) {
          assert_equals(messages[i].target, EXPECTED_RESULTS[i].target, `Message ${i+1} has unexpected target`);
          assert_equals(messages[i].data, EXPECTED_RESULTS[i].data, `Message ${i+1} has unexpected message contents`);
        }
        resolve();
      }
    });
  });
}

promise_test(async t => {

  const testResults = testCompletion(t);
  // Await them sequentially because we need the BroadcastChannel object in
  // iframe1 to be created first, we need the BroadcastChannel object in
  // iframe2 to be created second, and then we only want to call
  // postMessagesToChannel once both BroadcastChannels have been created.
  await with_iframe('resources/ordering.html?id=iframe1');
  await with_iframe('resources/ordering.html?id=iframe2');
  await postMessagesToChannel();
  return testResults;
}, "Messages are delivered in port creation order across multiple frames");

</script>
</body>