dom/power/test/browser_wakelocks.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 waitForExplicitFinish();
michael@0 8
michael@0 9 let kUrlSource = "http://mochi.test:8888/";
michael@0 10 let kDataSource = "data:text/html,";
michael@0 11
michael@0 12 let gOldPref;
michael@0 13 let gWin, gWin1, gWin2;
michael@0 14 let gTab, gTab1, gTab2;
michael@0 15 let gLock, gLock1, gLock2;
michael@0 16 let gCurStepIndex = -1;
michael@0 17 let gSteps = [
michael@0 18 function basicWakeLock() {
michael@0 19 gTab = gBrowser.addTab(kUrlSource);
michael@0 20 gWin = gBrowser.getBrowserForTab(gTab).contentWindow;
michael@0 21 let browser = gBrowser.getBrowserForTab(gTab);
michael@0 22
michael@0 23 browser.addEventListener("load", function onLoad(e) {
michael@0 24 browser.removeEventListener("load", onLoad, true);
michael@0 25 let nav = gWin.navigator;
michael@0 26 let power = nav.mozPower;
michael@0 27 gLock = nav.requestWakeLock("test");
michael@0 28
michael@0 29 ok(gLock != null,
michael@0 30 "navigator.requestWakeLock should return a wake lock");
michael@0 31 is(gLock.topic, "test",
michael@0 32 "wake lock should remember the locked topic");
michael@0 33 isnot(power.getWakeLockState("test"), "unlocked",
michael@0 34 "topic is locked");
michael@0 35
michael@0 36 gLock.unlock();
michael@0 37
michael@0 38 is(gLock.topic, "test",
michael@0 39 "wake lock should remember the locked topic even after unlock");
michael@0 40 is(power.getWakeLockState("test"), "unlocked",
michael@0 41 "topic is unlocked");
michael@0 42
michael@0 43 try {
michael@0 44 gLock.unlock();
michael@0 45 ok(false, "Should have thrown an error.");
michael@0 46 } catch (e) {
michael@0 47 is(e.name, "InvalidStateError", "double unlock should throw InvalidStateError");
michael@0 48 is(e.code, DOMException.INVALID_STATE_ERR, "double unlock should throw InvalidStateError");
michael@0 49 }
michael@0 50
michael@0 51 gBrowser.removeTab(gTab);
michael@0 52
michael@0 53 executeSoon(runNextStep);
michael@0 54 }, true);
michael@0 55 },
michael@0 56 function multiWakeLock() {
michael@0 57 gTab = gBrowser.addTab(kUrlSource);
michael@0 58 gWin = gBrowser.getBrowserForTab(gTab).contentWindow;
michael@0 59 let browser = gBrowser.getBrowserForTab(gTab);
michael@0 60
michael@0 61 browser.addEventListener("load", function onLoad(e) {
michael@0 62 browser.removeEventListener("load", onLoad, true);
michael@0 63 let nav = gWin.navigator;
michael@0 64 let power = nav.mozPower;
michael@0 65 let count = 0;
michael@0 66 power.addWakeLockListener(function onWakeLockEvent(topic, state) {
michael@0 67 is(topic, "test", "gLock topic is test");
michael@0 68 ok(state == "unlocked" ||
michael@0 69 state == "locked-foreground" ||
michael@0 70 state == "locked-background",
michael@0 71 "wake lock should be either locked or unlocked");
michael@0 72 count++;
michael@0 73 if (state == "locked-foreground" ||
michael@0 74 state == "locked-background") {
michael@0 75 is(count, 1,
michael@0 76 "wake lock should be locked and the listener should only fire once");
michael@0 77 }
michael@0 78 if (state == "unlocked") {
michael@0 79 is(count, 2,
michael@0 80 "wake lock should be unlocked and the listener should only fire once");
michael@0 81
michael@0 82 ok(power.getWakeLockState("test") == "unlocked",
michael@0 83 "topic is unlocked");
michael@0 84 power.removeWakeLockListener(onWakeLockEvent);
michael@0 85 gBrowser.removeTab(gTab);
michael@0 86 executeSoon(runNextStep);
michael@0 87 }
michael@0 88 });
michael@0 89
michael@0 90 gLock1 = nav.requestWakeLock("test");
michael@0 91 isnot(power.getWakeLockState("test"), "unlocked",
michael@0 92 "topic is locked");
michael@0 93
michael@0 94 gLock2 = nav.requestWakeLock("test");
michael@0 95 isnot(power.getWakeLockState("test"), "unlocked",
michael@0 96 "topic is locked");
michael@0 97
michael@0 98 gLock1.unlock();
michael@0 99 isnot(power.getWakeLockState("test"), "unlocked",
michael@0 100 "topic is locked");
michael@0 101
michael@0 102 gLock2.unlock();
michael@0 103 }, true);
michael@0 104 },
michael@0 105 function crossTabWakeLock1() {
michael@0 106 gTab1 = gBrowser.addTab(kUrlSource);
michael@0 107 gWin1 = gBrowser.getBrowserForTab(gTab1).contentWindow;
michael@0 108 gTab2 = gBrowser.addTab(kUrlSource);
michael@0 109 gWin2 = gBrowser.getBrowserForTab(gTab2).contentWindow;
michael@0 110
michael@0 111 gBrowser.selectedTab = gTab1;
michael@0 112 let browser = gBrowser.getBrowserForTab(gTab2);
michael@0 113
michael@0 114 browser.addEventListener("load", function onLoad(e) {
michael@0 115 browser.removeEventListener("load", onLoad, true);
michael@0 116 gLock2 = gWin2.navigator.requestWakeLock("test");
michael@0 117 is(gWin2.document.hidden, true,
michael@0 118 "window is background")
michael@0 119 is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-background",
michael@0 120 "wake lock is background");
michael@0 121 let doc2 = gWin2.document;
michael@0 122 doc2.addEventListener("visibilitychange", function onVisibilityChange(e) {
michael@0 123 if (!doc2.hidden) {
michael@0 124 doc2.removeEventListener("visibilitychange", onVisibilityChange);
michael@0 125 executeSoon(runNextStep);
michael@0 126 }
michael@0 127 });
michael@0 128 gBrowser.selectedTab = gTab2;
michael@0 129 }, true);
michael@0 130 },
michael@0 131 function crossTabWakeLock2() {
michael@0 132 is(gWin2.document.hidden, false,
michael@0 133 "window is foreground")
michael@0 134 is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
michael@0 135 "wake lock is foreground");
michael@0 136 gWin2.addEventListener("pagehide", function onPageHide(e) {
michael@0 137 gWin2.removeEventListener("pagehide", onPageHide, true);
michael@0 138 executeSoon(runNextStep);
michael@0 139 }, true);
michael@0 140 gWin2.addEventListener("pageshow", function onPageShow(e) {
michael@0 141 gWin2.removeEventListener("pageshow", onPageShow, true);
michael@0 142 executeSoon(runNextStep);
michael@0 143 }, true);
michael@0 144 gWin2.location = kDataSource;
michael@0 145 },
michael@0 146 function crossTabWakeLock3() {
michael@0 147 is(gWin1.navigator.mozPower.getWakeLockState("test"), "unlocked",
michael@0 148 "wake lock should auto-unlock when page is unloaded");
michael@0 149 gWin2.back();
michael@0 150 // runNextStep called in onPageShow
michael@0 151 },
michael@0 152 function crossTabWakeLock4() {
michael@0 153 is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
michael@0 154 "wake lock should auto-reacquire when page is available again");
michael@0 155 gBrowser.selectedTab = gTab1;
michael@0 156 executeSoon(runNextStep);
michael@0 157 },
michael@0 158 function crossTabWakeLock5() {
michael@0 159 // Test again in background tab
michael@0 160 is(gWin2.document.hidden, true,
michael@0 161 "window is background")
michael@0 162 is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-background",
michael@0 163 "wake lock is background");
michael@0 164 gWin2.addEventListener("pagehide", function onPageHide(e) {
michael@0 165 gWin2.removeEventListener("pagehide", onPageHide, true);
michael@0 166 executeSoon(runNextStep);
michael@0 167 }, true);
michael@0 168 gWin2.addEventListener("pageshow", function onPageShow(e) {
michael@0 169 gWin2.removeEventListener("pageshow", onPageShow, true);
michael@0 170 executeSoon(runNextStep);
michael@0 171 }, true);
michael@0 172 gWin2.location = kDataSource;
michael@0 173 },
michael@0 174 function crossTabWakeLock6() {
michael@0 175 is(gWin1.navigator.mozPower.getWakeLockState("test"), "unlocked",
michael@0 176 "wake lock should auto-unlock when page is unloaded");
michael@0 177 gWin2.back();
michael@0 178 // runNextStep called in onPageShow
michael@0 179 },
michael@0 180 function crossTabWakeLock7() {
michael@0 181 is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-background",
michael@0 182 "wake lock should auto-reacquire when page is available again");
michael@0 183 gLock2.unlock();
michael@0 184 gBrowser.selectedTab = gTab2;
michael@0 185 executeSoon(runNextStep);
michael@0 186 },
michael@0 187 function crossTabWakeLock8() {
michael@0 188 is(gWin1.document.hidden, true,
michael@0 189 "gWin1 is background");
michael@0 190 is(gWin2.document.hidden, false,
michael@0 191 "gWin2 is foreground");
michael@0 192
michael@0 193 gLock1 = gWin1.navigator.requestWakeLock("test");
michael@0 194 gLock2 = gWin2.navigator.requestWakeLock("test");
michael@0 195
michael@0 196 is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
michael@0 197 "topic is locked-foreground when one page is foreground and one is background");
michael@0 198
michael@0 199 gLock2.unlock();
michael@0 200
michael@0 201 is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-background",
michael@0 202 "topic is locked-background when all locks are background");
michael@0 203
michael@0 204 gLock2 = gWin2.navigator.requestWakeLock("test");
michael@0 205
michael@0 206 is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
michael@0 207 "topic is locked-foreground when one page is foreground and one is background");
michael@0 208
michael@0 209 gLock1.unlock();
michael@0 210
michael@0 211 is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
michael@0 212 "topic is locked-foreground");
michael@0 213
michael@0 214 gBrowser.removeTab(gTab1);
michael@0 215 gBrowser.removeTab(gTab2);
michael@0 216 executeSoon(runNextStep);
michael@0 217 },
michael@0 218 ];
michael@0 219
michael@0 220 function runNextStep() {
michael@0 221 gCurStepIndex++;
michael@0 222 if (gCurStepIndex < gSteps.length) {
michael@0 223 gSteps[gCurStepIndex]();
michael@0 224 } else {
michael@0 225 finish();
michael@0 226 }
michael@0 227 }
michael@0 228
michael@0 229 function test() {
michael@0 230 SpecialPowers.pushPermissions([
michael@0 231 {type: "power", allow: true, context: kUrlSource}
michael@0 232 ], function () {
michael@0 233 SpecialPowers.pushPrefEnv({"set": [["dom.wakelock.enabled", true]]},
michael@0 234 runNextStep);
michael@0 235 });
michael@0 236 }

mercurial