<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Testing basic binary data channel from offerer to receiver</title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
  </head>
  <body>
    <script src ="../routines.js"></script>
    <script>
var localChannel;
var remoteChannel;

function closeDataChannels() {
    localChannel.close();
    remoteChannel.close();
    closeConnections();
}

function createArrayBuffer(length)
{
    var array = new Uint8Array(length);
    for (var cptr = 0; cptr < length; cptr++)
        array[cptr] = cptr + 1;
    return array;
}

function createArrayBufferView(length)
{
    return createArrayBuffer(2 * length).subarray(length, 2 * length);
}

function checkArrayBuffer(array, length)
{
    if (array.byteLength !== length)
        return false;

    var a = new Uint8Array(array);
    for (var cptr = 0; cptr < length; cptr++) {
        if (a[cptr] !== cptr + 1)
             return false;
    }
    return true;
}

function checkArrayBufferView(array, length)
{
    if (array.byteLength !== length)
        return false;

    var a = new Uint8Array(array);
    for (var cptr = 0; cptr < length; cptr++) {
        if (a[cptr] !== cptr + length + 1)
             return false;
    }
    return true;
}

function testArrayBuffer(array, length)
{
    test(() => {
        assert_true(checkArrayBuffer(array, length));
    }, "test array buffer " + length);
}

function testArrayBufferView(array, length)
{
    test(() => {
        assert_true(checkArrayBufferView(array, length));
    }, "test array buffer view " + length);
}

function receiveMessages(event) {
    try {
        if (++counter === 1)
            testArrayBuffer(event.data, 1);
        else if (counter === 2)
            testArrayBuffer(event.data, 2);
        else if (counter === 3)
            testArrayBuffer(event.data, 3);
        else if (counter === 4)
            testArrayBuffer(event.data, 4);
        else if (counter === 5)
            testArrayBufferView(event.data, 1);
        else if (counter === 6)
            testArrayBufferView(event.data, 2);
        else if (counter === 7)
            testArrayBufferView(event.data, 3);
        else if (counter === 8) {
            testArrayBufferView(event.data, 4);
            closeDataChannels();
            finishTest();
        } else
            assert_unreached();
    } catch(e) {
        console.log(e);
    }
}

function sendMessages(channel)
{
    channel.send(createArrayBuffer(1));
    channel.send(createArrayBuffer(2));
    channel.send(createArrayBuffer(3));
    channel.send(createArrayBuffer(4));
    channel.send(createArrayBufferView(1));
    channel.send(createArrayBufferView(2));
    channel.send(createArrayBufferView(3));
    channel.send(createArrayBufferView(4));
}

var finishTest;
promise_test((test) => {
    counter = 0;
    return new Promise((resolve, reject) => {
        finishTest = resolve;
        createConnections((localConnection) => {
            localChannel = localConnection.createDataChannel('sendDataChannel');
            localChannel.binaryType = "arraybuffer";
            localChannel.onopen = () => { sendMessages(localChannel) };
        }, (remoteConnection) => {
            remoteConnection.ondatachannel = (event) => {
                remoteChannel = event.channel;
                remoteChannel.binaryType = "arraybuffer";
                remoteChannel.onmessage = receiveMessages;
            };
        });
        setTimeout(() => { reject("Test timed out"); }, 5000);
    });
}, "Basic binary data channel exchange from offerer to receiver");

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