no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / accessible / tests / mochitest / autocomplete.js
blobbaf9529473858718b665c6dbdfb518794d3d6c00
1 const nsISupports = Ci.nsISupports;
2 const nsIAutoCompleteResult = Ci.nsIAutoCompleteResult;
3 const nsIAutoCompleteSearch = Ci.nsIAutoCompleteSearch;
4 const nsIFactory = Ci.nsIFactory;
5 const nsIUUIDGenerator = Ci.nsIUUIDGenerator;
6 const nsIComponentRegistrar = Ci.nsIComponentRegistrar;
8 var gDefaultAutoCompleteSearch = null;
10 /**
11  * Register 'test-a11y-search' AutoCompleteSearch.
12  *
13  * @param aValues [in] set of possible results values
14  * @param aComments [in] set of possible results descriptions
15  */
16 function initAutoComplete(aValues, aComments) {
17   var allResults = new ResultsHeap(aValues, aComments);
18   gDefaultAutoCompleteSearch = new AutoCompleteSearch(
19     "test-a11y-search",
20     allResults
21   );
22   registerAutoCompleteSearch(
23     gDefaultAutoCompleteSearch,
24     "Accessibility Test AutoCompleteSearch"
25   );
28 /**
29  * Unregister 'test-a11y-search' AutoCompleteSearch.
30  */
31 function shutdownAutoComplete() {
32   unregisterAutoCompleteSearch(gDefaultAutoCompleteSearch);
33   gDefaultAutoCompleteSearch.cid = null;
34   gDefaultAutoCompleteSearch = null;
37 /**
38  * Register the given AutoCompleteSearch.
39  *
40  * @param aSearch       [in] AutoCompleteSearch object
41  * @param aDescription  [in] description of the search object
42  */
43 function registerAutoCompleteSearch(aSearch, aDescription) {
44   var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
46   var uuidGenerator =
47     Cc["@mozilla.org/uuid-generator;1"].getService(nsIUUIDGenerator);
48   var cid = uuidGenerator.generateUUID();
50   var componentManager = Components.manager.QueryInterface(
51     nsIComponentRegistrar
52   );
53   componentManager.registerFactory(cid, aDescription, name, aSearch);
55   // Keep the id on the object so we can unregister later.
56   aSearch.cid = cid;
59 /**
60  * Unregister the given AutoCompleteSearch.
61  */
62 function unregisterAutoCompleteSearch(aSearch) {
63   var componentManager = Components.manager.QueryInterface(
64     nsIComponentRegistrar
65   );
66   componentManager.unregisterFactory(aSearch.cid, aSearch);
69 /**
70  * A container to keep all possible results of autocomplete search.
71  */
72 function ResultsHeap(aValues, aComments) {
73   this.values = aValues;
74   this.comments = aComments;
77 ResultsHeap.prototype = {
78   constructor: ResultsHeap,
80   /**
81    * Return AutoCompleteResult for the given search string.
82    */
83   getAutoCompleteResultFor(aSearchString) {
84     var values = [],
85       comments = [];
86     for (var idx = 0; idx < this.values.length; idx++) {
87       if (this.values[idx].includes(aSearchString)) {
88         values.push(this.values[idx]);
89         comments.push(this.comments[idx]);
90       }
91     }
92     return new AutoCompleteResult(values, comments);
93   },
96 /**
97  * nsIAutoCompleteSearch implementation.
98  *
99  * @param aName       [in] the name of autocomplete search
100  * @param aAllResults [in] ResultsHeap object
101  */
102 function AutoCompleteSearch(aName, aAllResults) {
103   this.name = aName;
104   this.allResults = aAllResults;
107 AutoCompleteSearch.prototype = {
108   constructor: AutoCompleteSearch,
110   // nsIAutoCompleteSearch implementation
111   startSearch(aSearchString, aSearchParam, aPreviousResult, aListener) {
112     var result = this.allResults.getAutoCompleteResultFor(aSearchString);
113     aListener.onSearchResult(this, result);
114   },
116   stopSearch() {},
118   // nsISupports implementation
119   QueryInterface: ChromeUtils.generateQI([
120     "nsIFactory",
121     "nsIAutoCompleteSearch",
122   ]),
124   // nsIFactory implementation
125   createInstance(iid) {
126     return this.QueryInterface(iid);
127   },
129   // Search name. Used by AutoCompleteController.
130   name: null,
132   // Results heap.
133   allResults: null,
137  * nsIAutoCompleteResult implementation.
138  */
139 function AutoCompleteResult(aValues, aComments) {
140   this.values = aValues;
141   this.comments = aComments;
143   if (this.values.length) {
144     this.searchResult = nsIAutoCompleteResult.RESULT_SUCCESS;
145   } else {
146     this.searchResult = nsIAutoCompleteResult.NOMATCH;
147   }
150 AutoCompleteResult.prototype = {
151   constructor: AutoCompleteResult,
153   searchString: "",
154   searchResult: null,
156   defaultIndex: 0,
158   get matchCount() {
159     return this.values.length;
160   },
162   getValueAt(aIndex) {
163     return this.values[aIndex];
164   },
166   getLabelAt(aIndex) {
167     return this.getValueAt(aIndex);
168   },
170   getCommentAt(aIndex) {
171     return this.comments[aIndex];
172   },
174   getStyleAt() {
175     return null;
176   },
178   getImageAt() {
179     return "";
180   },
182   getFinalCompleteValueAt(aIndex) {
183     return this.getValueAt(aIndex);
184   },
186   isRemovableAt() {
187     return true;
188   },
190   removeValueAt() {},
192   // nsISupports implementation
193   QueryInterface: ChromeUtils.generateQI(["nsIAutoCompleteResult"]),
195   // Data
196   values: null,
197   comments: null,