<!DOCTYPE HTML>
<html>
<head>
  <title>MSE: check that duration change behaves properly</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="mediasource.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">

SimpleTest.waitForExplicitFinish();

runWithMSE(async (ms, v) => {
  await once(ms, "sourceopen");
  const sb = ms.addSourceBuffer("video/webm");

  const arrayBuffer = await fetchWithXHR("seek.webm");
  sb.appendBuffer(new Uint8Array(arrayBuffer, 0, 318));
  await Promise.all([once(v, "loadedmetadata"), once(sb, "updateend")]);
  is(v.duration, ms.duration, "video duration is mediasource one");
  must_not_throw(() => ms.duration = 0, "duration = 0 is valid initially");
  is(v.duration, 0, "reducing duration with no data buffered is valid");
  sb.appendBuffer(new Uint8Array(arrayBuffer, 318));
  // Adding more data will fire durationchange.
  await once(sb, "updateend");
  ok(true, "got updateend");
  // XXX: Duration should be exactly 4.0, see bug 1065207.
  ok(Math.abs(v.duration - 4) <= 0.002, "Video has correct duration");
  must_throw(() => ms.duration = 0,
             "Must use remove for range removal",
             "InvalidStateError");
  ok(Math.abs(v.duration - 4) <= 0.002, "Video has correct duration");
  must_not_throw(() => ms.duration = 10, "setting duration past data is valid");
  is(v.duration, 10, "extending duration is always valid");
  // The last sample has a start time of 3.967000s and a end time of 4.001 (see bug 1065207).
  must_not_throw(() => ms.duration = 3.967000,
                 "setting duration with >= highest frame presentation time is valid");
  is(v.duration, sb.buffered.end(0),
     "duration is the highest end time reported by the buffered attribute ");
  must_not_throw(() => ms.duration = 3.97,
                 "setting duration with >= highest frame presentation time is valid");
  is(v.duration, sb.buffered.end(0),
     "duration is the highest end time reported by the buffered attribute ");
  must_throw(() => ms.duration = 3.96,
             "setting duration with < highest frame presentation time is not valid",
             "InvalidStateError");
  is(v.duration, sb.buffered.end(0),
     "duration is the highest end time reported by the buffered attribute ");
  must_throw(() => ms.duration = -1, "can't set a negative duration", "TypeError");
  sb.remove(sb.buffered.end(0), Infinity);
  is(sb.updating, true, "updating is true");
  must_throw(() => ms.duration = Infinity,
             "setting the duration while updating is not allowed",
             "InvalidStateError");
  must_throw(() => sb.abort(),
             "Can't use abort while range removal is in progress",
             "InvalidStateError");
  is(v.duration, sb.buffered.end(0),
     "duration is the highest end time reported by the buffered attribute ");
  await once(sb, "updateend");
  ms.endOfStream();
  await once(ms, "sourceended");
  SimpleTest.finish();
});

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