<!DOCTYPE html>
<html>
    <head>
        <script src="../../resources/js-test-pre.js"></script>
        <script src="./resources/getUserMedia-helper.js"></script>
        <script>

            var stream1;
            var stream2;
            var audioTrack;
            var videoTrack;

            function tryAddTrack(stream, track)
            {
                try {
                    stream.addTrack(track);
                } catch (exception) {
                    testFailed("addTrack threw an exception.");
                    finishJSTest();
                }
            }

            function shouldNotFireActive()
            {
                testFailed("stream2 should not become active if an ended track is added");
            }

            function shouldFireActive()
            {
                debug("*** stream2 is active again");
                shouldBe('stream2.active', 'true');
                finishJSTest();
            }

            function continueTest()
            {
                debug("*** extract videoTrack (and verify liveness)");
                videoTrack = stream1.getVideoTracks()[0];
                shouldBeEqualToString('videoTrack.readyState', 'live');

                debug("*** add live videoTrack to stream2");
                stream2.onactive = shouldFireActive;
                tryAddTrack(stream2, videoTrack);
            }

            function createStreamAndAddTracks()
            {
                debug("*** extract audioTrack");
                audioTrack = stream1.getAudioTracks()[0];
                debug("*** stop audioTrack");
                audioTrack.stop();
                shouldBeEqualToString('audioTrack.readyState', 'ended');

                debug("*** construct new 'empty' stream2")
                stream2 = new MediaStream();
                shouldBe('stream2.getTracks().length', '0');
                shouldBe('stream2.active', 'false');

                debug("*** add ended audioTrack to stream2");
                stream2.onactive = shouldNotFireActive;
                tryAddTrack(stream2, audioTrack);

                debug("*** schedule continuation of test");
                setTimeout(continueTest);
            }

            function gotStream(s)
            {
                stream1 = s;
                shouldBe('stream1.getAudioTracks().length', '1');
                shouldBe('stream1.getVideoTracks().length', '1');
                createStreamAndAddTracks();
            }

            function startMedia()
            {
                description("Test adding tracks to inactive MediaStream.");
                getUserMedia("allow", {audio:true, video:true}, gotStream);
            }
            window.jsTestIsAsync = true;
            window.successfullyParsed = true;
        </script>
        <script src="../../resources/js-test-post.js"></script>
    </head>
    <body onload="startMedia()">
        <p id="description"></p>
        <div id="console"></div>
    </body>
</html>
