1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_previousResult.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,284 @@ 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 + * Unit test for Bug 438861 - Previous search results not returned to multiple 1.12 + * searches. 1.13 + */ 1.14 + 1.15 + 1.16 +/** 1.17 + * Dummy nsIAutoCompleteInput source that returns 1.18 + * the given list of AutoCompleteSearch names. 1.19 + * 1.20 + * Implements only the methods needed for this test. 1.21 + */ 1.22 +function AutoCompleteInput(aSearches) { 1.23 + this.searches = aSearches; 1.24 +} 1.25 +AutoCompleteInput.prototype = { 1.26 + constructor: AutoCompleteInput, 1.27 + 1.28 + // Array of AutoCompleteSearch names 1.29 + searches: null, 1.30 + 1.31 + minResultsForPopup: 0, 1.32 + timeout: 10, 1.33 + searchParam: "", 1.34 + textValue: "", 1.35 + disableAutoComplete: false, 1.36 + completeDefaultIndex: false, 1.37 + 1.38 + get searchCount() { 1.39 + return this.searches.length; 1.40 + }, 1.41 + 1.42 + getSearchAt: function(aIndex) { 1.43 + return this.searches[aIndex]; 1.44 + }, 1.45 + 1.46 + onSearchBegin: function() {}, 1.47 + onSearchComplete: function() {}, 1.48 + 1.49 + popupOpen: false, 1.50 + 1.51 + popup: { 1.52 + setSelectedIndex: function(aIndex) {}, 1.53 + invalidate: function() {}, 1.54 + 1.55 + // nsISupports implementation 1.56 + QueryInterface: function(iid) { 1.57 + if (iid.equals(Ci.nsISupports) || 1.58 + iid.equals(Ci.nsIAutoCompletePopup)) 1.59 + return this; 1.60 + 1.61 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.62 + } 1.63 + }, 1.64 + 1.65 + // nsISupports implementation 1.66 + QueryInterface: function(iid) { 1.67 + if (iid.equals(Ci.nsISupports) || 1.68 + iid.equals(Ci.nsIAutoCompleteInput)) 1.69 + return this; 1.70 + 1.71 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.72 + } 1.73 +} 1.74 + 1.75 + 1.76 + 1.77 +/** 1.78 + * nsIAutoCompleteResult implementation 1.79 + */ 1.80 +function AutoCompleteResult(aValues, aComments, aStyles) { 1.81 + this._values = aValues; 1.82 + this._comments = aComments; 1.83 + this._styles = aStyles; 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 + * nsIAutoCompleteSearch implementation that always returns 1.146 + * the same result set. 1.147 + */ 1.148 +function AutoCompleteSearch(aName, aResult) { 1.149 + this.name = aName; 1.150 + this._result = aResult; 1.151 +} 1.152 +AutoCompleteSearch.prototype = { 1.153 + constructor: AutoCompleteSearch, 1.154 + 1.155 + // Search name. Used by AutoCompleteController 1.156 + name: null, 1.157 + 1.158 + // AutoCompleteResult 1.159 + _result: null, 1.160 + 1.161 + _previousResult: null, 1.162 + 1.163 + 1.164 + /** 1.165 + * Return the same result set for every search 1.166 + */ 1.167 + startSearch: function(aSearchString, 1.168 + aSearchParam, 1.169 + aPreviousResult, 1.170 + aListener) 1.171 + { 1.172 + this._previousResult = aPreviousResult; 1.173 + aListener.onSearchResult(this, 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 + * Helper to register an AutoCompleteSearch with the given name. 1.197 + * Allows the AutoCompleteController to find the search. 1.198 + */ 1.199 +function registerAutoCompleteSearch(aSearch) { 1.200 + var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; 1.201 + 1.202 + var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]. 1.203 + getService(Ci.nsIUUIDGenerator); 1.204 + var cid = uuidGenerator.generateUUID(); 1.205 + 1.206 + var desc = "Test AutoCompleteSearch"; 1.207 + 1.208 + var componentManager = Components.manager 1.209 + .QueryInterface(Ci.nsIComponentRegistrar); 1.210 + componentManager.registerFactory(cid, desc, name, aSearch); 1.211 + 1.212 + // Keep the id on the object so we can unregister later 1.213 + aSearch.cid = cid; 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 +function run_test() { 1.230 + // Make an AutoCompleteSearch that always returns nothing 1.231 + var search1 = new AutoCompleteSearch("test-previous-result1", 1.232 + new AutoCompleteResult(["hello1"], [""], [""])); 1.233 + 1.234 + var search2 = new AutoCompleteSearch("test-previous-result2", 1.235 + new AutoCompleteResult(["hello2"], [""], [""])); 1.236 + 1.237 + // Register search so AutoCompleteController can find them 1.238 + registerAutoCompleteSearch(search1); 1.239 + registerAutoCompleteSearch(search2); 1.240 + 1.241 + var controller = Components.classes["@mozilla.org/autocomplete/controller;1"]. 1.242 + getService(Components.interfaces.nsIAutoCompleteController); 1.243 + 1.244 + // Make an AutoCompleteInput that uses our search 1.245 + // and confirms results on search complete 1.246 + var input = new AutoCompleteInput([search1.name, 1.247 + search2.name]); 1.248 + var numSearchesStarted = 0; 1.249 + var previousResult = null; 1.250 + 1.251 + input.onSearchBegin = function() { 1.252 + numSearchesStarted++; 1.253 + }; 1.254 + 1.255 + input.onSearchComplete = function() { 1.256 + do_check_eq(controller.searchStatus, 1.257 + Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH); 1.258 + do_check_eq(controller.matchCount, 2); 1.259 + 1.260 + if (numSearchesStarted == 1) { 1.261 + do_check_eq(search1._previousResult, null); 1.262 + do_check_eq(search2._previousResult, null); 1.263 + 1.264 + // Now start it again 1.265 + controller.startSearch("test"); 1.266 + return; 1.267 + } 1.268 + else { 1.269 + do_check_neq(search1._previousResult, null); 1.270 + do_check_neq(search2._previousResult, null); 1.271 + } 1.272 + 1.273 + // Unregister searches 1.274 + unregisterAutoCompleteSearch(search1); 1.275 + unregisterAutoCompleteSearch(search2); 1.276 + 1.277 + do_test_finished(); 1.278 + }; 1.279 + 1.280 + controller.input = input; 1.281 + 1.282 + // Search is asynchronous, so don't let the test finish immediately 1.283 + do_test_pending(); 1.284 + 1.285 + controller.startSearch("test"); 1.286 +} 1.287 +