toolkit/components/autocomplete/tests/unit/head_autocomplete.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/autocomplete/tests/unit/head_autocomplete.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,192 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     1.8 +
     1.9 +const Cc = Components.classes;
    1.10 +const Ci = Components.interfaces;
    1.11 +
    1.12 +/**
    1.13 + * Dummy nsIAutoCompleteInput source that returns
    1.14 + * the given list of AutoCompleteSearch names. 
    1.15 + * 
    1.16 + * Implements only the methods needed for this test.
    1.17 + */
    1.18 +function AutoCompleteInputBase(aSearches) {
    1.19 +  this.searches = aSearches;
    1.20 +}
    1.21 +AutoCompleteInputBase.prototype = {
    1.22 + 
    1.23 +  // Array of AutoCompleteSearch names
    1.24 +  searches: null,
    1.25 +  
    1.26 +  minResultsForPopup: 0,
    1.27 +  timeout: 10,
    1.28 +  searchParam: "",
    1.29 +  textValue: "",
    1.30 +  disableAutoComplete: false,  
    1.31 +  completeDefaultIndex: false,
    1.32 +
    1.33 +  // Text selection range
    1.34 +  _selStart: 0,
    1.35 +  _selEnd: 0,
    1.36 +  get selectionStart() {
    1.37 +    return this._selStart;
    1.38 +  },
    1.39 +  get selectionEnd() {
    1.40 +    return this._selEnd;
    1.41 +  },
    1.42 +  selectTextRange: function(aStart, aEnd) {
    1.43 +    this._selStart = aStart;
    1.44 +    this._selEnd = aEnd;
    1.45 +  },
    1.46 +  
    1.47 +  get searchCount() {
    1.48 +    return this.searches.length;
    1.49 +  },
    1.50 +  
    1.51 +  getSearchAt: function(aIndex) {
    1.52 +    return this.searches[aIndex];
    1.53 +  },
    1.54 +  
    1.55 +  onSearchBegin: function() {},
    1.56 +  onSearchComplete: function() {},
    1.57 +  
    1.58 +  popupOpen: false,  
    1.59 +  
    1.60 +  popup: { 
    1.61 +    selectedIndex: 0,
    1.62 +    invalidate: function() {},
    1.63 +
    1.64 +    // nsISupports implementation
    1.65 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup])   
    1.66 +  },
    1.67 +    
    1.68 +  // nsISupports implementation
    1.69 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput])
    1.70 +}
    1.71 +
    1.72 +/** 
    1.73 + * nsIAutoCompleteResult implementation
    1.74 + */
    1.75 +function AutoCompleteResultBase(aValues) {
    1.76 +  this._values = aValues;
    1.77 +}
    1.78 +AutoCompleteResultBase.prototype = {
    1.79 +  
    1.80 +  // Arrays
    1.81 +  _values: null,
    1.82 +  _comments: [],
    1.83 +  _styles: [],
    1.84 +  _finalCompleteValues: [],
    1.85 +
    1.86 +  searchString: "",
    1.87 +  searchResult: null,
    1.88 +  
    1.89 +  defaultIndex: -1,
    1.90 +  
    1.91 +  _typeAheadResult: false,
    1.92 +  get typeAheadResult() {
    1.93 +    return this._typeAheadResult;
    1.94 +  },
    1.95 +
    1.96 +  get matchCount() {
    1.97 +    return this._values.length;
    1.98 +  },
    1.99 +
   1.100 +  getValueAt: function(aIndex) {
   1.101 +    return this._values[aIndex];
   1.102 +  },
   1.103 +
   1.104 +  getLabelAt: function(aIndex) {
   1.105 +    return this.getValueAt(aIndex);
   1.106 +  },
   1.107 +  
   1.108 +  getCommentAt: function(aIndex) {
   1.109 +    return this._comments[aIndex];
   1.110 +  },
   1.111 +  
   1.112 +  getStyleAt: function(aIndex) {
   1.113 +    return this._styles[aIndex];
   1.114 +  },
   1.115 +  
   1.116 +  getImageAt: function(aIndex) {
   1.117 +    return "";
   1.118 +  },
   1.119 +
   1.120 +  getFinalCompleteValueAt: function(aIndex) {
   1.121 +    return this._finalCompleteValues[aIndex] || this._values[aIndex];
   1.122 +  },
   1.123 +
   1.124 +  removeValueAt: function (aRowIndex, aRemoveFromDb) {},
   1.125 +
   1.126 +  // nsISupports implementation
   1.127 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult])
   1.128 +}
   1.129 +
   1.130 +/** 
   1.131 + * nsIAutoCompleteSearch implementation that always returns
   1.132 + * the same result set.
   1.133 + */
   1.134 +function AutoCompleteSearchBase(aName, aResult) {
   1.135 +  this.name = aName;
   1.136 +  this._result = aResult;
   1.137 +}
   1.138 +AutoCompleteSearchBase.prototype = {
   1.139 +  
   1.140 +  // Search name. Used by AutoCompleteController
   1.141 +  name: null,
   1.142 +
   1.143 +  // AutoCompleteResult
   1.144 +  _result: null,
   1.145 +
   1.146 +  startSearch: function(aSearchString, 
   1.147 +                        aSearchParam, 
   1.148 +                        aPreviousResult, 
   1.149 +                        aListener) {
   1.150 +    var result = this._result;
   1.151 +
   1.152 +    result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
   1.153 +    aListener.onSearchResult(this, result);
   1.154 +  },
   1.155 +  
   1.156 +  stopSearch: function() {},
   1.157 +
   1.158 +  // nsISupports implementation
   1.159 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory,
   1.160 +                                         Ci.nsIAutoCompleteSearch]),
   1.161 +  
   1.162 +  // nsIFactory implementation
   1.163 +  createInstance: function(outer, iid) {
   1.164 +    return this.QueryInterface(iid);
   1.165 +  }
   1.166 +}
   1.167 +
   1.168 +/** 
   1.169 + * Helper to register an AutoCompleteSearch with the given name.
   1.170 + * Allows the AutoCompleteController to find the search.
   1.171 + */
   1.172 +function registerAutoCompleteSearch(aSearch) {
   1.173 +  var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
   1.174 +  var cid = Cc["@mozilla.org/uuid-generator;1"].
   1.175 +            getService(Ci.nsIUUIDGenerator).
   1.176 +            generateUUID();
   1.177 +
   1.178 +  var desc = "Test AutoCompleteSearch";
   1.179 +  var componentManager = Components.manager
   1.180 +                                   .QueryInterface(Ci.nsIComponentRegistrar);
   1.181 +  componentManager.registerFactory(cid, desc, name, aSearch);
   1.182 +
   1.183 +  // Keep the id on the object so we can unregister later
   1.184 +  aSearch.cid = cid; 
   1.185 +}
   1.186 +
   1.187 +/** 
   1.188 + * Helper to unregister an AutoCompleteSearch. 
   1.189 + */
   1.190 +function unregisterAutoCompleteSearch(aSearch) {
   1.191 +  var componentManager = Components.manager
   1.192 +                                   .QueryInterface(Ci.nsIComponentRegistrar);  
   1.193 +  componentManager.unregisterFactory(aSearch.cid, aSearch);
   1.194 +}
   1.195 +

mercurial