browser_inspector_picker-no-page-events.js (3431B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 // Test that the node picker prevents firing various events to the Web Page. 8 9 const TEST_URL = `data:text/html;charset=utf8,<h1>Pick target</h1><h2>Other element</h2> 10 <script> 11 window.events = {}; 12 function reset() { 13 window.events = { 14 enter: 0, 15 move: 0, 16 over: 0, 17 out: 0, 18 leave: 0, 19 }; 20 }; 21 reset(); 22 23 const h1 = document.querySelector("h1"); 24 h1.addEventListener("mouseenter", () => window.events.enter++, true); 25 h1.addEventListener("mouseover", () => window.events.over++, true); 26 h1.addEventListener("mousemove", () => window.events.move++, true); 27 h1.addEventListener("mouseout", () => window.events.out++, true); 28 h1.addEventListener("mouseleave", () => window.events.leave++, true); 29 </script>`; 30 31 async function triggerPageEvents(shouldTriggerListeners) { 32 await SpecialPowers.spawn( 33 gBrowser.selectedBrowser, 34 [shouldTriggerListeners], 35 async function (triggerListeners) { 36 const h1 = content.document.querySelector("h1"); 37 EventUtils.synthesizeMouse(h1, 1, 1, { type: "mouseover" }, content); 38 EventUtils.synthesizeMouse(h1, 2, 2, { type: "mousemove" }, content); 39 EventUtils.synthesizeMouse(h1, 3, 3, { type: "mousemove" }, content); 40 const h2 = content.document.querySelector("h2"); 41 // Hover the h2 element in order to trigger mouseout and mouseleave events on h1 42 const onH2MouseOver = triggerListeners 43 ? new Promise(r => { 44 h2.addEventListener("mouseover", r); 45 }) 46 : null; 47 EventUtils.synthesizeMouse(h2, 1, 1, { type: "mouseover" }, content); 48 if (triggerListeners) { 49 await onH2MouseOver; 50 } else { 51 await new Promise(r => { 52 content.setTimeout(r, 1000); 53 }); 54 } 55 if (triggerListeners) { 56 is(content.wrappedJSObject.events.enter, 1); 57 is(content.wrappedJSObject.events.over, 1); 58 is(content.wrappedJSObject.events.move, 2); 59 is(content.wrappedJSObject.events.out, 1); 60 is(content.wrappedJSObject.events.leave, 1); 61 } else { 62 is(content.wrappedJSObject.events.enter, 0); 63 is(content.wrappedJSObject.events.over, 0); 64 is(content.wrappedJSObject.events.move, 0); 65 is(content.wrappedJSObject.events.out, 0); 66 is(content.wrappedJSObject.events.leave, 0); 67 } 68 content.wrappedJSObject.reset(); 69 } 70 ); 71 } 72 add_task(async () => { 73 const { inspector, toolbox, highlighterTestFront } = 74 await openInspectorForURL(TEST_URL); 75 76 await triggerPageEvents(true); 77 78 const { waitForHighlighterTypeHidden } = getHighlighterTestHelpers(inspector); 79 80 info( 81 "Start the picker and hover an element to populate the picker hovered node reference" 82 ); 83 await startPicker(toolbox); 84 await hoverElement(inspector, "h1"); 85 ok( 86 await highlighterTestFront.assertHighlightedNode("h1"), 87 "The highlighter is shown on the expected node" 88 ); 89 90 await triggerPageEvents(false); 91 92 info("Hit Escape to cancel picking"); 93 const onHighlighterHidden = waitForHighlighterTypeHidden( 94 inspector.highlighters.TYPES.BOXMODEL 95 ); 96 await stopPickerWithEscapeKey(toolbox); 97 await onHighlighterHidden; 98 });