dom/power/test/browser_wakelocks.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial