summaryrefslogtreecommitdiffstats
path: root/browser/components/contextualidentity/test/browser/browser_usercontextid_new_window.js
blob: 5fd4c6f3c2fc2b1c2af6cb61a7462c1f6dab9cb6 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that the content of new browser windows have the expected
// userContextId when it passed as the window arguments.

const TEST_URI =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "http://mochi.test:8888"
  ) + "empty_file.html";

function openWindowWithUserContextId(userContextId, isPrivate) {
  let flags = "chrome,dialog=no,all";
  if (isPrivate) {
    flags += ",private";
  }

  let args = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);

  let urlSupports = Cc["@mozilla.org/supports-string;1"].createInstance(
    Ci.nsISupportsString
  );
  urlSupports.data = TEST_URI;
  args.appendElement(urlSupports);

  args.appendElement(null);
  args.appendElement(null);
  args.appendElement(null);
  args.appendElement(null);

  let userContextIdSupports = Cc[
    "@mozilla.org/supports-PRUint32;1"
  ].createInstance(Ci.nsISupportsPRUint32);
  userContextIdSupports.data = userContextId;
  args.appendElement(userContextIdSupports);

  args.appendElement(Services.scriptSecurityManager.getSystemPrincipal());
  args.appendElement(Services.scriptSecurityManager.getSystemPrincipal());
  args.appendElement(Services.scriptSecurityManager.getSystemPrincipal());

  let windowPromise = BrowserTestUtils.waitForNewWindow({ url: TEST_URI });
  Services.ww.openWindow(
    null,
    AppConstants.BROWSER_CHROME_URL,
    "_blank",
    flags,
    args
  );
  return windowPromise;
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.userContext.enabled", true]],
  });
});

add_task(async function test_new_window() {
  let win = await openWindowWithUserContextId(1, false);

  await SpecialPowers.spawn(win.gBrowser.selectedBrowser, [TEST_URI], url => {
    Assert.equal(content.document.URL, url, "expected document URL");
    let { originAttributes } = content.document.nodePrincipal;
    Assert.equal(originAttributes.userContextId, 1, "expected userContextId");
    Assert.equal(
      originAttributes.privateBrowsingId,
      0,
      "expected non-private context"
    );
  });

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_new_private_window() {
  let win = await openWindowWithUserContextId(1, true);

  await SpecialPowers.spawn(win.gBrowser.selectedBrowser, [TEST_URI], url => {
    Assert.equal(content.document.URL, url, "expected document URL");
    let { originAttributes } = content.document.nodePrincipal;
    Assert.equal(originAttributes.userContextId, 1, "expected userContextId");
    Assert.equal(
      originAttributes.privateBrowsingId,
      1,
      "expected private context"
    );
  });

  await BrowserTestUtils.closeWindow(win);
});