1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/accessible/tests/mochitest/autocomplete.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,221 @@ 1.4 + 1.5 +const nsISupports = Components.interfaces.nsISupports; 1.6 +const nsIAutoCompleteResult = Components.interfaces.nsIAutoCompleteResult; 1.7 +const nsIAutoCompleteSearch = Components.interfaces.nsIAutoCompleteSearch; 1.8 +const nsIFactory = Components.interfaces.nsIFactory; 1.9 +const nsIUUIDGenerator = Components.interfaces.nsIUUIDGenerator; 1.10 +const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar; 1.11 + 1.12 +var gDefaultAutoCompleteSearch = null; 1.13 + 1.14 +/** 1.15 + * Register 'test-a11y-search' AutoCompleteSearch. 1.16 + * 1.17 + * @param aValues [in] set of possible results values 1.18 + * @param aComments [in] set of possible results descriptions 1.19 + */ 1.20 +function initAutoComplete(aValues, aComments) 1.21 +{ 1.22 + var allResults = new ResultsHeap(aValues, aComments); 1.23 + gDefaultAutoCompleteSearch = 1.24 + new AutoCompleteSearch("test-a11y-search", allResults); 1.25 + registerAutoCompleteSearch(gDefaultAutoCompleteSearch, 1.26 + "Accessibility Test AutoCompleteSearch"); 1.27 +} 1.28 + 1.29 +/** 1.30 + * Unregister 'test-a11y-search' AutoCompleteSearch. 1.31 + */ 1.32 +function shutdownAutoComplete() 1.33 +{ 1.34 + unregisterAutoCompleteSearch(gDefaultAutoCompleteSearch); 1.35 + gDefaultAutoCompleteSearch.cid = null; 1.36 + gDefaultAutoCompleteSearch = null; 1.37 +} 1.38 + 1.39 + 1.40 +/** 1.41 + * Register the given AutoCompleteSearch. 1.42 + * 1.43 + * @param aSearch [in] AutoCompleteSearch object 1.44 + * @param aDescription [in] description of the search object 1.45 + */ 1.46 +function registerAutoCompleteSearch(aSearch, aDescription) 1.47 +{ 1.48 + var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; 1.49 + 1.50 + var uuidGenerator = Components.classes["@mozilla.org/uuid-generator;1"]. 1.51 + getService(nsIUUIDGenerator); 1.52 + var cid = uuidGenerator.generateUUID(); 1.53 + 1.54 + var componentManager = Components.manager.QueryInterface(nsIComponentRegistrar); 1.55 + componentManager.registerFactory(cid, aDescription, name, aSearch); 1.56 + 1.57 + // Keep the id on the object so we can unregister later. 1.58 + aSearch.cid = cid; 1.59 +} 1.60 + 1.61 +/** 1.62 + * Unregister the given AutoCompleteSearch. 1.63 + */ 1.64 +function unregisterAutoCompleteSearch(aSearch) 1.65 +{ 1.66 + var componentManager = Components.manager.QueryInterface(nsIComponentRegistrar); 1.67 + componentManager.unregisterFactory(aSearch.cid, aSearch); 1.68 +} 1.69 + 1.70 + 1.71 +/** 1.72 + * A container to keep all possible results of autocomplete search. 1.73 + */ 1.74 +function ResultsHeap(aValues, aComments) 1.75 +{ 1.76 + this.values = aValues; 1.77 + this.comments = aComments; 1.78 +} 1.79 + 1.80 +ResultsHeap.prototype = 1.81 +{ 1.82 + constructor: ResultsHeap, 1.83 + 1.84 + /** 1.85 + * Return AutoCompleteResult for the given search string. 1.86 + */ 1.87 + getAutoCompleteResultFor: function(aSearchString) 1.88 + { 1.89 + var values = [], comments = []; 1.90 + for (var idx = 0; idx < this.values.length; idx++) { 1.91 + if (this.values[idx].indexOf(aSearchString) != -1) { 1.92 + values.push(this.values[idx]); 1.93 + comments.push(this.comments[idx]); 1.94 + } 1.95 + } 1.96 + return new AutoCompleteResult(values, comments); 1.97 + } 1.98 +} 1.99 + 1.100 + 1.101 +/** 1.102 + * nsIAutoCompleteSearch implementation. 1.103 + * 1.104 + * @param aName [in] the name of autocomplete search 1.105 + * @param aAllResults [in] ResultsHeap object 1.106 + */ 1.107 +function AutoCompleteSearch(aName, aAllResults) 1.108 +{ 1.109 + this.name = aName; 1.110 + this.allResults = aAllResults; 1.111 +} 1.112 + 1.113 +AutoCompleteSearch.prototype = 1.114 +{ 1.115 + constructor: AutoCompleteSearch, 1.116 + 1.117 + // nsIAutoCompleteSearch implementation 1.118 + startSearch: function(aSearchString, aSearchParam, aPreviousResult, 1.119 + aListener) 1.120 + { 1.121 + var result = this.allResults.getAutoCompleteResultFor(aSearchString); 1.122 + aListener.onSearchResult(this, result); 1.123 + }, 1.124 + 1.125 + stopSearch: function() {}, 1.126 + 1.127 + // nsISupports implementation 1.128 + QueryInterface: function(iid) 1.129 + { 1.130 + if (iid.equals(nsISupports) || 1.131 + iid.equals(nsIFactory) || 1.132 + iid.equals(nsIAutoCompleteSearch)) 1.133 + return this; 1.134 + 1.135 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.136 + }, 1.137 + 1.138 + // nsIFactory implementation 1.139 + createInstance: function(outer, iid) 1.140 + { 1.141 + return this.QueryInterface(iid); 1.142 + }, 1.143 + 1.144 + // Search name. Used by AutoCompleteController. 1.145 + name: null, 1.146 + 1.147 + // Results heap. 1.148 + allResults: null 1.149 +} 1.150 + 1.151 + 1.152 +/** 1.153 + * nsIAutoCompleteResult implementation. 1.154 + */ 1.155 +function AutoCompleteResult(aValues, aComments) 1.156 +{ 1.157 + this.values = aValues; 1.158 + this.comments = aComments; 1.159 + 1.160 + if (this.values.length > 0) 1.161 + this.searchResult = nsIAutoCompleteResult.RESULT_SUCCESS; 1.162 + else 1.163 + this.searchResult = nsIAutoCompleteResult.NOMATCH; 1.164 +} 1.165 + 1.166 +AutoCompleteResult.prototype = 1.167 +{ 1.168 + constructor: AutoCompleteResult, 1.169 + 1.170 + searchString: "", 1.171 + searchResult: null, 1.172 + 1.173 + defaultIndex: 0, 1.174 + 1.175 + get matchCount() 1.176 + { 1.177 + return this.values.length; 1.178 + }, 1.179 + 1.180 + getValueAt: function(aIndex) 1.181 + { 1.182 + return this.values[aIndex]; 1.183 + }, 1.184 + 1.185 + getLabelAt: function(aIndex) 1.186 + { 1.187 + return this.getValueAt(aIndex); 1.188 + }, 1.189 + 1.190 + getCommentAt: function(aIndex) 1.191 + { 1.192 + return this.comments[aIndex]; 1.193 + }, 1.194 + 1.195 + getStyleAt: function(aIndex) 1.196 + { 1.197 + return null; 1.198 + }, 1.199 + 1.200 + getImageAt: function(aIndex) 1.201 + { 1.202 + return ""; 1.203 + }, 1.204 + 1.205 + getFinalCompleteValueAt: function(aIndex) 1.206 + { 1.207 + return this.getValueAt(aIndex); 1.208 + }, 1.209 + 1.210 + removeValueAt: function (aRowIndex, aRemoveFromDb) {}, 1.211 + 1.212 + // nsISupports implementation 1.213 + QueryInterface: function(iid) { 1.214 + if (iid.equals(nsISupports) || 1.215 + iid.equals(nsIAutoCompleteResult)) 1.216 + return this; 1.217 + 1.218 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.219 + }, 1.220 + 1.221 + // Data 1.222 + values: null, 1.223 + comments: null 1.224 +}