summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_scrollPositions.js
blob: 094aa11325de7faabc4396917121a049a3fdd8dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const BASE = "http://example.com/browser/browser/components/sessionstore/test/";
const URL2 = BASE + "browser_scrollPositions_sample2.html";
const URL_FRAMESET = BASE + "browser_scrollPositions_sample_frameset.html";

// Randomized set of scroll positions we will use in this test.
const SCROLL_X = Math.round(100 * (1 + Math.random()));
const SCROLL_Y = Math.round(200 * (1 + Math.random()));
const SCROLL_STR = SCROLL_X + "," + SCROLL_Y;

const SCROLL2_X = Math.round(300 * (1 + Math.random()));
const SCROLL2_Y = Math.round(400 * (1 + Math.random()));
const SCROLL2_STR = SCROLL2_X + "," + SCROLL2_Y;

requestLongerTimeout(10);

add_task(test_scroll_nested);

if (gFissionBrowser) {
  addCoopTask("browser_scrollPositions_sample.html", test_scroll, HTTPSROOT);
}
addNonCoopTask("browser_scrollPositions_sample.html", test_scroll, HTTPROOT);
addNonCoopTask("browser_scrollPositions_sample.html", test_scroll, HTTPSROOT);

addCoopTask(
  "browser_scrollPositions_sample.html",
  test_scroll_background_tabs,
  HTTPSROOT
);
addNonCoopTask(
  "browser_scrollPositions_sample.html",
  test_scroll_background_tabs,
  HTTPROOT
);

addNonCoopTask(
  "browser_scrollPositions_sample.html",
  test_scroll_background_tabs,
  HTTPSROOT
);

function getScrollPosition(bc) {
  return SpecialPowers.spawn(bc, [], () => {
    let x = {},
      y = {};
    content.windowUtils.getVisualViewportOffset(x, y);
    return { x: x.value, y: y.value };
  });
}

/**
 * This test ensures that we properly serialize and restore scroll positions
 * for an average page without any frames.
 */
async function test_scroll(aURL) {
  let tab = BrowserTestUtils.addTab(gBrowser, aURL);
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);

  // Scroll down a little.
  await setScrollPosition(browser, SCROLL_X, SCROLL_Y);
  await checkScroll(tab, { scroll: SCROLL_STR }, "scroll is fine");

  // Duplicate and check that the scroll position is restored.
  let tab2 = ss.duplicateTab(window, tab);
  let browser2 = tab2.linkedBrowser;
  await promiseTabRestored(tab2);

  let scroll = await getScrollPosition(browser2);
  is(
    JSON.stringify(scroll),
    JSON.stringify({ x: SCROLL_X, y: SCROLL_Y }),
    "scroll position has been duplicated correctly"
  );

  // Check that reloading retains the scroll positions.
  browser2.reload();
  await promiseBrowserLoaded(browser2);
  await checkScroll(
    tab2,
    { scroll: SCROLL_STR },
    "reloading retains scroll positions"
  );

  // Check that a force-reload resets scroll positions.
  browser2.reloadWithFlags(Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE);
  await promiseBrowserLoaded(browser2);
  await checkScroll(tab2, null, "force-reload resets scroll positions");

  // Scroll back to the top and check that the position has been reset. We
  // expect the scroll position to be "null" here because there is no data to
  // be stored if the frame is in its default scroll position.
  await setScrollPosition(browser, 0, 0);
  await checkScroll(tab, null, "no scroll stored");

  // Cleanup.
  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(tab2);
}

/**
 * This tests ensures that we properly serialize and restore scroll positions
 * for multiple frames of pages with framesets.
 */
async function test_scroll_nested() {
  let tab = BrowserTestUtils.addTab(gBrowser, URL_FRAMESET);
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);

  // Scroll the first child frame down a little.
  await setScrollPosition(
    browser.browsingContext.children[0],
    SCROLL_X,
    SCROLL_Y
  );
  await checkScroll(
    tab,
    { children: [{ scroll: SCROLL_STR }] },
    "scroll is fine"
  );

  // Scroll the second child frame down a little.
  await setScrollPosition(
    browser.browsingContext.children[1],
    SCROLL2_X,
    SCROLL2_Y
  );
  await checkScroll(
    tab,
    { children: [{ scroll: SCROLL_STR }, { scroll: SCROLL2_STR }] },
    "scroll is fine"
  );

  // Duplicate and check that the scroll position is restored.
  let tab2 = ss.duplicateTab(window, tab);
  let browser2 = tab2.linkedBrowser;
  await promiseTabRestored(tab2);

  let scroll = await getScrollPosition(browser2.browsingContext.children[0]);
  is(
    JSON.stringify(scroll),
    JSON.stringify({ x: SCROLL_X, y: SCROLL_Y }),
    "scroll position #1 has been duplicated correctly"
  );

  scroll = await getScrollPosition(browser2.browsingContext.children[1]);
  is(
    JSON.stringify(scroll),
    JSON.stringify({ x: SCROLL2_X, y: SCROLL2_Y }),
    "scroll position #2 has been duplicated correctly"
  );

  // Check that resetting one frame's scroll position removes it from the
  // serialized value.
  await setScrollPosition(browser.browsingContext.children[0], 0, 0);
  await checkScroll(
    tab,
    { children: [null, { scroll: SCROLL2_STR }] },
    "scroll is fine"
  );

  // Check the resetting all frames' scroll positions nulls the stored value.
  await setScrollPosition(browser.browsingContext.children[1], 0, 0);
  await checkScroll(tab, null, "no scroll stored");

  // Cleanup.
  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(tab2);
}

/**
 * Test that scroll positions persist after restoring background tabs in
 * a restored window (bug 1228518).
 * Also test that scroll positions for previous session history entries
 * are preserved as well (bug 1265818).
 */
async function test_scroll_background_tabs(aURL) {
  await pushPrefs(["browser.sessionstore.restore_on_demand", true]);

  let newWin = await BrowserTestUtils.openNewBrowserWindow();
  let tab = BrowserTestUtils.addTab(newWin.gBrowser, aURL);
  let browser = tab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser);

  // Scroll down a little.
  await setScrollPosition(browser, SCROLL_X, SCROLL_Y);
  await checkScroll(
    tab,
    { scroll: SCROLL_STR },
    "scroll on first page is fine"
  );

  // Navigate to a different page and scroll there as well.
  let browser2loaded = BrowserTestUtils.browserLoaded(browser, false, URL2);
  BrowserTestUtils.loadURIString(browser, URL2);
  await browser2loaded;

  // Scroll down a little.
  await setScrollPosition(browser, SCROLL2_X, SCROLL2_Y);
  await checkScroll(
    tab,
    { scroll: SCROLL2_STR },
    "scroll on second page is fine"
  );

  // Close the window
  await BrowserTestUtils.closeWindow(newWin);

  await forceSaveState();

  // Now restore the window
  newWin = ss.undoCloseWindow(0);

  // Make sure to wait for the window to be restored.
  await BrowserTestUtils.waitForEvent(newWin, "SSWindowStateReady");

  is(newWin.gBrowser.tabs.length, 2, "There should be two tabs");

  // The second tab should be the one we loaded aURL at still
  tab = newWin.gBrowser.tabs[1];

  ok(tab.hasAttribute("pending"), "Tab should be pending");
  browser = tab.linkedBrowser;

  // Ensure there are no pending queued messages in the child.
  await TabStateFlusher.flush(browser);

  // Now check to see if the background tab remembers where it
  // should be scrolled to.
  newWin.gBrowser.selectedTab = tab;
  await promiseTabRestored(tab);

  await checkScroll(
    tab,
    { scroll: SCROLL2_STR },
    "scroll is correct for restored tab"
  );

  // Now go back in history and check that the scroll position
  // is restored there as well.
  is(browser.canGoBack, true, "can go back");
  browser.goBack();

  await BrowserTestUtils.browserLoaded(browser);
  await TabStateFlusher.flush(browser);

  await checkScroll(
    tab,
    { scroll: SCROLL_STR },
    "scroll is correct after navigating back within the restored tab"
  );

  await BrowserTestUtils.closeWindow(newWin);
}