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

400 lines
11 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html>
<head>
<title>Test for Form History Autocomplete</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="/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: form field autocomplete
<p id="display"></p>
<!-- we presumably can't hide the content for this test. -->
<div id="content">
<!-- unused -->
<form id="unused" onsubmit="return false;">
<input type="text" name="field1" value="unused">
</form>
<!-- normal, basic form -->
<form id="form1" onsubmit="return false;">
<input type="text" name="field1">
<button type="submit">Submit</button>
</form>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Form History autocomplete **/
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var autocompletePopup = getAutocompletePopup();
var input = $_(1, "field1");
// Get the form history service
var fh = Components.classes["@mozilla.org/satchel/form-history;1"].
getService(Components.interfaces.nsIFormHistory2);
ok(fh != null, "got form history service");
fh.removeAllEntries();
fh.addEntry("field1", "value1");
fh.addEntry("field1", "value2");
fh.addEntry("field1", "value3");
fh.addEntry("field1", "value4");
fh.addEntry("field1", "value5");
fh.addEntry("field1", "value6");
fh.addEntry("field1", "value7");
fh.addEntry("field1", "value8");
fh.addEntry("field1", "value9");
function checkForm(expectedValue) {
var formID = input.parentNode.id;
is(input.value, expectedValue, "Checking " + formID + " input");
}
function checkPopupOpen(isOpen, expectedIndex) {
var actuallyOpen = autocompletePopup.popupOpen;
var actualIndex = autocompletePopup.selectedIndex;
is(actuallyOpen, isOpen, "popup should be " + (isOpen ? "open" : "closed"));
if (isOpen)
is(actualIndex, expectedIndex, "checking selected index");
// Correct state if needed, so following tests run as expected.
if (actuallyOpen != isOpen) {
if (isOpen)
autocompletePopup.openPopup();
else
autocompletePopup.closePopup();
}
if (isOpen && actualIndex != expectedIndex)
autocompletePopup.selectedIndex = expectedIndex;
}
function doKeyUnprivledged(key) {
var keyName = "DOM_VK_" + key.toUpperCase();
var keycode, charcode, alwaysVal;
if (key.length == 1) {
keycode = 0;
charcode = key.charCodeAt(0);
alwaysval = charcode;
} else {
keycode = KeyEvent[keyName];
if (!keycode)
throw "invalid keyname in test";
charcode = 0;
alwaysval = keycode;
}
var dnEvent = document.createEvent('KeyboardEvent');
var prEvent = document.createEvent('KeyboardEvent');
var upEvent = document.createEvent('KeyboardEvent');
dnEvent.initKeyEvent("keydown", true, true, null, false, false, false, false, alwaysval, 0);
prEvent.initKeyEvent("keypress", true, true, null, false, false, false, false, keycode, charcode);
upEvent.initKeyEvent("keyup", true, true, null, false, false, false, false, alwaysval, 0);
input.dispatchEvent(dnEvent);
input.dispatchEvent(prEvent);
input.dispatchEvent(upEvent);
}
function doClickUnprivledged() {
var dnEvent = document.createEvent('MouseEvent');
var upEvent = document.createEvent('MouseEvent');
var ckEvent = document.createEvent('MouseEvent');
dnEvent.initMouseEvent("mousedown", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
upEvent.initMouseEvent("mouseup", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
ckEvent.initMouseEvent("mouseclick", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
input.dispatchEvent(dnEvent);
input.dispatchEvent(upEvent);
input.dispatchEvent(ckEvent);
}
/*
* Main section of test...
*
* This is a bit hacky, because the events are either being sent or
* processes asynchronously, so we need to interrupt our flow with lots of
* setTimeout() calls. The case statements are executed in order, one per
* timeout.
*/
var nextTestPrivledged = true;
function runTest(testNum) {
ok(true, "Starting test #" + testNum);
if (nextTestPrivledged) {
ok(true, "This test is privledged.");
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
nextTestPrivledged = false;
}
switch(testNum) {
//
// Check initial state
//
case 1:
input.value = "";
checkForm("");
is(autocompletePopup.popupOpen, false, "popup should be initially closed");
break;
//
// Try to open the autocomplete popup from untrusted events.
//
// try a focus()
case 2:
input.focus();
nextTestPrivledged = true;
break;
case 3:
checkPopupOpen(false);
checkForm("");
break;
// try a click()
case 4:
input.click();
nextTestPrivledged = true;
break;
case 5:
checkPopupOpen(false);
checkForm("");
break;
// try a mouseclick event
case 6:
doClickUnprivledged();
nextTestPrivledged = true;
break;
case 7:
checkPopupOpen(false);
checkForm("");
break;
// try a down-arrow
case 8:
doKeyUnprivledged("down");
nextTestPrivledged = true;
break;
case 9:
checkPopupOpen(false);
checkForm("");
break;
// try a page-down
case 10:
doKeyUnprivledged("page_down");
nextTestPrivledged = true;
break;
case 11:
checkPopupOpen(false);
checkForm("");
break;
// try a return
case 12:
// XXX this causes later tests to fail for some reason.
// doKeyUnprivledged("return"); // not "enter"!
nextTestPrivledged = true;
break;
case 13:
checkPopupOpen(false);
checkForm("");
break;
// try a keypress
case 14:
doKeyUnprivledged('v');
nextTestPrivledged = true;
break;
case 15:
// XXX even with patch the popup is still opened.
// the keypress triggers a form input oninput / onchange
todo(false, "popup should be closed");
checkPopupOpen(true, -1);
checkForm("v");
break;
// try a space
case 16:
doKeyUnprivledged(" ");
nextTestPrivledged = true;
break;
case 17:
todo(false, "popup should be closed");
checkPopupOpen(true, -1);
checkForm("v ");
break;
// backspace
case 18:
doKeyUnprivledged("back_space");
nextTestPrivledged = true;
break;
case 19:
todo(false, "popup should be closed");
checkPopupOpen(true, -1);
checkForm("v");
nextTestPrivledged = true;
break;
case 20:
// We're privledged for this test, so open the popup.
todo(false, "popup should be closed");
checkPopupOpen(true, -1);
checkForm("v");
// doKey("down");
nextTestPrivledged = true;
break;
case 21:
checkPopupOpen(true, -1);
checkForm("v");
testNum = 99;
break;
//
// Try to change the selected autocomplete item from untrusted events
//
// try a down-arrow
case 100:
doKeyUnprivledged("down");
nextTestPrivledged = true;
break;
case 101:
checkPopupOpen(true, -1);
checkForm("v");
break;
// try a page-down
case 102:
doKeyUnprivledged("page_down");
nextTestPrivledged = true;
break;
case 103:
checkPopupOpen(true, -1);
checkForm("v");
nextTestPrivledged = true;
break;
// really adjust the index
case 104:
doKey("down");
nextTestPrivledged = true;
break;
case 105:
checkPopupOpen(true, 0);
checkForm("v");
break;
// try a down-arrow
case 106:
doKeyUnprivledged("down");
nextTestPrivledged = true;
break;
case 107:
checkPopupOpen(true, 0);
checkForm("v");
break;
// try a page-down
case 108:
doKeyUnprivledged("page_down");
nextTestPrivledged = true;
break;
case 109:
checkPopupOpen(true, 0);
checkForm("v");
// try a keypress
case 110:
// XXX this causes the popup to close, and makes the value "vaa" (sic)
//doKeyUnprivledged('a');
nextTestPrivledged = true;
break;
case 111:
checkPopupOpen(true, 0);
checkForm("v");
testNum = 199;
break;
//
// Try to use the selected autocomplete item from untrusted events
//
// try a right-arrow
case 200:
doKeyUnprivledged("right");
nextTestPrivledged = true;
break;
case 201:
checkPopupOpen(true, 0);
checkForm("v");
break;
// try a space
case 202:
doKeyUnprivledged(" ");
nextTestPrivledged = true;
break;
case 203:
// XXX we should ignore this input while popup is open?
checkPopupOpen(true, -1);
checkForm("v ");
break;
// backspace
case 204:
doKeyUnprivledged("back_space");
nextTestPrivledged = true;
break;
case 205:
// XXX we should ignore this input while popup is open?
checkPopupOpen(true, -1);
checkForm("v");
nextTestPrivledged = true;
break;
// Oops, reselect 1st entry
case 206:
doKey("down");
nextTestPrivledged = true;
break;
case 207:
checkPopupOpen(true, 0);
checkForm("v");
break;
// try a return
case 208:
// XXX this seems to cause problems with reliably closing the popup
// doKeyUnprivledged("return"); // not "enter"!
nextTestPrivledged = true;
break;
case 209:
checkPopupOpen(true, 0);
checkForm("v");
break;
// Send a real Escape to ensure popup closed at end of test.
case 210:
doKey("escape");
nextTestPrivledged = true;
break;
case 211:
checkPopupOpen(false);
checkForm("v");
SimpleTest.finish();
return;
default:
ok(false, "Unexpected invocation of test #" + testNum);
SimpleTest.finish();
return;
}
setTimeout(runTest, 50, testNum + 1);
}
function startTest() {
runTest(1);
}
window.onload = startTest;
SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>