tor-browser

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

browser_aboutdebugging_serviceworker_pushservice_url.js (3433B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /* import-globals-from helper-serviceworker.js */
      7 Services.scriptloader.loadSubScript(
      8  CHROME_URL_ROOT + "helper-serviceworker.js",
      9  this
     10 );
     11 
     12 const SERVICE_WORKER =
     13  URL_ROOT_SSL + "resources/service-workers/push-sw.worker.js";
     14 const TAB_URL = URL_ROOT_SSL + "resources/service-workers/push-sw.html";
     15 
     16 const FAKE_ENDPOINT = "https://fake/endpoint";
     17 
     18 // Test that the push service url is displayed for service workers subscribed to a push
     19 // service.
     20 add_task(async function () {
     21  await enableServiceWorkerDebugging();
     22 
     23  info("Mock the push service");
     24  mockPushService(FAKE_ENDPOINT);
     25 
     26  const { document, tab, window } = await openAboutDebugging({
     27    enableWorkerUpdates: true,
     28  });
     29  await selectThisFirefoxPage(document, window.AboutDebugging.store);
     30 
     31  // Open a tab that registers a push service worker.
     32  const swTab = await addTab(TAB_URL);
     33 
     34  info(
     35    "Wait for the service worker to claim the test window before proceeding."
     36  );
     37  await SpecialPowers.spawn(
     38    swTab.linkedBrowser,
     39    [],
     40    () => content.wrappedJSObject.onSwClaimed
     41  );
     42 
     43  info("Wait until the service worker appears and is running");
     44  const targetElement = await waitForServiceWorkerRunning(
     45    SERVICE_WORKER,
     46    document
     47  );
     48 
     49  info("Subscribe from the push service");
     50  SpecialPowers.spawn(swTab.linkedBrowser, [], () => {
     51    content.wrappedJSObject.subscribeToPush();
     52  });
     53 
     54  info("Wait until the push service appears");
     55  await waitUntil(() =>
     56    targetElement.querySelector(".qa-worker-push-service-value")
     57  );
     58  const pushUrl = targetElement.querySelector(".qa-worker-push-service-value");
     59 
     60  ok(!!pushUrl, "Push URL is displayed for the serviceworker");
     61  is(pushUrl.textContent, FAKE_ENDPOINT, "Push URL shows the expected content");
     62 
     63  info("Unsubscribe from the push service");
     64  SpecialPowers.spawn(swTab.linkedBrowser, [], () => {
     65    content.wrappedJSObject.unsubscribeToPush();
     66  });
     67 
     68  info("Wait until the push service disappears");
     69  await waitUntil(
     70    () => !targetElement.querySelector(".qa-worker-push-service-value")
     71  );
     72 
     73  info("Unregister the service worker");
     74  await unregisterServiceWorker(swTab);
     75 
     76  info("Wait until the service worker disappears from about:debugging");
     77  await waitUntil(() => !findDebugTargetByText(SERVICE_WORKER, document));
     78 
     79  info("Remove the service worker tab");
     80  await removeTab(swTab);
     81 
     82  await removeTab(tab);
     83 });
     84 
     85 function mockPushService(endpoint) {
     86  const PushService = Cc["@mozilla.org/push/Service;1"].getService(
     87    Ci.nsIPushService
     88  ).wrappedJSObject;
     89 
     90  PushService.service = {
     91    _registrations: new Map(),
     92    _notify(scope) {
     93      Services.obs.notifyObservers(
     94        null,
     95        PushService.subscriptionModifiedTopic,
     96        scope
     97      );
     98    },
     99    init() {},
    100    register(pageRecord) {
    101      const registration = {
    102        endpoint,
    103      };
    104      this._registrations.set(pageRecord.scope, registration);
    105      this._notify(pageRecord.scope);
    106      return Promise.resolve(registration);
    107    },
    108    registration(pageRecord) {
    109      return Promise.resolve(this._registrations.get(pageRecord.scope));
    110    },
    111    unregister(pageRecord) {
    112      const deleted = this._registrations.delete(pageRecord.scope);
    113      if (deleted) {
    114        this._notify(pageRecord.scope);
    115      }
    116      return Promise.resolve(deleted);
    117    },
    118  };
    119 }