<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
    <head>
        <script src="../../resources/js-test-pre.js"></script>
        <script src="resources/promise-utils.js"></script>
    </head>
    <body>
        <script>
            if (window.internals)
                internals.useMockRTCPeerConnectionFactory("");

            var stream;
            var audioTrack;
            var videoTrack;
            var firstOffer;
            var audioTransceiver;
            var videoTransceiver;

            description("Test RTCPeerConnection.setLocalDescription called with an RTCSessionDescription of type 'offer'");

            if (window.testRunner)
                testRunner.setUserMediaPermission(true);
            else {
                debug("This test can not be run without the testRunner");
                finishJSTest();
            }

            var pc = new RTCPeerConnection({iceServers:[{urls:'stun:foo.com'}]});

            shouldBe("pc.signalingState", "'stable'");

            shouldBeNull("pc.localDescription");
            shouldBeNull("pc.pendingLocalDescription");
            shouldBeNull("pc.currentLocalDescription");
            debug("");

            navigator.mediaDevices.getUserMedia({ "audio": true, "video": true })
            .then(function (s) {
                testPassed("Got stream");
                stream = s;
                audioTrack = stream.getAudioTracks()[0];
                videoTrack = stream.getVideoTracks()[0];

                debug("*** Add audioTrack.");
                pc.addTrack(audioTrack, stream);

                shouldBe("pc.getTransceivers().length", "1");
                audioTransceiver = pc.getTransceivers()[0];
                shouldBeNull("audioTransceiver.mid");
                debug("");

                return pc.createOffer();
            })
            .then(function (offer) {
                testPassed("Got firstOffer");
                firstOffer = offer;

                shouldBe("pc.signalingState", "'stable'");
                shouldBeNull("audioTransceiver.mid");
                debug("");

                return pc.setLocalDescription(firstOffer);
            })
            .then(function () {
                testPassed("firstOffer set as local description");

                shouldBe("pc.signalingState", "'have-local-offer'");

                shouldBe("pc.localDescription", "firstOffer");
                shouldBe("pc.pendingLocalDescription", "firstOffer");
                shouldBeNull("pc.currentLocalDescription");

                shouldBe("firstOffer.type", "pc.localDescription.type");
                shouldBe("firstOffer.sdp", "pc.localDescription.sdp");

                shouldBeTrue("typeof pc.getTransceivers()[0].mid == 'string'");

                debug("Set firstOffer as local description again.");
                debug("");

                return pc.setLocalDescription(firstOffer);
            })
            .then(function () {
                testPassed("firstOffer set as local description (again)");

                shouldBe("pc.signalingState", "'have-local-offer'");
                debug("");

                debug("*** Try setting local descriptions with bad types for the current state");
                return promiseShouldReject("pc.setLocalDescription({type:'answer', sdp:firstOffer.sdp});");
            })
            .then(function () {
                return promiseShouldReject("pc.setLocalDescription({type:'pranswer', sdp:firstOffer.sdp});");
            })
            .then(function () {
                debug("");

                debug("*** Add videoTrack");
                pc.addTrack(videoTrack, stream);

                shouldBe("pc.getTransceivers().length", "2");

                debug("*** Find the transceiver for the newly added video track");
                var transceivers = pc.getTransceivers();
                videoTransceiver = transceivers[1].sender.track.kind == "video" ? transceivers[1] : transceivers[0];
                shouldBeDefined("videoTransceiver");

                shouldBeNull("videoTransceiver.mid");
                debug("");

                return pc.createOffer();
            })
            .then(function (offer) {
                testPassed("Got secondOffer");
                secondOffer = offer;

                shouldBe("pc.signalingState", "'have-local-offer'");
                shouldBeNull("videoTransceiver.mid");
                debug("");

                return pc.setLocalDescription(secondOffer);
            })
            .then(function () {
                testPassed("secondOffer set as local description");

                shouldBe("pc.signalingState", "'have-local-offer'");

                shouldBe("pc.localDescription", "secondOffer");
                shouldBe("pc.pendingLocalDescription", "secondOffer");
                shouldBeNull("pc.currentLocalDescription");

                shouldBe("secondOffer.type", "pc.localDescription.type");
                shouldBe("secondOffer.sdp", "pc.localDescription.sdp");

                shouldBeTrue("typeof videoTransceiver.mid == 'string'");

                finishJSTest();
            })
            .catch(function (error) {
                testFailed("Error caught in promise chain: " + error);
                finishJSTest();
            });

            window.jsTestIsAsync = true;
            window.successfullyParsed = true;

        </script>
        <script src="../../resources/js-test-post.js"></script>
    </body>
</html>
