<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <script src="/common/media.js"></script>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            async_test(function(test)
            {
                var video = document.createElement("video");

                // Create an out-of-band text track by adding a track element.
                var trackElement = document.createElement('track');

                trackElement.addEventListener("error", test.step_func(function()
                {
                    assert_unreached("'error' event on track element should not fire.")
                }));

                video.appendChild(trackElement);
                trackElement.src = 'resources/webvtt-file.vtt';
                trackElement.track.mode = 'hidden';

                assert_equals(video.textTracks.length, 1);
                var outOfBandTrack = video.textTracks[0];

                // Load a media file with an inband text track.
                var inbandTrack = null;
                var url = "resources/vp8-vorbis-webvtt.webm"

                var firstAddTrackHandler = test.step_func(function()
                {
                    assert_equals(event.target, video.textTracks);
                    assert_equals(event instanceof window.TrackEvent, true);
                    if (event.track == outOfBandTrack) {
                        return;
                    }

                    assert_equals(inbandTrack, null);
                    assert_equals(video.textTracks.length, 2);
                    assert_equals(event.track, video.textTracks[1]);
                    inbandTrack = event.track;

                    video.textTracks.removeEventListener("addtrack", firstAddTrackHandler);

                    // Clear .src to force the inband track to get destroyed.
                    video.src = "";

                    // Verify that the inband track was removed.
                    assert_not_equals(inbandTrack, null);
                    assert_equals(video.textTracks.length, 1);
                    assert_equals(video.textTracks[0], outOfBandTrack);

                    // Load the URL again to trigger another 'addtrack' event to make sure
                    // no 'removetrack' event was queued.
                    video.src = url;
                    video.textTracks.addEventListener("addtrack", test.step_func(function()
                    {
                        assert_equals(video.textTracks.length, 2);
                        test.done();
                    }));
                });
                video.textTracks.addEventListener("addtrack", firstAddTrackHandler);

                video.textTracks.addEventListener("removetrack", test.step_func(function()
                {
                    assert_unreached("'removetrack' event should not fire.")
                }));

                video.src = url;
            }, "Tests that the 'removetrack' event is NOT fired for inband TextTrack on a failed load.");

        </script>
    </body>
</html>
