michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: let toolbox; michael@0: michael@0: function test() michael@0: { michael@0: waitForExplicitFinish(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: let target = TargetFactory.forTab(gBrowser.selectedTab); michael@0: michael@0: gBrowser.selectedBrowser.addEventListener("load", function onLoad(evt) { michael@0: gBrowser.selectedBrowser.removeEventListener(evt.type, onLoad, true); michael@0: gDevTools.showToolbox(target).then(testRegister); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html,test for dynamically registering and unregistering tools"; michael@0: } michael@0: michael@0: function testRegister(aToolbox) michael@0: { michael@0: toolbox = aToolbox michael@0: gDevTools.once("tool-registered", toolRegistered); michael@0: michael@0: gDevTools.registerTool({ michael@0: id: "test-tool", michael@0: label: "Test Tool", michael@0: inMenu: true, michael@0: isTargetSupported: function() true, michael@0: build: function() {} michael@0: }); michael@0: } michael@0: michael@0: function toolRegistered(event, toolId) michael@0: { michael@0: is(toolId, "test-tool", "tool-registered event handler sent tool id"); michael@0: michael@0: ok(gDevTools.getToolDefinitionMap().has(toolId), "tool added to map"); michael@0: michael@0: // test that it appeared in the UI michael@0: let doc = toolbox.frame.contentDocument; michael@0: let tab = doc.getElementById("toolbox-tab-" + toolId); michael@0: ok(tab, "new tool's tab exists in toolbox UI"); michael@0: michael@0: let panel = doc.getElementById("toolbox-panel-" + toolId); michael@0: ok(panel, "new tool's panel exists in toolbox UI"); michael@0: michael@0: for (let win of getAllBrowserWindows()) { michael@0: let command = win.document.getElementById("Tools:" + toolId); michael@0: ok(command, "command for new tool added to every browser window"); michael@0: let menuitem = win.document.getElementById("menuitem_" + toolId); michael@0: ok(menuitem, "menu item of new tool added to every browser window"); michael@0: } michael@0: michael@0: // then unregister it michael@0: testUnregister(); michael@0: } michael@0: michael@0: function getAllBrowserWindows() { michael@0: let wins = []; michael@0: let enumerator = Services.wm.getEnumerator("navigator:browser"); michael@0: while (enumerator.hasMoreElements()) { michael@0: wins.push(enumerator.getNext()); michael@0: } michael@0: return wins; michael@0: } michael@0: michael@0: function testUnregister() michael@0: { michael@0: gDevTools.once("tool-unregistered", toolUnregistered); michael@0: michael@0: gDevTools.unregisterTool("test-tool"); michael@0: } michael@0: michael@0: function toolUnregistered(event, toolDefinition) michael@0: { michael@0: let toolId = toolDefinition.id; michael@0: is(toolId, "test-tool", "tool-unregistered event handler sent tool id"); michael@0: michael@0: ok(!gDevTools.getToolDefinitionMap().has(toolId), "tool removed from map"); michael@0: michael@0: // test that it disappeared from the UI michael@0: let doc = toolbox.frame.contentDocument; michael@0: let tab = doc.getElementById("toolbox-tab-" + toolId); michael@0: ok(!tab, "tool's tab was removed from the toolbox UI"); michael@0: michael@0: let panel = doc.getElementById("toolbox-panel-" + toolId); michael@0: ok(!panel, "tool's panel was removed from toolbox UI"); michael@0: michael@0: for (let win of getAllBrowserWindows()) { michael@0: let command = win.document.getElementById("Tools:" + toolId); michael@0: ok(!command, "command removed from every browser window"); michael@0: let menuitem = win.document.getElementById("menuitem_" + toolId); michael@0: ok(!menuitem, "menu item removed from every browser window"); michael@0: } michael@0: michael@0: cleanup(); michael@0: } michael@0: michael@0: function cleanup() michael@0: { michael@0: toolbox.destroy(); michael@0: toolbox = null; michael@0: gBrowser.removeCurrentTab(); michael@0: finish(); michael@0: }