Comment 154 for bug 604635

Revision history for this message
In , Justin-lebar+bug (justin-lebar+bug) wrote :

diff --git a/dom/power/test/power_wakelock_child.html b/dom/power/test/power_wakelock_child.html
new file mode 100644
--- /dev/null
+++ b/dom/power/test/power_wakelock_child.html
@@ -0,0 +1,11 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for Power API</title>
+ <script type="application/javascript">
+ lock = navigator.requestWakeLock("test");
+ </script>
+</head>
+<body>
+</body>
+</html>
diff --git a/dom/power/test/test_power_basics.html b/dom/power/test/test_power_basics.html
--- a/dom/power/test/test_power_basics.html
+++ b/dom/power/test/test_power_basics.html
@@ -9,14 +9,102 @@
 <p id="display"></p>
 <div id="content" style="display: none">
 </div>
 <pre id="test">
 <script type="application/javascript">

 /** Test for Power API **/

+function checkWakeLock() {
+ var count = 0;
+ var callback = function wakeLockListener(topic, state) {
+ 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.mozPower.addWakeLockListener(callback);
+
+ var lock1, lock2;
+ ok(lock1 = navigator.requestWakeLock("test"), "navigator.requestWakeLock should return a wakelock");
+ ok(lock2 = navigator.requestWakeLock("test"), "navigator.requestWakeLock should return a wakelock");
+ setTimeout(function() {
+ is(count, 1, "WakeLockListener should only fire once");

+ checkProperty(lock1, "topic");
+ checkProperty(lock2, "topic");

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.

+function test() {
+ SpecialPowers.setCharPref("dom.mozPowerWhitelist", "http://mochi.test:8888");

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.waitForExplicitFinish();
+ checkInterface("PowerManager");
+ checkInterface("WakeLock");
+ checkInterface("WakeLockListener");
+ checkProperty(navigator, "mozPower");
+ checkProperty(navigator, "requestWakeLock");
+ checkProperty(navigator.mozPower, "getWakeLockState");
+ checkProperty(navigator.mozPower, "addWakeLockListener");
+ checkProperty(navigator.mozPower, "removeWakeLockListener");
+ 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 () {
  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?