michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: waitForExplicitFinish(); michael@0: michael@0: let kUrlSource = "http://mochi.test:8888/"; michael@0: let kDataSource = "data:text/html,"; michael@0: michael@0: let gOldPref; michael@0: let gWin, gWin1, gWin2; michael@0: let gTab, gTab1, gTab2; michael@0: let gLock, gLock1, gLock2; michael@0: let gCurStepIndex = -1; michael@0: let gSteps = [ michael@0: function basicWakeLock() { michael@0: gTab = gBrowser.addTab(kUrlSource); michael@0: gWin = gBrowser.getBrowserForTab(gTab).contentWindow; michael@0: let browser = gBrowser.getBrowserForTab(gTab); michael@0: michael@0: browser.addEventListener("load", function onLoad(e) { michael@0: browser.removeEventListener("load", onLoad, true); michael@0: let nav = gWin.navigator; michael@0: let power = nav.mozPower; michael@0: gLock = nav.requestWakeLock("test"); michael@0: michael@0: ok(gLock != null, michael@0: "navigator.requestWakeLock should return a wake lock"); michael@0: is(gLock.topic, "test", michael@0: "wake lock should remember the locked topic"); michael@0: isnot(power.getWakeLockState("test"), "unlocked", michael@0: "topic is locked"); michael@0: michael@0: gLock.unlock(); michael@0: michael@0: is(gLock.topic, "test", michael@0: "wake lock should remember the locked topic even after unlock"); michael@0: is(power.getWakeLockState("test"), "unlocked", michael@0: "topic is unlocked"); michael@0: michael@0: try { michael@0: gLock.unlock(); michael@0: ok(false, "Should have thrown an error."); michael@0: } catch (e) { michael@0: is(e.name, "InvalidStateError", "double unlock should throw InvalidStateError"); michael@0: is(e.code, DOMException.INVALID_STATE_ERR, "double unlock should throw InvalidStateError"); michael@0: } michael@0: michael@0: gBrowser.removeTab(gTab); michael@0: michael@0: executeSoon(runNextStep); michael@0: }, true); michael@0: }, michael@0: function multiWakeLock() { michael@0: gTab = gBrowser.addTab(kUrlSource); michael@0: gWin = gBrowser.getBrowserForTab(gTab).contentWindow; michael@0: let browser = gBrowser.getBrowserForTab(gTab); michael@0: michael@0: browser.addEventListener("load", function onLoad(e) { michael@0: browser.removeEventListener("load", onLoad, true); michael@0: let nav = gWin.navigator; michael@0: let power = nav.mozPower; michael@0: let count = 0; michael@0: power.addWakeLockListener(function onWakeLockEvent(topic, state) { michael@0: is(topic, "test", "gLock topic is test"); michael@0: ok(state == "unlocked" || michael@0: state == "locked-foreground" || michael@0: state == "locked-background", michael@0: "wake lock should be either locked or unlocked"); michael@0: count++; michael@0: if (state == "locked-foreground" || michael@0: state == "locked-background") { michael@0: is(count, 1, michael@0: "wake lock should be locked and the listener should only fire once"); michael@0: } michael@0: if (state == "unlocked") { michael@0: is(count, 2, michael@0: "wake lock should be unlocked and the listener should only fire once"); michael@0: michael@0: ok(power.getWakeLockState("test") == "unlocked", michael@0: "topic is unlocked"); michael@0: power.removeWakeLockListener(onWakeLockEvent); michael@0: gBrowser.removeTab(gTab); michael@0: executeSoon(runNextStep); michael@0: } michael@0: }); michael@0: michael@0: gLock1 = nav.requestWakeLock("test"); michael@0: isnot(power.getWakeLockState("test"), "unlocked", michael@0: "topic is locked"); michael@0: michael@0: gLock2 = nav.requestWakeLock("test"); michael@0: isnot(power.getWakeLockState("test"), "unlocked", michael@0: "topic is locked"); michael@0: michael@0: gLock1.unlock(); michael@0: isnot(power.getWakeLockState("test"), "unlocked", michael@0: "topic is locked"); michael@0: michael@0: gLock2.unlock(); michael@0: }, true); michael@0: }, michael@0: function crossTabWakeLock1() { michael@0: gTab1 = gBrowser.addTab(kUrlSource); michael@0: gWin1 = gBrowser.getBrowserForTab(gTab1).contentWindow; michael@0: gTab2 = gBrowser.addTab(kUrlSource); michael@0: gWin2 = gBrowser.getBrowserForTab(gTab2).contentWindow; michael@0: michael@0: gBrowser.selectedTab = gTab1; michael@0: let browser = gBrowser.getBrowserForTab(gTab2); michael@0: michael@0: browser.addEventListener("load", function onLoad(e) { michael@0: browser.removeEventListener("load", onLoad, true); michael@0: gLock2 = gWin2.navigator.requestWakeLock("test"); michael@0: is(gWin2.document.hidden, true, michael@0: "window is background") michael@0: is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-background", michael@0: "wake lock is background"); michael@0: let doc2 = gWin2.document; michael@0: doc2.addEventListener("visibilitychange", function onVisibilityChange(e) { michael@0: if (!doc2.hidden) { michael@0: doc2.removeEventListener("visibilitychange", onVisibilityChange); michael@0: executeSoon(runNextStep); michael@0: } michael@0: }); michael@0: gBrowser.selectedTab = gTab2; michael@0: }, true); michael@0: }, michael@0: function crossTabWakeLock2() { michael@0: is(gWin2.document.hidden, false, michael@0: "window is foreground") michael@0: is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-foreground", michael@0: "wake lock is foreground"); michael@0: gWin2.addEventListener("pagehide", function onPageHide(e) { michael@0: gWin2.removeEventListener("pagehide", onPageHide, true); michael@0: executeSoon(runNextStep); michael@0: }, true); michael@0: gWin2.addEventListener("pageshow", function onPageShow(e) { michael@0: gWin2.removeEventListener("pageshow", onPageShow, true); michael@0: executeSoon(runNextStep); michael@0: }, true); michael@0: gWin2.location = kDataSource; michael@0: }, michael@0: function crossTabWakeLock3() { michael@0: is(gWin1.navigator.mozPower.getWakeLockState("test"), "unlocked", michael@0: "wake lock should auto-unlock when page is unloaded"); michael@0: gWin2.back(); michael@0: // runNextStep called in onPageShow michael@0: }, michael@0: function crossTabWakeLock4() { michael@0: is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground", michael@0: "wake lock should auto-reacquire when page is available again"); michael@0: gBrowser.selectedTab = gTab1; michael@0: executeSoon(runNextStep); michael@0: }, michael@0: function crossTabWakeLock5() { michael@0: // Test again in background tab michael@0: is(gWin2.document.hidden, true, michael@0: "window is background") michael@0: is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-background", michael@0: "wake lock is background"); michael@0: gWin2.addEventListener("pagehide", function onPageHide(e) { michael@0: gWin2.removeEventListener("pagehide", onPageHide, true); michael@0: executeSoon(runNextStep); michael@0: }, true); michael@0: gWin2.addEventListener("pageshow", function onPageShow(e) { michael@0: gWin2.removeEventListener("pageshow", onPageShow, true); michael@0: executeSoon(runNextStep); michael@0: }, true); michael@0: gWin2.location = kDataSource; michael@0: }, michael@0: function crossTabWakeLock6() { michael@0: is(gWin1.navigator.mozPower.getWakeLockState("test"), "unlocked", michael@0: "wake lock should auto-unlock when page is unloaded"); michael@0: gWin2.back(); michael@0: // runNextStep called in onPageShow michael@0: }, michael@0: function crossTabWakeLock7() { michael@0: is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-background", michael@0: "wake lock should auto-reacquire when page is available again"); michael@0: gLock2.unlock(); michael@0: gBrowser.selectedTab = gTab2; michael@0: executeSoon(runNextStep); michael@0: }, michael@0: function crossTabWakeLock8() { michael@0: is(gWin1.document.hidden, true, michael@0: "gWin1 is background"); michael@0: is(gWin2.document.hidden, false, michael@0: "gWin2 is foreground"); michael@0: michael@0: gLock1 = gWin1.navigator.requestWakeLock("test"); michael@0: gLock2 = gWin2.navigator.requestWakeLock("test"); michael@0: michael@0: is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground", michael@0: "topic is locked-foreground when one page is foreground and one is background"); michael@0: michael@0: gLock2.unlock(); michael@0: michael@0: is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-background", michael@0: "topic is locked-background when all locks are background"); michael@0: michael@0: gLock2 = gWin2.navigator.requestWakeLock("test"); michael@0: michael@0: is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground", michael@0: "topic is locked-foreground when one page is foreground and one is background"); michael@0: michael@0: gLock1.unlock(); michael@0: michael@0: is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground", michael@0: "topic is locked-foreground"); michael@0: michael@0: gBrowser.removeTab(gTab1); michael@0: gBrowser.removeTab(gTab2); michael@0: executeSoon(runNextStep); michael@0: }, michael@0: ]; michael@0: michael@0: function runNextStep() { michael@0: gCurStepIndex++; michael@0: if (gCurStepIndex < gSteps.length) { michael@0: gSteps[gCurStepIndex](); michael@0: } else { michael@0: finish(); michael@0: } michael@0: } michael@0: michael@0: function test() { michael@0: SpecialPowers.pushPermissions([ michael@0: {type: "power", allow: true, context: kUrlSource} michael@0: ], function () { michael@0: SpecialPowers.pushPrefEnv({"set": [["dom.wakelock.enabled", true]]}, michael@0: runNextStep); michael@0: }); michael@0: }