|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 /** |
|
5 * Search object that returns results at different times. |
|
6 * First, the search that returns results asynchronously. |
|
7 */ |
|
8 function AutoCompleteAsyncSearch(aName, aResult) { |
|
9 this.name = aName; |
|
10 this._result = aResult; |
|
11 } |
|
12 AutoCompleteAsyncSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); |
|
13 AutoCompleteAsyncSearch.prototype.startSearch = function(aSearchString, |
|
14 aSearchParam, |
|
15 aPreviousResult, |
|
16 aListener) { |
|
17 setTimeout(this._returnResults.bind(this), 500, aListener); |
|
18 }; |
|
19 |
|
20 AutoCompleteAsyncSearch.prototype._returnResults = function(aListener) { |
|
21 var result = this._result; |
|
22 |
|
23 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; |
|
24 aListener.onSearchResult(this, result); |
|
25 }; |
|
26 |
|
27 /** |
|
28 * The synchronous version |
|
29 */ |
|
30 function AutoCompleteSyncSearch(aName, aResult) { |
|
31 this.name = aName; |
|
32 this._result = aResult; |
|
33 } |
|
34 AutoCompleteSyncSearch.prototype = Object.create(AutoCompleteAsyncSearch.prototype); |
|
35 AutoCompleteSyncSearch.prototype.startSearch = function(aSearchString, |
|
36 aSearchParam, |
|
37 aPreviousResult, |
|
38 aListener) { |
|
39 this._returnResults(aListener); |
|
40 }; |
|
41 |
|
42 /** |
|
43 * Results object |
|
44 */ |
|
45 function AutoCompleteResult(aValues, aDefaultIndex) { |
|
46 this._values = aValues; |
|
47 this.defaultIndex = aDefaultIndex; |
|
48 } |
|
49 AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); |
|
50 |
|
51 |
|
52 /** |
|
53 * Test AutoComplete with multiple AutoCompleteSearch sources, with one of them |
|
54 * (index != 0) returning before the rest. |
|
55 */ |
|
56 function run_test() { |
|
57 do_test_pending(); |
|
58 |
|
59 var results = ["mozillaTest"]; |
|
60 var inputStr = "moz"; |
|
61 |
|
62 // Async search |
|
63 var asyncSearch = new AutoCompleteAsyncSearch("Async", |
|
64 new AutoCompleteResult(results, -1)); |
|
65 // Sync search |
|
66 var syncSearch = new AutoCompleteSyncSearch("Sync", |
|
67 new AutoCompleteResult(results, 0)); |
|
68 |
|
69 // Register searches so AutoCompleteController can find them |
|
70 registerAutoCompleteSearch(asyncSearch); |
|
71 registerAutoCompleteSearch(syncSearch); |
|
72 |
|
73 var controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
|
74 getService(Ci.nsIAutoCompleteController); |
|
75 |
|
76 // Make an AutoCompleteInput that uses our searches |
|
77 // and confirms results on search complete. |
|
78 // Async search MUST be FIRST to trigger the bug this tests. |
|
79 var input = new AutoCompleteInputBase([asyncSearch.name, syncSearch.name]); |
|
80 input.completeDefaultIndex = true; |
|
81 input.textValue = inputStr; |
|
82 |
|
83 // Caret must be at the end. Autofill doesn't happen unless you're typing |
|
84 // characters at the end. |
|
85 var strLen = inputStr.length; |
|
86 input.selectTextRange(strLen, strLen); |
|
87 |
|
88 controller.input = input; |
|
89 controller.startSearch(inputStr); |
|
90 |
|
91 input.onSearchComplete = function() { |
|
92 do_check_eq(input.textValue, results[0]); |
|
93 |
|
94 // Unregister searches |
|
95 unregisterAutoCompleteSearch(asyncSearch); |
|
96 unregisterAutoCompleteSearch(syncSearch); |
|
97 do_test_finished(); |
|
98 }; |
|
99 } |