<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Testing two data channels in the same peer connection</title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
  </head>
  <body>
    <script src ="../routines.js"></script>
    <script>
var counter = 0;
function receiveMessages(event) {
    if (++counter === 4)
        finishTest();
}

function sendMessages1(channel)
{
    channel.send("one");
    channel.send("two");
}

function sendMessages2(channel)
{
    channel.send("three");
    channel.send("four");
}

var finishTest;
promise_test((test) => {
    counter = 0;
    return new Promise((resolve, reject) => {
        finishTest = resolve;
        createConnections((localConnection) => {
            var localChannel1 = localConnection.createDataChannel('channel1');
            var localChannel2 = localConnection.createDataChannel('channel2');
            localChannel1.onopen = () => { sendMessages1(localChannel1); };
            localChannel2.onopen = () => { sendMessages2(localChannel2); };
        }, (remoteConnection) => {
            remoteConnection.ondatachannel = (event) => {
                event.channel.onmessage = receiveMessages;
            };
        });
        setTimeout(() => { reject("Test timed out"); }, 5000);
    });
}, "Using two data channels in the same peer connection");

    </script>
  </body>
</html>
