summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_multiselect_tabs_close.js
blob: 2d2295c14aff5d0de0952c8e825b807775e96ce9 (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
const PREF_WARN_ON_CLOSE = "browser.tabs.warnOnCloseOtherTabs";

async function openTabMenuFor(tab) {
  let tabMenu = tab.ownerDocument.getElementById("tabContextMenu");

  let tabMenuShown = BrowserTestUtils.waitForEvent(tabMenu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(
    tab,
    { type: "contextmenu" },
    tab.ownerGlobal
  );
  await tabMenuShown;

  return tabMenu;
}

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

add_task(async function usingTabCloseButton() {
  let tab1 = await addTab();
  let tab2 = await addTab();
  let tab3 = await addTab();
  let tab4 = await addTab();

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

  await BrowserTestUtils.switchTab(gBrowser, tab1);
  await triggerClickOn(tab2, { ctrlKey: true });

  ok(tab1.multiselected, "Tab1 is multiselected");
  ok(tab2.multiselected, "Tab2 is multiselected");
  ok(!tab3.multiselected, "Tab3 is not multiselected");
  ok(!tab4.multiselected, "Tab4 is not multiselected");
  is(gBrowser.multiSelectedTabsCount, 2, "Two multiselected tabs");
  is(gBrowser.selectedTab, tab1, "Tab1 is active");

  await triggerClickOn(tab3, { ctrlKey: true });
  is(gBrowser.multiSelectedTabsCount, 3, "Three multiselected tabs");
  gBrowser.hideTab(tab3);
  is(
    gBrowser.multiSelectedTabsCount,
    2,
    "Two multiselected tabs after hiding one tab"
  );
  gBrowser.showTab(tab3);
  is(
    gBrowser.multiSelectedTabsCount,
    3,
    "Three multiselected tabs after re-showing hidden tab"
  );
  await triggerClickOn(tab3, { ctrlKey: true });
  is(
    gBrowser.multiSelectedTabsCount,
    2,
    "Two multiselected tabs after ctrl-clicking multiselected tab"
  );

  // Closing a tab which is not multiselected
  let tab4CloseBtn = tab4.closeButton;
  let tab4Closing = BrowserTestUtils.waitForTabClosing(tab4);

  tab4.mOverCloseButton = true;
  ok(tab4.mOverCloseButton, "Mouse over tab4 close button");
  tab4CloseBtn.click();
  await tab4Closing;

  is(gBrowser.selectedTab, tab1, "Tab1 is active");
  ok(tab1.multiselected, "Tab1 is multiselected");
  ok(!tab1.closing, "Tab1 is not closing");
  ok(tab2.multiselected, "Tab2 is multiselected");
  ok(!tab2.closing, "Tab2 is not closing");
  ok(!tab3.multiselected, "Tab3 is not multiselected");
  ok(!tab3.closing, "Tab3 is not closing");
  ok(tab4.closing, "Tab4 is closing");
  is(gBrowser.multiSelectedTabsCount, 2, "Two multiselected tabs");

  // Closing a selected tab
  let tab2CloseBtn = tab2.closeButton;
  tab2.mOverCloseButton = true;
  let tab1Closing = BrowserTestUtils.waitForTabClosing(tab1);
  let tab2Closing = BrowserTestUtils.waitForTabClosing(tab2);
  tab2CloseBtn.click();
  await tab1Closing;
  await tab2Closing;

  ok(tab1.closing, "Tab1 is closing");
  ok(tab2.closing, "Tab2 is closing");
  ok(!tab3.closing, "Tab3 is not closing");
  is(gBrowser.multiSelectedTabsCount, 0, "Zero multiselected tabs");

  BrowserTestUtils.removeTab(tab3);
});

add_task(async function usingTabContextMenu() {
  let tab1 = await addTab();
  let tab2 = await addTab();
  let tab3 = await addTab();
  let tab4 = await addTab();

  let menuItemCloseTab = document.getElementById("context_closeTab");
  is(gBrowser.multiSelectedTabsCount, 0, "Zero multiselected tabs");

  await BrowserTestUtils.switchTab(gBrowser, tab1);
  await triggerClickOn(tab2, { ctrlKey: true });

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

  // Check the context menu with a non-multiselected tab
  updateTabContextMenu(tab4);
  let { args } = document.l10n.getAttributes(menuItemCloseTab);
  is(args.tabCount, 1, "Close Tab item lists a single tab");

  // Check the context menu with a multiselected tab. We have to actually open
  // it (not just call `updateTabContextMenu`) in order for
  // `TabContextMenu.contextTab` to stay non-null when we click an item.
  let menu = await openTabMenuFor(tab2);
  ({ args } = document.l10n.getAttributes(menuItemCloseTab));
  is(args.tabCount, 2, "Close Tab item lists more than one tab");

  let tab1Closing = BrowserTestUtils.waitForTabClosing(tab1);
  let tab2Closing = BrowserTestUtils.waitForTabClosing(tab2);
  menu.activateItem(menuItemCloseTab);
  await tab1Closing;
  await tab2Closing;

  ok(tab1.closing, "Tab1 is closing");
  ok(tab2.closing, "Tab2 is closing");
  ok(!tab3.closing, "Tab3 is not closing");
  ok(!tab4.closing, "Tab4 is not closing");
  is(gBrowser.multiSelectedTabsCount, 0, "Zero multiselected tabs");

  BrowserTestUtils.removeTab(tab3);
  BrowserTestUtils.removeTab(tab4);
});

add_task(async function closeAllMultiselectedMiddleClick() {
  let tab1 = await addTab();
  let tab2 = await addTab();
  let tab3 = await addTab();
  let tab4 = await addTab();
  let tab5 = await addTab();
  let tab6 = await addTab();

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

  // Close currently selected tab1
  await BrowserTestUtils.switchTab(gBrowser, tab1);
  let tab1Closing = BrowserTestUtils.waitForTabClosing(tab1);
  await triggerMiddleClickOn(tab1);
  await tab1Closing;

  // Close a not currently selected tab2
  await BrowserTestUtils.switchTab(gBrowser, tab3);
  let tab2Closing = BrowserTestUtils.waitForTabClosing(tab2);
  await triggerMiddleClickOn(tab2);
  await tab2Closing;

  // Close the not multiselected middle clicked tab6
  await triggerClickOn(tab4, { ctrlKey: true });
  await triggerClickOn(tab5, { ctrlKey: true });
  ok(tab3.multiselected, "Tab3 is multiselected");
  ok(tab4.multiselected, "Tab4 is multiselected");
  ok(tab5.multiselected, "Tab5 is multiselected");
  ok(!tab6.multiselected, "Tab6 is not multiselected");
  is(gBrowser.multiSelectedTabsCount, 3, "Three multiselected tabs");

  let tab6Closing = BrowserTestUtils.waitForTabClosing(tab6);
  await triggerMiddleClickOn(tab6);
  await tab6Closing;

  // Close multiselected tabs(3, 4, 5)
  ok(tab3.multiselected, "Tab3 is multiselected");
  ok(tab4.multiselected, "Tab4 is multiselected");
  ok(tab5.multiselected, "Tab5 is multiselected");
  is(gBrowser.multiSelectedTabsCount, 3, "Three multiselected tabs");

  let tab3Closing = BrowserTestUtils.waitForTabClosing(tab3);
  let tab4Closing = BrowserTestUtils.waitForTabClosing(tab4);
  let tab5Closing = BrowserTestUtils.waitForTabClosing(tab5);
  await triggerMiddleClickOn(tab5);
  await tab3Closing;
  await tab4Closing;
  await tab5Closing;
});