<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <title>MediaSource.readyState equals "closed" test cases.</title>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <script src="mediasource-util.js"></script>
    </head>
    <body>
        <div id="log"></div>
        <script>
          test(function ()
          {
              var mediaSource = new MediaSource();
              assert_equals(mediaSource.sourceBuffers.length, 0, "sourceBuffers is empty");
              assert_equals(mediaSource.activeSourceBuffers.length, 0, "activeSourceBuffers is empty");
              assert_equals(mediaSource.readyState, "closed", "readyState is 'closed'");
              assert_true(isNaN(mediaSource.duration), "duration is NaN");
          }, "Test attribute values on a closed MediaSource object.");

          test(function ()
          {
              var mediaSource = new MediaSource();
              assert_throws_dom("InvalidStateError",
                  function() { mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE); },
                  "addSourceBuffer() throws an exception when closed.");
          }, "Test addSourceBuffer() while closed.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

              // Setup a handler to run when the MediaSource closes.
              mediaSource.addEventListener('sourceclose', test.step_func(function (event)
              {
                  assert_equals(mediaSource.sourceBuffers.length, 0, "sourceBuffers is empty");
                  assert_equals(mediaSource.activeSourceBuffers.length, 0, "activeSourceBuffers is empty");
                  assert_equals(mediaSource.readyState, "closed", "readyState is 'closed'");
                  assert_throws_dom("NotFoundError",
                      function() { mediaSource.removeSourceBuffer(sourceBuffer); },
                      "removeSourceBuffer() throws an exception when closed.");
                  test.done();
              }));

              // Trigger the MediaSource to close.
              mediaElement.src = "";
          }, "Test removeSourceBuffer() while closed.");

          test(function ()
          {
              var mediaSource = new MediaSource();
              assert_throws_dom("InvalidStateError",
                  function() { mediaSource.endOfStream(); },
                  "endOfStream() throws an exception when closed.");
          }, "Test endOfStream() while closed.");

          test(function ()
          {
              var mediaSource = new MediaSource();
              assert_throws_dom("InvalidStateError",
                  function() { mediaSource.endOfStream("decode"); },
                  "endOfStream(decode) throws an exception when closed.");
          }, "Test endOfStream(decode) while closed.");

          test(function ()
          {
              var mediaSource = new MediaSource();
              assert_throws_dom("InvalidStateError",
                  function() { mediaSource.endOfStream("network"); },
                  "endOfStream(network) throws an exception when closed.");
          }, "Test endOfStream(network) while closed.");

          test(function ()
          {
              var mediaSource = new MediaSource();
              assert_throws_dom("InvalidStateError",
                  function() { mediaSource.duration = 10; },
                  "Setting duration throws an exception when closed.");
          }, "Test setting duration while closed.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

              assert_equals(mediaSource.readyState, "open", "readyState is 'open'");
              // Setup a handler to run when the MediaSource closes.
              mediaSource.addEventListener("sourceclose", test.step_func(function (event)
              {
                  assert_equals(mediaSource.readyState, "closed", "readyState is 'closed'");
                  assert_throws_dom("InvalidStateError",
                      function() { mediaSource.duration = 10; },
                      "Setting duration when closed throws an exception");
                  test.done();
              }));

              // Trigger the MediaSource to close.
              mediaElement.src = "";
          }, "Test setting duration while open->closed.");

           mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

              assert_equals(mediaSource.readyState, "open", "readyState is 'open'");
              // Setup a handler to run when the MediaSource closes.
              mediaSource.addEventListener("sourceclose", test.step_func(function (event)
              {
                  assert_equals(mediaSource.readyState, "closed", "readyState is 'closed'");
                  assert_true(isNaN(mediaSource.duration), "duration is NaN");
                  test.done();
              }));

              // Trigger the MediaSource to close.
              mediaElement.src = "";
          }, "Test getting duration while open->closed.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

              assert_equals(mediaSource.readyState, "open", "readyState is open");

              // Setup a handler to run when the MediaSource closes.
              mediaSource.addEventListener("sourceclose", test.step_func(function (event)
              {
                  assert_equals(mediaSource.readyState, "closed", "readyState is closed");
                  assert_throws_dom("InvalidStateError",
                    function() { sourceBuffer.abort(); },
                    "sourceBuffer.abort() throws INVALID_STATE_ERROR");
                  test.done();
              }));

              // Trigger the MediaSource to close.
              mediaElement.src = "";
          }, "Test sourcebuffer.abort when closed.");

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