summaryrefslogtreecommitdiffstats
path: root/dom/base/test/browser_outline_refocus.js
blob: 066c25de6342b4a3d2d52a01259a9d2157ef94de (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
const URL = `data:text/html,<a target="_blank" href="http://example.com">Click me</a>`;

async function test_browser_outline_refocus(
  aMessage,
  aShouldFocusBeVisible,
  aOpenTabCallback
) {
  await BrowserTestUtils.withNewTab(URL, async function (browser) {
    let tab = gBrowser.getTabForBrowser(browser);
    let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);

    await aOpenTabCallback(browser);

    info("waiting for new tab");
    let newTab = await newTabPromise;

    is(gBrowser.selectedTab, newTab, "Should've switched to the new tab");

    info("switching back");
    await BrowserTestUtils.switchTab(gBrowser, tab);

    info("checking focus");
    let [wasFocused, wasFocusVisible] = await SpecialPowers.spawn(
      browser,
      [],
      () => {
        let link = content.document.querySelector("a");
        return [link.matches(":focus"), link.matches(":focus-visible")];
      }
    );

    ok(wasFocused, "Link should be refocused");
    is(wasFocusVisible, aShouldFocusBeVisible, aMessage);

    info("closing tab");
    await BrowserTestUtils.removeTab(newTab);
  });
}

add_task(async function browser_outline_refocus_mouse() {
  await test_browser_outline_refocus(
    "Link shouldn't show outlines since it was originally focused by mouse",
    false,
    function (aBrowser) {
      info("clicking on link");
      return BrowserTestUtils.synthesizeMouseAtCenter("a", {}, aBrowser);
    }
  );
});

add_task(async function browser_outline_refocus_key() {
  await SpecialPowers.pushPrefEnv({
    set: [["accessibility.tabfocus", 7]],
  });

  await test_browser_outline_refocus(
    "Link should show outlines since it was originally focused by keyboard",
    true,
    function (aBrowser) {
      info("Navigating via keyboard");
      EventUtils.sendKey("tab");
      EventUtils.sendKey("return");
    }
  );
});