summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/browser_unregister_with_containers.js
blob: 0304f70c1db72ddc937e8ad70a2f0a83a4f94c6d (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
"use strict";

const BASE_URI = "http://mochi.test:8888/browser/dom/serviceworkers/test/";
const PAGE_URI = BASE_URI + "empty.html";
const SCOPE = PAGE_URI + "?unregister_with_containers";
const SW_SCRIPT = BASE_URI + "empty.js";

function doRegister(browser) {
  return SpecialPowers.spawn(
    browser,
    [{ script: SW_SCRIPT, scope: SCOPE }],
    async function(opts) {
      let reg = await content.navigator.serviceWorker.register(opts.script, {
        scope: opts.scope,
      });
      let worker = reg.installing || reg.waiting || reg.active;
      await new Promise(resolve => {
        if (worker.state === "activated") {
          resolve();
          return;
        }
        worker.addEventListener("statechange", function onStateChange() {
          if (worker.state === "activated") {
            worker.removeEventListener("statechange", onStateChange);
            resolve();
          }
        });
      });
    }
  );
}

function doUnregister(browser) {
  return SpecialPowers.spawn(browser, [SCOPE], async function(uri) {
    let reg = await content.navigator.serviceWorker.getRegistration(uri);
    let worker = reg.active;
    await reg.unregister();
    await new Promise(resolve => {
      if (worker.state === "redundant") {
        resolve();
        return;
      }
      worker.addEventListener("statechange", function onStateChange() {
        if (worker.state === "redundant") {
          worker.removeEventListener("statechange", onStateChange);
          resolve();
        }
      });
    });
  });
}

function isControlled(browser) {
  return SpecialPowers.spawn(browser, [], function() {
    return !!content.navigator.serviceWorker.controller;
  });
}

async function checkControlled(browser) {
  let controlled = await isControlled(browser);
  ok(controlled, "window should be controlled");
}

async function checkUncontrolled(browser) {
  let controlled = await isControlled(browser);
  ok(!controlled, "window should not be controlled");
}

add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [
      // Avoid service worker propagation races by disabling multi-e10s for now.
      // This can be removed after the e10s refactor is complete.
      ["dom.ipc.processCount", 1],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
    ],
  });

  // Setup service workers in two different contexts with the same scope.
  let containerTab1 = BrowserTestUtils.addTab(gBrowser, PAGE_URI, {
    userContextId: 1,
  });
  let containerBrowser1 = gBrowser.getBrowserForTab(containerTab1);
  await BrowserTestUtils.browserLoaded(containerBrowser1);

  let containerTab2 = BrowserTestUtils.addTab(gBrowser, PAGE_URI, {
    userContextId: 2,
  });
  let containerBrowser2 = gBrowser.getBrowserForTab(containerTab2);
  await BrowserTestUtils.browserLoaded(containerBrowser2);

  await doRegister(containerBrowser1);
  await doRegister(containerBrowser2);

  await checkUncontrolled(containerBrowser1);
  await checkUncontrolled(containerBrowser2);

  // Close the tabs we used to register the service workers.  These are not
  // controlled.
  BrowserTestUtils.removeTab(containerTab1);
  BrowserTestUtils.removeTab(containerTab2);

  // Open a controlled tab in each container.
  containerTab1 = BrowserTestUtils.addTab(gBrowser, SCOPE, {
    userContextId: 1,
  });
  containerBrowser1 = gBrowser.getBrowserForTab(containerTab1);
  await BrowserTestUtils.browserLoaded(containerBrowser1);

  containerTab2 = BrowserTestUtils.addTab(gBrowser, SCOPE, {
    userContextId: 2,
  });
  containerBrowser2 = gBrowser.getBrowserForTab(containerTab2);
  await BrowserTestUtils.browserLoaded(containerBrowser2);

  await checkControlled(containerBrowser1);
  await checkControlled(containerBrowser2);

  // Remove the first container's controlled tab
  BrowserTestUtils.removeTab(containerTab1);

  // Create a new uncontrolled tab for the first container and use it to
  // unregister the service worker.
  containerTab1 = BrowserTestUtils.addTab(gBrowser, PAGE_URI, {
    userContextId: 1,
  });
  containerBrowser1 = gBrowser.getBrowserForTab(containerTab1);
  await BrowserTestUtils.browserLoaded(containerBrowser1);
  await doUnregister(containerBrowser1);

  await checkUncontrolled(containerBrowser1);
  await checkControlled(containerBrowser2);

  // Remove the second container's controlled tab
  BrowserTestUtils.removeTab(containerTab2);

  // Create a new uncontrolled tab for the second container and use it to
  // unregister the service worker.
  containerTab2 = BrowserTestUtils.addTab(gBrowser, PAGE_URI, {
    userContextId: 2,
  });
  containerBrowser2 = gBrowser.getBrowserForTab(containerTab2);
  await BrowserTestUtils.browserLoaded(containerBrowser2);
  await doUnregister(containerBrowser2);

  await checkUncontrolled(containerBrowser1);
  await checkUncontrolled(containerBrowser2);

  // Close the two tabs we used to unregister the service worker.
  BrowserTestUtils.removeTab(containerTab1);
  BrowserTestUtils.removeTab(containerTab2);
});