Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Tests for Bug 850364 && Bug 918940</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=850364">Mozilla Bug 850364</a>
<p id="display"></p>
<div id="content">
<!-- "SetRangeText() supported types"-->
<input type="text" id="input_text"></input>
<input type="search" id="input_search"></input>
<input type="url" id="input_url"></input>
<input type="tel" id="input_tel"></input>
<input type="password" id="input_password"></input>
<textarea id="input_textarea"></textarea>
<!-- "SetRangeText() non-supported types" -->
<input type="button" id="input_button"></input>
<input type="submit" id="input_submit"></input>
<input type="image" id="input_image"></input>
<input type="reset" id="input_reset"></input>
<input type="radio" id="input_radio"></input>
<input type="checkbox" id="input_checkbox"></input>
<input type="range" id="input_range"></input>
<input type="file" id="input_file"></input>
<input type="email" id="input_email"></input>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Tests for Bug 850364 && Bug 918940**/
var SupportedTypes = ["text", "search", "url", "tel", "password", "textarea"];
var NonSupportedTypes = ["button", "submit", "image", "reset", "radio",
"checkbox", "range", "file", "email"];
SimpleTest.waitForExplicitFinish();
function TestInputs() {
var opThrows, elem, i, msg;
//Non-supported types should throw
for (i = 0; i < NonSupportedTypes.length; ++i) {
opThrows = false;
msg = "input_" + NonSupportedTypes[i];
elem = document.getElementById(msg);
elem.focus();
try {
elem.setRangeText("abc");
} catch (ex) {
opThrows = true;
}
ok(opThrows, msg + " should throw InvalidStateError");
}
var numOfSelectCalls = 0, expectedNumOfSelectCalls = 0;
//Supported types should not throw
for (i = 0; i < SupportedTypes.length; ++i) {
opThrows = false;
msg = "input_" + SupportedTypes[i];
elem = document.getElementById(msg);
elem.focus();
try {
elem.setRangeText("abc");
} catch (ex) {
opThrows = true;
}
is(opThrows, false, msg + " should not throw InvalidStateError");
elem.addEventListener("select", function (aEvent) {
ok(true, "select event should be fired for " + aEvent.target.id);
if (++numOfSelectCalls == expectedNumOfSelectCalls) {
SimpleTest.finish();
} else if (numOfSelectCalls > expectedNumOfSelectCalls) {
ok(false, "Too many select events were fired");
}
});
elem.addEventListener("input", function (aEvent) {
ok(false, "input event should NOT be fired for " + + aEvent.target.id);
});
var test = " setRange(replacement), shrink";
elem.value = "0123456789ABCDEF";
elem.setSelectionRange(1, 6);
elem.setRangeText("xyz");
is(elem.value, "0xyz6789ABCDEF", msg + test);
is(elem.selectionStart, 1, msg + test);
is(elem.selectionEnd, 4, msg + test);
elem.setRangeText("mnk");
is(elem.value, "0mnk6789ABCDEF", msg + test);
expectedNumOfSelectCalls += 2;
test = " setRange(replacement), expand";
elem.value = "0123456789ABCDEF";
elem.setSelectionRange(1, 2);
elem.setRangeText("xyz");
is(elem.value, "0xyz23456789ABCDEF", msg + test);
is(elem.selectionStart, 1, msg + test);
is(elem.selectionEnd, 4, msg + test);
elem.setRangeText("mnk");
is(elem.value, "0mnk23456789ABCDEF", msg + test);
expectedNumOfSelectCalls += 2;
test = " setRange(replacement) pure insertion at start";
elem.value = "0123456789ABCDEF";
elem.setSelectionRange(0, 0);
elem.setRangeText("xyz");
is(elem.value, "xyz0123456789ABCDEF", msg + test);
is(elem.selectionStart, 0, msg + test);
is(elem.selectionEnd, 0, msg + test);
elem.setRangeText("mnk");
is(elem.value, "mnkxyz0123456789ABCDEF", msg + test);
expectedNumOfSelectCalls += 1;
test = " setRange(replacement) pure insertion in the middle";
elem.value = "0123456789ABCDEF";
elem.setSelectionRange(4, 4);
elem.setRangeText("xyz");
is(elem.value, "0123xyz456789ABCDEF", msg + test);
is(elem.selectionStart, 4, msg + test);
is(elem.selectionEnd, 4, msg + test);
elem.setRangeText("mnk");
is(elem.value, "0123mnkxyz456789ABCDEF", msg + test);
expectedNumOfSelectCalls += 1;
test = " setRange(replacement) pure insertion at the end";
elem.value = "0123456789ABCDEF";
elem.setSelectionRange(16, 16);
elem.setRangeText("xyz");
is(elem.value, "0123456789ABCDEFxyz", msg + test);
is(elem.selectionStart, 16, msg + test);
is(elem.selectionEnd, 16, msg + test);
elem.setRangeText("mnk");
is(elem.value, "0123456789ABCDEFmnkxyz", msg + test);
//test SetRange(replacement, start, end, mode) with start > end
try {
elem.setRangeText("abc", 20, 4);
} catch (ex) {
opThrows = (ex.name == "IndexSizeError" && ex.code == DOMException.INDEX_SIZE_ERR);
}
is(opThrows, true, msg + " should throw IndexSizeError");
//test SelectionMode 'select'
elem.value = "0123456789ABCDEF";
elem.setRangeText("xyz", 4, 9, "select");
is(elem.value, "0123xyz9ABCDEF", msg + ".value == \"0123xyz9ABCDEF\"");
is(elem.selectionStart, 4, msg + ".selectionStart == 4, with \"select\"");
is(elem.selectionEnd, 7, msg + ".selectionEnd == 7, with \"select\"");
expectedNumOfSelectCalls += 1;
elem.setRangeText("pqm", 6, 25, "select");
is(elem.value, "0123xypqm", msg + ".value == \"0123xypqm\"");
is(elem.selectionStart, 6, msg + ".selectionStart == 6, with \"select\"");
is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"select\"");
expectedNumOfSelectCalls += 1;
//test SelectionMode 'start'
elem.value = "0123456789ABCDEF";
elem.setRangeText("xyz", 4, 9, "start");
is(elem.value, "0123xyz9ABCDEF", msg + ".value == \"0123xyz9ABCDEF\"");
is(elem.selectionStart, 4, msg + ".selectionStart == 4, with \"start\"");
is(elem.selectionEnd, 4, msg + ".selectionEnd == 4, with \"start\"");
expectedNumOfSelectCalls += 1;
elem.setRangeText("pqm", 6, 25, "start");
is(elem.value, "0123xypqm", msg + ".value == \"0123xypqm\"");
is(elem.selectionStart, 6, msg + ".selectionStart == 6, with \"start\"");
is(elem.selectionEnd, 6, msg + ".selectionEnd == 6, with \"start\"");
expectedNumOfSelectCalls += 1;
//test SelectionMode 'end'
elem.value = "0123456789ABCDEF";
elem.setRangeText("xyz", 4, 9, "end");
is(elem.value, "0123xyz9ABCDEF", msg + ".value == \"0123xyz9ABCDEF\"");
is(elem.selectionStart, 7, msg + ".selectionStart == 7, with \"end\"");
is(elem.selectionEnd, 7, msg + ".selectionEnd == 7, with \"end\"");
expectedNumOfSelectCalls += 1;
elem.setRangeText("pqm", 6, 25, "end");
is(elem.value, "0123xypqm", msg + ".value == \"0123xypqm\"");
is(elem.selectionStart, 9, msg + ".selectionStart == 9, with \"end\"");
is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"end\"");
expectedNumOfSelectCalls += 1;
//test SelectionMode 'preserve' (default)
//subcase: selection{Start|End} > end
elem.value = "0123456789";
elem.setSelectionRange(6, 9);
elem.setRangeText("Z", 1, 2, "preserve");
is(elem.value, "0Z23456789", msg + ".value == \"0Z23456789\"");
is(elem.selectionStart, 6, msg + ".selectionStart == 6, with \"preserve\"");
is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"preserve\"");
expectedNumOfSelectCalls += 1;
//subcase: selection{Start|End} < end
elem.value = "0123456789";
elem.setSelectionRange(4, 5);
elem.setRangeText("QRST", 2, 9, "preserve");
is(elem.value, "01QRST9", msg + ".value == \"01QRST9\"");
is(elem.selectionStart, 2, msg + ".selectionStart == 2, with \"preserve\"");
is(elem.selectionEnd, 6, msg + ".selectionEnd == 6, with \"preserve\"");
expectedNumOfSelectCalls += 2;
//subcase: selectionStart > end, selectionEnd < end
elem.value = "0123456789";
elem.setSelectionRange(8, 4);
elem.setRangeText("QRST", 1, 5);
is(elem.value, "0QRST56789", msg + ".value == \"0QRST56789\"");
is(elem.selectionStart, 1, msg + ".selectionStart == 1, with \"default\"");
is(elem.selectionEnd, 5, msg + ".selectionEnd == 5, with \"default\"");
expectedNumOfSelectCalls += 2;
//subcase: selectionStart < end, selectionEnd > end
elem.value = "0123456789";
elem.setSelectionRange(4, 9);
elem.setRangeText("QRST", 2, 6);
is(elem.value, "01QRST6789", msg + ".value == \"01QRST6789\"");
is(elem.selectionStart, 2, msg + ".selectionStart == 2, with \"default\"");
is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"default\"");
expectedNumOfSelectCalls += 2;
}
}
addLoadEvent(TestInputs);
</script>
</pre>
</body>
</html>