tor-browser

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

browser_toolbox_dynamic_registration.js (2352B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const TEST_URL =
      5  "data:text/html,test for dynamically registering and unregistering tools";
      6 
      7 var gToolbox;
      8 
      9 function test() {
     10  addTab(TEST_URL).then(async tab => {
     11    gDevTools.showToolboxForTab(tab).then(testRegister);
     12  });
     13 }
     14 
     15 function testRegister(toolbox) {
     16  gToolbox = toolbox;
     17  gDevTools.once("tool-registered", toolRegistered);
     18 
     19  gDevTools.registerTool({
     20    id: "testTool",
     21    label: "Test Tool",
     22    inMenu: true,
     23    isToolSupported: () => true,
     24    build() {},
     25  });
     26 }
     27 
     28 function toolRegistered(toolId) {
     29  is(toolId, "testTool", "tool-registered event handler sent tool id");
     30 
     31  ok(gDevTools.getToolDefinitionMap().has(toolId), "tool added to map");
     32 
     33  // test that it appeared in the UI
     34  const doc = gToolbox.doc;
     35  const tab = getToolboxTab(doc, toolId);
     36  ok(tab, "new tool's tab exists in toolbox UI");
     37 
     38  const panel = doc.getElementById("toolbox-panel-" + toolId);
     39  ok(panel, "new tool's panel exists in toolbox UI");
     40 
     41  for (const win of getAllBrowserWindows()) {
     42    const menuitem = win.document.getElementById("menuitem_" + toolId);
     43    ok(menuitem, "menu item of new tool added to every browser window");
     44  }
     45 
     46  // then unregister it
     47  testUnregister();
     48 }
     49 
     50 function getAllBrowserWindows() {
     51  return Array.from(Services.wm.getEnumerator("navigator:browser"));
     52 }
     53 
     54 function testUnregister() {
     55  gDevTools.once("tool-unregistered", toolUnregistered);
     56 
     57  gDevTools.unregisterTool("testTool");
     58 }
     59 
     60 function toolUnregistered(toolId) {
     61  is(toolId, "testTool", "tool-unregistered event handler sent tool id");
     62 
     63  ok(!gDevTools.getToolDefinitionMap().has(toolId), "tool removed from map");
     64 
     65  // test that it disappeared from the UI
     66  const doc = gToolbox.doc;
     67  const tab = getToolboxTab(doc, toolId);
     68  ok(!tab, "tool's tab was removed from the toolbox UI");
     69 
     70  const panel = doc.getElementById("toolbox-panel-" + toolId);
     71  ok(!panel, "tool's panel was removed from toolbox UI");
     72 
     73  for (const win of getAllBrowserWindows()) {
     74    const menuitem = win.document.getElementById("menuitem_" + toolId);
     75    ok(!menuitem, "menu item removed from every browser window");
     76  }
     77 
     78  cleanup();
     79 }
     80 
     81 function cleanup() {
     82  gToolbox.destroy().then(() => {
     83    gToolbox = null;
     84    gBrowser.removeCurrentTab();
     85    finish();
     86  });
     87 }