summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_navigate_through_urls_origin_attributes.js
blob: 226817a350a38609724333172ca44e798e3578b9 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* import-globals-from helper_origin_attrs_testing.js */
loadTestSubscript("helper_origin_attrs_testing.js");

const PATH = "browser/browser/base/content/test/tabs/blank.html";

var TEST_CASES = [
  { uri: "https://example.com/" + PATH },
  { uri: "https://example.org/" + PATH },
  { uri: "about:preferences" },
  { uri: "about:config" },
];

// 3 container tabs, 1 regular tab and 1 private tab
const NUM_PAGES_OPEN_FOR_EACH_TEST_CASE = 5;
var remoteTypes;
var xulFrameLoaderCreatedCounter = {};

function handleEventLocal(aEvent) {
  if (aEvent.type != "XULFrameLoaderCreated") {
    return;
  }
  // Ignore <browser> element in about:preferences and any other special pages
  if ("gBrowser" in aEvent.target.ownerGlobal) {
    xulFrameLoaderCreatedCounter.numCalledSoFar++;
  }
}

var gPrevRemoteTypeRegularTab;
var gPrevRemoteTypeContainerTab;
var gPrevRemoteTypePrivateTab;

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.userContext.enabled", true],
      // don't preload tabs so we don't have extra XULFrameLoaderCreated events
      // firing
      ["browser.newtab.preload", false],
    ],
  });

  requestLongerTimeout(4);
});

function setupRemoteTypes() {
  gPrevRemoteTypeRegularTab = null;
  gPrevRemoteTypeContainerTab = {};
  gPrevRemoteTypePrivateTab = null;

  remoteTypes = getExpectedRemoteTypes(
    gFissionBrowser,
    NUM_PAGES_OPEN_FOR_EACH_TEST_CASE
  );
}

add_task(async function testNavigate() {
  setupRemoteTypes();
  /**
   * Open a regular tab, 3 container tabs and a private window, load about:blank or about:privatebrowsing
   * For each test case
   *  load the uri
   *  verify correct remote type
   * close tabs
   */

  let regularPage = await openURIInRegularTab("about:blank", window);
  gPrevRemoteTypeRegularTab = regularPage.tab.linkedBrowser.remoteType;
  let containerPages = [];
  for (
    var user_context_id = 1;
    user_context_id <= NUM_USER_CONTEXTS;
    user_context_id++
  ) {
    let containerPage = await openURIInContainer(
      "about:blank",
      window,
      user_context_id
    );
    gPrevRemoteTypeContainerTab[user_context_id] =
      containerPage.tab.linkedBrowser.remoteType;
    containerPages.push(containerPage);
  }

  let privatePage = await openURIInPrivateTab();
  gPrevRemoteTypePrivateTab = privatePage.tab.linkedBrowser.remoteType;

  for (const testCase of TEST_CASES) {
    let uri = testCase.uri;

    await loadURIAndCheckRemoteType(
      regularPage.tab.linkedBrowser,
      uri,
      "regular tab",
      gPrevRemoteTypeRegularTab
    );
    gPrevRemoteTypeRegularTab = regularPage.tab.linkedBrowser.remoteType;

    for (const page of containerPages) {
      await loadURIAndCheckRemoteType(
        page.tab.linkedBrowser,
        uri,
        `container tab ${page.user_context_id}`,
        gPrevRemoteTypeContainerTab[page.user_context_id]
      );
      gPrevRemoteTypeContainerTab[page.user_context_id] =
        page.tab.linkedBrowser.remoteType;
    }

    await loadURIAndCheckRemoteType(
      privatePage.tab.linkedBrowser,
      uri,
      "private tab",
      gPrevRemoteTypePrivateTab
    );
    gPrevRemoteTypePrivateTab = privatePage.tab.linkedBrowser.remoteType;
  }
  // Close tabs
  containerPages.forEach(containerPage => {
    BrowserTestUtils.removeTab(containerPage.tab);
  });
  BrowserTestUtils.removeTab(regularPage.tab);
  BrowserTestUtils.removeTab(privatePage.tab);
});

async function loadURIAndCheckRemoteType(
  aBrowser,
  aURI,
  aText,
  aPrevRemoteType
) {
  let expectedCurr = remoteTypes.shift();
  initXulFrameLoaderCreatedCounter(xulFrameLoaderCreatedCounter);
  aBrowser.ownerGlobal.gBrowser.addEventListener(
    "XULFrameLoaderCreated",
    handleEventLocal
  );
  let loaded = BrowserTestUtils.browserLoaded(aBrowser, false, aURI);
  info(`About to load ${aURI} in ${aText}`);
  await BrowserTestUtils.loadURIString(aBrowser, aURI);
  await loaded;

  // Verify correct remote type
  is(
    expectedCurr,
    aBrowser.remoteType,
    `correct remote type for ${aURI} ${aText}`
  );

  // Verify XULFrameLoaderCreated firing correct number of times
  info(
    `XULFrameLoaderCreated was fired ${xulFrameLoaderCreatedCounter.numCalledSoFar} time(s) for ${aURI} ${aText}`
  );
  var numExpected =
    expectedCurr == aPrevRemoteType &&
    // With BFCache in the parent we'll get a XULFrameLoaderCreated even if
    // expectedCurr == aPrevRemoteType, because we store the old frameloader
    // in the BFCache. We have to make an exception for loads in the parent
    // process (which have a null aPrevRemoteType/expectedCurr) because
    // BFCache in the parent disables caching for those loads.
    (!SpecialPowers.Services.appinfo.sessionHistoryInParent || !expectedCurr)
      ? 0
      : 1;
  is(
    xulFrameLoaderCreatedCounter.numCalledSoFar,
    numExpected,
    `XULFrameLoaderCreated fired correct number of times for ${aURI} ${aText} 
    prev=${aPrevRemoteType} curr =${aBrowser.remoteType}`
  );
  aBrowser.ownerGlobal.gBrowser.removeEventListener(
    "XULFrameLoaderCreated",
    handleEventLocal
  );
}