From 869bad3ecb62cebc3e5131e931c2af6050ce4f74 Mon Sep 17 00:00:00 2001 From: alwu Date: Mon, 19 Dec 2022 20:57:56 +0000 Subject: [PATCH] Bug 1773551 - part7 : add mochitests for checking autoplay policy. r=media-playback-reviewers,padenot Differential Revision: https://phabricator.services.mozilla.com/D164854 --- dom/media/autoplay/test/browser/browser.ini | 2 + ...policy_detection_global_and_site_sticky.js | 168 ++++++++++++++++++ ...autoplay_policy_detection_global_sticky.js | 105 +++++++++++ dom/media/autoplay/test/browser/head.js | 44 +++++ 4 files changed, 319 insertions(+) create mode 100644 dom/media/autoplay/test/browser/browser_autoplay_policy_detection_global_and_site_sticky.js create mode 100644 dom/media/autoplay/test/browser/browser_autoplay_policy_detection_global_sticky.js diff --git a/dom/media/autoplay/test/browser/browser.ini b/dom/media/autoplay/test/browser/browser.ini index 69bce350dca8..87bb3675e901 100644 --- a/dom/media/autoplay/test/browser/browser.ini +++ b/dom/media/autoplay/test/browser/browser.ini @@ -11,6 +11,8 @@ support-files = head.js [browser_autoplay_policy.js] +[browser_autoplay_policy_detection_global_sticky.js] +[browser_autoplay_policy_detection_global_and_site_sticky.js] [browser_autoplay_policy_play_twice.js] [browser_autoplay_policy_user_gestures.js] https_first_disabled = true diff --git a/dom/media/autoplay/test/browser/browser_autoplay_policy_detection_global_and_site_sticky.js b/dom/media/autoplay/test/browser/browser_autoplay_policy_detection_global_and_site_sticky.js new file mode 100644 index 000000000000..f74f0cd83c3f --- /dev/null +++ b/dom/media/autoplay/test/browser/browser_autoplay_policy_detection_global_and_site_sticky.js @@ -0,0 +1,168 @@ +/** + * This test checks whether Autoplay Policy Detection API works correctly under + * different situations of having global permission set for block autoplay + * along with different site permission setting. This test only checks the + * sticky user gesture blocking model. + */ +"use strict"; + +const { PermissionTestUtils } = ChromeUtils.import( + "resource://testing-common/PermissionTestUtils.jsm" +); + +// We can't set site permission on 'about:blank' so we use an empty page. +const PAGE_URL = GetTestWebBasedURL("file_empty.html"); + +add_setup(async function setSharedPrefs() { + await SpecialPowers.pushPrefEnv({ + set: [ + ["dom.media.autoplay-policy-detection.enabled", true], + ["media.autoplay.blocking_policy", 0], + ["media.autoplay.block-webaudio", true], + ], + }); +}); + +add_task(async function testGlobalPermissionIsAllowed() { + await SpecialPowers.pushPrefEnv({ + set: [["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.ALLOWED]], + }); + let tab = await createTabAndSetupPolicyAssertFunc(PAGE_URL); + PermissionTestUtils.add( + tab.linkedBrowser.currentURI, + "autoplay-media", + Services.perms.DENY_ACTION + ); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info("site permission blocks audible autoplay"); + content.assertAutoplayPolicy({ + resultForElementType: "allowed-muted", + resultForElement: "allowed-muted", + resultForContextType: "disallowed", + resultForContext: "disallowed", + }); + }); + PermissionTestUtils.add( + tab.linkedBrowser.currentURI, + "autoplay-media", + Ci.nsIAutoplay.BLOCKED_ALL + ); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info("site permission blocks all autoplay"); + content.assertAutoplayPolicy({ + resultForElementType: "disallowed", + resultForElement: "disallowed", + resultForContextType: "disallowed", + resultForContext: "disallowed", + }); + + info( + "activate document by using user gesture, all autoplay will be allowed" + ); + content.document.notifyUserGestureActivation(); + content.assertAutoplayPolicy({ + resultForElementType: "allowed", + resultForElement: "allowed", + resultForContextType: "allowed", + resultForContext: "allowed", + }); + }); + PermissionTestUtils.remove(tab.linkedBrowser.currentURI, "autoplay-media"); + BrowserTestUtils.removeTab(tab); +}); + +add_task(async function testGlobalPermissionIsBlocked() { + await SpecialPowers.pushPrefEnv({ + set: [["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED]], + }); + let tab = await createTabAndSetupPolicyAssertFunc(PAGE_URL); + PermissionTestUtils.add( + tab.linkedBrowser.currentURI, + "autoplay-media", + Services.perms.ALLOW_ACTION + ); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info("site permission allows all autoplay"); + content.assertAutoplayPolicy({ + resultForElementType: "allowed", + resultForElement: "allowed", + resultForContextType: "allowed", + resultForContext: "allowed", + }); + }); + PermissionTestUtils.add( + tab.linkedBrowser.currentURI, + "autoplay-media", + Ci.nsIAutoplay.BLOCKED_ALL + ); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info("site permission blocks all autoplay"); + content.assertAutoplayPolicy({ + resultForElementType: "disallowed", + resultForElement: "disallowed", + resultForContextType: "disallowed", + resultForContext: "disallowed", + }); + + info( + "activate document by using user gesture, all autoplay will be allowed" + ); + content.document.notifyUserGestureActivation(); + content.assertAutoplayPolicy({ + resultForElementType: "allowed", + resultForElement: "allowed", + resultForContextType: "allowed", + resultForContext: "allowed", + }); + }); + PermissionTestUtils.remove(tab.linkedBrowser.currentURI, "autoplay-media"); + BrowserTestUtils.removeTab(tab); +}); + +add_task(async function testGlobalPermissionIsBlockedAll() { + await SpecialPowers.pushPrefEnv({ + set: [["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED_ALL]], + }); + let tab = await createTabAndSetupPolicyAssertFunc(PAGE_URL); + PermissionTestUtils.add( + tab.linkedBrowser.currentURI, + "autoplay-media", + Services.perms.ALLOW_ACTION + ); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info("site permission allows all autoplay"); + content.assertAutoplayPolicy({ + resultForElementType: "allowed", + resultForElement: "allowed", + resultForContextType: "allowed", + resultForContext: "allowed", + }); + }); + PermissionTestUtils.add( + tab.linkedBrowser.currentURI, + "autoplay-media", + Services.perms.DENY_ACTION + ); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info("site permission blocks audible autoplay"); + content.assertAutoplayPolicy({ + resultForElementType: "allowed-muted", + resultForElement: "allowed-muted", + resultForContextType: "disallowed", + resultForContext: "disallowed", + }); + + info( + "activate document by using user gesture, all autoplay will be allowed" + ); + content.document.notifyUserGestureActivation(); + content.assertAutoplayPolicy({ + resultForElementType: "allowed", + resultForElement: "allowed", + resultForContextType: "allowed", + resultForContext: "allowed", + }); + }); + PermissionTestUtils.remove(tab.linkedBrowser.currentURI, "autoplay-media"); + BrowserTestUtils.removeTab(tab); +}); diff --git a/dom/media/autoplay/test/browser/browser_autoplay_policy_detection_global_sticky.js b/dom/media/autoplay/test/browser/browser_autoplay_policy_detection_global_sticky.js new file mode 100644 index 000000000000..ab9fe6b41829 --- /dev/null +++ b/dom/media/autoplay/test/browser/browser_autoplay_policy_detection_global_sticky.js @@ -0,0 +1,105 @@ +/** + * This test checks whether Autoplay Policy Detection API works correctly under + * different situations of having global permission set for block autoplay. + * This test only checks the sticky user gesture blocking model. + */ +"use strict"; + +add_setup(async function setSharedPrefs() { + await SpecialPowers.pushPrefEnv({ + set: [ + ["dom.media.autoplay-policy-detection.enabled", true], + ["media.autoplay.blocking_policy", 0], + ["media.autoplay.block-webaudio", true], + ], + }); +}); + +add_task(async function testGlobalPermissionIsAllowed() { + await SpecialPowers.pushPrefEnv({ + set: [["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.ALLOWED]], + }); + let tab = await createTabAndSetupPolicyAssertFunc("about:blank"); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info("global setting allows any autoplay"); + content.assertAutoplayPolicy({ + resultForElementType: "allowed", + resultForElement: "allowed", + resultForContextType: "allowed", + resultForContext: "allowed", + }); + }); + BrowserTestUtils.removeTab(tab); +}); + +add_task(async function testGlobalPermissionIsBlocked() { + await SpecialPowers.pushPrefEnv({ + set: [["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED]], + }); + let tab = await createTabAndSetupPolicyAssertFunc("about:blank"); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info( + "global setting allows inaudible autoplay but audible autoplay is still not allowed" + ); + content.assertAutoplayPolicy({ + resultForElementType: "allowed-muted", + resultForElement: "allowed-muted", + resultForContextType: "disallowed", + resultForContext: "disallowed", + }); + + info("tweaking video's muted attribute won't change the result"); + content.video.muted = true; + is( + "allowed-muted", + content.navigator.getAutoplayPolicy(content.video), + "getAutoplayPolicy(video) returns correct value" + ); + content.video.muted = false; + is( + "allowed-muted", + content.navigator.getAutoplayPolicy(content.video), + "getAutoplayPolicy(video) returns correct value" + ); + + info( + "activate document by using user gesture, all autoplay will be allowed" + ); + content.document.notifyUserGestureActivation(); + content.assertAutoplayPolicy({ + resultForElementType: "allowed", + resultForElement: "allowed", + resultForContextType: "allowed", + resultForContext: "allowed", + }); + }); + BrowserTestUtils.removeTab(tab); +}); + +add_task(async function testGlobalPermissionIsBlockedAll() { + await SpecialPowers.pushPrefEnv({ + set: [["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED_ALL]], + }); + let tab = await createTabAndSetupPolicyAssertFunc("about:blank"); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + info("global setting doesn't allow any autoplay"); + content.assertAutoplayPolicy({ + resultForElementType: "disallowed", + resultForElement: "disallowed", + resultForContextType: "disallowed", + resultForContext: "disallowed", + }); + + info( + "activate document by using user gesture, all autoplay will be allowed" + ); + content.document.notifyUserGestureActivation(); + content.assertAutoplayPolicy({ + resultForElementType: "allowed", + resultForElement: "allowed", + resultForContextType: "allowed", + resultForContext: "allowed", + }); + }); + BrowserTestUtils.removeTab(tab); +}); diff --git a/dom/media/autoplay/test/browser/head.js b/dom/media/autoplay/test/browser/head.js index 648b6df59df4..c84850900a86 100644 --- a/dom/media/autoplay/test/browser/head.js +++ b/dom/media/autoplay/test/browser/head.js @@ -103,3 +103,47 @@ function checkVideoDidPlay(browser, args) { content.document.body.remove(video); }); } + +/** + * Create a tab that will load the given url, and define an autoplay policy + * check function inside the content window in that tab. This function should + * only be used when `dom.media.autoplay-policy-detection.enabled` is true. + * @param {url} url + * the url which the created tab should load + */ +async function createTabAndSetupPolicyAssertFunc(url) { + let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, url); + await SpecialPowers.spawn(tab.linkedBrowser, [], _ => { + content.video = content.document.createElement("video"); + content.ac = new content.AudioContext(); + content.assertAutoplayPolicy = ({ + resultForElementType, + resultForElement, + resultForContextType, + resultForContext, + }) => { + is( + content.navigator.getAutoplayPolicy("mediaelement"), + resultForElementType, + "getAutoplayPolicy('mediaelement') returns correct value" + ); + is( + content.navigator.getAutoplayPolicy(content.video), + resultForElement, + "getAutoplayPolicy(content.video) returns correct value" + ); + // note, per spec "allowed-muted" won't be used for audio context. + is( + content.navigator.getAutoplayPolicy("audiocontext"), + resultForContextType, + "getAutoplayPolicy('audiocontext') returns correct value" + ); + is( + content.navigator.getAutoplayPolicy(content.ac), + resultForContext, + "getAutoplayPolicy(content.ac) returns correct value" + ); + }; + }); + return tab; +}