summaryrefslogtreecommitdiffstats
path: root/browser/components/tests/browser/browser_initial_tab_remoteType.js
blob: fac4675c9dc69a2b6aaa4685e18cb5a570b897ae (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * These tests test that the initial browser tab has the right
 * process type assigned to it on creation, which avoids needless
 * process flips.
 */

"use strict";

const PRIVILEGEDABOUT_PROCESS_PREF =
  "browser.tabs.remote.separatePrivilegedContentProcess";
const PRIVILEGEDABOUT_PROCESS_ENABLED = Services.prefs.getBoolPref(
  PRIVILEGEDABOUT_PROCESS_PREF
);

const REMOTE_BROWSER_SHOWN = "remote-browser-shown";

// When the privileged content process is enabled, we expect about:home
// to load in it. Otherwise, it's in a normal web content process.
const EXPECTED_ABOUTHOME_REMOTE_TYPE = PRIVILEGEDABOUT_PROCESS_ENABLED
  ? E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE
  : E10SUtils.DEFAULT_REMOTE_TYPE;

/**
 * Test helper function that takes an nsICommandLine, and passes it
 * into the default command line handler for the browser. It expects
 * a new browser window to open, and then checks that the expected page
 * loads in the initial tab in the expected remote type, without doing
 * unnecessary process flips. The helper function then closes the window.
 *
 * @param aCmdLine (nsICommandLine)
 *        The command line to be processed by the default
 *        nsICommandLineHandler
 * @param aExpectedURL (string)
 *        The URL that the initial browser tab is expected to load.
 * @param aRemoteType (string)
 *        The expected remoteType on the initial browser tab.
 * @returns Promise
 *        Resolves once the checks have completed, and the opened window
 *        have been closed.
 */
async function assertOneRemoteBrowserShown(
  aCmdLine,
  aExpectedURL,
  aRemoteType
) {
  let shownRemoteBrowsers = 0;
  let observer = () => {
    shownRemoteBrowsers++;
  };
  Services.obs.addObserver(observer, REMOTE_BROWSER_SHOWN);

  let newWinPromise = BrowserTestUtils.waitForNewWindow({
    url: aExpectedURL,
  });

  let cmdLineHandler = Cc["@mozilla.org/browser/final-clh;1"].getService(
    Ci.nsICommandLineHandler
  );
  cmdLineHandler.handle(aCmdLine);

  let newWin = await newWinPromise;

  Services.obs.removeObserver(observer, REMOTE_BROWSER_SHOWN);

  if (aRemoteType == E10SUtils.WEB_REMOTE_TYPE) {
    Assert.ok(
      E10SUtils.isWebRemoteType(newWin.gBrowser.selectedBrowser.remoteType)
    );
  } else {
    Assert.equal(newWin.gBrowser.selectedBrowser.remoteType, aRemoteType);
  }

  Assert.equal(
    shownRemoteBrowsers,
    1,
    "Should have only shown 1 remote browser"
  );
  await BrowserTestUtils.closeWindow(newWin);
}

/**
 * Constructs an object that implements an nsICommandLine that should
 * cause the default nsICommandLineHandler to open aURL as the initial
 * tab in a new window. The returns nsICommandLine is stateful, and
 * shouldn't be reused.
 *
 * @param aURL (string)
 *        The URL to load in the initial tab of the new window.
 * @returns nsICommandLine
 */
function constructOnePageCmdLine(aURL) {
  return Cu.createCommandLine(
    ["-url", aURL],
    null,
    Ci.nsICommandLine.STATE_INITIAL_LAUNCH
  );
}

add_setup(async function () {
  NewTabPagePreloading.removePreloadedBrowser(window);

  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.newtab.preload", false],
      ["browser.startup.homepage", "about:home"],
      ["browser.startup.page", 1],
    ],
  });
});

/**
 * This tests the default case, where no arguments are passed.
 */
add_task(async function test_default_args_and_homescreen() {
  let cmdLine = Cu.createCommandLine(
    [],
    null,
    Ci.nsICommandLine.STATE_INITIAL_LAUNCH
  );
  await assertOneRemoteBrowserShown(
    cmdLine,
    "about:home",
    EXPECTED_ABOUTHOME_REMOTE_TYPE
  );
});

/**
 * This tests the case where about:home is passed as the lone
 * argument.
 */
add_task(async function test_abouthome_arg() {
  const URI = "about:home";
  let cmdLine = constructOnePageCmdLine(URI);
  await assertOneRemoteBrowserShown(
    cmdLine,
    URI,
    EXPECTED_ABOUTHOME_REMOTE_TYPE
  );
});

/**
 * This tests the case where example.com is passed as the lone
 * argument.
 */
add_task(async function test_examplecom_arg() {
  const URI = "http://example.com/";
  let cmdLine = constructOnePageCmdLine(URI);
  await assertOneRemoteBrowserShown(
    cmdLine,
    URI,
    E10SUtils.DEFAULT_REMOTE_TYPE
  );
});