3 <!-- ***** BEGIN LICENSE BLOCK *****
4 - Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 - The contents of this file are subject to the Mozilla Public License Version
7 - 1.1 (the "License"); you may not use this file except in compliance with
8 - the License. You may obtain a copy of the License at
9 - http://www.mozilla.org/MPL/
11 - Software distributed under the License is distributed on an "AS IS" basis,
12 - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 - for the specific language governing rights and limitations under the
16 - The Original Code is Native Menus Test code
18 - The Initial Developer of the Original Code is
19 - Mozilla Corporation.
20 - Portions created by the Initial Developer are Copyright (C) 2008
21 - the Initial Developer. All Rights Reserved.
24 - Josh Aas <josh@mozilla.com>
26 - Alternatively, the contents of this file may be used under the terms of
27 - either the GNU General Public License Version 2 or later (the "GPL"), or
28 - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 - in which case the provisions of the GPL or the LGPL are applicable instead
30 - of those above. If you wish to allow use of your version of this file only
31 - under the terms of either the GPL or the LGPL, and not to allow others to
32 - use your version of this file under the terms of the MPL, indicate your
33 - decision by deleting the provisions above and replace them with the notice
34 - and other provisions required by the GPL or the LGPL. If you do not delete
35 - the provisions above, a recipient may use your version of this file under
36 - the terms of any one of the MPL, the GPL or the LGPL.
38 - ***** END LICENSE BLOCK ***** -->
40 <?xml-stylesheet href=
"chrome://global/skin" type=
"text/css"?>
42 <window id=
"NativeMenuWindow"
43 xmlns=
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
47 title=
"Native Menu Test">
49 <command id=
"cmd_FooItem0" oncommand=
"executedCommandID = 'cmd_FooItem0';"/>
50 <command id=
"cmd_FooItem1" oncommand=
"executedCommandID = 'cmd_FooItem1';"/>
51 <command id=
"cmd_BarItem0" oncommand=
"executedCommandID = 'cmd_BarItem0';"/>
52 <command id=
"cmd_BarItem1" oncommand=
"executedCommandID = 'cmd_BarItem1';"/>
53 <command id=
"cmd_NewItem0" oncommand=
"executedCommandID = 'cmd_NewItem0';"/>
54 <command id=
"cmd_NewItem1" oncommand=
"executedCommandID = 'cmd_NewItem1';"/>
55 <command id=
"cmd_NewItem2" oncommand=
"executedCommandID = 'cmd_NewItem2';"/>
56 <command id=
"cmd_NewItem3" oncommand=
"executedCommandID = 'cmd_NewItem3';"/>
57 <command id=
"cmd_NewItem4" oncommand=
"executedCommandID = 'cmd_NewItem4';"/>
58 <command id=
"cmd_NewItem5" oncommand=
"executedCommandID = 'cmd_NewItem5';"/>
60 <!-- We do not modify any menus or menu items defined here in testing. These
61 serve as a baseline structure for us to test after other modifications.
62 We add children to the menubar defined here and test by modifying those
64 <menubar id=
"nativemenubar">
65 <menu id=
"foo" label=
"Foo">
67 <menuitem label=
"FooItem0" command=
"cmd_FooItem0"/>
68 <menuitem label=
"FooItem1" command=
"cmd_FooItem1"/>
72 <menuitem label=
"BarItem0" command=
"cmd_BarItem0"/>
73 <menuitem label=
"BarItem1" command=
"cmd_BarItem1"/>
80 <script type=
"application/javascript"><![CDATA[
82 function ok(condition, message) {
83 window.opener.wrappedJSObject.SimpleTest.ok(condition, message);
86 function onTestsFinished() {
88 window.opener.wrappedJSObject.SimpleTest.finish();
91 // Force a menu to update itself. All of the menus parents will be updated
92 // as well. An empty string will force a complete menu system reload.
93 function forceUpdateNativeMenuAt(location) {
94 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
95 var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
96 getInterface(Components.interfaces.nsIDOMWindowUtils);
98 utils.forceUpdateNativeMenuAt(location);
105 var executedCommandID =
"";
107 function testItem(location, targetID) {
108 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
109 var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
110 getInterface(Components.interfaces.nsIDOMWindowUtils);
111 var correctCommandHandler = false;
113 utils.activateNativeMenuItemAt(location);
114 correctCommandHandler = executedCommandID == targetID;
120 executedCommandID =
"";
121 return correctCommandHandler;
125 function createXULMenuPopup() {
126 const XUL_NS =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
127 var item = document.createElementNS(XUL_NS,
"menupopup");
131 function createXULMenu(aLabel) {
132 const XUL_NS =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
133 var item = document.createElementNS(XUL_NS,
"menu");
134 item.setAttribute(
"label", aLabel);
138 function createXULMenuItem(aLabel, aCommandId) {
139 const XUL_NS =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
140 var item = document.createElementNS(XUL_NS,
"menuitem");
141 item.setAttribute(
"label", aLabel);
142 item.setAttribute(
"command", aCommandId);
146 function runBaseMenuTests() {
147 forceUpdateNativeMenuAt(
"0|3");
148 return testItem(
"0|0",
"cmd_FooItem0") &&
149 testItem(
"0|1",
"cmd_FooItem1") &&
150 testItem(
"0|3|0",
"cmd_BarItem0") &&
151 testItem(
"0|3|1",
"cmd_BarItem1");
155 var _delayedOnLoad = function() {
156 // First let's run the base menu tests.
157 ok(runBaseMenuTests());
159 // Set up some nodes that we'll use.
160 var menubarNode = document.getElementById(
"nativemenubar");
161 var newMenu0 = createXULMenu(
"NewMenu0");
162 var newMenu1 = createXULMenu(
"NewMenu1");
163 var newMenuPopup0 = createXULMenuPopup();
164 var newMenuPopup1 = createXULMenuPopup();
165 var newMenuItem0 = createXULMenuItem(
"NewMenuItem0",
"cmd_NewItem0");
166 var newMenuItem1 = createXULMenuItem(
"NewMenuItem1",
"cmd_NewItem1");
167 var newMenuItem2 = createXULMenuItem(
"NewMenuItem2",
"cmd_NewItem2");
168 var newMenuItem3 = createXULMenuItem(
"NewMenuItem3",
"cmd_NewItem3");
169 var newMenuItem4 = createXULMenuItem(
"NewMenuItem4",
"cmd_NewItem4");
170 var newMenuItem5 = createXULMenuItem(
"NewMenuItem5",
"cmd_NewItem5");
172 // Create another submenu with hierarchy via DOM manipulation.
173 // ******************
174 // * Foo * NewMenu0 *
<- Menu bar
175 // ******************
177 // * NewMenuItem0 *
<- NewMenu0 submenu
182 // *******************************
183 // * NewMenu1
> * NewMenuItem3 *
<- NewMenu1 submenu
184 // *******************************
189 newMenu0.appendChild(newMenuPopup0);
190 newMenuPopup0.appendChild(newMenuItem0);
191 newMenuPopup0.appendChild(newMenuItem1);
192 newMenuPopup0.appendChild(newMenuItem2);
193 newMenuPopup0.appendChild(newMenu1);
194 newMenu1.appendChild(newMenuPopup1);
195 newMenuPopup1.appendChild(newMenuItem3);
196 newMenuPopup1.appendChild(newMenuItem4);
197 newMenuPopup1.appendChild(newMenuItem5);
198 //XXX - we have to append the menu to the top-level of the menu bar
199 // only after constructing it. If we append before construction, it is
200 // invalid because it has no children and we don't validate it if we add
202 menubarNode.appendChild(newMenu0);
203 forceUpdateNativeMenuAt(
"1|3");
204 // Run basic tests again.
205 ok(runBaseMenuTests());
208 var sa =
"Command handler(s) should have activated";
209 var sna =
"Command handler(s) should not have activated";
211 // Test middle items.
212 ok(testItem(
"1|1",
"cmd_NewItem1"), sa);
213 ok(testItem(
"1|3|1",
"cmd_NewItem4"), sa);
216 newMenu0.setAttribute(
"hidden",
"true");
217 ok(runBaseMenuTests(), sa); // the base menu should still be unhidden
218 ok(!testItem(
"1|0",
""), sna);
219 ok(!testItem(
"1|1",
""), sna);
220 ok(!testItem(
"1|2",
""), sna);
221 ok(!testItem(
"1|3|0",
""), sna);
222 ok(!testItem(
"1|3|1",
""), sna);
223 ok(!testItem(
"1|3|2",
""), sna);
226 newMenu0.setAttribute(
"hidden",
"false");
227 forceUpdateNativeMenuAt(
"1|3");
228 ok(runBaseMenuTests(), sa);
229 ok(testItem(
"1|0",
"cmd_NewItem0"), sa);
230 ok(testItem(
"1|1",
"cmd_NewItem1"), sa);
231 ok(testItem(
"1|2",
"cmd_NewItem2"), sa);
232 ok(testItem(
"1|3|0",
"cmd_NewItem3"), sa);
233 ok(testItem(
"1|3|1",
"cmd_NewItem4"), sa);
234 ok(testItem(
"1|3|2",
"cmd_NewItem5"), sa);
237 newMenuItem1.setAttribute(
"hidden",
"true");
238 newMenuItem4.setAttribute(
"hidden",
"true");
239 forceUpdateNativeMenuAt(
"1|2");
240 ok(runBaseMenuTests(), sa);
241 ok(testItem(
"1|0",
"cmd_NewItem0"), sa);
242 ok(testItem(
"1|1",
"cmd_NewItem2"), sa);
243 ok(!testItem(
"1|2",
""), sna);
244 ok(testItem(
"1|2|0",
"cmd_NewItem3"), sa);
245 ok(testItem(
"1|2|1",
"cmd_NewItem5"), sa);
246 ok(!testItem(
"1|2|2",
""), sna);
249 newMenuItem1.setAttribute(
"hidden",
"false");
250 newMenuItem4.setAttribute(
"hidden",
"false");
251 forceUpdateNativeMenuAt(
"1|3");
252 ok(runBaseMenuTests(), sa);
253 ok(testItem(
"1|0",
"cmd_NewItem0"), sa);
254 ok(testItem(
"1|1",
"cmd_NewItem1"), sa);
255 ok(testItem(
"1|2",
"cmd_NewItem2"), sa);
256 ok(testItem(
"1|3|0",
"cmd_NewItem3"), sa);
257 ok(testItem(
"1|3|1",
"cmd_NewItem4"), sa);
258 ok(testItem(
"1|3|2",
"cmd_NewItem5"), sa);
260 // At this point in the tests the state of the menus has been returned
261 // to the originally diagramed state.
264 menubarNode.removeChild(newMenu0);
265 ok(runBaseMenuTests(), sa);
266 ok(!testItem(
"1|0",
""), sna);
267 ok(!testItem(
"1|1",
""), sna);
268 ok(!testItem(
"1|2",
""), sna);
269 ok(!testItem(
"1|3|0",
""), sna);
270 ok(!testItem(
"1|3|1",
""), sna);
271 ok(!testItem(
"1|3|2",
""), sna);
272 // return state to original diagramed state
273 menubarNode.appendChild(newMenu0);
275 // Test for bug
447042, make sure that adding a menu node with no children
276 // to the menu bar and then adding another menu node with children works.
277 // Menus with no children don't get their native menu items shown and that
278 // caused internal arrays to get out of sync and an append crashed.
279 var tmpMenu0 = createXULMenu(
"tmpMenu0");
280 menubarNode.removeChild(newMenu0);
281 menubarNode.appendChild(tmpMenu0);
282 menubarNode.appendChild(newMenu0);
283 forceUpdateNativeMenuAt(
"1|3");
284 ok(runBaseMenuTests());
285 ok(testItem(
"1|0",
"cmd_NewItem0"), sa);
286 ok(testItem(
"1|1",
"cmd_NewItem1"), sa);
287 ok(testItem(
"1|2",
"cmd_NewItem2"), sa);
288 ok(testItem(
"1|3|0",
"cmd_NewItem3"), sa);
289 ok(testItem(
"1|3|1",
"cmd_NewItem4"), sa);
290 ok(testItem(
"1|3|2",
"cmd_NewItem5"), sa);
291 // return state to original diagramed state
292 menubarNode.removeChild(tmpMenu0);
295 // This test is basically a crash test for bug
433858.
296 newMenuItem1.setAttribute(
"hidden",
"true");
297 newMenuItem2.setAttribute(
"hidden",
"true");
298 newMenu1.setAttribute(
"hidden",
"true");
299 forceUpdateNativeMenuAt(
"1");
300 newMenuItem1.setAttribute(
"hidden",
"false");
301 newMenuItem2.setAttribute(
"hidden",
"false");
302 newMenu1.setAttribute(
"hidden",
"false");
303 forceUpdateNativeMenuAt(
"1");
308 setTimeout(_delayedOnLoad,
1000);