summaryrefslogtreecommitdiffstats
path: root/toolkit/components/remotebrowserutils/tests/browser/browser_RemoteWebNavigation.js
blob: 22952e85f53b4b3c9097c6a360c1dde13861e788 (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
const SYSTEMPRINCIPAL = Services.scriptSecurityManager.getSystemPrincipal();
const DUMMY1 =
  "http://test1.example.org/browser/toolkit/modules/tests/browser/dummy_page.html";
const DUMMY2 =
  "http://test2.example.org/browser/toolkit/modules/tests/browser/dummy_page.html";
const LOAD_URI_OPTIONS = { triggeringPrincipal: SYSTEMPRINCIPAL };

function waitForLoad(uri) {
  return BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, false, uri);
}

function waitForPageShow(browser = gBrowser.selectedBrowser) {
  return BrowserTestUtils.waitForContentEvent(browser, "pageshow", true);
}

// Tests that loadURI accepts a referrer and it is included in the load.
add_task(async function test_referrer() {
  gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
  let browser = gBrowser.selectedBrowser;
  let ReferrerInfo = Components.Constructor(
    "@mozilla.org/referrer-info;1",
    "nsIReferrerInfo",
    "init"
  );

  let loadURIOptionsWithReferrer = {
    triggeringPrincipal: SYSTEMPRINCIPAL,
    referrerInfo: new ReferrerInfo(
      Ci.nsIReferrerInfo.EMPTY,
      true,
      Services.io.newURI(DUMMY2)
    ),
  };
  browser.webNavigation.loadURI(
    Services.io.newURI(DUMMY1),
    loadURIOptionsWithReferrer
  );
  await waitForLoad(DUMMY1);

  await SpecialPowers.spawn(
    browser,
    [[DUMMY1, DUMMY2]],
    function ([dummy1, dummy2]) {
      function getExpectedReferrer(referrer) {
        let defaultPolicy = Services.prefs.getIntPref(
          "network.http.referer.defaultPolicy"
        );
        ok(
          [2, 3].indexOf(defaultPolicy) > -1,
          "default referrer policy should be either strict-origin-when-cross-origin(2) or no-referrer-when-downgrade(3)"
        );
        if (defaultPolicy == 2) {
          return referrer.match(/https?:\/\/[^\/]+\/?/i)[0];
        }
        return referrer;
      }

      is(content.location.href, dummy1, "Should have loaded the right URL");
      is(
        content.document.referrer,
        getExpectedReferrer(dummy2),
        "Should have the right referrer"
      );
    }
  );

  gBrowser.removeCurrentTab();
});

// Tests that remote access to webnavigation.sessionHistory works.
add_task(async function test_history() {
  async function checkHistoryIndex(browser, n) {
    if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
      return SpecialPowers.spawn(browser, [n], function (n) {
        let history =
          docShell.browsingContext.childSessionHistory.legacySHistory;

        is(history.index, n, "Should be at the right place in history");
      });
    }

    let history = browser.browsingContext.sessionHistory;
    is(history.index, n, "Should be at the right place in history");
    return null;
  }

  gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
  let browser = gBrowser.selectedBrowser;

  browser.webNavigation.loadURI(Services.io.newURI(DUMMY1), LOAD_URI_OPTIONS);
  await waitForLoad(DUMMY1);

  browser.webNavigation.loadURI(Services.io.newURI(DUMMY2), LOAD_URI_OPTIONS);
  await waitForLoad(DUMMY2);

  if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
    await SpecialPowers.spawn(
      browser,
      [[DUMMY1, DUMMY2]],
      function ([dummy1, dummy2]) {
        let history =
          docShell.browsingContext.childSessionHistory.legacySHistory;

        is(history.count, 2, "Should be two history items");
        is(history.index, 1, "Should be at the right place in history");
        let entry = history.getEntryAtIndex(0);
        is(entry.URI.spec, dummy1, "Should have the right history entry");
        entry = history.getEntryAtIndex(1);
        is(entry.URI.spec, dummy2, "Should have the right history entry");
      }
    );
  } else {
    let history = browser.browsingContext.sessionHistory;

    is(history.count, 2, "Should be two history items");
    is(history.index, 1, "Should be at the right place in history");
    let entry = history.getEntryAtIndex(0);
    is(entry.URI.spec, DUMMY1, "Should have the right history entry");
    entry = history.getEntryAtIndex(1);
    is(entry.URI.spec, DUMMY2, "Should have the right history entry");
  }

  let promise = waitForPageShow();
  browser.webNavigation.goBack();
  await promise;
  await checkHistoryIndex(browser, 0);

  promise = waitForPageShow();
  browser.webNavigation.goForward();
  await promise;
  await checkHistoryIndex(browser, 1);

  promise = waitForPageShow();
  browser.webNavigation.gotoIndex(0);
  await promise;
  await checkHistoryIndex(browser, 0);

  gBrowser.removeCurrentTab();
});

// Tests that load flags are passed through to the content process.
add_task(async function test_flags() {
  async function checkHistory(browser, { count, index }) {
    if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
      return SpecialPowers.spawn(
        browser,
        [[DUMMY2, count, index]],
        function ([dummy2, count, index]) {
          let history =
            docShell.browsingContext.childSessionHistory.legacySHistory;
          is(history.count, count, "Should be one history item");
          is(history.index, index, "Should be at the right place in history");
          let entry = history.getEntryAtIndex(index);
          is(entry.URI.spec, dummy2, "Should have the right history entry");
        }
      );
    }

    let history = browser.browsingContext.sessionHistory;
    is(history.count, count, "Should be one history item");
    is(history.index, index, "Should be at the right place in history");
    let entry = history.getEntryAtIndex(index);
    is(entry.URI.spec, DUMMY2, "Should have the right history entry");

    return null;
  }

  gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
  let browser = gBrowser.selectedBrowser;

  browser.webNavigation.loadURI(Services.io.newURI(DUMMY1), LOAD_URI_OPTIONS);
  await waitForLoad(DUMMY1);
  let loadURIOptionsReplaceHistory = {
    triggeringPrincipal: SYSTEMPRINCIPAL,
    loadFlags: Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY,
  };
  browser.webNavigation.loadURI(
    Services.io.newURI(DUMMY2),
    loadURIOptionsReplaceHistory
  );
  await waitForLoad(DUMMY2);
  await checkHistory(browser, { count: 1, index: 0 });
  let loadURIOptionsBypassHistory = {
    triggeringPrincipal: SYSTEMPRINCIPAL,
    loadFlags: Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_HISTORY,
  };
  browser.webNavigation.loadURI(
    Services.io.newURI(DUMMY1),
    loadURIOptionsBypassHistory
  );
  await waitForLoad(DUMMY1);
  await checkHistory(browser, { count: 1, index: 0 });

  gBrowser.removeCurrentTab();
});

// Tests that attempts to use unsupported arguments throw an exception.
add_task(async function test_badarguments() {
  if (!gMultiProcessBrowser) {
    return;
  }

  gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
  let browser = gBrowser.selectedBrowser;

  try {
    let loadURIOptionsBadPostData = {
      triggeringPrincipal: SYSTEMPRINCIPAL,
      postData: {},
    };
    browser.webNavigation.loadURI(
      Services.io.newURI(DUMMY1),
      loadURIOptionsBadPostData
    );
    ok(
      false,
      "Should have seen an exception from trying to pass some postdata"
    );
  } catch (e) {
    ok(true, "Should have seen an exception from trying to pass some postdata");
  }

  try {
    let loadURIOptionsBadHeader = {
      triggeringPrincipal: SYSTEMPRINCIPAL,
      headers: {},
    };
    browser.webNavigation.loadURI(
      Services.io.newURI(DUMMY1),
      loadURIOptionsBadHeader
    );
    ok(false, "Should have seen an exception from trying to pass some headers");
  } catch (e) {
    ok(true, "Should have seen an exception from trying to pass some headers");
  }

  gBrowser.removeCurrentTab();
});