gecko-dev/dom/html/test/test_video_wakelock.html

150 lines
4.1 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=868943
-->
<head>
<title>Test for Bug 868943</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=868943">Mozilla Bug 868943</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 868943 **/
function testVideoPlayPause() {
info("#1 testVideoPlayPause");
var lockState_cpu = true;
var lockState_screen = true;
var count_cpu = 0;
var count_screen = 0;
var content = document.getElementById('content');
var video = document.createElement('video');
video.src = "wakelock.ogv";
content.appendChild(video);
var startDate;
function testVideoPlayPauseListener(topic, state) {
info("#1 topic=" + topic + ", state=" + state);
var locked = state == "locked-foreground" ||
state == "locked-background";
if (topic == "cpu") {
is(locked, lockState_cpu, "#1 Video element locked the cpu");
count_cpu++;
} else if (topic == "screen") {
is(locked, lockState_screen, "#1 Video element locked the screen");
count_screen++;
}
if (count_cpu == 1 && count_screen == 1) {
info("#1 Both cpu and screen are locked");
// The next step is to unlock the resource.
lockState_cpu = false;
lockState_screen = false;
video.pause();
startDate = new Date();
}
if (count_cpu == 2 && count_screen == 2) {
var diffDate = (new Date() - startDate);
ok(diffDate > 200, "#1 There was at least 200 milliseconds between the stop and the wakelock release");
content.removeChild(video);
navigator.mozPower.removeWakeLockListener(testVideoPlayPauseListener);
runTests();
}
}
navigator.mozPower.addWakeLockListener(testVideoPlayPauseListener);
video.play();
}
function testVideoPlay() {
info("#2 testVideoPlay");
var lockState_cpu = true;
var lockState_screen = true;
var count_cpu = 0;
var count_screen = 0;
var content = document.getElementById('content');
var video = document.createElement('video');
video.src = "wakelock.ogv";
content.appendChild(video);
var startDate;
video.addEventListener('progress', function() {
startDate = new Date();
});
function testVideoPlayListener(topic, state) {
info("#2 topic=" + topic + ", state=" + state);
var locked = state == "locked-foreground" ||
state == "locked-background";
if (topic == "cpu") {
is(locked, lockState_cpu, "#2 Video element locked the cpu");
count_cpu++;
} else if (topic == "screen") {
is(locked, lockState_screen, "#2 Video element locked the screen");
count_screen++;
}
if (count_cpu == 1 && count_screen == 1) {
info("#2 Both cpu and screen are locked");
// The next step is to unlock the resource.
lockState_cpu = false;
lockState_screen = false;
} else if (count_cpu == 2 && count_screen == 2) {
var diffDate = (new Date() - startDate);
ok(diffDate > 200, "#2 There was at least milliseconds between the stop and the wakelock release");
content.removeChild(video);
navigator.mozPower.removeWakeLockListener(testVideoPlayListener);
runTests();
}
}
navigator.mozPower.addWakeLockListener(testVideoPlayListener);
video.play();
}
var tests = [ testVideoPlayPause, testVideoPlay ];
function runTests() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.pop();
test();
};
SpecialPowers.pushPermissions(
[{'type': 'power', 'allow': true, 'context': document}],
function() {
SpecialPowers.pushPrefEnv({"set": [["media.wakelock_timeout", 500]]}, runTests);
});
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>