summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_backforward_userinteraction_systemprincipal.js
blob: 289ed1d1337e5a3a0ab7564007cd15b803e6deac (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
/* 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";

async function runTest(privilegedLoad) {
  let prefVals;
  // Test with both pref on and off, unless parent-controlled pref is enabled.
  // This distinction can be removed once SHIP is enabled by default.
  if (
    Services.prefs.getBoolPref("browser.tabs.documentchannel.parent-controlled")
  ) {
    prefVals = [false];
  } else {
    prefVals = [true, false];
  }

  for (let requireUserInteraction of prefVals) {
    Services.prefs.setBoolPref(
      "browser.navigation.requireUserInteraction",
      requireUserInteraction
    );

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

    assertBackForwardState(false, false);

    await followLink(TEST_PAGE + "?entry=1");

    assertBackForwardState(true, false);

    await followLink(TEST_PAGE + "?entry=2");

    assertBackForwardState(true, false);

    await followLink(TEST_PAGE + "?entry=3");

    assertBackForwardState(true, false);

    // Entry 4 will be added through a user action in browser chrome,
    // giving user interaction to entry 3. Entry 4 should not gain automatic
    // user interaction.
    await privilegedLoad(TEST_PAGE + "?entry=4");

    assertBackForwardState(true, false);

    await followLink(TEST_PAGE + "?entry=5");

    assertBackForwardState(true, false);

    if (!requireUserInteraction) {
      await goBack(TEST_PAGE + "?entry=4");
    }
    await goBack(TEST_PAGE + "?entry=3");

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

    assertBackForwardState(true, true);

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

    assertBackForwardState(false, true);

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

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

    assertBackForwardState(true, true);

    if (!requireUserInteraction) {
      await goForward(TEST_PAGE + "?entry=4");
    }

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

    assertBackForwardState(true, false);

    BrowserTestUtils.removeTab(tab);
  }

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

// Test that we add a user interaction flag to the previous site when loading
// a new site from user interaction with privileged UI, e.g. through the
// URL bar.
add_task(async function test_urlBar() {
  await runTest(async function (url) {
    info(`Loading ${url} via the URL bar.`);
    let browser = gBrowser.selectedBrowser;
    let loaded = BrowserTestUtils.browserLoaded(browser, false, url);
    gURLBar.focus();
    gURLBar.value = url;
    gURLBar.goButton.click();
    await loaded;
  });
});