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