summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/browser_userContextId_openWindow.js
blob: 599745e372f247036f835359030886d8f6914eab (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
let Cm = Components.manager;

let swm = Cc["@mozilla.org/serviceworkers/manager;1"].getService(
  Ci.nsIServiceWorkerManager
);

const URI = "https://example.com/browser/dom/serviceworkers/test/empty.html";
const MOCK_CID = Components.ID("{2a0f83c4-8818-4914-a184-f1172b4eaaa7}");
const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
const USER_CONTEXT_ID = 3;

let mockAlertsService = {
  showAlert(alert, alertListener) {
    ok(true, "Showing alert");
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    setTimeout(function () {
      alertListener.observe(null, "alertshow", alert.cookie);
    }, 100);
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    setTimeout(function () {
      alertListener.observe(null, "alertclickcallback", alert.cookie);
    }, 100);
  },

  showAlertNotification(
    imageUrl,
    title,
    text,
    textClickable,
    cookie,
    alertListener,
    name,
    dir,
    lang,
    data
  ) {
    this.showAlert();
  },

  QueryInterface(aIID) {
    if (aIID.equals(Ci.nsISupports) || aIID.equals(Ci.nsIAlertsService)) {
      return this;
    }
    throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
  },

  createInstance(aIID) {
    return this.QueryInterface(aIID);
  },
};

registerCleanupFunction(() => {
  Cm.QueryInterface(Ci.nsIComponentRegistrar).unregisterFactory(
    MOCK_CID,
    mockAlertsService
  );
});

add_setup(async function () {
  // make sure userContext, SW and notifications are enabled.
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.userContext.enabled", true],
      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
      ["dom.webnotifications.serviceworker.enabled", true],
      ["notification.prompt.testing", true],
      ["dom.serviceWorkers.disable_open_click_delay", 1000],
      ["dom.serviceWorkers.idle_timeout", 299999],
      ["dom.serviceWorkers.idle_extended_timeout", 299999],
      ["browser.link.open_newwindow", 3],
    ],
  });
});

add_task(async function test() {
  Cm.QueryInterface(Ci.nsIComponentRegistrar).registerFactory(
    MOCK_CID,
    "alerts service",
    ALERTS_SERVICE_CONTRACT_ID,
    mockAlertsService
  );

  // open the tab in the correct userContextId
  let tab = BrowserTestUtils.addTab(gBrowser, URI, {
    userContextId: USER_CONTEXT_ID,
  });
  let browser = gBrowser.getBrowserForTab(tab);

  // select tab and make sure its browser is focused
  gBrowser.selectedTab = tab;
  tab.ownerGlobal.focus();

  // wait for tab load
  await BrowserTestUtils.browserLoaded(gBrowser.getBrowserForTab(tab));

  // Waiting for new tab.
  let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, null, true);

  // here the test.
  /* eslint-disable no-shadow */
  let uci = await SpecialPowers.spawn(browser, [URI], uri => {
    let uci = content.document.nodePrincipal.userContextId;

    // Registration of the SW
    return (
      content.navigator.serviceWorker
        .register("file_userContextId_openWindow.js")

        // Activation
        .then(swr => {
          return new content.window.Promise(resolve => {
            let worker = swr.installing;
            worker.addEventListener("statechange", () => {
              if (worker.state === "activated") {
                resolve(swr);
              }
            });
          });
        })

        // Ask for an openWindow.
        .then(swr => {
          swr.showNotification("testPopup");
          return uci;
        })
    );
  });
  /* eslint-enable no-shadow */

  is(uci, USER_CONTEXT_ID, "Tab runs with UCI " + USER_CONTEXT_ID);

  let newTab = await newTabPromise;

  is(
    newTab.getAttribute("usercontextid"),
    USER_CONTEXT_ID,
    "New tab has UCI equal " + USER_CONTEXT_ID
  );

  // wait for SW unregistration
  /* eslint-disable no-shadow */
  uci = await SpecialPowers.spawn(browser, [], () => {
    let uci = content.document.nodePrincipal.userContextId;

    return content.navigator.serviceWorker
      .getRegistration(".")
      .then(registration => {
        return registration.unregister();
      })
      .then(() => {
        return uci;
      });
  });
  /* eslint-enable no-shadow */

  is(uci, USER_CONTEXT_ID, "Tab runs with UCI " + USER_CONTEXT_ID);

  BrowserTestUtils.removeTab(newTab);
  BrowserTestUtils.removeTab(tab);
});