Restore typeAheadResult support in autocomplete

This commit is contained in:
NTD 2018-04-17 07:31:18 -04:00 • committed by Roy Tam
commit 6106e49360
11 changed files with 273 additions and 52 deletions

View file

@ -85,6 +85,11 @@ AutoCompleteResultBase.prototype = {
defaultIndex: -1,
_typeAheadResult: false,
get typeAheadResult() {
return this._typeAheadResult;
},
get matchCount() {
return this._values.length;
},

View file

@ -0,0 +1,76 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
function AutoCompleteResult(aValues) {
this._values = aValues;
this.defaultIndex = -1;
this._typeAheadResult = false;
}
AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);
function AutoCompleteTypeAheadResult(aValues) {
this._values = aValues;
this.defaultIndex = 0;
this._typeAheadResult = true;
}
AutoCompleteTypeAheadResult.prototype = Object.create(AutoCompleteResultBase.prototype);
/**
* Test AutoComplete with multiple AutoCompleteSearch sources, with one of them
* being hidden from the popup, but can still do typeahead completion.
*/
function run_test() {
do_test_pending();
var inputStr = "moz";
// Type ahead result
var searchTypeAhead = new AutoCompleteSearchBase("search1",
new AutoCompleteTypeAheadResult(["mozillaTest1"]));
// Regular result
var searchNormal = new AutoCompleteSearchBase("search2",
new AutoCompleteResult(["mozillaTest2"]));
// Register searches so AutoCompleteController can find them
registerAutoCompleteSearch(searchNormal);
registerAutoCompleteSearch(searchTypeAhead);
// Make an AutoCompleteInput that uses our searches
// and confirms results on search complete.
var input = new AutoCompleteInputBase([searchTypeAhead.name, searchNormal.name]);
input.completeDefaultIndex = true;
input.textValue = inputStr;
// Caret must be at the end. Autofill doesn't happen unless you're typing
// characters at the end.
var strLen = inputStr.length;
input.selectTextRange(strLen, strLen);
do_check_eq(input.selectionStart, strLen);
do_check_eq(input.selectionEnd, strLen);
var controller = Cc["@mozilla.org/autocomplete/controller;1"].
getService(Ci.nsIAutoCompleteController);
controller.input = input;
controller.startSearch(inputStr);
input.onSearchComplete = function() {
// Hidden results should still be able to do inline autocomplete
do_check_eq(input.textValue, "mozillaTest1");
// Now, let's fill the textbox with the first result of the popup.
// The first search is marked as hidden, so we must always get the
// second search.
controller.handleEnter(true);
do_check_eq(input.textValue, "mozillaTest2");
// Only one item in the popup.
do_check_eq(controller.matchCount, 1);
// Unregister searches
unregisterAutoCompleteSearch(searchNormal);
unregisterAutoCompleteSearch(searchTypeAhead);
do_test_finished();
};
}

View file

@ -0,0 +1,71 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
function AutoCompleteTypeAheadResult(aValues, aFinalCompleteValues) {
this._values = aValues;
this._finalCompleteValues = aFinalCompleteValues;
this.defaultIndex = 0;
this._typeAheadResult = true;
}
AutoCompleteTypeAheadResult.prototype = Object.create(AutoCompleteResultBase.prototype);
function AutoCompleteResult(aValues) {
this._values = aValues;
}
AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);
function AutoCompleteInput(aSearches) {
this.searches = aSearches;
this.popupOpen = true;
this.completeDefaultIndex = true;
this.completeSelectedIndex = true;
}
AutoCompleteInput.prototype = Object.create(AutoCompleteInputBase.prototype);
function run_test() {
run_next_test();
}
add_test(function test_handleEnter() {
doSearch("moz", function(aController) {
do_check_eq(aController.input.textValue, "mozilla.com");
aController.handleEnter(true);
do_check_eq(aController.input.textValue, "mozilla.org");
});
});
function doSearch(aSearchString, aOnCompleteCallback) {
let typeAheadSearch = new AutoCompleteSearchBase(
"typeAheadSearch",
new AutoCompleteTypeAheadResult([ "mozilla.com" ], [ "http://www.mozilla.com" ])
);
registerAutoCompleteSearch(typeAheadSearch);
let search = new AutoCompleteSearchBase(
"search",
new AutoCompleteResult([ "mozilla.org" ])
);
registerAutoCompleteSearch(search);
let controller = Cc["@mozilla.org/autocomplete/controller;1"].
getService(Ci.nsIAutoCompleteController);
// Make an AutoCompleteInput that uses our searches and confirms results.
let input = new AutoCompleteInput([ typeAheadSearch.name, search.name ]);
input.textValue = aSearchString;
// Caret must be at the end for autofill to happen.
let strLen = aSearchString.length;
input.selectTextRange(strLen, strLen);
controller.input = input;
controller.startSearch(aSearchString);
input.onSearchComplete = function onSearchComplete() {
aOnCompleteCallback(controller);
// Clean up.
unregisterAutoCompleteSearch(search);
run_next_test();
};
}

View file

@ -18,7 +18,9 @@ tail =
[test_finalCompleteValue_forceComplete.js]
[test_finalCompleteValueSelectedIndex.js]
[test_finalDefaultCompleteValue.js]
[test_hiddenResult.js]
[test_immediate_search.js]
[test_insertMatchAt.js]
[test_popupSelectionVsDefaultCompleteValue.js]
[test_previousResult.js]
[test_stopSearch.js]