summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_undo_close_tabs.js
blob: e4e9da5d5dc97fc3137fdeb5e086a19d30c25363 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const PREF_WARN_ON_CLOSE = "browser.tabs.warnOnCloseOtherTabs";

add_task(async function setPref() {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_WARN_ON_CLOSE, false]],
  });
});

add_task(async function withMultiSelectedTabs() {
  let initialTab = gBrowser.selectedTab;
  let tab1 = await addTab("https://example.com/1");
  let tab2 = await addTab("https://example.com/2");
  let tab3 = await addTab("https://example.com/3");
  let tab4 = await addTab("https://example.com/4");

  is(gBrowser.multiSelectedTabsCount, 0, "Zero multiselected tabs");

  gBrowser.selectedTab = tab2;
  await triggerClickOn(tab4, { shiftKey: true });

  ok(!initialTab.multiselected, "InitialTab is not multiselected");
  ok(!tab1.multiselected, "Tab1 is not multiselected");
  ok(tab2.multiselected, "Tab2 is multiselected");
  ok(tab3.multiselected, "Tab3 is multiselected");
  ok(tab4.multiselected, "Tab4 is multiselected");
  is(gBrowser.multiSelectedTabsCount, 3, "Three multiselected tabs");

  gBrowser.removeMultiSelectedTabs();
  await TestUtils.waitForCondition(
    () => SessionStore.getLastClosedTabCount(window) == 3,
    "wait for the multi selected tabs to close in SessionStore"
  );
  is(
    SessionStore.getLastClosedTabCount(window),
    3,
    "SessionStore should know how many tabs were just closed"
  );

  undoCloseTab();
  await TestUtils.waitForCondition(
    () => gBrowser.tabs.length == 5,
    "wait for the tabs to reopen"
  );

  is(
    SessionStore.getLastClosedTabCount(window),
    SessionStore.getClosedTabCountForWindow(window) ? 1 : 0,
    "LastClosedTabCount should be reset"
  );

  info("waiting for the browsers to finish loading");
  // Check that the tabs are restored in the correct order
  for (let tabId of [2, 3, 4]) {
    let browser = gBrowser.tabs[tabId].linkedBrowser;
    await ContentTask.spawn(browser, tabId, async aTabId => {
      await ContentTaskUtils.waitForCondition(() => {
        return (
          content?.document?.readyState == "complete" &&
          content?.document?.location.href == "https://example.com/" + aTabId
        );
      }, "waiting for tab " + aTabId + " to load");
    });
  }

  gBrowser.removeAllTabsBut(initialTab);
});

add_task(async function withBothGroupsAndTab() {
  let initialTab = gBrowser.selectedTab;
  let tab1 = await addTab("https://example.com/1");
  let tab2 = await addTab("https://example.com/2");
  let tab3 = await addTab("https://example.com/3");

  gBrowser.selectedTab = tab2;
  await triggerClickOn(tab3, { shiftKey: true });

  ok(!initialTab.multiselected, "InitialTab is not multiselected");
  ok(!tab1.multiselected, "Tab1 is not multiselected");
  ok(tab2.multiselected, "Tab2 is multiselected");
  ok(tab3.multiselected, "Tab3 is multiselected");
  is(gBrowser.multiSelectedTabsCount, 2, "Two multiselected tabs");

  gBrowser.removeMultiSelectedTabs();
  await TestUtils.waitForCondition(
    () => gBrowser.tabs.length == 2,
    "wait for the multiselected tabs to close"
  );

  is(
    SessionStore.getLastClosedTabCount(window),
    2,
    "SessionStore should know how many tabs were just closed"
  );

  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  let tab4 = await addTab("http://example.com/4");

  is(
    SessionStore.getLastClosedTabCount(window),
    2,
    "LastClosedTabCount should be the same"
  );

  gBrowser.removeTab(tab4);

  await TestUtils.waitForCondition(
    () => SessionStore.getLastClosedTabCount(window) == 1,
    "wait for the tab to close in SessionStore"
  );

  let count = 3;
  for (let i = 0; i < 3; i++) {
    is(
      SessionStore.getLastClosedTabCount(window),
      1,
      "LastClosedTabCount should be one"
    );
    undoCloseTab();

    await TestUtils.waitForCondition(
      () => gBrowser.tabs.length == count,
      "wait for the tabs to reopen"
    );
    count++;
  }

  gBrowser.removeAllTabsBut(initialTab);
});

add_task(async function withCloseTabsToTheRight() {
  let initialTab = gBrowser.selectedTab;
  let tab1 = await addTab("https://example.com/1");
  await addTab("https://example.com/2");
  await addTab("https://example.com/3");
  await addTab("https://example.com/4");

  gBrowser.removeTabsToTheEndFrom(tab1);
  await TestUtils.waitForCondition(
    () => gBrowser.tabs.length == 2,
    "wait for the multiselected tabs to close"
  );
  is(
    SessionStore.getLastClosedTabCount(window),
    3,
    "SessionStore should know how many tabs were just closed"
  );

  undoCloseTab();
  await TestUtils.waitForCondition(
    () => gBrowser.tabs.length == 5,
    "wait for the tabs to reopen"
  );
  info("waiting for the browsers to finish loading");
  // Check that the tabs are restored in the correct order
  for (let tabId of [2, 3, 4]) {
    let browser = gBrowser.tabs[tabId].linkedBrowser;
    ContentTask.spawn(browser, tabId, async aTabId => {
      await ContentTaskUtils.waitForCondition(() => {
        return (
          content?.document?.readyState == "complete" &&
          content?.document?.location.href == "https://example.com/" + aTabId
        );
      }, "waiting for tab " + aTabId + " to load");
    });
  }

  gBrowser.removeAllTabsBut(initialTab);
});