summaryrefslogtreecommitdiffstats
path: root/dom/base/test/browser_multiple_popups.js
blob: 2e72cdce8151021d2f8508fdc946ae8bf1a16761 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
 * In this test, we check that the content can't open more than one popup at a
 * time (depending on "dom.allow_mulitple_popups" preference value).
 */

const TEST_DOMAIN = "http://example.net";
const TEST_PATH = "/browser/dom/base/test/";
const CHROME_DOMAIN = "chrome://mochitests/content";

requestLongerTimeout(2);

function promisePopups(count) {
  let windows = [];
  return new Promise(resolve => {
    if (count == 0) {
      resolve([]);
      return;
    }

    let windowObserver = function(aSubject, aTopic, aData) {
      if (aTopic != "domwindowopened") {
        return;
      }
      windows.push(aSubject);
      if (--count == 0) {
        Services.ww.unregisterNotification(windowObserver);
        SimpleTest.executeSoon(() => resolve(windows));
      }
    };
    Services.ww.registerNotification(windowObserver);
  });
}

async function withTestPage(popupCount, optionsOrCallback, callback) {
  let options = optionsOrCallback;
  if (!callback) {
    callback = optionsOrCallback;
    options = {};
  }

  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.block_multiple_popups", true],
      ["dom.disable_open_during_load", true],
    ],
  });

  let domain = options.chrome ? CHROME_DOMAIN : TEST_DOMAIN;
  let tab = BrowserTestUtils.addTab(
    gBrowser,
    domain + TEST_PATH + "browser_multiple_popups.html" + (options.query || "")
  );
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let obs = promisePopups(popupCount);

  await callback(browser);

  let windows = await obs;
  ok(true, `We had ${popupCount} windows.`);
  for (let win of windows) {
    if (win.document.readyState !== "complete") {
      await BrowserTestUtils.waitForEvent(win, "load");
    }
    await BrowserTestUtils.closeWindow(win);
  }
  BrowserTestUtils.removeTab(tab);
}

function promisePopupsBlocked(browser, expectedCount) {
  return SpecialPowers.spawn(browser, [expectedCount], count => {
    return new content.Promise(resolve => {
      content.addEventListener("DOMPopupBlocked", function cb() {
        if (--count == 0) {
          content.removeEventListener("DOMPopupBlocked", cb);
          ok(true, "The popup has been blocked");
          resolve();
        }
      });
    });
  });
}

function startOpeningTwoPopups(browser) {
  return SpecialPowers.spawn(browser.browsingContext, [], () => {
    let p = content.document.createElement("p");
    p.setAttribute("id", "start");
    content.document.body.appendChild(p);
  });
}

add_task(async _ => {
  info("All opened if the pref is off");
  await withTestPage(2, async function(browser) {
    await SpecialPowers.pushPrefEnv({
      set: [
        ["dom.block_multiple_popups", false],
        ["dom.disable_open_during_load", true],
      ],
    });

    await BrowserTestUtils.synthesizeMouseAtCenter("#openPopups", {}, browser);
  });
});

add_task(async _ => {
  info("2 window.open()s in a click event allowed because whitelisted domain.");

  const uri = Services.io.newURI(TEST_DOMAIN);
  const principal = Services.scriptSecurityManager.createContentPrincipal(
    uri,
    {}
  );

  Services.perms.addFromPrincipal(
    principal,
    "popup",
    Services.perms.ALLOW_ACTION
  );

  await withTestPage(2, async function(browser) {
    await BrowserTestUtils.synthesizeMouseAtCenter("#openPopups", {}, browser);
  });

  await new Promise(resolve => {
    Services.clearData.deleteData(
      Ci.nsIClearDataService.CLEAR_PERMISSIONS,
      value => {
        Assert.equal(value, 0);
        resolve();
      }
    );
  });
});

add_task(async _ => {
  info(
    "2 window.open()s in a mouseup event allowed because whitelisted domain."
  );

  const uri = Services.io.newURI(TEST_DOMAIN);
  const principal = Services.scriptSecurityManager.createContentPrincipal(
    uri,
    {}
  );

  Services.perms.addFromPrincipal(
    principal,
    "popup",
    Services.perms.ALLOW_ACTION
  );

  await withTestPage(2, async function(browser) {
    await BrowserTestUtils.synthesizeMouseAtCenter("#input", {}, browser);
  });

  await new Promise(aResolve => {
    Services.clearData.deleteData(
      Ci.nsIClearDataService.CLEAR_PERMISSIONS,
      value => {
        Assert.equal(value, 0);
        aResolve();
      }
    );
  });
});

add_task(async _ => {
  info(
    "2 window.open()s in a single click event: only the first one is allowed."
  );

  await withTestPage(1, async function(browser) {
    let p = promisePopupsBlocked(browser, 1);
    await BrowserTestUtils.synthesizeMouseAtCenter("#openPopups", {}, browser);
    await p;
  });
});

add_task(async _ => {
  info(
    "2 window.open()s in a single mouseup event: only the first one is allowed."
  );

  await withTestPage(1, async function(browser) {
    let p = promisePopupsBlocked(browser, 1);
    await BrowserTestUtils.synthesizeMouseAtCenter("#input", {}, browser);
    await p;
  });
});

add_task(async _ => {
  info("2 window.open()s by non-event code: no windows allowed.");

  await withTestPage(0, { query: "?openPopups" }, async function(browser) {
    let p = promisePopupsBlocked(browser, 2);
    await startOpeningTwoPopups(browser);
    await p;
  });
});

add_task(async _ => {
  info("2 window.open()s by non-event code allowed by permission");
  const uri = Services.io.newURI(TEST_DOMAIN);
  const principal = Services.scriptSecurityManager.createContentPrincipal(
    uri,
    {}
  );

  Services.perms.addFromPrincipal(
    principal,
    "popup",
    Services.perms.ALLOW_ACTION
  );

  await withTestPage(2, { query: "?openPopups" }, async function(browser) {
    await startOpeningTwoPopups(browser);
  });

  await new Promise(aResolve => {
    Services.clearData.deleteData(
      Ci.nsIClearDataService.CLEAR_PERMISSIONS,
      value => {
        Assert.equal(value, 0);
        aResolve();
      }
    );
  });
});

add_task(async _ => {
  info(
    "1 window.open() executing another window.open(): only the first one is allowed."
  );

  await withTestPage(1, async function(browser) {
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#openNestedPopups",
      {},
      browser
    );
  });
});

add_task(async _ => {
  info("window.open() and .click() on the element opening the window.");

  await withTestPage(1, async function(browser) {
    let p = promisePopupsBlocked(browser, 1);

    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#openPopupAndClick",
      {},
      browser
    );

    await p;
  });
});

add_task(async _ => {
  info("All opened from chrome.");
  await withTestPage(2, { chrome: true }, async function(browser) {
    await BrowserTestUtils.synthesizeMouseAtCenter("#openPopups", {}, browser);
  });
});

add_task(async function test_bug_1685056() {
  info(
    "window.open() from a blank iframe window during an event dispatched at the parent page: window should be allowed"
  );

  await withTestPage(1, async function(browser) {
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#openPopupInFrame",
      {},
      browser
    );
  });
});

add_task(async function test_bug_1689853() {
  info("window.open() from a js bookmark (LOAD_FLAGS_ALLOW_POPUPS)");
  await withTestPage(1, async function(browser) {
    const URI =
      "javascript:void(window.open('empty.html', '_blank', 'width=100,height=100'));";
    window.openTrustedLinkIn(URI, "current", {
      allowPopups: true,
      inBackground: false,
      allowInheritPrincipal: true,
    });
  });
});