gecko-dev/dom/media/test/test_autoplay_policy_permission.html
Chris Pearce 2e3c4bd9af Bug 1478208 - Implement HTMLMediaElement.allowedToPlay. r=alwu,bz
Various web authors have expressed desire to know in advance whether autoplay
will work.

They want this in order to avoid paying the price for downloading media that
won't play. Or they want to take other action such as showing a poster image
instead.

This is of particular interest to Firefox, as we're planning on showing a
prompt to ask the user whether they would like a site to play. If sites want to
determine whether they can autoplay but avoid the prompt showing, they won't be
able to just call play() in Firefox and see whether it works, as that would
likely show the prompt if the user doesn't already have a stored permission.

We've been working out a spec here:
https://github.com/whatwg/html/issues/3617#issuecomment-398613484

This implements what is the consensus to date there;
HTMLMediaElement.allowedToPlay, which returns true when a play() call would not
be blocked with NotAllowedError by autoplay blocking policies.

MozReview-Commit-ID: AkBu0G7uCJ0

--HG--
extra : rebase_source : 3f31db79aa1e570fdd9fc7062d0ddac7c96a8931
2018-07-25 14:25:17 +12:00

84 lines
3.1 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Autoplay policy test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="text/javascript" src="manifest.js"></script>
<script type="text/javascript" src="AutoplayTestUtils.js"></script>
</head>
<body>
<pre id="test">
<script>
// Tests that origins with "autoplay-media" permission can autoplay.
gTestPrefs.push(["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
["media.autoplay.enabled.user-gestures-needed", true],
// Note: permission prompt disabled, as we want immediate
// notification in this test that an origin is blocked.
["media.autoplay.ask-permission", false]);
SpecialPowers.pushPrefEnv({ 'set': gTestPrefs }, () => {
runTest();
});
async function testPlayInOrigin(testCase) {
// Run test in a new window, to ensure its user gesture
// activation state isn't tainted by preceeding tests.
let url = testCase.origin + "/tests/dom/media/test/file_autoplay_policy_activation_frame.html";
let child = window.open(url, "", "width=500,height=500");
is((await nextWindowMessage()).data, "ready", "Expected a 'ready' message");
child.postMessage("play-audible", testCase.origin);
// Wait for the window to tell us whether it could play video.
let result = await nextWindowMessage();
is(result.data.allowedToPlay, testCase.shouldPlay, "allowedToPlay - " + testCase.message);
is(result.data.played, testCase.shouldPlay, "played - " + testCase.message);
child.close();
}
async function runTest() {
// First verify that we can't play in a document unwhitelisted.
is(window.origin, "http://mochi.test:8888", "Origin should be as we assume, otherwise the rest of the test is bogus!");
await testPlayInOrigin({
origin: "http://mochi.test:8888",
shouldPlay: false,
message: "Should not be able to play unwhitelisted."
});
// Add our origin to the whitelist.
await pushAutoplayAllowedPermission();
// Now we should be able to play...
await testPlayInOrigin({
origin: "http://mochi.test:8888",
shouldPlay: true,
message: "Should be able to play since whitelisted."
});
// But sub-origins should not.
await testPlayInOrigin({
origin: "http://test1.mochi.test:8888",
shouldPlay: false,
message: "Sub origin should not count as whitelisted."
});
await testPlayInOrigin({
origin: "http://sub1.test1.mochi.test:8888",
shouldPlay: false,
message: "Sub-sub-origins should not count as whitelisted."
});
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>