michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Tests that scrollIntoViewIfNeeded works properly. michael@0: michael@0: let imported = {}; michael@0: Components.utils.import("resource://gre/modules/devtools/LayoutHelpers.jsm", michael@0: imported); michael@0: registerCleanupFunction(function () { michael@0: imported = {}; michael@0: }); michael@0: michael@0: let LayoutHelpers = imported.LayoutHelpers; michael@0: michael@0: const TEST_URI = "http://example.com/browser/browser/devtools/shared/test/browser_layoutHelpers.html"; michael@0: michael@0: function test() { michael@0: addTab(TEST_URI, function(browser, tab) { michael@0: info("Starting browser_layoutHelpers.js"); michael@0: let doc = browser.contentDocument; michael@0: runTest(doc.defaultView, doc.getElementById('some')); michael@0: gBrowser.removeCurrentTab(); michael@0: finish(); michael@0: }); michael@0: } michael@0: michael@0: function runTest(win, some) { michael@0: let lh = new LayoutHelpers(win); michael@0: michael@0: some.style.top = win.innerHeight + 'px'; michael@0: some.style.left = win.innerWidth + 'px'; michael@0: // The tests start with a black 2x2 pixels square below bottom right. michael@0: // Do not resize the window during the tests. michael@0: michael@0: win.scroll(win.innerWidth / 2, win.innerHeight + 2); // Above the viewport. michael@0: lh.scrollIntoViewIfNeeded(some); michael@0: is(win.scrollY, Math.floor(win.innerHeight / 2) + 1, michael@0: 'Element completely hidden above should appear centered.'); michael@0: michael@0: win.scroll(win.innerWidth / 2, win.innerHeight + 1); // On the top edge. michael@0: lh.scrollIntoViewIfNeeded(some); michael@0: is(win.scrollY, win.innerHeight, michael@0: 'Element partially visible above should appear above.'); michael@0: michael@0: win.scroll(win.innerWidth / 2, 0); // Just below the viewport. michael@0: lh.scrollIntoViewIfNeeded(some); michael@0: is(win.scrollY, Math.floor(win.innerHeight / 2) + 1, michael@0: 'Element completely hidden below should appear centered.'); michael@0: michael@0: win.scroll(win.innerWidth / 2, 1); // On the bottom edge. michael@0: lh.scrollIntoViewIfNeeded(some); michael@0: is(win.scrollY, 2, michael@0: 'Element partially visible below should appear below.'); michael@0: michael@0: michael@0: win.scroll(win.innerWidth / 2, win.innerHeight + 2); // Above the viewport. michael@0: lh.scrollIntoViewIfNeeded(some, false); michael@0: is(win.scrollY, win.innerHeight, michael@0: 'Element completely hidden above should appear above ' + michael@0: 'if parameter is false.'); michael@0: michael@0: win.scroll(win.innerWidth / 2, win.innerHeight + 1); // On the top edge. michael@0: lh.scrollIntoViewIfNeeded(some, false); michael@0: is(win.scrollY, win.innerHeight, michael@0: 'Element partially visible above should appear above ' + michael@0: 'if parameter is false.'); michael@0: michael@0: win.scroll(win.innerWidth / 2, 0); // Below the viewport. michael@0: lh.scrollIntoViewIfNeeded(some, false); michael@0: is(win.scrollY, 2, michael@0: 'Element completely hidden below should appear below ' + michael@0: 'if parameter is false.'); michael@0: michael@0: win.scroll(win.innerWidth / 2, 1); // On the bottom edge. michael@0: lh.scrollIntoViewIfNeeded(some, false); michael@0: is(win.scrollY, 2, michael@0: 'Element partially visible below should appear below ' + michael@0: 'if parameter is false.'); michael@0: michael@0: // The case of iframes. michael@0: win.scroll(0, 0); michael@0: michael@0: let frame = win.document.getElementById('frame'); michael@0: let fwin = frame.contentWindow; michael@0: michael@0: frame.style.top = win.innerHeight + 'px'; michael@0: frame.style.left = win.innerWidth + 'px'; michael@0: michael@0: fwin.addEventListener('load', function frameLoad() { michael@0: let some = fwin.document.getElementById('some'); michael@0: lh.scrollIntoViewIfNeeded(some); michael@0: is(win.scrollX, Math.floor(win.innerWidth / 2) + 20, michael@0: 'Scrolling from an iframe should center the iframe vertically.'); michael@0: is(win.scrollY, Math.floor(win.innerHeight / 2) + 20, michael@0: 'Scrolling from an iframe should center the iframe horizontally.'); michael@0: is(fwin.scrollX, Math.floor(fwin.innerWidth / 2) + 1, michael@0: 'Scrolling from an iframe should center the element vertically.'); michael@0: is(fwin.scrollY, Math.floor(fwin.innerHeight / 2) + 1, michael@0: 'Scrolling from an iframe should center the element horizontally.'); michael@0: }, false); michael@0: }