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
|
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script src="../service-workers/service-worker/resources/test-helpers.sub.js"></script>
<script>
async function createConnections(test, firstConnectionCallback, secondConnectionCallback)
{
const pc1 = new RTCPeerConnection();
const pc2 = new RTCPeerConnection();
test.add_cleanup(() => pc1.close());
test.add_cleanup(() => pc2.close());
pc1.onicecandidate = (e) => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = (e) => pc1.addIceCandidate(e.candidate);
firstConnectionCallback(pc1);
const offer = await pc1.createOffer();
await pc1.setLocalDescription(offer);
await pc2.setRemoteDescription(offer);
secondConnectionCallback(pc2);
const answer = await pc2.createAnswer();
await pc2.setLocalDescription(answer);
await pc1.setRemoteDescription(answer);
}
async function waitForMessage(receiver, data)
{
while (true) {
const received = await new Promise(resolve => receiver.onmessage = (event) => resolve(event.data));
if (data === received)
return;
}
}
promise_test(async (test) => {
let localChannel;
let remoteChannel;
const worker = new Worker('transfer-datachannel-worker.js');
let data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
assert_equals(await data, "registered");
await new Promise((resolve, reject) => {
createConnections(test, (firstConnection) => {
localChannel = firstConnection.createDataChannel('sendDataChannel');
worker.postMessage({channel: localChannel}, [localChannel]);
data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
}, (secondConnection) => {
secondConnection.ondatachannel = (event) => {
remoteChannel = event.channel;
remoteChannel.onopen = resolve;
};
});
});
assert_equals(await data, "opened");
data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
remoteChannel.send("OK");
assert_equals(await data, "OK");
data = new Promise(resolve => remoteChannel.onmessage = (event) => resolve(event.data));
worker.postMessage({message: "OK2"});
assert_equals(await data, "OK2");
}, "offerer data channel in workers");
promise_test(async (test) => {
let localChannel;
let remoteChannel;
const worker = new Worker('transfer-datachannel-worker.js');
let data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
assert_equals(await data, "registered");
data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
await new Promise((resolve, reject) => {
createConnections(test, (firstConnection) => {
localChannel = firstConnection.createDataChannel('sendDataChannel');
localChannel.onopen = resolve;
}, (secondConnection) => {
secondConnection.ondatachannel = (event) => {
remoteChannel = event.channel;
worker.postMessage({channel: remoteChannel}, [remoteChannel]);
};
});
});
assert_equals(await data, "opened");
data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
localChannel.send("OK");
assert_equals(await data, "OK");
data = new Promise(resolve => localChannel.onmessage = (event) => resolve(event.data));
worker.postMessage({message: "OK2"});
assert_equals(await data, "OK2");
}, "answerer data channel in workers");
promise_test(async (test) => {
let localChannel;
let remoteChannel;
const worker = new Worker('transfer-datachannel-worker.js');
let data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
assert_equals(await data, "registered");
data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
await new Promise((resolve, reject) => {
createConnections(test, (firstConnection) => {
localChannel = firstConnection.createDataChannel('sendDataChannel');
worker.postMessage({channel: localChannel}, [localChannel]);
}, (secondConnection) => {
secondConnection.ondatachannel = (event) => {
remoteChannel = event.channel;
remoteChannel.onopen = resolve;
};
});
});
assert_equals(await data, "opened");
data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
remoteChannel.close();
assert_equals(await data, "closed");
}, "data channel close event in worker");
promise_test(async (test) => {
let localChannel;
let remoteChannel;
const worker = new Worker('transfer-datachannel-worker.js');
let data = new Promise(resolve => worker.onmessage = (event) => resolve(event.data));
assert_equals(await data, "registered");
await new Promise((resolve, reject) => {
createConnections(test, (firstConnection) => {
localChannel = firstConnection.createDataChannel('sendDataChannel');
}, (secondConnection) => {
secondConnection.ondatachannel = (event) => {
remoteChannel = event.channel;
test.step_timeout(() => {
try {
worker.postMessage({channel: remoteChannel}, [remoteChannel]);
reject("postMessage ok");
} catch(e) {
resolve();
}
}, 0);
};
});
});
}, "Failing to transfer a data channel");
</script>
</body>
</html>
|