browser/devtools/shared/test/browser_layoutHelpers.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // Tests that scrollIntoViewIfNeeded works properly.
michael@0 5
michael@0 6 let imported = {};
michael@0 7 Components.utils.import("resource://gre/modules/devtools/LayoutHelpers.jsm",
michael@0 8 imported);
michael@0 9 registerCleanupFunction(function () {
michael@0 10 imported = {};
michael@0 11 });
michael@0 12
michael@0 13 let LayoutHelpers = imported.LayoutHelpers;
michael@0 14
michael@0 15 const TEST_URI = "http://example.com/browser/browser/devtools/shared/test/browser_layoutHelpers.html";
michael@0 16
michael@0 17 function test() {
michael@0 18 addTab(TEST_URI, function(browser, tab) {
michael@0 19 info("Starting browser_layoutHelpers.js");
michael@0 20 let doc = browser.contentDocument;
michael@0 21 runTest(doc.defaultView, doc.getElementById('some'));
michael@0 22 gBrowser.removeCurrentTab();
michael@0 23 finish();
michael@0 24 });
michael@0 25 }
michael@0 26
michael@0 27 function runTest(win, some) {
michael@0 28 let lh = new LayoutHelpers(win);
michael@0 29
michael@0 30 some.style.top = win.innerHeight + 'px';
michael@0 31 some.style.left = win.innerWidth + 'px';
michael@0 32 // The tests start with a black 2x2 pixels square below bottom right.
michael@0 33 // Do not resize the window during the tests.
michael@0 34
michael@0 35 win.scroll(win.innerWidth / 2, win.innerHeight + 2); // Above the viewport.
michael@0 36 lh.scrollIntoViewIfNeeded(some);
michael@0 37 is(win.scrollY, Math.floor(win.innerHeight / 2) + 1,
michael@0 38 'Element completely hidden above should appear centered.');
michael@0 39
michael@0 40 win.scroll(win.innerWidth / 2, win.innerHeight + 1); // On the top edge.
michael@0 41 lh.scrollIntoViewIfNeeded(some);
michael@0 42 is(win.scrollY, win.innerHeight,
michael@0 43 'Element partially visible above should appear above.');
michael@0 44
michael@0 45 win.scroll(win.innerWidth / 2, 0); // Just below the viewport.
michael@0 46 lh.scrollIntoViewIfNeeded(some);
michael@0 47 is(win.scrollY, Math.floor(win.innerHeight / 2) + 1,
michael@0 48 'Element completely hidden below should appear centered.');
michael@0 49
michael@0 50 win.scroll(win.innerWidth / 2, 1); // On the bottom edge.
michael@0 51 lh.scrollIntoViewIfNeeded(some);
michael@0 52 is(win.scrollY, 2,
michael@0 53 'Element partially visible below should appear below.');
michael@0 54
michael@0 55
michael@0 56 win.scroll(win.innerWidth / 2, win.innerHeight + 2); // Above the viewport.
michael@0 57 lh.scrollIntoViewIfNeeded(some, false);
michael@0 58 is(win.scrollY, win.innerHeight,
michael@0 59 'Element completely hidden above should appear above ' +
michael@0 60 'if parameter is false.');
michael@0 61
michael@0 62 win.scroll(win.innerWidth / 2, win.innerHeight + 1); // On the top edge.
michael@0 63 lh.scrollIntoViewIfNeeded(some, false);
michael@0 64 is(win.scrollY, win.innerHeight,
michael@0 65 'Element partially visible above should appear above ' +
michael@0 66 'if parameter is false.');
michael@0 67
michael@0 68 win.scroll(win.innerWidth / 2, 0); // Below the viewport.
michael@0 69 lh.scrollIntoViewIfNeeded(some, false);
michael@0 70 is(win.scrollY, 2,
michael@0 71 'Element completely hidden below should appear below ' +
michael@0 72 'if parameter is false.');
michael@0 73
michael@0 74 win.scroll(win.innerWidth / 2, 1); // On the bottom edge.
michael@0 75 lh.scrollIntoViewIfNeeded(some, false);
michael@0 76 is(win.scrollY, 2,
michael@0 77 'Element partially visible below should appear below ' +
michael@0 78 'if parameter is false.');
michael@0 79
michael@0 80 // The case of iframes.
michael@0 81 win.scroll(0, 0);
michael@0 82
michael@0 83 let frame = win.document.getElementById('frame');
michael@0 84 let fwin = frame.contentWindow;
michael@0 85
michael@0 86 frame.style.top = win.innerHeight + 'px';
michael@0 87 frame.style.left = win.innerWidth + 'px';
michael@0 88
michael@0 89 fwin.addEventListener('load', function frameLoad() {
michael@0 90 let some = fwin.document.getElementById('some');
michael@0 91 lh.scrollIntoViewIfNeeded(some);
michael@0 92 is(win.scrollX, Math.floor(win.innerWidth / 2) + 20,
michael@0 93 'Scrolling from an iframe should center the iframe vertically.');
michael@0 94 is(win.scrollY, Math.floor(win.innerHeight / 2) + 20,
michael@0 95 'Scrolling from an iframe should center the iframe horizontally.');
michael@0 96 is(fwin.scrollX, Math.floor(fwin.innerWidth / 2) + 1,
michael@0 97 'Scrolling from an iframe should center the element vertically.');
michael@0 98 is(fwin.scrollY, Math.floor(fwin.innerHeight / 2) + 1,
michael@0 99 'Scrolling from an iframe should center the element horizontally.');
michael@0 100 }, false);
michael@0 101 }

mercurial