tor-browser

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

browser_net_offline_mode.js (2872B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test network throttling `offline` mode
      5 
      6 "use strict";
      7 
      8 requestLongerTimeout(2);
      9 
     10 const {
     11  PROFILE_CONSTANTS,
     12 } = require("resource://devtools/client/shared/components/throttling/profiles.js");
     13 
     14 add_task(async function () {
     15  await pushPref("devtools.cache.disabled", true);
     16 
     17  const { tab, monitor, toolbox } = await initNetMonitor(HTTPS_CUSTOM_GET_URL, {
     18    requestCount: 1,
     19  });
     20  await selectThrottle(monitor, PROFILE_CONSTANTS.OFFLINE);
     21  assertCurrentThrottleSelected(monitor, PROFILE_CONSTANTS.OFFLINE);
     22 
     23  const offlineEventFired = SpecialPowers.spawn(
     24    tab.linkedBrowser,
     25    [],
     26    async function () {
     27      return content.wrappedJSObject.hasOfflineEventFired();
     28    }
     29  );
     30 
     31  ok(offlineEventFired, "The offline event on the page fired");
     32 
     33  // As the browser is offline, load event won't fire
     34  await reloadBrowser({ waitForLoad: false });
     35 
     36  await assertNavigatorOnlineInConsole(toolbox, "false");
     37  await assertPageIsOffline();
     38 
     39  await selectThrottle(monitor, PROFILE_CONSTANTS.REGULAR_4G_LTE);
     40  assertCurrentThrottleSelected(monitor, PROFILE_CONSTANTS.REGULAR_4G_LTE);
     41 
     42  await reloadBrowser();
     43 
     44  await assertNavigatorOnlineInConsole(toolbox, "true");
     45  await assertPageIsOnline();
     46 
     47  await teardown(monitor);
     48 });
     49 
     50 function assertCurrentThrottleSelected(monitor, expectedProfile) {
     51  const doc = monitor.panelWin.document;
     52  is(
     53    doc.querySelector("#network-throttling").innerText,
     54    expectedProfile,
     55    `The '${expectedProfile}' throttle profile is correctly selected`
     56  );
     57 }
     58 
     59 function assertPageIsOffline() {
     60  // This is an error page.
     61  return SpecialPowers.spawn(
     62    gBrowser.selectedTab.linkedBrowser,
     63    [HTTPS_CUSTOM_GET_URL],
     64    function (uri) {
     65      is(
     66        content.document.documentURI.substring(0, 27),
     67        "about:neterror?e=netOffline",
     68        "Document URI is the error page."
     69      );
     70 
     71      // But location bar should show the original request.
     72      is(content.location.href, uri, "Docshell URI is the original URI.");
     73    }
     74  );
     75 }
     76 
     77 function assertPageIsOnline() {
     78  // This is an error page.
     79  return SpecialPowers.spawn(
     80    gBrowser.selectedTab.linkedBrowser,
     81    [HTTPS_CUSTOM_GET_URL],
     82    function (uri) {
     83      is(content.document.documentURI, uri, "Document URI is the original URI");
     84 
     85      // But location bar should show the original request.
     86      is(content.location.href, uri, "Docshell URI is the original URI.");
     87    }
     88  );
     89 }
     90 
     91 async function assertNavigatorOnlineInConsole(toolbox, expectedResultValue) {
     92  const input = "navigator.onLine";
     93  info(`Assert the value of '${input}' in the console`);
     94  await toolbox.openSplitConsole();
     95  const { hud } = await toolbox.getPanel("webconsole");
     96  hud.setInputValue(input);
     97  return waitForEagerEvaluationResult(hud, expectedResultValue);
     98 }