<!DOCTYPE html>
<html>
    <head>
        <script src="../../resources/js-test-pre.js"></script>
        <script src="resources/promise-utils.js"></script>
    </head>
    <body>
        <script>
            let stream;
            let offer;

            description("Test setting up media between two RTCPeerConnection instances with a single SDP dialog using legacy callbacks API.");

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

            debug("Create RTCPeerConnection A");
            const pcA = new RTCPeerConnection({iceServers:[{urls:'stun:foo.com'}]});

            debug("Create RTCPeerConnection B");
            const pcB = new RTCPeerConnection({iceServers:[{urls:'stun:foo.com'}]});

            pcA.ontrack = function () {
                debug("----------");
                testPassed("A: got remote track event");
                debug("----------");
            };

            pcB.ontrack = function () {
                debug("----------");
                testPassed("B: got remote track event");
                debug("----------");
            };

            navigator.mediaDevices.getUserMedia({ "audio": true })
            .then(function (s) {
                stream = s;

                return startTestWithMedia();
            })
            .catch(function (error) {
                testFailed(`Unable to get user media: ${error}`);
                finishJSTest();
            });

            function startTestWithMedia() {
                debug("A: add media");
                pcA.addTrack(stream.getAudioTracks()[0], stream);

                debug("A: create offer");
                return pcA.createOffer().then(offer => {
                    debug("A: got offer, set it as local description");
                    return pcA.setLocalDescription(offer).then(() => {
                        debug("A: local offer set");
                        shouldBe("pcA.signalingState", "'have-local-offer'");

                        debug("A: send offer to B");
                        debug("A --- offer --> B");
                        return offerToPcB(pcA.localDescription);
                    }, gotError);

                }, gotError);
            }

            function offerToPcB(offer) {
                debug("B: got offer from A, set it as remote description");
                return pcB.setRemoteDescription(offer).then(() => {
                    debug("B: remote offer set");
                    shouldBe("pcB.signalingState", "'have-remote-offer'");

                    debug("B: add media (to include in answer)");
                    pcB.addTrack(stream.getAudioTracks()[0], stream);

                    debug("B: create answer");
                    return pcB.createAnswer().then(answer => {
                        debug("B: got answer, set it as local description");
                        return pcB.setLocalDescription(answer).then(() => {
                            debug("B: local answer set");
                            shouldBe("pcB.signalingState", "'stable'");

                            debug("B: send answer to A");
                            debug("A <-- answer -- B");
                            return answerToA(pcB.localDescription);
                        }, gotError);
                    }, gotError);
                }, gotError);
            }

            function answerToA(answer) {
                debug("A: got answer from B, set it as remote description");
                return pcA.setRemoteDescription(answer).then(() => {
                    debug("A: remote answer set");
                    shouldBe("pcA.signalingState", "'stable'");

                    testPassed("Offer/answer dialog completed")
                    debug("");

                    finishJSTest();
                }, gotError);
            }

            function gotError() {
                testFailed("Unexpected error callback fired");
            }

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

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