<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Testing description getters after connection is closed</title>
        <script src="../resources/testharness.js"></script>
        <script src="../resources/testharnessreport.js"></script>
        <script src="routines.js"></script>
    </head>
    <body>
        <script>
function closedConnection()
{
    return new Promise((resolve, reject) => {
        var localConnection = new RTCPeerConnection();
        localConnection.close();
        setTimeout(() => resolve(localConnection), 100);
    });
}

promise_test(() => {
    return closedConnection().then((localConnection) => {
        assert_equals(localConnection.currentLocalDescription, localConnection.localDescription);
        assert_equals(localConnection.pendingLocalDescription, localConnection.localDescription);

        assert_equals(localConnection.currentRemoteDescription, localConnection.remoteDescription);
        assert_equals(localConnection.pendingRemoteDescription, localConnection.remoteDescription);
    });
}, "Ensuring closed connection getters do not crash");

promise_test((test) => {
    return closedConnection().then((connection) => {
        return promise_rejects_dom(test, 'InvalidStateError', connection.createOffer());
    });
}, "Ensuring closed connection createOffer does not crash");

promise_test(() => {
    return closedConnection().then((connection) => {
        assert_throws_dom("InvalidStateError", () => {  connection.createDataChannel("test"); });
    });
}, "Ensuring closed connection createDataChannel does not crash");

promise_test(() => {
    return closedConnection().then((connection) => {
        assert_throws_dom("InvalidStateError", () => { connection.addTransceiver("video"); });
    });
}, "Ensuring closed connection addTransceiver does not crash");

promise_test(() => {
    return closedConnection().then((connection) => {
        return navigator.mediaDevices.getUserMedia({video: true}).then((stream) => {
            assert_throws_dom("InvalidStateError", () => { connection.addTrack(stream.getVideoTracks()[0], stream); });
        });
    });
}, "Ensuring closed connection addTrack does not crash");

promise_test((test) => {
    var connection;
    return closedConnection().then((pc) => {
        connection = pc;
        var pc2 = new RTCPeerConnection();
        pc2.createDataChannel("test");
        return pc2.createOffer();
    }).then((sdp) => {
        return promise_rejects_dom(test, 'InvalidStateError', connection.setRemoteDescription(sdp));
    });
}, "Ensuring closed connection setRemoteDescription does not crash");

promise_test((test) => {
    var localConnection = new RTCPeerConnection();
    return localConnection.createOffer().then((sdp) => {
        localConnection.close();
        return new Promise((resolve, reject) => {
            setTimeout(() => resolve(sdp), 100);
        });
    }).then((sdp) => {
        return promise_rejects_dom(test, 'InvalidStateError', localConnection.setLocalDescription(sdp));
    });
}, "Ensuring closed connection setLocalDescription does not crash");

promise_test((test) => {
    var localConnection = new RTCPeerConnection();
    var remoteConnection = new RTCPeerConnection();
    remoteConnection.createDataChannel("test");
    return remoteConnection.createOffer().then((sdp) => {
        return localConnection.setRemoteDescription(sdp);
    }).then(() => {
        localConnection.close();
    }).then(() => {
        return promise_rejects_dom(test, 'InvalidStateError', localConnection.createAnswer());
    });
}, "Ensuring closed connection createAnswer does not crash");

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