summaryrefslogtreecommitdiffstats
path: root/dom/media/autoplay/test/browser/browser_autoplay_videoDocument.js
blob: 77ce4ddbc11e947b687942d0df2c976c85788311 (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
"use strict";

const PAGE = GetTestWebBasedURL("audio.ogg");

function setup_test_preference() {
  return SpecialPowers.pushPrefEnv({
    set: [
      ["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
      ["media.autoplay.blocking_policy", 0],
    ],
  });
}

async function checkIsVideoDocumentAutoplay(browser) {
  const played = await SpecialPowers.spawn(browser, [], async () => {
    const video = content.document.getElementsByTagName("video")[0];
    const played =
      video &&
      (await video.play().then(
        () => true,
        () => false
      ));
    return played;
  });
  ok(played, "Should be able to play in video document.");
}

async function checkIsIframeVideoDocumentAutoplay(browser) {
  info("- create iframe video document -");
  const iframeBC = await SpecialPowers.spawn(browser, [PAGE], async pageURL => {
    const iframe = content.document.createElement("iframe");
    iframe.src = pageURL;
    content.document.body.appendChild(iframe);
    const iframeLoaded = new Promise((resolve, reject) => {
      iframe.addEventListener("load", e => resolve(), { once: true });
    });
    await iframeLoaded;
    return iframe.browsingContext;
  });

  info("- check whether iframe video document starts playing -");
  const [paused, playedLength] = await SpecialPowers.spawn(iframeBC, [], () => {
    const video = content.document.querySelector("video");
    return [video.paused, video.played.length];
  });
  ok(paused, "Subdoc video should not have played");
  is(playedLength, 0, "Should have empty played ranges");
}

add_task(async () => {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: PAGE,
    },
    async browser => {
      info("- setup test preference -");
      await setup_test_preference();

      info(`- check whether video document is autoplay -`);
      await checkIsVideoDocumentAutoplay(browser);
    }
  );
});

add_task(async () => {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:blank",
    },
    async browser => {
      info("- setup test preference -");
      await setup_test_preference();

      info(`- check whether video document in iframe is autoplay -`);
      await checkIsIframeVideoDocumentAutoplay(browser);
    }
  );
});