1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_393191.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,271 @@ 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 393191 - AutoComplete crashes if result is null 1.12 + */ 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 + 1.85 + if (this._values.length > 0) { 1.86 + this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; 1.87 + } else { 1.88 + this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH; 1.89 + } 1.90 +} 1.91 +AutoCompleteResult.prototype = { 1.92 + constructor: AutoCompleteResult, 1.93 + 1.94 + // Arrays 1.95 + _values: null, 1.96 + _comments: null, 1.97 + _styles: null, 1.98 + 1.99 + searchString: "", 1.100 + searchResult: null, 1.101 + 1.102 + defaultIndex: 0, 1.103 + 1.104 + get matchCount() { 1.105 + return this._values.length; 1.106 + }, 1.107 + 1.108 + getValueAt: function(aIndex) { 1.109 + return this._values[aIndex]; 1.110 + }, 1.111 + 1.112 + getLabelAt: function(aIndex) { 1.113 + return this.getValueAt(aIndex); 1.114 + }, 1.115 + 1.116 + getCommentAt: function(aIndex) { 1.117 + return this._comments[aIndex]; 1.118 + }, 1.119 + 1.120 + getStyleAt: function(aIndex) { 1.121 + return this._styles[aIndex]; 1.122 + }, 1.123 + 1.124 + getImageAt: function(aIndex) { 1.125 + return ""; 1.126 + }, 1.127 + 1.128 + getFinalCompleteValueAt: function(aIndex) { 1.129 + return this.getValueAt(aIndex); 1.130 + }, 1.131 + 1.132 + removeValueAt: function (aRowIndex, aRemoveFromDb) {}, 1.133 + 1.134 + // nsISupports implementation 1.135 + QueryInterface: function(iid) { 1.136 + if (iid.equals(Ci.nsISupports) || 1.137 + iid.equals(Ci.nsIAutoCompleteResult)) 1.138 + return this; 1.139 + 1.140 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.141 + } 1.142 +} 1.143 + 1.144 + 1.145 + 1.146 +/** 1.147 + * nsIAutoCompleteSearch implementation that always returns 1.148 + * the same result set. 1.149 + */ 1.150 +function AutoCompleteSearch(aName, aResult) { 1.151 + this.name = aName; 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 a search that returns a null result 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 + // Register search so AutoCompleteController can find them 1.238 + registerAutoCompleteSearch(emptySearch); 1.239 + 1.240 + var controller = Components.classes["@mozilla.org/autocomplete/controller;1"]. 1.241 + getService(Components.interfaces.nsIAutoCompleteController); 1.242 + 1.243 + // Make an AutoCompleteInput that uses our search 1.244 + // and confirms results on search complete 1.245 + var input = new AutoCompleteInput([emptySearch.name]); 1.246 + var numSearchesStarted = 0; 1.247 + 1.248 + input.onSearchBegin = function() { 1.249 + numSearchesStarted++; 1.250 + do_check_eq(numSearchesStarted, 1); 1.251 + }; 1.252 + 1.253 + input.onSearchComplete = function() { 1.254 + 1.255 + do_check_eq(numSearchesStarted, 1); 1.256 + 1.257 + do_check_eq(controller.searchStatus, 1.258 + Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH); 1.259 + do_check_eq(controller.matchCount, 0); 1.260 + 1.261 + // Unregister searches 1.262 + unregisterAutoCompleteSearch(emptySearch); 1.263 + 1.264 + do_test_finished(); 1.265 + }; 1.266 + 1.267 + controller.input = input; 1.268 + 1.269 + // Search is asynchronous, so don't let the test finish immediately 1.270 + do_test_pending(); 1.271 + 1.272 + controller.startSearch("test"); 1.273 +} 1.274 +