1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_440866.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,285 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/** 1.9 + * Unit test for Bug 440866 - First AutoCompleteSearch that returns 1.10 + * RESULT_NOMATCH cancels all other searches when popup is open 1.11 + */ 1.12 + 1.13 + 1.14 + 1.15 +/** 1.16 + * Dummy nsIAutoCompleteInput source that returns 1.17 + * the given list of AutoCompleteSearch names. 1.18 + * 1.19 + * Implements only the methods needed for this test. 1.20 + */ 1.21 +function AutoCompleteInput(aSearches) { 1.22 + this.searches = aSearches; 1.23 +} 1.24 +AutoCompleteInput.prototype = { 1.25 + constructor: AutoCompleteInput, 1.26 + 1.27 + // Array of AutoCompleteSearch names 1.28 + searches: null, 1.29 + 1.30 + minResultsForPopup: 0, 1.31 + timeout: 10, 1.32 + searchParam: "", 1.33 + textValue: "", 1.34 + disableAutoComplete: false, 1.35 + completeDefaultIndex: false, 1.36 + 1.37 + get searchCount() { 1.38 + return this.searches.length; 1.39 + }, 1.40 + 1.41 + getSearchAt: function(aIndex) { 1.42 + return this.searches[aIndex]; 1.43 + }, 1.44 + 1.45 + onSearchBegin: function() {}, 1.46 + onSearchComplete: function() {}, 1.47 + 1.48 + popupOpen: false, 1.49 + 1.50 + popup: { 1.51 + setSelectedIndex: function(aIndex) {}, 1.52 + invalidate: function() {}, 1.53 + 1.54 + // nsISupports implementation 1.55 + QueryInterface: function(iid) { 1.56 + if (iid.equals(Ci.nsISupports) || 1.57 + iid.equals(Ci.nsIAutoCompletePopup)) 1.58 + return this; 1.59 + 1.60 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.61 + } 1.62 + }, 1.63 + 1.64 + // nsISupports implementation 1.65 + QueryInterface: function(iid) { 1.66 + if (iid.equals(Ci.nsISupports) || 1.67 + iid.equals(Ci.nsIAutoCompleteInput)) 1.68 + return this; 1.69 + 1.70 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.71 + } 1.72 +} 1.73 + 1.74 + 1.75 + 1.76 +/** 1.77 + * nsIAutoCompleteResult implementation 1.78 + */ 1.79 +function AutoCompleteResult(aValues, aComments, aStyles) { 1.80 + this._values = aValues; 1.81 + this._comments = aComments; 1.82 + this._styles = aStyles; 1.83 + 1.84 + if (this._values.length > 0) { 1.85 + this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; 1.86 + } else { 1.87 + this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH; 1.88 + } 1.89 +} 1.90 +AutoCompleteResult.prototype = { 1.91 + constructor: AutoCompleteResult, 1.92 + 1.93 + // Arrays 1.94 + _values: null, 1.95 + _comments: null, 1.96 + _styles: null, 1.97 + 1.98 + searchString: "", 1.99 + searchResult: null, 1.100 + 1.101 + defaultIndex: 0, 1.102 + 1.103 + get matchCount() { 1.104 + return this._values.length; 1.105 + }, 1.106 + 1.107 + getValueAt: function(aIndex) { 1.108 + return this._values[aIndex]; 1.109 + }, 1.110 + 1.111 + getLabelAt: function(aIndex) { 1.112 + return this.getValueAt(aIndex); 1.113 + }, 1.114 + 1.115 + getCommentAt: function(aIndex) { 1.116 + return this._comments[aIndex]; 1.117 + }, 1.118 + 1.119 + getStyleAt: function(aIndex) { 1.120 + return this._styles[aIndex]; 1.121 + }, 1.122 + 1.123 + getImageAt: function(aIndex) { 1.124 + return ""; 1.125 + }, 1.126 + 1.127 + getFinalCompleteValueAt: function(aIndex) { 1.128 + return this.getValueAt(aIndex); 1.129 + }, 1.130 + 1.131 + removeValueAt: function (aRowIndex, aRemoveFromDb) {}, 1.132 + 1.133 + // nsISupports implementation 1.134 + QueryInterface: function(iid) { 1.135 + if (iid.equals(Ci.nsISupports) || 1.136 + iid.equals(Ci.nsIAutoCompleteResult)) 1.137 + return this; 1.138 + 1.139 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.140 + } 1.141 +} 1.142 + 1.143 + 1.144 + 1.145 +/** 1.146 + * nsIAutoCompleteSearch implementation that always returns 1.147 + * the same result set. 1.148 + */ 1.149 +function AutoCompleteSearch(aName, aResult) { 1.150 + this.name = aName; 1.151 + this._result = aResult; 1.152 +} 1.153 +AutoCompleteSearch.prototype = { 1.154 + constructor: AutoCompleteSearch, 1.155 + 1.156 + // Search name. Used by AutoCompleteController 1.157 + name: null, 1.158 + 1.159 + // AutoCompleteResult 1.160 + _result:null, 1.161 + 1.162 + 1.163 + /** 1.164 + * Return the same result set for every search 1.165 + */ 1.166 + startSearch: function(aSearchString, 1.167 + aSearchParam, 1.168 + aPreviousResult, 1.169 + aListener) 1.170 + { 1.171 + aListener.onSearchResult(this, this._result); 1.172 + }, 1.173 + 1.174 + stopSearch: function() {}, 1.175 + 1.176 + // nsISupports implementation 1.177 + QueryInterface: function(iid) { 1.178 + if (iid.equals(Ci.nsISupports) || 1.179 + iid.equals(Ci.nsIFactory) || 1.180 + iid.equals(Ci.nsIAutoCompleteSearch)) 1.181 + return this; 1.182 + 1.183 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.184 + }, 1.185 + 1.186 + // nsIFactory implementation 1.187 + createInstance: function(outer, iid) { 1.188 + return this.QueryInterface(iid); 1.189 + } 1.190 +} 1.191 + 1.192 + 1.193 + 1.194 +/** 1.195 + * Helper to register an AutoCompleteSearch with the given name. 1.196 + * Allows the AutoCompleteController to find the search. 1.197 + */ 1.198 +function registerAutoCompleteSearch(aSearch) { 1.199 + var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; 1.200 + 1.201 + var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]. 1.202 + getService(Ci.nsIUUIDGenerator); 1.203 + var cid = uuidGenerator.generateUUID(); 1.204 + 1.205 + var desc = "Test AutoCompleteSearch"; 1.206 + 1.207 + var componentManager = Components.manager 1.208 + .QueryInterface(Ci.nsIComponentRegistrar); 1.209 + componentManager.registerFactory(cid, desc, name, aSearch); 1.210 + 1.211 + // Keep the id on the object so we can unregister later 1.212 + aSearch.cid = cid; 1.213 +} 1.214 + 1.215 + 1.216 + 1.217 +/** 1.218 + * Helper to unregister an AutoCompleteSearch. 1.219 + */ 1.220 +function unregisterAutoCompleteSearch(aSearch) { 1.221 + var componentManager = Components.manager 1.222 + .QueryInterface(Ci.nsIComponentRegistrar); 1.223 + componentManager.unregisterFactory(aSearch.cid, aSearch); 1.224 +} 1.225 + 1.226 + 1.227 + 1.228 +/** 1.229 + * Test AutoComplete with multiple AutoCompleteSearch sources. 1.230 + */ 1.231 +function run_test() { 1.232 + 1.233 + // Make an AutoCompleteSearch that always returns nothing 1.234 + var emptySearch = new AutoCompleteSearch("test-empty-search", 1.235 + new AutoCompleteResult([], [], [])); 1.236 + 1.237 + // Make an AutoCompleteSearch that returns two values 1.238 + var expectedValues = ["test1", "test2"]; 1.239 + var regularSearch = new AutoCompleteSearch("test-regular-search", 1.240 + new AutoCompleteResult(expectedValues, [], [])); 1.241 + 1.242 + // Register searches so AutoCompleteController can find them 1.243 + registerAutoCompleteSearch(emptySearch); 1.244 + registerAutoCompleteSearch(regularSearch); 1.245 + 1.246 + var controller = Components.classes["@mozilla.org/autocomplete/controller;1"]. 1.247 + getService(Components.interfaces.nsIAutoCompleteController); 1.248 + 1.249 + // Make an AutoCompleteInput that uses our searches 1.250 + // and confirms results on search complete 1.251 + var input = new AutoCompleteInput([emptySearch.name, regularSearch.name]); 1.252 + var numSearchesStarted = 0; 1.253 + 1.254 + input.onSearchBegin = function() { 1.255 + numSearchesStarted++; 1.256 + do_check_eq(numSearchesStarted, 1); 1.257 + do_check_eq(input.searchCount, 2); 1.258 + }; 1.259 + 1.260 + input.onSearchComplete = function() { 1.261 + do_check_eq(numSearchesStarted, 1); 1.262 + 1.263 + do_check_eq(controller.searchStatus, 1.264 + Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH); 1.265 + do_check_eq(controller.matchCount, 2); 1.266 + 1.267 + // Confirm expected result values 1.268 + for (var i = 0; i < expectedValues.length; i++) { 1.269 + do_check_eq(expectedValues[i], controller.getValueAt(i)); 1.270 + } 1.271 + 1.272 + do_check_true(input.popupOpen); 1.273 + 1.274 + // Unregister searches 1.275 + unregisterAutoCompleteSearch(emptySearch); 1.276 + unregisterAutoCompleteSearch(regularSearch); 1.277 + 1.278 + do_test_finished(); 1.279 + }; 1.280 + 1.281 + controller.input = input; 1.282 + 1.283 + // Search is asynchronous, so don't let the test finish immediately 1.284 + do_test_pending(); 1.285 + 1.286 + controller.startSearch("test"); 1.287 +} 1.288 +