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

const USER_CONTEXTS = ["default", "personal", "work"];

const BASE_URI =
  "http://mochi.test:8888/browser/browser/components/" +
  "contextualidentity/test/browser/file_reflect_cookie_into_title.html";

// opens `uri' in a new tab with the provided userContextId and focuses it.
// returns the newly opened tab
function openTabInUserContext(uri, userContextId) {
  // open the tab in the correct userContextId
  let tab = BrowserTestUtils.addTab(gBrowser, uri, { userContextId });

  // select tab and make sure its browser is focused
  gBrowser.selectedTab = tab;
  tab.ownerGlobal.focus();

  return tab;
}

add_setup(async function () {
  // make sure userContext is enabled.
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.userContext.enabled", true],
      ["dom.ipc.processCount", 1],
    ],
  });
});

add_task(async function test() {
  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    // load the page in 3 different contexts and set a cookie
    // which should only be visible in that context
    let cookie = USER_CONTEXTS[userContextId];

    // open our tab in the given user context
    let tab = openTabInUserContext(BASE_URI + "?" + cookie, userContextId);

    // wait for tab load
    await BrowserTestUtils.browserLoaded(gBrowser.getBrowserForTab(tab));

    // remove the tab
    gBrowser.removeTab(tab);
  }

  {
    // Set a cookie in a different context so we can detect if that affects
    // cross-context properly. If we don't do that, we get an UNEXPECTED-PASS
    // for the localStorage case for the last tab we set.
    let tab = openTabInUserContext(BASE_URI + "?foo", 9999);
    await BrowserTestUtils.browserLoaded(gBrowser.getBrowserForTab(tab));
    gBrowser.removeTab(tab);
  }

  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    // Load the page without setting the cookie this time
    let expectedContext = USER_CONTEXTS[userContextId];

    let tab = openTabInUserContext(BASE_URI, userContextId);

    // wait for load
    let browser = gBrowser.getBrowserForTab(tab);
    await BrowserTestUtils.browserLoaded(browser);

    // get the title
    let title = browser.contentTitle.trim().split("|");

    // check each item in the title and validate it meets expectatations
    for (let part of title) {
      let [storageMethodName, value] = part.split("=");
      is(
        value,
        expectedContext,
        "the title reflects the expected contextual identity of " +
          expectedContext +
          " for method " +
          storageMethodName +
          ": " +
          value
      );
    }

    gBrowser.removeTab(tab);
  }
});