summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_forget_closed_tab_window_byId.js
blob: bcd3c146968940e1fad26125ad1d58b34fdb624c (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
/* 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/. */

const TEST_URLS = [
  "http://mochi.test:8888/browser/",
  "https://www.example.com/",
  "https://example.net/",
  "https://example.org/",
];

let window1, privateWin;
let tab1, privateTab;

async function openTab(window, url) {
  let tab = BrowserTestUtils.addTab(window.gBrowser, url);
  await promiseBrowserLoaded(tab.linkedBrowser, true, url);
  return tab;
}

add_setup(async function testSetup() {
  // prepare some closed state
  window1 = await BrowserTestUtils.openNewBrowserWindow();
  await openAndCloseTab(window1, TEST_URLS[0]);
  // open another non-empty tab
  tab1 = await openTab(window1, TEST_URLS[1]);

  // open a private window
  privateWin = await BrowserTestUtils.openNewBrowserWindow({ private: true });
  // open another non-empty tab
  privateTab = await openTab(privateWin, TEST_URLS[2]);
  await openAndCloseTab(privateWin, TEST_URLS[3]);

  registerCleanupFunction(async () => {
    let windows = [];
    for (let win of BrowserWindowTracker.orderedWindows) {
      if (win != window) {
        windows.push(win);
      }
    }
    await Promise.all(windows.map(BrowserTestUtils.closeWindow));
    Services.obs.notifyObservers(null, "browser:purge-session-history");
  });
});

add_task(async function closeAndForgetTabs() {
  // sanity check recently-closed tab data
  Assert.equal(
    SessionStore.getClosedTabCount(window1),
    1,
    "Only one tab closed in non-private windows"
  );
  Assert.equal(
    SessionStore.getClosedTabCount(privateWin),
    1,
    "Only one tab closed in non-private windows"
  );

  // sanity check open tabs
  Assert.equal(window1.gBrowser.tabs.length, 2, "Expected number of tabs open");
  Assert.equal(
    privateWin.gBrowser.tabs.length,
    2,
    "Expected number of tabs open"
  );

  let closedTabs = SessionStore.getClosedTabData(window1); // only non-private closed tabs
  Assert.equal(typeof closedTabs[0].closedId, "number", "closedId is a number");
  let sessionStoreUpdated = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  SessionStore.forgetClosedTabById(closedTabs[0].closedId);
  await sessionStoreUpdated;
  Assert.equal(
    SessionStore.getClosedTabCount(window1),
    0,
    "There's no more records of closed tabs"
  );

  closedTabs = SessionStore.getClosedTabData(privateWin); // only private closed tabs
  Assert.equal(typeof closedTabs[0].closedId, "number", "closedId is a number");

  Assert.throws(
    () => {
      SessionStore.forgetClosedTabById(closedTabs[0].closedId, {
        includePrivate: false,
      });
    },
    /Invalid closedId/,
    "forgetClosedWindowById should throw an exception when trying to match a closedId from a private window in non-private windows"
  );

  Assert.equal(
    SessionStore.getClosedTabCount(privateWin),
    1,
    "Still one tab closed in non-private windows"
  );

  sessionStoreUpdated = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  SessionStore.forgetClosedTabById(closedTabs[0].closedId);
  await sessionStoreUpdated;
  Assert.equal(
    SessionStore.getClosedTabCount(privateWin),
    0,
    "There's no more records of private closed tabs"
  );
});

add_task(async function closeAndForgetWindows() {
  let closedWindows;
  await BrowserTestUtils.closeWindow(window1);
  closedWindows = SessionStore.getClosedWindowData();
  Assert.equal(
    closedWindows.length,
    1,
    "There's one record for closed windows"
  );

  let sessionStoreUpdated = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  let closedWindowId = closedWindows[0].closedId;
  SessionStore.forgetClosedWindowById(closedWindowId);
  await sessionStoreUpdated;
  Assert.equal(
    SessionStore.getClosedWindowCount(),
    0,
    "There's no more records of closed windows"
  );

  await BrowserTestUtils.closeWindow(privateWin);
  await TestUtils.waitForTick();
  Assert.equal(
    SessionStore.getClosedWindowCount(),
    0,
    "Closing the private window didn't add a closed window entry"
  );
  // check a non-matching closedId throws an exception
  Assert.throws(
    () => {
      SessionStore.forgetClosedWindowById(closedWindowId);
    },
    /Invalid closedId/,
    "forgetClosedWindowById should throw an exception when given an closedId that doesnt match any closed window"
  );
});