head.js (4099B)
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 /* exported CommonUtils, testChildAtPoint, Layout, hitTest, testOffsetAtPoint */ 8 9 // Load the shared-head file first. 10 Services.scriptloader.loadSubScript( 11 "chrome://mochitests/content/browser/accessible/tests/browser/shared-head.js", 12 this 13 ); 14 15 /* import-globals-from ../../mochitest/role.js */ 16 17 // Loading and common.js from accessible/tests/mochitest/ for all tests, as 18 // well as promisified-events.js and role.js. 19 loadScripts( 20 { name: "common.js", dir: MOCHITESTS_DIR }, 21 { name: "promisified-events.js", dir: MOCHITESTS_DIR }, 22 { name: "role.js", dir: MOCHITESTS_DIR } 23 ); 24 25 const { CommonUtils } = ChromeUtils.importESModule( 26 "chrome://mochitests/content/browser/accessible/tests/browser/Common.sys.mjs" 27 ); 28 29 const { Layout } = ChromeUtils.importESModule( 30 "chrome://mochitests/content/browser/accessible/tests/browser/Layout.sys.mjs" 31 ); 32 33 function getChildAtPoint(container, x, y, findDeepestChild) { 34 try { 35 return findDeepestChild 36 ? container.getDeepestChildAtPoint(x, y) 37 : container.getChildAtPoint(x, y); 38 } catch (e) { 39 // Failed to get child at point. 40 } 41 info("could not get child at point"); 42 return null; 43 } 44 45 async function testChildAtPoint(dpr, x, y, container, child, grandChild) { 46 const [containerX, containerY] = Layout.getBounds(container, dpr); 47 x += containerX; 48 y += containerY; 49 let actual = null; 50 await untilCacheIs( 51 () => { 52 actual = getChildAtPoint(container, x, y, false); 53 info(`Got direct child match of ${CommonUtils.prettyName(actual)}`); 54 return actual; 55 }, 56 child, 57 `Wrong direct child accessible at the point (${x}, ${y}) of ${CommonUtils.prettyName( 58 container 59 )}, sought ${CommonUtils.prettyName( 60 child 61 )} and got ${CommonUtils.prettyName(actual)}` 62 ); 63 actual = null; 64 await untilCacheIs( 65 () => { 66 actual = getChildAtPoint(container, x, y, true); 67 info(`Got deepest child match of ${CommonUtils.prettyName(actual)}`); 68 return actual; 69 }, 70 grandChild, 71 `Wrong deepest child accessible at the point (${x}, ${y}) of ${CommonUtils.prettyName( 72 container 73 )}, sought ${CommonUtils.prettyName( 74 grandChild 75 )} and got ${CommonUtils.prettyName(actual)}` 76 ); 77 } 78 79 /** 80 * Test if getChildAtPoint returns the given child and grand child accessibles 81 * at coordinates of child accessible (direct and deep hit test). 82 */ 83 async function hitTest(browser, container, child, grandChild) { 84 let domEl = getAccessibleDOMNodeID(child); 85 if (!domEl) { 86 // It is possible this accessible has died, but it is also 87 // possible we are dealing with an accessible constructed 88 // from a pseudoelement, like ::details-content 89 if (child.parent.role == ROLE_DETAILS) { 90 // In the ::details-content case, attempt to use the 91 // inner content to construct our hittesting point. 92 domEl = getAccessibleDOMNodeID(child.firstChild); 93 } 94 } 95 const [childX, childY] = await getContentBoundsForDOMElm(browser, domEl); 96 const x = childX + 1; 97 const y = childY + 1; 98 99 await untilCacheIs( 100 () => getChildAtPoint(container, x, y, false), 101 child, 102 `Wrong direct child accessible at the point (${x}, ${y}) of ${CommonUtils.prettyName( 103 container 104 )}, sought ${CommonUtils.prettyName(child)}` 105 ); 106 await untilCacheIs( 107 () => getChildAtPoint(container, x, y, true), 108 grandChild, 109 `Wrong deepest child accessible at the point (${x}, ${y}) of ${CommonUtils.prettyName( 110 container 111 )}, sought ${CommonUtils.prettyName(grandChild)}` 112 ); 113 } 114 115 /** 116 * Test if getOffsetAtPoint returns the given text offset at given coordinates. 117 */ 118 async function testOffsetAtPoint(hyperText, x, y, coordType, expectedOffset) { 119 await untilCacheIs( 120 () => hyperText.getOffsetAtPoint(x, y, coordType), 121 expectedOffset, 122 `Wrong offset at given point (${x}, ${y}) for ${prettyName(hyperText)}` 123 ); 124 }