|
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 438861 - Previous search results not returned to multiple |
|
9 * searches. |
|
10 */ |
|
11 |
|
12 |
|
13 /** |
|
14 * Dummy nsIAutoCompleteInput source that returns |
|
15 * the given list of AutoCompleteSearch names. |
|
16 * |
|
17 * Implements only the methods needed for this test. |
|
18 */ |
|
19 function AutoCompleteInput(aSearches) { |
|
20 this.searches = aSearches; |
|
21 } |
|
22 AutoCompleteInput.prototype = { |
|
23 constructor: AutoCompleteInput, |
|
24 |
|
25 // Array of AutoCompleteSearch names |
|
26 searches: null, |
|
27 |
|
28 minResultsForPopup: 0, |
|
29 timeout: 10, |
|
30 searchParam: "", |
|
31 textValue: "", |
|
32 disableAutoComplete: false, |
|
33 completeDefaultIndex: false, |
|
34 |
|
35 get searchCount() { |
|
36 return this.searches.length; |
|
37 }, |
|
38 |
|
39 getSearchAt: function(aIndex) { |
|
40 return this.searches[aIndex]; |
|
41 }, |
|
42 |
|
43 onSearchBegin: function() {}, |
|
44 onSearchComplete: function() {}, |
|
45 |
|
46 popupOpen: false, |
|
47 |
|
48 popup: { |
|
49 setSelectedIndex: function(aIndex) {}, |
|
50 invalidate: function() {}, |
|
51 |
|
52 // nsISupports implementation |
|
53 QueryInterface: function(iid) { |
|
54 if (iid.equals(Ci.nsISupports) || |
|
55 iid.equals(Ci.nsIAutoCompletePopup)) |
|
56 return this; |
|
57 |
|
58 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
59 } |
|
60 }, |
|
61 |
|
62 // nsISupports implementation |
|
63 QueryInterface: function(iid) { |
|
64 if (iid.equals(Ci.nsISupports) || |
|
65 iid.equals(Ci.nsIAutoCompleteInput)) |
|
66 return this; |
|
67 |
|
68 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
69 } |
|
70 } |
|
71 |
|
72 |
|
73 |
|
74 /** |
|
75 * nsIAutoCompleteResult implementation |
|
76 */ |
|
77 function AutoCompleteResult(aValues, aComments, aStyles) { |
|
78 this._values = aValues; |
|
79 this._comments = aComments; |
|
80 this._styles = aStyles; |
|
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 * nsIAutoCompleteSearch implementation that always returns |
|
143 * the same result set. |
|
144 */ |
|
145 function AutoCompleteSearch(aName, aResult) { |
|
146 this.name = aName; |
|
147 this._result = aResult; |
|
148 } |
|
149 AutoCompleteSearch.prototype = { |
|
150 constructor: AutoCompleteSearch, |
|
151 |
|
152 // Search name. Used by AutoCompleteController |
|
153 name: null, |
|
154 |
|
155 // AutoCompleteResult |
|
156 _result: null, |
|
157 |
|
158 _previousResult: null, |
|
159 |
|
160 |
|
161 /** |
|
162 * Return the same result set for every search |
|
163 */ |
|
164 startSearch: function(aSearchString, |
|
165 aSearchParam, |
|
166 aPreviousResult, |
|
167 aListener) |
|
168 { |
|
169 this._previousResult = aPreviousResult; |
|
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 * Helper to register an AutoCompleteSearch with the given name. |
|
194 * Allows the AutoCompleteController to find the search. |
|
195 */ |
|
196 function registerAutoCompleteSearch(aSearch) { |
|
197 var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; |
|
198 |
|
199 var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]. |
|
200 getService(Ci.nsIUUIDGenerator); |
|
201 var cid = uuidGenerator.generateUUID(); |
|
202 |
|
203 var desc = "Test AutoCompleteSearch"; |
|
204 |
|
205 var componentManager = Components.manager |
|
206 .QueryInterface(Ci.nsIComponentRegistrar); |
|
207 componentManager.registerFactory(cid, desc, name, aSearch); |
|
208 |
|
209 // Keep the id on the object so we can unregister later |
|
210 aSearch.cid = cid; |
|
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 function run_test() { |
|
227 // Make an AutoCompleteSearch that always returns nothing |
|
228 var search1 = new AutoCompleteSearch("test-previous-result1", |
|
229 new AutoCompleteResult(["hello1"], [""], [""])); |
|
230 |
|
231 var search2 = new AutoCompleteSearch("test-previous-result2", |
|
232 new AutoCompleteResult(["hello2"], [""], [""])); |
|
233 |
|
234 // Register search so AutoCompleteController can find them |
|
235 registerAutoCompleteSearch(search1); |
|
236 registerAutoCompleteSearch(search2); |
|
237 |
|
238 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"]. |
|
239 getService(Components.interfaces.nsIAutoCompleteController); |
|
240 |
|
241 // Make an AutoCompleteInput that uses our search |
|
242 // and confirms results on search complete |
|
243 var input = new AutoCompleteInput([search1.name, |
|
244 search2.name]); |
|
245 var numSearchesStarted = 0; |
|
246 var previousResult = null; |
|
247 |
|
248 input.onSearchBegin = function() { |
|
249 numSearchesStarted++; |
|
250 }; |
|
251 |
|
252 input.onSearchComplete = function() { |
|
253 do_check_eq(controller.searchStatus, |
|
254 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH); |
|
255 do_check_eq(controller.matchCount, 2); |
|
256 |
|
257 if (numSearchesStarted == 1) { |
|
258 do_check_eq(search1._previousResult, null); |
|
259 do_check_eq(search2._previousResult, null); |
|
260 |
|
261 // Now start it again |
|
262 controller.startSearch("test"); |
|
263 return; |
|
264 } |
|
265 else { |
|
266 do_check_neq(search1._previousResult, null); |
|
267 do_check_neq(search2._previousResult, null); |
|
268 } |
|
269 |
|
270 // Unregister searches |
|
271 unregisterAutoCompleteSearch(search1); |
|
272 unregisterAutoCompleteSearch(search2); |
|
273 |
|
274 do_test_finished(); |
|
275 }; |
|
276 |
|
277 controller.input = input; |
|
278 |
|
279 // Search is asynchronous, so don't let the test finish immediately |
|
280 do_test_pending(); |
|
281 |
|
282 controller.startSearch("test"); |
|
283 } |
|
284 |