summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/menubar/browser_history_recently_closed_tabs.js
blob: 246dce4db095c94821715a43a67642348f5170d5 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
/**
 * This test verifies behavior from bug 1819675:
 * https://bugzilla.mozilla.org/show_bug.cgi?id=1819675
 *
 * The recently closed tabs menu item should be enabled when there are tabs
 * closed from any window that is in the same private/non-private bucket as
 * the current window.
 */

const { SessionStoreTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/SessionStoreTestUtils.sys.mjs"
);
const triggeringPrincipal_base64 = E10SUtils.SERIALIZED_SYSTEMPRINCIPAL;
SessionStoreTestUtils.init(this, window);

async function checkMenu(window, expected) {
  await SimpleTest.promiseFocus(window);
  const historyMenubarItem = window.document.getElementById("history-menu");
  const historyMenu = window.document.getElementById("historyMenuPopup");
  const recentlyClosedTabsItem = historyMenu.querySelector("#historyUndoMenu");

  const menuShown = BrowserTestUtils.waitForEvent(historyMenu, "popupshown");
  historyMenubarItem.openMenu(true);
  info("checkMenu:, waiting for menuShown");
  await menuShown;

  Assert.equal(
    recentlyClosedTabsItem.disabled,
    expected.menuItemDisabled,
    `Recently closed tabs menu item is ${
      expected.menuItemDisabled ? "disabled" : "not disabled"
    }`
  );
  const menuHidden = BrowserTestUtils.waitForEvent(historyMenu, "popuphidden");
  historyMenu.hidePopup();
  info("checkMenu:, waiting for menuHidden");
  await menuHidden;
  info("checkMenu:, menuHidden, returning");
}

function resetClosedTabsAndWindows() {
  // Clear the lists of closed windows and tabs.
  Services.obs.notifyObservers(null, "browser:purge-session-history");
  is(SessionStore.getClosedWindowCount(), 0, "Expect 0 closed windows");
  for (const win of BrowserWindowTracker.orderedWindows) {
    is(
      SessionStore.getClosedTabCountForWindow(win),
      0,
      "Expect 0 closed tabs for this window"
    );
  }
}

add_task(async function test_recently_closed_tabs_nonprivate() {
  await resetClosedTabsAndWindows();

  const win1 = window;
  const win2 = await BrowserTestUtils.openNewBrowserWindow();
  await BrowserTestUtils.openNewForegroundTab(
    win1.gBrowser,
    "https://example.com"
  );
  // we're going to close a tab and don't want to accidentally close the window when it has 0 tabs
  await BrowserTestUtils.openNewForegroundTab(win2.gBrowser, "about:about");
  await BrowserTestUtils.openNewForegroundTab(
    win2.gBrowser,
    "https://example.org"
  );

  info("Checking the menuitem is initially disabled in both windows");
  for (let win of [win1, win2]) {
    await checkMenu(win, {
      menuItemDisabled: true,
    });
  }

  await SessionStoreTestUtils.closeTab(win2.gBrowser.selectedTab);
  is(
    SessionStore.getClosedTabCount(),
    1,
    "Expect closed tab count of 1 after closing a tab"
  );

  for (let win of [win1, win2]) {
    await checkMenu(win, {
      menuItemDisabled: false,
    });
  }

  // clean up
  info("clean up opened window");
  const sessionStoreChanged = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  await BrowserTestUtils.closeWindow(win2);
  await sessionStoreChanged;

  info("starting tab cleanup");
  while (gBrowser.tabs.length > 1) {
    await SessionStoreTestUtils.closeTab(
      gBrowser.tabs[gBrowser.tabs.length - 1]
    );
  }
  info("finished tab cleanup");
});

add_task(async function test_recently_closed_tabs_nonprivate_pref_off() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.sessionstore.closedTabsFromAllWindows", false]],
  });
  await resetClosedTabsAndWindows();

  const win1 = window;
  const win2 = await BrowserTestUtils.openNewBrowserWindow();
  await BrowserTestUtils.openNewForegroundTab(
    win1.gBrowser,
    "https://example.com"
  );
  // we're going to close a tab and don't want to accidentally close the window when it has 0 tabs
  await BrowserTestUtils.openNewForegroundTab(win2.gBrowser, "about:about");
  await BrowserTestUtils.openNewForegroundTab(
    win2.gBrowser,
    "https://example.org"
  );

  info("Checking the menuitem is initially disabled in both windows");
  for (let win of [win1, win2]) {
    await checkMenu(win, {
      menuItemDisabled: true,
    });
  }
  await SimpleTest.promiseFocus(win2);
  await SessionStoreTestUtils.closeTab(win2.gBrowser.selectedTab);
  is(
    SessionStore.getClosedTabCount(),
    1,
    "Expect closed tab count of 1 after closing a tab"
  );

  await checkMenu(win1, {
    menuItemDisabled: true,
  });
  await checkMenu(win2, {
    menuItemDisabled: false,
  });

  // clean up
  info("clean up opened window");
  const sessionStoreChanged = TestUtils.topicObserved(
    "sessionstore-closed-objects-changed"
  );
  await BrowserTestUtils.closeWindow(win2);
  await sessionStoreChanged;

  info("starting tab cleanup");
  while (gBrowser.tabs.length > 1) {
    await SessionStoreTestUtils.closeTab(
      gBrowser.tabs[gBrowser.tabs.length - 1]
    );
  }
  info("finished tab cleanup");
  SpecialPowers.popPrefEnv();
});

add_task(async function test_recently_closed_tabs_mixed_private() {
  await resetClosedTabsAndWindows();
  is(
    SessionStore.getClosedTabCount(),
    0,
    "Expect closed tab count of 0 after reset"
  );

  await BrowserTestUtils.openNewForegroundTab(window.gBrowser, "about:robots");
  await BrowserTestUtils.openNewForegroundTab(
    window.gBrowser,
    "https://example.com"
  );

  const privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await BrowserTestUtils.openNewForegroundTab(
    privateWin.gBrowser,
    "about:about"
  );
  await BrowserTestUtils.openNewForegroundTab(
    privateWin.gBrowser,
    "https://example.org"
  );

  for (let win of [window, privateWin]) {
    await checkMenu(win, {
      menuItemDisabled: true,
    });
  }

  await SessionStoreTestUtils.closeTab(privateWin.gBrowser.selectedTab);
  is(
    SessionStore.getClosedTabCount(privateWin),
    1,
    "Expect closed tab count of 1 for private windows"
  );
  is(
    SessionStore.getClosedTabCount(window),
    0,
    "Expect closed tab count of 0 for non-private windows"
  );

  // the menu should be enabled only for the private window
  await checkMenu(window, {
    menuItemDisabled: true,
  });
  await checkMenu(privateWin, {
    menuItemDisabled: false,
  });

  await resetClosedTabsAndWindows();
  await SimpleTest.promiseFocus(window);

  info("closing tab in non-private window");
  await SessionStoreTestUtils.closeTab(window.gBrowser.selectedTab);
  is(
    SessionStore.getClosedTabCount(window),
    1,
    "Expect 1 closed tab count after closing the a tab in the non-private window"
  );

  // the menu should be enabled only for the non-private window
  await checkMenu(window, {
    menuItemDisabled: false,
  });
  await checkMenu(privateWin, {
    menuItemDisabled: true,
  });

  // clean up
  info("closing private window");
  await BrowserTestUtils.closeWindow(privateWin);
  await TestUtils.waitForTick();

  info("starting tab cleanup");
  while (gBrowser.tabs.length > 1) {
    await SessionStoreTestUtils.closeTab(
      gBrowser.tabs[gBrowser.tabs.length - 1]
    );
  }
  info("finished tab cleanup");
});

add_task(async function test_recently_closed_tabs_mixed_private_pref_off() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.sessionstore.closedTabsFromAllWindows", false]],
  });
  await resetClosedTabsAndWindows();

  await BrowserTestUtils.openNewForegroundTab(window.gBrowser, "about:robots");
  await BrowserTestUtils.openNewForegroundTab(
    window.gBrowser,
    "https://example.com"
  );

  const privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await BrowserTestUtils.openNewForegroundTab(
    privateWin.gBrowser,
    "about:about"
  );
  await BrowserTestUtils.openNewForegroundTab(
    privateWin.gBrowser,
    "https://example.org"
  );

  for (let win of [window, privateWin]) {
    await checkMenu(win, {
      menuItemDisabled: true,
    });
  }

  await SimpleTest.promiseFocus(privateWin);
  await SessionStoreTestUtils.closeTab(privateWin.gBrowser.selectedTab);
  is(
    SessionStore.getClosedTabCount(privateWin),
    1,
    "Expect closed tab count of 1 for private windows"
  );
  is(
    SessionStore.getClosedTabCount(window),
    0,
    "Expect closed tab count of 0 for non-private windows"
  );

  // the menu should be enabled only for the private window
  await checkMenu(window, {
    menuItemDisabled: true,
  });
  await checkMenu(privateWin, {
    menuItemDisabled: false,
  });

  await resetClosedTabsAndWindows();
  is(
    SessionStore.getClosedTabCount(privateWin),
    0,
    "Expect 0 closed tab count after reset"
  );
  is(
    SessionStore.getClosedTabCount(window),
    0,
    "Expect 0 closed tab count after reset"
  );

  info("closing tab in non-private window");
  await SimpleTest.promiseFocus(window);
  await SessionStoreTestUtils.closeTab(window.gBrowser.selectedTab);

  // the menu should be enabled only for the non-private window
  await checkMenu(window, {
    menuItemDisabled: false,
  });
  await checkMenu(privateWin, {
    menuItemDisabled: true,
  });

  // clean up
  info("closing private window");
  await BrowserTestUtils.closeWindow(privateWin);
  await TestUtils.waitForTick();

  info("starting tab cleanup");
  while (gBrowser.tabs.length > 1) {
    await SessionStoreTestUtils.closeTab(
      gBrowser.tabs[gBrowser.tabs.length - 1]
    );
  }
  info("finished tab cleanup");
  SpecialPowers.popPrefEnv();
});

add_task(async function test_recently_closed_tabs_closed_windows() {
  // prepare window state with closed tabs from closed windows
  await SpecialPowers.pushPrefEnv({
    set: [["sessionstore.closedTabsFromClosedWindows", true]],
  });
  const closedTabUrls = ["about:robots"];
  const closedWindowState = {
    tabs: [
      {
        entries: [{ url: "about:mozilla", triggeringPrincipal_base64 }],
      },
    ],
    _closedTabs: closedTabUrls.map(url => {
      return {
        state: {
          entries: [
            {
              url,
              triggeringPrincipal_base64,
            },
          ],
        },
      };
    }),
  };
  await SessionStoreTestUtils.promiseBrowserState({
    windows: [
      {
        tabs: [
          {
            entries: [{ url: "about:mozilla", triggeringPrincipal_base64 }],
          },
        ],
      },
    ],
    _closedWindows: [closedWindowState],
  });

  // verify the recently-closed-tabs menu item is enabled
  await checkMenu(window, {
    menuItemDisabled: false,
  });

  // flip the pref
  await SpecialPowers.popPrefEnv();
  await SpecialPowers.pushPrefEnv({
    set: [["browser.sessionstore.closedTabsFromClosedWindows", false]],
  });

  // verify the recently-closed-tabs menu item is disabled
  await checkMenu(window, {
    menuItemDisabled: true,
  });

  SpecialPowers.popPrefEnv();
});