| <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |
| <html> |
| <head> |
| <script src="../../resources/js-test.js"></script> |
| </head> |
| <body> |
| <script> |
| description("Tests RTCDataChannel."); |
| |
| var pc = null; |
| var pc2 = null; |
| var dc = null; |
| var dc2 = null; |
| var data; |
| var array; |
| |
| function dc_onclose() { |
| testPassed("dc_onclose was called"); |
| shouldBe("dc.readyState", "'closed'"); |
| |
| finishJSTest(); |
| } |
| |
| function dc_onmessage_sharedarraybuffer_view() { |
| if (window.SharedArrayBuffer) { |
| shouldThrow("dc.send(new Uint8Array(new SharedArrayBuffer(16)));"); |
| } |
| |
| dc2.onclose = dc_onclose; |
| dc.close(); |
| } |
| |
| function dc_onmessage_dataview(e) { |
| testPassed("dc_onmessage_dataview was called"); |
| data = e.data; |
| shouldBe("data.byteLength", "10"); |
| array = new Int8Array(e.data); |
| shouldBe("array[0]", "1"); |
| shouldBe("array[9]", "10"); |
| |
| dc_onmessage_sharedarraybuffer_view(); |
| } |
| |
| function dc_onmessage_arraybuffer(e) { |
| testPassed("dc_onmessage_arraybuffer was called"); |
| data = e.data; |
| shouldBe("data.byteLength", "2"); |
| array = new Int8Array(e.data); |
| shouldBe("array[0]", "17"); |
| shouldBe("array[1]", "19"); |
| |
| data = new ArrayBuffer(12); |
| array = new Int8Array(data); |
| array[1]=1; |
| array[10]=10; |
| |
| shouldBe("data.byteLength", "12"); |
| |
| shrunkView = new DataView(data, 1, 10); |
| |
| dc2.onmessage = dc_onmessage_dataview; |
| shouldNotThrow("dc.send(shrunkView);"); |
| } |
| |
| function dc_onmessage_string(e) { |
| testPassed("dc_onmessage_string was called"); |
| data = e.data; |
| shouldBe("data", "'xyzzy'"); |
| |
| dc.binaryType = "arraybuffer"; |
| buffer = new ArrayBuffer(2); |
| var array = new Int8Array(buffer); |
| array[0] = 17; |
| array[1] = 19; |
| dc2.onmessage = dc_onmessage_arraybuffer; |
| shouldNotThrow("dc.send(buffer);"); |
| } |
| |
| function dc_onopen() { |
| testPassed("dc_onopen was called"); |
| shouldBe("dc.readyState", "'open'"); |
| shouldBe("dc.label", "'label'"); |
| } |
| |
| function pc_ondatachannel(e) { |
| testPassed("pc_ondatachannel was called"); |
| dc2 = e.channel; |
| dc2.onmessage = dc_onmessage_string; |
| shouldBe("dc2.label", "'label'"); |
| shouldNotThrow("dc.send('xyzzy');"); |
| } |
| |
| var pc_has_connected = false; |
| function pc_onicechange() { |
| if ((pc.iceConnectionState === "connected" || |
| pc.iceConnectionState === "completed") && |
| !pc_has_connected) { |
| testPassed("pc is connected"); |
| pc_has_connected = true; |
| } |
| } |
| |
| function connect_pcs(pc_a, pc_b) { |
| pc_a.onicecandiate = function(event) { |
| if (event.candidate) { |
| pc_b.addIceCandidate(event.candidate); |
| } |
| } |
| pc_b.onicecandidate = function(event) { |
| if (event.candidate) { |
| pc_a.addIceCandidate(event.candidate); |
| } |
| } |
| pc_a.createOffer() |
| .then(function(offer) { |
| return pc_a.setLocalDescription(offer); |
| }) |
| .then(function() { |
| return pc_b.setRemoteDescription(pc_a.localDescription); |
| }) |
| .then(function() { |
| return pc_b.createAnswer(); |
| }) |
| .then(function(answer) { |
| return pc_b.setLocalDescription(answer); |
| }) |
| .then(function() { |
| return pc_a.setRemoteDescription(pc_b.localDescription); |
| }) |
| } |
| |
| pc = new RTCPeerConnection(); |
| shouldNotThrow('dc = pc.createDataChannel("label1");'); |
| shouldBe("dc.reliable", "true"); |
| shouldNotThrow('dc = pc.createDataChannel("label2", {});'); |
| shouldBe("dc.reliable", "true"); |
| shouldNotThrow('dc = pc.createDataChannel("label3", {ordered:true});'); |
| shouldBe("dc.reliable", "true"); |
| shouldNotThrow('dc = pc.createDataChannel("label3", {ordered:false});'); |
| shouldBe("dc.reliable", "true"); |
| shouldBe("dc.ordered", "false"); |
| shouldNotThrow('dc = pc.createDataChannel("label3", {maxRetransmits:0});'); |
| shouldBe("dc.reliable", "false"); |
| shouldNotThrow('dc = pc.createDataChannel("label3", {maxPacketLifeTime:0});'); |
| shouldBe("dc.reliable", "false"); |
| // Test that older name has stopped affecting the reliability mode. |
| shouldNotThrow('dc = pc.createDataChannel("label3", {maxRetransmitTime:0});'); |
| shouldBe("dc.reliable", "true"); |
| |
| pc = new RTCPeerConnection(); |
| pc2 = new RTCPeerConnection(); |
| // We must offer a datachannel before negotiation to get ICE to start. |
| shouldNotThrow('dc = pc.createDataChannel("label");'); |
| shouldBe("dc.readyState", "'connecting'"); |
| dc.onopen = dc_onopen; |
| connect_pcs(pc, pc2); |
| |
| pc.oniceconnectionstatechange = pc_onicechange; |
| pc2.ondatachannel = pc_ondatachannel; |
| |
| window.jsTestIsAsync = true; |
| window.successfullyParsed = true; |
| </script> |
| </body> |
| </html> |