|
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 393191 - AutoComplete crashes if result is null |
|
9 */ |
|
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 |
|
82 if (this._values.length > 0) { |
|
83 this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; |
|
84 } else { |
|
85 this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH; |
|
86 } |
|
87 } |
|
88 AutoCompleteResult.prototype = { |
|
89 constructor: AutoCompleteResult, |
|
90 |
|
91 // Arrays |
|
92 _values: null, |
|
93 _comments: null, |
|
94 _styles: null, |
|
95 |
|
96 searchString: "", |
|
97 searchResult: null, |
|
98 |
|
99 defaultIndex: 0, |
|
100 |
|
101 get matchCount() { |
|
102 return this._values.length; |
|
103 }, |
|
104 |
|
105 getValueAt: function(aIndex) { |
|
106 return this._values[aIndex]; |
|
107 }, |
|
108 |
|
109 getLabelAt: function(aIndex) { |
|
110 return this.getValueAt(aIndex); |
|
111 }, |
|
112 |
|
113 getCommentAt: function(aIndex) { |
|
114 return this._comments[aIndex]; |
|
115 }, |
|
116 |
|
117 getStyleAt: function(aIndex) { |
|
118 return this._styles[aIndex]; |
|
119 }, |
|
120 |
|
121 getImageAt: function(aIndex) { |
|
122 return ""; |
|
123 }, |
|
124 |
|
125 getFinalCompleteValueAt: function(aIndex) { |
|
126 return this.getValueAt(aIndex); |
|
127 }, |
|
128 |
|
129 removeValueAt: function (aRowIndex, aRemoveFromDb) {}, |
|
130 |
|
131 // nsISupports implementation |
|
132 QueryInterface: function(iid) { |
|
133 if (iid.equals(Ci.nsISupports) || |
|
134 iid.equals(Ci.nsIAutoCompleteResult)) |
|
135 return this; |
|
136 |
|
137 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
138 } |
|
139 } |
|
140 |
|
141 |
|
142 |
|
143 /** |
|
144 * nsIAutoCompleteSearch implementation that always returns |
|
145 * the same result set. |
|
146 */ |
|
147 function AutoCompleteSearch(aName, aResult) { |
|
148 this.name = aName; |
|
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 a search that returns a null result |
|
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 // Register search so AutoCompleteController can find them |
|
235 registerAutoCompleteSearch(emptySearch); |
|
236 |
|
237 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"]. |
|
238 getService(Components.interfaces.nsIAutoCompleteController); |
|
239 |
|
240 // Make an AutoCompleteInput that uses our search |
|
241 // and confirms results on search complete |
|
242 var input = new AutoCompleteInput([emptySearch.name]); |
|
243 var numSearchesStarted = 0; |
|
244 |
|
245 input.onSearchBegin = function() { |
|
246 numSearchesStarted++; |
|
247 do_check_eq(numSearchesStarted, 1); |
|
248 }; |
|
249 |
|
250 input.onSearchComplete = function() { |
|
251 |
|
252 do_check_eq(numSearchesStarted, 1); |
|
253 |
|
254 do_check_eq(controller.searchStatus, |
|
255 Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH); |
|
256 do_check_eq(controller.matchCount, 0); |
|
257 |
|
258 // Unregister searches |
|
259 unregisterAutoCompleteSearch(emptySearch); |
|
260 |
|
261 do_test_finished(); |
|
262 }; |
|
263 |
|
264 controller.input = input; |
|
265 |
|
266 // Search is asynchronous, so don't let the test finish immediately |
|
267 do_test_pending(); |
|
268 |
|
269 controller.startSearch("test"); |
|
270 } |
|
271 |