gecko-dev/dom/media/test/test_autoplay_policy.html
Dale Harvey c9e5e7c554 Bug 1470082 - Change autoplay checkbox to combobox. r=cpearce,flod,johannh
MozReview-Commit-ID: E71TxvgfJlJ

--HG--
extra : rebase_source : 30ca63df77e48a44de4d3e90182440c3937ed32f
2018-06-29 14:14:33 +01:00

156 lines
4.4 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>
</head>
<body>
<pre id="test">
<script>
let manager = new MediaTestManager;
gTestPrefs.push(["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
["media.autoplay.enabled.user-gestures-needed", true]);
window.info = function(msg, token) {
SimpleTest.info(msg + ", token=" + token);
}
window.is = function(valA, valB, msg, token) {
SimpleTest.is(valA, valB, msg + ", token=" + token);
}
window.ok = function(val, msg, token) {
SimpleTest.ok(val, msg + ", token=" + token);
}
/**
* test files and paremeters
*/
var autoplayTests = [
/* video */
{ name: "gizmo.mp4", type: "video/mp4" },
{ name: "gizmo-noaudio.mp4", type: "video/mp4" },
{ name: "gizmo.webm", type: "video/webm" },
{ name: "gizmo-noaudio.webm", type: "video/webm" },
/* audio */
{ name: "small-shot.ogg", type: "audio/ogg" },
{ name: "small-shot.m4a", type: "audio/mp4" },
{ name: "small-shot.mp3", type: "audio/mpeg" },
{ name: "small-shot.flac", type: "audio/flac" }
];
var autoplayParams = [
{ volume: 1.0, muted: false },
{ volume: 0.0, muted: false },
{ volume: 1.0, muted: true },
{ volume: 0.0, muted: true },
];
function createTestArray()
{
var tests = [];
for (let test of autoplayTests) {
for (let param of autoplayParams) {
tests.push({
name: test.name,
type: test.type,
volume: param.volume,
muted: param.muted,
});
}
}
return tests;
}
/**
* Main test function for different autoplay cases without user interaction.
*
* When the pref "media.autoplay.default" is 1 and the pref
* "media.autoplay.enabled.user-gestures-needed" is true, we only allow
* audible media to autoplay after the website has been activated by specific
* user gestures. However, inaudible media won't be restricted.
*
* Audible means the volume is non zero and muted is false.
*
* Inaudible means the volume is zero, or the muted is true.
*
* We do not take into account whether a media has an audio track.
*/
async function runTest(test, token) {
manager.started(token);
await testPlay(test, token);
await testAutoplayKeyword(test, token);
manager.finished(token);
}
manager.runTests(createTestArray(), runTest);
/**
* Different test scenarios
*/
async function testPlay(test, token) {
info("### start testPlay", token);
info(`volume=${test.volume}, muted=${test.muted}`, token);
let element = document.createElement(getMajorMimeType(test.type));
element.volume = test.volume;
element.muted = test.muted;
element.src = test.name;
document.body.appendChild(element);
let isAudible = test.volume != 0.0 &&
!test.muted;
let state = isAudible? "audible" : "non-audible";
info(`### calling play() for ${state} media`, token);
let promise = element.play();
if (isAudible) {
await promise.catch(function(error) {
ok(element.paused, `${state} media fail to start via play()`, token);
is(error.name, "NotAllowedError", "rejected play promise", token);
});
} else {
// since we just want to check the value of 'paused', we don't need to wait
// resolved play promise. (it's equal to wait for 'playing' event)
await once(element, "play");
ok(!element.paused, `${state} media start via play()`, token);
}
removeNodeAndSource(element);
}
async function testAutoplayKeyword(test, token) {
info("### start testAutoplayKeyword", token);
info(`volume=${test.volume}, muted=${test.muted}`, token);
let element = document.createElement(getMajorMimeType(test.type));
element.autoplay = true;
element.volume = test.volume;
element.muted = test.muted;
element.src = test.name;
document.body.appendChild(element);
let isAudible = test.volume != 0.0 &&
!test.muted;
let state = isAudible? "audible" : "non-audible";
info(`### wait to autoplay for ${state} media`, token);
if (isAudible) {
await once(element, "canplay");
ok(element.paused, `can not start with 'autoplay' keyword for ${state} media`, token);
} else {
await once(element, "play");
ok(!element.paused, `start with 'autoplay' keyword for ${state} media`, token);
}
removeNodeAndSource(element);
}
</script>