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