summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_tab_drag_drop_perwindow.js
blob: de4e17b97d2b57f8289c8b1d6c6120043af56210 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

requestLongerTimeout(2);

const EVENTUTILS_URL =
  "chrome://mochikit/content/tests/SimpleTest/EventUtils.js";
var EventUtils = {};

Services.scriptloader.loadSubScript(EVENTUTILS_URL, EventUtils);

/**
 * Tests that tabs from Private Browsing windows cannot be dragged
 * into non-private windows, and vice-versa.
 */
add_task(async function test_dragging_private_windows() {
  let normalWin = await BrowserTestUtils.openNewBrowserWindow();
  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });

  let normalTab = await BrowserTestUtils.openNewForegroundTab(
    normalWin.gBrowser
  );
  let privateTab = await BrowserTestUtils.openNewForegroundTab(
    privateWin.gBrowser
  );

  let effect = EventUtils.synthesizeDrop(
    normalTab,
    privateTab,
    [[{ type: TAB_DROP_TYPE, data: normalTab }]],
    null,
    normalWin,
    privateWin
  );
  is(
    effect,
    "none",
    "Should not be able to drag a normal tab to a private window"
  );

  effect = EventUtils.synthesizeDrop(
    privateTab,
    normalTab,
    [[{ type: TAB_DROP_TYPE, data: privateTab }]],
    null,
    privateWin,
    normalWin
  );
  is(
    effect,
    "none",
    "Should not be able to drag a private tab to a normal window"
  );

  normalWin.gBrowser.swapBrowsersAndCloseOther(normalTab, privateTab);
  is(
    normalWin.gBrowser.tabs.length,
    2,
    "Prevent moving a normal tab to a private tabbrowser"
  );
  is(
    privateWin.gBrowser.tabs.length,
    2,
    "Prevent accepting a normal tab in a private tabbrowser"
  );

  privateWin.gBrowser.swapBrowsersAndCloseOther(privateTab, normalTab);
  is(
    privateWin.gBrowser.tabs.length,
    2,
    "Prevent moving a private tab to a normal tabbrowser"
  );
  is(
    normalWin.gBrowser.tabs.length,
    2,
    "Prevent accepting a private tab in a normal tabbrowser"
  );

  await BrowserTestUtils.closeWindow(normalWin);
  await BrowserTestUtils.closeWindow(privateWin);
});

/**
 * Tests that tabs from e10s windows cannot be dragged into non-e10s
 * windows, and vice-versa.
 */
add_task(async function test_dragging_e10s_windows() {
  if (!gMultiProcessBrowser) {
    return;
  }

  let remoteWin = await BrowserTestUtils.openNewBrowserWindow({ remote: true });
  let nonRemoteWin = await BrowserTestUtils.openNewBrowserWindow({
    remote: false,
    fission: false,
  });

  let remoteTab = await BrowserTestUtils.openNewForegroundTab(
    remoteWin.gBrowser
  );
  let nonRemoteTab = await BrowserTestUtils.openNewForegroundTab(
    nonRemoteWin.gBrowser
  );

  let effect = EventUtils.synthesizeDrop(
    remoteTab,
    nonRemoteTab,
    [[{ type: TAB_DROP_TYPE, data: remoteTab }]],
    null,
    remoteWin,
    nonRemoteWin
  );
  is(
    effect,
    "none",
    "Should not be able to drag a remote tab to a non-e10s window"
  );

  effect = EventUtils.synthesizeDrop(
    nonRemoteTab,
    remoteTab,
    [[{ type: TAB_DROP_TYPE, data: nonRemoteTab }]],
    null,
    nonRemoteWin,
    remoteWin
  );
  is(
    effect,
    "none",
    "Should not be able to drag a non-remote tab to an e10s window"
  );

  remoteWin.gBrowser.swapBrowsersAndCloseOther(remoteTab, nonRemoteTab);
  is(
    remoteWin.gBrowser.tabs.length,
    2,
    "Prevent moving a normal tab to a private tabbrowser"
  );
  is(
    nonRemoteWin.gBrowser.tabs.length,
    2,
    "Prevent accepting a normal tab in a private tabbrowser"
  );

  nonRemoteWin.gBrowser.swapBrowsersAndCloseOther(nonRemoteTab, remoteTab);
  is(
    nonRemoteWin.gBrowser.tabs.length,
    2,
    "Prevent moving a private tab to a normal tabbrowser"
  );
  is(
    remoteWin.gBrowser.tabs.length,
    2,
    "Prevent accepting a private tab in a normal tabbrowser"
  );

  await BrowserTestUtils.closeWindow(remoteWin);
  await BrowserTestUtils.closeWindow(nonRemoteWin);
});

/**
 * Tests that tabs from fission windows cannot be dragged into non-fission
 * windows, and vice-versa.
 */
add_task(async function test_dragging_fission_windows() {
  let fissionWin = await BrowserTestUtils.openNewBrowserWindow({
    remote: true,
    fission: true,
  });
  let nonFissionWin = await BrowserTestUtils.openNewBrowserWindow({
    remote: true,
    fission: false,
  });

  let fissionTab = await BrowserTestUtils.openNewForegroundTab(
    fissionWin.gBrowser
  );
  let nonFissionTab = await BrowserTestUtils.openNewForegroundTab(
    nonFissionWin.gBrowser
  );

  let effect = EventUtils.synthesizeDrop(
    fissionTab,
    nonFissionTab,
    [[{ type: TAB_DROP_TYPE, data: fissionTab }]],
    null,
    fissionWin,
    nonFissionWin
  );
  is(
    effect,
    "none",
    "Should not be able to drag a fission tab to a non-fission window"
  );

  effect = EventUtils.synthesizeDrop(
    nonFissionTab,
    fissionTab,
    [[{ type: TAB_DROP_TYPE, data: nonFissionTab }]],
    null,
    nonFissionWin,
    fissionWin
  );
  is(
    effect,
    "none",
    "Should not be able to drag a non-fission tab to an fission window"
  );

  let swapOk = fissionWin.gBrowser.swapBrowsersAndCloseOther(
    fissionTab,
    nonFissionTab
  );
  is(
    swapOk,
    false,
    "Returns false swapping fission tab to a non-fission tabbrowser"
  );
  is(
    fissionWin.gBrowser.tabs.length,
    2,
    "Prevent moving a fission tab to a non-fission tabbrowser"
  );
  is(
    nonFissionWin.gBrowser.tabs.length,
    2,
    "Prevent accepting a fission tab in a non-fission tabbrowser"
  );

  swapOk = nonFissionWin.gBrowser.swapBrowsersAndCloseOther(
    nonFissionTab,
    fissionTab
  );
  is(
    swapOk,
    false,
    "Returns false swapping non-fission tab to a fission tabbrowser"
  );
  is(
    nonFissionWin.gBrowser.tabs.length,
    2,
    "Prevent moving a non-fission tab to a fission tabbrowser"
  );
  is(
    fissionWin.gBrowser.tabs.length,
    2,
    "Prevent accepting a non-fission tab in a fission tabbrowser"
  );

  await BrowserTestUtils.closeWindow(fissionWin);
  await BrowserTestUtils.closeWindow(nonFissionWin);
});

/**
 * Tests that remoteness-blacklisted tabs from e10s windows can
 * be dragged between e10s windows.
 */
add_task(async function test_dragging_blacklisted() {
  if (!gMultiProcessBrowser) {
    return;
  }

  let remoteWin1 = await BrowserTestUtils.openNewBrowserWindow({
    remote: true,
  });
  remoteWin1.gBrowser.myID = "remoteWin1";
  let remoteWin2 = await BrowserTestUtils.openNewBrowserWindow({
    remote: true,
  });
  remoteWin2.gBrowser.myID = "remoteWin2";

  // Anything under chrome://mochitests/content/ will be blacklisted, and
  // open in the parent process.
  const BLACKLISTED_URL =
    getRootDirectory(gTestPath) + "browser_tab_drag_drop_perwindow.js";
  let blacklistedTab = await BrowserTestUtils.openNewForegroundTab(
    remoteWin1.gBrowser,
    BLACKLISTED_URL
  );

  ok(blacklistedTab.linkedBrowser, "Newly created tab should have a browser.");

  ok(
    !blacklistedTab.linkedBrowser.isRemoteBrowser,
    `Expected a non-remote browser for URL: ${BLACKLISTED_URL}`
  );

  let otherTab = await BrowserTestUtils.openNewForegroundTab(
    remoteWin2.gBrowser
  );

  let effect = EventUtils.synthesizeDrop(
    blacklistedTab,
    otherTab,
    [[{ type: TAB_DROP_TYPE, data: blacklistedTab }]],
    null,
    remoteWin1,
    remoteWin2
  );
  is(effect, "move", "Should be able to drag the blacklisted tab.");

  // The synthesized drop should also do the work of swapping the
  // browsers, so no need to call swapBrowsersAndCloseOther manually.

  is(
    remoteWin1.gBrowser.tabs.length,
    1,
    "Should have moved the blacklisted tab out of this window."
  );
  is(
    remoteWin2.gBrowser.tabs.length,
    3,
    "Should have inserted the blacklisted tab into the other window."
  );

  // The currently selected tab in the second window should be the
  // one we just dragged in.
  let draggedBrowser = remoteWin2.gBrowser.selectedBrowser;
  ok(
    !draggedBrowser.isRemoteBrowser,
    "The browser we just dragged in should not be remote."
  );

  is(
    draggedBrowser.currentURI.spec,
    BLACKLISTED_URL,
    `Expected the URL of the dragged in tab to be ${BLACKLISTED_URL}`
  );

  await BrowserTestUtils.closeWindow(remoteWin1);
  await BrowserTestUtils.closeWindow(remoteWin2);
});

/**
 * Tests that tabs dragged between windows dispatch TabOpen and TabClose
 * events with the appropriate adoption details.
 */
add_task(async function test_dragging_adoption_events() {
  let win1 = await BrowserTestUtils.openNewBrowserWindow();
  let win2 = await BrowserTestUtils.openNewBrowserWindow();

  let tab1 = await BrowserTestUtils.openNewForegroundTab(win1.gBrowser);
  let tab2 = await BrowserTestUtils.openNewForegroundTab(win2.gBrowser);

  let awaitCloseEvent = BrowserTestUtils.waitForEvent(tab1, "TabClose");
  let awaitOpenEvent = BrowserTestUtils.waitForEvent(win2, "TabOpen");

  let effect = EventUtils.synthesizeDrop(
    tab1,
    tab2,
    [[{ type: TAB_DROP_TYPE, data: tab1 }]],
    null,
    win1,
    win2
  );
  is(effect, "move", "Tab should be moved from win1 to win2.");

  let closeEvent = await awaitCloseEvent;
  let openEvent = await awaitOpenEvent;

  is(openEvent.detail.adoptedTab, tab1, "New tab adopted old tab");
  is(
    closeEvent.detail.adoptedBy,
    openEvent.target,
    "Old tab adopted by new tab"
  );

  await BrowserTestUtils.closeWindow(win1);
  await BrowserTestUtils.closeWindow(win2);
});

/**
 * Tests that per-site zoom settings remain active after a tab is
 * dragged between windows.
 */
add_task(async function test_dragging_zoom_handling() {
  const ZOOM_FACTOR = 1.62;

  let win1 = await BrowserTestUtils.openNewBrowserWindow();
  let win2 = await BrowserTestUtils.openNewBrowserWindow();

  let tab1 = await BrowserTestUtils.openNewForegroundTab(win1.gBrowser);
  let tab2 = await BrowserTestUtils.openNewForegroundTab(
    win2.gBrowser,
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://example.com/"
  );

  win2.FullZoom.setZoom(ZOOM_FACTOR);
  is(
    ZoomManager.getZoomForBrowser(tab2.linkedBrowser),
    ZOOM_FACTOR,
    "Original tab should have correct zoom factor"
  );

  let effect = EventUtils.synthesizeDrop(
    tab2,
    tab1,
    [[{ type: TAB_DROP_TYPE, data: tab2 }]],
    null,
    win2,
    win1
  );
  is(effect, "move", "Tab should be moved from win2 to win1.");

  // Delay slightly to make sure we've finished executing any promise
  // chains in the zoom code.
  await new Promise(resolve => setTimeout(resolve, 0));

  is(
    ZoomManager.getZoomForBrowser(win1.gBrowser.selectedBrowser),
    ZOOM_FACTOR,
    "Dragged tab should have correct zoom factor"
  );

  win1.FullZoom.reset();

  await BrowserTestUtils.closeWindow(win1);
  await BrowserTestUtils.closeWindow(win2);
});