summaryrefslogtreecommitdiffstats
path: root/dom/tests/browser/browser_cancel_keydown_keypress_event.js
blob: d3414185c210b4f45bc549c9c40e4dd237197a65 (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
const URL =
  "https://example.com/browser/dom/tests/browser/prevent_return_key.html";

// Wait for alert dialog and dismiss it immediately.
function awaitAndCloseAlertDialog() {
  return new Promise(resolve => {
    function onDialogShown(node) {
      Services.obs.removeObserver(onDialogShown, "tabmodal-dialog-loaded");
      let button = node.querySelector(".tabmodalprompt-button0");
      button.click();
      resolve();
    }
    Services.obs.addObserver(onDialogShown, "tabmodal-dialog-loaded");
  });
}

add_task(async function() {
  await SpecialPowers.pushPrefEnv({
    set: [["prompts.contentPromptSubDialog", false]],
  });

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);
  let browser = tab.linkedBrowser;

  // Focus and enter random text on input.
  await SpecialPowers.spawn(browser, [], async function() {
    let input = content.document.getElementById("input");
    input.focus();
    input.value = "abcd";
  });

  // Send return key (cross process) to submit the form implicitly.
  let dialogShown = awaitAndCloseAlertDialog();
  EventUtils.synthesizeKey("KEY_Enter");
  await dialogShown;

  // Check that the form should not have been submitted.
  await SpecialPowers.spawn(browser, [], async function() {
    let result = content.document.getElementById("result").innerHTML;
    info("submit result: " + result);
    is(result, "not submitted", "form should not have submitted");
  });

  gBrowser.removeCurrentTab();
});