summaryrefslogtreecommitdiffstats
path: root/dom/media/autoplay/test/browser/browser_autoplay_policy_touchScroll.js
blob: fa28bf2943fac40783a264a3b279a9a9c91637f1 (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
/**
 * This test is used to ensure that touch in point can activate document and
 * allow autoplay, but touch scroll can't activate document.
 */
/* eslint-disable mozilla/no-arbitrary-setTimeout */
"use strict";

const PAGE = GetTestWebBasedURL("file_nonAutoplayAudio.html");

function checkMediaPlayingState(isPlaying) {
  let audio = content.document.getElementById("testAudio");
  if (!audio) {
    ok(false, "can't get the audio element!");
  }

  is(!audio.paused, isPlaying, "media playing state is correct.");
}

async function callMediaPlay(shouldStartPlaying) {
  let audio = content.document.getElementById("testAudio");
  if (!audio) {
    ok(false, "can't get the audio element!");
  }

  info(`call media.play().`);
  let playPromise = new Promise((resolve, reject) => {
    audio.play().then(() => {
      audio.isPlayStarted = true;
      resolve();
    });
    content.setTimeout(() => {
      if (audio.isPlayStarted) {
        return;
      }
      reject();
    }, 3000);
  });

  let isStartPlaying = await playPromise.then(
    () => true,
    () => false
  );
  is(
    isStartPlaying,
    shouldStartPlaying,
    "media is " + (isStartPlaying ? "" : "not ") + "playing."
  );
}

async function synthesizeTouchScroll(target, browser) {
  const offset = 100;
  await BrowserTestUtils.synthesizeTouch(
    target,
    0,
    0,
    { type: "touchstart" },
    browser
  );
  await BrowserTestUtils.synthesizeTouch(
    target,
    offset / 2,
    offset / 2,
    { type: "touchmove" },
    browser
  );
  await BrowserTestUtils.synthesizeTouch(
    target,
    offset,
    offset,
    { type: "touchend" },
    browser
  );
}

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

add_task(async function testTouchScroll() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: PAGE,
    },
    async browser => {
      info(`- media should not start playing -`);
      await SpecialPowers.spawn(browser, [false], checkMediaPlayingState);

      info(`- simulate touch scroll which should not activate document -`);
      await synthesizeTouchScroll("#testAudio", browser);
      await SpecialPowers.spawn(browser, [false], callMediaPlay);

      info(`- simulate touch at a point which should activate document -`);
      await BrowserTestUtils.synthesizeTouch("#testAudio", 0, 0, {}, browser);
      await SpecialPowers.spawn(browser, [true], callMediaPlay);
    }
  );
});