summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/test_multi_sharedWorker.html
blob: ba028302faf6da7eee1a5582de3e9e58ebd17751 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
  <head>
    <title>Test for SharedWorker</title>
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
      <script class="testbody" type="text/javascript;version=1.7">
        "use strict";

        const basePath =
          location.pathname.substring(0,
                                      location.pathname.lastIndexOf("/") + 1);
        const baseURL = location.origin + basePath;

        const frameRelativeURL = "multi_sharedWorker_frame.html";
        const frameAbsoluteURL = baseURL + frameRelativeURL;
        const workerAbsoluteURL =
          baseURL + "multi_sharedWorker_sharedWorker.js";

        const storedData = "0123456789abcdefghijklmnopqrstuvwxyz";
        const errorMessage = "Error: Expected";
        const errorLineno = 34;

        let testGenerator = (function() {
          SimpleTest.waitForExplicitFinish();

          window.addEventListener("message", function(event) {
            if (typeof(event.data) == "string") {
              info(event.data);
            } else {
              sendToGenerator(event);
            }
          });

          let frame1 = document.getElementById("frame1");
          frame1.src = frameRelativeURL;
          frame1.onload = sendToGenerator;

          yield undefined;

          frame1 = frame1.contentWindow;

          let frame2 = document.getElementById("frame2");
          frame2.src = frameAbsoluteURL;
          frame2.onload = sendToGenerator;

          yield undefined;

          frame2 = frame2.contentWindow;

          let data = {
            command: "start"
          };

          frame1.postMessage(data, "*");
          frame2.postMessage(data, "*");

          let event = yield undefined;
          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.source, frame1, "First window got the event");
          is(event.data.type, "connect", "Got a connect message");

          data = {
            command: "retrieve"
          };
          frame1.postMessage(data, "*");

          event = yield undefined;
          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.source, frame1, "First window got the event");
          is(event.data.type, "result", "Got a result message");
          is(event.data.data, undefined, "No data stored yet");

          frame2.postMessage(data, "*");

          event = yield undefined;
          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.source, frame2, "Second window got the event");
          is(event.data.type, "result", "Got a result message");
          is(event.data.data, undefined, "No data stored yet");

          data = {
            command: "store",
            data: storedData
          };
          frame2.postMessage(data, "*");

          data = {
            command: "retrieve"
          };
          frame1.postMessage(data, "*");

          event = yield undefined;
          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.source, frame1, "First window got the event");
          is(event.data.type, "result", "Got a result message");
          is(event.data.data, storedData, "Got stored data");

          // This will generate two MessageEvents, one for each window.
          let sawFrame1Error = false;
          let sawFrame2Error = false;

          data = {
            command: "error"
          };
          frame1.postMessage(data, "*");

          // First event.
          event = yield undefined;

          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.data.type, "worker-error", "Got an error message");
          is(event.data.message, errorMessage, "Got correct error message");
          is(event.data.filename, workerAbsoluteURL, "Got correct filename");
          is(event.data.lineno, errorLineno, "Got correct lineno");
          if (event.source == frame1) {
            is(sawFrame1Error, false, "Haven't seen error for frame1 yet");
            sawFrame1Error = true;
          } else if (event.source == frame2) {
            is(sawFrame2Error, false, "Haven't seen error for frame1 yet");
            sawFrame2Error = true;
          } else {
            ok(false, "Saw error from unknown window");
          }

          // Second event
          event = yield undefined;

          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.data.type, "worker-error", "Got an error message");
          is(event.data.message, errorMessage, "Got correct error message");
          is(event.data.filename, workerAbsoluteURL, "Got correct filename");
          is(event.data.lineno, errorLineno, "Got correct lineno");
          if (event.source == frame1) {
            is(sawFrame1Error, false, "Haven't seen error for frame1 yet");
            sawFrame1Error = true;
          } else if (event.source == frame2) {
            is(sawFrame2Error, false, "Haven't seen error for frame1 yet");
            sawFrame2Error = true;
          } else {
            ok(false, "Saw error from unknown window");
          }

          is(sawFrame1Error, true, "Saw error for frame1");
          is(sawFrame2Error, true, "Saw error for frame2");

          // This will generate two MessageEvents, one for each window.
          sawFrame1Error = false;
          sawFrame2Error = false;

          data = {
            command: "error"
          };
          frame1.postMessage(data, "*");

          // First event.
          event = yield undefined;

          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.data.type, "error", "Got an error message");
          is(event.data.message, errorMessage, "Got correct error message");
          is(event.data.filename, workerAbsoluteURL, "Got correct filename");
          is(event.data.lineno, errorLineno, "Got correct lineno");
          is(event.data.isErrorEvent, true, "Frame got an ErrorEvent");
          if (event.source == frame1) {
            is(sawFrame1Error, false, "Haven't seen error for frame1 yet");
            sawFrame1Error = true;
          } else if (event.source == frame2) {
            is(sawFrame2Error, false, "Haven't seen error for frame1 yet");
            sawFrame2Error = true;
          } else {
            ok(false, "Saw error from unknown window");
          }

          // Second event
          event = yield undefined;

          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.data.type, "error", "Got an error message");
          is(event.data.message, errorMessage, "Got correct error message");
          is(event.data.filename, workerAbsoluteURL, "Got correct filename");
          is(event.data.lineno, errorLineno, "Got correct lineno");
          is(event.data.isErrorEvent, true, "Frame got an ErrorEvent");
          if (event.source == frame1) {
            is(sawFrame1Error, false, "Haven't seen error for frame1 yet");
            sawFrame1Error = true;
          } else if (event.source == frame2) {
            is(sawFrame2Error, false, "Haven't seen error for frame1 yet");
            sawFrame2Error = true;
          } else {
            ok(false, "Saw error from unknown window");
          }

          is(sawFrame1Error, true, "Saw error for frame1");
          is(sawFrame2Error, true, "Saw error for frame2");

          // Try a shared worker in a different origin.
          frame1 = document.getElementById("frame1");
          frame1.src = "http://example.org" + basePath + frameRelativeURL;
          frame1.onload = sendToGenerator;
          yield undefined;

          frame1 = frame1.contentWindow;

          data = {
            command: "retrieve"
          };
          frame1.postMessage(data, "*");

          event = yield undefined;
          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.source, frame1, "First window got the event");
          is(event.data.type, "result", "Got a result message");
          is(event.data.data, undefined, "No data stored yet");

          frame2.postMessage(data, "*");

          event = yield undefined;
          ok(event instanceof MessageEvent, "Got a MessageEvent");
          is(event.source, frame2, "First window got the event");
          is(event.data.type, "result", "Got a result message");
          is(event.data.data, storedData, "Got stored data");

          window.removeEventListener("message", sendToGenerator);

          SimpleTest.finish();
          yield undefined;
        })();

        let sendToGenerator = testGenerator.send.bind(testGenerator);

      </script>
  </head>
  <body onload="testGenerator.next();">
    <iframe id="frame1"></iframe>
    <iframe id="frame2"></iframe>
  </body>
</html>