Bug 449522 - Context menu for HTML5 <video> elements. r=gavin, ui-r=boriss
[wine-gecko.git] / browser / base / content / test / test_contextmenu.html
blobaf05ff86fde92adb0bd557de407a95e6d232d25b
1 <!DOCTYPE HTML>
2 <html>
3 <head>
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" />
9 </head>
10 <body>
11 Browser context menu tests.
12 <p id="display"></p>
14 <div id="content">
15 </div>
17 <pre id="test">
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;
50 else
51 chromeWin.document.popupNode = element;
52 contextMenu.openPopup(element, "overlap", 5, 5, true, false);
55 function closeContextMenu() {
56 contextMenu.hidePopup();
59 function getVisibleMenuItems(aMenu) {
60 var items = [];
61 var accessKeys = {};
62 for (var i = 0; i < aMenu.childNodes.length; i++) {
63 var item = aMenu.childNodes[i];
64 if (item.hidden)
65 continue;
67 var key = item.accessKey;
68 if (key)
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");
74 if (accessKeys[key])
75 ok(false, "menuitem " + item.id + " has same accesskey as " + accessKeys[key]);
76 else
77 accessKeys[key] = item.id
78 items.push(item.id);
79 } else if (item.nodeName == "menuseparator") {
80 ok(true, "--- seperator id is " + item.id);
81 items.push("---");
82 } else if (item.nodeName == "menu") {
83 ok(item.id, "child menu #" + i + " has an ID");
84 ok(key, "menu has an access key");
85 if (accessKeys[key])
86 ok(false, "menu " + item.id + " has same accesskey as " + accessKeys[key]);
87 else
88 accessKeys[key] = item.id
89 items.push(item.id);
90 // Add a dummy item to that the indexes in checkMenu are the same
91 // for expectedItems and actualItems.
92 items.push([]);
93 } else {
94 ok(false, "child #" + i + " of menu ID " + aMenu.id +
95 " has an unknown type (" + item.nodeName + ")");
98 return items;
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]);
123 } else {
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");
133 * runTest
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);
145 switch(testNum) {
146 case 1:
147 // Invoke context menu for next test.
148 openContextMenuFor(text);
149 break;
151 case 2:
152 // Context menu for plain text
153 checkContextMenu(["context-back",
154 "context-forward",
155 "context-reload",
156 "context-stop",
157 "---",
158 "context-bookmarkpage",
159 "context-savepage",
160 "context-sendpage",
161 "---",
162 "context-viewbgimage",
163 "context-selectall",
164 "---",
165 "context-viewsource",
166 "context-viewinfo"]);
167 closeContextMenu()
168 openContextMenuFor(link); // Invoke context menu for next test.
169 break;
171 case 3:
172 // Context menu for text link
173 checkContextMenu(["context-openlink",
174 "context-openlinkintab",
175 "---",
176 "context-bookmarklink",
177 "context-savelink",
178 "context-sendlink",
179 "context-copylink",
180 "---",
181 "context-metadata"]);
182 closeContextMenu()
183 openContextMenuFor(mailto); // Invoke context menu for next test.
184 break;
186 case 4:
187 // Context menu for text mailto-link
188 checkContextMenu(["context-copyemail",
189 "---",
190 "context-metadata"]);
191 closeContextMenu()
192 openContextMenuFor(input); // Invoke context menu for next test.
193 break;
195 case 5:
196 // Context menu for text input field
197 checkContextMenu(["context-undo",
198 "---",
199 "context-cut",
200 "context-copy",
201 "context-paste",
202 "context-delete",
203 "---",
204 "context-selectall",
205 "---",
206 "spell-check-enabled"]);
207 closeContextMenu()
208 openContextMenuFor(img); // Invoke context menu for next test.
209 break;
211 case 6:
212 // Context menu for an image
213 checkContextMenu(["context-viewimage",
214 "context-copyimage-contents",
215 "context-copyimage",
216 "---",
217 "context-saveimage",
218 "context-sendimage",
219 "context-setDesktopBackground",
220 "context-blockimage",
221 "---",
222 "context-metadata"]);
223 closeContextMenu();
224 openContextMenuFor(canvas); // Invoke context menu for next test.
225 break;
227 case 7:
228 // Context menu for a canvas
229 checkContextMenu(["context-viewimage",
230 "context-saveimage",
231 "context-bookmarkpage",
232 "context-selectall"]);
233 closeContextMenu();
234 openContextMenuFor(video); // Invoke context menu for next test.
235 break;
237 case 8:
238 // Context menu for a video
239 checkContextMenu(["context-media-play",
240 "context-media-mute",
241 "context-media-showcontrols",
242 "---",
243 "context-copyvideourl",
244 "---",
245 "context-savevideo",
246 "context-sendvideo"]);
247 closeContextMenu();
248 openContextMenuFor(iframe); // Invoke context menu for next test.
249 break;
251 case 9:
252 // Context menu for an iframe
253 checkContextMenu(["context-back",
254 "context-forward",
255 "context-reload",
256 "context-stop",
257 "---",
258 "context-bookmarkpage",
259 "context-savepage",
260 "context-sendpage",
261 "---",
262 "context-viewbgimage",
263 "context-selectall",
264 "---",
265 "frame",
266 ["context-showonlythisframe",
267 "context-openframe",
268 "context-openframeintab",
269 "---",
270 "context-reloadframe",
271 "---",
272 "context-bookmarkframe",
273 "context-saveframe",
274 "---",
275 "context-printframe",
276 "---",
277 "context-viewframesource",
278 "context-viewframeinfo"],
279 "---",
280 "context-viewsource",
281 "context-viewinfo"]);
282 closeContextMenu();
284 subwindow.close();
285 SimpleTest.finish();
286 return;
289 * Other things that would be nice to test:
290 * - selected text
291 * - spelling / misspelled word (in text input?)
292 * - check state of disabled items
293 * - test execution of menu items (maybe as a separate test?)
296 default:
297 ok(false, "Unexpected invocataion of test #" + testNum);
298 subwindow.close();
299 SimpleTest.finish();
300 return;
306 var testNum = 1;
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)
316 .rootTreeItem
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);
333 runTest(1);
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 var subwindow = window.open("./subtst_contextmenu.html", "contextmenu-subtext", "width=600,height=700");
341 subwindow.onload = startTest;
343 SimpleTest.waitForExplicitFinish();
344 </script>
345 </pre>
346 </body>
347 </html>