summaryrefslogtreecommitdiffstats
path: root/dom/media/autoplay/test/browser/browser_autoplay_policy_request_permission.js
blob: 5cdb3ffe6c876440b6b60191c0419e342e5aba7b (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

const { PermissionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PermissionTestUtils.sys.mjs"
);

const VIDEO_PAGE_URI = GetTestWebBasedURL("file_empty.html");
const SAME_ORIGIN_FRAME_URI = GetTestWebBasedURL(
  "file_mediaplayback_frame.html"
);
const DIFFERENT_ORIGIN_FRAME_URI = GetTestWebBasedURL(
  "file_mediaplayback_frame.html",
  { crossOrigin: true }
);

const gPermissionName = "autoplay-media";

function setTestingPreferences(defaultSetting) {
  info(`set default autoplay setting to '${defaultSetting}'`);
  let defaultValue =
    defaultSetting == "blocked"
      ? SpecialPowers.Ci.nsIAutoplay.BLOCKED
      : SpecialPowers.Ci.nsIAutoplay.ALLOWED;
  return SpecialPowers.pushPrefEnv({
    set: [
      ["media.autoplay.default", defaultValue],
      ["media.autoplay.blocking_policy", 0],
      ["media.autoplay.block-event.enabled", true],
    ],
  });
}

async function testAutoplayExistingPermission(args) {
  info("- Starting '" + args.name + "' -");
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: VIDEO_PAGE_URI,
    },
    async browser => {
      let promptShowing = () =>
        PopupNotifications.getNotification("autoplay-media", browser);

      PermissionTestUtils.add(
        browser.currentURI,
        "autoplay-media",
        args.permission
      );
      ok(!promptShowing(), "Should not be showing permission prompt yet");

      await loadAutoplayVideo(browser, args);
      await checkVideoDidPlay(browser, args);

      // Reset permission.
      PermissionTestUtils.remove(browser.currentURI, "autoplay-media");

      info("- Finished '" + args.name + "' -");
    }
  );
}

async function testAutoplayExistingPermissionAgainstDefaultSetting(args) {
  await setTestingPreferences(args.defaultSetting);
  await testAutoplayExistingPermission(args);
}

// Test the simple ALLOW/BLOCK cases; when permission is already set to ALLOW,
// we shoud be able to autoplay via calling play(), or via the autoplay attribute,
// and when it's set to BLOCK, we should not.
add_task(async () => {
  await setTestingPreferences("blocked" /* default setting */);
  await testAutoplayExistingPermission({
    name: "Prexisting allow permission autoplay attribute",
    permission: Services.perms.ALLOW_ACTION,
    shouldPlay: true,
    mode: "autoplay attribute",
  });
  await testAutoplayExistingPermission({
    name: "Prexisting allow permission call play",
    permission: Services.perms.ALLOW_ACTION,
    shouldPlay: true,
    mode: "call play",
  });
  await testAutoplayExistingPermission({
    name: "Prexisting block permission autoplay attribute",
    permission: Services.perms.DENY_ACTION,
    shouldPlay: false,
    mode: "autoplay attribute",
  });
  await testAutoplayExistingPermission({
    name: "Prexisting block permission call play",
    permission: Services.perms.DENY_ACTION,
    shouldPlay: false,
    mode: "call play",
  });
});

/**
 * These tests are used to ensure the autoplay setting for specific site can
 * always override the default autoplay setting.
 */
add_task(async () => {
  await testAutoplayExistingPermissionAgainstDefaultSetting({
    name: "Site has prexisting allow permission but default setting is 'blocked'",
    permission: Services.perms.ALLOW_ACTION,
    defaultSetting: "blocked",
    shouldPlay: true,
    mode: "autoplay attribute",
  });
  await testAutoplayExistingPermissionAgainstDefaultSetting({
    name: "Site has prexisting block permission but default setting is 'allowed'",
    permission: Services.perms.DENY_ACTION,
    defaultSetting: "allowed",
    shouldPlay: false,
    mode: "autoplay attribute",
  });
});

/**
 * The permission of the main page's domain would determine the final autoplay
 * result when a page contains multiple iframes which are in the different
 * domain from the main pages's.
 * That means we would not check the permission of iframe's domain, even if it
 * has been set.
 */
add_task(async function testExistingPermissionForIframe() {
  await setTestingPreferences("blocked" /* default setting */);
  await testAutoplayExistingPermissionForIframe({
    name: "Prexisting ALLOW for main page with same origin iframe",
    permissionForParent: Services.perms.ALLOW_ACTION,
    isIframeDifferentOrgin: true,
    shouldPlay: true,
  });

  await testAutoplayExistingPermissionForIframe({
    name: "Prexisting ALLOW for main page with different origin iframe",
    permissionForParent: Services.perms.ALLOW_ACTION,
    isIframeDifferentOrgin: false,
    shouldPlay: true,
  });

  await testAutoplayExistingPermissionForIframe({
    name: "Prexisting ALLOW for main page, prexisting DENY for different origin iframe",
    permissionForParent: Services.perms.ALLOW_ACTION,
    permissionForChild: Services.perms.DENY_ACTION,
    isIframeDifferentOrgin: false,
    shouldPlay: true,
  });

  await testAutoplayExistingPermissionForIframe({
    name: "Prexisting DENY for main page, prexisting ALLOW for different origin iframe",
    permissionForParent: Services.perms.DENY_ACTION,
    permissionForChild: Services.perms.ALLOW_ACTION,
    isIframeDifferentOrgin: false,
    shouldPlay: false,
  });
});

/**
 * The following are helper functions.
 */
async function testAutoplayExistingPermissionForIframe(args) {
  info(`Start test : ${args.name}`);
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: VIDEO_PAGE_URI,
    },
    async browser => {
      setupSitesPermission(browser, args);

      await createIframe(browser, args);
      await checkAutplayInIframe(browser, args);

      clearSitesPermission(browser, args);
    }
  );
  info(`Finish test : ${args.name}`);
}

function setupSitesPermission(
  browser,
  {
    isIframeDifferentOrgin,
    permissionForParent,
    permissionForChild = Services.perms.UNKNOWN_ACTION,
  }
) {
  info(`setupSitesPermission`);
  // Set permission for the main page's domain
  setPermissionForBrowser(browser, browser.currentURI, permissionForParent);
  if (isIframeDifferentOrgin) {
    // Set permission for different domain of the iframe
    setPermissionForBrowser(
      browser,
      DIFFERENT_ORIGIN_FRAME_URI,
      permissionForChild
    );
  }
}

function clearSitesPermission(browser, { isIframeDifferentOrgin }) {
  info(`clearSitesPermission`);
  // Clear permission for the main page's domain
  setPermissionForBrowser(
    browser,
    browser.currentURI,
    Services.perms.UNKNOWN_ACTION
  );
  if (isIframeDifferentOrgin) {
    // Clear permission for different domain of the iframe
    setPermissionForBrowser(
      browser,
      DIFFERENT_ORIGIN_FRAME_URI,
      Services.perms.UNKNOWN_ACTION
    );
  }
}

function setPermissionForBrowser(browser, uri, permValue) {
  const promptShowing = () =>
    PopupNotifications.getNotification(gPermissionName, browser);
  PermissionTestUtils.add(uri, gPermissionName, permValue);
  ok(!promptShowing(), "Should not be showing permission prompt yet");
  is(
    PermissionTestUtils.testExactPermission(uri, gPermissionName),
    permValue,
    "Set permission correctly"
  );
}

function createIframe(browser, { isIframeDifferentOrgin }) {
  const iframeURL = isIframeDifferentOrgin
    ? DIFFERENT_ORIGIN_FRAME_URI
    : SAME_ORIGIN_FRAME_URI;
  return SpecialPowers.spawn(browser, [iframeURL], async url => {
    info(`Create iframe and wait until it finsihes loading`);
    const iframe = content.document.createElement("iframe");
    iframe.src = url;
    content.document.body.appendChild(iframe);
    await new Promise(r => (iframe.onload = r));
  });
}

function checkAutplayInIframe(browser, args) {
  return SpecialPowers.spawn(browser, [args], async ({ shouldPlay }) => {
    info(`check if media in iframe can start playing`);
    const iframe = content.document.getElementsByTagName("iframe")[0];
    if (!iframe) {
      ok(false, `can not get the iframe!`);
      return;
    }
    iframe.contentWindow.postMessage("play", "*");
    await new Promise(r => {
      content.onmessage = event => {
        if (shouldPlay) {
          is(event.data, "played", `played media in iframe`);
        } else {
          is(event.data, "blocked", `blocked media in iframe`);
        }
        r();
      };
    });
  });
}