gecko-dev/dom/media/test/test_autoplay_policy_activation.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

159 lines
5.0 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 videos can only play audibly in windows/frames
// which have been activated by same-origin user gesture.
gTestPrefs.push(["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
["media.autoplay.enabled.user-gestures-needed", true]);
SpecialPowers.pushPrefEnv({'set': gTestPrefs}, () => {
runTest();
});
let test_cases = [
{
name: "inaudible playback in unactivated same-origin iframe in activated parent allowed",
muted: true,
same_origin_child: true,
activated_child: false,
activated_parent: true,
should_play: true,
},
{
name: "inaudible playback in unactivated same-origin iframe in unactivated parent allowed",
muted: true,
same_origin_child: true,
activated_child: false,
activated_parent: false,
should_play: true,
},
{
name: "audible playback in unactivated same-origin iframe in activated parent allowed",
muted: false,
same_origin_child: true,
activated_child: false,
activated_parent: true,
should_play: true,
},
{
name: "audible playback in unactivated same-origin iframe in unactivated parent blocked",
muted: false,
same_origin_child: true,
activated_child: false,
activated_parent: false,
should_play: false,
},
{
name: "inaudible playback in unactivated cross-origin iframe in activated parent allowed",
muted: true,
same_origin_child: false,
activated_child: false,
activated_parent: true,
should_play: true,
},
{
name: "inaudible playback in unactivated cross-origin iframe in unactivated parent allowed",
muted: true,
same_origin_child: false,
activated_child: false,
activated_parent: false,
should_play: true,
},
{
name: "audible playback in unactivated cross-origin iframe in activated parent allowed",
muted: false,
same_origin_child: false,
activated_child: false,
activated_parent: true,
should_play: true,
},
{
name: "audible playback in unactivated cross-origin iframe in unactivated parent blocked",
muted: false,
same_origin_child: false,
activated_child: false,
activated_parent: false,
should_play: false,
},
{
name: "audible playback in activated cross-origin iframe allowed",
muted: false,
same_origin_child: false,
activated_child: true,
activated_parent: false,
should_play: true,
},
{
name: "audible playback in activated document allowed",
muted: false,
activated_parent: true,
should_play: true,
},
{
name: "audible playback in unactivated document blocked",
muted: false,
activated_parent: false,
should_play: false,
},
{
name: "inaudible playback in activated document allowed",
muted: true,
activated_parent: true,
should_play: true,
},
{
name: "inaudible playback in unactivated document allowed",
muted: true,
activated_parent: false,
should_play: true,
},
];
let child_url = "file_autoplay_policy_activation_window.html";
async function runTest() {
for (test_case of test_cases) {
// Run each test in a new window, to ensure its user gesture
// activation state isn't tainted by preceeding tests.
let child = window.open(child_url, "", "width=500,height=500");
await once(child, "load");
child.postMessage(test_case, window.origin);
let result = await nextWindowMessage();
SimpleTest.is(result.data.allowedToPlay, test_case.should_play, "allowed - " + test_case.name);
SimpleTest.is(result.data.played, test_case.should_play, "played - " + test_case.name);
child.close();
}
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>