This commit is contained in:
Dão Gottwald 2011-09-24 12:24:30 +02:00
Родитель d219382f28 5e0e04c4bf
Коммит 3dfcbfe370
2 изменённых файлов: 146 добавлений и 272 удалений

Просмотреть файл

@ -135,6 +135,7 @@ Highlighter.prototype = {
this.handleResize();
},
/**
* Build the veil:
*
@ -554,7 +555,6 @@ Highlighter.prototype = {
var InspectorUI = {
browser: null,
tools: {},
toolEvents: {},
showTextNodesWithWhitespace: false,
inspecting: false,
treeLoaded: false,
@ -820,8 +820,6 @@ var InspectorUI = {
this.winID = this.getWindowID(this.win);
this.toolbar = document.getElementById("inspector-toolbar");
this.initTools();
if (!this.domplate) {
Cu.import("resource:///modules/domplate.jsm", this);
this.domplateUtils.setDOM(window);
@ -835,13 +833,6 @@ var InspectorUI = {
gBrowser.addProgressListener(InspectorProgressListener);
},
/**
* Register and initialize any included tools.
*/
initTools: function IUI_initTools()
{
},
/**
* Initialize highlighter.
*/
@ -918,12 +909,6 @@ var InspectorUI = {
}
this.stopInspecting();
this.saveToolState(this.winID);
this.toolsDo(function IUI_toolsHide(aTool) {
this.unregisterTool(aTool);
}.bind(this));
if (this.highlighter) {
this.highlighter.destroy();
this.highlighter = null;
@ -951,6 +936,13 @@ var InspectorUI = {
delete this.domplateUtils;
}
this.saveToolState(this.winID);
this.toolsDo(function IUI_toolsHide(aTool) {
if (aTool.panel) {
aTool.panel.hidePopup();
}
});
this.inspectCmd.setAttribute("checked", false);
this.browser = this.win = null; // null out references to browser and window
this.winID = null;
@ -1034,8 +1026,11 @@ var InspectorUI = {
}
this.ioBox.select(this.selection, true, true, aScroll);
}
this.toolsSelect();
this.toolsDo(function IUI_toolsOnSelect(aTool) {
if (aTool.panel.state == "open") {
aTool.onSelect.apply(aTool.context, [aNode]);
}
});
},
/////////////////////////////////////////////////////////////////////////
@ -1050,8 +1045,6 @@ var InspectorUI = {
this.startInspecting();
}
this.restoreToolState(this.winID);
this.win.focus();
Services.obs.notifyObservers(null, INSPECTOR_NOTIFICATIONS.OPENED, null);
},
@ -1086,6 +1079,7 @@ var InspectorUI = {
}, INSPECTOR_NOTIFICATIONS.CLOSED, false);
} else {
this.openInspectorUI();
this.restoreToolState(winID);
}
}
@ -1604,19 +1598,6 @@ var InspectorUI = {
this._log("END TRACE");
},
/**
* Get the toolbar button name for a given id string. Used by the
* registerTools API to retrieve a consistent name for toolbar buttons
* based on the ID of the tool.
* @param anId String
* id of the tool to be buttonized
* @returns String
*/
getToolbarButtonId: function IUI_createButtonId(anId)
{
return "inspector-" + anId + "-toolbutton";
},
/**
* Register an external tool with the inspector.
*
@ -1627,174 +1608,93 @@ var InspectorUI = {
* icon: "chrome://somepath.png",
* tooltiptext: "Button tooltip",
* accesskey: "S",
* isOpen: object.property, (getter) returning true if tool is open.
* onSelect: object.method,
* show: object.method, called to show the tool when button is pressed.
* hide: object.method, called to hide the tool when button is pressed.
* unregister: object.method, called when tool should be destroyed.
* onShow: object.method,
* onHide: object.method,
* panel: myTool.panel
* }
*
* @param aRegObj Object
* The Registration Object used to register this tool described
* above. The tool should cache this object for later deregistration.
* @param aRegObj
*/
registerTool: function IUI_registerTool(aRegObj)
{
if (this.toolRegistered(aRegObj.id)) {
registerTool: function IUI_RegisterTool(aRegObj) {
if (this.tools[aRegObj.id]) {
return;
} else {
let id = aRegObj.id;
let buttonId = "inspector-" + id + "-toolbutton";
aRegObj.buttonId = buttonId;
aRegObj.panel.addEventListener("popuphiding",
function IUI_toolPanelHiding() {
btn.setAttribute("checked", "false");
}, false);
aRegObj.panel.addEventListener("popupshowing",
function IUI_toolPanelShowing() {
btn.setAttribute("checked", "true");
}, false);
this.tools[id] = aRegObj;
}
this.tools[aRegObj.id] = aRegObj;
let buttonContainer = document.getElementById("inspector-tools");
let toolbox = document.getElementById("inspector-tools");
let btn = document.createElement("toolbarbutton");
let buttonId = this.getToolbarButtonId(aRegObj.id);
btn.setAttribute("id", buttonId);
btn.setAttribute("id", aRegObj.buttonId);
btn.setAttribute("label", aRegObj.label);
btn.setAttribute("tooltiptext", aRegObj.tooltiptext);
btn.setAttribute("accesskey", aRegObj.accesskey);
btn.setAttribute("class", "toolbarbutton-text");
btn.setAttribute("image", aRegObj.icon || "");
buttonContainer.appendChild(btn);
toolbox.appendChild(btn);
/**
* Save the registered tool's toolbar button's click handler so we can remove
* it at deregistration time.
* @param aButton XUL:toolbarbutton
* @param aCallback Function the click event handler for the button
*/
function bindToolEvent(aWidget, aEvent, aCallback)
{
let toolEvent = aWidget.id + "_" + aEvent;
InspectorUI.toolEvents[toolEvent] = aCallback;
aWidget.addEventListener(aEvent, aCallback, false);
}
bindToolEvent(btn, "click",
function IUI_toolButtonClick(aEvent) {
if (btn.checked) {
this.toolHide(aRegObj);
btn.addEventListener("click",
function IUI_ToolButtonClick(aEvent) {
if (btn.getAttribute("checked") == "true") {
aRegObj.onHide.apply(aRegObj.context);
} else {
this.toolShow(aRegObj);
aRegObj.onShow.apply(aRegObj.context, [InspectorUI.selection]);
aRegObj.onSelect.apply(aRegObj.context, [InspectorUI.selection]);
}
}.bind(this));
if (aRegObj.panel) {
bindToolEvent(aRegObj.panel, "popuphiding",
function IUI_toolPanelHiding() {
btn.checked = false;
});
}
}, false);
},
/**
* Show the specified tool.
* @param aTool Object (see comment for IUI_registerTool)
*/
toolShow: function IUI_toolShow(aTool)
{
aTool.show.call(aTool.context, this.selection);
document.getElementById(this.getToolbarButtonId(aTool.id)).checked = true;
},
/**
* Hide the specified tool.
* @param aTool Object (see comment for IUI_registerTool)
*/
toolHide: function IUI_toolHide(aTool)
{
aTool.hide.call(aTool.context);
document.getElementById(this.getToolbarButtonId(aTool.id)).checked = false;
},
/**
* Unregister the registered tool, unbinding click events for the buttons
* and showing and hiding events for the panel.
* @param aRegObj Object
* The registration object used to register the tool.
*/
unregisterTool: function IUI_unregisterTool(aRegObj)
{
let button = document.getElementById(this.getToolbarButtonId(aRegObj.id));
/**
* Unregister the click handler for the registered tool's button.
* @param aButton XUL:toolbarbutton
*/
function unbindToolEvent(aWidget, aEvent)
{
let toolEvent = aWidget.id + "_" + aEvent;
if (!InspectorUI.toolEvents[toolEvent]) {
return;
}
aWidget.removeEventListener(aEvent, InspectorUI.toolEvents[toolEvent], false);
delete InspectorUI.toolEvents[toolEvent]
}
let buttonContainer = document.getElementById("inspector-tools");
unbindToolEvent(button, "click");
if (aRegObj.panel)
unbindToolEvent(aRegObj.panel, "popuphiding");
buttonContainer.removeChild(button);
if (aRegObj.unregister)
aRegObj.unregister.call(aRegObj.context);
delete this.tools[aRegObj.id];
},
/**
* Save a list of open tools to the inspector store.
*
* @param aWinID The ID of the window used to save the associated tools
*/
/**
* Save a list of open tools to the inspector store.
*
* @param aWinID The ID of the window used to save the associated tools
*/
saveToolState: function IUI_saveToolState(aWinID)
{
let openTools = {};
this.toolsDo(function IUI_toolsSetId(aTool) {
if (aTool.isOpen) {
if (aTool.panel.state == "open") {
openTools[aTool.id] = true;
}
});
InspectorStore.setValue(aWinID, "openTools", openTools);
},
/**
* Restore tools previously save using saveToolState().
*
* @param aWinID The ID of the window to which the associated tools are to be
* restored.
*/
/**
* Restore tools previously save using saveToolState().
*
* @param aWinID The ID of the window to which the associated tools are to be
* restored.
*/
restoreToolState: function IUI_restoreToolState(aWinID)
{
let openTools = InspectorStore.getValue(aWinID, "openTools");
InspectorUI.selection = InspectorUI.selection;
if (openTools) {
this.toolsDo(function IUI_toolsOnShow(aTool) {
if (aTool.id in openTools) {
this.toolShow(aTool);
aTool.onShow.apply(aTool.context, [InspectorUI.selection]);
}
}.bind(this));
});
}
},
/**
* For each tool in the tools collection select the current node that is
* selected in the highlighter
*/
toolsSelect: function IUI_toolsSelect()
{
this.toolsDo(function IUI_toolsOnSelect(aTool) {
if (aTool.isOpen) {
aTool.onSelect.call(aTool.context, InspectorUI.selection);
}
});
},
/**
* Loop through all registered tools and pass each into the provided function
*
* @param aFunction The function to which each tool is to be passed
*/
toolsDo: function IUI_toolsDo(aFunction)
@ -1803,15 +1703,6 @@ var InspectorUI = {
aFunction(tool);
}
},
/**
* Check if a tool is registered?
* @param aId The id of the tool to check
*/
toolRegistered: function IUI_toolRegistered(aId)
{
return aId in this.tools;
},
};
/**

Просмотреть файл

@ -39,20 +39,16 @@
let doc;
let h1;
let p2;
let toolsLength = 0;
let toolEvents = 0;
let tool1;
let tool2;
let tool3;
let initToolsMethod = InspectorUI.initTools;
function createDocument()
{
let div = doc.createElement("div");
h1 = doc.createElement("h1");
let h1 = doc.createElement("h1");
let p1 = doc.createElement("p");
p2 = doc.createElement("p");
let p2 = doc.createElement("p");
let div2 = doc.createElement("div");
let p3 = doc.createElement("p");
doc.title = "Inspector Tree Selection Test";
@ -83,70 +79,73 @@ function createDocument()
function setupHighlighterTests()
{
h1 = doc.querySelectorAll("h1")[0];
ok(h1, "we have the header node");
Services.obs.addObserver(inspectorOpen, INSPECTOR_NOTIFICATIONS.OPENED, false);
registerTools();
Services.obs.addObserver(inspectorOpen, "inspector-opened", false);
InspectorUI.toggleInspectorUI();
}
function inspectorOpen()
{
info("we received the inspector-opened notification");
Services.obs.removeObserver(inspectorOpen, INSPECTOR_NOTIFICATIONS.OPENED);
toolsLength = InspectorUI.tools.length;
toolEvents = InspectorUI.toolEvents.length;
info("tools registered");
Services.obs.addObserver(startToolTests, INSPECTOR_NOTIFICATIONS.HIGHLIGHTING, false);
InspectorUI.inspectNode(h1);
Services.obs.removeObserver(inspectorOpen, "inspector-opened", false);
Services.obs.addObserver(startToolTests, "inspector-highlighting", false);
let rect = h1.getBoundingClientRect();
executeSoon(function() {
EventUtils.synthesizeMouse(h1, 2, 2, {type: "mousemove"}, content);
});
}
function startToolTests(evt)
{
Services.obs.removeObserver(startToolTests, INSPECTOR_NOTIFICATIONS.HIGHLIGHTING);
info("we received the inspector-highlighting notification");
Services.obs.removeObserver(startToolTests, "inspector-highlighting", false);
InspectorUI.stopInspecting();
info("Getting InspectorUI.tools");
let tools = InspectorUI.tools;
tool1 = InspectorUI.tools["tool_1"];
tool2 = InspectorUI.tools["tool_2"];
tool3 = InspectorUI.tools["tool_3"];
info("Checking panel states 1");
ok(!tool1.isOpen, "Panel 1 is closed");
ok(!tool2.isOpen, "Panel 2 is closed");
ok(!tool3.isOpen, "Panel 3 is closed");
ok(tool1.context.panelIsClosed, "Panel 1 is closed");
ok(tool2.context.panelIsClosed, "Panel 2 is closed");
ok(tool3.context.panelIsClosed, "Panel 3 is closed");
info("Calling show method for all tools");
InspectorUI.toolShow(tool1);
InspectorUI.toolShow(tool2);
InspectorUI.toolShow(tool3);
tool1.onShow.apply(tool1.context, [h1]);
tool2.onShow.apply(tool2.context, [h1]);
tool3.onShow.apply(tool3.context, [h1]);
info("Checking panel states 2");
ok(tool1.isOpen, "Panel 1 is open");
ok(tool2.isOpen, "Panel 2 is open");
ok(tool3.isOpen, "Panel 3 is open");
ok(tool1.context.panelIsOpen, "Panel 1 is open");
ok(tool2.context.panelIsOpen, "Panel 2 is open");
ok(tool3.context.panelIsOpen, "Panel 3 is open");
info("Calling selectNode method for all tools, should see 3 selects");
InspectorUI.inspectNode(p2);
info("Calling selectNode method for all tools");
tool1.onSelect.apply(tool1.context, [h1]);
tool2.onSelect.apply(tool2.context, [h1]);
tool3.onSelect.apply(tool3.context, [h1]);
info("Calling hide method for all tools");
InspectorUI.toolHide(tool1);
InspectorUI.toolHide(tool2);
InspectorUI.toolHide(tool3);
tool1.onHide.apply(tool1.context, [h1]);
tool2.onHide.apply(tool2.context, [h1]);
tool3.onHide.apply(tool3.context, [h1]);
info("Checking panel states 3");
ok(!tool1.isOpen, "Panel 1 is closed");
ok(!tool2.isOpen, "Panel 2 is closed");
ok(!tool3.isOpen, "Panel 3 is closed");
ok(tool1.context.panelIsClosed, "Panel 1 is closed");
ok(tool2.context.panelIsClosed, "Panel 2 is closed");
ok(tool3.context.panelIsClosed, "Panel 3 is closed");
info("Showing tools 1 & 3");
InspectorUI.toolShow(tool1);
InspectorUI.toolShow(tool3);
tool1.onShow.apply(tool1.context, [h1]);
tool3.onShow.apply(tool3.context, [h1]);
info("Checking panel states 4");
ok(tool1.isOpen, "Panel 1 is open");
ok(!tool2.isOpen, "Panel 2 is closed");
ok(tool3.isOpen, "Panel 3 is open");
ok(tool1.context.panelIsOpen, "Panel 1 is open");
ok(tool2.context.panelIsClosed, "Panel 2 is closed");
ok(tool3.context.panelIsOpen, "Panel 3 is open");
gBrowser.selectedTab = gBrowser.addTab();
gBrowser.selectedBrowser.addEventListener("load", function() {
@ -161,52 +160,24 @@ function testSecondTab()
{
info("Opened second tab");
info("Checking panel states 5");
let tools = InspectorUI.tools;
ok(!(tool1 in tools), "Panel 1 not in tools");
ok(!(tool2 in tools), "Panel 2 not in tools");
ok(!(tool3 in tools), "Panel 3 not in tools");
ok(tool1.context.panelIsClosed, "Panel 1 is closed");
ok(tool2.context.panelIsClosed, "Panel 2 is closed");
ok(tool3.context.panelIsClosed, "Panel 3 is closed");
info("Closing current tab");
Services.obs.addObserver(testOriginalTab, INSPECTOR_NOTIFICATIONS.OPENED, false);
gBrowser.removeCurrentTab();
}
function testOriginalTab()
{
Services.obs.removeObserver(testOriginalTab, INSPECTOR_NOTIFICATIONS.OPENED);
info("Checking panel states 6");
ok(tool1.context.panelIsOpen, "Panel 1 is open");
ok(tool2.context.panelIsClosed, "Panel 2 is closed");
ok(tool3.context.panelIsOpen, "Panel 3 is open");
info("Tools: " + InspectorUI.tools);
// reacquaint ourselves with our tools
tool1 = InspectorUI.tools["tool_1"];
tool2 = InspectorUI.tools["tool_2"];
tool3 = InspectorUI.tools["tool_3"];
ok(tool1.isOpen, "Panel 1 is open after reactivation");
ok(!tool2.isOpen, "Panel 2 is closed after reactivation");
ok(tool3.isOpen, "Panel 3 is open after reactivation");
Services.obs.addObserver(unregisterTools, INSPECTOR_NOTIFICATIONS.CLOSED, false);
InspectorUI.closeInspectorUI(true);
}
function unregisterTools()
{
Services.obs.removeObserver(unregisterTools, INSPECTOR_NOTIFICATIONS.CLOSED);
let tools = InspectorUI.tools;
ok(!(tool1 in tools), "Tool 1 removed");
ok(!(tool2 in tools), "Tool 2 removed");
ok(!(tool3 in tools), "Tool 3 removed");
is(tools.length, toolsLength, "Number of Registered Tools matches original");
is(InspectorUI.toolEvents.length, toolEvents, "Number of tool events matches original");
finishUp();
executeSoon(finishUp);
}
function finishUp() {
InspectorUI.closeInspectorUI(true);
gBrowser.removeCurrentTab();
InspectorUI.initTools = initToolsMethod;
finish();
}
@ -217,20 +188,18 @@ function test()
gBrowser.selectedBrowser.addEventListener("load", function() {
gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
doc = content.document;
waitForFocus(createDocument, content);
waitForFocus(registerTools, content);
}, true);
content.location = "data:text/html,registertool tests for inspector";
}
function registerTools()
{
InspectorUI.initTools = function() {
info("(re)registering tools");
registerTool(new testTool("tool_1", "Tool 1", "Tool 1 tooltip", "I"));
registerTool(new testTool("tool_2", "Tool 2", "Tool 2 tooltip", "J"));
registerTool(new testTool("tool_3", "Tool 3", "Tool 3 tooltip", "K"));
}
createDocument();
registerTool(new testTool("tool_1", "Tool 1", "Tool 1 tooltip", "I"));
registerTool(new testTool("tool_2", "Tool 2", "Tool 2 tooltip", "J"));
registerTool(new testTool("tool_3", "Tool 3", "Tool 3 tooltip", "K"));
}
function registerTool(aTool)
@ -241,11 +210,10 @@ function registerTool(aTool)
tooltiptext: aTool.tooltip,
accesskey: aTool.accesskey,
context: aTool,
get isOpen() aTool.isOpen(),
onSelect: aTool.selectNode,
show: aTool.show,
hide: aTool.hide,
unregister: aTool.destroy,
onShow: aTool.show,
onHide: aTool.hide,
panel: aTool.panel
});
}
@ -255,13 +223,19 @@ function testTool(aToolId, aLabel, aTooltip, aAccesskey)
this.id = aToolId;
this.label = aLabel;
this.tooltip = aTooltip;
this.accesskey = aAccesskey;
this._isOpen = false;
this.accesskey = aAccesskey
this.panel = this.createPanel();
}
testTool.prototype = {
isOpen: function BIR_isOpen() {
return this._isOpen;
get panelIsOpen()
{
return this.panel.state == "open" || this.panel.state == "showing";
},
get panelIsClosed()
{
return this.panel.state == "closed" || this.panel.state == "hiding";
},
selectNode: function BIR_selectNode(aNode) {
@ -270,23 +244,32 @@ testTool.prototype = {
},
show: function BIR_show(aNode) {
this._isOpen = true;
this.panel.openPopup(gBrowser.selectedBrowser,
"end_before", 0, 20, false, false);
is(InspectorUI.selection, aNode,
"show: currently selected node was passed: " + this.id);
},
hide: function BIR_hide() {
info(this.id + " hide");
this._isOpen = false;
this.panel.hidePopup();
},
destroy: function BIR_destroy() {
info("tool destroyed " + this.id);
if (this.isOpen())
this.hide();
delete this.id;
delete this.label;
delete this.tooltip;
delete this.accesskey;
createPanel: function BIR_createPanel() {
let popupSet = document.getElementById("mainPopupSet");
let ns = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
let panel = this.panel = document.createElementNS(ns, "panel");
panel.setAttribute("orient", "vertical");
panel.setAttribute("noautofocus", "true");
panel.setAttribute("noautohide", "true");
panel.setAttribute("titlebar", "normal");
panel.setAttribute("close", "true");
panel.setAttribute("label", "Panel for " + this.id);
panel.setAttribute("width", 200);
panel.setAttribute("height", 400);
popupSet.appendChild(panel);
ok(panel.parentNode == popupSet, "Panel created and appended successfully");
return panel;
},
};