summaryrefslogtreecommitdiffstats
path: root/dom/base/test/browser_aboutnewtab_process_selection.js
blob: b831d66fffbbb37e24f995d57137b7972078dea4 (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
const TEST_URL = "http://www.example.com/browser/dom/base/test/dummy.html";
const TEST_URL_2 = "http://example.org/browser/dom/base/test/dummy.html";
const PRELOADED_STATE = "preloaded";
const CONSUMED_STATE = "consumed";

var ppmm = Services.ppmm;

add_task(async function() {
  // We want to count processes in this test, so let's disable the pre-allocated process manager.
  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.ipc.processPrelaunch.enabled", false],
      ["dom.ipc.processCount", 10],
      ["dom.ipc.keepProcessesAlive.web", 10],
    ],
  });
});

add_task(async function() {
  // This test is only relevant in e10s.
  if (!gMultiProcessBrowser) {
    return;
  }

  ppmm.releaseCachedProcesses();

  // Wait for the preloaded browser to load.
  await BrowserTestUtils.maybeCreatePreloadedBrowser(gBrowser);

  // Store the number of processes.
  let expectedChildCount = ppmm.childCount;

  // Open 3 tabs using the preloaded browser.
  let tabs = [];
  for (let i = 0; i < 3; i++) {
    BrowserOpenTab();
    tabs.unshift(gBrowser.selectedTab);
    await BrowserTestUtils.maybeCreatePreloadedBrowser(gBrowser);

    // Check that the process count did not change.
    is(
      ppmm.childCount,
      expectedChildCount,
      "Preloaded browser should not create a new content process."
    );
  }

  // Navigate to a content page from the parent side.
  //
  // We should create a new content process.
  expectedChildCount += 1;
  BrowserTestUtils.loadURI(tabs[0].linkedBrowser, TEST_URL);
  await BrowserTestUtils.browserLoaded(tabs[0].linkedBrowser, false, TEST_URL);
  is(
    ppmm.childCount,
    expectedChildCount,
    "Navigating away from the preloaded browser (parent side) should create a new content process."
  );

  // Navigate to the same content page from the child side.
  //
  // We already have a content process for TEST_URL, so we don't create a new
  // one when Fission is enabled.
  expectedChildCount += gFissionBrowser ? 0 : 1;
  await BrowserTestUtils.switchTab(gBrowser, tabs[1]);
  await SpecialPowers.spawn(tabs[1].linkedBrowser, [TEST_URL], url => {
    content.location.href = url;
  });
  await BrowserTestUtils.browserLoaded(tabs[1].linkedBrowser, false, TEST_URL);
  is(
    ppmm.childCount,
    expectedChildCount,
    `Navigating away from the preloaded browser (child side, same-origin) should${
      gFissionBrowser ? " not " : " "
    }create a new content process.`
  );

  // Navigate to a new content page from the child side.
  //
  // We should create a new content process, with or without Fission.
  expectedChildCount += 1;
  await BrowserTestUtils.switchTab(gBrowser, tabs[2]);
  await ContentTask.spawn(tabs[2].linkedBrowser, TEST_URL_2, url => {
    content.location.href = url;
  });
  await BrowserTestUtils.browserLoaded(
    tabs[2].linkedBrowser,
    false,
    TEST_URL_2
  );
  is(
    ppmm.childCount,
    expectedChildCount,
    "Navigating away from the preloaded browser (child side, cross-origin) should create a new content process."
  );

  for (let tab of tabs) {
    BrowserTestUtils.removeTab(tab);
  }

  // Make sure the preload browser does not keep any of the new processes alive.
  NewTabPagePreloading.removePreloadedBrowser(window);

  // Since we kept alive all the processes, we can shut down the ones that do
  // not host any tabs reliably.
  ppmm.releaseCachedProcesses();
});

add_task(async function preloaded_state_attribute() {
  // Wait for a preloaded browser to exist, use it, and then create another one
  await BrowserTestUtils.maybeCreatePreloadedBrowser(gBrowser);
  let preloadedTabState = gBrowser.preloadedBrowser.getAttribute(
    "preloadedState"
  );
  is(
    preloadedTabState,
    PRELOADED_STATE,
    "Sanity check that the first preloaded browser has the correct attribute"
  );

  BrowserOpenTab();
  await BrowserTestUtils.maybeCreatePreloadedBrowser(gBrowser);

  // Now check that the tabs have the correct browser attributes set
  let consumedTabState = gBrowser.selectedBrowser.getAttribute(
    "preloadedState"
  );
  is(
    consumedTabState,
    CONSUMED_STATE,
    "The opened tab consumed the preloaded browser and updated the attribute"
  );

  preloadedTabState = gBrowser.preloadedBrowser.getAttribute("preloadedState");
  is(
    preloadedTabState,
    PRELOADED_STATE,
    "The preloaded browser has the correct attribute"
  );

  // Navigate away and check that the attribute has been removed altogether
  BrowserTestUtils.loadURI(gBrowser.selectedBrowser, TEST_URL);
  await BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    TEST_URL
  );
  let navigatedTabHasState = gBrowser.selectedBrowser.hasAttribute(
    "preloadedState"
  );
  ok(
    !navigatedTabHasState,
    "Correctly removed the preloadState attribute when navigating away"
  );

  // Remove tabs and preloaded browsers
  BrowserTestUtils.removeTab(gBrowser.selectedTab);
  NewTabPagePreloading.removePreloadedBrowser(window);
});