We don't need these tests; if lock1.topic doesn't exist, then reading it below will throw an exception and break the test.
+ is(lock1.topic, "test", "wakelock should remember the locked topic");
+ is(lock2.topic, "test", "wakelock should remember the locked topic");
+ ok(navigator.mozPower.getWakeLockState("test") == "foreground", "mozPower should remember the locked topic");
+ lock1.unlock();
+ lock2.unlock();
I'd like a test that when you unlock one of two held locks, we don't get a topic unlock notification.
+ setTimeout(function() {
+ ok(navigator.mozPower.getWakeLockState("test") == "notheld", "all locks should have been unlocked");
+ is(count, 0, "WakeLockListener should only fire once");
+ navigator.mozPower.removeWakeLockListener(callback);
+ ok(lock1 = navigator.requestWakeLock("test"), "navigator.requestWakeLock should return a wakelock");
+ setTimeout(function() {
+ isnot(count, 1, "navigator.mozPower.removeWakeLockListener should have removed the listener");
+ lock1.unlock();
+ checkWakeLockNavigation();
+ }, 1000);
+ }, 1000);
+ }, 1000);
+}
+
+function checkWakeLockNavigation() {
+ childWindow = window.open("power_wakelock_child.html");
+ SimpleTest.waitForFocus(function () {
Need to wait for the child window's onload event to fire.
+ ok(navigator.mozPower.getWakeLockState("test") == "foreground", "lock should be held by child window");
+ childWindow.location = "about:blank";
+ // XXX how to wait page transition?
Add an onload listener and you'll hear it.
+ setTimeout(function() {
+ ok(navigator.mozPower.getWakeLockState("test") == "notheld", "lock should be canceled when pagehide.");
+ childWindow.history.back();
+ // XXX how to wait page transition?
Onload should do it, again.
+ setTimeout(function () {
+ ok(navigator.mozPower.getWakeLockState("test") == "foreground", "lock should be reacquired when pageshow.");
+ childWindow.close();
+ ok(navigator.mozPower.getWakeLockState("test") == "notheld", "lock should be canceled when documents are unloaded.");
+ SimpleTest.finish();
+ }, 1000);
+ }, 1000);
+ }, childWindow);
+}
There's no test of background? You may need to open a new tab. I'm not quite sure how to do that from a mochitest, actually. You may need to write a browser-chrome test, which would be unfortunate.
I'd also like a test of the lock levels: Acquire a foreground and background lock, it should say foreground. Then release the foreground lock, and it should drop to background. Then acquire another foreground lock, and it should switch again. And so on.
All of these setTimeout(1000) calls have to go; it's just asking for randomorange.
These asynchronous tests get kind of hairy, unfortunately. You can use generators (see runTest() in test_bug500328.html), or you can roll your own with something like:
curTestIndex = 0;
tests = [
function() { // this is part 1 },
function() { // this is part 2 }
...
];
function onEventReceived () {
SimpleTest.executeSoon(function() { tests[curTestIndex]() });
curTestIndex++;
}
The SimpleTest.executeSoon (equivalent to a setTimeout(0)) is there to force the event loop to spin before running the next test step. You may not need it, but if you get rid of it, you'll have to do
function onEventReceived () {
curTestIndex++;
tests[curTestIndex - 1]();
}
because, for example, the last line of part 1 might trigger the event which triggers part 2 synchronously. So we'll run test2 before test1 finishes. Thus you have to increment before you run the test.
You can probably listen to the wake lock events (and onload on the popup window) to know when you can go on.
Feel free to ask for help if you get stuck with this test. It's not easy to get right.
Does this need to be checked in tomorrow, or are we no longer waiting on this for the demo?
diff --git a/dom/power/ test/power_ wakelock_ child.html b/dom/power/ test/power_ wakelock_ child.html test/power_ wakelock_ child.html on/javascript" > requestWakeLock ("test" ); test/test_ power_basics. html b/dom/power/ test/test_ power_basics. html test/test_ power_basics. html test/test_ power_basics. html on/javascript" >
new file mode 100644
--- /dev/null
+++ b/dom/power/
@@ -0,0 +1,11 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for Power API</title>
+ <script type="applicati
+ lock = navigator.
+ </script>
+</head>
+<body>
+</body>
+</html>
diff --git a/dom/power/
--- a/dom/power/
+++ b/dom/power/
@@ -9,14 +9,102 @@
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="applicati
/** Test for Power API **/
+function checkWakeLock() { r(topic, state) { mozPower. addWakeLockList ener(callback) ; requestWakeLock ("test" ), "navigator. requestWakeLock should return a wakelock"); requestWakeLock ("test" ), "navigator. requestWakeLock should return a wakelock"); function( ) {
+ var count = 0;
+ var callback = function wakeLockListene
+ is(topic, "test", "WakeLockListener should receive the lock topic");
+ switch (state) {
+ case "foreground":
+ count += 1;
+ break;
+ case "background":
+ count += 1;
+ break;
+ case "notheld":
+ count -= 1;
+ break;
+ }
+ }
+ navigator.
+
+ var lock1, lock2;
+ ok(lock1 = navigator.
+ ok(lock2 = navigator.
+ setTimeout(
+ is(count, 1, "WakeLockListener should only fire once");
+ checkProperty( lock1, "topic"); lock2, "topic");
+ checkProperty(
We don't need these tests; if lock1.topic doesn't exist, then reading it below will throw an exception and break the test.
+ is(lock1.topic, "test", "wakelock should remember the locked topic"); mozPower. getWakeLockStat e("test" ) == "foreground", "mozPower should remember the locked topic");
+ is(lock2.topic, "test", "wakelock should remember the locked topic");
+ ok(navigator.
+ lock1.unlock();
+ lock2.unlock();
I'd like a test that when you unlock one of two held locks, we don't get a topic unlock notification.
+ setTimeout( function( ) { mozPower. getWakeLockStat e("test" ) == "notheld", "all locks should have been unlocked"); mozPower. removeWakeLockL istener( callback) ; requestWakeLock ("test" ), "navigator. requestWakeLock should return a wakelock"); function( ) { mozPower. removeWakeLockL istener should have removed the listener"); vigation( ); vigation( ) { open("power_ wakelock_ child.html" ); waitForFocus( function () {
+ ok(navigator.
+ is(count, 0, "WakeLockListener should only fire once");
+ navigator.
+ ok(lock1 = navigator.
+ setTimeout(
+ isnot(count, 1, "navigator.
+ lock1.unlock();
+ checkWakeLockNa
+ }, 1000);
+ }, 1000);
+ }, 1000);
+}
+
+function checkWakeLockNa
+ childWindow = window.
+ SimpleTest.
Need to wait for the child window's onload event to fire.
+ ok(navigator. mozPower. getWakeLockStat e("test" ) == "foreground", "lock should be held by child window"); location = "about:blank";
+ childWindow.
+ // XXX how to wait page transition?
Add an onload listener and you'll hear it.
+ setTimeout( function( ) { mozPower. getWakeLockStat e("test" ) == "notheld", "lock should be canceled when pagehide."); history. back();
+ ok(navigator.
+ childWindow.
+ // XXX how to wait page transition?
Onload should do it, again.
+ setTimeout(function () { mozPower. getWakeLockStat e("test" ) == "foreground", "lock should be reacquired when pageshow."); close() ; mozPower. getWakeLockStat e("test" ) == "notheld", "lock should be canceled when documents are unloaded."); finish( );
+ ok(navigator.
+ childWindow.
+ ok(navigator.
+ SimpleTest.
+ }, 1000);
+ }, 1000);
+ }, childWindow);
+}
There's no test of background? You may need to open a new tab. I'm not quite sure how to do that from a mochitest, actually. You may need to write a browser-chrome test, which would be unfortunate.
+function test() { setCharPref( "dom.mozPowerWh itelist" , "http:// mochi.test: 8888");
+ SpecialPowers.
The mochitest location has changed in the past. Let's use |'http:// ' + location.host| instead and hope that we never serve mochitests over https. :)
+ SimpleTest. waitForExplicit Finish( ); "PowerManager" ); "WakeLock" ); "WakeLockListen er"); navigator, "mozPower"); navigator, "requestWakeLock"); navigator. mozPower, "getWakeLockSta te"); navigator. mozPower, "addWakeLockLis tener") ; navigator. mozPower, "removeWakeLock Listener" );
+ checkInterface(
+ checkInterface(
+ checkInterface(
+ checkProperty(
+ checkProperty(
+ checkProperty(
+ checkProperty(
+ checkProperty(
+ checkWakeLock();
I'd also like a test of the lock levels: Acquire a foreground and background lock, it should say foreground. Then release the foreground lock, and it should drop to background. Then acquire another foreground lock, and it should switch again. And so on.
All of these setTimeout(1000) calls have to go; it's just asking for randomorange.
These asynchronous tests get kind of hairy, unfortunately. You can use generators (see runTest() in test_bug500328. html), or you can roll your own with something like:
curTestIndex = 0;
tests = [
function() { // this is part 1 },
function() { // this is part 2 }
...
];
function onEventReceived () { executeSoon( function( ) { tests[curTestIn dex]() });
SimpleTest.
curTestIndex++;
}
The SimpleTest. executeSoon (equivalent to a setTimeout(0)) is there to force the event loop to spin before running the next test step. You may not need it, but if you get rid of it, you'll have to do
function onEventReceived () { curTestIndex - 1]();
curTestIndex++;
tests[
}
because, for example, the last line of part 1 might trigger the event which triggers part 2 synchronously. So we'll run test2 before test1 finishes. Thus you have to increment before you run the test.
You can probably listen to the wake lock events (and onload on the popup window) to know when you can go on.
Feel free to ask for help if you get stuck with this test. It's not easy to get right.
Does this need to be checked in tomorrow, or are we no longer waiting on this for the demo?