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

"use strict";

add_task(async function () {
  info("Make sure cookie changes in one process are visible in the other");

  const kRoot = getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content/",
    "https://example.com/"
  );
  const kTestPage = kRoot + "dummy.html";

  Services.cookies.removeAll();

  let tab1 = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: kTestPage,
    forceNewProcess: true,
  });
  let tab2 = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: kTestPage,
    forceNewProcess: true,
  });

  let browser1 = gBrowser.getBrowserForTab(tab1);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  let pid1 = browser1.frameLoader.remoteTab.osPid;
  let pid2 = browser2.frameLoader.remoteTab.osPid;

  // Note, this might not be true once fission is implemented (Bug 1451850)
  Assert.notEqual(pid1, pid2, "We should have different processes here.");

  await SpecialPowers.spawn(browser1, [], async function () {
    is(content.document.cookie, "", "Expecting no cookies");
  });

  await SpecialPowers.spawn(browser2, [], async function () {
    is(content.document.cookie, "", "Expecting no cookies");
  });

  await SpecialPowers.spawn(browser1, [], async function () {
    content.document.cookie = "a1=test";
  });

  await SpecialPowers.spawn(browser2, [], async function () {
    is(content.document.cookie, "a1=test", "Cookie should be set");
    content.document.cookie = "a1=other_test";
  });

  await SpecialPowers.spawn(browser1, [], async function () {
    is(content.document.cookie, "a1=other_test", "Cookie should be set");
    content.document.cookie = "a2=again";
  });

  await SpecialPowers.spawn(browser2, [], async function () {
    is(
      content.document.cookie,
      "a1=other_test; a2=again",
      "Cookie should be set"
    );
    content.document.cookie = "a1=; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
    content.document.cookie = "a2=; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
  });

  await SpecialPowers.spawn(browser1, [], async function () {
    is(content.document.cookie, "", "Cookies should be cleared");
  });

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);

  ok(true, "Got to the end of the test!");
});