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