tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_autoplay_policy_web_audio_mediaElementAudioSourceNode.html (2966B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Autoplay policy test : use media element as source for web audio</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7  <script type="text/javascript" src="manifest.js"></script>
      8 </head>
      9 <body>
     10 <script>
     11 /* import-globals-from ../../../test/manifest.js */
     12 /**
     13 * This test is used to ensure blocked AudioContext would be resumed when the
     14 * source media element of MediaElementAudioSouceNode which has been created and
     15 * connected to destinationnode starts.
     16 */
     17 
     18 SimpleTest.waitForExplicitFinish();
     19 
     20 (async function testResumeAudioContextWhenMediaElementSourceStarted() {
     21  await setupTestPreferences();
     22 
     23  info(`- create audio context -`);
     24  createAudioContext();
     25 
     26  info(`- AudioContext is not allowed to start in beginning -`);
     27  await audioContextShouldBeBlocked();
     28 
     29  info(`- create a source for web audio and start the source -`);
     30  await useMediaElementAsSourceAndPlayMediaElement();
     31 
     32  info(`- AudioContext should be allowed to start after MediaElementAudioSourceNode started -`);
     33  await audioContextShouldBeAllowedToStart();
     34 
     35  endTest();
     36 })();
     37 
     38 /**
     39 * Test utility functions
     40 */
     41 
     42 function setupTestPreferences() {
     43  return SpecialPowers.pushPrefEnv({"set": [
     44    ["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
     45    ["media.autoplay.blocking_policy", 0],
     46    ["media.autoplay.block-event.enabled", true],
     47  ]});
     48 }
     49 
     50 function createAudioContext() {
     51  /* global ac */
     52  window.ac = new AudioContext();
     53 
     54  ac.allowedToStart = new Promise(resolve => {
     55    ac.addEventListener("statechange", function() {
     56      if (ac.state === "running") {
     57        resolve();
     58      }
     59    }, {once: true});
     60  });
     61 
     62  ac.notAllowedToStart = new Promise(resolve => {
     63    ac.addEventListener("blocked", async function() {
     64      resolve();
     65    }, {once: true});
     66  });
     67 }
     68 
     69 async function audioContextShouldBeBlocked() {
     70  await ac.notAllowedToStart;
     71  is(ac.state, "suspended", `AudioContext is blocked.`);
     72 }
     73 
     74 async function useMediaElementAsSourceAndPlayMediaElement() {
     75  let video = document.createElement('video');
     76  video.src = "gizmo.mp4";
     77 
     78  let source = ac.createMediaElementSource(video);
     79  source.connect(ac.destination);
     80  // simulate user gesture in order to start video.
     81  SpecialPowers.wrap(document).notifyUserGestureActivation();
     82  await playVideo(video);
     83 }
     84 
     85 async function playVideo(video) {
     86  video.play();
     87  await once(video, "play");
     88  ok(true, `video started.`);
     89  removeNodeAndSource(video);
     90 }
     91 
     92 async function audioContextShouldBeAllowedToStart() {
     93  await ac.allowedToStart;
     94  is(ac.state, "running", `AudioContext is allowed to start.`);
     95 }
     96 
     97 function endTest() {
     98  // reset the activation flag in order not to interfere following test in the
     99  // verify mode which would run the test using same document couple times.
    100  SpecialPowers.wrap(document).clearUserGestureActivation();
    101  SimpleTest.finish();
    102 }
    103 
    104 </script>