summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_cookies_legacy.js
blob: 087c2d516f5d3fe1121aeff2716820cb6bb38311 (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
"use strict";

function createTestState() {
  let r = Math.round(Math.random() * 100000);

  let cookie = {
    host: "http://example.com",
    path: "/",
    name: `name${r}`,
    value: `value${r}`,
  };

  let state = {
    windows: [
      {
        tabs: [
          { entries: [{ url: "about:robots", triggeringPrincipal_base64 }] },
        ],
        cookies: [cookie],
      },
    ],
  };

  return [state, cookie];
}

function waitForNewCookie({ host, name, value }) {
  info(`waiting for cookie ${name}=${value} from ${host}...`);

  return new Promise(resolve => {
    Services.obs.addObserver(function observer(subj, topic, data) {
      if (data != "added") {
        return;
      }

      let cookie = subj.QueryInterface(Ci.nsICookie);
      if (cookie.host == host && cookie.name == name && cookie.value == value) {
        ok(true, "cookie added by the cookie service");
        Services.obs.removeObserver(observer, topic);
        resolve();
      }
    }, "session-cookie-changed");
  });
}

// Setup and cleanup.
add_task(async function test_setup() {
  Services.cookies.removeAll();

  registerCleanupFunction(() => {
    Services.cookies.removeAll();
  });
});

// Test that calling ss.setWindowState() with a legacy state object that
// contains cookies in the window state restores session cookies properly.
add_task(async function test_window() {
  let [state, cookie] = createTestState();
  let win = await promiseNewWindowLoaded();

  let promiseCookie = waitForNewCookie(cookie);
  ss.setWindowState(win, JSON.stringify(state), true);
  await promiseCookie;

  await BrowserTestUtils.closeWindow(win);
});

// Test that calling ss.setBrowserState() with a legacy state object that
// contains cookies in the window state restores session cookies properly.
add_task(async function test_browser() {
  let backupState = ss.getBrowserState();
  let [state, cookie] = createTestState();
  await Promise.all([waitForNewCookie(cookie), promiseBrowserState(state)]);
  await promiseBrowserState(backupState);
});