browser_layoutHelpers.js (3840B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Tests that scrollIntoViewIfNeeded works properly. 7 const { scrollIntoViewIfNeeded } = ChromeUtils.importESModule( 8 "resource://devtools/client/shared/scroll.mjs" 9 ); 10 11 const TEST_URI = CHROME_URL_ROOT + "doc_layoutHelpers.html"; 12 13 add_task(async function () { 14 const { host, win } = await createHost("bottom", TEST_URI); 15 await runTest(win); 16 host.destroy(); 17 }); 18 19 async function runTest(win) { 20 const some = win.document.getElementById("some"); 21 22 some.style.top = win.innerHeight + "px"; 23 some.style.left = win.innerWidth + "px"; 24 // The tests start with a black 2x2 pixels square below bottom right. 25 // Do not resize the window during the tests. 26 27 const xPos = Math.floor(win.innerWidth / 2); 28 // Above the viewport. 29 win.scroll(xPos, win.innerHeight + 2); 30 scrollIntoViewIfNeeded(some); 31 is( 32 win.scrollY, 33 Math.floor(win.innerHeight / 2) + 1, 34 "Element completely hidden above should appear centered." 35 ); 36 is(win.scrollX, xPos, "scrollX position has not changed."); 37 38 // On the top edge. 39 win.scroll(win.innerWidth / 2, win.innerHeight + 1); 40 scrollIntoViewIfNeeded(some); 41 is( 42 win.scrollY, 43 win.innerHeight, 44 "Element partially visible above should appear above." 45 ); 46 is(win.scrollX, xPos, "scrollX position has not changed."); 47 48 // Just below the viewport. 49 win.scroll(win.innerWidth / 2, 0); 50 scrollIntoViewIfNeeded(some); 51 is( 52 win.scrollY, 53 Math.floor(win.innerHeight / 2) + 1, 54 "Element completely hidden below should appear centered." 55 ); 56 is(win.scrollX, xPos, "scrollX position has not changed."); 57 58 // On the bottom edge. 59 win.scroll(win.innerWidth / 2, 1); 60 scrollIntoViewIfNeeded(some); 61 is(win.scrollY, 2, "Element partially visible below should appear below."); 62 is(win.scrollX, xPos, "scrollX position has not changed."); 63 64 // Above the viewport. 65 win.scroll(win.innerWidth / 2, win.innerHeight + 2); 66 scrollIntoViewIfNeeded(some, false); 67 is( 68 win.scrollY, 69 win.innerHeight, 70 "Element completely hidden above should appear above " + 71 "if parameter is false." 72 ); 73 is(win.scrollX, xPos, "scrollX position has not changed."); 74 75 // On the top edge. 76 win.scroll(win.innerWidth / 2, win.innerHeight + 1); 77 scrollIntoViewIfNeeded(some, false); 78 is( 79 win.scrollY, 80 win.innerHeight, 81 "Element partially visible above should appear above " + 82 "if parameter is false." 83 ); 84 is(win.scrollX, xPos, "scrollX position has not changed."); 85 86 // Below the viewport. 87 win.scroll(win.innerWidth / 2, 0); 88 scrollIntoViewIfNeeded(some, false); 89 is( 90 win.scrollY, 91 2, 92 "Element completely hidden below should appear below " + 93 "if parameter is false." 94 ); 95 is(win.scrollX, xPos, "scrollX position has not changed."); 96 97 // On the bottom edge. 98 win.scroll(win.innerWidth / 2, 1); 99 scrollIntoViewIfNeeded(some, false); 100 is( 101 win.scrollY, 102 2, 103 "Element partially visible below should appear below " + 104 "if parameter is false." 105 ); 106 is(win.scrollX, xPos, "scrollX position has not changed."); 107 108 // Check smooth flag (scroll goes below the viewport) 109 await pushPref("ui.prefersReducedMotion", 0); 110 111 info("Checking smooth flag"); 112 is( 113 win.matchMedia("(prefers-reduced-motion)").matches, 114 false, 115 "Reduced motion is disabled" 116 ); 117 118 const other = win.document.getElementById("other"); 119 other.style.top = win.innerHeight + "px"; 120 other.style.left = win.innerWidth + "px"; 121 win.scroll(0, 0); 122 123 scrollIntoViewIfNeeded(other, false, true); 124 Assert.less( 125 win.scrollY, 126 other.clientHeight, 127 "Window has not instantly scrolled to the final position" 128 ); 129 await waitUntil(() => win.scrollY === other.clientHeight); 130 ok(true, "Window did finish scrolling"); 131 }