summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_closed_tabs_windows.js
blob: 203c8c8d965f28752ac545eb5521c2e9c8b397d3 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const ORIG_STATE = ss.getBrowserState();

const multiWindowState = {
  windows: [
    {
      tabs: [
        {
          entries: [
            {
              url: "https://example.com#win-0-tab-0",
              triggeringPrincipal_base64,
            },
          ],
        },
      ],
      _closedTabs: [
        {
          state: {
            entries: [
              {
                url: "https://example.com#win-0-closed-0",
                triggeringPrincipal_base64,
              },
            ],
          },
        },
        {
          state: {
            entries: [
              {
                url: "https://example.com#win-0-closed-1",
                triggeringPrincipal_base64,
              },
            ],
          },
        },
      ],
      selected: 1,
    },
    {
      tabs: [
        {
          entries: [
            {
              url: "https://example.org#win-1-tab-0",
              triggeringPrincipal_base64,
            },
          ],
        },
        {
          entries: [
            {
              url: "https://example.org#win-1-tab-1",
              triggeringPrincipal_base64,
            },
          ],
        },
      ],
      _closedTabs: [
        {
          state: {
            entries: [
              {
                url: "https://example.org#win-1-closed-0",
                triggeringPrincipal_base64,
              },
            ],
          },
        },
      ],
      selected: 1,
    },
  ],
  _closedWindows: [
    {
      tabs: [
        {
          entries: [
            {
              url: "https://example.org#closedWin-0-tab-0",
              triggeringPrincipal_base64,
            },
          ],
        },
      ],
      _closedTabs: [
        {
          state: {
            entries: [
              {
                url: "https://example.org##closedWin-0-closed-0",
                triggeringPrincipal_base64,
              },
            ],
          },
        },
      ],
    },
  ],
};

add_setup(async function testSetup() {
  await SessionStoreTestUtils.promiseBrowserState(multiWindowState);
});

add_task(async function test_ClosedTabMethods() {
  let sessionStoreUpdated;
  const browserWindows = BrowserWindowTracker.orderedWindows;
  Assert.equal(
    browserWindows.length,
    multiWindowState.windows.length,
    `We expect ${multiWindowState.windows} open browser windows`
  );
  info(
    `After setup, current tab URI: ${browserWindows[0].gBrowser.currentURI.spec}`
  );
  Assert.equal(
    browserWindows[0].gBrowser.currentURI.spec,
    "https://example.com/#win-0-tab-0",
    "The first tab of the first window we restored is current"
  );

  for (let idx = 0; idx < browserWindows.length; idx++) {
    const win = browserWindows[idx];
    const winData = multiWindowState.windows[idx];
    info(`window ${idx}: ${win.gBrowser.selectedBrowser.currentURI.spec}`);
    Assert.equal(
      winData._closedTabs.length,
      SessionStore.getClosedTabDataForWindow(win).length,
      `getClosedTabDataForWindow() for open window ${idx} returned the expected number of objects`
    );
  }

  let closedCount;
  closedCount = SessionStore.getClosedTabCountForWindow(browserWindows[0]);
  Assert.equal(2, closedCount, "2 closed tab for this window");

  closedCount = SessionStore.getClosedTabCountForWindow(browserWindows[1]);
  Assert.equal(1, closedCount, "1 closed tab for this window");

  closedCount = SessionStore.getClosedTabCount();
  // 3 closed tabs from open windows, 1 closed tab from the closed window
  Assert.equal(4, closedCount, "4 closed tab for all windows");

  let allWindowsClosedTabs = SessionStore.getClosedTabData();
  Assert.equal(
    SessionStore.getClosedTabCount({ closedTabsFromClosedWindows: false }),
    allWindowsClosedTabs.length,
    "getClosedTabData returned the correct number of entries"
  );
  for (let tabData of allWindowsClosedTabs) {
    Assert.ok(tabData.sourceWindowId, "each tab has a sourceWindowId property");
  }

  closedCount = SessionStore.getClosedTabCountFromClosedWindows();
  Assert.equal(1, closedCount, "1 closed tabs from closed windows");

  // ***********************************
  info("check with the all-windows pref off");
  await SpecialPowers.pushPrefEnv({
    set: [["browser.sessionstore.closedTabsFromAllWindows", false]],
  });

  info("With this pref off, the closed tab count is that of the top window");
  closedCount = SessionStore.getClosedTabCount({
    closedTabsFromClosedWindows: false,
  }); // Equivalent to SS.getClosedTabCountForWindow(browserWindows[0]),
  Assert.equal(closedCount, 2, `2 closed tabs from the top (first) window`);
  Assert.equal(
    SessionStore.getClosedTabCount(), // includes closed tabs from closed windows, but only the top open window
    3,
    `3 closed tabs when counting the top window and the closed window`
  );

  allWindowsClosedTabs = SessionStore.getClosedTabData(); // Equivalent to SS.getClosedTabDataForWindow(browserWindows[0]),
  Assert.equal(
    allWindowsClosedTabs.length,
    2,
    "getClosedTabData returned the number of entries for the top window"
  );
  SpecialPowers.popPrefEnv();
  await TestUtils.waitForTick();
  //
  // ***********************************

  closedCount = SessionStore.getClosedTabCount();
  Assert.equal(
    4,
    closedCount,
    "Sanity-check, with the pref on, we're back to 4 closed tabs"
  );

  // ***********************************
  info("check with the closed-tabs-from-closed-windows pref off");
  await SpecialPowers.pushPrefEnv({
    set: [["browser.sessionstore.closedTabsFromClosedWindows", false]],
  });

  info(
    "With this pref off, the closed tab count is that of only the only windows"
  );
  closedCount = SessionStore.getClosedTabCount(); // Equivalent to SS.getClosedTabCountForWindow(browserWindows[0]),
  Assert.equal(3, closedCount, `3 closed tabs from the open windows`);

  allWindowsClosedTabs = SessionStore.getClosedTabData();
  Assert.equal(
    allWindowsClosedTabs.length,
    3,
    "getClosedTabData returned the number of entries for the top window"
  );
  SpecialPowers.popPrefEnv();
  await TestUtils.waitForTick();
  //
  // ***********************************

  info("forget the closed tab from the closed window");
  sessionStoreUpdated = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  SessionStore.forgetClosedTab(
    { sourceClosedId: SessionStore.getClosedWindowData()[0].closedId },
    0
  );
  await sessionStoreUpdated;

  closedCount = SessionStore.getClosedTabCountFromClosedWindows();
  Assert.equal(
    0,
    closedCount,
    "0 closed tabs from closed windows after forgetting them"
  );
  closedCount = SessionStore.getClosedTabCount();
  Assert.equal(
    3,
    closedCount,
    "3 closed tabs now that the closed tab from the closed window is forgotten"
  );

  info("restore one of the closed tabs from an open window");
  sessionStoreUpdated = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );

  SessionStore.undoCloseTab(browserWindows[0], 0);
  await sessionStoreUpdated;

  Assert.equal(
    1,
    SessionStore.getClosedTabCountForWindow(browserWindows[0]),
    "Now theres one closed tab in the first window"
  );
  Assert.equal(
    1,
    SessionStore.getClosedTabCountForWindow(browserWindows[1]),
    "Theres still one closed tab in the 2nd window"
  );

  Assert.equal(
    2,
    SessionStore.getClosedTabCount(),
    "Theres a total for 2 closed tabs"
  );

  // Bug 1836198 - sometimes this reports 1 not 2.
  Assert.equal(
    2,
    SessionStore.getClosedTabData().length,
    "We get the right number of tab entries from getClosedTabData()"
  );

  info("forget the last closed tab in the first window");
  sessionStoreUpdated = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  SessionStore.forgetClosedTab(browserWindows[0], 0);
  await sessionStoreUpdated;

  Assert.equal(
    0,
    SessionStore.getClosedTabCountForWindow(browserWindows[0]),
    "The first window has 0 closed tabs after forgetting the last tab"
  );
  Assert.equal(
    1,
    SessionStore.getClosedTabCountForWindow(browserWindows[1]),
    "Theres still one closed tab in the 2nd window"
  );

  Assert.equal(
    1,
    SessionStore.getClosedTabCount(),
    "Theres a total of one closed tabs"
  );
  Assert.equal(
    1,
    SessionStore.getClosedTabData().length,
    "We get the right number of tab entries from getClosedTabData()"
  );

  info(
    "Close the 2nd window. This makes its 1 closed tab a 'closed tab from closed window'"
  );
  await promiseAllButPrimaryWindowClosed();

  Assert.equal(
    0,
    SessionStore.getClosedTabCountForWindow(browserWindows[0]),
    "Closed tab count is unchanged after closing the other browser window"
  );

  Assert.equal(
    0,
    SessionStore.getClosedTabCount({ closedTabsFromClosedWindows: false }),
    "Theres now 0 closed tabs from open windows after closing the other browser window which had the last one"
  );

  Assert.equal(
    1,
    SessionStore.getClosedTabCount({ closedTabsFromClosedWindows: true }),
    "Theres now 1 closed tabs including closed windows after closing the other browser window which had the last one"
  );

  Assert.equal(
    0,
    SessionStore.getClosedTabData().length,
    "We get the right number of tab entries from getClosedTabData()"
  );

  Assert.equal(
    1,
    SessionStore.getClosedTabCountFromClosedWindows(),
    "There's 1 closed tabs from closed windows"
  );

  // Cleanup.
  await promiseBrowserState(ORIG_STATE);
});