Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
7 /**
8 * Unit test for Bug 438861 - Previous search results not returned to multiple
9 * searches.
10 */
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,
25 // Array of AutoCompleteSearch names
26 searches: null,
28 minResultsForPopup: 0,
29 timeout: 10,
30 searchParam: "",
31 textValue: "",
32 disableAutoComplete: false,
33 completeDefaultIndex: false,
35 get searchCount() {
36 return this.searches.length;
37 },
39 getSearchAt: function(aIndex) {
40 return this.searches[aIndex];
41 },
43 onSearchBegin: function() {},
44 onSearchComplete: function() {},
46 popupOpen: false,
48 popup: {
49 setSelectedIndex: function(aIndex) {},
50 invalidate: function() {},
52 // nsISupports implementation
53 QueryInterface: function(iid) {
54 if (iid.equals(Ci.nsISupports) ||
55 iid.equals(Ci.nsIAutoCompletePopup))
56 return this;
58 throw Components.results.NS_ERROR_NO_INTERFACE;
59 }
60 },
62 // nsISupports implementation
63 QueryInterface: function(iid) {
64 if (iid.equals(Ci.nsISupports) ||
65 iid.equals(Ci.nsIAutoCompleteInput))
66 return this;
68 throw Components.results.NS_ERROR_NO_INTERFACE;
69 }
70 }
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,
90 // Arrays
91 _values: null,
92 _comments: null,
93 _styles: null,
95 searchString: "",
96 searchResult: null,
98 defaultIndex: 0,
100 get matchCount() {
101 return this._values.length;
102 },
104 getValueAt: function(aIndex) {
105 return this._values[aIndex];
106 },
108 getLabelAt: function(aIndex) {
109 return this.getValueAt(aIndex);
110 },
112 getCommentAt: function(aIndex) {
113 return this._comments[aIndex];
114 },
116 getStyleAt: function(aIndex) {
117 return this._styles[aIndex];
118 },
120 getImageAt: function(aIndex) {
121 return "";
122 },
124 getFinalCompleteValueAt: function(aIndex) {
125 return this.getValueAt(aIndex);
126 },
128 removeValueAt: function (aRowIndex, aRemoveFromDb) {},
130 // nsISupports implementation
131 QueryInterface: function(iid) {
132 if (iid.equals(Ci.nsISupports) ||
133 iid.equals(Ci.nsIAutoCompleteResult))
134 return this;
136 throw Components.results.NS_ERROR_NO_INTERFACE;
137 }
138 }
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,
152 // Search name. Used by AutoCompleteController
153 name: null,
155 // AutoCompleteResult
156 _result: null,
158 _previousResult: null,
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 },
173 stopSearch: function() {},
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;
182 throw Components.results.NS_ERROR_NO_INTERFACE;
183 },
185 // nsIFactory implementation
186 createInstance: function(outer, iid) {
187 return this.QueryInterface(iid);
188 }
189 }
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;
199 var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].
200 getService(Ci.nsIUUIDGenerator);
201 var cid = uuidGenerator.generateUUID();
203 var desc = "Test AutoCompleteSearch";
205 var componentManager = Components.manager
206 .QueryInterface(Ci.nsIComponentRegistrar);
207 componentManager.registerFactory(cid, desc, name, aSearch);
209 // Keep the id on the object so we can unregister later
210 aSearch.cid = cid;
211 }
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 }
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"], [""], [""]));
231 var search2 = new AutoCompleteSearch("test-previous-result2",
232 new AutoCompleteResult(["hello2"], [""], [""]));
234 // Register search so AutoCompleteController can find them
235 registerAutoCompleteSearch(search1);
236 registerAutoCompleteSearch(search2);
238 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
239 getService(Components.interfaces.nsIAutoCompleteController);
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;
248 input.onSearchBegin = function() {
249 numSearchesStarted++;
250 };
252 input.onSearchComplete = function() {
253 do_check_eq(controller.searchStatus,
254 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
255 do_check_eq(controller.matchCount, 2);
257 if (numSearchesStarted == 1) {
258 do_check_eq(search1._previousResult, null);
259 do_check_eq(search2._previousResult, null);
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 }
270 // Unregister searches
271 unregisterAutoCompleteSearch(search1);
272 unregisterAutoCompleteSearch(search2);
274 do_test_finished();
275 };
277 controller.input = input;
279 // Search is asynchronous, so don't let the test finish immediately
280 do_test_pending();
282 controller.startSearch("test");
283 }