summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/browser_remote_type_process_swap.js
blob: 2acd3b9a51fb42685d30aeb06d04d22821ece0a2 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * This test tests a navigation request to a Service Worker-controlled origin &
 * scope that results in a cross-origin redirect to a
 * non-Service Worker-controlled scope which additionally participates in
 * cross-process redirect.
 *
 * On success, the test will not crash.
 */

const ORIGIN = "http://mochi.test:8888";
const TEST_ROOT = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  ORIGIN
);

const SW_REGISTER_PAGE_URL = `${TEST_ROOT}empty_with_utils.html`;
const SW_SCRIPT_URL = `${TEST_ROOT}empty.js`;

const FILE_URL = (() => {
  // Get the file as an nsIFile.
  const file = getChromeDir(getResolvedURI(gTestPath));
  file.append("empty.html");

  // Convert the nsIFile to an nsIURI to access the path.
  return Services.io.newFileURI(file).spec;
})();

const CROSS_ORIGIN = "https://example.com";
const CROSS_ORIGIN_URL = SW_REGISTER_PAGE_URL.replace(ORIGIN, CROSS_ORIGIN);
const CROSS_ORIGIN_REDIRECT_URL = `${TEST_ROOT}redirect.sjs?${CROSS_ORIGIN_URL}`;

async function loadURI(aXULBrowser, aURI) {
  const browserLoadedPromise = BrowserTestUtils.browserLoaded(aXULBrowser);
  BrowserTestUtils.loadURIString(aXULBrowser, aURI);

  return browserLoadedPromise;
}

async function runTest() {
  // Step 1: register a Service Worker under `ORIGIN` so that all subsequent
  // requests to `ORIGIN` will be marked as controlled.
  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
      ["dom.serviceWorkers.testing.enabled", true],
      ["devtools.console.stdout.content", true],
    ],
  });

  info(`Loading tab with page ${SW_REGISTER_PAGE_URL}`);
  const tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: SW_REGISTER_PAGE_URL,
  });
  info(`Loaded page ${SW_REGISTER_PAGE_URL}`);

  info(`Registering Service Worker ${SW_SCRIPT_URL}`);
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [{ scriptURL: SW_SCRIPT_URL }],
    async ({ scriptURL }) => {
      await content.wrappedJSObject.registerAndWaitForActive(scriptURL);
    }
  );
  info(`Registered and activated Service Worker ${SW_SCRIPT_URL}`);

  // Step 2: open a page over file:// and navigate to trigger a process swap
  // for the response.
  info(`Loading ${FILE_URL}`);
  await loadURI(tab.linkedBrowser, FILE_URL);

  Assert.equal(
    tab.linkedBrowser.remoteType,
    E10SUtils.FILE_REMOTE_TYPE,
    `${FILE_URL} should load in a file process`
  );

  info(`Dynamically creating ${FILE_URL}'s link`);
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [{ href: CROSS_ORIGIN_REDIRECT_URL }],
    ({ href }) => {
      const { document } = content;
      const link = document.createElement("a");
      link.href = href;
      link.id = "link";
      link.appendChild(document.createTextNode(href));
      document.body.appendChild(link);
    }
  );

  const redirectPromise = BrowserTestUtils.waitForLocationChange(
    gBrowser,
    CROSS_ORIGIN_URL
  );

  info("Starting navigation");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#link",
    {},
    tab.linkedBrowser
  );

  info(`Waiting for location to change to ${CROSS_ORIGIN_URL}`);
  await redirectPromise;

  info("Waiting for the browser to stop");
  await BrowserTestUtils.browserStopped(tab.linkedBrowser);

  if (SpecialPowers.useRemoteSubframes) {
    Assert.ok(
      E10SUtils.isWebRemoteType(tab.linkedBrowser.remoteType),
      `${CROSS_ORIGIN_URL} should load in a web-content process`
    );
  }

  // Step 3: cleanup.
  info("Loading initial page to unregister all Service Workers");
  await loadURI(tab.linkedBrowser, SW_REGISTER_PAGE_URL);

  info("Unregistering all Service Workers");
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    async () => await content.wrappedJSObject.unregisterAll()
  );

  info("Closing tab");
  BrowserTestUtils.removeTab(tab);
}

add_task(runTest);