summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_background_video_suspend_ready_state.html
blob: af5a4811efa6890f5e527ef47d34511e45d9c08f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE HTML>
<html>
<head>
<title>Check element's ready state while suspending video decoding</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="manifest.js"></script>
<script src="background_video.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript">

/**
 * This test is used to ensure that the media element's ready state won't be
 * incorrectly changed to `HAVE_METADATA` or lower when seeking happens on a
 * null decoder that is caused by suspending video decoding.
 */
add_task(async function setupTestingPref() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["media.test.video-suspend", true],
      ["media.suspend-bkgnd-video.enabled", true],
      ["media.suspend-bkgnd-video.delay-ms", 0],
    ],
  });
});

add_task(async function testReadyStateWhenSuspendingVideoDecoding() {
  const video = await createPlayingVideo();
  assertVideoReadyStateIsSufficientForPlaying(video);
  await suspendVideoDecoding(video);
  await seekVideo(video);
  assertVideoReadyStateIsSufficientForPlaying(video);
});

async function createPlayingVideo() {
  let video = document.createElement("video");
  video.src = "gizmo.mp4";
  video.controls = true;
  video.loop = true;
  document.body.appendChild(video);
  ok(await video.play().then(_=>true, _=>false), "video started playing");
  return video;
}

function assertVideoReadyStateIsSufficientForPlaying(video) {
  ok(video.readyState > HTMLMediaElement.HAVE_METADATA,
    `Ready state ${video.readyState} is suffient for playing`);
}

async function suspendVideoDecoding(video) {
  video.setVisible(false);
  nextVideoSuspends(video);
  info(`suspended video decoding`);
  // Because the suspend event we received can only indicate MDSM finishes the
  // setup of suspending decoding, but the actual setting the null decoder
  // happens on MediaFormatReader where we are not able to observe the behavior
  // via event. Instread of implementing something in MFR to propagate the event
  // or changing the timing of current suspend event, which might increase the
  // maintenance effect, Here choose to use a simple workaround to wait for a
  // short while to ensure the decoding has been switched to the null decoder.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 2000));
}

async function seekVideo(video) {
  video.currentTime = 0;
  ok(await new Promise(r => video.onseeked = r).then(_=>true, _=>false),
     `seeking completed`);
}

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