summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_suspend_media_by_inactive_docshell.html
blob: 7f819ca33b8e1239a767c3c9837737ee158ee8c3 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test suspending media by inactive docShell</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="manifest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<video id="testVideo" src="gizmo.mp4" loop></video>
<script class="testbody" type="text/javascript">
/**
 * When calling `browser.suspendMediaWhenInactive`, it can set the docShell's
 * corresponding flag that is used to suspend media when the docShell is
 * inactive. This test is used to check if we can suspend/resume the media
 * correctly when changing docShell's active state.
 */
async function startTest() {
  const video = document.getElementById("testVideo");

  info(`start video`);
  await video.play();

  info(`set docShell inactive which would suspend media`);
  await setDocShellActive(false);

  info(`set docShell active which would resume media`);
  await setDocShellActive(true);

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv(
  {"set": [["media.testing-only-events", true]]}, startTest);

/**
 * The following are test helper functions.
 */
function mediaSuspendedStateShouldEqualTo(expected) {
  const video = document.getElementById("testVideo");
  const result = SpecialPowers.wrap(video).isSuspendedByInactiveDocOrDocShell;
  is(result, expected, `media's suspended state is correct`);
}

function setDocShellActive(isActive) {
  const win = SpecialPowers.wrap(window);
  const docShell = win.docShell;
  const browsingContext = win.browsingContext;
  // This flag is used to prevent media from playing when docShell is inactive.
  browsingContext.top.suspendMediaWhenInactive = true;
  browsingContext.isActive = isActive;
  // After updating `docshell.isActive`, it would suspend/resume media and we
  // wait suspending/resuming finishing by listening to `MozMediaSuspendChanged`
  return new Promise(r => {
    docShell.chromeEventHandler.addEventListener("MozMediaSuspendChanged",
      () => {
        mediaSuspendedStateShouldEqualTo(!isActive);
        r();
      }, {once : true}
    );
  });
}

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