gecko-dev/toolkit/components/satchel/test/test_submit_on_keydown_ente...

119 строки
4.0 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test for events while the form history popup is open</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<script type="text/javascript" src="satchel_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Form History test: Test for keydown handler submitting the form
<p id="display"></p>
<div id="content">
<form id="form1" action="javascript:handleSubmit()">
<input type="text" name="field1">
<button type="submit">Submit</button>
</form>
</div>
<pre id="test">
<script class="testbody">
var form = document.getElementById("form1");
var input = $_(1, "field1");
var expectedValue = "value1";
var beforeInputFired = false;
function handleSubmit() { // eslint-disable-line no-unused-vars
info("Submit");
ok(false, "The form should not be submitted");
input.removeEventListener("beforeinput", handleBeforeInput, true);
input.removeEventListener("input", handleInput, true);
SimpleTest.finish();
}
function handleBeforeInput(aEvent) {
info("BeforeInput");
beforeInputFired = true;
is(input.value, "value", "The value should've not been modified yet");
ok(aEvent instanceof InputEvent,
'"beforeinput" event should be dispatched with InputEvent interface');
is(aEvent.cancelable, SpecialPowers.getBoolPref("dom.input_event.allow_to_cancel_set_user_input"),
`"beforeinput" event should be cancelable unless it's supporessed by the pref`);
is(aEvent.bubbles, true,
'"beforeinput" event should always bubble');
is(aEvent.inputType, "insertReplacementText",
'inputType of "beforeinput" event should be "insertReplacementText"');
is(aEvent.data, expectedValue,
`data of "beforeinput" event should be "${expectedValue}"`);
is(aEvent.dataTransfer, null,
'dataTransfer of "beforeinput" event should be null');
is(aEvent.getTargetRanges().length, 0,
'getTargetRanges() of "beforeinput" event should return empty array');
}
function handleInput(aEvent) {
info("Input");
ok(beforeInputFired, '"beforeinput" event should have been fired');
is(input.value, expectedValue, "Check input value");
ok(aEvent instanceof InputEvent,
'"input" event should be dispatched with InputEvent interface');
is(aEvent.cancelable, false,
'"input" event should be never cancelable');
is(aEvent.bubbles, true,
'"input" event should always bubble');
is(aEvent.inputType, "insertReplacementText",
'inputType of "input" event should be "insertReplacementText"');
is(aEvent.data, expectedValue,
`data of "input" event should be "${expectedValue}"`);
is(aEvent.dataTransfer, null,
'dataTransfer of "input" event should be null');
is(aEvent.getTargetRanges().length, 0,
'getTargetRanges() of "input" event should return empty array');
input.removeEventListener("beforeinput", handleBeforeInput, true);
input.removeEventListener("input", handleInput, true);
SimpleTest.finish();
}
function runTest() {
input.addEventListener("beforeinput", handleBeforeInput, true);
input.addEventListener("input", handleInput, true);
input.addEventListener("keydown", function handleEnterDown(e) {
if (e.keyCode != KeyEvent.DOM_VK_RETURN) {
return;
}
info("Enter KeyDown");
input.removeEventListener("keydown", handleEnterDown, true);
form.submit();
}, true);
registerPopupShownListener(() => {
synthesizeKey("KEY_ArrowDown");
synthesizeKey("KEY_Enter"); // select the first entry in the popup
});
// Focus the input before adjusting.value so that the caret goes to the end
// (since OS X doesn't show the dropdown otherwise).
input.focus();
input.value = "value";
input.focus();
synthesizeKey("KEY_ArrowDown");
}
function startTest() {
updateFormHistory([
{ op: "remove" },
{ op: "add", fieldname: "field1", value: "value1" },
], runTest);
}
window.onload = startTest;
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>