summaryrefslogtreecommitdiffstats
path: root/dom/power/test/browser_wakelocks.js
blob: 5bd4de67c9f488321453234cbe01cf87e990578b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

waitForExplicitFinish();

var kUrlSource = "http://mochi.test:8888/";
var kDataSource = "data:text/html,";

var gOldPref;
var gWin, gWin1, gWin2;
var gTab, gTab1, gTab2;
var gLock, gLock1, gLock2;
var gCurStepIndex = -1;
var gSteps = [
  function basicWakeLock() {
    gTab = gBrowser.addTab(kUrlSource);
    gWin = gBrowser.getBrowserForTab(gTab).contentWindow;
    let browser = gBrowser.getBrowserForTab(gTab);

    browser.addEventListener("load", function onLoad(e) {
      browser.removeEventListener("load", onLoad, true);
      let nav = gWin.navigator;
      let power = nav.mozPower;
      gLock = nav.requestWakeLock("test");

      ok(gLock != null,
         "navigator.requestWakeLock should return a wake lock");
      is(gLock.topic, "test",
         "wake lock should remember the locked topic");
      isnot(power.getWakeLockState("test"), "unlocked",
            "topic is locked");

      gLock.unlock();

      is(gLock.topic, "test",
         "wake lock should remember the locked topic even after unlock");
      is(power.getWakeLockState("test"), "unlocked",
         "topic is unlocked");

      try {
        gLock.unlock();
        ok(false, "Should have thrown an error.");
      } catch (e) {
        is(e.name, "InvalidStateError", "double unlock should throw InvalidStateError");
        is(e.code, DOMException.INVALID_STATE_ERR, "double unlock should throw InvalidStateError");
      }

      gBrowser.removeTab(gTab);

      executeSoon(runNextStep);
    }, true);
  },
  function multiWakeLock() {
    gTab = gBrowser.addTab(kUrlSource);
    gWin = gBrowser.getBrowserForTab(gTab).contentWindow;
    let browser = gBrowser.getBrowserForTab(gTab);

    browser.addEventListener("load", function onLoad(e) {
      browser.removeEventListener("load", onLoad, true);
      let nav = gWin.navigator;
      let power = nav.mozPower;
      let count = 0;
      power.addWakeLockListener(function onWakeLockEvent(topic, state) {
        is(topic, "test", "gLock topic is test");
        ok(state == "unlocked" ||
           state == "locked-foreground" ||
           state == "locked-background",
           "wake lock should be either locked or unlocked");
        count++;
        if (state == "locked-foreground" ||
            state == "locked-background") {
          is(count, 1,
             "wake lock should be locked and the listener should only fire once");
        }
        if (state == "unlocked") {
          is(count, 2,
             "wake lock should be unlocked and the listener should only fire once");

          ok(power.getWakeLockState("test") == "unlocked",
             "topic is unlocked");
          power.removeWakeLockListener(onWakeLockEvent);
          gBrowser.removeTab(gTab);
          executeSoon(runNextStep);
        }
      });

      gLock1 = nav.requestWakeLock("test");
      isnot(power.getWakeLockState("test"), "unlocked",
            "topic is locked");

      gLock2 = nav.requestWakeLock("test");
      isnot(power.getWakeLockState("test"), "unlocked",
            "topic is locked");

      gLock1.unlock();
      isnot(power.getWakeLockState("test"), "unlocked",
            "topic is locked");

      gLock2.unlock();
    }, true);
  },
  function crossTabWakeLock1() {
    gTab1 = gBrowser.addTab(kUrlSource);
    gWin1 = gBrowser.getBrowserForTab(gTab1).contentWindow;
    gTab2 = gBrowser.addTab(kUrlSource);
    gWin2 = gBrowser.getBrowserForTab(gTab2).contentWindow;

    gBrowser.selectedTab = gTab1;
    let browser = gBrowser.getBrowserForTab(gTab2);

    browser.addEventListener("load", function onLoad(e) {
      browser.removeEventListener("load", onLoad, true);
      gLock2 = gWin2.navigator.requestWakeLock("test");
      is(gWin2.document.hidden, true,
         "window is background")
      is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-background",
         "wake lock is background");
      let doc2 = gWin2.document;
      doc2.addEventListener("visibilitychange", function onVisibilityChange(e) {
        if (!doc2.hidden) {
          doc2.removeEventListener("visibilitychange", onVisibilityChange);
          executeSoon(runNextStep);
        }
      });
      gBrowser.selectedTab = gTab2;
    }, true);
  },
  function crossTabWakeLock2() {
    is(gWin2.document.hidden, false,
       "window is foreground")
    is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
      "wake lock is foreground");
    gWin2.addEventListener("pagehide", function onPageHide(e) {
      gWin2.removeEventListener("pagehide", onPageHide, true);
      executeSoon(runNextStep);
    }, true);
    gWin2.addEventListener("pageshow", function onPageShow(e) {
      gWin2.removeEventListener("pageshow", onPageShow, true);
      executeSoon(runNextStep);
    }, true);
    gWin2.location = kDataSource;
  },
  function crossTabWakeLock3() {
    is(gWin1.navigator.mozPower.getWakeLockState("test"), "unlocked",
       "wake lock should auto-unlock when page is unloaded");
    gWin2.back();
    // runNextStep called in onPageShow
  },
  function crossTabWakeLock4() {
    is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
       "wake lock should auto-reacquire when page is available again");
    gBrowser.selectedTab = gTab1;
    executeSoon(runNextStep);
  },
  function crossTabWakeLock5() {
    // Test again in background tab
    is(gWin2.document.hidden, true,
       "window is background")
    is(gWin2.navigator.mozPower.getWakeLockState("test"), "locked-background",
      "wake lock is background");
    gWin2.addEventListener("pagehide", function onPageHide(e) {
      gWin2.removeEventListener("pagehide", onPageHide, true);
      executeSoon(runNextStep);
    }, true);
    gWin2.addEventListener("pageshow", function onPageShow(e) {
      gWin2.removeEventListener("pageshow", onPageShow, true);
      executeSoon(runNextStep);
    }, true);
    gWin2.location = kDataSource;
  },
  function crossTabWakeLock6() {
    is(gWin1.navigator.mozPower.getWakeLockState("test"), "unlocked",
       "wake lock should auto-unlock when page is unloaded");
    gWin2.back();
    // runNextStep called in onPageShow
  },
  function crossTabWakeLock7() {
    is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-background",
       "wake lock should auto-reacquire when page is available again");
    gLock2.unlock();
    gBrowser.selectedTab = gTab2;
    executeSoon(runNextStep);
  },
  function crossTabWakeLock8() {
    is(gWin1.document.hidden, true,
       "gWin1 is background");
    is(gWin2.document.hidden, false,
       "gWin2 is foreground");

    gLock1 = gWin1.navigator.requestWakeLock("test");
    gLock2 = gWin2.navigator.requestWakeLock("test");

    is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
       "topic is locked-foreground when one page is foreground and one is background");

    gLock2.unlock();

    is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-background",
       "topic is locked-background when all locks are background");

    gLock2 = gWin2.navigator.requestWakeLock("test");

    is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
       "topic is locked-foreground when one page is foreground and one is background");

    gLock1.unlock();

    is(gWin1.navigator.mozPower.getWakeLockState("test"), "locked-foreground",
       "topic is locked-foreground");

    gBrowser.removeTab(gTab1);
    gBrowser.removeTab(gTab2);
    executeSoon(runNextStep);
  },
];

function runNextStep() {
  gCurStepIndex++;
  if (gCurStepIndex < gSteps.length) {
    gSteps[gCurStepIndex]();
  } else {
    finish();
  }
}

function test() {
  SpecialPowers.pushPrefEnv({"set": [["dom.wakelock.enabled", true]]},
                            runNextStep);
}