browser/base/content/test/general/contextmenu_common.js

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 var lastElement;
michael@0 2
michael@0 3 function openContextMenuFor(element, shiftkey, waitForSpellCheck) {
michael@0 4 // Context menu should be closed before we open it again.
michael@0 5 is(SpecialPowers.wrap(contextMenu).state, "closed", "checking if popup is closed");
michael@0 6
michael@0 7 if (lastElement)
michael@0 8 lastElement.blur();
michael@0 9 element.focus();
michael@0 10
michael@0 11 // Some elements need time to focus and spellcheck before any tests are
michael@0 12 // run on them.
michael@0 13 function actuallyOpenContextMenuFor() {
michael@0 14 lastElement = element;
michael@0 15 var eventDetails = { type : "contextmenu", button : 2, shiftKey : shiftkey };
michael@0 16 synthesizeMouse(element, 2, 2, eventDetails, element.ownerDocument.defaultView);
michael@0 17 }
michael@0 18
michael@0 19 if (waitForSpellCheck)
michael@0 20 onSpellCheck(element, actuallyOpenContextMenuFor);
michael@0 21 else
michael@0 22 actuallyOpenContextMenuFor();
michael@0 23 }
michael@0 24
michael@0 25 function closeContextMenu() {
michael@0 26 contextMenu.hidePopup();
michael@0 27 }
michael@0 28
michael@0 29 function getVisibleMenuItems(aMenu, aData) {
michael@0 30 var items = [];
michael@0 31 var accessKeys = {};
michael@0 32 for (var i = 0; i < aMenu.childNodes.length; i++) {
michael@0 33 var item = aMenu.childNodes[i];
michael@0 34 if (item.hidden)
michael@0 35 continue;
michael@0 36
michael@0 37 var key = item.accessKey;
michael@0 38 if (key)
michael@0 39 key = key.toLowerCase();
michael@0 40
michael@0 41 var isGenerated = item.hasAttribute("generateditemid");
michael@0 42
michael@0 43 if (item.nodeName == "menuitem") {
michael@0 44 var isSpellSuggestion = item.className == "spell-suggestion";
michael@0 45 if (isSpellSuggestion) {
michael@0 46 is(item.id, "", "child menuitem #" + i + " is a spelling suggestion");
michael@0 47 } else if (isGenerated) {
michael@0 48 is(item.id, "", "child menuitem #" + i + " is a generated item");
michael@0 49 } else {
michael@0 50 ok(item.id, "child menuitem #" + i + " has an ID");
michael@0 51 }
michael@0 52 var label = item.getAttribute("label");
michael@0 53 ok(label.length, "menuitem " + item.id + " has a label");
michael@0 54 if (isSpellSuggestion) {
michael@0 55 is(key, "", "Spell suggestions shouldn't have an access key");
michael@0 56 items.push("*" + label);
michael@0 57 } else if (isGenerated) {
michael@0 58 items.push("+" + label);
michael@0 59 } else if (item.id.indexOf("spell-check-dictionary-") != 0 &&
michael@0 60 item.id != "spell-no-suggestions" &&
michael@0 61 item.id != "spell-add-dictionaries-main") {
michael@0 62 ok(key, "menuitem " + item.id + " has an access key");
michael@0 63 if (accessKeys[key])
michael@0 64 ok(false, "menuitem " + item.id + " has same accesskey as " + accessKeys[key]);
michael@0 65 else
michael@0 66 accessKeys[key] = item.id;
michael@0 67 }
michael@0 68 if (!isSpellSuggestion && !isGenerated) {
michael@0 69 items.push(item.id);
michael@0 70 }
michael@0 71 if (isGenerated) {
michael@0 72 var p = {};
michael@0 73 p.type = item.getAttribute("type");
michael@0 74 p.icon = item.getAttribute("image");
michael@0 75 p.checked = item.hasAttribute("checked");
michael@0 76 p.disabled = item.hasAttribute("disabled");
michael@0 77 items.push(p);
michael@0 78 } else {
michael@0 79 items.push(!item.disabled);
michael@0 80 }
michael@0 81 } else if (item.nodeName == "menuseparator") {
michael@0 82 ok(true, "--- seperator id is " + item.id);
michael@0 83 items.push("---");
michael@0 84 items.push(null);
michael@0 85 } else if (item.nodeName == "menu") {
michael@0 86 if (isGenerated) {
michael@0 87 item.id = "generated-submenu-" + aData.generatedSubmenuId++;
michael@0 88 }
michael@0 89 ok(item.id, "child menu #" + i + " has an ID");
michael@0 90 if (!isGenerated) {
michael@0 91 ok(key, "menu has an access key");
michael@0 92 if (accessKeys[key])
michael@0 93 ok(false, "menu " + item.id + " has same accesskey as " + accessKeys[key]);
michael@0 94 else
michael@0 95 accessKeys[key] = item.id;
michael@0 96 }
michael@0 97 items.push(item.id);
michael@0 98 items.push(!item.disabled);
michael@0 99 // Add a dummy item to that the indexes in checkMenu are the same
michael@0 100 // for expectedItems and actualItems.
michael@0 101 items.push([]);
michael@0 102 items.push(null);
michael@0 103 } else {
michael@0 104 ok(false, "child #" + i + " of menu ID " + aMenu.id +
michael@0 105 " has an unknown type (" + item.nodeName + ")");
michael@0 106 }
michael@0 107 }
michael@0 108 return items;
michael@0 109 }
michael@0 110
michael@0 111 function checkContextMenu(expectedItems) {
michael@0 112 is(contextMenu.state, "open", "checking if popup is open");
michael@0 113 var data = { generatedSubmenuId: 1 };
michael@0 114 checkMenu(contextMenu, expectedItems, data);
michael@0 115 }
michael@0 116
michael@0 117 /*
michael@0 118 * checkMenu - checks to see if the specified <menupopup> contains the
michael@0 119 * expected items and state.
michael@0 120 * expectedItems is a array of (1) item IDs and (2) a boolean specifying if
michael@0 121 * the item is enabled or not (or null to ignore it). Submenus can be checked
michael@0 122 * by providing a nested array entry after the expected <menu> ID.
michael@0 123 * For example: ["blah", true, // item enabled
michael@0 124 * "submenu", null, // submenu
michael@0 125 * ["sub1", true, // submenu contents
michael@0 126 * "sub2", false], null, // submenu contents
michael@0 127 * "lol", false] // item disabled
michael@0 128 *
michael@0 129 */
michael@0 130 function checkMenu(menu, expectedItems, data) {
michael@0 131 var actualItems = getVisibleMenuItems(menu, data);
michael@0 132 //ok(false, "Items are: " + actualItems);
michael@0 133 for (var i = 0; i < expectedItems.length; i+=2) {
michael@0 134 var actualItem = actualItems[i];
michael@0 135 var actualEnabled = actualItems[i + 1];
michael@0 136 var expectedItem = expectedItems[i];
michael@0 137 var expectedEnabled = expectedItems[i + 1];
michael@0 138 if (expectedItem instanceof Array) {
michael@0 139 ok(true, "Checking submenu...");
michael@0 140 var menuID = expectedItems[i - 2]; // The last item was the menu ID.
michael@0 141 var submenu = menu.getElementsByAttribute("id", menuID)[0];
michael@0 142 ok(submenu, "got a submenu element of id='" + menuID + "'");
michael@0 143 if (submenu) {
michael@0 144 is(submenu.nodeName, "menu", "submenu element of id='" + menuID +
michael@0 145 "' has expected nodeName");
michael@0 146 checkMenu(submenu.menupopup, expectedItem, data);
michael@0 147 }
michael@0 148 } else {
michael@0 149 is(actualItem, expectedItem,
michael@0 150 "checking item #" + i/2 + " (" + expectedItem + ") name");
michael@0 151
michael@0 152 if (typeof expectedEnabled == "object" && expectedEnabled != null ||
michael@0 153 typeof actualEnabled == "object" && actualEnabled != null) {
michael@0 154
michael@0 155 ok(!(actualEnabled == null), "actualEnabled is not null");
michael@0 156 ok(!(expectedEnabled == null), "expectedEnabled is not null");
michael@0 157 is(typeof actualEnabled, typeof expectedEnabled, "checking types");
michael@0 158
michael@0 159 if (typeof actualEnabled != typeof expectedEnabled ||
michael@0 160 actualEnabled == null || expectedEnabled == null)
michael@0 161 continue;
michael@0 162
michael@0 163 is(actualEnabled.type, expectedEnabled.type,
michael@0 164 "checking item #" + i/2 + " (" + expectedItem + ") type attr value");
michael@0 165 var icon = actualEnabled.icon;
michael@0 166 if (icon) {
michael@0 167 var tmp = "";
michael@0 168 var j = icon.length - 1;
michael@0 169 while (j && icon[j] != "/") {
michael@0 170 tmp = icon[j--] + tmp;
michael@0 171 }
michael@0 172 icon = tmp;
michael@0 173 }
michael@0 174 is(icon, expectedEnabled.icon,
michael@0 175 "checking item #" + i/2 + " (" + expectedItem + ") icon attr value");
michael@0 176 is(actualEnabled.checked, expectedEnabled.checked,
michael@0 177 "checking item #" + i/2 + " (" + expectedItem + ") has checked attr");
michael@0 178 is(actualEnabled.disabled, expectedEnabled.disabled,
michael@0 179 "checking item #" + i/2 + " (" + expectedItem + ") has disabled attr");
michael@0 180 } else if (expectedEnabled != null)
michael@0 181 is(actualEnabled, expectedEnabled,
michael@0 182 "checking item #" + i/2 + " (" + expectedItem + ") enabled state");
michael@0 183 }
michael@0 184 }
michael@0 185 // Could find unexpected extra items at the end...
michael@0 186 is(actualItems.length, expectedItems.length, "checking expected number of menu entries");
michael@0 187 }

mercurial