summaryrefslogtreecommitdiffstats
path: root/dom/tests/browser/browser_test_focus_after_modal_state.js
blob: 2193d8fdc41844795cbe4b05a535efe365b8fbef (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
const TEST_URL =
  "https://example.com/browser/dom/tests/browser/focus_after_prompt.html";

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

function awaitAndClosePrompt(browser) {
  return PromptTestUtils.handleNextPrompt(
    browser,
    { modalType: Services.prompt.MODAL_TYPE_CONTENT, promptType: "prompt" },
    { buttonNumClick: 0 }
  );
}

add_task(async function () {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
  let browser = tab.linkedBrowser;

  // Focus on editable iframe.
  await BrowserTestUtils.synthesizeMouseAtCenter("#edit", {}, browser);
  await SpecialPowers.spawn(browser, [], async function () {
    is(
      content.document.activeElement,
      content.document.getElementById("edit"),
      "Focus should be on iframe element"
    );
  });

  let focusBlurPromise = SpecialPowers.spawn(browser, [], async function () {
    let focusOccurred = false;
    let blurOccurred = false;

    return new Promise(resolve => {
      let doc = content.document.getElementById("edit").contentDocument;
      doc.addEventListener("focus", function (event) {
        focusOccurred = true;
        if (blurOccurred) {
          resolve(true);
        }
      });

      doc.addEventListener("blur", function (event) {
        blurOccurred = true;
        if (focusOccurred) {
          resolve(false);
        }
      });
    });
  });

  // Click on div that triggers a prompt, and then check that focus is back on
  // the editable iframe.
  let dialogShown = awaitAndClosePrompt(browser);
  await SpecialPowers.spawn(browser, [], async function () {
    let div = content.document.getElementById("clickMeDiv");
    div.click();
  });
  await dialogShown;
  let blurCameFirst = await focusBlurPromise;
  await SpecialPowers.spawn(browser, [], async function () {
    is(
      content.document.activeElement,
      content.document.getElementById("edit"),
      "Focus should be back on iframe element"
    );
  });
  ok(blurCameFirst, "Should receive blur and then focus event");

  BrowserTestUtils.removeTab(tab);
});