|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 "use strict"; |
|
5 |
|
6 const URL = ROOT + "browser_scrollPositions_sample.html"; |
|
7 const URL_FRAMESET = ROOT + "browser_scrollPositions_sample_frameset.html"; |
|
8 |
|
9 // Randomized set of scroll positions we will use in this test. |
|
10 const SCROLL_X = Math.round(100 * (1 + Math.random())); |
|
11 const SCROLL_Y = Math.round(200 * (1 + Math.random())); |
|
12 const SCROLL_STR = SCROLL_X + "," + SCROLL_Y; |
|
13 |
|
14 const SCROLL2_X = Math.round(300 * (1 + Math.random())); |
|
15 const SCROLL2_Y = Math.round(400 * (1 + Math.random())); |
|
16 const SCROLL2_STR = SCROLL2_X + "," + SCROLL2_Y; |
|
17 |
|
18 /** |
|
19 * This test ensures that we properly serialize and restore scroll positions |
|
20 * for an average page without any frames. |
|
21 */ |
|
22 add_task(function test_scroll() { |
|
23 let tab = gBrowser.addTab(URL); |
|
24 let browser = tab.linkedBrowser; |
|
25 yield promiseBrowserLoaded(browser); |
|
26 |
|
27 // Scroll down a little. |
|
28 yield sendMessage(browser, "ss-test:setScrollPosition", {x: SCROLL_X, y: SCROLL_Y}); |
|
29 checkScroll(tab, {scroll: SCROLL_STR}, "scroll is fine"); |
|
30 |
|
31 // Duplicate and check that the scroll position is restored. |
|
32 let tab2 = ss.duplicateTab(window, tab); |
|
33 let browser2 = tab2.linkedBrowser; |
|
34 yield promiseTabRestored(tab2); |
|
35 |
|
36 let scroll = yield sendMessage(browser2, "ss-test:getScrollPosition"); |
|
37 is(JSON.stringify(scroll), JSON.stringify({x: SCROLL_X, y: SCROLL_Y}), |
|
38 "scroll position has been duplicated correctly"); |
|
39 |
|
40 // Check that reloading retains the scroll positions. |
|
41 browser2.reload(); |
|
42 yield promiseBrowserLoaded(browser2); |
|
43 checkScroll(tab2, {scroll: SCROLL_STR}, "reloading retains scroll positions"); |
|
44 |
|
45 // Check that a force-reload resets scroll positions. |
|
46 browser2.reloadWithFlags(Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE); |
|
47 yield promiseBrowserLoaded(browser2); |
|
48 checkScroll(tab2, null, "force-reload resets scroll positions"); |
|
49 |
|
50 // Scroll back to the top and check that the position has been reset. We |
|
51 // expect the scroll position to be "null" here because there is no data to |
|
52 // be stored if the frame is in its default scroll position. |
|
53 yield sendMessage(browser, "ss-test:setScrollPosition", {x: 0, y: 0}); |
|
54 checkScroll(tab, null, "no scroll stored"); |
|
55 |
|
56 // Cleanup. |
|
57 gBrowser.removeTab(tab); |
|
58 gBrowser.removeTab(tab2); |
|
59 }); |
|
60 |
|
61 /** |
|
62 * This tests ensures that we properly serialize and restore scroll positions |
|
63 * for multiple frames of pages with framesets. |
|
64 */ |
|
65 add_task(function test_scroll_nested() { |
|
66 let tab = gBrowser.addTab(URL_FRAMESET); |
|
67 let browser = tab.linkedBrowser; |
|
68 yield promiseBrowserLoaded(browser); |
|
69 |
|
70 // Scroll the first child frame down a little. |
|
71 yield sendMessage(browser, "ss-test:setScrollPosition", {x: SCROLL_X, y: SCROLL_Y, frame: 0}); |
|
72 checkScroll(tab, {children: [{scroll: SCROLL_STR}]}, "scroll is fine"); |
|
73 |
|
74 // Scroll the second child frame down a little. |
|
75 yield sendMessage(browser, "ss-test:setScrollPosition", {x: SCROLL2_X, y: SCROLL2_Y, frame: 1}); |
|
76 checkScroll(tab, {children: [{scroll: SCROLL_STR}, {scroll: SCROLL2_STR}]}, "scroll is fine"); |
|
77 |
|
78 // Duplicate and check that the scroll position is restored. |
|
79 let tab2 = ss.duplicateTab(window, tab); |
|
80 let browser2 = tab2.linkedBrowser; |
|
81 yield promiseTabRestored(tab2); |
|
82 |
|
83 let scroll = yield sendMessage(browser2, "ss-test:getScrollPosition", {frame: 0}); |
|
84 is(JSON.stringify(scroll), JSON.stringify({x: SCROLL_X, y: SCROLL_Y}), |
|
85 "scroll position #1 has been duplicated correctly"); |
|
86 |
|
87 scroll = yield sendMessage(browser2, "ss-test:getScrollPosition", {frame: 1}); |
|
88 is(JSON.stringify(scroll), JSON.stringify({x: SCROLL2_X, y: SCROLL2_Y}), |
|
89 "scroll position #2 has been duplicated correctly"); |
|
90 |
|
91 // Check that resetting one frame's scroll position removes it from the |
|
92 // serialized value. |
|
93 yield sendMessage(browser, "ss-test:setScrollPosition", {x: 0, y: 0, frame: 0}); |
|
94 checkScroll(tab, {children: [null, {scroll: SCROLL2_STR}]}, "scroll is fine"); |
|
95 |
|
96 // Check the resetting all frames' scroll positions nulls the stored value. |
|
97 yield sendMessage(browser, "ss-test:setScrollPosition", {x: 0, y: 0, frame: 1}); |
|
98 checkScroll(tab, null, "no scroll stored"); |
|
99 |
|
100 // Cleanup. |
|
101 gBrowser.removeTab(tab); |
|
102 gBrowser.removeTab(tab2); |
|
103 }); |
|
104 |
|
105 /** |
|
106 * This test ensures that by moving scroll positions out of tabData.entries[] |
|
107 * we still support the old scroll data format stored per shistory entry. |
|
108 */ |
|
109 add_task(function test_scroll_old_format() { |
|
110 const TAB_STATE = { entries: [{url: URL, scroll: SCROLL_STR}] }; |
|
111 |
|
112 // Add a blank tab. |
|
113 let tab = gBrowser.addTab("about:blank"); |
|
114 let browser = tab.linkedBrowser; |
|
115 yield promiseBrowserLoaded(browser); |
|
116 |
|
117 // Apply the tab state with the old format. |
|
118 ss.setTabState(tab, JSON.stringify(TAB_STATE)); |
|
119 yield promiseTabRestored(tab); |
|
120 |
|
121 // Check that the scroll positions has been applied. |
|
122 let scroll = yield sendMessage(browser, "ss-test:getScrollPosition"); |
|
123 is(JSON.stringify(scroll), JSON.stringify({x: SCROLL_X, y: SCROLL_Y}), |
|
124 "scroll position has been restored correctly"); |
|
125 |
|
126 // Cleanup. |
|
127 gBrowser.removeTab(tab); |
|
128 }); |
|
129 |
|
130 function checkScroll(tab, expected, msg) { |
|
131 let browser = tab.linkedBrowser; |
|
132 SyncHandlers.get(browser).flush(); |
|
133 |
|
134 let scroll = JSON.parse(ss.getTabState(tab)).scroll || null; |
|
135 is(JSON.stringify(scroll), JSON.stringify(expected), msg); |
|
136 } |