summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_tab_manager_drag.js
blob: 1dc1933e3eea9eadc3a4bf4fb88b6ed0ca3b2686 (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
/**
 * Test reordering the tabs in the Tab Manager, moving the tab between the
 * Tab Manager and tab bar.
 */

"use strict";

const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

const URL1 = "data:text/plain,tab1";
const URL2 = "data:text/plain,tab2";
const URL3 = "data:text/plain,tab3";
const URL4 = "data:text/plain,tab4";
const URL5 = "data:text/plain,tab5";

function assertOrder(order, expected, message) {
  is(
    JSON.stringify(order),
    JSON.stringify(expected),
    `The order of the tabs ${message}`
  );
}

function toIndex(url) {
  const m = url.match(/^data:text\/plain,tab(\d)/);
  if (m) {
    return parseInt(m[1]);
  }
  return 0;
}

function getOrderOfList(list) {
  return [...list.querySelectorAll("toolbaritem")].map(row => {
    const url = row.firstElementChild.tab.linkedBrowser.currentURI.spec;
    return toIndex(url);
  });
}

function getOrderOfTabs(tabs) {
  return tabs.map(tab => {
    const url = tab.linkedBrowser.currentURI.spec;
    return toIndex(url);
  });
}

async function testWithNewWindow(func) {
  Services.prefs.setBoolPref("browser.tabs.tabmanager.enabled", true);

  const newWindow =
    await BrowserTestUtils.openNewWindowWithFlushedCacheForMozSupports();

  await Promise.all([
    addTabTo(newWindow.gBrowser, URL1),
    addTabTo(newWindow.gBrowser, URL2),
    addTabTo(newWindow.gBrowser, URL3),
    addTabTo(newWindow.gBrowser, URL4),
    addTabTo(newWindow.gBrowser, URL5),
  ]);

  newWindow.gTabsPanel.init();

  const button = newWindow.document.getElementById("alltabs-button");

  const allTabsView = newWindow.document.getElementById(
    "allTabsMenu-allTabsView"
  );
  const allTabsPopupShownPromise = BrowserTestUtils.waitForEvent(
    allTabsView,
    "ViewShown"
  );
  button.click();
  await allTabsPopupShownPromise;

  await func(newWindow);

  await BrowserTestUtils.closeWindow(newWindow);

  Services.prefs.clearUserPref("browser.tabs.tabmanager.enabled");
}

add_task(async function test_reorder() {
  await testWithNewWindow(async function (newWindow) {
    Services.telemetry.clearScalars();

    const list = newWindow.document.getElementById(
      "allTabsMenu-allTabsView-tabs"
    );

    assertOrder(getOrderOfList(list), [0, 1, 2, 3, 4, 5], "before reorder");

    let rows;
    rows = list.querySelectorAll("toolbaritem");
    EventUtils.synthesizeDrop(
      rows[3],
      rows[1],
      null,
      "move",
      newWindow,
      newWindow,
      { clientX: 0, clientY: 0 }
    );

    assertOrder(getOrderOfList(list), [0, 3, 1, 2, 4, 5], "after moving up");

    rows = list.querySelectorAll("toolbaritem");
    EventUtils.synthesizeDrop(
      rows[1],
      rows[5],
      null,
      "move",
      newWindow,
      newWindow,
      { clientX: 0, clientY: 0 }
    );

    assertOrder(getOrderOfList(list), [0, 1, 2, 4, 3, 5], "after moving down");

    rows = list.querySelectorAll("toolbaritem");
    EventUtils.synthesizeDrop(
      rows[4],
      rows[3],
      null,
      "move",
      newWindow,
      newWindow,
      { clientX: 0, clientY: 0 }
    );

    assertOrder(
      getOrderOfList(list),
      [0, 1, 2, 3, 4, 5],
      "after moving up again"
    );

    let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
    TelemetryTestUtils.assertScalar(
      scalars,
      "browser.ui.interaction.all_tabs_panel_dragstart_tab_event_count",
      3
    );
  });
});

function tabOf(row) {
  return row.firstElementChild.tab;
}

add_task(async function test_move_to_tab_bar() {
  await testWithNewWindow(async function (newWindow) {
    Services.telemetry.clearScalars();

    const list = newWindow.document.getElementById(
      "allTabsMenu-allTabsView-tabs"
    );

    assertOrder(getOrderOfList(list), [0, 1, 2, 3, 4, 5], "before reorder");

    let rows;
    rows = list.querySelectorAll("toolbaritem");
    EventUtils.synthesizeDrop(
      rows[3],
      tabOf(rows[1]),
      null,
      "move",
      newWindow,
      newWindow,
      { clientX: 0, clientY: 0 }
    );

    assertOrder(
      getOrderOfList(list),
      [0, 3, 1, 2, 4, 5],
      "after moving up with tab bar"
    );

    rows = list.querySelectorAll("toolbaritem");
    EventUtils.synthesizeDrop(
      rows[1],
      tabOf(rows[4]),
      null,
      "move",
      newWindow,
      newWindow,
      { clientX: 0, clientY: 0 }
    );

    assertOrder(
      getOrderOfList(list),
      [0, 1, 2, 3, 4, 5],
      "after moving down with tab bar"
    );

    let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
    TelemetryTestUtils.assertScalar(
      scalars,
      "browser.ui.interaction.all_tabs_panel_dragstart_tab_event_count",
      2
    );
  });
});

add_task(async function test_move_to_different_tab_bar() {
  const newWindow2 =
    await BrowserTestUtils.openNewWindowWithFlushedCacheForMozSupports();

  await testWithNewWindow(async function (newWindow) {
    Services.telemetry.clearScalars();

    const list = newWindow.document.getElementById(
      "allTabsMenu-allTabsView-tabs"
    );

    assertOrder(
      getOrderOfList(list),
      [0, 1, 2, 3, 4, 5],
      "before reorder in newWindow"
    );
    assertOrder(
      getOrderOfTabs(newWindow2.gBrowser.tabs),
      [0],
      "before reorder in newWindow2"
    );

    let rows;
    rows = list.querySelectorAll("toolbaritem");
    EventUtils.synthesizeDrop(
      rows[3],
      newWindow2.gBrowser.tabs[0],
      null,
      "move",
      newWindow,
      newWindow2,
      { clientX: 0, clientY: 0 }
    );

    assertOrder(
      getOrderOfList(list),
      [0, 1, 2, 4, 5],
      "after moving to other window in newWindow"
    );

    assertOrder(
      getOrderOfTabs(newWindow2.gBrowser.tabs),
      [3, 0],
      "after moving to other window in newWindow2"
    );

    let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
    TelemetryTestUtils.assertScalar(
      scalars,
      "browser.ui.interaction.all_tabs_panel_dragstart_tab_event_count",
      1
    );
  });

  await BrowserTestUtils.closeWindow(newWindow2);
});