tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_userContextId_openWindow.js (4274B)


      1 let Cm = Components.manager;
      2 
      3 let swm = Cc["@mozilla.org/serviceworkers/manager;1"].getService(
      4  Ci.nsIServiceWorkerManager
      5 );
      6 
      7 const URI =
      8  "https://example.com/browser/dom/notification/test/browser/empty.html";
      9 const MOCK_CID = Components.ID("{2a0f83c4-8818-4914-a184-f1172b4eaaa7}");
     10 const SYSTEM_CID = Components.ID("{a0ccaaf8-09da-44d8-b250-9ac3e93c8117}");
     11 const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
     12 const USER_CONTEXT_ID = 3;
     13 
     14 let mockAlertsService = {
     15  showAlert(alert, alertListener) {
     16    ok(true, "Showing alert");
     17    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     18    setTimeout(function () {
     19      alertListener.observe(null, "alertshow", alert.cookie);
     20    }, 100);
     21    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     22    setTimeout(function () {
     23      alertListener.observe(null, "alertclickcallback", alert.cookie);
     24    }, 100);
     25  },
     26 
     27  QueryInterface: ChromeUtils.generateQI(["nsIAlertsService"]),
     28 
     29  createInstance(aIID) {
     30    return this.QueryInterface(aIID);
     31  },
     32 };
     33 
     34 registerCleanupFunction(() => {
     35  const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
     36  registrar.unregisterFactory(MOCK_CID, mockAlertsService);
     37  registrar.registerFactory(SYSTEM_CID, "", ALERTS_SERVICE_CONTRACT_ID, null);
     38 });
     39 
     40 add_setup(async function () {
     41  // make sure userContext, SW and notifications are enabled.
     42  await SpecialPowers.pushPrefEnv({
     43    set: [
     44      ["privacy.userContext.enabled", true],
     45      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
     46      ["dom.serviceWorkers.enabled", true],
     47      ["dom.serviceWorkers.testing.enabled", true],
     48      ["dom.webnotifications.disable_open_click_delay", 1000],
     49      ["dom.serviceWorkers.idle_timeout", 299999],
     50      ["dom.serviceWorkers.idle_extended_timeout", 299999],
     51      ["browser.link.open_newwindow", 3],
     52    ],
     53  });
     54 });
     55 
     56 add_task(async function test() {
     57  Cm.QueryInterface(Ci.nsIComponentRegistrar).registerFactory(
     58    MOCK_CID,
     59    "alerts service",
     60    ALERTS_SERVICE_CONTRACT_ID,
     61    mockAlertsService
     62  );
     63 
     64  // open the tab in the correct userContextId
     65  let tab = BrowserTestUtils.addTab(gBrowser, URI, {
     66    userContextId: USER_CONTEXT_ID,
     67  });
     68  let browser = gBrowser.getBrowserForTab(tab);
     69 
     70  // select tab and make sure its browser is focused
     71  gBrowser.selectedTab = tab;
     72  tab.ownerGlobal.focus();
     73 
     74  // wait for tab load
     75  await BrowserTestUtils.browserLoaded(gBrowser.getBrowserForTab(tab));
     76 
     77  // Waiting for new tab.
     78  let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, null, true);
     79 
     80  // here the test.
     81  /* eslint-disable no-shadow */
     82  let uci = await SpecialPowers.spawn(browser, [], async () => {
     83    let uci = content.document.nodePrincipal.userContextId;
     84 
     85    await SpecialPowers.pushPermissions([
     86      {
     87        type: "desktop-notification",
     88        allow: SpecialPowers.Services.perms.ALLOW_ACTION,
     89        context: content.document,
     90      },
     91    ]);
     92 
     93    // Registration of the SW
     94    const swr = await content.navigator.serviceWorker.register(
     95      "file_userContextId_openWindow.serviceworker.js"
     96    );
     97 
     98    // Activation
     99    await new content.window.Promise(resolve => {
    100      let worker = swr.installing;
    101      worker.addEventListener("statechange", () => {
    102        if (worker.state === "activated") {
    103          resolve(swr);
    104        }
    105      });
    106    });
    107 
    108    // Ask for an openWindow.
    109    await swr.showNotification("testPopup");
    110    return uci;
    111  });
    112  /* eslint-enable no-shadow */
    113 
    114  is(uci, USER_CONTEXT_ID, "Tab runs with UCI " + USER_CONTEXT_ID);
    115 
    116  let newTab = await newTabPromise;
    117 
    118  is(
    119    newTab.getAttribute("usercontextid"),
    120    USER_CONTEXT_ID.toString(),
    121    "New tab has UCI equal " + USER_CONTEXT_ID
    122  );
    123 
    124  // wait for SW unregistration
    125  /* eslint-disable no-shadow */
    126  uci = await SpecialPowers.spawn(browser, [], () => {
    127    let uci = content.document.nodePrincipal.userContextId;
    128 
    129    return content.navigator.serviceWorker
    130      .getRegistration(".")
    131      .then(registration => {
    132        return registration.unregister();
    133      })
    134      .then(() => {
    135        return uci;
    136      });
    137  });
    138  /* eslint-enable no-shadow */
    139 
    140  is(uci, USER_CONTEXT_ID, "Tab runs with UCI " + USER_CONTEXT_ID);
    141 
    142  BrowserTestUtils.removeTab(newTab);
    143  BrowserTestUtils.removeTab(tab);
    144 });