Bug 1506290 - part2 : add test. r=cpearce
authorAlastor Wu <alwu@mozilla.com>
Mon, 14 Jan 2019 23:47:35 +0000
changeset 453890 3420cf8f79a96c6c7647a0f8fc7f477590d3a488
parent 453889 63c3828351f0ad793bd8e58426ea5dcc1990aa76
child 453891 db57901030e954ada2c5ba75a3c6bd0160e6b7e2
push id76057
push useralwu@mozilla.com
push dateTue, 15 Jan 2019 02:50:41 +0000
treeherderautoland@3420cf8f79a9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpearce
bugs1506290
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1506290 - part2 : add test. r=cpearce Differential Revision: https://phabricator.services.mozilla.com/D15451
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_autoplay_policy.js
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -25,16 +25,19 @@ support-files =
   silentAudioTrack.webm
   doggy.png
   firebird.png
 
 [browser_audioCompeting.js]
 tags = audiochannel
 [browser_audioCompeting_onlyForActiveAgent.js]
 tags = audiochannel
+[browser_autoplay_policy.js]
+support-files =
+  file_empty.html
 [browser_autoplay_policy_iframe_hierarchy.js]
 support-files =
   file_autoplay_three_layers_frame1.html
   file_autoplay_three_layers_frame2.html
   file_autoplay_two_layers_frame1.html
   file_autoplay_two_layers_frame2.html
   file_video.html
   gizmo.mp4
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_autoplay_policy.js
@@ -0,0 +1,59 @@
+/**
+ * This test is used to ensure we can get the correct document's autoplay policy
+ * under different situations.
+ * Spec discussion : https://github.com/WICG/autoplay/issues/1
+ */
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_empty.html";
+
+function setupTestPreferences(isAllowedAutoplay, isAllowedMuted) {
+  const autoplayDefault = isAllowedAutoplay ?
+    SpecialPowers.Ci.nsIAutoplay.ALLOWED : SpecialPowers.Ci.nsIAutoplay.BLOCKED;
+  return SpecialPowers.pushPrefEnv({"set": [
+    ["dom.media.autoplay.autoplay-policy-api", true],
+    ["media.autoplay.default", autoplayDefault],
+    ["media.autoplay.enabled.user-gestures-needed", true],
+    ["media.autoplay.allow-muted", isAllowedMuted],
+  ]});
+}
+
+async function checkAutoplayPolicy(expectedAutoplayPolicy) {
+  is(content.document.autoplayPolicy, expectedAutoplayPolicy,
+    `Autoplay policy is correct.`);
+}
+
+add_task(async function testAutoplayPolicy() {
+  await BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: PAGE,
+  }, async browser => {
+    info(`- Allow all kinds of media to autoplay -`);
+    let isAllowedAutoplay = true;
+    let isAllowedMuted = true;
+    await setupTestPreferences(isAllowedAutoplay, isAllowedMuted);
+    await ContentTask.spawn(browser, "allowed", checkAutoplayPolicy);
+
+    info(`- Allow all kinds of media to autoplay even if changing the pref for muted media -`);
+    isAllowedAutoplay = true;
+    isAllowedMuted = false;
+    await setupTestPreferences(isAllowedAutoplay, isAllowedMuted);
+    await ContentTask.spawn(browser, "allowed", checkAutoplayPolicy);
+
+    info(`- Disable autoplay for audible media -`);
+    isAllowedAutoplay = false;
+    isAllowedMuted = true;
+    await setupTestPreferences(isAllowedAutoplay, isAllowedMuted);
+    await ContentTask.spawn(browser, "allowed-muted", checkAutoplayPolicy);
+
+    info(`- Disable autoplay for all kinds of media -`);
+    isAllowedAutoplay = false;
+    isAllowedMuted = false;
+    await setupTestPreferences(isAllowedAutoplay, isAllowedMuted);
+    await ContentTask.spawn(browser, "disallowed", checkAutoplayPolicy);
+
+    info(`- Simulate user gesture activation which would allow all kinds of media to autoplay -`);
+    await ContentTask.spawn(browser, null, async () => {
+      content.document.notifyUserGestureActivation();
+    });
+    await ContentTask.spawn(browser, "allowed", checkAutoplayPolicy);
+  });
+});