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

"use strict";

const { _LastSession, _lastClosedActions } = ChromeUtils.importESModule(
  "resource:///modules/sessionstore/SessionStore.sys.mjs"
);

async function testLastClosedActionsEntries() {
  SessionStore.resetLastClosedActions();

  let win2 = await BrowserTestUtils.openNewBrowserWindow();
  BrowserTestUtils.startLoadingURIString(
    win2.gBrowser.selectedBrowser,
    "https://www.mozilla.org/"
  );

  await BrowserTestUtils.browserLoaded(win2.gBrowser.selectedBrowser);
  await openAndCloseTab(win2, "https://example.org/");

  Assert.equal(
    SessionStore.lastClosedActions.length,
    1,
    `1 closed action has been recorded`
  );

  await BrowserTestUtils.closeWindow(win2);

  Assert.equal(
    SessionStore.lastClosedActions.length,
    2,
    `2 closed actions have been recorded`
  );
}

add_setup(() => {
  forgetClosedTabs(window);
  registerCleanupFunction(() => {
    forgetClosedTabs(window);
  });

  // needed for verify tests so that forgetting tabs isn't recorded
  SessionStore.resetLastClosedActions();
});

/**
 * Tests that if the user invokes restoreLastClosedTabOrWindowOrSession it will
 * result in either the last session will be restored, if possible, the last
 * tab (or multiple selected tabs) that was closed is reopened, or the last
 * window that is closed is reopened.
 */
add_task(async function test_undo_last_action() {
  const state = {
    windows: [
      {
        tabs: [
          {
            entries: [
              {
                title: "example.org",
                url: "https://example.org/",
                triggeringPrincipal_base64,
              },
            ],
          },
          {
            entries: [
              {
                title: "example.com",
                url: "https://example.com/",
                triggeringPrincipal_base64,
              },
            ],
          },
          {
            entries: [
              {
                title: "mozilla",
                url: "https://www.mozilla.org/",
                triggeringPrincipal_base64,
              },
            ],
          },
        ],
        selected: 3,
      },
    ],
  };

  _LastSession.setState(state);

  let sessionRestored = promiseSessionStoreLoads(3 /* total restored tabs */);
  restoreLastClosedTabOrWindowOrSession();
  await sessionRestored;
  Assert.equal(
    window.gBrowser.tabs.length,
    3,
    "Window has three tabs after session is restored"
  );

  // open and close a window, then reopen it
  let win2 = await BrowserTestUtils.openNewBrowserWindow();
  Assert.equal(win2.gBrowser.tabs.length, 1, "Second window has one open tab");
  BrowserTestUtils.startLoadingURIString(
    win2.gBrowser.selectedBrowser,
    "https://example.com/"
  );
  await BrowserTestUtils.browserLoaded(win2.gBrowser.selectedBrowser);
  await BrowserTestUtils.closeWindow(win2);
  let restoredWinPromise = BrowserTestUtils.waitForNewWindow({
    url: "https://example.com/",
  });
  restoreLastClosedTabOrWindowOrSession();
  let restoredWin = await restoredWinPromise;
  Assert.equal(
    restoredWin.gBrowser.tabs.length,
    1,
    "First tab in the second window has been reopened"
  );
  await BrowserTestUtils.closeWindow(restoredWin);
  SessionStore.forgetClosedWindow(restoredWin.index);
  restoreLastClosedTabOrWindowOrSession();

  // close one tab and reopen it
  BrowserTestUtils.removeTab(window.gBrowser.tabs[2]);
  Assert.equal(window.gBrowser.tabs.length, 2, "Window has two open tabs");
  restoreLastClosedTabOrWindowOrSession();
  Assert.equal(
    window.gBrowser.tabs.length,
    3,
    "Window now has three open tabs"
  );

  // select 2 tabs and close both via the 'close 2 tabs' context menu option
  let tab2 = window.gBrowser.tabs[1];
  let tab3 = window.gBrowser.tabs[2];
  await triggerClickOn(tab2, { ctrlKey: true });
  Assert.equal(tab2.multiselected, true);
  Assert.equal(tab3.multiselected, true);

  let menu = await openTabMenuFor(tab3);
  let menuItemCloseTab = document.getElementById("context_closeTab");
  let tab2Closing = BrowserTestUtils.waitForTabClosing(tab2);
  let tab3Closing = BrowserTestUtils.waitForTabClosing(tab3);
  menu.activateItem(menuItemCloseTab);
  await tab2Closing;
  await tab3Closing;
  Assert.equal(window.gBrowser.tabs[0].selected, true);
  await TestUtils.waitForCondition(() => window.gBrowser.tabs.length == 1);
  Assert.equal(
    window.gBrowser.tabs.length,
    1,
    "Window now has one open tab after closing two multi-selected tabs"
  );

  // ensure both tabs are reopened with a single click
  restoreLastClosedTabOrWindowOrSession();
  Assert.equal(
    window.gBrowser.tabs.length,
    3,
    "Window now has three open tabs after reopening closed tabs"
  );

  // close one tab and forget it - it should not be reopened
  BrowserTestUtils.removeTab(window.gBrowser.tabs[2]);
  Assert.equal(window.gBrowser.tabs.length, 2, "Window has two open tabs");
  SessionStore.forgetClosedTab(window, 0);
  restoreLastClosedTabOrWindowOrSession();
  Assert.equal(
    window.gBrowser.tabs.length,
    2,
    "Window still has two open tabs"
  );

  gBrowser.removeAllTabsBut(gBrowser.tabs[0]);
});

add_task(async function test_forget_closed_window() {
  await testLastClosedActionsEntries();
  SessionStore.forgetClosedWindow();

  // both the forgotten window and its closed tab has been removed from the list
  Assert.ok(
    !SessionStore.lastClosedActions.length,
    `0 closed actions have been recorded`
  );
});

add_task(async function test_user_clears_history() {
  await testLastClosedActionsEntries();
  Services.obs.notifyObservers(null, "browser:purge-session-history");

  // both the forgotten window and its closed tab has been removed from the list
  Assert.ok(
    !SessionStore.lastClosedActions.length,
    `0 closed actions have been recorded`
  );
});

/**
 * It the browser has restarted and the closed actions list is empty, we
 * should fallback to re-opening the last closed tab if one exists.
 */
add_task(async function test_reopen_last_tab_if_no_closed_actions() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "about:blank",
    },
    async browser => {
      const TEST_URL = "https://example.com/";
      let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
      let update = BrowserTestUtils.waitForSessionStoreUpdate(tab);
      BrowserTestUtils.removeTab(tab);
      await update;

      SessionStore.resetLastClosedActions();

      let promiseTab = BrowserTestUtils.waitForNewTab(gBrowser, TEST_URL);
      restoreLastClosedTabOrWindowOrSession();
      let newTab = await promiseTab;
      Assert.equal(newTab.linkedBrowser.currentURI.spec, TEST_URL);
    }
  );
});

/**
 * It the browser has restarted and the closed actions list is empty, and
 * no closed tabs exist for the window, we should fallback to re-opening
 * the last session if one exists.
 */
add_task(async function test_reopen_last_session_if_no_closed_actions() {
  gBrowser.removeAllTabsBut(gBrowser.tabs[0]);
  await TabStateFlusher.flushWindow(window);

  forgetClosedTabs(window);

  const state = {
    windows: [
      {
        tabs: [
          {
            entries: [
              {
                title: "example.org",
                url: "https://example.org/",
                triggeringPrincipal_base64,
              },
            ],
          },
          {
            entries: [
              {
                title: "example.com",
                url: "https://example.com/",
                triggeringPrincipal_base64,
              },
            ],
          },
          {
            entries: [
              {
                title: "mozilla",
                url: "https://www.mozilla.org/",
                triggeringPrincipal_base64,
              },
            ],
          },
        ],
        selected: 3,
      },
    ],
  };

  _LastSession.setState(state);
  SessionStore.resetLastClosedActions();

  let sessionRestored = promiseSessionStoreLoads(3 /* total restored tabs */);
  restoreLastClosedTabOrWindowOrSession();
  await sessionRestored;
  Assert.equal(gBrowser.tabs.length, 4, "Got the expected number of tabs");
  gBrowser.removeAllTabsBut(gBrowser.tabs[0]);
});