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