summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_backforward_userinteraction.js
blob: 264299c9025d046c406a4bedc480e94c33acb730 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_PAGE =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "dummy_page.html";
const IFRAME_PAGE =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "dummy_iframe_page.html";

async function assertMenulist(entries, baseURL = TEST_PAGE) {
  // Wait for the session data to be flushed before continuing the test
  await new Promise(resolve =>
    SessionStore.getSessionHistory(gBrowser.selectedTab, resolve)
  );

  let backButton = document.getElementById("back-button");
  let contextMenu = document.getElementById("backForwardMenu");

  info("waiting for the history menu to open");

  let popupShownPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popupshown"
  );
  EventUtils.synthesizeMouseAtCenter(backButton, {
    type: "contextmenu",
    button: 2,
  });
  await popupShownPromise;

  ok(true, "history menu opened");

  let nodes = contextMenu.childNodes;

  is(
    nodes.length,
    entries.length,
    "Has the expected number of contextMenu entries"
  );

  for (let i = 0; i < entries.length; i++) {
    let node = nodes[i];
    is(
      node.getAttribute("uri").replace(/[?|#]/, "!"),
      baseURL + "!entry=" + entries[i],
      "contextMenu node has the correct uri"
    );
  }

  let popupHiddenPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popuphidden"
  );
  contextMenu.hidePopup();
  await popupHiddenPromise;
}

// There are different ways of loading a page, but they should exhibit roughly the same
// back-forward behavior for the purpose of requiring user interaction. Thus, we
// have a utility function that runs the same test with a parameterized method of loading
// new URLs.
async function runTopLevelTest(loadMethod, useHashes = false) {
  let p = useHashes ? "#" : "?";

  // Test with both pref on and off
  for (let requireUserInteraction of [true, false]) {
    Services.prefs.setBoolPref(
      "browser.navigation.requireUserInteraction",
      requireUserInteraction
    );

    let tab = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      TEST_PAGE + p + "entry=0"
    );
    let browser = tab.linkedBrowser;

    assertBackForwardState(false, false);

    await loadMethod(TEST_PAGE + p + "entry=1");

    assertBackForwardState(true, false);
    await assertMenulist([1, 0]);

    await loadMethod(TEST_PAGE + p + "entry=2");

    assertBackForwardState(true, false);
    await assertMenulist(requireUserInteraction ? [2, 0] : [2, 1, 0]);

    await loadMethod(TEST_PAGE + p + "entry=3");

    info("Adding user interaction for entry=3");
    // Add some user interaction to entry 3
    await BrowserTestUtils.synthesizeMouse(
      "body",
      0,
      0,
      {},
      browser.browsingContext,
      true
    );

    assertBackForwardState(true, false);
    await assertMenulist(requireUserInteraction ? [3, 0] : [3, 2, 1, 0]);

    await loadMethod(TEST_PAGE + p + "entry=4");

    assertBackForwardState(true, false);
    await assertMenulist(requireUserInteraction ? [4, 3, 0] : [4, 3, 2, 1, 0]);

    info("Adding user interaction for entry=4");
    // Add some user interaction to entry 4
    await BrowserTestUtils.synthesizeMouse(
      "body",
      0,
      0,
      {},
      browser.browsingContext,
      true
    );

    await loadMethod(TEST_PAGE + p + "entry=5");

    assertBackForwardState(true, false);
    await assertMenulist(
      requireUserInteraction ? [5, 4, 3, 0] : [5, 4, 3, 2, 1, 0]
    );

    await goBack(TEST_PAGE + p + "entry=4");
    await goBack(TEST_PAGE + p + "entry=3");

    if (!requireUserInteraction) {
      await goBack(TEST_PAGE + p + "entry=2");
      await goBack(TEST_PAGE + p + "entry=1");
    }

    assertBackForwardState(true, true);
    await assertMenulist(
      requireUserInteraction ? [5, 4, 3, 0] : [5, 4, 3, 2, 1, 0]
    );

    await goBack(TEST_PAGE + p + "entry=0");

    assertBackForwardState(false, true);

    if (!requireUserInteraction) {
      await goForward(TEST_PAGE + p + "entry=1");
      await goForward(TEST_PAGE + p + "entry=2");
    }

    await goForward(TEST_PAGE + p + "entry=3");

    assertBackForwardState(true, true);
    await assertMenulist(
      requireUserInteraction ? [5, 4, 3, 0] : [5, 4, 3, 2, 1, 0]
    );

    await goForward(TEST_PAGE + p + "entry=4");

    assertBackForwardState(true, true);
    await assertMenulist(
      requireUserInteraction ? [5, 4, 3, 0] : [5, 4, 3, 2, 1, 0]
    );

    await goForward(TEST_PAGE + p + "entry=5");

    assertBackForwardState(true, false);
    await assertMenulist(
      requireUserInteraction ? [5, 4, 3, 0] : [5, 4, 3, 2, 1, 0]
    );

    BrowserTestUtils.removeTab(tab);
  }

  Services.prefs.clearUserPref("browser.navigation.requireUserInteraction");
}

async function runIframeTest(loadMethod) {
  // Test with both pref on and off
  for (let requireUserInteraction of [true, false]) {
    Services.prefs.setBoolPref(
      "browser.navigation.requireUserInteraction",
      requireUserInteraction
    );

    // First test the boring case where we only have one iframe.
    let tab = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      IFRAME_PAGE + "?entry=0"
    );
    let browser = tab.linkedBrowser;

    assertBackForwardState(false, false);

    await loadMethod(TEST_PAGE + "?sub_entry=1", "frame1");

    assertBackForwardState(true, false);
    await assertMenulist([0, 0], IFRAME_PAGE);

    await loadMethod(TEST_PAGE + "?sub_entry=2", "frame1");

    assertBackForwardState(true, false);
    await assertMenulist(
      requireUserInteraction ? [0, 0] : [0, 0, 0],
      IFRAME_PAGE
    );

    let bc = await SpecialPowers.spawn(browser, [], function () {
      return content.document.getElementById("frame1").browsingContext;
    });

    // Add some user interaction to sub entry 2
    await BrowserTestUtils.synthesizeMouse("body", 0, 0, {}, bc, true);

    await loadMethod(TEST_PAGE + "?sub_entry=3", "frame1");

    assertBackForwardState(true, false);
    await assertMenulist(
      requireUserInteraction ? [0, 0, 0] : [0, 0, 0, 0],
      IFRAME_PAGE
    );

    await loadMethod(TEST_PAGE + "?sub_entry=4", "frame1");

    assertBackForwardState(true, false);
    await assertMenulist(
      requireUserInteraction ? [0, 0, 0] : [0, 0, 0, 0, 0],
      IFRAME_PAGE
    );

    if (!requireUserInteraction) {
      await goBack(TEST_PAGE + "?sub_entry=3", true);
    }

    await goBack(TEST_PAGE + "?sub_entry=2", true);

    assertBackForwardState(true, true);
    await assertMenulist(
      requireUserInteraction ? [0, 0, 0] : [0, 0, 0, 0, 0],
      IFRAME_PAGE
    );

    await loadMethod(IFRAME_PAGE + "?entry=1");

    assertBackForwardState(true, false);
    await assertMenulist(
      requireUserInteraction ? [1, 0, 0] : [1, 0, 0, 0],
      IFRAME_PAGE
    );

    BrowserTestUtils.removeTab(tab);

    // Two iframes, now we're talking.
    tab = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      IFRAME_PAGE + "?entry=0"
    );
    browser = tab.linkedBrowser;

    await loadMethod(IFRAME_PAGE + "?entry=1");

    assertBackForwardState(true, false);
    await assertMenulist(requireUserInteraction ? [1, 0] : [1, 0], IFRAME_PAGE);

    // Add some user interaction to frame 1.
    bc = await SpecialPowers.spawn(browser, [], function () {
      return content.document.getElementById("frame1").browsingContext;
    });
    await BrowserTestUtils.synthesizeMouse("body", 0, 0, {}, bc, true);

    // Add some user interaction to frame 2.
    bc = await SpecialPowers.spawn(browser, [], function () {
      return content.document.getElementById("frame2").browsingContext;
    });
    await BrowserTestUtils.synthesizeMouse("body", 0, 0, {}, bc, true);

    // Navigate frame 2.
    await loadMethod(TEST_PAGE + "?sub_entry=1", "frame2");

    assertBackForwardState(true, false);
    await assertMenulist(
      requireUserInteraction ? [1, 1, 0] : [1, 1, 0],
      IFRAME_PAGE
    );

    // Add some user interaction to frame 1, again.
    bc = await SpecialPowers.spawn(browser, [], function () {
      return content.document.getElementById("frame1").browsingContext;
    });
    await BrowserTestUtils.synthesizeMouse("body", 0, 0, {}, bc, true);

    // Navigate frame 2, again.
    await loadMethod(TEST_PAGE + "?sub_entry=2", "frame2");

    assertBackForwardState(true, false);
    await assertMenulist(
      requireUserInteraction ? [1, 1, 1, 0] : [1, 1, 1, 0],
      IFRAME_PAGE
    );

    await goBack(TEST_PAGE + "?sub_entry=1", true);

    assertBackForwardState(true, true);
    await assertMenulist(
      requireUserInteraction ? [1, 1, 1, 0] : [1, 1, 1, 0],
      IFRAME_PAGE
    );

    await goBack(TEST_PAGE + "?sub_entry=0", true);

    assertBackForwardState(true, true);
    await assertMenulist(
      requireUserInteraction ? [1, 1, 1, 0] : [1, 1, 1, 0],
      IFRAME_PAGE
    );

    BrowserTestUtils.removeTab(tab);
  }

  Services.prefs.clearUserPref("browser.navigation.requireUserInteraction");
}

// Test that when the pref is flipped, we are skipping history
// entries without user interaction when following links with hash URIs.
add_task(async function test_hashURI() {
  async function followLinkHash(url) {
    info(`Creating and following a link to ${url}`);
    let browser = gBrowser.selectedBrowser;
    let loaded = BrowserTestUtils.waitForLocationChange(gBrowser, url);
    await SpecialPowers.spawn(browser, [url], function (url) {
      let a = content.document.createElement("a");
      a.href = url;
      content.document.body.appendChild(a);
      a.click();
    });
    await loaded;
    info(`Loaded ${url}`);
  }

  await runTopLevelTest(followLinkHash, true);
});

// Test that when the pref is flipped, we are skipping history
// entries without user interaction when using history.pushState.
add_task(async function test_pushState() {
  await runTopLevelTest(pushState);
});

// Test that when the pref is flipped, we are skipping history
// entries without user interaction when following a link.
add_task(async function test_followLink() {
  await runTopLevelTest(followLink);
});

// Test that when the pref is flipped, we are skipping history
// entries without user interaction when navigating inside an iframe
// using history.pushState.
add_task(async function test_iframe_pushState() {
  await runIframeTest(pushState);
});

// Test that when the pref is flipped, we are skipping history
// entries without user interaction when navigating inside an iframe
// by following links.
add_task(async function test_iframe_followLink() {
  await runIframeTest(followLink);
});