|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
5 |
|
6 const Cc = Components.classes; |
|
7 const Ci = Components.interfaces; |
|
8 |
|
9 /** |
|
10 * Dummy nsIAutoCompleteInput source that returns |
|
11 * the given list of AutoCompleteSearch names. |
|
12 * |
|
13 * Implements only the methods needed for this test. |
|
14 */ |
|
15 function AutoCompleteInputBase(aSearches) { |
|
16 this.searches = aSearches; |
|
17 } |
|
18 AutoCompleteInputBase.prototype = { |
|
19 |
|
20 // Array of AutoCompleteSearch names |
|
21 searches: null, |
|
22 |
|
23 minResultsForPopup: 0, |
|
24 timeout: 10, |
|
25 searchParam: "", |
|
26 textValue: "", |
|
27 disableAutoComplete: false, |
|
28 completeDefaultIndex: false, |
|
29 |
|
30 // Text selection range |
|
31 _selStart: 0, |
|
32 _selEnd: 0, |
|
33 get selectionStart() { |
|
34 return this._selStart; |
|
35 }, |
|
36 get selectionEnd() { |
|
37 return this._selEnd; |
|
38 }, |
|
39 selectTextRange: function(aStart, aEnd) { |
|
40 this._selStart = aStart; |
|
41 this._selEnd = aEnd; |
|
42 }, |
|
43 |
|
44 get searchCount() { |
|
45 return this.searches.length; |
|
46 }, |
|
47 |
|
48 getSearchAt: function(aIndex) { |
|
49 return this.searches[aIndex]; |
|
50 }, |
|
51 |
|
52 onSearchBegin: function() {}, |
|
53 onSearchComplete: function() {}, |
|
54 |
|
55 popupOpen: false, |
|
56 |
|
57 popup: { |
|
58 selectedIndex: 0, |
|
59 invalidate: function() {}, |
|
60 |
|
61 // nsISupports implementation |
|
62 QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup]) |
|
63 }, |
|
64 |
|
65 // nsISupports implementation |
|
66 QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput]) |
|
67 } |
|
68 |
|
69 /** |
|
70 * nsIAutoCompleteResult implementation |
|
71 */ |
|
72 function AutoCompleteResultBase(aValues) { |
|
73 this._values = aValues; |
|
74 } |
|
75 AutoCompleteResultBase.prototype = { |
|
76 |
|
77 // Arrays |
|
78 _values: null, |
|
79 _comments: [], |
|
80 _styles: [], |
|
81 _finalCompleteValues: [], |
|
82 |
|
83 searchString: "", |
|
84 searchResult: null, |
|
85 |
|
86 defaultIndex: -1, |
|
87 |
|
88 _typeAheadResult: false, |
|
89 get typeAheadResult() { |
|
90 return this._typeAheadResult; |
|
91 }, |
|
92 |
|
93 get matchCount() { |
|
94 return this._values.length; |
|
95 }, |
|
96 |
|
97 getValueAt: function(aIndex) { |
|
98 return this._values[aIndex]; |
|
99 }, |
|
100 |
|
101 getLabelAt: function(aIndex) { |
|
102 return this.getValueAt(aIndex); |
|
103 }, |
|
104 |
|
105 getCommentAt: function(aIndex) { |
|
106 return this._comments[aIndex]; |
|
107 }, |
|
108 |
|
109 getStyleAt: function(aIndex) { |
|
110 return this._styles[aIndex]; |
|
111 }, |
|
112 |
|
113 getImageAt: function(aIndex) { |
|
114 return ""; |
|
115 }, |
|
116 |
|
117 getFinalCompleteValueAt: function(aIndex) { |
|
118 return this._finalCompleteValues[aIndex] || this._values[aIndex]; |
|
119 }, |
|
120 |
|
121 removeValueAt: function (aRowIndex, aRemoveFromDb) {}, |
|
122 |
|
123 // nsISupports implementation |
|
124 QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult]) |
|
125 } |
|
126 |
|
127 /** |
|
128 * nsIAutoCompleteSearch implementation that always returns |
|
129 * the same result set. |
|
130 */ |
|
131 function AutoCompleteSearchBase(aName, aResult) { |
|
132 this.name = aName; |
|
133 this._result = aResult; |
|
134 } |
|
135 AutoCompleteSearchBase.prototype = { |
|
136 |
|
137 // Search name. Used by AutoCompleteController |
|
138 name: null, |
|
139 |
|
140 // AutoCompleteResult |
|
141 _result: null, |
|
142 |
|
143 startSearch: function(aSearchString, |
|
144 aSearchParam, |
|
145 aPreviousResult, |
|
146 aListener) { |
|
147 var result = this._result; |
|
148 |
|
149 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; |
|
150 aListener.onSearchResult(this, result); |
|
151 }, |
|
152 |
|
153 stopSearch: function() {}, |
|
154 |
|
155 // nsISupports implementation |
|
156 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory, |
|
157 Ci.nsIAutoCompleteSearch]), |
|
158 |
|
159 // nsIFactory implementation |
|
160 createInstance: function(outer, iid) { |
|
161 return this.QueryInterface(iid); |
|
162 } |
|
163 } |
|
164 |
|
165 /** |
|
166 * Helper to register an AutoCompleteSearch with the given name. |
|
167 * Allows the AutoCompleteController to find the search. |
|
168 */ |
|
169 function registerAutoCompleteSearch(aSearch) { |
|
170 var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; |
|
171 var cid = Cc["@mozilla.org/uuid-generator;1"]. |
|
172 getService(Ci.nsIUUIDGenerator). |
|
173 generateUUID(); |
|
174 |
|
175 var desc = "Test AutoCompleteSearch"; |
|
176 var componentManager = Components.manager |
|
177 .QueryInterface(Ci.nsIComponentRegistrar); |
|
178 componentManager.registerFactory(cid, desc, name, aSearch); |
|
179 |
|
180 // Keep the id on the object so we can unregister later |
|
181 aSearch.cid = cid; |
|
182 } |
|
183 |
|
184 /** |
|
185 * Helper to unregister an AutoCompleteSearch. |
|
186 */ |
|
187 function unregisterAutoCompleteSearch(aSearch) { |
|
188 var componentManager = Components.manager |
|
189 .QueryInterface(Ci.nsIComponentRegistrar); |
|
190 componentManager.unregisterFactory(aSearch.cid, aSearch); |
|
191 } |
|
192 |