summaryrefslogtreecommitdiffstats
path: root/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_context_and_chromeFlags.js
blob: 66e8dea3598f56c4fa9d4fb245f8904f94efa625 (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
"use strict";

/**
 * Given some window in the parent process, ensure that
 * the nsIAppWindow has the CHROME_PRIVATE_WINDOW chromeFlag,
 * and that the usePrivateBrowsing property is set to true on
 * both the window's nsILoadContext, as well as on the initial
 * browser's content docShell nsILoadContext.
 *
 * @param win (nsIDOMWindow)
 *        An nsIDOMWindow in the parent process.
 * @return Promise
 */
function assertWindowIsPrivate(win) {
  let winDocShell = win.docShell;
  let chromeFlags = winDocShell.treeOwner
    .QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIAppWindow).chromeFlags;

  if (!win.gBrowser.selectedBrowser.hasContentOpener) {
    Assert.ok(
      chromeFlags & Ci.nsIWebBrowserChrome.CHROME_PRIVATE_WINDOW,
      "Should have the private window chrome flag"
    );
  }

  let loadContext = winDocShell.QueryInterface(Ci.nsILoadContext);
  Assert.ok(
    loadContext.usePrivateBrowsing,
    "The parent window should be using private browsing"
  );

  return SpecialPowers.spawn(
    win.gBrowser.selectedBrowser,
    [],
    async function () {
      let contentLoadContext = docShell.QueryInterface(Ci.nsILoadContext);
      Assert.ok(
        contentLoadContext.usePrivateBrowsing,
        "Content docShell should be using private browsing"
      );
    }
  );
}

/**
 * Tests that chromeFlags bits and the nsILoadContext.usePrivateBrowsing
 * attribute are properly set when opening a new private browsing
 * window.
 */
add_task(async function test_context_and_chromeFlags() {
  let win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
  await assertWindowIsPrivate(win);

  let browser = win.gBrowser.selectedBrowser;

  let newWinPromise = BrowserTestUtils.waitForNewWindow({
    url: "https://example.com/",
  });
  await SpecialPowers.spawn(browser, [], async function () {
    content.open("https://example.com", "_blank", "width=100,height=100");
  });

  let win2 = await newWinPromise;
  await assertWindowIsPrivate(win2);

  await BrowserTestUtils.closeWindow(win2);
  await BrowserTestUtils.closeWindow(win);
});