summaryrefslogtreecommitdiffstats
path: root/dom/media/autoplay/test/mochitest/test_autoplay_policy_web_audio_mediaElementAudioSourceNode.html
blob: 41fab54133b0e699f820c666b24e25b56cb562fc (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE HTML>
<html>
<head>
  <title>Autoplay policy test : use media element as source for web audio</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="manifest.js"></script>
</head>
<body>
<script>
/* import-globals-from ../../../test/manifest.js */
/**
 * This test is used to ensure blocked AudioContext would be resumed when the
 * source media element of MediaElementAudioSouceNode which has been created and
 * connected to destinationnode starts.
 */

SimpleTest.waitForExplicitFinish();

(async function testResumeAudioContextWhenMediaElementSourceStarted() {
  await setupTestPreferences();

  info(`- create audio context -`);
  createAudioContext();

  info(`- AudioContext is not allowed to start in beginning -`);
  await audioContextShouldBeBlocked();

  info(`- create a source for web audio and start the source -`);
  await useMediaElementAsSourceAndPlayMediaElement();

  info(`- AudioContext should be allowed to start after MediaElementAudioSourceNode started -`);
  await audioContextShouldBeAllowedToStart();

  endTest();
})();

/**
 * Test utility functions
 */

function setupTestPreferences() {
  return SpecialPowers.pushPrefEnv({"set": [
    ["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
    ["media.autoplay.blocking_policy", 0],
    ["media.autoplay.block-webaudio", true],
    ["media.autoplay.block-event.enabled", true],
  ]});
}

function createAudioContext() {
  /* global ac */
  window.ac = new AudioContext();

  ac.allowedToStart = new Promise(resolve => {
    ac.addEventListener("statechange", function() {
      if (ac.state === "running") {
        resolve();
      }
    }, {once: true});
  });

  ac.notAllowedToStart = new Promise(resolve => {
    ac.addEventListener("blocked", async function() {
      resolve();
    }, {once: true});
  });
}

async function audioContextShouldBeBlocked() {
  await ac.notAllowedToStart;
  is(ac.state, "suspended", `AudioContext is blocked.`);
}

async function useMediaElementAsSourceAndPlayMediaElement() {
  let video = document.createElement('video');
  video.src = "gizmo.mp4";

  let source = ac.createMediaElementSource(video);
  source.connect(ac.destination);
  // simulate user gesture in order to start video.
  SpecialPowers.wrap(document).notifyUserGestureActivation();
  await playVideo(video);
}

async function playVideo(video) {
  video.play();
  await once(video, "play");
  ok(true, `video started.`);
  removeNodeAndSource(video);
}

async function audioContextShouldBeAllowedToStart() {
  await ac.allowedToStart;
  is(ac.state, "running", `AudioContext is allowed to start.`);
}

function endTest() {
  // reset the activation flag in order not to interfere following test in the
  // verify mode which would run the test using same document couple times.
  SpecialPowers.wrap(document).clearUserGestureActivation();
  SimpleTest.finish();
}

</script>