tor-browser

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

browser_aboutdebugging_serviceworker_status.js (3053B)


      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 SW_TAB_URL =
     13  URL_ROOT_SSL + "resources/service-workers/controlled-sw.html";
     14 const SW_URL =
     15  URL_ROOT_SSL + "resources/service-workers/controlled-sw.worker.js";
     16 
     17 /**
     18 * Test that the service worker has the status "registering" when the service worker is
     19 * not installed yet. Other states (stopped, running) are covered by the existing tests.
     20 */
     21 add_task(async function () {
     22  await enableServiceWorkerDebugging();
     23 
     24  const { document, tab, window } = await openAboutDebugging({
     25    enableWorkerUpdates: true,
     26  });
     27  await selectThisFirefoxPage(document, window.AboutDebugging.store);
     28 
     29  info("Open tab with a service worker that never leaves `registering` status");
     30  const swTab = await addTab(SW_TAB_URL);
     31 
     32  // Wait for the registration to make sure service worker has been started, and that we
     33  // are not just reading STOPPED as the initial state.
     34  await waitForRegistration(swTab);
     35 
     36  info("Wait until the service worker is in registering status");
     37  await waitForServiceWorkerRegistering(SW_URL, document);
     38 
     39  // Check that the buttons are displayed as expected.
     40  checkButtons(
     41    { inspect: true, push: false, start: false, unregister: false },
     42    SW_URL,
     43    document
     44  );
     45 
     46  info("Install the service worker");
     47  SpecialPowers.spawn(swTab.linkedBrowser, [], () =>
     48    content.wrappedJSObject.installServiceWorker()
     49  );
     50 
     51  info("Wait until the service worker is running");
     52  await waitForServiceWorkerRunning(SW_URL, document);
     53 
     54  checkButtons(
     55    { inspect: true, push: true, start: false, unregister: true },
     56    SW_URL,
     57    document
     58  );
     59 
     60  info("Unregister service worker");
     61  await unregisterServiceWorker(swTab);
     62 
     63  info("Wait until the service worker disappears from about:debugging");
     64  await waitUntil(() => !findDebugTargetByText(SW_URL, document));
     65 
     66  info("Remove tabs");
     67  await removeTab(swTab);
     68  await removeTab(tab);
     69 });
     70 
     71 function checkButtons(
     72  { inspect, push, start, unregister },
     73  workerText,
     74  document
     75 ) {
     76  const targetElement = findDebugTargetByText(SW_URL, document);
     77 
     78  const inspectButton = targetElement.querySelector(
     79    ".qa-debug-target-inspect-button"
     80  );
     81  const pushButton = targetElement.querySelector(".qa-push-button");
     82  const startButton = targetElement.querySelector(".qa-start-button");
     83  const unregisterButton = targetElement.querySelector(".qa-unregister-button");
     84 
     85  is(
     86    !!inspectButton,
     87    inspect,
     88    "Inspect button should be " + (inspect ? "visible" : "hidden")
     89  );
     90  is(
     91    !!pushButton,
     92    push,
     93    "Push button should be " + (push ? "visible" : "hidden")
     94  );
     95  is(
     96    !!startButton,
     97    start,
     98    "Start button should be " + (start ? "visible" : "hidden")
     99  );
    100  is(
    101    !!unregisterButton,
    102    unregister,
    103    "Unregister button should be " + (unregister ? "visible" : "hidden")
    104  );
    105 }