Bug 854028 - New mochitests for Power API. r=kchen

--HG--
rename : dom/power/test/browser_bug697132.js => dom/power/test/browser_wakelocks.js
This commit is contained in:
Luqman Aden 2013-03-25 10:57:26 -07:00
parent 5b8d2542d2
commit 2d3277f7d7
5 changed files with 206 additions and 1 deletions

View File

@ -13,10 +13,13 @@ include $(DEPTH)/config/autoconf.mk
MOCHITEST_FILES = \
test_power_basics.html \
test_power_set_cpusleepallowed.html \
test_power_set_screen_brightness.html \
test_power_set_screen_enabled.html \
$(NULL)
MOCHITEST_BROWSER_FILES = \
browser_bug697132.js \
browser_wakelocks.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,61 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test Enabling/Disabling CPU Sleep with Power Management API</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script type="application/javascript">
"use strict";
function testEnableSleep() {
try {
navigator.mozPower.cpuSleepAllowed = true;
ok(navigator.mozPower.cpuSleepAllowed === true, "Allow entering suspend state.");
} catch (e) {
ok(false, "Unexpected exception trying to enable entering suspend state.");
}
}
function testDisableSleep() {
try {
navigator.mozPower.cpuSleepAllowed = false;
ok(navigator.mozPower.cpuSleepAllowed === false, "Deny entering suspend state.");
} catch (e) {
ok(false, "Unexpected exception trying to disable entering suspend state.");
}
}
function startTests() {
testDisableSleep();
testEnableSleep();
SimpleTest.finish();
}
SimpleTest.expectAssertions(0, 9);
SimpleTest.waitForExplicitFinish();
if (SpecialPowers.hasPermission("power", document)) {
// Currently only applicable on FxOS
if (navigator.userAgent.indexOf("Mobile") != -1 &&
navigator.appVersion.indexOf("Android") == -1) {
startTests();
} else {
ok(true, "mozPower on Firefox OS only.");
SimpleTest.finish();
}
} else {
// Add the permission and reload so it's propogated
SpecialPowers.addPermission("power", true, document);
window.location.reload();
}
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,76 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test Setting Screen Brightness with Power Management API</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script type="application/javascript">
"use strict";
// Test setting brightness to a value < 0
function testInvalidBrightness1() {
try {
navigator.mozPower.screenBrightness = -1;
} catch (e) {
return ok(true, "Invalid brightness level results in exception.");
}
ok(false, "Exeception not thrown for invalid brightness level.");
}
// Test setting brightness to a value > 1
function testInvalidBrightness2() {
try {
navigator.mozPower.screenBrightness = 2;
} catch (e) {
return ok(true, "Invalid brightness level results in exception.");
}
ok(false, "Exeception not thrown for invalid brightness level.");
}
function testSettingBrightness() {
var newBright = 0.312;
navigator.mozPower.screenBrightness = newBright;
ok(fuzzyEq(newBright, navigator.mozPower.screenBrightness),
"Set new brightness value.");
}
function startTests() {
testInvalidBrightness1();
testInvalidBrightness2();
testSettingBrightness();
SimpleTest.finish();
}
function fuzzyEq(a, b) {
var epsilon = 0.002;
return Math.abs(a - b) < epsilon;
}
SimpleTest.expectAssertions(0, 9);
SimpleTest.waitForExplicitFinish();
if (SpecialPowers.hasPermission("power", document)) {
// Currently only applicable on FxOS
if (navigator.userAgent.indexOf("Mobile") != -1 &&
navigator.appVersion.indexOf("Android") == -1) {
startTests();
} else {
ok(true, "mozPower on Firefox OS only.");
SimpleTest.finish();
}
} else {
// Add the permission and reload so it's propogated
SpecialPowers.addPermission("power", true, document);
window.location.reload();
}
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,65 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test Enabling/Disabling Screen with Power Management API</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script type="application/javascript">
"use strict";
function testEnableScreen() {
try {
navigator.mozPower.screenEnabled = true;
ok(navigator.mozPower.screenEnabled === true, "Enabled screen.");
} catch (e) {
ok(false, "Unexpected exception trying to enable screen.");
}
}
function testDisableScreen() {
try {
navigator.mozPower.screenEnabled = false;
ok(navigator.mozPower.screenEnabled === false, "Disabled screen.");
} catch (e) {
ok(false, "Unexpected exception trying to disable screen.");
}
}
function startTests() {
// Make sure we don't suspend
navigator.mozPower.cpuSleepAllowed = false;
testDisableScreen();
testEnableScreen();
SimpleTest.finish();
}
SimpleTest.expectAssertions(0, 9);
SimpleTest.waitForExplicitFinish();
if (SpecialPowers.hasPermission("power", document)) {
// Currently only applicable on FxOS
if (navigator.userAgent.indexOf("Mobile") != -1 &&
navigator.appVersion.indexOf("Android") == -1) {
startTests();
} else {
ok(true, "mozPower on Firefox OS only.");
SimpleTest.finish();
}
} else {
// Add the permission and reload so it's propogated
SpecialPowers.addPermission("power", true, document);
window.location.reload();
}
</script>
</pre>
</body>
</html>