tor-browser

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

browser_aboutdebugging_serviceworker_timeout.js (3842B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // This test will be idle for a long period to give a chance to the service worker to
      7 // timeout.
      8 requestLongerTimeout(3);
      9 
     10 /* import-globals-from helper-serviceworker.js */
     11 Services.scriptloader.loadSubScript(
     12  CHROME_URL_ROOT + "helper-serviceworker.js",
     13  this
     14 );
     15 
     16 const SW_TAB_URL = URL_ROOT_SSL + "resources/service-workers/empty-sw.html";
     17 const SW_URL = URL_ROOT_SSL + "resources/service-workers/empty-sw.worker.js";
     18 const SW_TIMEOUT = 4000;
     19 
     20 /**
     21 * Test that service workers will _not_ timeout and be stopped when a toolbox is attached
     22 * to them. Feature implemented in Bug 1228382.
     23 */
     24 add_task(async function () {
     25  await enableServiceWorkerDebugging();
     26 
     27  // Setting a low idle_timeout and idle_extended_timeout will allow the service worker
     28  // to reach the STOPPED state quickly, which will allow us to test the start button.
     29  // The default value is 30000 milliseconds.
     30  info("Set a low service worker idle timeout");
     31  await pushPref("dom.serviceWorkers.idle_timeout", SW_TIMEOUT);
     32  await pushPref("dom.serviceWorkers.idle_extended_timeout", SW_TIMEOUT);
     33 
     34  const { document, tab, window } = await openAboutDebugging({
     35    enableWorkerUpdates: true,
     36  });
     37  await selectThisFirefoxPage(document, window.AboutDebugging.store);
     38 
     39  // Open a tab that registers a basic service worker.
     40  const swTab = await addTab(SW_TAB_URL);
     41 
     42  // Wait for the registration to make sure service worker has been started, and that we
     43  // are not just reading STOPPED as the initial state.
     44  await waitForRegistration(swTab);
     45 
     46  info("Wait until the service worker stops");
     47  await waitForServiceWorkerStopped(SW_URL, document);
     48 
     49  info(
     50    "Click on the start button and wait for the service worker to be running"
     51  );
     52  const onServiceWorkerRunning = waitForServiceWorkerRunning(SW_URL, document);
     53  const startButton = getStartButton(SW_URL, document);
     54  startButton.click();
     55  await onServiceWorkerRunning;
     56 
     57  const inspectButton = getInspectButton(SW_URL, document);
     58  ok(!!inspectButton, "Service worker target has an inspect button");
     59 
     60  info("Click on inspect and wait for the toolbox to open");
     61  const onToolboxReady = gDevTools.once("toolbox-ready");
     62  inspectButton.click();
     63  await onToolboxReady;
     64 
     65  // Wait for more 5 times the service worker timeout to check that the toolbox prevents
     66  // the worker from being destroyed.
     67  await wait(SW_TIMEOUT * 5);
     68 
     69  // Check that the service worker is still running, even after waiting 5 times the
     70  // service worker timeout.
     71  const hasInspectButton = !!getInspectButton(SW_URL, document);
     72  ok(hasInspectButton, "Service worker target still has an inspect button");
     73 
     74  info("Destroy the toolbox");
     75  const devtoolsTab = gBrowser.selectedTab;
     76  await closeAboutDevtoolsToolbox(document, devtoolsTab, window);
     77 
     78  // After stopping the toolbox, the service worker instance should be released and the
     79  // service worker registration should be displayed as stopped again.
     80  info("Wait until the service worker stops after closing the toolbox");
     81  await waitForServiceWorkerStopped(SW_URL, document);
     82 
     83  info("Unregister service worker");
     84  await unregisterServiceWorker(swTab);
     85 
     86  info("Wait until the service worker disappears from about:debugging");
     87  await waitUntil(() => !findDebugTargetByText(SW_URL, document));
     88 
     89  info("Remove tabs");
     90  await removeTab(swTab);
     91  await removeTab(tab);
     92 });
     93 
     94 function getStartButton(workerText, doc) {
     95  const target = findDebugTargetByText(workerText, doc);
     96  return target ? target.querySelector(".qa-start-button") : null;
     97 }
     98 
     99 function getInspectButton(workerText, doc) {
    100  const target = findDebugTargetByText(workerText, doc);
    101  return target
    102    ? target.querySelector(".qa-debug-target-inspect-button")
    103    : null;
    104 }