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