<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Testing basic data channel creation</title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
  </head>
  <body>
    <script>
var longString = "abcdefgh";
for (var cptr = 0; cptr < 16; ++cptr)
    longString += longString;

test(() => {
    var pc = new RTCPeerConnection();
    assert_throws_js(TypeError, () => { pc.createDataChannel(longString); });
}, "Creating data channel with very long label");

test(() => {
    var pc = new RTCPeerConnection();
    pc.close();
    assert_throws_dom("InvalidStateError", () => { pc.createDataChannel('sendDataChannel'); });
}, "Creating data channel after closing the connection");

function testWrongDataChannelInit(init, title)
{
    return test(() => {
        var pc = new RTCPeerConnection();
        assert_throws_js(TypeError, () => { pc.createDataChannel('sendDataChannel', init); });
    }, "Wrong data channel init: " + title);
}

function testRightDataChannelInit(init, title)
{
    return test(() => {
        var pc = new RTCPeerConnection();
        channel = pc.createDataChannel('sendDataChannel', init);

        if (!init)
            init = { };
        if (init.ordered === undefined)
            init.ordered = true;
        if (init.maxPacketLifeTime === undefined)
            init.maxPacketLifeTime = null;
        if (init.maxRetransmits === undefined)
            init.maxRetransmits = null;
        if (init.protocol === undefined)
            init.protocol = "";
        if (init.negotiated === undefined)
            init.negotiated = false;
        if (init.id === undefined)
            init.id = null;

        assert_equals(channel.ordered, init.ordered, "ordered");
        assert_equals(channel.maxPacketLifeTime, init.maxPacketLifeTime, "maxPacketLifeTime");
        assert_equals(channel.maxRetransmitTime, init.maxRetransmitTime, "maxRetransmitTime");
        assert_equals(channel.maxRetransmits, init.maxRetransmits, "maxRetransmits");
        assert_equals(channel.protocol, init.protocol, "protocol");
        assert_equals(channel.negotiated, init.negotiated, "negotiated");
        if (channel.negotiated)
            assert_equals(channel.id, init.id, "id");
     }, "Right data channel init: " + title);
}

testWrongDataChannelInit({negotiated: false, protocol: longString}, "long protocol");
testWrongDataChannelInit({negotiated: true, id: 65535}, "long id");
testWrongDataChannelInit({maxPacketLifeTime: 1, maxRetransmits: 1}, "maxPacketLifeTime and maxRetransmits");

testRightDataChannelInit({id: 65535}, "long id, but negotiated is false");
testRightDataChannelInit({ordered: true, maxRetransmit: 11, protocol: "whatever", negotiated: false, id: 1 }, "ordered init");
testRightDataChannelInit({ordered: false, maxPacketLifeTime: 10, protocol: "whatever", negotiated: false, id: 2 }, "unordered init");
testRightDataChannelInit({protocol: "whatever", negotiated: false, id: 123 }, "default ordered init with id, not negotiated");
testRightDataChannelInit({protocol: "whatever", negotiated: true, id: 123 }, "default ordered init with id, negotiated");
testRightDataChannelInit(undefined, "no init");
testRightDataChannelInit(null, "null");
    </script>
  </body>
</html>
