browser_test_contextmenu_in_popup.js (3578B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 Services.scriptloader.loadSubScript( 7 "chrome://mochikit/content/tests/SimpleTest/paint_listener.js", 8 this 9 ); 10 11 Services.scriptloader.loadSubScript( 12 new URL("apz_test_utils.js", gTestPath).href, 13 this 14 ); 15 16 Services.scriptloader.loadSubScript( 17 new URL("apz_test_native_event_utils.js", gTestPath).href, 18 this 19 ); 20 21 /* import-globals-from helper_browser_test_utils.js */ 22 // For openSelectPopup. 23 Services.scriptloader.loadSubScript( 24 new URL("helper_browser_test_utils.js", gTestPath).href, 25 this 26 ); 27 28 // Cleanup for paint_listener.js. 29 add_task(() => { 30 registerCleanupFunction(() => { 31 delete window.waitForAllPaintsFlushed; 32 delete window.waitForAllPaints; 33 delete window.promiseAllPaintsDone; 34 }); 35 }); 36 37 add_task(async () => { 38 await SpecialPowers.pushPrefEnv({ 39 set: [ 40 ["apz.popups.enabled", true], 41 ["apz.popups_without_remote.enabled", true], 42 ["apz.max_tap_time", 10000], 43 ["ui.click_hold_context_menus.delay", 0], 44 ], 45 }); 46 47 const navBar = document.getElementById("nav-bar"); 48 49 const anchor = document.createXULElement("toolbarbutton"); 50 anchor.classList.add("toolbarbutton-1", "chromeclass-toolbar-additional"); 51 navBar.appendChild(anchor); 52 53 // Prepare a popup panel with touchstart and click event listeners. 54 const panel = document.createXULElement("panel"); 55 panel.setAttribute("noautohide", true); 56 navBar.appendChild(panel); 57 58 const container = document.createElement("div"); 59 container.style = "width: 100px; height: 100px;"; 60 panel.appendChild(container); 61 62 const contextmenuPromise = new Promise(resolve => { 63 window.addEventListener("contextmenu", e => { 64 e.preventDefault(); 65 resolve(e); 66 }); 67 }); 68 69 registerCleanupFunction(() => { 70 panel.remove(); 71 anchor.remove(); 72 }); 73 74 // Open the popup panel. 75 const popupshownPromise = promiseOneEvent(panel, "popupshown"); 76 panel.openPopup(anchor); 77 await popupshownPromise; 78 79 const panelRect = panel.getBoundingClientRect(); 80 81 // Make sure APZ is ready in the popup. 82 await promiseApzFlushedRepaints(panel); 83 84 // Open the contextmenu by a long press event. 85 await synthesizeNativeTouch( 86 panel, 87 10, 88 10, 89 SpecialPowers.DOMWindowUtils.TOUCH_CONTACT 90 ); 91 92 const isWindows = getPlatform() == "windows"; 93 let contextmenuEvent; 94 if (isWindows) { 95 // On Windows contextmenu opens after the user lifted their finger from the touchscreen. 96 // Wait a frame to give a chance to trigger a long-tap event. 97 await promiseFrame(); 98 await synthesizeNativeTouch( 99 panel, 100 10, 101 10, 102 SpecialPowers.DOMWindowUtils.TOUCH_REMOVE 103 ); 104 contextmenuEvent = await contextmenuPromise; 105 } else { 106 contextmenuEvent = await contextmenuPromise; 107 await synthesizeNativeTouch( 108 panel, 109 10, 110 10, 111 SpecialPowers.DOMWindowUtils.TOUCH_REMOVE 112 ); 113 } 114 115 // The contextmenu event should be inside the popup panel. 116 ok( 117 contextmenuEvent.clientX >= panelRect.x, 118 `${contextmenuEvent.clientX} >= ${panelRect.x}` 119 ); 120 ok( 121 contextmenuEvent.clientX <= panelRect.x + panelRect.width, 122 `${contextmenuEvent.clientX} <= ${panelRect.x} + ${panelRect.width}` 123 ); 124 ok( 125 contextmenuEvent.clientY >= panelRect.y, 126 `${contextmenuEvent.clientY} >= ${panelRect.y}` 127 ); 128 ok( 129 contextmenuEvent.clientY <= panelRect.y + panelRect.height, 130 `${contextmenuEvent.clientY} <= ${panelRect.y} + ${panelRect.height}` 131 ); 132 133 await hideSelectPopup(); 134 });