Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<window title="Autocomplete Widget Test 5"
<html:input id="autocomplete"
is="autocomplete-input"
autocompletesearch="simple"
notifylegacyevents="true"/>
<script class="testbody" type="application/javascript">
<![CDATA[
const ACR = Ci.nsIAutoCompleteResult;
// This result can't be constructed in-line, because otherwise we leak memory.
function nsAutoCompleteSimpleResult(aString)
{
this.searchString = aString;
this.searchResult = ACR.RESULT_SUCCESS;
this.matchCount = 1;
this._param = "SUCCESS";
}
nsAutoCompleteSimpleResult.prototype = {
_param: "",
searchString: null,
searchResult: ACR.RESULT_FAILURE,
defaultIndex: -1,
errorDescription: null,
matchCount: 0,
getValueAt() { return this._param; },
getCommentAt() { return null; },
getStyleAt() { return null; },
getImageAt() { return null; },
getFinalCompleteValueAt() { return this.getValueAt(); },
getLabelAt() { return null; },
removeValueAt() {}
};
// A basic autocomplete implementation that either returns one result or none
var autoCompleteSimpleID = Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca");
var autoCompleteSimpleName = "@mozilla.org/autocomplete/search;1?name=simple"
var autoCompleteSimple = {
QueryInterface: ChromeUtils.generateQI(["nsIFactory", "nsIAutoCompleteSearch"]),
createInstance(iid) {
return this.QueryInterface(iid);
},
startSearch(aString, aParam, aResult, aListener) {
var result = new nsAutoCompleteSimpleResult(aString);
aListener.onSearchResult(this, result);
},
stopSearch() {}
};
let element = document.getElementById("autocomplete");
// Create stub to intercept `onSearchBegin` event.
element.onSearchBegin = function(original) {
return function() {
original.apply(this, arguments);
checkSearchBegin();
};
}(element.onSearchBegin);
// Create stub to intercept `onSearchComplete` event.
element.onSearchComplete = function(original) {
return function() {
original.apply(this, arguments);
checkSearchCompleted();
};
}(element.onSearchComplete);
element.addEventListener("textEntered", checkTextEntered);
element.addEventListener("textReverted", checkTextReverted);
var componentManager = Components.manager
.QueryInterface(Ci.nsIComponentRegistrar);
componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete",
autoCompleteSimpleName, autoCompleteSimple);
SimpleTest.waitForExplicitFinish();
setTimeout(startTest, 0);
function startTest() {
let autocomplete = $("autocomplete");
// blur the field to ensure that the popup is closed and that the previous
// search has stopped, then start a new search.
autocomplete.blur();
autocomplete.focus();
sendString("r");
}
let hasTextEntered = false;
let hasSearchBegun = false;
function checkSearchBegin() {
hasSearchBegun = true;
}
let test = 0;
function checkSearchCompleted() {
is(hasSearchBegun, true, "onsearchbegin handler has been correctly called.");
if (test == 0) {
hasSearchBegun = false;
synthesizeKey("KEY_Enter");
} else if (test == 1) {
hasSearchBegun = false;
synthesizeKey("KEY_Escape");
} else {
throw new Error("checkSearchCompleted should only be called twice.");
}
}
function checkTextEntered() {
is(test, 0, "checkTextEntered should be reached from first test.");
is(hasSearchBegun, false, "onsearchbegin handler should not be called on text revert.");
// fire second test
test++;
let autocomplete = $("autocomplete");
autocomplete.textValue = "";
autocomplete.blur();
autocomplete.focus();
sendString("r");
}
function checkTextReverted() {
is(test, 1, "checkTextReverted should be the second test reached.");
is(hasSearchBegun, false, "onsearchbegin handler should not be called on text revert.");
setTimeout(function() {
// Unregister the factory so that we don't get in the way of other tests
componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple);
SimpleTest.finish();
}, 0);
}
]]>
</script>
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</window>