<!DOCTYPE HTML>
<html>
<head>
  <title>MSE: verify correct frames selected for given position</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");

  let arrayBuffer = await fetchWithXHR("seek.webm");
  let p = once(v, "loadedmetadata");
  // Append entire file covering range [0, 4].
  sb.appendBuffer(new Uint8Array(arrayBuffer));
  await p;
  is(v.currentTime, 0, "currentTime has correct initial value");
  is(v.videoWidth, 320, "videoWidth has correct initial value");
  is(v.videoHeight, 240, "videoHeight has correct initial value");

  arrayBuffer = await fetchWithXHR("seek_lowres.webm");
  // Append initialization segment.
  info("Appending low-res init segment");
  p = once(sb, "updateend");
  sb.appendBuffer(new Uint8Array(arrayBuffer, 0, 438));
  await p;

  info("Appending low-res range [2,4]");
  // Append media segment covering range [2, 4].
  p = once(sb, "updateend");
  sb.appendBuffer(new Uint8Array(arrayBuffer, 51003));
  await p;

  ms.endOfStream();

  info("Seeking to t=3");
  p = Promise.all([once(v, "seeked"), once(v, "resize")]);
  v.currentTime = 3;
  await p;
  is(v.currentTime, 3, "Video currentTime at target");
  is(v.videoWidth, 160, "videoWidth has correct low-res value");
  is(v.videoHeight, 120, "videoHeight has correct low-res value");

  info("Seeking to t=1");
  p = Promise.all([once(v, "seeked"), once(v, "resize")]);
  v.currentTime = 1;
  await p;
  is(v.currentTime, 1, "Video currentTime at target");
  is(v.videoWidth, 320, "videoWidth has correct high-res value");
  is(v.videoHeight, 240, "videoHeight has correct high-res value");
  SimpleTest.finish();
});

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