|
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 * Unit test for Bug 378079 - AutoComplete returns invalid rows when |
|
9 * more than one AutoCompleteSearch is used. |
|
10 */ |
|
11 |
|
12 |
|
13 |
|
14 /** |
|
15 * Dummy nsIAutoCompleteInput source that returns |
|
16 * the given list of AutoCompleteSearch names. |
|
17 * |
|
18 * Implements only the methods needed for this test. |
|
19 */ |
|
20 function AutoCompleteInput(aSearches) { |
|
21 this.searches = aSearches; |
|
22 } |
|
23 AutoCompleteInput.prototype = { |
|
24 constructor: AutoCompleteInput, |
|
25 |
|
26 // Array of AutoCompleteSearch names |
|
27 searches: null, |
|
28 |
|
29 minResultsForPopup: 0, |
|
30 timeout: 10, |
|
31 searchParam: "", |
|
32 textValue: "", |
|
33 disableAutoComplete: false, |
|
34 completeDefaultIndex: false, |
|
35 |
|
36 get searchCount() { |
|
37 return this.searches.length; |
|
38 }, |
|
39 |
|
40 getSearchAt: function(aIndex) { |
|
41 return this.searches[aIndex]; |
|
42 }, |
|
43 |
|
44 onSearchBegin: function() {}, |
|
45 onSearchComplete: function() {}, |
|
46 |
|
47 popupOpen: false, |
|
48 |
|
49 popup: { |
|
50 setSelectedIndex: function(aIndex) {}, |
|
51 invalidate: function() {}, |
|
52 |
|
53 // nsISupports implementation |
|
54 QueryInterface: function(iid) { |
|
55 if (iid.equals(Ci.nsISupports) || |
|
56 iid.equals(Ci.nsIAutoCompletePopup)) |
|
57 return this; |
|
58 |
|
59 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
60 } |
|
61 }, |
|
62 |
|
63 // nsISupports implementation |
|
64 QueryInterface: function(iid) { |
|
65 if (iid.equals(Ci.nsISupports) || |
|
66 iid.equals(Ci.nsIAutoCompleteInput)) |
|
67 return this; |
|
68 |
|
69 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
70 } |
|
71 } |
|
72 |
|
73 |
|
74 |
|
75 /** |
|
76 * nsIAutoCompleteResult implementation |
|
77 */ |
|
78 function AutoCompleteResult(aValues, aComments, aStyles) { |
|
79 this._values = aValues; |
|
80 this._comments = aComments; |
|
81 this._styles = aStyles; |
|
82 |
|
83 if (this._values.length > 0) { |
|
84 this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; |
|
85 } else { |
|
86 this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH; |
|
87 } |
|
88 } |
|
89 AutoCompleteResult.prototype = { |
|
90 constructor: AutoCompleteResult, |
|
91 |
|
92 // Arrays |
|
93 _values: null, |
|
94 _comments: null, |
|
95 _styles: null, |
|
96 |
|
97 searchString: "", |
|
98 searchResult: null, |
|
99 |
|
100 defaultIndex: 0, |
|
101 |
|
102 get matchCount() { |
|
103 return this._values.length; |
|
104 }, |
|
105 |
|
106 getValueAt: function(aIndex) { |
|
107 return this._values[aIndex]; |
|
108 }, |
|
109 |
|
110 getLabelAt: function(aIndex) { |
|
111 return this.getValueAt(aIndex); |
|
112 }, |
|
113 |
|
114 getCommentAt: function(aIndex) { |
|
115 return this._comments[aIndex]; |
|
116 }, |
|
117 |
|
118 getStyleAt: function(aIndex) { |
|
119 return this._styles[aIndex]; |
|
120 }, |
|
121 |
|
122 getImageAt: function(aIndex) { |
|
123 return ""; |
|
124 }, |
|
125 |
|
126 getFinalCompleteValueAt: function(aIndex) { |
|
127 return this.getValueAt(aIndex); |
|
128 }, |
|
129 |
|
130 removeValueAt: function (aRowIndex, aRemoveFromDb) {}, |
|
131 |
|
132 // nsISupports implementation |
|
133 QueryInterface: function(iid) { |
|
134 if (iid.equals(Ci.nsISupports) || |
|
135 iid.equals(Ci.nsIAutoCompleteResult)) |
|
136 return this; |
|
137 |
|
138 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
139 } |
|
140 } |
|
141 |
|
142 |
|
143 |
|
144 /** |
|
145 * nsIAutoCompleteSearch implementation that always returns |
|
146 * the same result set. |
|
147 */ |
|
148 function AutoCompleteSearch(aName, aResult) { |
|
149 this.name = aName; |
|
150 this._result = aResult; |
|
151 } |
|
152 AutoCompleteSearch.prototype = { |
|
153 constructor: AutoCompleteSearch, |
|
154 |
|
155 // Search name. Used by AutoCompleteController |
|
156 name: null, |
|
157 |
|
158 // AutoCompleteResult |
|
159 _result:null, |
|
160 |
|
161 |
|
162 /** |
|
163 * Return the same result set for every search |
|
164 */ |
|
165 startSearch: function(aSearchString, |
|
166 aSearchParam, |
|
167 aPreviousResult, |
|
168 aListener) |
|
169 { |
|
170 aListener.onSearchResult(this, 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 |
|
232 // Make an AutoCompleteSearch that always returns nothing |
|
233 var emptySearch = new AutoCompleteSearch("test-empty-search", |
|
234 new AutoCompleteResult([], [], [])); |
|
235 |
|
236 // Make an AutoCompleteSearch that returns two values |
|
237 var expectedValues = ["test1", "test2"]; |
|
238 var regularSearch = new AutoCompleteSearch("test-regular-search", |
|
239 new AutoCompleteResult(expectedValues, [], [])); |
|
240 |
|
241 // Register searches so AutoCompleteController can find them |
|
242 registerAutoCompleteSearch(emptySearch); |
|
243 registerAutoCompleteSearch(regularSearch); |
|
244 |
|
245 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"]. |
|
246 getService(Components.interfaces.nsIAutoCompleteController); |
|
247 |
|
248 // Make an AutoCompleteInput that uses our searches |
|
249 // and confirms results on search complete |
|
250 var input = new AutoCompleteInput([emptySearch.name, regularSearch.name]); |
|
251 var numSearchesStarted = 0; |
|
252 |
|
253 input.onSearchBegin = function() { |
|
254 numSearchesStarted++; |
|
255 do_check_eq(numSearchesStarted, 1); |
|
256 }; |
|
257 |
|
258 input.onSearchComplete = function() { |
|
259 |
|
260 do_check_eq(numSearchesStarted, 1); |
|
261 |
|
262 do_check_eq(controller.searchStatus, |
|
263 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH); |
|
264 do_check_eq(controller.matchCount, 2); |
|
265 |
|
266 // Confirm expected result values |
|
267 for (var i = 0; i < expectedValues.length; i++) { |
|
268 do_check_eq(expectedValues[i], controller.getValueAt(i)); |
|
269 } |
|
270 |
|
271 // Unregister searches |
|
272 unregisterAutoCompleteSearch(emptySearch); |
|
273 unregisterAutoCompleteSearch(regularSearch); |
|
274 |
|
275 do_test_finished(); |
|
276 }; |
|
277 |
|
278 controller.input = input; |
|
279 |
|
280 // Search is asynchronous, so don't let the test finish immediately |
|
281 do_test_pending(); |
|
282 |
|
283 controller.startSearch("test"); |
|
284 } |
|
285 |