tor-browser

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

browser_toolbox_window_reload_target.js (4309B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that pressing various page reload keyboard shortcuts always works when devtools
      5 // has focus, no matter if it's undocked or docked, and whatever the tool selected (this
      6 // is to avoid tools from overriding the page reload shortcuts).
      7 // This test also serves as a safety net checking that tools just don't explode when the
      8 // page is reloaded.
      9 // It is therefore quite long to run.
     10 
     11 requestLongerTimeout(10);
     12 const { PromiseTestUtils } = ChromeUtils.importESModule(
     13  "resource://testing-common/PromiseTestUtils.sys.mjs"
     14 );
     15 
     16 // allow a context error because it is harmless. This could likely be removed in the next patch because it is a symptom of events coming from the target-list and debugger targets module...
     17 PromiseTestUtils.allowMatchingRejectionsGlobally(/Page has navigated/);
     18 
     19 const TEST_URL =
     20  "data:text/html;charset=utf-8," +
     21  "<html><head><title>Test reload</title></head>" +
     22  "<body><h1>Testing reload from devtools</h1></body></html>";
     23 
     24 const { Toolbox } = require("resource://devtools/client/framework/toolbox.js");
     25 const L10N = new LocalizationHelper(
     26  "devtools/client/locales/toolbox.properties"
     27 );
     28 
     29 // Track how many page reloads we've sent to the page.
     30 var reloadsSent = 0;
     31 
     32 add_task(async function () {
     33  await addTab(TEST_URL);
     34  const tab = gBrowser.selectedTab;
     35  const toolIDs = await getSupportedToolIds(tab);
     36 
     37  info(
     38    "Display the toolbox, docked at the bottom, with the first tool selected"
     39  );
     40  const toolbox = await gDevTools.showToolboxForTab(tab, {
     41    toolId: toolIDs[0],
     42    hostType: Toolbox.HostType.BOTTOM,
     43  });
     44 
     45  info(
     46    "Listen to page reloads to check that they are indeed sent by the toolbox"
     47  );
     48  let reloadDetected = 0;
     49  const reloadCounter = () => {
     50    reloadDetected++;
     51    info("Detected reload #" + reloadDetected);
     52    is(
     53      reloadDetected,
     54      reloadsSent,
     55      "Detected the right number of reloads in the page"
     56    );
     57  };
     58 
     59  const removeLoadListener = BrowserTestUtils.addContentEventListener(
     60    gBrowser.selectedBrowser,
     61    "load",
     62    reloadCounter,
     63    {}
     64  );
     65 
     66  info("Start testing with the toolbox docked");
     67  // Note that we actually only test 1 tool in docked mode, to cut down on test time.
     68  await testOneTool(toolbox, toolIDs[toolIDs.length - 1]);
     69 
     70  info("Switch to undocked mode");
     71  await toolbox.switchHost(Toolbox.HostType.WINDOW);
     72  toolbox.win.focus();
     73 
     74  info("Now test with the toolbox undocked");
     75  for (const toolID of toolIDs) {
     76    await testOneTool(toolbox, toolID);
     77  }
     78 
     79  info("Switch back to docked mode");
     80  await toolbox.switchHost(Toolbox.HostType.BOTTOM);
     81 
     82  removeLoadListener();
     83 
     84  await toolbox.destroy();
     85  gBrowser.removeCurrentTab();
     86 });
     87 
     88 async function testOneTool(toolbox, toolID) {
     89  info(`Select tool ${toolID}`);
     90  await toolbox.selectTool(toolID);
     91 
     92  assertThemeStyleSheet(toolbox, toolID);
     93 
     94  await testReload("toolbox.reload.key", toolbox);
     95  await testReload("toolbox.reload2.key", toolbox);
     96  await testReload("toolbox.forceReload.key", toolbox);
     97  await testReload("toolbox.forceReload2.key", toolbox);
     98 }
     99 
    100 async function testReload(shortcut, toolbox) {
    101  info(`Reload with ${shortcut}`);
    102 
    103  await sendToolboxReloadShortcut(L10N.getStr(shortcut), toolbox);
    104  reloadsSent++;
    105 }
    106 
    107 /**
    108 * As opening all panels is an expensive operation, reuse this test in order
    109 * to add a few assertions around panel's stylesheets.
    110 * Ensure the proper ordering of the theme stylesheet. `global.css` should come
    111 * first if it exists, then the theme.
    112 */
    113 function assertThemeStyleSheet(toolbox, toolID) {
    114  const iframe = toolbox.doc.getElementById("toolbox-panel-iframe-" + toolID);
    115  const styleSheets = iframe.contentDocument.querySelectorAll(
    116    `link[rel="stylesheet"]`
    117  );
    118  ok(
    119    !!styleSheets.length,
    120    `The panel ${toolID} should have at least have one stylesheet`
    121  );
    122 
    123  // In the web console, we have a special case where global.css is registered very first
    124  if (styleSheets[0].href === "chrome://global/skin/global.css") {
    125    is(styleSheets[1].href, "chrome://devtools/skin/light-theme.css");
    126  } else {
    127    // Otherwise, in all other panels, the theme file is registered very first
    128    is(styleSheets[0].href, "chrome://devtools/skin/light-theme.css");
    129  }
    130 }