4 <title>Tests for browser context menu
</title>
5 <script type=
"text/javascript" src=
"/MochiKit/MochiKit.js"></script>
6 <script type=
"text/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <script type=
"text/javascript" src=
"/tests/SimpleTest/EventUtils.js"></script>
8 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
11 Browser context menu tests.
18 <script class=
"testbody" type=
"text/javascript">
20 /** Test for Login Manager: multiple login autocomplete. **/
22 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
24 const Cc = Components.classes;
25 const Ci = Components.interfaces;
27 function openContextMenuFor(element) {
28 // Context menu should be closed before we open it again.
29 is(contextMenu.state,
"closed",
"checking if popup is closed");
31 // XXX this doesn't work:
32 // var eventDetails = { type :
"contextmenu", button :
2 }
33 // synthesizeMouse(element,
50,
90, eventDetails);
35 // It triggers the popup, but then we fail in nsContextMenu when
36 // initializing: ine
565: this.target.ownerDocument is null
37 // |this.target| was assigned there from |document.popupNode|, but it's a
38 // HTMLDocument instead of the node we supposedly fired the event at.
39 // I think the event's |target| is never being set, and we're hitting the
40 // fallback case in nsXULPopupListener::PreLaunchPopup.
42 // Also interesting is that just firing a mousedown+mouseup doesn't seem
43 // to do anything. Not clear why we'd specifically have to fire a
44 // contextmenu event instead of just a right-click.
46 // This seems to work, as long as we explicitly set the popupNode first.
47 // For frames, the popupNode needs to be set to inside the frame.
48 if (element.localName ==
"IFRAME")
49 chromeWin.document.popupNode = element.contentDocument.body;
51 chromeWin.document.popupNode = element;
52 contextMenu.openPopup(element,
"overlap",
5,
5, true, false);
55 function closeContextMenu() {
56 contextMenu.hidePopup();
59 function getVisibleMenuItems(aMenu) {
62 for (var i =
0; i < aMenu.childNodes.length; i++) {
63 var item = aMenu.childNodes[i];
67 var key = item.accessKey;
69 key = key.toLowerCase();
71 if (item.nodeName ==
"menuitem") {
72 ok(item.id,
"child menuitem #" + i +
" has an ID");
73 ok(key,
"menuitem has an access key");
75 ok(false,
"menuitem " + item.id +
" has same accesskey as " + accessKeys[key]);
77 accessKeys[key] = item.id
79 } else if (item.nodeName ==
"menuseparator") {
80 ok(true,
"--- seperator id is " + item.id);
82 } else if (item.nodeName ==
"menu") {
83 ok(item.id,
"child menu #" + i +
" has an ID");
84 ok(key,
"menu has an access key");
86 ok(false,
"menu " + item.id +
" has same accesskey as " + accessKeys[key]);
88 accessKeys[key] = item.id
90 // Add a dummy item to that the indexes in checkMenu are the same
91 // for expectedItems and actualItems.
94 ok(false,
"child #" + i +
" of menu ID " + aMenu.id +
95 " has an unknown type (" + item.nodeName +
")");
101 function checkContextMenu(expectedItems) {
102 is(contextMenu.state,
"open",
"checking if popup is open");
103 checkMenu(contextMenu, expectedItems);
107 * checkMenu - checks to see if the specified
<menupopup> contains the
108 * expected items, as specified by an array of element IDs. To check the
109 * contents of a submenu, include a nested array after the expected
<menu> ID.
110 * For example: [
"foo, "submenu
", ["sub1
", "sub2
"], "bar
"]
113 function checkMenu(menu, expectedItems) {
114 var actualItems = getVisibleMenuItems(menu);
115 //ok(false, "Items are:
" + actualItems);
116 for (var i = 0; i < expectedItems.length; i++) {
117 if (expectedItems[i] instanceof Array) {
118 ok(true, "Checking submenu...
");
119 var menuID = expectedItems[i - 1]; // The last item was the menu ID.
120 var submenu = menu.getElementsByAttribute("id
", menuID)[0];
121 ok(submenu && submenu.nodeName == "menu
", "got expected submenu element
");
122 checkMenu(submenu.menupopup, expectedItems[i]);
124 is(actualItems[i], expectedItems[i],
125 "checking item #
" + i + " (
" + expectedItems[i] + ")
");
128 // Could find unexpected extra items at the end...
129 is(actualItems.length, expectedItems.length, "checking expected number of menu entries
");
135 * Called by a popupshowing event handler. Each test checks for expected menu
136 * contents, closes the popup, and finally triggers the popup on a new element
137 * (thus kicking off another cycle).
140 function runTest(testNum) {
141 // Seems we need to enable this again, or sendKeyEvent() complaints.
142 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
143 ok(true, "Starting test #
" + testNum);
147 // Invoke context menu for next test.
148 openContextMenuFor(text);
152 // Context menu for plain text
153 checkContextMenu(["context-back
",
158 "context-bookmarkpage
",
162 "context-viewbgimage
",
165 "context-viewsource
",
166 "context-viewinfo
"]);
168 openContextMenuFor(link); // Invoke context menu for next test.
172 // Context menu for text link
173 checkContextMenu(["context-openlink
",
174 "context-openlinkintab
",
176 "context-bookmarklink
",
181 "context-metadata
"]);
183 openContextMenuFor(mailto); // Invoke context menu for next test.
187 // Context menu for text mailto-link
188 checkContextMenu(["context-copyemail
",
190 "context-metadata
"]);
192 openContextMenuFor(input); // Invoke context menu for next test.
196 // Context menu for text input field
197 checkContextMenu(["context-undo
",
206 "spell-check-enabled
"]);
208 openContextMenuFor(img); // Invoke context menu for next test.
212 // Context menu for an image
213 checkContextMenu(["context-viewimage
",
214 "context-copyimage-contents
",
219 "context-setDesktopBackground
",
220 "context-blockimage
",
222 "context-metadata
"]);
224 openContextMenuFor(canvas); // Invoke context menu for next test.
228 // Context menu for a canvas
229 checkContextMenu(["context-viewimage
",
231 "context-bookmarkpage
",
232 "context-selectall
"]);
234 openContextMenuFor(video); // Invoke context menu for next test.
238 // Context menu for a video
239 checkContextMenu(["context-media-play
",
240 "context-media-mute
",
241 "context-media-showcontrols
",
243 "context-copyvideourl
",
246 "context-sendvideo
"]);
248 openContextMenuFor(iframe); // Invoke context menu for next test.
252 // Context menu for an iframe
253 checkContextMenu(["context-back
",
258 "context-bookmarkpage
",
262 "context-viewbgimage
",
266 ["context-showonlythisframe
",
268 "context-openframeintab
",
270 "context-reloadframe
",
272 "context-bookmarkframe
",
275 "context-printframe
",
277 "context-viewframesource
",
278 "context-viewframeinfo
"],
280 "context-viewsource
",
281 "context-viewinfo
"]);
289 * Other things that would be nice to test:
291 * - spelling / misspelled word (in text input?)
292 * - check state of disabled items
293 * - test execution of menu items (maybe as a separate test?)
297 ok(false, "Unexpected invocataion of test #
" + testNum);
307 var subwindow, chromeWin, contextMenu;
308 var text, link, mailto, input, img, canvas, video, iframe;
310 function startTest() {
311 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
312 chromeWin = subwindow
313 .QueryInterface(Ci.nsIInterfaceRequestor)
314 .getInterface(Ci.nsIWebNavigation)
315 .QueryInterface(Ci.nsIDocShellTreeItem)
317 .QueryInterface(Ci.nsIInterfaceRequestor)
318 .getInterface(Ci.nsIDOMWindow)
319 .QueryInterface(Ci.nsIDOMChromeWindow);
320 contextMenu = chromeWin.document.getElementById("contentAreaContextMenu
");
321 ok(contextMenu, "Got context menu XUL
");
323 text = subwindow.document.getElementById("test-text
");
324 link = subwindow.document.getElementById("test-link
");
325 mailto = subwindow.document.getElementById("test-mailto
");
326 input = subwindow.document.getElementById("test-input
");
327 img = subwindow.document.getElementById("test-image
");
328 canvas = subwindow.document.getElementById("test-canvas
");
329 video = subwindow.document.getElementById("test-video
");
330 iframe = subwindow.document.getElementById("test-iframe
");
332 contextMenu.addEventListener("popupshown
", function() { runTest(++testNum); }, false);
336 // We open this in a separate window, because the Mochitests run inside a frame.
337 // The frame causes an extra menu item, and prevents running the test
338 // standalone (ie, clicking the test name in the Mochitest window) to see
339 // success/failure messages.
340 // XXX Disable test to diagnose cause of other test failures.
341 todo(false, "Test disabled
");
342 //var subwindow = window.open("./subtst_contextmenu.html
", "contextmenu-subtext
", "width=
600,height=
700");
343 //subwindow.onload = startTest;
345 //SimpleTest.waitForExplicitFinish();