1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_660156.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * Search object that returns results at different times. 1.9 + * First, the search that returns results asynchronously. 1.10 + */ 1.11 +function AutoCompleteAsyncSearch(aName, aResult) { 1.12 + this.name = aName; 1.13 + this._result = aResult; 1.14 +} 1.15 +AutoCompleteAsyncSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); 1.16 +AutoCompleteAsyncSearch.prototype.startSearch = function(aSearchString, 1.17 + aSearchParam, 1.18 + aPreviousResult, 1.19 + aListener) { 1.20 + setTimeout(this._returnResults.bind(this), 500, aListener); 1.21 +}; 1.22 + 1.23 +AutoCompleteAsyncSearch.prototype._returnResults = function(aListener) { 1.24 + var result = this._result; 1.25 + 1.26 + result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; 1.27 + aListener.onSearchResult(this, result); 1.28 +}; 1.29 + 1.30 +/** 1.31 + * The synchronous version 1.32 + */ 1.33 +function AutoCompleteSyncSearch(aName, aResult) { 1.34 + this.name = aName; 1.35 + this._result = aResult; 1.36 +} 1.37 +AutoCompleteSyncSearch.prototype = Object.create(AutoCompleteAsyncSearch.prototype); 1.38 +AutoCompleteSyncSearch.prototype.startSearch = function(aSearchString, 1.39 + aSearchParam, 1.40 + aPreviousResult, 1.41 + aListener) { 1.42 + this._returnResults(aListener); 1.43 +}; 1.44 + 1.45 +/** 1.46 + * Results object 1.47 + */ 1.48 +function AutoCompleteResult(aValues, aDefaultIndex) { 1.49 + this._values = aValues; 1.50 + this.defaultIndex = aDefaultIndex; 1.51 +} 1.52 +AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); 1.53 + 1.54 + 1.55 +/** 1.56 + * Test AutoComplete with multiple AutoCompleteSearch sources, with one of them 1.57 + * (index != 0) returning before the rest. 1.58 + */ 1.59 +function run_test() { 1.60 + do_test_pending(); 1.61 + 1.62 + var results = ["mozillaTest"]; 1.63 + var inputStr = "moz"; 1.64 + 1.65 + // Async search 1.66 + var asyncSearch = new AutoCompleteAsyncSearch("Async", 1.67 + new AutoCompleteResult(results, -1)); 1.68 + // Sync search 1.69 + var syncSearch = new AutoCompleteSyncSearch("Sync", 1.70 + new AutoCompleteResult(results, 0)); 1.71 + 1.72 + // Register searches so AutoCompleteController can find them 1.73 + registerAutoCompleteSearch(asyncSearch); 1.74 + registerAutoCompleteSearch(syncSearch); 1.75 + 1.76 + var controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.77 + getService(Ci.nsIAutoCompleteController); 1.78 + 1.79 + // Make an AutoCompleteInput that uses our searches 1.80 + // and confirms results on search complete. 1.81 + // Async search MUST be FIRST to trigger the bug this tests. 1.82 + var input = new AutoCompleteInputBase([asyncSearch.name, syncSearch.name]); 1.83 + input.completeDefaultIndex = true; 1.84 + input.textValue = inputStr; 1.85 + 1.86 + // Caret must be at the end. Autofill doesn't happen unless you're typing 1.87 + // characters at the end. 1.88 + var strLen = inputStr.length; 1.89 + input.selectTextRange(strLen, strLen); 1.90 + 1.91 + controller.input = input; 1.92 + controller.startSearch(inputStr); 1.93 + 1.94 + input.onSearchComplete = function() { 1.95 + do_check_eq(input.textValue, results[0]); 1.96 + 1.97 + // Unregister searches 1.98 + unregisterAutoCompleteSearch(asyncSearch); 1.99 + unregisterAutoCompleteSearch(syncSearch); 1.100 + do_test_finished(); 1.101 + }; 1.102 +}