|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 let doc = null, toolbox = null, panelWin = null, modifiedPrefs = []; |
|
5 |
|
6 function test() { |
|
7 waitForExplicitFinish(); |
|
8 |
|
9 gBrowser.selectedTab = gBrowser.addTab(); |
|
10 let target = TargetFactory.forTab(gBrowser.selectedTab); |
|
11 |
|
12 gBrowser.selectedBrowser.addEventListener("load", function onLoad(evt) { |
|
13 gBrowser.selectedBrowser.removeEventListener(evt.type, onLoad, true); |
|
14 gDevTools.showToolbox(target) |
|
15 .then(testSelectTool) |
|
16 .then(testToggleToolboxButtons) |
|
17 .then(testPrefsAreRespectedWhenReopeningToolbox) |
|
18 .then(cleanup, errorHandler); |
|
19 }, true); |
|
20 |
|
21 content.location = "data:text/html;charset=utf8,test for dynamically registering and unregistering tools"; |
|
22 } |
|
23 |
|
24 function testPrefsAreRespectedWhenReopeningToolbox() { |
|
25 let deferred = promise.defer(); |
|
26 let target = TargetFactory.forTab(gBrowser.selectedTab); |
|
27 |
|
28 info ("Closing toolbox to test after reopening"); |
|
29 gDevTools.closeToolbox(target).then(() => { |
|
30 let target = TargetFactory.forTab(gBrowser.selectedTab); |
|
31 gDevTools.showToolbox(target) |
|
32 .then(testSelectTool) |
|
33 .then(() => { |
|
34 info ("Toolbox has been reopened. Checking UI state."); |
|
35 testPreferenceAndUIStateIsConsistent(); |
|
36 deferred.resolve(); |
|
37 }); |
|
38 }); |
|
39 |
|
40 return deferred.promise; |
|
41 } |
|
42 |
|
43 function testSelectTool(aToolbox) { |
|
44 let deferred = promise.defer(); |
|
45 info ("Selecting the options panel"); |
|
46 |
|
47 toolbox = aToolbox; |
|
48 doc = toolbox.doc; |
|
49 toolbox.once("options-selected", (event, tool) => { |
|
50 ok(true, "Options panel selected via selectTool method"); |
|
51 panelWin = tool.panelWin; |
|
52 deferred.resolve(); |
|
53 }); |
|
54 toolbox.selectTool("options"); |
|
55 |
|
56 return deferred.promise; |
|
57 } |
|
58 |
|
59 function testPreferenceAndUIStateIsConsistent() { |
|
60 let checkNodes = [...panelWin.document.querySelectorAll("#enabled-toolbox-buttons-box > checkbox")]; |
|
61 let toolboxButtonNodes = [...doc.querySelectorAll("#toolbox-buttons > toolbarbutton")]; |
|
62 let toggleableTools = toolbox.toolboxButtons; |
|
63 |
|
64 for (let tool of toggleableTools) { |
|
65 let isVisible = getBoolPref(tool.visibilityswitch); |
|
66 |
|
67 let button = toolboxButtonNodes.filter(button=>button.id === tool.id)[0]; |
|
68 is (!button.hasAttribute("hidden"), isVisible, "Button visibility matches pref for " + tool.id); |
|
69 |
|
70 let check = checkNodes.filter(node=>node.id === tool.id)[0]; |
|
71 is (check.checked, isVisible, "Checkbox should be selected based on current pref for " + tool.id); |
|
72 } |
|
73 } |
|
74 |
|
75 function testToggleToolboxButtons() { |
|
76 let checkNodes = [...panelWin.document.querySelectorAll("#enabled-toolbox-buttons-box > checkbox")]; |
|
77 let toolboxButtonNodes = [...doc.querySelectorAll("#toolbox-buttons > toolbarbutton")]; |
|
78 let visibleButtons = toolboxButtonNodes.filter(button=>!button.hasAttribute("hidden")); |
|
79 let toggleableTools = toolbox.toolboxButtons; |
|
80 |
|
81 is (checkNodes.length, toggleableTools.length, "All of the buttons are toggleable." ); |
|
82 is (checkNodes.length, toolboxButtonNodes.length, "All of the DOM buttons are toggleable." ); |
|
83 |
|
84 for (let tool of toggleableTools) { |
|
85 let id = tool.id; |
|
86 let matchedCheckboxes = checkNodes.filter(node=>node.id === id); |
|
87 let matchedButtons = toolboxButtonNodes.filter(button=>button.id === id); |
|
88 ok (matchedCheckboxes.length === 1, |
|
89 "There should be a single toggle checkbox for: " + id); |
|
90 ok (matchedButtons.length === 1, |
|
91 "There should be a DOM button for: " + id); |
|
92 is (matchedButtons[0], tool.button, |
|
93 "DOM buttons should match for: " + id); |
|
94 |
|
95 is (matchedCheckboxes[0].getAttribute("label"), tool.label, |
|
96 "The label for checkbox matches the tool definition.") |
|
97 is (matchedButtons[0].getAttribute("tooltiptext"), tool.label, |
|
98 "The tooltip for button matches the tool definition.") |
|
99 } |
|
100 |
|
101 // Store modified pref names so that they can be cleared on error. |
|
102 for (let tool of toggleableTools) { |
|
103 let pref = tool.visibilityswitch; |
|
104 modifiedPrefs.push(pref); |
|
105 } |
|
106 |
|
107 // Try checking each checkbox, making sure that it changes the preference |
|
108 for (let node of checkNodes) { |
|
109 let tool = toggleableTools.filter(tool=>tool.id === node.id)[0]; |
|
110 let isVisible = getBoolPref(tool.visibilityswitch); |
|
111 |
|
112 testPreferenceAndUIStateIsConsistent(); |
|
113 toggleButton(node); |
|
114 testPreferenceAndUIStateIsConsistent(); |
|
115 |
|
116 let isVisibleAfterClick = getBoolPref(tool.visibilityswitch); |
|
117 |
|
118 is (isVisible, !isVisibleAfterClick, |
|
119 "Clicking on the node should have toggled visibility preference for " + tool.visibilityswitch); |
|
120 } |
|
121 |
|
122 return promise.resolve(); |
|
123 } |
|
124 |
|
125 function getBoolPref(key) { |
|
126 return Services.prefs.getBoolPref(key); |
|
127 } |
|
128 |
|
129 function toggleButton(node) { |
|
130 node.scrollIntoView(); |
|
131 EventUtils.synthesizeMouseAtCenter(node, {}, panelWin); |
|
132 } |
|
133 |
|
134 function cleanup() { |
|
135 toolbox.destroy().then(function() { |
|
136 gBrowser.removeCurrentTab(); |
|
137 for (let pref of modifiedPrefs) { |
|
138 Services.prefs.clearUserPref(pref); |
|
139 } |
|
140 toolbox = doc = panelWin = modifiedPrefs = null; |
|
141 finish(); |
|
142 }); |
|
143 } |
|
144 |
|
145 function errorHandler(error) { |
|
146 ok(false, "Unexpected error: " + error); |
|
147 cleanup(); |
|
148 } |