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
|
<!DOCTYPE html>
<title>Service Worker: postMessage</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
promise_test(t => {
var script = 'resources/postmessage-worker.js';
var scope = 'resources/blank.html';
var registration;
var worker;
var port;
return service_worker_unregister_and_register(t, script, scope)
.then(r => {
t.add_cleanup(() => r.unregister());
registration = r;
worker = registration.installing;
var messageChannel = new MessageChannel();
port = messageChannel.port1;
return new Promise(resolve => {
port.onmessage = resolve;
worker.postMessage({port: messageChannel.port2},
[messageChannel.port2]);
worker.postMessage({value: 1});
worker.postMessage({value: 2});
worker.postMessage({done: true});
});
})
.then(e => {
assert_equals(e.data, 'Acking value: 1');
return new Promise(resolve => { port.onmessage = resolve; });
})
.then(e => {
assert_equals(e.data, 'Acking value: 2');
return new Promise(resolve => { port.onmessage = resolve; });
})
.then(e => {
assert_equals(e.data, 'quit');
return registration.unregister(scope);
});
}, 'postMessage to a ServiceWorker (and back via MessagePort)');
promise_test(t => {
var script = 'resources/postmessage-transferables-worker.js';
var scope = 'resources/blank.html';
var sw = navigator.serviceWorker;
var message = 'Hello, world!';
var text_encoder = new TextEncoder;
var text_decoder = new TextDecoder;
return service_worker_unregister_and_register(t, script, scope)
.then(r => {
t.add_cleanup(() => r.unregister());
var ab = text_encoder.encode(message);
assert_equals(ab.byteLength, message.length);
r.installing.postMessage(ab, [ab.buffer]);
assert_equals(text_decoder.decode(ab), '');
assert_equals(ab.byteLength, 0);
return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify the integrity of the transferred array buffer.
assert_equals(e.data.content, message);
assert_equals(e.data.byteLength, message.length);
return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify the integrity of the array buffer sent back from
// ServiceWorker via Client.postMessage.
assert_equals(text_decoder.decode(e.data), message);
assert_equals(e.data.byteLength, message.length);
return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify that the array buffer on ServiceWorker is neutered.
assert_equals(e.data.content, '');
assert_equals(e.data.byteLength, 0);
});
}, 'postMessage a transferable ArrayBuffer between ServiceWorker and Client');
promise_test(t => {
var script = 'resources/postmessage-transferables-worker.js';
var scope = 'resources/blank.html';
var message = 'Hello, world!';
var text_encoder = new TextEncoder;
var text_decoder = new TextDecoder;
var port;
return service_worker_unregister_and_register(t, script, scope)
.then(r => {
t.add_cleanup(() => r.unregister());
var channel = new MessageChannel;
port = channel.port1;
r.installing.postMessage(undefined, [channel.port2]);
var ab = text_encoder.encode(message);
assert_equals(ab.byteLength, message.length);
port.postMessage(ab, [ab.buffer]);
assert_equals(text_decoder.decode(ab), '');
assert_equals(ab.byteLength, 0);
return new Promise(resolve => { port.onmessage = resolve; });
})
.then(e => {
// Verify the integrity of the transferred array buffer.
assert_equals(e.data.content, message);
assert_equals(e.data.byteLength, message.length);
return new Promise(resolve => { port.onmessage = resolve; });
})
.then(e => {
// Verify the integrity of the array buffer sent back from
// ServiceWorker via Client.postMessage.
assert_equals(text_decoder.decode(e.data), message);
assert_equals(e.data.byteLength, message.length);
return new Promise(resolve => { port.onmessage = resolve; });
})
.then(e => {
// Verify that the array buffer on ServiceWorker is neutered.
assert_equals(e.data.content, '');
assert_equals(e.data.byteLength, 0);
});
}, 'postMessage a transferable ArrayBuffer between ServiceWorker and Client' +
' over MessagePort');
promise_test(t => {
var script = 'resources/postmessage-dictionary-transferables-worker.js';
var scope = 'resources/blank.html';
var sw = navigator.serviceWorker;
var message = 'Hello, world!';
var text_encoder = new TextEncoder;
var text_decoder = new TextDecoder;
return service_worker_unregister_and_register(t, script, scope)
.then(r => {
t.add_cleanup(() => r.unregister());
var ab = text_encoder.encode(message);
assert_equals(ab.byteLength, message.length);
r.installing.postMessage(ab, {transfer: [ab.buffer]});
assert_equals(text_decoder.decode(ab), '');
assert_equals(ab.byteLength, 0);
return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify the integrity of the transferred array buffer.
assert_equals(e.data.content, message);
assert_equals(e.data.byteLength, message.length);
return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify the integrity of the array buffer sent back from
// ServiceWorker via Client.postMessage.
assert_equals(text_decoder.decode(e.data), message);
assert_equals(e.data.byteLength, message.length);
return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify that the array buffer on ServiceWorker is neutered.
assert_equals(e.data.content, '');
assert_equals(e.data.byteLength, 0);
});
}, 'postMessage with dictionary a transferable ArrayBuffer between ServiceWorker and Client');
promise_test(async t => {
const firstScript = 'resources/postmessage-echo-worker.js?one';
const secondScript = 'resources/postmessage-echo-worker.js?two';
const scope = 'resources/';
const registration = await service_worker_unregister_and_register(t, firstScript, scope);
t.add_cleanup(() => registration.unregister());
const firstWorker = registration.installing;
const messagePromise = new Promise(resolve => {
navigator.serviceWorker.addEventListener('message', (event) => {
resolve(event.data);
}, {once: true});
});
await wait_for_state(t, firstWorker, 'activated');
await navigator.serviceWorker.register(secondScript, {scope});
const secondWorker = registration.installing;
await wait_for_state(t, firstWorker, 'redundant');
// postMessage() to a redundant worker should be dropped silently.
// Historically, this threw an exception.
firstWorker.postMessage('firstWorker');
// To test somewhat that it was not received, send a message to another
// worker and check that we get a reply for that one.
secondWorker.postMessage('secondWorker');
const data = await messagePromise;
assert_equals(data, 'secondWorker');
}, 'postMessage to a redundant worker');
</script>
|