gecko/toolkit/components/satchel/test/test_form_submission.html

249 lines
8.3 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Satchel Test for Form Submisstion</title>
<script type="text/javascript" src="/MochiKit/packed.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="satchel_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
<!-- ===== Things that should not be saved. ===== -->
<!-- autocomplete=off for input -->
<form id="form1" onsubmit="return checkSubmit(1)">
<input type="text" name="test1" autocomplete="off">
<button type="submit">Submit</button>
</form>
<!-- autocomplete=off for form -->
<form id="form2" onsubmit="return checkSubmit(2)" autocomplete="off">
<input type="text" name="test1">
<button type="submit">Submit</button>
</form>
<!-- don't save type=hidden -->
<form id="form3" onsubmit="return checkSubmit(3)">
<input type="hidden" name="test1">
<button type="submit">Submit</button>
</form>
<!-- don't save type=checkbox -->
<form id="form4" onsubmit="return checkSubmit(4)">
<input type="checkbox" name="test1">
<button type="submit">Submit</button>
</form>
<!-- Don't save empty values. -->
<form id="form5" onsubmit="return checkSubmit(5)">
<input type="text" name="test1" value="originalValue">
<button type="submit">Submit</button>
</form>
<!-- Don't save unchanged values. -->
<form id="form6" onsubmit="return checkSubmit(6)">
<input type="text" name="test1" value="dontSaveThis">
<button type="submit">Submit</button>
</form>
<!-- Don't save unchanged values. (.value not touched) -->
<form id="form7" onsubmit="return checkSubmit(7)">
<input type="text" name="test1" value="dontSaveThis">
<button type="submit">Submit</button>
</form>
<!-- No field name or ID. -->
<form id="form8" onsubmit="return checkSubmit(8)">
<input type="text">
<button type="submit">Submit</button>
</form>
<!-- Nothing to save! -->
<form id="form9" onsubmit="return checkSubmit(9)">
<button type="submit">Submit</button>
</form>
<!-- input with name too long (300 chars.) -->
<form id="form10" onsubmit="return checkSubmit(10)">
<input type="text" name="12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890">
<button type="submit">Submit</button>
</form>
<!-- input with value too long (300 chars.) -->
<form id="form11" onsubmit="return checkSubmit(11)">
<input type="text" name="test1">
<button type="submit">Submit</button>
</form>
<!-- input with value of one space (which should be trimmed) -->
<form id="form12" onsubmit="return checkSubmit(12)">
<input type="text" name="test1">
<button type="submit">Submit</button>
</form>
<!-- ===== Things that should be saved ===== -->
<!-- input with no default value -->
<form id="form101" onsubmit="return checkSubmit(101)">
<input type="text" name="test1">
<button type="submit">Submit</button>
</form>
<!-- input with a default value -->
<form id="form102" onsubmit="return checkSubmit(102)">
<input type="text" name="test2" value="originalValue">
<button type="submit">Submit</button>
</form>
<!-- input uses id but not name -->
<form id="form103" onsubmit="return checkSubmit(103)">
<input type="text" id="test3">
<button type="submit">Submit</button>
</form>
<!-- input with leading and trailing space -->
<form id="form104" onsubmit="return checkSubmit(104)">
<input type="text" name="test4">
<button type="submit">Submit</button>
</form>
<!-- input with leading and trailing whitespace -->
<form id="form105" onsubmit="return checkSubmit(105)">
<input type="text" name="test5">
<button type="submit">Submit</button>
</form>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var numSubmittedForms = 0;
function startTest() {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
ok(!fh.hasEntries, "checking for initially empty storage");
// Fill in values for the various fields. We could just set the <input>'s
// value attribute, but we don't save default form values (and we want to
// ensure unsaved values are because of autocomplete=off or whatever).
$_(1, "test1").value = "dontSaveThis";
$_(2, "test1").value = "dontSaveThis";
$_(3, "test1").value = "dontSaveThis";
$_(4, "test1").value = "dontSaveThis";
$_(5, "test1").value = "";
$_(6, "test1").value = "dontSaveThis";
// Form 7 deliberately left untouched.
// Form 8 has an input with no name or input attribute.
var input = document.getElementById("form8").elements[0];
is(input.type, "text", "checking we got unidentified input");
input.value = "dontSaveThis";
// Form 9 has nothing to modify.
$_(10, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890").value = "dontSaveThis";
$_(11, "test1").value = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
$_(12, "test1").value = " ";
$_(101, "test1").value = "savedValue";
$_(102, "test2").value = "savedValue";
$_(103, "test3").value = "savedValue";
$_(104, "test4").value = " trimTrailingAndLeadingSpace ";
$_(105, "test5").value = "\t trimTrailingAndLeadingWhitespace\t ";
// submit the first form.
var button = getFormSubmitButton(1);
button.click();
}
// Called by each form's onsubmit handler.
function checkSubmit(formNum) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
ok(true, "form " + formNum + " submitted");
numSubmittedForms++;
// Check for expected storage state.
switch (formNum) {
// Test 1-12 should not save anything.
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
ok(!fh.hasEntries, "checking for empty storage");
break;
// The other tests do save data...
case 101:
ok(fh.entryExists("test1", "savedValue"), "checking saved value");
break;
case 102:
ok(fh.entryExists("test2", "savedValue"), "checking saved value");
break;
case 103:
ok(fh.entryExists("test3", "savedValue"), "checking saved value");
break;
case 104:
ok(fh.entryExists("test4", "trimTrailingAndLeadingSpace"), "checking saved value is trimmed on both sides");
break;
case 105:
ok(fh.entryExists("test5", "trimTrailingAndLeadingWhitespace"), "checking saved value is trimmed on both sides");
break;
default:
ok(false, "Unexpected form submission");
break;
}
// End the test at the last form.
if (formNum == 105) {
is(numSubmittedForms, 17, "Ensuring all forms were submitted.");
SimpleTest.finish();
return false; // return false to cancel current form submission
}
// submit the next form.
var button = getFormSubmitButton(formNum == 12 ? 101 : (formNum + 1));
button.click();
return false; // cancel current form submission
}
function getFormSubmitButton(formNum) {
var form = $("form" + formNum); // by id, not name
ok(form != null, "getting form " + formNum);
// we can't just call form.submit(), because that doesn't seem to
// invoke the form onsubmit handler.
var button = form.firstChild;
while (button && button.type != "submit") { button = button.nextSibling; }
ok(button != null, "getting form submit button");
return button;
}
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var fh = Components.classes["@mozilla.org/satchel/form-history;1"].
getService(Components.interfaces.nsIFormHistory2);
ok(fh != null, "Got formHistory service");
window.onload = startTest;
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>