summaryrefslogtreecommitdiffstats
path: root/dom/media/autoplay/test/mochitest/test_autoplay_policy_web_audio_createMediaStreamSource.html
blob: 5fe9aa64fc41a51cd9d97a2212fce1a8344f0f4a (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE HTML>
<html>
<head>
  <title>Autoplay policy test : createMediaStreamSource with active stream</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>

/**
 * This test is used to ensure that we would try to start the blocked AudioContext
 * which is blocked by autoplay policy, when it creates a MediaStreamAudioSourceNode
 * which has a active input stream.
 */

SimpleTest.waitForExplicitFinish();

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

  info(`- create 2 AudioContext, one is used to generate active stream, another one is used to test whether it would be resumed after starting MediaStreamAudioSource with active stream -`);
  createAudioContexts();

  info(`- both AudioContext are not allowed to start in beginning -`);
  await audioContextsShouldBeBlocked();

  info(`- using AudioContext2 to create a MediaStreamAudioSourceNode with active stream, which should resume AudioContext2 -`);
  await createMediaStreamAudioSourceByAudioContext2();

  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 createAudioContexts() {
  /* global ac1, ac2 */
  window.ac1 = new AudioContext();
  window.ac2 = new AudioContext();

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

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


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

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

async function audioContextsShouldBeBlocked() {
  await ac1.notAllowedToStart;
  await ac2.notAllowedToStart;
  is(ac1.state, "suspended", `AudioContext1 is blocked.`);
  is(ac2.state, "suspended", `AudioContext2 is blocked.`);
}

async function startAudioContext1() {
  // simulate user gesture in order to start video.
  SpecialPowers.wrap(document).notifyUserGestureActivation();
  ok(await ac1.resume().then(() => true, () => false), `resumed AudioContext1.`);
  await ac1.allowedToStart;
  is(ac1.state, "running", `AudioContext1 is running.`);
}

async function getActiveStream() {
  await startAudioContext1();
  // As AudioContext1 has been resumed, we can use it to create active stream.
  return ac1.createMediaStreamDestination().stream;
}

async function createMediaStreamAudioSourceByAudioContext2() {
  is(ac2.state, "suspended", `AudioContext2 is suspended.`);
  let source = ac2.createMediaStreamSource(await getActiveStream());
  source.connect(ac2.destination);
  await ac2.allowedToStart;
  is(ac2.state, "running", `AudioContext2 is running.`);
}

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>