summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_sessionStoreContainer.js
blob: 1c2a82305f3ce6f2ba6dde1289ab62c1d21b8dbe (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

add_task(async function () {
  for (let i = 0; i < 3; ++i) {
    let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/", {
      userContextId: i,
    });
    let browser = tab.linkedBrowser;

    await promiseBrowserLoaded(browser);

    let tab2 = gBrowser.duplicateTab(tab);
    Assert.equal(tab2.getAttribute("usercontextid"), i);
    let browser2 = tab2.linkedBrowser;
    await promiseTabRestored(tab2);

    await SpecialPowers.spawn(
      browser2,
      [{ expectedId: i }],
      async function (args) {
        let loadContext = docShell.QueryInterface(Ci.nsILoadContext);
        Assert.equal(
          loadContext.originAttributes.userContextId,
          args.expectedId,
          "The docShell has the correct userContextId"
        );
      }
    );

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

add_task(async function () {
  let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/", {
    userContextId: 1,
  });
  let browser = tab.linkedBrowser;

  await promiseBrowserLoaded(browser);

  gBrowser.selectedTab = tab;

  let tab2 = gBrowser.duplicateTab(tab);
  let browser2 = tab2.linkedBrowser;
  await promiseTabRestored(tab2);

  await SpecialPowers.spawn(
    browser2,
    [{ expectedId: 1 }],
    async function (args) {
      Assert.equal(
        docShell.getOriginAttributes().userContextId,
        args.expectedId,
        "The docShell has the correct userContextId"
      );
    }
  );

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

add_task(async function () {
  let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/", {
    userContextId: 1,
  });
  let browser = tab.linkedBrowser;

  await promiseBrowserLoaded(browser);

  gBrowser.removeTab(tab);

  let tab2 = ss.undoCloseTab(window, 0);
  Assert.equal(tab2.getAttribute("usercontextid"), 1);
  await promiseTabRestored(tab2);
  await SpecialPowers.spawn(
    tab2.linkedBrowser,
    [{ expectedId: 1 }],
    async function (args) {
      Assert.equal(
        docShell.getOriginAttributes().userContextId,
        args.expectedId,
        "The docShell has the correct userContextId"
      );
    }
  );

  BrowserTestUtils.removeTab(tab2);
});

// Opens "uri" in a new tab with the provided userContextId and focuses it.
// Returns the newly opened tab.
async function openTabInUserContext(userContextId) {
  // Open the tab in the correct userContextId.
  let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com", {
    userContextId,
  });

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

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);
  return { tab, browser };
}

function waitForNewCookie() {
  return new Promise(resolve => {
    Services.obs.addObserver(function observer(subj, topic, data) {
      if (data == "added") {
        Services.obs.removeObserver(observer, topic);
        resolve();
      }
    }, "session-cookie-changed");
  });
}

add_task(async function test() {
  const USER_CONTEXTS = ["default", "personal", "work"];

  // Make sure userContext is enabled.
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.userContext.enabled", true]],
  });

  Services.cookies.removeAll();

  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, browser } = await openTabInUserContext(userContextId);

    await Promise.all([
      waitForNewCookie(),
      SpecialPowers.spawn(
        browser,
        [cookie],
        passedCookie => (content.document.cookie = passedCookie)
      ),
    ]);

    // Ensure the tab's session history is up-to-date.
    await TabStateFlusher.flush(browser);

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

  let state = JSON.parse(SessionStore.getBrowserState());
  is(
    state.cookies.length,
    USER_CONTEXTS.length,
    "session restore should have each container's cookie"
  );
});