tor-browser

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

browser_autoplay_videoDocument.js (2459B)


      1 "use strict";
      2 
      3 const PAGE = GetTestWebBasedURL("audio.ogg");
      4 
      5 function setup_test_preference() {
      6  return SpecialPowers.pushPrefEnv({
      7    set: [
      8      ["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
      9      ["media.autoplay.blocking_policy", 0],
     10    ],
     11  });
     12 }
     13 
     14 async function checkIsVideoDocumentAutoplay(browser) {
     15  const played = await SpecialPowers.spawn(browser, [], async () => {
     16    const video = content.document.getElementsByTagName("video")[0];
     17    const played =
     18      video &&
     19      (await video.play().then(
     20        () => true,
     21        () => false
     22      ));
     23    return played;
     24  });
     25  // On Android, we won't allow top level video document to autoplay.
     26  const expectToPlay = SpecialPowers.Services.appinfo.OS != "Android";
     27  is(
     28    played,
     29    expectToPlay,
     30    `Should ${expectToPlay ? "" : "NOT "} be able to play a video document.`
     31  );
     32 }
     33 
     34 async function checkIsIframeVideoDocumentAutoplay(browser) {
     35  info("- create iframe video document -");
     36  const iframeBC = await SpecialPowers.spawn(browser, [PAGE], async pageURL => {
     37    const iframe = content.document.createElement("iframe");
     38    iframe.src = pageURL;
     39    content.document.body.appendChild(iframe);
     40    const iframeLoaded = new Promise(resolve => {
     41      iframe.addEventListener("load", () => resolve(), { once: true });
     42    });
     43    await iframeLoaded;
     44    return iframe.browsingContext;
     45  });
     46 
     47  info("- check whether iframe video document starts playing -");
     48  const [paused, playedLength] = await SpecialPowers.spawn(iframeBC, [], () => {
     49    const video = content.document.querySelector("video");
     50    return [video.paused, video.played.length];
     51  });
     52  ok(paused, "Subdoc video should not have played");
     53  is(playedLength, 0, "Should have empty played ranges");
     54 }
     55 
     56 add_task(async () => {
     57  await BrowserTestUtils.withNewTab(
     58    {
     59      gBrowser,
     60      url: PAGE,
     61    },
     62    async browser => {
     63      info("- setup test preference -");
     64      await setup_test_preference();
     65 
     66      info(`- check whether video document is autoplay -`);
     67      await checkIsVideoDocumentAutoplay(browser);
     68    }
     69  );
     70 });
     71 
     72 add_task(async () => {
     73  await BrowserTestUtils.withNewTab(
     74    {
     75      gBrowser,
     76      url: "about:blank",
     77    },
     78    async browser => {
     79      info("- setup test preference -");
     80      await setup_test_preference();
     81 
     82      info(`- check whether video document in iframe is autoplay -`);
     83      await checkIsIframeVideoDocumentAutoplay(browser);
     84    }
     85  );
     86 });