tor-browser

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

browser_toolbox_tools_per_toolbox_registration.js (3703B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const TEST_URL = `data:text/html,<!DOCTYPE html>
      5  <html>
      6    <head>
      7      <meta charset="utf-8">
      8    </head>
      9    <body>
     10      test for registering and unregistering tools to a specific toolbox
     11    </body>
     12  </html>`;
     13 
     14 const TOOL_ID = "test-toolbox-tool";
     15 var gToolbox;
     16 
     17 function test() {
     18  addTab(TEST_URL).then(async tab => {
     19    gDevTools
     20      .showToolboxForTab(tab)
     21      .then(toolboxRegister)
     22      .then(testToolRegistered);
     23  });
     24 }
     25 
     26 var resolveToolInstanceBuild;
     27 var waitForToolInstanceBuild = new Promise(resolve => {
     28  resolveToolInstanceBuild = resolve;
     29 });
     30 
     31 var resolveToolInstanceDestroyed;
     32 var waitForToolInstanceDestroyed = new Promise(resolve => {
     33  resolveToolInstanceDestroyed = resolve;
     34 });
     35 
     36 function toolboxRegister(toolbox) {
     37  gToolbox = toolbox;
     38 
     39  waitForToolInstanceBuild = new Promise(resolve => {
     40    resolveToolInstanceBuild = resolve;
     41  });
     42 
     43  info("add per-toolbox tool in the opened toolbox.");
     44 
     45  gToolbox.addAdditionalTool({
     46    id: TOOL_ID,
     47    // The size of the label can make the test fail if it's too long.
     48    // See ok(tab, ...) assert below and Bug 1596345.
     49    label: "Test Tool",
     50    inMenu: true,
     51    isToolSupported: () => true,
     52    build() {
     53      info("per-toolbox tool has been built.");
     54      resolveToolInstanceBuild();
     55 
     56      return {
     57        destroy: () => {
     58          info("per-toolbox tool has been destroyed.");
     59          resolveToolInstanceDestroyed();
     60        },
     61      };
     62    },
     63    key: "t",
     64  });
     65 }
     66 
     67 function testToolRegistered() {
     68  ok(
     69    !gDevTools.getToolDefinitionMap().has(TOOL_ID),
     70    "per-toolbox tool is not registered globally"
     71  );
     72  ok(
     73    gToolbox.hasAdditionalTool(TOOL_ID),
     74    "per-toolbox tool registered to the specific toolbox"
     75  );
     76 
     77  // Test that the tool appeared in the UI.
     78  const doc = gToolbox.doc;
     79  const tab = getToolboxTab(doc, TOOL_ID);
     80 
     81  ok(tab, "new tool's tab exists in toolbox UI");
     82 
     83  const panel = doc.getElementById("toolbox-panel-" + TOOL_ID);
     84  ok(panel, "new tool's panel exists in toolbox UI");
     85 
     86  for (const win of getAllBrowserWindows()) {
     87    const key = win.document.getElementById("key_" + TOOL_ID);
     88    if (win.document == doc) {
     89      continue;
     90    }
     91    ok(!key, "key for new tool should not exists in the other browser windows");
     92    const menuitem = win.document.getElementById("menuitem_" + TOOL_ID);
     93    ok(!menuitem, "menu item should not exists in the other browser window");
     94  }
     95 
     96  // Test that the tool is built once selected and then test its unregistering.
     97  info("select per-toolbox tool in the opened toolbox.");
     98  gDevTools
     99    .showToolboxForTab(gBrowser.selectedTab, { toolId: TOOL_ID })
    100    .then(waitForToolInstanceBuild)
    101    .then(testUnregister);
    102 }
    103 
    104 function getAllBrowserWindows() {
    105  return Array.from(Services.wm.getEnumerator("navigator:browser"));
    106 }
    107 
    108 function testUnregister() {
    109  info("remove per-toolbox tool in the opened toolbox.");
    110  gToolbox.removeAdditionalTool(TOOL_ID);
    111 
    112  Promise.all([waitForToolInstanceDestroyed]).then(toolboxToolUnregistered);
    113 }
    114 
    115 function toolboxToolUnregistered() {
    116  ok(
    117    !gToolbox.hasAdditionalTool(TOOL_ID),
    118    "per-toolbox tool unregistered from the specific toolbox"
    119  );
    120 
    121  // test that it disappeared from the UI
    122  const doc = gToolbox.doc;
    123  const tab = getToolboxTab(doc, TOOL_ID);
    124  ok(!tab, "tool's tab was removed from the toolbox UI");
    125 
    126  const panel = doc.getElementById("toolbox-panel-" + TOOL_ID);
    127  ok(!panel, "tool's panel was removed from toolbox UI");
    128 
    129  cleanup();
    130 }
    131 
    132 function cleanup() {
    133  gToolbox.destroy().then(() => {
    134    gToolbox = null;
    135    gBrowser.removeCurrentTab();
    136    finish();
    137  });
    138 }