summaryrefslogtreecommitdiffstats
path: root/dom/push/test/test_serviceworker_lifetime.html
blob: 03f66887ad06dd90ed729b1484f1ec29597810ef (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<!DOCTYPE HTML>
<html>
<!--
  Test the lifetime management of service workers. We keep this test in
  dom/push/tests to pass the external network check when connecting to
  the mozilla push service.

  How this test works:
  - the service worker maintains a state variable and a promise used for
    extending its lifetime. Note that the terminating the worker will reset
    these variables to their default values.
  - we send 3 types of requests to the service worker:
    |update|, |wait| and |release|. All three requests will cause the sw to update
    its state to the new value and reply with a message containing
    its previous state. Furthermore, |wait| will set a waitUntil or a respondWith
    promise that's not resolved until the next |release| message.
  - Each subtest will use a combination of values for the timeouts and check
    if the service worker is in the correct state as we send it different
    events.
  - We also wait and assert for service worker termination using an event dispatched
    through nsIObserverService.
  -->
<head>
  <title>Test for Bug 1188545</title>
  <script type="text/javascript" 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=1188545">Mozilla Bug 118845</a>
<p id="display"></p>
<div id="content" style="display: none">

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

<script class="testbody" type="text/javascript">

  function start() {
    return navigator.serviceWorker.register("lifetime_worker.js", {scope: "./"})
    .then((swr) => ({registration: swr}));
  }

  function waitForActiveServiceWorker(ctx) {
    return waitForActive(ctx.registration).then(function(result) {
      ok(ctx.registration.active, "Service Worker is active");
      return ctx;
    });
  }

  function unregister(ctx) {
    return ctx.registration.unregister().then(function(result) {
      ok(result, "Unregister should return true.");
    }, function(e) {
      dump("Unregistering the SW failed with " + e + "\n");
    });
  }

  function registerPushNotification(ctx) {
    var p = new Promise(function(res, rej) {
      ctx.registration.pushManager.subscribe().then(
        function(pushSubscription) {
          ok(true, "successful registered for push notification");
          ctx.subscription = pushSubscription;
          res(ctx);
        }, function(error) {
          ok(false, "could not register for push notification");
          res(ctx);
        });
    });
    return p;
  }

  var mockSocket = new MockWebSocket();
  var endpoint = "https://example.com/endpoint/1";
  var channelID = null;
  mockSocket.onRegister = function(request) {
    channelID = request.channelID;
    this.serverSendMsg(JSON.stringify({
      messageType: "register",
      uaid: "fa8f2e4b-5ddc-4408-b1e3-5f25a02abff0",
      channelID,
      status: 200,
      pushEndpoint: endpoint
    }));
  };

  function sendPushToPushServer(pushEndpoint) {
    is(pushEndpoint, endpoint, "Got unexpected endpoint");
    mockSocket.serverSendMsg(JSON.stringify({
      messageType: "notification",
      version: "vDummy",
      channelID
    }));
  }

  function unregisterPushNotification(ctx) {
    return ctx.subscription.unsubscribe().then(function(result) {
      ok(result, "unsubscribe should succeed.");
      ctx.subscription = null;
      return ctx;
    });
  }

  function createIframe(ctx) {
    var p = new Promise(function(res, rej) {
      var iframe = document.createElement('iframe');
      // This file doesn't exist, the service worker will give us an empty
      // document.
      iframe.src = "http://mochi.test:8888/tests/dom/push/test/lifetime_frame.html";

      iframe.onload = function() {
        ctx.iframe = iframe;
        res(ctx);
      }
      document.body.appendChild(iframe);
    });
    return p;
  }

  function closeIframe(ctx) {
    ctx.iframe.parentNode.removeChild(ctx.iframe);
    return new Promise(function(res, rej) {
      // XXXcatalinb: give the worker more time to "notice" it stopped
      // controlling documents
      ctx.iframe = null;
      setTimeout(res, 0);
    }).then(() => ctx);
  }

  function waitAndCheckMessage(contentWindow, expected) {
    function checkMessage(expected, resolve, event) {
      ok(event.data.type == expected.type, "Received correct message type: " + expected.type);
      ok(event.data.state == expected.state, "Service worker is in the correct state: " + expected.state);
      this.navigator.serviceWorker.onmessage = null;
      resolve();
    }
    return new Promise(function(res, rej) {
      contentWindow.navigator.serviceWorker.onmessage =
        checkMessage.bind(contentWindow, expected, res);
    });
  }

  function fetchEvent(ctx, expected_state, new_state) {
    var expected = { type: "fetch", state: expected_state };
    var p = waitAndCheckMessage(ctx.iframe.contentWindow, expected);
    ctx.iframe.contentWindow.fetch(new_state);
    return p;
  }

  function pushEvent(ctx, expected_state, new_state) {
    var expected = {type: "push", state: expected_state};
    var p = waitAndCheckMessage(ctx.iframe.contentWindow, expected);
    sendPushToPushServer(ctx.subscription.endpoint);
    return p;
  }

  function messageEventIframe(ctx, expected_state, new_state) {
    var expected = {type: "message", state: expected_state};
    var p = waitAndCheckMessage(ctx.iframe.contentWindow, expected);
    ctx.iframe.contentWindow.navigator.serviceWorker.controller.postMessage(new_state);
    return p;
  }

  function messageEvent(ctx, expected_state, new_state) {
    var expected = {type: "message", state: expected_state};
    var p = waitAndCheckMessage(window, expected);
    ctx.registration.active.postMessage(new_state);
    return p;
  }

  function checkStateAndUpdate(eventFunction, expected_state, new_state) {
    return function(ctx) {
      return eventFunction(ctx, expected_state, new_state)
        .then(() => ctx);
    }
  }

  function setShutdownObserver(expectingEvent) {
    info("Setting shutdown observer: expectingEvent=" + expectingEvent);
    return function(ctx) {
      cancelShutdownObserver(ctx);

      ctx.observer_promise = new Promise(function(res, rej) {
        ctx.observer = {
          observe: function(subject, topic, data) {
            ok((topic == "service-worker-shutdown") && expectingEvent, "Service worker was terminated.");
            this.remove(ctx);
          },
          remove: function(ctx) {
            SpecialPowers.removeObserver(this, "service-worker-shutdown");
            ctx.observer = null;
            res(ctx);
          }
        }
        SpecialPowers.addObserver(ctx.observer, "service-worker-shutdown", false);
      });

      return ctx;
    }
  }

  function waitOnShutdownObserver(ctx) {
    info("Waiting on worker to shutdown.");
    return ctx.observer_promise;
  }

  function cancelShutdownObserver(ctx) {
    if (ctx.observer) {
      ctx.observer.remove(ctx);
    }
    return ctx.observer_promise;
  }

  function subTest(test) {
    return function(ctx) {
      return new Promise(function(res, rej) {
        function run() {
          test.steps(ctx).catch(function(e) {
            ok(false, "Some test failed with error: " + e);
          }).then((ctx) => res(ctx));
        }

        SpecialPowers.pushPrefEnv({"set" : test.prefs}, run);
      });
    }
  }

  var test1 = {
    prefs: [
      ["dom.serviceWorkers.idle_timeout", 0],
      ["dom.serviceWorkers.idle_extended_timeout", 2999999]
    ],
    // Test that service workers are terminated after the grace period expires
    // when there are no pending waitUntil or respondWith promises.
    steps: function(ctx) {
      // Test with fetch events and respondWith promises
      return createIframe(ctx)
        .then(setShutdownObserver(true))
        .then(checkStateAndUpdate(fetchEvent, "from_scope", "update"))
        .then(waitOnShutdownObserver)
        .then(setShutdownObserver(false))
        .then(checkStateAndUpdate(fetchEvent, "from_scope", "wait"))
        .then(checkStateAndUpdate(fetchEvent, "wait", "update"))
        .then(checkStateAndUpdate(fetchEvent, "update", "update"))
        .then(setShutdownObserver(true))
        // The service worker should be terminated when the promise is resolved.
        .then(checkStateAndUpdate(fetchEvent, "update", "release"))
        .then(waitOnShutdownObserver)
        .then(setShutdownObserver(false))
        .then(closeIframe)
        .then(cancelShutdownObserver)

        // Test with push events and message events
        .then(setShutdownObserver(true))
        .then(createIframe)
        // Make sure we are shutdown before entering our "no shutdown" sequence
        // to avoid races.
        .then(waitOnShutdownObserver)
        .then(setShutdownObserver(false))
        .then(checkStateAndUpdate(pushEvent, "from_scope", "wait"))
        .then(checkStateAndUpdate(messageEventIframe, "wait", "update"))
        .then(checkStateAndUpdate(messageEventIframe, "update", "update"))
        .then(setShutdownObserver(true))
        .then(checkStateAndUpdate(messageEventIframe, "update", "release"))
        .then(waitOnShutdownObserver)
        .then(closeIframe)
    }
  }

  var test2 = {
    prefs: [
      ["dom.serviceWorkers.idle_timeout", 0],
      ["dom.serviceWorkers.idle_extended_timeout", 2999999]
    ],
    steps: function(ctx) {
      // Older versions used to terminate workers when the last controlled
      // window was closed.  This should no longer happen, though.  Verify
      // the new behavior.
      setShutdownObserver(true)(ctx);
      return createIframe(ctx)
        // Make sure we are shutdown before entering our "no shutdown" sequence
        // to avoid races.
        .then(waitOnShutdownObserver)
        .then(setShutdownObserver(false))
        .then(checkStateAndUpdate(fetchEvent, "from_scope", "wait"))
        .then(closeIframe)
        .then(setShutdownObserver(true))
        .then(checkStateAndUpdate(messageEvent, "wait", "release"))
        .then(waitOnShutdownObserver)

      // Push workers were exempt from the old rule and should continue to
      // survive past the closing of the last controlled window.
        .then(setShutdownObserver(true))
        .then(createIframe)
        // Make sure we are shutdown before entering our "no shutdown" sequence
        // to avoid races.
        .then(waitOnShutdownObserver)
        .then(setShutdownObserver(false))
        .then(checkStateAndUpdate(pushEvent, "from_scope", "wait"))
        .then(closeIframe)
        .then(setShutdownObserver(true))
        .then(checkStateAndUpdate(messageEvent, "wait", "release"))
        .then(waitOnShutdownObserver)
    }
  };

  var test3 = {
    prefs: [
      ["dom.serviceWorkers.idle_timeout", 2999999],
      ["dom.serviceWorkers.idle_extended_timeout", 0]
    ],
    steps: function(ctx) {
      // set the grace period to 0 and dispatch a message which will reset
      // the internal sw timer to the new value.
      var test3_1 = {
        prefs: [
          ["dom.serviceWorkers.idle_timeout", 0],
          ["dom.serviceWorkers.idle_extended_timeout", 0]
        ],
        steps: function(ctx) {
          return new Promise(function(res, rej) {
            ctx.iframe.contentWindow.fetch("update");
            res(ctx);
          });
        }
      }

      // Test that service worker is closed when the extended timeout expired
      return createIframe(ctx)
        .then(setShutdownObserver(false))
        .then(checkStateAndUpdate(messageEvent, "from_scope", "update"))
        .then(checkStateAndUpdate(messageEventIframe, "update", "update"))
        .then(checkStateAndUpdate(fetchEvent, "update", "wait"))
        .then(setShutdownObserver(true))
        .then(subTest(test3_1)) // This should cause the internal timer to expire.
        .then(waitOnShutdownObserver)
        .then(closeIframe)
    }
  }

  function runTest() {
    start()
      .then(waitForActiveServiceWorker)
      .then(registerPushNotification)
      .then(subTest(test1))
      .then(subTest(test2))
      .then(subTest(test3))
      .then(unregisterPushNotification)
      .then(unregister)
      .catch(function(e) {
        ok(false, "Some test failed with error " + e)
      }).then(SimpleTest.finish);
  }

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