michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * Search object that returns results at different times. michael@0: * First, the search that returns results asynchronously. michael@0: */ michael@0: function AutoCompleteAsyncSearch(aName, aResult) { michael@0: this.name = aName; michael@0: this._result = aResult; michael@0: } michael@0: AutoCompleteAsyncSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); michael@0: AutoCompleteAsyncSearch.prototype.startSearch = function(aSearchString, michael@0: aSearchParam, michael@0: aPreviousResult, michael@0: aListener) { michael@0: setTimeout(this._returnResults.bind(this), 500, aListener); michael@0: }; michael@0: michael@0: AutoCompleteAsyncSearch.prototype._returnResults = function(aListener) { michael@0: var result = this._result; michael@0: michael@0: result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; michael@0: aListener.onSearchResult(this, result); michael@0: }; michael@0: michael@0: /** michael@0: * The synchronous version michael@0: */ michael@0: function AutoCompleteSyncSearch(aName, aResult) { michael@0: this.name = aName; michael@0: this._result = aResult; michael@0: } michael@0: AutoCompleteSyncSearch.prototype = Object.create(AutoCompleteAsyncSearch.prototype); michael@0: AutoCompleteSyncSearch.prototype.startSearch = function(aSearchString, michael@0: aSearchParam, michael@0: aPreviousResult, michael@0: aListener) { michael@0: this._returnResults(aListener); michael@0: }; michael@0: michael@0: /** michael@0: * Results object michael@0: */ michael@0: function AutoCompleteResult(aValues, aDefaultIndex) { michael@0: this._values = aValues; michael@0: this.defaultIndex = aDefaultIndex; michael@0: } michael@0: AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); michael@0: michael@0: michael@0: /** michael@0: * Test AutoComplete with multiple AutoCompleteSearch sources, with one of them michael@0: * (index != 0) returning before the rest. michael@0: */ michael@0: function run_test() { michael@0: do_test_pending(); michael@0: michael@0: var results = ["mozillaTest"]; michael@0: var inputStr = "moz"; michael@0: michael@0: // Async search michael@0: var asyncSearch = new AutoCompleteAsyncSearch("Async", michael@0: new AutoCompleteResult(results, -1)); michael@0: // Sync search michael@0: var syncSearch = new AutoCompleteSyncSearch("Sync", michael@0: new AutoCompleteResult(results, 0)); michael@0: michael@0: // Register searches so AutoCompleteController can find them michael@0: registerAutoCompleteSearch(asyncSearch); michael@0: registerAutoCompleteSearch(syncSearch); michael@0: michael@0: var controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: michael@0: // Make an AutoCompleteInput that uses our searches michael@0: // and confirms results on search complete. michael@0: // Async search MUST be FIRST to trigger the bug this tests. michael@0: var input = new AutoCompleteInputBase([asyncSearch.name, syncSearch.name]); michael@0: input.completeDefaultIndex = true; michael@0: input.textValue = inputStr; michael@0: michael@0: // Caret must be at the end. Autofill doesn't happen unless you're typing michael@0: // characters at the end. michael@0: var strLen = inputStr.length; michael@0: input.selectTextRange(strLen, strLen); michael@0: michael@0: controller.input = input; michael@0: controller.startSearch(inputStr); michael@0: michael@0: input.onSearchComplete = function() { michael@0: do_check_eq(input.textValue, results[0]); michael@0: michael@0: // Unregister searches michael@0: unregisterAutoCompleteSearch(asyncSearch); michael@0: unregisterAutoCompleteSearch(syncSearch); michael@0: do_test_finished(); michael@0: }; michael@0: }