summaryrefslogtreecommitdiffstats
path: root/dom/push/test/test_try_registering_offline_disabled.html
blob: 82ef2b8755018c815a2758c2487f2ccc6fddf14b (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!DOCTYPE HTML>
<html>
<!--
Bug 1150812: Try to register when serviced if offline or connection is disabled.

Any copyright is dedicated to the Public Domain.
http://creativecommons.org/licenses/publicdomain/

-->
<head>
  <title>Test for Bug 1150812</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/dom/push/test/test_utils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1150812">Mozilla Bug 1150812</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>

<script class="testbody" type="text/javascript">
  /* eslint-env mozilla/frame-script */

  function debug(str) {
  //  console.log(str + "\n");
  }

  function registerServiceWorker() {
    return navigator.serviceWorker.register("worker.js?" + (Math.random()), {scope: "."})
      .then(swr => waitForActive(swr));
  }

  function unregister(swr) {
    return swr.unregister()
      .then(result => {
        ok(result, "Unregister should return true.");
      }, err => {
        dump("Unregistering the SW failed with " + err + "\n");
        throw err;
      });
  }

  function subscribe(swr) {
    return swr.pushManager.subscribe()
      .then(sub => {
        ok(true, "successful registered for push notification");
        return sub;
      }, err => {
        ok(false, "could not register for push notification");
        throw err;
      });
  }

  function subscribeFail(swr) {
    return new Promise((res, rej) => {
      swr.pushManager.subscribe()
        .then(sub => {
          ok(false, "successful registered for push notification");
          throw new Error("Should fail");
        }, err => {
          ok(true, "could not register for push notification");
          res(swr);
        });
    });
  }

  function getEndpointExpectNull(swr) {
    return swr.pushManager.getSubscription()
      .then(pushSubscription => {
        ok(pushSubscription == null, "getEndpoint should return null when app not subscribed.");
      }, err => {
        ok(false, "could not register for push notification");
        throw err;
      });
  }

  function getEndpoint(swr, subOld) {
    return swr.pushManager.getSubscription()
      .then(sub => {
        ok(subOld.endpoint == sub.endpoint, "getEndpoint - Got the same endpoint back.");
        return sub;
      }, err => {
          ok(false, "could not register for push notification");
          throw err;
      });
  }

  // Load chrome script to change offline status in the
  // parent process.
  var offlineChromeScript = SpecialPowers.loadChromeScript(_ => {
    addMessageListener("change-status", function(offline) {
      // eslint-disable-next-line mozilla/use-services
      const ioService = Cc["@mozilla.org/network/io-service;1"]
        .getService(Ci.nsIIOService);
      ioService.offline = offline;
    });
  });

  function offlineObserver(res) {
    this._res = res;
  }
  offlineObserver.prototype = {
    _res: null,

    observe(subject, topic, data) {
    debug("observe: " + subject + " " + topic + " " + data);
    if (topic === "network:offline-status-changed") {
      // eslint-disable-next-line mozilla/use-services
      const obsService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
        .getService(SpecialPowers.Ci.nsIObserverService);
      obsService.removeObserver(this, topic);
      this._res(null);
    }
  },
  };

  function changeOfflineState(offline) {
    return new Promise(function(res, rej) {
      // eslint-disable-next-line mozilla/use-services
      const obsService = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
        .getService(SpecialPowers.Ci.nsIObserverService);
      obsService.addObserver(SpecialPowers.wrapCallbackObject(new offlineObserver(res)),
                               "network:offline-status-changed");
      offlineChromeScript.sendAsyncMessage("change-status", offline);
    });
  }

  function changePushServerConnectionEnabled(enable) {
    debug("changePushServerConnectionEnabled");
    SpecialPowers.setBoolPref("dom.push.connection.enabled", enable);
  }

  function unsubscribe(sub) {
    return sub.unsubscribe()
      .then(_ => { ok(true, "Unsubscribed!"); });
  }

  // go offline then go online
  function runTest1() {
    return registerServiceWorker()
    .then(swr =>
      getEndpointExpectNull(swr)
        .then(_ => changeOfflineState(true))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changeOfflineState(false))
        .then(_ => subscribe(swr))
        .then(sub => getEndpoint(swr, sub)
          .then(s => unsubscribe(s))
        )
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => unregister(swr))
    )
    .catch(err => {
      ok(false, "Some test failed with error " + err);
    });
  }

  // disable - enable push connection.
  function runTest2() {
    return registerServiceWorker()
    .then(swr =>
      getEndpointExpectNull(swr)
        .then(_ => changePushServerConnectionEnabled(false))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changePushServerConnectionEnabled(true))
        .then(_ => subscribe(swr))
        .then(sub => getEndpoint(swr, sub)
          .then(s => unsubscribe(s))
        )
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => unregister(swr))
    )
    .catch(err => {
      ok(false, "Some test failed with error " + err);
    });
  }

  // go offline - disable - enable - go online
  function runTest3() {
    return registerServiceWorker()
    .then(swr =>
      getEndpointExpectNull(swr)
        .then(_ => changeOfflineState(true))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changePushServerConnectionEnabled(false))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changePushServerConnectionEnabled(true))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changeOfflineState(false))
        .then(_ => subscribe(swr))
        .then(sub => getEndpoint(swr, sub)
          .then(s => unsubscribe(s))
        )
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => unregister(swr))
    )
    .catch(err => {
      ok(false, "Some test failed with error " + err);
    });
  }

  // disable - offline - online - enable.
  function runTest4() {
    return registerServiceWorker()
    .then(swr =>
      getEndpointExpectNull(swr)
        .then(_ => changePushServerConnectionEnabled(false))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changeOfflineState(true))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changeOfflineState(false))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changePushServerConnectionEnabled(true))
        .then(_ => subscribe(swr))
        .then(sub => getEndpoint(swr, sub)
          .then(s => unsubscribe(s))
        )
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => unregister(swr))
    )
    .catch(err => {
      ok(false, "Some test failed with error " + err);
    });
  }

  // go offline - disable - go online - enable
  function runTest5() {
    return registerServiceWorker()
    .then(swr =>
      getEndpointExpectNull(swr)
        .then(_ => changeOfflineState(true))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changePushServerConnectionEnabled(false))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changeOfflineState(false))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changePushServerConnectionEnabled(true))
        .then(_ => subscribe(swr))
        .then(sub => getEndpoint(swr, sub)
          .then(s => unsubscribe(s))
        )
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => unregister(swr))
    )
    .catch(err => {
      ok(false, "Some test failed with error " + err);
    });
  }

  // disable - go offline - enable - go online.
  function runTest6() {
    return registerServiceWorker()
    .then(swr =>
      getEndpointExpectNull(swr)
        .then(_ => changePushServerConnectionEnabled(false))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changeOfflineState(true))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changePushServerConnectionEnabled(true))
        .then(_ => subscribeFail(swr))
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => changeOfflineState(false))
        .then(_ => subscribe(swr))
        .then(sub => getEndpoint(swr, sub)
          .then(s => unsubscribe(s))
        )
        .then(_ => getEndpointExpectNull(swr))
        .then(_ => unregister(swr))
    )
    .catch(err => {
      ok(false, "Some test failed with error " + err);
    });
  }

  function runTest() {
    runTest1()
    .then(_ => runTest2())
    .then(_ => runTest3())
    .then(_ => runTest4())
    .then(_ => runTest5())
    .then(_ => runTest6())
    .then(SimpleTest.finish);
  }

  setupPrefsAndMockSocket(new MockWebSocket()).then(_ => runTest());
  SpecialPowers.addPermission("desktop-notification", true, document);
  SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>