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

395 lines
11 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Test for Form History Autocomplete</title>
<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" style="direction: rtl;">
<!-- 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();
autocompletePopup.style.direction = "ltr";
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 doKeyUnprivileged(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 doClickUnprivileged() {
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 privileged.");
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:
doClickUnprivileged();
nextTestPrivledged = true;
break;
case 7:
checkPopupOpen(false);
checkForm("");
break;
// try a down-arrow
case 8:
doKeyUnprivileged("down");
nextTestPrivledged = true;
break;
case 9:
checkPopupOpen(false);
checkForm("");
break;
// try a page-down
case 10:
doKeyUnprivileged("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.
// doKeyUnprivileged("return"); // not "enter"!
nextTestPrivledged = true;
break;
case 13:
checkPopupOpen(false);
checkForm("");
break;
// try a keypress
case 14:
doKeyUnprivileged('v');
nextTestPrivledged = true;
break;
case 15:
checkPopupOpen(false);
checkForm("");
break;
// try a space
case 16:
doKeyUnprivileged(" ");
nextTestPrivledged = true;
break;
case 17:
checkPopupOpen(false);
checkForm("");
break;
// backspace
case 18:
doKeyUnprivileged("back_space");
nextTestPrivledged = true;
break;
case 19:
checkPopupOpen(false);
checkForm("");
nextTestPrivledged = true;
break;
case 20:
// We're privileged for this test, so open the popup.
checkPopupOpen(false);
checkForm("");
doKey("down");
nextTestPrivledged = true;
break;
case 21:
checkPopupOpen(true, -1);
checkForm("");
testNum = 99;
break;
//
// Try to change the selected autocomplete item from untrusted events
//
// try a down-arrow
case 100:
doKeyUnprivileged("down");
nextTestPrivledged = true;
break;
case 101:
checkPopupOpen(true, -1);
checkForm("");
break;
// try a page-down
case 102:
doKeyUnprivileged("page_down");
nextTestPrivledged = true;
break;
case 103:
checkPopupOpen(true, -1);
checkForm("");
nextTestPrivledged = true;
break;
// really adjust the index
case 104:
// (We're privileged for this test.) Try a privileged down-arrow.
doKey("down");
nextTestPrivledged = true;
break;
case 105:
checkPopupOpen(true, 0);
checkForm("");
break;
// try a down-arrow
case 106:
doKeyUnprivileged("down");
nextTestPrivledged = true;
break;
case 107:
checkPopupOpen(true, 0);
checkForm("");
break;
// try a page-down
case 108:
doKeyUnprivileged("page_down");
nextTestPrivledged = true;
break;
case 109:
checkPopupOpen(true, 0);
checkForm("");
// try a keypress
case 110:
// XXX this causes the popup to close, and makes the value "vaa" (sic)
//doKeyUnprivileged('a');
nextTestPrivledged = true;
break;
case 111:
checkPopupOpen(true, 0);
checkForm("");
testNum = 199;
break;
//
// Try to use the selected autocomplete item from untrusted events
//
// try a right-arrow
case 200:
doKeyUnprivileged("right");
nextTestPrivledged = true;
break;
case 201:
checkPopupOpen(true, 0);
checkForm("");
break;
// try a space
case 202:
doKeyUnprivileged(" ");
nextTestPrivledged = true;
break;
case 203:
// XXX we should ignore this input while popup is open?
checkPopupOpen(true, 0);
checkForm("");
break;
// backspace
case 204:
doKeyUnprivileged("back_space");
nextTestPrivledged = true;
break;
case 205:
// XXX we should ignore this input while popup is open?
checkPopupOpen(true, 0);
checkForm("");
nextTestPrivledged = true;
break;
case 206:
// (this space intentionally left blank)
nextTestPrivledged = true;
break;
case 207:
checkPopupOpen(true, 0);
checkForm("");
break;
// try a return
case 208:
// XXX this seems to cause problems with reliably closing the popup
// doKeyUnprivileged("return"); // not "enter"!
nextTestPrivledged = true;
break;
case 209:
checkPopupOpen(true, 0);
checkForm("");
break;
// Send a real Escape to ensure popup closed at end of test.
case 210:
// Need to use doKey(), even though this test is not privileged.
doKey("escape");
nextTestPrivledged = true;
break;
case 211:
checkPopupOpen(false);
checkForm("");
is(autocompletePopup.style.direction, "rtl", "direction should have been changed from ltr to rtl");
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>