summaryrefslogtreecommitdiffstats
path: root/dom/base/test/browser_aboutnewtab_process_selection.js
blob: db0e80acdf7524f0d71a1175bc3ae0a64078dd77 (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
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";

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.processCount.webIsolated", 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++) {
    BrowserCommands.openTab();
    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.startLoadingURIString(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 should create a new content process.
  expectedChildCount += 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 create a new content process."
  );

  // Navigate to a new content page from the child side.
  //
  // We should create a new content process.
  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"
  );

  BrowserCommands.openTab();
  await BrowserTestUtils.maybeCreatePreloadedBrowser(gBrowser);

  // Now check that the tabs have the correct browser attributes set
  is(
    gBrowser.selectedBrowser.hasAttribute("preloadedState"),
    false,
    "The opened tab consumed the preloaded browser and removed the attribute"
  );

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

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