test-common.js (1855B)
1 // Asserts that the anchored element is at the top/bottom/left/right of the 2 // anchor. 3 function assert_fallback_position(anchored, anchor, direction) { 4 let anchoredRect = anchored.getBoundingClientRect(); 5 let anchorRect = anchor.getBoundingClientRect(); 6 let message = `Anchored element should be at the ${direction} of anchor`; 7 switch (direction) { 8 case 'top': 9 assert_equals(anchoredRect.bottom, anchorRect.top, message); 10 return; 11 case 'bottom': 12 assert_equals(anchoredRect.top, anchorRect.bottom, message); 13 return; 14 case 'left': 15 assert_equals(anchoredRect.right, anchorRect.left, message); 16 return; 17 case 'right': 18 assert_equals(anchoredRect.left, anchorRect.right, message); 19 return; 20 default: 21 assert_unreached('unsupported direction'); 22 } 23 } 24 25 async function waitUntilNextAnimationFrame() { 26 return new Promise(resolve => requestAnimationFrame(resolve)); 27 } 28 29 // This function is a thin wrapper around `checkLayout` (from 30 // resources/check-layout-th.js) and simply reads the `CHECK_LAYOUT_DELAY` 31 // variable to optionally add a delay. This global variable is not intended 32 // to be set by other tests; instead, polyfills can set it to give themselves 33 // time to apply changes before proceeding with assertions about the layout. 34 // Tests that call this function and then do additional work after the call 35 // should `await` it to avoid race conditions. 36 window.checkLayoutForAnchorPos = async function(selectorList, callDone = true) { 37 if (window.CHECK_LAYOUT_DELAY) { 38 assert_equals(window.INJECTED_SCRIPT,undefined,'CHECK_LAYOUT_DELAY is only allowed when serving WPT with --injected-script.'); 39 await waitUntilNextAnimationFrame(); 40 await waitUntilNextAnimationFrame(); 41 await waitUntilNextAnimationFrame(); 42 } 43 return window.checkLayout(selectorList, callDone); 44 }