1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
'use strict';
SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.setClipboardPlainTextOnlyPref(false);
browserElementTestHelpers.addPermission();
var audioUrl = 'http://mochi.test:8888/tests/dom/browser-element/mochitest/audio.ogg';
var videoUrl = 'http://mochi.test:8888/tests/dom/browser-element/mochitest/short-video.ogv';
function runTests() {
createIframe(function onIframeLoaded() {
checkEmptyContextMenu();
});
}
function checkEmptyContextMenu() {
sendContextMenuTo('body', function onContextMenu(detail) {
is(detail.contextmenu, null, 'Body context clicks have no context menu');
checkInnerContextMenu();
});
}
function checkInnerContextMenu() {
sendContextMenuTo('#inner-link', function onContextMenu(detail) {
is(detail.systemTargets.length, 1, 'Includes anchor data');
is(detail.contextmenu.items.length, 3, 'Inner clicks trigger correct customized menu');
is(detail.contextmenu.items[0].label, 'foo', 'Customized menu has a "foo" menu item');
is(detail.contextmenu.items[1].label, 'bar', 'Customized menu has a "bar" menu item');
is(detail.contextmenu.items[2].id, 'copy-link', '#inner-link has a copy-link menu item');
is(detail.contextmenu.customized, true, 'Make sure contextmenu has customized items');
var target = detail.systemTargets[0];
is(target.nodeName, 'A', 'Reports correct nodeName');
is(target.data.uri, 'foo.html', 'Reports correct uri');
is(target.data.text, 'Menu 1', 'Reports correct link text');
checkCustomContextMenu();
});
}
function checkCustomContextMenu() {
sendContextMenuTo('#menu1-trigger', function onContextMenu(detail) {
is(detail.contextmenu.items.length, 2, 'trigger custom contextmenu');
checkNestedContextMenu();
});
}
function checkNestedContextMenu() {
sendContextMenuTo('#menu2-trigger', function onContextMenu(detail) {
var innerMenu = detail.contextmenu.items.filter(function(x) {
return x.type === 'menu';
});
is(detail.systemTargets.length, 2, 'Includes two systemTargets');
is(detail.systemTargets[0].nodeName, 'IMG', 'Includes "IMG" node');
is(detail.systemTargets[0].data.uri, 'example.png', 'Img data has the correct uri');
is(detail.systemTargets[1].nodeName, 'A', 'Includes "A" node');
is(detail.systemTargets[1].data.uri, 'bar.html', 'Anchor has the correct uri');
ok(innerMenu.length > 0, 'Menu contains a nested menu');
is(detail.contextmenu.items.length, 4, 'We have correct # of menu items')
is(detail.contextmenu.customized, true, 'Make sure contextmenu has customized items');
is(detail.contextmenu.items[0].label, 'outer', 'Customized menu has an "outer" menu item');
is(detail.contextmenu.items[1].label, 'submenu', 'Customized menu has an "submenu" menu item');
is(detail.contextmenu.items[2].id, 'copy-link', 'Has a copy-link menu item');
is(detail.contextmenu.items[3].id, 'copy-image', 'Has a copy-image menu item');
checkPreviousContextMenuHandler();
});
}
// Finished testing the data passed to the contextmenu handler,
// now we start selecting contextmenu items
function checkPreviousContextMenuHandler() {
// This is previously triggered contextmenu data, since we have
// fired subsequent contextmenus this should not be mistaken
// for a current menuitem
var detail = previousContextMenuDetail;
var previousId = detail.contextmenu.items[0].id;
checkContextMenuCallbackForId(detail, previousId, function onCallbackFired(label) {
is(label, null, 'Callback label should be empty since this handler is old');
checkCurrentContextMenuHandler();
});
}
function checkCurrentContextMenuHandler() {
// This triggers a current menuitem
var detail = currentContextMenuDetail;
var innerMenu = detail.contextmenu.items.filter(function(x) {
return x.type === 'menu';
});
var currentId = innerMenu[0].items[1].id;
checkContextMenuCallbackForId(detail, currentId, function onCallbackFired(label) {
is(label, 'inner 2', 'Callback label should be set correctly');
checkAgainCurrentContextMenuHandler();
});
}
function checkAgainCurrentContextMenuHandler() {
// Once an item it selected, subsequent selections are ignored
var detail = currentContextMenuDetail;
var innerMenu = detail.contextmenu.items.filter(function(x) {
return x.type === 'menu';
});
var currentId = innerMenu[0].items[1].id;
checkContextMenuCallbackForId(detail, currentId, function onCallbackFired(label) {
is(label, null, 'Callback label should be empty since this handler has already been used');
checkCallbackWithPreventDefault();
});
};
// Finished testing callbacks if the embedder calls preventDefault() on the
// mozbrowsercontextmenu event, now we start checking for some cases where the embedder
// does not want to call preventDefault() for some reasons.
function checkCallbackWithPreventDefault() {
sendContextMenuTo('#menu1-trigger', function onContextMenu(detail) {
var id = detail.contextmenu.items[0].id;
checkContextMenuCallbackForId(detail, id, function onCallbackFired(label) {
is(label, 'foo', 'Callback label should be set correctly');
checkCallbackWithoutPreventDefault();
});
});
}
function checkCallbackWithoutPreventDefault() {
sendContextMenuTo('#menu1-trigger', function onContextMenu(detail) {
var id = detail.contextmenu.items[0].id;
checkContextMenuCallbackForId(detail, id, function onCallbackFired(label) {
is(label, null, 'Callback label should be null');
checkImageContextMenu();
});
}, /* ignorePreventDefault */ true);
}
function checkImageContextMenu() {
sendContextMenuTo('#menu3-trigger', function onContextMenu(detail) {
var target = detail.systemTargets[0];
is(target.nodeName, 'IMG', 'Reports correct nodeName');
is(target.data.uri, 'example.png', 'Reports correct uri');
is(detail.contextmenu.items.length, 1, 'Reports correct # of menu items');
is(detail.contextmenu.items[0].id, 'copy-image', 'IMG has a copy-image menu item');
is(detail.contextmenu.customized, false, 'Make sure we do not have customized items');
checkVideoContextMenu();
}, /* ignorePreventDefault */ true);
}
function checkVideoContextMenu() {
sendContextMenuTo('#menu4-trigger', function onContextMenu(detail) {
var target = detail.systemTargets[0];
is(target.nodeName, 'VIDEO', 'Reports correct nodeName');
is(target.data.uri, videoUrl, 'Reports uri correctly in data');
is(target.data.hasVideo, true, 'Video data in video tag does "hasVideo"');
checkAudioContextMenu();
}, /* ignorePreventDefault */ true);
}
function checkAudioContextMenu() {
sendContextMenuTo('#menu6-trigger', function onContextMenu(detail) {
var target = detail.systemTargets[0];
is(target.nodeName, 'AUDIO', 'Reports correct nodeName');
is(target.data.uri, audioUrl, 'Reports uri correctly in data');
checkAudioinVideoContextMenu();
}, /* ignorePreventDefault */ true);
}
function checkAudioinVideoContextMenu() {
sendSrcTo('#menu5-trigger', audioUrl, function onSrcSet() {
sendContextMenuTo('#menu5-trigger', function onContextMenu(detail) {
var target = detail.systemTargets[0];
is(target.nodeName, 'VIDEO', 'Reports correct nodeName');
is(target.data.uri, audioUrl, 'Reports uri correctly in data');
is(target.data.hasVideo, false, 'Audio data in video tag reports no "hasVideo"');
checkFormNoMethod();
}, /* ignorePreventDefault */ true);
});
}
function checkFormNoMethod() {
sendContextMenuTo('#menu7-trigger', function onContextMenu(detail) {
var target = detail.systemTargets[0];
is(target.nodeName, 'INPUT', 'Reports correct nodeName');
is(target.data.method, 'get', 'Reports correct method');
is(target.data.action, 'no_method', 'Reports correct action url');
is(target.data.name, 'input1', 'Reports correct input name');
checkFormGetMethod();
}, /* ignorePreventDefault */ true);
}
function checkFormGetMethod() {
sendContextMenuTo('#menu8-trigger', function onContextMenu(detail) {
var target = detail.systemTargets[0];
is(target.nodeName, 'INPUT', 'Reports correct nodeName');
is(target.data.method, 'get', 'Reports correct method');
is(target.data.action, 'http://example.com/get_method', 'Reports correct action url');
is(target.data.name, 'input2', 'Reports correct input name');
checkFormPostMethod();
}, /* ignorePreventDefault */ true);
}
function checkFormPostMethod() {
sendContextMenuTo('#menu9-trigger', function onContextMenu(detail) {
var target = detail.systemTargets[0];
is(target.nodeName, 'INPUT', 'Reports correct nodeName');
is(target.data.method, 'post', 'Reports correct method');
is(target.data.action, 'post_method', 'Reports correct action url');
is(target.data.name, 'input3', 'Reports correct input name');
SimpleTest.finish();
}, /* ignorePreventDefault */ true);
}
/* Helpers */
var mm = null;
var previousContextMenuDetail = null;
var currentContextMenuDetail = null;
function sendSrcTo(selector, src, callback) {
mm.sendAsyncMessage('setsrc', { 'selector': selector, 'src': src });
mm.addMessageListener('test:srcset', function onSrcSet(msg) {
mm.removeMessageListener('test:srcset', onSrcSet);
callback();
});
}
function sendContextMenuTo(selector, callback, ignorePreventDefault) {
iframe.addEventListener('mozbrowsercontextmenu', function oncontextmenu(e) {
iframe.removeEventListener(e.type, oncontextmenu);
// The embedder should call preventDefault() on the event if it will handle
// it. Not calling preventDefault() means it won't handle the event and
// should not be able to deal with context menu callbacks.
if (ignorePreventDefault !== true) {
e.preventDefault();
}
// Keep a reference to previous/current contextmenu event details.
previousContextMenuDetail = currentContextMenuDetail;
currentContextMenuDetail = e.detail;
setTimeout(function() { callback(e.detail); });
});
mm.sendAsyncMessage('contextmenu', { 'selector': selector });
}
function checkContextMenuCallbackForId(detail, id, callback) {
mm.addMessageListener('test:callbackfired', function onCallbackFired(msg) {
mm.removeMessageListener('test:callbackfired', onCallbackFired);
msg = SpecialPowers.wrap(msg);
setTimeout(function() { callback(msg.data.label); });
});
detail.contextMenuItemSelected(id);
}
var iframe = null;
function createIframe(callback) {
iframe = document.createElement('iframe');
iframe.setAttribute('mozbrowser', 'true');
iframe.src = 'data:text/html,<html>' +
'<body>' +
'<menu type="context" id="menu1" label="firstmenu">' +
'<menuitem label="foo" onclick="window.onContextMenuCallbackFired(event)"></menuitem>' +
'<menuitem label="bar" onclick="window.onContextMenuCallbackFired(event)"></menuitem>' +
'</menu>' +
'<menu type="context" id="menu2" label="secondmenu">' +
'<menuitem label="outer" onclick="window.onContextMenuCallbackFired(event)"></menuitem>' +
'<menu label="submenu">' +
'<menuitem label="inner 1"></menuitem>' +
'<menuitem label="inner 2" onclick="window.onContextMenuCallbackFired(event)"></menuitem>' +
'</menu>' +
'</menu>' +
'<div id="menu1-trigger" contextmenu="menu1"><a id="inner-link" href="foo.html">Menu 1</a></div>' +
'<a href="bar.html" contextmenu="menu2"><img id="menu2-trigger" src="example.png" /></a>' +
'<img id="menu3-trigger" src="example.png" />' +
'<video id="menu4-trigger" src="' + videoUrl + '"></video>' +
'<video id="menu5-trigger" preload="metadata"></video>' +
'<audio id="menu6-trigger" src="' + audioUrl + '"></audio>' +
'<form action="no_method"><input id="menu7-trigger" name="input1"></input></form>' +
'<form action="http://example.com/get_method" method="get"><input id="menu8-trigger" name="input2"></input></form>' +
'<form action="post_method" method="post"><input id="menu9-trigger" name="input3"></input></form>' +
'</body></html>';
document.body.appendChild(iframe);
// The following code will be included in the child
// =========================================================================
function iframeScript() {
addMessageListener('contextmenu', function onContextMenu(msg) {
var document = content.document;
var evt = document.createEvent('HTMLEvents');
evt.initEvent('contextmenu', true, true);
document.querySelector(msg.data.selector).dispatchEvent(evt);
});
addMessageListener('setsrc', function onContextMenu(msg) {
var wrappedTarget = content.document.querySelector(msg.data.selector);
var target = XPCNativeWrapper.unwrap(wrappedTarget);
target.addEventListener('loadedmetadata', function() {
sendAsyncMessage('test:srcset');
});
target.src = msg.data.src;
});
addMessageListener('browser-element-api:call', function onCallback(msg) {
if (msg.data.msg_name != 'fire-ctx-callback')
return;
/* Use setTimeout in order to react *after* the platform */
content.setTimeout(function() {
sendAsyncMessage('test:callbackfired', { label: label });
label = null;
});
});
var label = null;
XPCNativeWrapper.unwrap(content).onContextMenuCallbackFired = function(e) {
label = e.target.getAttribute('label');
};
}
// =========================================================================
iframe.addEventListener('mozbrowserloadend', function onload(e) {
iframe.removeEventListener(e.type, onload);
mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
mm.loadFrameScript('data:,(' + iframeScript.toString() + ')();', false);
// Now we're ready, let's start testing.
callback();
});
}
addEventListener('testready', runTests);
|