michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: /** michael@0: * Dummy nsIAutoCompleteInput source that returns michael@0: * the given list of AutoCompleteSearch names. michael@0: * michael@0: * Implements only the methods needed for this test. michael@0: */ michael@0: function AutoCompleteInputBase(aSearches) { michael@0: this.searches = aSearches; michael@0: } michael@0: AutoCompleteInputBase.prototype = { michael@0: michael@0: // Array of AutoCompleteSearch names michael@0: searches: null, michael@0: michael@0: minResultsForPopup: 0, michael@0: timeout: 10, michael@0: searchParam: "", michael@0: textValue: "", michael@0: disableAutoComplete: false, michael@0: completeDefaultIndex: false, michael@0: michael@0: // Text selection range michael@0: _selStart: 0, michael@0: _selEnd: 0, michael@0: get selectionStart() { michael@0: return this._selStart; michael@0: }, michael@0: get selectionEnd() { michael@0: return this._selEnd; michael@0: }, michael@0: selectTextRange: function(aStart, aEnd) { michael@0: this._selStart = aStart; michael@0: this._selEnd = aEnd; michael@0: }, michael@0: michael@0: get searchCount() { michael@0: return this.searches.length; michael@0: }, michael@0: michael@0: getSearchAt: function(aIndex) { michael@0: return this.searches[aIndex]; michael@0: }, michael@0: michael@0: onSearchBegin: function() {}, michael@0: onSearchComplete: function() {}, michael@0: michael@0: popupOpen: false, michael@0: michael@0: popup: { michael@0: selectedIndex: 0, michael@0: invalidate: function() {}, michael@0: michael@0: // nsISupports implementation michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup]) michael@0: }, michael@0: michael@0: // nsISupports implementation michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput]) michael@0: } michael@0: michael@0: /** michael@0: * nsIAutoCompleteResult implementation michael@0: */ michael@0: function AutoCompleteResultBase(aValues) { michael@0: this._values = aValues; michael@0: } michael@0: AutoCompleteResultBase.prototype = { michael@0: michael@0: // Arrays michael@0: _values: null, michael@0: _comments: [], michael@0: _styles: [], michael@0: _finalCompleteValues: [], michael@0: michael@0: searchString: "", michael@0: searchResult: null, michael@0: michael@0: defaultIndex: -1, michael@0: michael@0: _typeAheadResult: false, michael@0: get typeAheadResult() { michael@0: return this._typeAheadResult; michael@0: }, michael@0: michael@0: get matchCount() { michael@0: return this._values.length; michael@0: }, michael@0: michael@0: getValueAt: function(aIndex) { michael@0: return this._values[aIndex]; michael@0: }, michael@0: michael@0: getLabelAt: function(aIndex) { michael@0: return this.getValueAt(aIndex); michael@0: }, michael@0: michael@0: getCommentAt: function(aIndex) { michael@0: return this._comments[aIndex]; michael@0: }, michael@0: michael@0: getStyleAt: function(aIndex) { michael@0: return this._styles[aIndex]; michael@0: }, michael@0: michael@0: getImageAt: function(aIndex) { michael@0: return ""; michael@0: }, michael@0: michael@0: getFinalCompleteValueAt: function(aIndex) { michael@0: return this._finalCompleteValues[aIndex] || this._values[aIndex]; michael@0: }, michael@0: michael@0: removeValueAt: function (aRowIndex, aRemoveFromDb) {}, michael@0: michael@0: // nsISupports implementation michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult]) michael@0: } michael@0: michael@0: /** michael@0: * nsIAutoCompleteSearch implementation that always returns michael@0: * the same result set. michael@0: */ michael@0: function AutoCompleteSearchBase(aName, aResult) { michael@0: this.name = aName; michael@0: this._result = aResult; michael@0: } michael@0: AutoCompleteSearchBase.prototype = { michael@0: michael@0: // Search name. Used by AutoCompleteController michael@0: name: null, michael@0: michael@0: // AutoCompleteResult michael@0: _result: null, michael@0: michael@0: startSearch: function(aSearchString, michael@0: aSearchParam, michael@0: aPreviousResult, michael@0: aListener) { michael@0: var result = this._result; michael@0: michael@0: result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS; michael@0: aListener.onSearchResult(this, result); michael@0: }, michael@0: michael@0: stopSearch: function() {}, michael@0: michael@0: // nsISupports implementation michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory, michael@0: Ci.nsIAutoCompleteSearch]), michael@0: michael@0: // nsIFactory implementation michael@0: createInstance: function(outer, iid) { michael@0: return this.QueryInterface(iid); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Helper to register an AutoCompleteSearch with the given name. michael@0: * Allows the AutoCompleteController to find the search. michael@0: */ michael@0: function registerAutoCompleteSearch(aSearch) { michael@0: var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; michael@0: var cid = Cc["@mozilla.org/uuid-generator;1"]. michael@0: getService(Ci.nsIUUIDGenerator). michael@0: generateUUID(); michael@0: michael@0: var desc = "Test AutoCompleteSearch"; michael@0: var componentManager = Components.manager michael@0: .QueryInterface(Ci.nsIComponentRegistrar); michael@0: componentManager.registerFactory(cid, desc, name, aSearch); michael@0: michael@0: // Keep the id on the object so we can unregister later michael@0: aSearch.cid = cid; michael@0: } michael@0: michael@0: /** michael@0: * Helper to unregister an AutoCompleteSearch. michael@0: */ michael@0: function unregisterAutoCompleteSearch(aSearch) { michael@0: var componentManager = Components.manager michael@0: .QueryInterface(Ci.nsIComponentRegistrar); michael@0: componentManager.unregisterFactory(aSearch.cid, aSearch); michael@0: } michael@0: