summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_tabswitch_window_focus.js
blob: a808ab4f09d3243e0bf07691cd1323e20af4d93d (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
"use strict";

// Allow to open popups without any kind of interaction.
SpecialPowers.pushPrefEnv({ set: [["dom.disable_window_flip", false]] });

const FILE = getRootDirectory(gTestPath) + "open_window_in_new_tab.html";

add_task(async function () {
  info("Opening first tab: " + FILE);
  let firstTab = await BrowserTestUtils.openNewForegroundTab(gBrowser, FILE);

  let promiseTabOpened = BrowserTestUtils.waitForNewTab(
    gBrowser,
    FILE + "?open-click",
    true
  );
  info("Opening second tab using a click");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#open-click",
    {},
    firstTab.linkedBrowser
  );

  info("Waiting for the second tab to be opened");
  let secondTab = await promiseTabOpened;

  info("Going back to the first tab");
  await BrowserTestUtils.switchTab(gBrowser, firstTab);

  info("Focusing second tab by clicking on the first tab");
  await BrowserTestUtils.switchTab(gBrowser, async function () {
    await SpecialPowers.spawn(firstTab.linkedBrowser, [""], async function () {
      content.document.querySelector("#focus").click();
    });
  });

  is(gBrowser.selectedTab, secondTab, "Should've switched tabs");

  await BrowserTestUtils.removeTab(firstTab);
  await BrowserTestUtils.removeTab(secondTab);
});

add_task(async function () {
  info("Opening first tab: " + FILE);
  let firstTab = await BrowserTestUtils.openNewForegroundTab(gBrowser, FILE);

  let promiseTabOpened = BrowserTestUtils.waitForNewTab(
    gBrowser,
    FILE + "?open-mousedown",
    true
  );
  info("Opening second tab using a click");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#open-mousedown",
    { type: "mousedown" },
    firstTab.linkedBrowser
  );

  info("Waiting for the second tab to be opened");
  let secondTab = await promiseTabOpened;

  is(gBrowser.selectedTab, secondTab, "Should've switched tabs");

  info("Ensuring we don't switch back");
  await new Promise(resolve => {
    // We need to wait for something _not_ happening, so we need to use an arbitrary setTimeout.
    //
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    setTimeout(function () {
      is(gBrowser.selectedTab, secondTab, "Should've remained in original tab");
      resolve();
    }, 500);
  });

  info("cleanup");
  await BrowserTestUtils.removeTab(firstTab);
  await BrowserTestUtils.removeTab(secondTab);
});