<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
description("Tests RTCPeerConnection remoteDescription.");

var pc = null;

function requestFailed2()
{
    testPassed('requestFailed was called.');

    shouldBeEqualToString('pc.remoteDescription.type', "answer");
    shouldBeEqualToString('pc.remoteDescription.sdp', "remote");
    pc.close();
    shouldBeEqualToString('pc.remoteDescription.type', "answer");
    shouldBeEqualToString('pc.remoteDescription.sdp', "remote");

    finishJSTest();
}

function requestSucceeded1()
{
    testPassed('requestSucceeded was called.');

    sessionDescription = new RTCSessionDescription({type:"offer", sdp:"local"});
    shouldNotThrow('pc.setRemoteDescription(sessionDescription, unexpectedCallback, requestFailed2);');
}

function unexpectedCallback()
{
    testFailed('unexpectedCallback was called.');
    finishJSTest();
}

function expectedTypeError(error)
{
    window.error = error;
    shouldBe('error.name', '"TypeError"');
    testPassed('expectedTypeError was called.');
}

function expectedInvalidSessionDescription(error)
{
    window.error = error;
    shouldBe('error.name', '"OperationError"');
    testPassed('expectedInvalidSessionDescription was called.');
}

function testExecutionOrderClosedConnection()
{
    var localPeerConnection = new webkitRTCPeerConnection(null, null);
    localPeerConnection.close();
    var counter = 0;
    window.events = [];
    Promise.resolve().then(_ => events[counter++] = 1);
    var sessionDescription = new RTCSessionDescription({type:"answer", sdp:"remote"});
    localPeerConnection.setRemoteDescription(sessionDescription, unexpectedCallback, error => {
        window.error = error;
        shouldBe('error.name', '"InvalidStateError"');
        shouldBe('error.toString()', '"InvalidStateError: The RTCPeerConnection\'s signalingState is \'closed\'."');
        events[counter++] = 2;
    });
    Promise.resolve().then(_ => {
        events[counter++] = 3; 
        shouldBe('events', '[1,2,3]');
    });
}

shouldNotThrow('testExecutionOrderClosedConnection()');
pc = new webkitRTCPeerConnection(null, null);
shouldNotThrow('pc.setRemoteDescription().catch(expectedTypeError)');
shouldNotThrow('pc.setRemoteDescription(null).catch(expectedInvalidSessionDescription)');
var sessionDescription = new RTCSessionDescription({type:"answer", sdp:"remote"});
shouldNotThrow('pc.setRemoteDescription(sessionDescription, requestSucceeded1, unexpectedCallback);');

window.jsTestIsAsync = true;
window.successfullyParsed = true;
</script>
</body>
</html>