|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /** |
|
8 * Dummy nsIAutoCompleteInput source that returns |
|
9 * the given list of AutoCompleteSearch names. |
|
10 * |
|
11 * Implements only the methods needed for this test. |
|
12 */ |
|
13 function AutoCompleteInput(aSearches) { |
|
14 this.searches = aSearches; |
|
15 } |
|
16 AutoCompleteInput.prototype = { |
|
17 constructor: AutoCompleteInput, |
|
18 |
|
19 // Array of AutoCompleteSearch names |
|
20 searches: null, |
|
21 |
|
22 minResultsForPopup: 0, |
|
23 timeout: 10, |
|
24 searchParam: "", |
|
25 textValue: "", |
|
26 disableAutoComplete: false, |
|
27 completeDefaultIndex: false, |
|
28 |
|
29 get searchCount() { |
|
30 return this.searches.length; |
|
31 }, |
|
32 |
|
33 getSearchAt: function(aIndex) { |
|
34 return this.searches[aIndex]; |
|
35 }, |
|
36 |
|
37 onSearchBegin: function() {}, |
|
38 onSearchComplete: function() {}, |
|
39 |
|
40 popupOpen: false, |
|
41 |
|
42 popup: { |
|
43 setSelectedIndex: function(aIndex) {}, |
|
44 invalidate: function() {}, |
|
45 |
|
46 // nsISupports implementation |
|
47 QueryInterface: function(iid) { |
|
48 if (iid.equals(Ci.nsISupports) || |
|
49 iid.equals(Ci.nsIAutoCompletePopup)) |
|
50 return this; |
|
51 |
|
52 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
53 } |
|
54 }, |
|
55 |
|
56 // nsISupports implementation |
|
57 QueryInterface: function(iid) { |
|
58 if (iid.equals(Ci.nsISupports) || |
|
59 iid.equals(Ci.nsIAutoCompleteInput)) |
|
60 return this; |
|
61 |
|
62 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
63 } |
|
64 } |
|
65 |
|
66 |
|
67 |
|
68 /** |
|
69 * nsIAutoCompleteResult implementation |
|
70 */ |
|
71 function AutoCompleteResult(aValues, aComments, aStyles) { |
|
72 this._values = aValues; |
|
73 this._comments = aComments; |
|
74 this._styles = aStyles; |
|
75 } |
|
76 AutoCompleteResult.prototype = { |
|
77 constructor: AutoCompleteResult, |
|
78 |
|
79 // Arrays |
|
80 _values: null, |
|
81 _comments: null, |
|
82 _styles: null, |
|
83 |
|
84 searchString: "", |
|
85 searchResult: null, |
|
86 |
|
87 defaultIndex: 0, |
|
88 |
|
89 get matchCount() { |
|
90 return this._values.length; |
|
91 }, |
|
92 |
|
93 getValueAt: function(aIndex) { |
|
94 return this._values[aIndex]; |
|
95 }, |
|
96 |
|
97 getLabelAt: function(aIndex) { |
|
98 return this.getValueAt(aIndex); |
|
99 }, |
|
100 |
|
101 getCommentAt: function(aIndex) { |
|
102 return this._comments[aIndex]; |
|
103 }, |
|
104 |
|
105 getStyleAt: function(aIndex) { |
|
106 return this._styles[aIndex]; |
|
107 }, |
|
108 |
|
109 getImageAt: function(aIndex) { |
|
110 return ""; |
|
111 }, |
|
112 |
|
113 getFinalCompleteValueAt: function(aIndex) { |
|
114 return this.getValueAt(aIndex); |
|
115 }, |
|
116 |
|
117 removeValueAt: function (aRowIndex, aRemoveFromDb) {}, |
|
118 |
|
119 // nsISupports implementation |
|
120 QueryInterface: function(iid) { |
|
121 if (iid.equals(Ci.nsISupports) || |
|
122 iid.equals(Ci.nsIAutoCompleteResult)) |
|
123 return this; |
|
124 |
|
125 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
126 } |
|
127 } |
|
128 |
|
129 |
|
130 |
|
131 /** |
|
132 * nsIAutoCompleteSearch implementation that always returns |
|
133 * the same result set. |
|
134 */ |
|
135 function AutoCompleteSearch(aName, aResult) { |
|
136 this.name = aName; |
|
137 this._result = aResult; |
|
138 } |
|
139 AutoCompleteSearch.prototype = { |
|
140 constructor: AutoCompleteSearch, |
|
141 |
|
142 // Search name. Used by AutoCompleteController |
|
143 name: null, |
|
144 |
|
145 // AutoCompleteResult |
|
146 _result:null, |
|
147 |
|
148 |
|
149 /** |
|
150 * Return the same result set for every search |
|
151 */ |
|
152 startSearch: function(aSearchString, |
|
153 aSearchParam, |
|
154 aPreviousResult, |
|
155 aListener) |
|
156 { |
|
157 var result = this._result; |
|
158 if (result._values.length > 0) { |
|
159 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING; |
|
160 } else { |
|
161 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH_ONGOING; |
|
162 } |
|
163 aListener.onSearchResult(this, result); |
|
164 |
|
165 if (result._values.length > 0) { |
|
166 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; |
|
167 } else { |
|
168 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH; |
|
169 } |
|
170 aListener.onSearchResult(this, result); |
|
171 }, |
|
172 |
|
173 stopSearch: function() {}, |
|
174 |
|
175 // nsISupports implementation |
|
176 QueryInterface: function(iid) { |
|
177 if (iid.equals(Ci.nsISupports) || |
|
178 iid.equals(Ci.nsIFactory) || |
|
179 iid.equals(Ci.nsIAutoCompleteSearch)) |
|
180 return this; |
|
181 |
|
182 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
183 }, |
|
184 |
|
185 // nsIFactory implementation |
|
186 createInstance: function(outer, iid) { |
|
187 return this.QueryInterface(iid); |
|
188 } |
|
189 } |
|
190 |
|
191 |
|
192 |
|
193 /** |
|
194 * Helper to register an AutoCompleteSearch with the given name. |
|
195 * Allows the AutoCompleteController to find the search. |
|
196 */ |
|
197 function registerAutoCompleteSearch(aSearch) { |
|
198 var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; |
|
199 |
|
200 var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]. |
|
201 getService(Ci.nsIUUIDGenerator); |
|
202 var cid = uuidGenerator.generateUUID(); |
|
203 |
|
204 var desc = "Test AutoCompleteSearch"; |
|
205 |
|
206 var componentManager = Components.manager |
|
207 .QueryInterface(Ci.nsIComponentRegistrar); |
|
208 componentManager.registerFactory(cid, desc, name, aSearch); |
|
209 |
|
210 // Keep the id on the object so we can unregister later |
|
211 aSearch.cid = cid; |
|
212 } |
|
213 |
|
214 |
|
215 |
|
216 /** |
|
217 * Helper to unregister an AutoCompleteSearch. |
|
218 */ |
|
219 function unregisterAutoCompleteSearch(aSearch) { |
|
220 var componentManager = Components.manager |
|
221 .QueryInterface(Ci.nsIComponentRegistrar); |
|
222 componentManager.unregisterFactory(aSearch.cid, aSearch); |
|
223 } |
|
224 |
|
225 |
|
226 |
|
227 /** |
|
228 * Test AutoComplete with multiple AutoCompleteSearch sources. |
|
229 */ |
|
230 function run_test() { |
|
231 var expected1 = ["1","2","3"]; |
|
232 var expected2 = ["a","b","c"]; |
|
233 var search1 = new AutoCompleteSearch("search1", |
|
234 new AutoCompleteResult(expected1, [], [])); |
|
235 var search2 = new AutoCompleteSearch("search2", |
|
236 new AutoCompleteResult(expected2, [], [])); |
|
237 |
|
238 // Register searches so AutoCompleteController can find them |
|
239 registerAutoCompleteSearch(search1); |
|
240 registerAutoCompleteSearch(search2); |
|
241 |
|
242 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"]. |
|
243 getService(Components.interfaces.nsIAutoCompleteController); |
|
244 |
|
245 // Make an AutoCompleteInput that uses our searches |
|
246 // and confirms results on search complete |
|
247 var input = new AutoCompleteInput([search1.name, search2.name]); |
|
248 var numSearchesStarted = 0; |
|
249 |
|
250 input.onSearchBegin = function() { |
|
251 numSearchesStarted++; |
|
252 do_check_eq(numSearchesStarted, 1); |
|
253 }; |
|
254 |
|
255 input.onSearchComplete = function() { |
|
256 |
|
257 do_check_eq(numSearchesStarted, 1); |
|
258 |
|
259 do_check_eq(controller.searchStatus, |
|
260 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH); |
|
261 do_check_eq(controller.matchCount, expected1.length + expected2.length); |
|
262 |
|
263 // Unregister searches |
|
264 unregisterAutoCompleteSearch(search1); |
|
265 unregisterAutoCompleteSearch(search2); |
|
266 |
|
267 do_test_finished(); |
|
268 }; |
|
269 |
|
270 controller.input = input; |
|
271 |
|
272 // Search is asynchronous, so don't let the test finish immediately |
|
273 do_test_pending(); |
|
274 |
|
275 controller.startSearch("test"); |
|
276 } |