browser/devtools/shared/test/browser_layoutHelpers.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/shared/test/browser_layoutHelpers.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Tests that scrollIntoViewIfNeeded works properly.
     1.8 +
     1.9 +let imported = {};
    1.10 +Components.utils.import("resource://gre/modules/devtools/LayoutHelpers.jsm",
    1.11 +    imported);
    1.12 +registerCleanupFunction(function () {
    1.13 +  imported = {};
    1.14 +});
    1.15 +
    1.16 +let LayoutHelpers = imported.LayoutHelpers;
    1.17 +
    1.18 +const TEST_URI = "http://example.com/browser/browser/devtools/shared/test/browser_layoutHelpers.html";
    1.19 +
    1.20 +function test() {
    1.21 +  addTab(TEST_URI, function(browser, tab) {
    1.22 +    info("Starting browser_layoutHelpers.js");
    1.23 +    let doc = browser.contentDocument;
    1.24 +    runTest(doc.defaultView, doc.getElementById('some'));
    1.25 +    gBrowser.removeCurrentTab();
    1.26 +    finish();
    1.27 +  });
    1.28 +}
    1.29 +
    1.30 +function runTest(win, some) {
    1.31 +  let lh = new LayoutHelpers(win);
    1.32 +
    1.33 +  some.style.top = win.innerHeight + 'px';
    1.34 +  some.style.left = win.innerWidth + 'px';
    1.35 +  // The tests start with a black 2x2 pixels square below bottom right.
    1.36 +  // Do not resize the window during the tests.
    1.37 +
    1.38 +  win.scroll(win.innerWidth / 2, win.innerHeight + 2);  // Above the viewport.
    1.39 +  lh.scrollIntoViewIfNeeded(some);
    1.40 +  is(win.scrollY, Math.floor(win.innerHeight / 2) + 1,
    1.41 +     'Element completely hidden above should appear centered.');
    1.42 +
    1.43 +  win.scroll(win.innerWidth / 2, win.innerHeight + 1);  // On the top edge.
    1.44 +  lh.scrollIntoViewIfNeeded(some);
    1.45 +  is(win.scrollY, win.innerHeight,
    1.46 +     'Element partially visible above should appear above.');
    1.47 +
    1.48 +  win.scroll(win.innerWidth / 2, 0);  // Just below the viewport.
    1.49 +  lh.scrollIntoViewIfNeeded(some);
    1.50 +  is(win.scrollY, Math.floor(win.innerHeight / 2) + 1,
    1.51 +     'Element completely hidden below should appear centered.');
    1.52 +
    1.53 +  win.scroll(win.innerWidth / 2, 1);  // On the bottom edge.
    1.54 +  lh.scrollIntoViewIfNeeded(some);
    1.55 +  is(win.scrollY, 2,
    1.56 +     'Element partially visible below should appear below.');
    1.57 +
    1.58 +
    1.59 +  win.scroll(win.innerWidth / 2, win.innerHeight + 2);  // Above the viewport.
    1.60 +  lh.scrollIntoViewIfNeeded(some, false);
    1.61 +  is(win.scrollY, win.innerHeight,
    1.62 +     'Element completely hidden above should appear above ' +
    1.63 +     'if parameter is false.');
    1.64 +
    1.65 +  win.scroll(win.innerWidth / 2, win.innerHeight + 1);  // On the top edge.
    1.66 +  lh.scrollIntoViewIfNeeded(some, false);
    1.67 +  is(win.scrollY, win.innerHeight,
    1.68 +     'Element partially visible above should appear above ' +
    1.69 +     'if parameter is false.');
    1.70 +
    1.71 +  win.scroll(win.innerWidth / 2, 0);  // Below the viewport.
    1.72 +  lh.scrollIntoViewIfNeeded(some, false);
    1.73 +  is(win.scrollY, 2,
    1.74 +     'Element completely hidden below should appear below ' +
    1.75 +     'if parameter is false.');
    1.76 +
    1.77 +  win.scroll(win.innerWidth / 2, 1);  // On the bottom edge.
    1.78 +  lh.scrollIntoViewIfNeeded(some, false);
    1.79 +  is(win.scrollY, 2,
    1.80 +     'Element partially visible below should appear below ' +
    1.81 +     'if parameter is false.');
    1.82 +
    1.83 +  // The case of iframes.
    1.84 +  win.scroll(0, 0);
    1.85 +
    1.86 +  let frame = win.document.getElementById('frame');
    1.87 +  let fwin = frame.contentWindow;
    1.88 +
    1.89 +  frame.style.top = win.innerHeight + 'px';
    1.90 +  frame.style.left = win.innerWidth + 'px';
    1.91 +
    1.92 +  fwin.addEventListener('load', function frameLoad() {
    1.93 +    let some = fwin.document.getElementById('some');
    1.94 +    lh.scrollIntoViewIfNeeded(some);
    1.95 +    is(win.scrollX, Math.floor(win.innerWidth / 2) + 20,
    1.96 +       'Scrolling from an iframe should center the iframe vertically.');
    1.97 +    is(win.scrollY, Math.floor(win.innerHeight / 2) + 20,
    1.98 +       'Scrolling from an iframe should center the iframe horizontally.');
    1.99 +    is(fwin.scrollX, Math.floor(fwin.innerWidth / 2) + 1,
   1.100 +       'Scrolling from an iframe should center the element vertically.');
   1.101 +    is(fwin.scrollY, Math.floor(fwin.innerHeight / 2) + 1,
   1.102 +       'Scrolling from an iframe should center the element horizontally.');
   1.103 +  }, false);
   1.104 +}

mercurial