summaryrefslogtreecommitdiffstats
path: root/dom/tests/browser/browser_noopener.js
blob: d514ff20478f20ac9c055e43d4444676303d2203 (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
178
179
180
181
182
"use strict";

const TESTS = [
  { id: "#test1", name: "", opener: true, newWindow: false },
  { id: "#test2", name: "", opener: false, newWindow: false },
  { id: "#test3", name: "", opener: false, newWindow: false },

  { id: "#test4", name: "uniquename1", opener: true, newWindow: false },
  { id: "#test5", name: "uniquename2", opener: false, newWindow: false },
  { id: "#test6", name: "uniquename3", opener: false, newWindow: false },

  { id: "#test7", name: "", opener: true, newWindow: false },
  { id: "#test8", name: "", opener: false, newWindow: false },
  { id: "#test9", name: "", opener: false, newWindow: false },

  { id: "#test10", name: "uniquename1", opener: true, newWindow: false },
  { id: "#test11", name: "uniquename2", opener: false, newWindow: false },
  { id: "#test12", name: "uniquename3", opener: false, newWindow: false },
];

const TEST_URL =
  "http://mochi.test:8888/browser/dom/tests/browser/test_noopener_source.html";
const TARGET_URL =
  "http://mochi.test:8888/browser/dom/tests/browser/test_noopener_target.html";

const OPEN_NEWWINDOW_PREF = "browser.link.open_newwindow";
const OPEN_NEWWINDOW = 2;
const OPEN_NEWTAB = 3;

const NOOPENER_NEWPROC_PREF = "dom.noopener.newprocess.enabled";

async function doTests(usePrivate, container) {
  let alwaysNewWindow =
    SpecialPowers.getIntPref(OPEN_NEWWINDOW_PREF) == OPEN_NEWWINDOW;

  let window = await BrowserTestUtils.openNewBrowserWindow({
    private: usePrivate,
  });

  let tabOpenOptions = {};
  if (container) {
    tabOpenOptions.userContextId = 1;
  }

  for (let test of TESTS) {
    const testid = `${test.id} (private=${usePrivate}, container=${container}, alwaysNewWindow=${alwaysNewWindow})`;
    let originalTab = BrowserTestUtils.addTab(
      window.gBrowser,
      TEST_URL,
      tabOpenOptions
    );
    await BrowserTestUtils.browserLoaded(originalTab.linkedBrowser);
    await BrowserTestUtils.switchTab(window.gBrowser, originalTab);

    let waitFor;
    if (test.newWindow || alwaysNewWindow) {
      waitFor = BrowserTestUtils.waitForNewWindow({ url: TARGET_URL });
      // Confirm that this window has private browsing set if we're doing a private browsing test
    } else {
      waitFor = BrowserTestUtils.waitForNewTab(
        window.gBrowser,
        TARGET_URL,
        true
      );
    }

    BrowserTestUtils.synthesizeMouseAtCenter(
      test.id,
      {},
      window.gBrowser.getBrowserForTab(originalTab)
    );

    let tab;
    if (test.newWindow || alwaysNewWindow) {
      let window = await waitFor;
      is(
        PrivateBrowsingUtils.isWindowPrivate(window),
        usePrivate,
        "Private status should match for " + testid
      );
      tab = window.gBrowser.selectedTab;
    } else {
      tab = await waitFor;
    }

    // Check that the name matches.
    await SpecialPowers.spawn(
      tab.linkedBrowser,
      [test, container, testid],
      async (test, container, testid) => {
        Assert.equal(
          content.document.nodePrincipal.originAttributes.userContextId,
          container ? 1 : 0,
          `User context ID should match for ${testid}`
        );

        Assert.equal(
          content.window.name,
          test.name,
          `Name should match for ${testid}`
        );
        if (test.opener) {
          Assert.ok(
            content.window.opener,
            `Opener should have been set for ${testid}`
          );
        } else {
          Assert.ok(
            !content.window.opener,
            `Opener should not have been set for ${testid}`
          );
        }
      }
    );

    BrowserTestUtils.removeTab(tab);
    BrowserTestUtils.removeTab(originalTab);
  }

  window.close();
}

async function doAllTests() {
  // Non-private window
  await doTests(false, false);

  // Private window
  await doTests(true, false);

  // Non-private window with container
  await doTests(false, true);
}

// This test takes a really long time, especially in debug builds, as it is
// constant starting and stopping processes, and opens a new window ~144 times.
requestLongerTimeout(30);

add_task(async function prepare() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.window.open.noreferrer.enabled", true]],
  });
});

add_task(async function newtab_sameproc() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [OPEN_NEWWINDOW_PREF, OPEN_NEWTAB],
      [NOOPENER_NEWPROC_PREF, false],
    ],
  });
  await doAllTests();
});

add_task(async function newtab_newproc() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [OPEN_NEWWINDOW_PREF, OPEN_NEWTAB],
      [NOOPENER_NEWPROC_PREF, true],
    ],
  });
  await doAllTests();
});

add_task(async function newwindow_sameproc() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [OPEN_NEWWINDOW_PREF, OPEN_NEWWINDOW],
      [NOOPENER_NEWPROC_PREF, false],
    ],
  });
  await doAllTests();
});

add_task(async function newwindow_newproc() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [OPEN_NEWWINDOW_PREF, OPEN_NEWWINDOW],
      [NOOPENER_NEWPROC_PREF, true],
    ],
  });
  await doAllTests();
});