summaryrefslogtreecommitdiffstats
path: root/dom/media/autoplay/test/browser/browser_autoplay_policy_play_twice.js
blob: 7130e6e781e7a7a7ccf764579ffc0bf73e640b2b (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
const VIDEO_PAGE = GetTestWebBasedURL("file_video.html");

function setup_test_preference(enableUserGesture) {
  let state = enableUserGesture ? "enable" : "disable";
  info(`- set pref : ${state} user gesture -`);
  return SpecialPowers.pushPrefEnv({
    set: [
      ["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
      ["media.autoplay.blocking_policy", enableUserGesture ? 0 : 1],
    ],
  });
}

async function allow_play_for_played_video() {
  info("- open new tab  -");
  let tab = await BrowserTestUtils.openNewForegroundTab(
    window.gBrowser,
    "about:blank"
  );
  BrowserTestUtils.loadURIString(tab.linkedBrowser, VIDEO_PAGE);
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  info("- simulate user-click to start video -");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#v",
    { button: 0 },
    tab.linkedBrowser
  );

  async function play_video_again() {
    let video = content.document.getElementById("v");
    ok(!video.paused, "video is playing");

    info("- call video play() again -");
    try {
      await video.play();
      ok(true, "success to resolve play promise");
    } catch (e) {
      ok(false, "promise should not be rejected");
    }
  }
  await SpecialPowers.spawn(tab.linkedBrowser, [], play_video_again);

  info("- remove tab -");
  BrowserTestUtils.removeTab(tab);
}

add_task(async function start_test() {
  await setup_test_preference(true);
  await allow_play_for_played_video();

  await setup_test_preference(false);
  await allow_play_for_played_video();
});