<!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;
            var allTracks;
            var id;

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

            function createStreamAndAddTracks()
            {
                shouldBe('stream2.getTracks().length', '2');

                debug('getAudioTracks(), getVideoTracks() and getTracks() must return a new sequence at every invocation');
                shouldBeTrue('stream2.getAudioTracks() != stream2.getAudioTracks()');
                shouldBeTrue('stream2.getVideoTracks() != stream2.getVideoTracks()');
                shouldBeTrue('stream2.getTracks() != stream2.getTracks()');

                audioTrack = stream1.getAudioTracks()[0];
                tryAddTrack(stream2, audioTrack);
                shouldBe('stream2.getAudioTracks().length', '2');
                shouldBe('stream2.getVideoTracks().length', '1');
                shouldBe('stream2.getTracks().length', '3');
                videoTrack = stream1.getVideoTracks()[0];
                tryAddTrack(stream2, videoTrack);
                shouldBe('stream2.getAudioTracks().length', '2');
                shouldBe('stream2.getVideoTracks().length', '2');
                allTracks = stream2.getTracks();
                shouldBe('allTracks.length', '4');
                allTracks.forEach(function (track) {
                    id = track.id;
                    shouldBeNonNull('stream2.getTrackById(id)');
                })
                shouldBeNull('stream2.getTrackById("foo-id")');

                finishJSTest();
            }

            function gotStream2(s)
            {
                stream2 = s;
                createStreamAndAddTracks();
            }

            function gotStream1(s)
            {
                stream1 = s;
                shouldBe('stream1.getTracks().length', '2');
                getUserMedia("allow", {audio:true, video:true}, gotStream2);
            }

            function startMedia()
            {
                description("Test the different methods to get tracks from a MediaStream.");
                getUserMedia("allow", {audio:true, video:true}, gotStream1);
            }
            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>
