blob: cb8b4edc3fae1501fe7e717615d60cb54ac003c1 [file] [log] [blame]
Soares Chen17354db2017-06-13 18:39:041<!doctype html>
2<meta charset=utf-8>
3<title>RTCDataChannel.prototype.send</title>
4<script src="/resources/testharness.js"></script>
5<script src="/resources/testharnessreport.js"></script>
6<script src="RTCPeerConnection-helper.js"></script>
7<script>
8 'use strict';
9
10 // Test is based on the following editor draft:
11 // https://w3c.github.io/webrtc-pc/archives/20170605/webrtc.html
12
13 // The following helper functions are called from RTCPeerConnection-helper.js:
14 // createDataChannelPair
15 // awaitMessage
16 // blobToArrayBuffer
youennfb75b8762018-12-20 21:29:2517 // assert_equals_typed_array
Soares Chen17354db2017-06-13 18:39:0418
19 /*
20 6.2. RTCDataChannel
21 interface RTCDataChannel : EventTarget {
22 ...
23 readonly attribute RTCDataChannelState readyState;
24 readonly attribute unsigned long bufferedAmount;
25 attribute EventHandler onmessage;
26 attribute DOMString binaryType;
27
28 void send(USVString data);
29 void send(Blob data);
30 void send(ArrayBuffer data);
31 void send(ArrayBufferView data);
32 };
33 */
34
35 // Simple ASCII encoded string
36 const helloString = 'hello';
37 // ASCII encoded buffer representation of the string
38 const helloBuffer = Uint8Array.of(0x68, 0x65, 0x6c, 0x6c, 0x6f);
39 const helloBlob = new Blob([helloBuffer]);
40
41 // Unicode string with multiple code units
42 const unicodeString = '世界你好';
43 // UTF-8 encoded buffer representation of the string
44 const unicodeBuffer = Uint8Array.of(
45 0xe4, 0xb8, 0x96, 0xe7, 0x95, 0x8c,
46 0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd);
47
48 /*
49 6.2. send()
50 2. If channel's readyState attribute is connecting, throw an InvalidStateError.
51 */
52 test(t => {
53 const pc = new RTCPeerConnection();
54 const channel = pc.createDataChannel('test');
55 assert_equals(channel.readyState, 'connecting');
56 assert_throws('InvalidStateError', () => channel.send(helloString));
57 }, 'Calling send() when data channel is in connecting state should throw InvalidStateError');
58
59 /*
60 6.2. send()
61 3. Execute the sub step that corresponds to the type of the methods argument:
62
63 string object
64 Let data be the object and increase the bufferedAmount attribute
65 by the number of bytes needed to express data as UTF-8.
66
67 [WebSocket]
68 5. Feedback from the protocol
69 When a WebSocket message has been received
70 4. If type indicates that the data is Text, then initialize event's data
71 attribute to data.
72 */
73 promise_test(t => {
74 return createDataChannelPair()
75 .then(([channel1, channel2]) => {
76 channel1.send(helloString);
77 return awaitMessage(channel2)
78 }).then(message => {
79 assert_equals(typeof message, 'string',
80 'Expect message to be a string');
81
82 assert_equals(message, helloString);
83 });
84 }, 'Data channel should be able to send simple string and receive as string');
85
86 promise_test(t => {
87 return createDataChannelPair()
88 .then(([channel1, channel2]) => {
89 channel1.send(unicodeString);
90 return awaitMessage(channel2)
91 }).then(message => {
92 assert_equals(typeof message, 'string',
93 'Expect message to be a string');
94
95 assert_equals(message, unicodeString);
96 });
97 }, 'Data channel should be able to send unicode string and receive as unicode string');
98 promise_test(t => {
99 return createDataChannelPair()
100 .then(([channel1, channel2]) => {
101 channel2.binaryType = 'arraybuffer';
102 channel1.send(helloString);
103 return awaitMessage(channel2);
104 }).then(message => {
105 assert_equals(typeof message, 'string',
106 'Expect message to be a string');
107
108 assert_equals(message, helloString);
109 });
110 }, 'Data channel should ignore binaryType and always receive string message as string');
111
112 /*
113 6.2. send()
114 3. Execute the sub step that corresponds to the type of the methods argument:
115 ArrayBufferView object
116 Let data be the data stored in the section of the buffer described
117 by the ArrayBuffer object that the ArrayBufferView object references
118 and increase the bufferedAmount attribute by the length of the
119 ArrayBufferView in bytes.
120
121 [WebSocket]
122 5. Feedback from the protocol
123 When a WebSocket message has been received
124 4. If binaryType is set to "arraybuffer", then initialize event's data
125 attribute to a new read-only ArrayBuffer object whose contents are data.
126
127 [WebIDL]
128 4.1. ArrayBufferView
129 typedef (Int8Array or Int16Array or Int32Array or
130 Uint8Array or Uint16Array or Uint32Array or Uint8ClampedArray or
131 Float32Array or Float64Array or DataView) ArrayBufferView;
132 */
133 promise_test(t => {
134 return createDataChannelPair()
135 .then(([channel1, channel2]) => {
136 channel2.binaryType = 'arraybuffer';
137 channel1.send(helloBuffer);
138 return awaitMessage(channel2)
139 }).then(messageBuffer => {
140 assert_true(messageBuffer instanceof ArrayBuffer,
141 'Expect messageBuffer to be an ArrayBuffer');
142
youennfb75b8762018-12-20 21:29:25143 assert_equals_typed_array(messageBuffer, helloBuffer.buffer);
Soares Chen17354db2017-06-13 18:39:04144 });
145 }, 'Data channel should be able to send Uint8Array message and receive as ArrayBuffer');
146
147 /*
148 6.2. send()
149 3. Execute the sub step that corresponds to the type of the methods argument:
150 ArrayBuffer object
151 Let data be the data stored in the buffer described by the ArrayBuffer
152 object and increase the bufferedAmount attribute by the length of the
153 ArrayBuffer in bytes.
154 */
155 promise_test(t => {
156 return createDataChannelPair()
157 .then(([channel1, channel2]) => {
158 channel2.binaryType = 'arraybuffer';
159 channel1.send(helloBuffer.buffer);
160 return awaitMessage(channel2)
161 }).then(messageBuffer => {
162 assert_true(messageBuffer instanceof ArrayBuffer,
163 'Expect messageBuffer to be an ArrayBuffer');
164
youennfb75b8762018-12-20 21:29:25165 assert_equals_typed_array(messageBuffer, helloBuffer.buffer);
Soares Chen17354db2017-06-13 18:39:04166 });
167 }, 'Data channel should be able to send ArrayBuffer message and receive as ArrayBuffer');
168
169 /*
170 6.2. send()
171 3. Execute the sub step that corresponds to the type of the methods argument:
172 Blob object
173 Let data be the raw data represented by the Blob object and increase
174 the bufferedAmount attribute by the size of data, in bytes.
175 */
176 promise_test(t => {
177 return createDataChannelPair()
178 .then(([channel1, channel2]) => {
179 channel2.binaryType = 'arraybuffer';
180 channel1.send(helloBlob);
181 return awaitMessage(channel2);
182 }).then(messageBuffer => {
183 assert_true(messageBuffer instanceof ArrayBuffer,
184 'Expect messageBuffer to be an ArrayBuffer');
185
youennfb75b8762018-12-20 21:29:25186 assert_equals_typed_array(messageBuffer, helloBuffer.buffer);
Soares Chen17354db2017-06-13 18:39:04187 });
188 }, 'Data channel should be able to send Blob message and receive as ArrayBuffer');
189
190 /*
191 [WebSocket]
192 5. Feedback from the protocol
193 When a WebSocket message has been received
194 4. If binaryType is set to "blob", then initialize event's data attribute
195 to a new Blob object that represents data as its raw data.
196 */
197 promise_test(t => {
198 return createDataChannelPair()
199 .then(([channel1, channel2]) => {
200 channel2.binaryType = 'blob';
201 channel1.send(helloBuffer);
202 return awaitMessage(channel2);
203 })
204 .then(messageBlob => {
205 assert_true(messageBlob instanceof Blob,
206 'Expect received messageBlob to be a Blob');
207
208 return blobToArrayBuffer(messageBlob);
209 }).then(messageBuffer => {
210 assert_true(messageBuffer instanceof ArrayBuffer,
211 'Expect messageBuffer to be an ArrayBuffer');
212
youennfb75b8762018-12-20 21:29:25213 assert_equals_typed_array(messageBuffer, helloBuffer.buffer);
Soares Chen17354db2017-06-13 18:39:04214 });
215 }, 'Data channel should be able to send ArrayBuffer message and receive as Blob');
216
217 /*
218 6.2. RTCDataChannel
219 binaryType
220 The binaryType attribute must, on getting, return the value to which it was
221 last set. On setting, the user agent must set the IDL attribute to the new
222 value. When a RTCDataChannel object is created, the binaryType attribute must
223 be initialized to the string "blob".
224 */
225 promise_test(t => {
226 return createDataChannelPair()
227 .then(([channel1, channel2]) => {
228 assert_equals(channel2.binaryType, 'blob',
229 'Expect initial binaryType value to be blob');
230
231 channel1.send(helloBuffer);
232 return awaitMessage(channel2);
233 })
234 .then(messageBlob => {
235 assert_true(messageBlob instanceof Blob,
236 'Expect received messageBlob to be a Blob');
237
238 return blobToArrayBuffer(messageBlob);
239 }).then(messageBuffer => {
240 assert_true(messageBuffer instanceof ArrayBuffer,
241 'Expect messageBuffer to be an ArrayBuffer');
242
youennfb75b8762018-12-20 21:29:25243 assert_equals_typed_array(messageBuffer, helloBuffer.buffer);
Soares Chen17354db2017-06-13 18:39:04244 });
245 }, 'Data channel binaryType should receive message as Blob by default');
246
247 // Test sending 3 messages: helloBuffer, unicodeString, helloBlob
248 async_test(t => {
249 const receivedMessages = [];
250
251 const onMessage = t.step_func(event => {
252 const { data } = event;
253 receivedMessages.push(data);
254
255 if(receivedMessages.length === 3) {
youennfb75b8762018-12-20 21:29:25256 assert_equals_typed_array(receivedMessages[0], helloBuffer.buffer);
Soares Chen17354db2017-06-13 18:39:04257 assert_equals(receivedMessages[1], unicodeString);
youennfb75b8762018-12-20 21:29:25258 assert_equals_typed_array(receivedMessages[2], helloBuffer.buffer);
Soares Chen17354db2017-06-13 18:39:04259
260 t.done();
261 }
262 });
263
264 createDataChannelPair()
265 .then(([channel1, channel2]) => {
266 channel2.binaryType = 'arraybuffer';
267 channel2.addEventListener('message', onMessage);
268
269 channel1.send(helloBuffer);
270 channel1.send(unicodeString);
271 channel1.send(helloBlob);
272
273 }).catch(t.step_func(err =>
274 assert_unreached(`Unexpected promise rejection: ${err}`)));
275 }, 'Sending multiple messages with different types should succeed and be received');
276
277 /*
278 [Deferred]
279 6.2. RTCDataChannel
280 The send() method is being amended in w3c/webrtc-pc#1209 to throw error instead
281 of closing data channel when buffer is full
282
283 send()
284 4. If channel's underlying data transport is not established yet, or if the
285 closing procedure has started, then abort these steps.
286 5. Attempt to send data on channel's underlying data transport; if the data
287 cannot be sent, e.g. because it would need to be buffered but the buffer
288 is full, the user agent must abruptly close channel's underlying data
289 transport with an error.
290
291 test(t => {
292 const pc = new RTCPeerConnection();
293 const channel = pc.createDataChannel('test');
294 channel.close();
295 assert_equals(channel.readyState, 'closing');
296 channel.send(helloString);
297 }, 'Calling send() when data channel is in closing state should succeed');
298 */
299</script>