widget/tests/native_menus_window.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <?xml version="1.0"?>
michael@0 2
michael@0 3 <!-- This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 - License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
michael@0 6
michael@0 7 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
michael@0 8
michael@0 9 <window id="NativeMenuWindow"
michael@0 10 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
michael@0 11 width="300"
michael@0 12 height="300"
michael@0 13 onload="onLoad();"
michael@0 14 title="Native Menu Test">
michael@0 15
michael@0 16 <command id="cmd_FooItem0" oncommand="executedCommandID = 'cmd_FooItem0';"/>
michael@0 17 <command id="cmd_FooItem1" oncommand="executedCommandID = 'cmd_FooItem1';"/>
michael@0 18 <command id="cmd_BarItem0" oncommand="executedCommandID = 'cmd_BarItem0';"/>
michael@0 19 <command id="cmd_BarItem1" oncommand="executedCommandID = 'cmd_BarItem1';"/>
michael@0 20 <command id="cmd_NewItem0" oncommand="executedCommandID = 'cmd_NewItem0';"/>
michael@0 21 <command id="cmd_NewItem1" oncommand="executedCommandID = 'cmd_NewItem1';"/>
michael@0 22 <command id="cmd_NewItem2" oncommand="executedCommandID = 'cmd_NewItem2';"/>
michael@0 23 <command id="cmd_NewItem3" oncommand="executedCommandID = 'cmd_NewItem3';"/>
michael@0 24 <command id="cmd_NewItem4" oncommand="executedCommandID = 'cmd_NewItem4';"/>
michael@0 25 <command id="cmd_NewItem5" oncommand="executedCommandID = 'cmd_NewItem5';"/>
michael@0 26
michael@0 27 <!-- We do not modify any menus or menu items defined here in testing. These
michael@0 28 serve as a baseline structure for us to test after other modifications.
michael@0 29 We add children to the menubar defined here and test by modifying those
michael@0 30 children. -->
michael@0 31 <menubar id="nativemenubar">
michael@0 32 <menu id="foo" label="Foo">
michael@0 33 <menupopup>
michael@0 34 <menuitem label="FooItem0" command="cmd_FooItem0"/>
michael@0 35 <menuitem label="FooItem1" command="cmd_FooItem1"/>
michael@0 36 <menuseparator/>
michael@0 37 <menu label="Bar">
michael@0 38 <menupopup>
michael@0 39 <menuitem label="BarItem0" command="cmd_BarItem0"/>
michael@0 40 <menuitem label="BarItem1" command="cmd_BarItem1"/>
michael@0 41 </menupopup>
michael@0 42 </menu>
michael@0 43 </menupopup>
michael@0 44 </menu>
michael@0 45 </menubar>
michael@0 46
michael@0 47 <script type="application/javascript"><![CDATA[
michael@0 48
michael@0 49 function ok(condition, message) {
michael@0 50 window.opener.wrappedJSObject.SimpleTest.ok(condition, message);
michael@0 51 }
michael@0 52
michael@0 53 function onTestsFinished() {
michael@0 54 window.close();
michael@0 55 window.opener.wrappedJSObject.SimpleTest.finish();
michael@0 56 }
michael@0 57
michael@0 58 // Force a menu to update itself. All of the menus parents will be updated
michael@0 59 // as well. An empty string will force a complete menu system reload.
michael@0 60 function forceUpdateNativeMenuAt(location) {
michael@0 61 var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
michael@0 62 getInterface(Components.interfaces.nsIDOMWindowUtils);
michael@0 63 try {
michael@0 64 utils.forceUpdateNativeMenuAt(location);
michael@0 65 }
michael@0 66 catch (e) {
michael@0 67 dump(e + "\n");
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 var executedCommandID = "";
michael@0 72
michael@0 73 function testItem(location, targetID) {
michael@0 74 var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
michael@0 75 getInterface(Components.interfaces.nsIDOMWindowUtils);
michael@0 76 var correctCommandHandler = false;
michael@0 77 try {
michael@0 78 utils.activateNativeMenuItemAt(location);
michael@0 79 correctCommandHandler = executedCommandID == targetID;
michael@0 80 }
michael@0 81 catch (e) {
michael@0 82 dump(e + "\n");
michael@0 83 }
michael@0 84 finally {
michael@0 85 executedCommandID = "";
michael@0 86 return correctCommandHandler;
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 function createXULMenuPopup() {
michael@0 91 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
michael@0 92 var item = document.createElementNS(XUL_NS, "menupopup");
michael@0 93 return item;
michael@0 94 }
michael@0 95
michael@0 96 function createXULMenu(aLabel) {
michael@0 97 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
michael@0 98 var item = document.createElementNS(XUL_NS, "menu");
michael@0 99 item.setAttribute("label", aLabel);
michael@0 100 return item;
michael@0 101 }
michael@0 102
michael@0 103 function createXULMenuItem(aLabel, aCommandId) {
michael@0 104 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
michael@0 105 var item = document.createElementNS(XUL_NS, "menuitem");
michael@0 106 item.setAttribute("label", aLabel);
michael@0 107 item.setAttribute("command", aCommandId);
michael@0 108 return item;
michael@0 109 }
michael@0 110
michael@0 111 function runBaseMenuTests() {
michael@0 112 forceUpdateNativeMenuAt("0|3");
michael@0 113 return testItem("0|0", "cmd_FooItem0") &&
michael@0 114 testItem("0|1", "cmd_FooItem1") &&
michael@0 115 testItem("0|3|0", "cmd_BarItem0") &&
michael@0 116 testItem("0|3|1", "cmd_BarItem1");
michael@0 117 }
michael@0 118
michael@0 119 function onLoad() {
michael@0 120 var _delayedOnLoad = function() {
michael@0 121 // First let's run the base menu tests.
michael@0 122 ok(runBaseMenuTests());
michael@0 123
michael@0 124 // Set up some nodes that we'll use.
michael@0 125 var menubarNode = document.getElementById("nativemenubar");
michael@0 126 var newMenu0 = createXULMenu("NewMenu0");
michael@0 127 var newMenu1 = createXULMenu("NewMenu1");
michael@0 128 var newMenuPopup0 = createXULMenuPopup();
michael@0 129 var newMenuPopup1 = createXULMenuPopup();
michael@0 130 var newMenuItem0 = createXULMenuItem("NewMenuItem0", "cmd_NewItem0");
michael@0 131 var newMenuItem1 = createXULMenuItem("NewMenuItem1", "cmd_NewItem1");
michael@0 132 var newMenuItem2 = createXULMenuItem("NewMenuItem2", "cmd_NewItem2");
michael@0 133 var newMenuItem3 = createXULMenuItem("NewMenuItem3", "cmd_NewItem3");
michael@0 134 var newMenuItem4 = createXULMenuItem("NewMenuItem4", "cmd_NewItem4");
michael@0 135 var newMenuItem5 = createXULMenuItem("NewMenuItem5", "cmd_NewItem5");
michael@0 136
michael@0 137 // Create another submenu with hierarchy via DOM manipulation.
michael@0 138 // ******************
michael@0 139 // * Foo * NewMenu0 * <- Menu bar
michael@0 140 // ******************
michael@0 141 // ****************
michael@0 142 // * NewMenuItem0 * <- NewMenu0 submenu
michael@0 143 // ****************
michael@0 144 // * NewMenuItem1 *
michael@0 145 // ****************
michael@0 146 // * NewMenuItem2 *
michael@0 147 // *******************************
michael@0 148 // * NewMenu1 > * NewMenuItem3 * <- NewMenu1 submenu
michael@0 149 // *******************************
michael@0 150 // * NewMenuItem4 *
michael@0 151 // ****************
michael@0 152 // * NewMenuItem5 *
michael@0 153 // ****************
michael@0 154 newMenu0.appendChild(newMenuPopup0);
michael@0 155 newMenuPopup0.appendChild(newMenuItem0);
michael@0 156 newMenuPopup0.appendChild(newMenuItem1);
michael@0 157 newMenuPopup0.appendChild(newMenuItem2);
michael@0 158 newMenuPopup0.appendChild(newMenu1);
michael@0 159 newMenu1.appendChild(newMenuPopup1);
michael@0 160 newMenuPopup1.appendChild(newMenuItem3);
michael@0 161 newMenuPopup1.appendChild(newMenuItem4);
michael@0 162 newMenuPopup1.appendChild(newMenuItem5);
michael@0 163 //XXX - we have to append the menu to the top-level of the menu bar
michael@0 164 // only after constructing it. If we append before construction, it is
michael@0 165 // invalid because it has no children and we don't validate it if we add
michael@0 166 // children later.
michael@0 167 menubarNode.appendChild(newMenu0);
michael@0 168 forceUpdateNativeMenuAt("1|3");
michael@0 169 // Run basic tests again.
michael@0 170 ok(runBaseMenuTests());
michael@0 171
michael@0 172 // Error strings.
michael@0 173 var sa = "Command handler(s) should have activated";
michael@0 174 var sna = "Command handler(s) should not have activated";
michael@0 175
michael@0 176 // Test middle items.
michael@0 177 ok(testItem("1|1", "cmd_NewItem1"), sa);
michael@0 178 ok(testItem("1|3|1", "cmd_NewItem4"), sa);
michael@0 179
michael@0 180 // Hide newMenu0.
michael@0 181 newMenu0.setAttribute("hidden", "true");
michael@0 182 ok(runBaseMenuTests(), sa); // the base menu should still be unhidden
michael@0 183 ok(!testItem("1|0", ""), sna);
michael@0 184 ok(!testItem("1|1", ""), sna);
michael@0 185 ok(!testItem("1|2", ""), sna);
michael@0 186 ok(!testItem("1|3|0", ""), sna);
michael@0 187 ok(!testItem("1|3|1", ""), sna);
michael@0 188 ok(!testItem("1|3|2", ""), sna);
michael@0 189
michael@0 190 // Show newMenu0.
michael@0 191 newMenu0.setAttribute("hidden", "false");
michael@0 192 forceUpdateNativeMenuAt("1|3");
michael@0 193 ok(runBaseMenuTests(), sa);
michael@0 194 ok(testItem("1|0", "cmd_NewItem0"), sa);
michael@0 195 ok(testItem("1|1", "cmd_NewItem1"), sa);
michael@0 196 ok(testItem("1|2", "cmd_NewItem2"), sa);
michael@0 197 ok(testItem("1|3|0", "cmd_NewItem3"), sa);
michael@0 198 ok(testItem("1|3|1", "cmd_NewItem4"), sa);
michael@0 199 ok(testItem("1|3|2", "cmd_NewItem5"), sa);
michael@0 200
michael@0 201 // Hide items.
michael@0 202 newMenuItem1.setAttribute("hidden", "true");
michael@0 203 newMenuItem4.setAttribute("hidden", "true");
michael@0 204 forceUpdateNativeMenuAt("1|2");
michael@0 205 ok(runBaseMenuTests(), sa);
michael@0 206 ok(testItem("1|0", "cmd_NewItem0"), sa);
michael@0 207 ok(testItem("1|1", "cmd_NewItem2"), sa);
michael@0 208 ok(!testItem("1|2", ""), sna);
michael@0 209 ok(testItem("1|2|0", "cmd_NewItem3"), sa);
michael@0 210 ok(testItem("1|2|1", "cmd_NewItem5"), sa);
michael@0 211 ok(!testItem("1|2|2", ""), sna);
michael@0 212
michael@0 213 // Show items.
michael@0 214 newMenuItem1.setAttribute("hidden", "false");
michael@0 215 newMenuItem4.setAttribute("hidden", "false");
michael@0 216 forceUpdateNativeMenuAt("1|3");
michael@0 217 ok(runBaseMenuTests(), sa);
michael@0 218 ok(testItem("1|0", "cmd_NewItem0"), sa);
michael@0 219 ok(testItem("1|1", "cmd_NewItem1"), sa);
michael@0 220 ok(testItem("1|2", "cmd_NewItem2"), sa);
michael@0 221 ok(testItem("1|3|0", "cmd_NewItem3"), sa);
michael@0 222 ok(testItem("1|3|1", "cmd_NewItem4"), sa);
michael@0 223 ok(testItem("1|3|2", "cmd_NewItem5"), sa);
michael@0 224
michael@0 225 // At this point in the tests the state of the menus has been returned
michael@0 226 // to the originally diagramed state.
michael@0 227
michael@0 228 // Remove menu.
michael@0 229 menubarNode.removeChild(newMenu0);
michael@0 230 ok(runBaseMenuTests(), sa);
michael@0 231 ok(!testItem("1|0", ""), sna);
michael@0 232 ok(!testItem("1|1", ""), sna);
michael@0 233 ok(!testItem("1|2", ""), sna);
michael@0 234 ok(!testItem("1|3|0", ""), sna);
michael@0 235 ok(!testItem("1|3|1", ""), sna);
michael@0 236 ok(!testItem("1|3|2", ""), sna);
michael@0 237 // return state to original diagramed state
michael@0 238 menubarNode.appendChild(newMenu0);
michael@0 239
michael@0 240 // Test for bug 447042, make sure that adding a menu node with no children
michael@0 241 // to the menu bar and then adding another menu node with children works.
michael@0 242 // Menus with no children don't get their native menu items shown and that
michael@0 243 // caused internal arrays to get out of sync and an append crashed.
michael@0 244 var tmpMenu0 = createXULMenu("tmpMenu0");
michael@0 245 menubarNode.removeChild(newMenu0);
michael@0 246 menubarNode.appendChild(tmpMenu0);
michael@0 247 menubarNode.appendChild(newMenu0);
michael@0 248 forceUpdateNativeMenuAt("1|3");
michael@0 249 ok(runBaseMenuTests());
michael@0 250 ok(testItem("1|0", "cmd_NewItem0"), sa);
michael@0 251 ok(testItem("1|1", "cmd_NewItem1"), sa);
michael@0 252 ok(testItem("1|2", "cmd_NewItem2"), sa);
michael@0 253 ok(testItem("1|3|0", "cmd_NewItem3"), sa);
michael@0 254 ok(testItem("1|3|1", "cmd_NewItem4"), sa);
michael@0 255 ok(testItem("1|3|2", "cmd_NewItem5"), sa);
michael@0 256 // return state to original diagramed state
michael@0 257 menubarNode.removeChild(tmpMenu0);
michael@0 258 delete tmpMenu0;
michael@0 259
michael@0 260 // This test is basically a crash test for bug 433858.
michael@0 261 newMenuItem1.setAttribute("hidden", "true");
michael@0 262 newMenuItem2.setAttribute("hidden", "true");
michael@0 263 newMenu1.setAttribute("hidden", "true");
michael@0 264 forceUpdateNativeMenuAt("1");
michael@0 265 newMenuItem1.setAttribute("hidden", "false");
michael@0 266 newMenuItem2.setAttribute("hidden", "false");
michael@0 267 newMenu1.setAttribute("hidden", "false");
michael@0 268 forceUpdateNativeMenuAt("1");
michael@0 269
michael@0 270 onTestsFinished();
michael@0 271 }
michael@0 272
michael@0 273 setTimeout(_delayedOnLoad, 1000);
michael@0 274 }
michael@0 275
michael@0 276 ]]></script>
michael@0 277 </window>

mercurial