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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_378079.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,285 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * Unit test for Bug 378079 - AutoComplete returns invalid rows when
    1.12 + * more than one AutoCompleteSearch is used.
    1.13 + */
    1.14 +
    1.15 +
    1.16 +
    1.17 +/**
    1.18 + * Dummy nsIAutoCompleteInput source that returns
    1.19 + * the given list of AutoCompleteSearch names. 
    1.20 + * 
    1.21 + * Implements only the methods needed for this test.
    1.22 + */
    1.23 +function AutoCompleteInput(aSearches) {
    1.24 +  this.searches = aSearches;
    1.25 +}
    1.26 +AutoCompleteInput.prototype = {
    1.27 +  constructor: AutoCompleteInput, 
    1.28 +  
    1.29 +  // Array of AutoCompleteSearch names
    1.30 +  searches: null,
    1.31 +  
    1.32 +  minResultsForPopup: 0,
    1.33 +  timeout: 10,
    1.34 +  searchParam: "",
    1.35 +  textValue: "",
    1.36 +  disableAutoComplete: false,  
    1.37 +  completeDefaultIndex: false,
    1.38 +  
    1.39 +  get searchCount() {
    1.40 +    return this.searches.length;
    1.41 +  },
    1.42 +  
    1.43 +  getSearchAt: function(aIndex) {
    1.44 +    return this.searches[aIndex];
    1.45 +  },
    1.46 +  
    1.47 +  onSearchBegin: function() {},
    1.48 +  onSearchComplete: function() {},
    1.49 +  
    1.50 +  popupOpen: false,  
    1.51 +  
    1.52 +  popup: { 
    1.53 +    setSelectedIndex: function(aIndex) {},
    1.54 +    invalidate: function() {},
    1.55 +
    1.56 +    // nsISupports implementation
    1.57 +    QueryInterface: function(iid) {
    1.58 +      if (iid.equals(Ci.nsISupports) ||
    1.59 +          iid.equals(Ci.nsIAutoCompletePopup))
    1.60 +        return this;
    1.61 +
    1.62 +      throw Components.results.NS_ERROR_NO_INTERFACE;
    1.63 +    }    
    1.64 +  },
    1.65 +    
    1.66 +  // nsISupports implementation
    1.67 +  QueryInterface: function(iid) {
    1.68 +    if (iid.equals(Ci.nsISupports) ||
    1.69 +        iid.equals(Ci.nsIAutoCompleteInput))
    1.70 +      return this;
    1.71 +
    1.72 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.73 +  }
    1.74 +}
    1.75 +
    1.76 +
    1.77 +
    1.78 +/** 
    1.79 + * nsIAutoCompleteResult implementation
    1.80 + */
    1.81 +function AutoCompleteResult(aValues, aComments, aStyles) {
    1.82 +  this._values = aValues;
    1.83 +  this._comments = aComments;
    1.84 +  this._styles = aStyles;
    1.85 +  
    1.86 +  if (this._values.length > 0) {
    1.87 +    this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
    1.88 +  } else {
    1.89 +    this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH;
    1.90 +  }
    1.91 +}
    1.92 +AutoCompleteResult.prototype = {
    1.93 +  constructor: AutoCompleteResult,
    1.94 +  
    1.95 +  // Arrays
    1.96 +  _values: null,
    1.97 +  _comments: null,
    1.98 +  _styles: null,
    1.99 +  
   1.100 +  searchString: "",
   1.101 +  searchResult: null,
   1.102 +  
   1.103 +  defaultIndex: 0,
   1.104 +
   1.105 +  get matchCount() {
   1.106 +    return this._values.length;
   1.107 +  },
   1.108 +
   1.109 +  getValueAt: function(aIndex) {
   1.110 +    return this._values[aIndex];
   1.111 +  },
   1.112 + 
   1.113 +  getLabelAt: function(aIndex) {
   1.114 +    return this.getValueAt(aIndex);
   1.115 +  },
   1.116 +  
   1.117 +  getCommentAt: function(aIndex) {
   1.118 +    return this._comments[aIndex];
   1.119 +  },
   1.120 +  
   1.121 +  getStyleAt: function(aIndex) {
   1.122 +    return this._styles[aIndex];
   1.123 +  },
   1.124 +  
   1.125 +  getImageAt: function(aIndex) {
   1.126 +    return "";
   1.127 +  },
   1.128 +
   1.129 +  getFinalCompleteValueAt: function(aIndex) {
   1.130 +    return this.getValueAt(aIndex);
   1.131 +  },
   1.132 +
   1.133 +  removeValueAt: function (aRowIndex, aRemoveFromDb) {},
   1.134 +
   1.135 +  // nsISupports implementation
   1.136 +  QueryInterface: function(iid) {
   1.137 +    if (iid.equals(Ci.nsISupports) ||
   1.138 +        iid.equals(Ci.nsIAutoCompleteResult))
   1.139 +      return this;
   1.140 +
   1.141 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.142 +  }  
   1.143 +}
   1.144 +
   1.145 +
   1.146 +
   1.147 +/** 
   1.148 + * nsIAutoCompleteSearch implementation that always returns
   1.149 + * the same result set.
   1.150 + */
   1.151 +function AutoCompleteSearch(aName, aResult) {
   1.152 +  this.name = aName;
   1.153 +  this._result = aResult;
   1.154 +}
   1.155 +AutoCompleteSearch.prototype = {
   1.156 +  constructor: AutoCompleteSearch,
   1.157 +  
   1.158 +  // Search name. Used by AutoCompleteController
   1.159 +  name: null,
   1.160 +
   1.161 +  // AutoCompleteResult
   1.162 +  _result:null,  
   1.163 +  
   1.164 +  
   1.165 +  /**
   1.166 +   * Return the same result set for every search
   1.167 +   */
   1.168 +  startSearch: function(aSearchString, 
   1.169 +                        aSearchParam, 
   1.170 +                        aPreviousResult, 
   1.171 +                        aListener) 
   1.172 +  {
   1.173 +    aListener.onSearchResult(this, this._result);
   1.174 +  },
   1.175 +  
   1.176 +  stopSearch: function() {},
   1.177 +
   1.178 +  // nsISupports implementation
   1.179 +  QueryInterface: function(iid) {
   1.180 +    if (iid.equals(Ci.nsISupports) ||
   1.181 +        iid.equals(Ci.nsIFactory) ||
   1.182 +        iid.equals(Ci.nsIAutoCompleteSearch))
   1.183 +      return this;
   1.184 +
   1.185 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.186 +  },
   1.187 +  
   1.188 +  // nsIFactory implementation
   1.189 +  createInstance: function(outer, iid) {
   1.190 +    return this.QueryInterface(iid);
   1.191 +  }
   1.192 +}
   1.193 +
   1.194 +
   1.195 +
   1.196 +/** 
   1.197 + * Helper to register an AutoCompleteSearch with the given name.
   1.198 + * Allows the AutoCompleteController to find the search.
   1.199 + */
   1.200 +function registerAutoCompleteSearch(aSearch) {
   1.201 +  var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
   1.202 +
   1.203 +  var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].
   1.204 +                      getService(Ci.nsIUUIDGenerator);
   1.205 +  var cid = uuidGenerator.generateUUID();
   1.206 +
   1.207 +  var desc = "Test AutoCompleteSearch";
   1.208 +  
   1.209 +  var componentManager = Components.manager
   1.210 +                                   .QueryInterface(Ci.nsIComponentRegistrar);
   1.211 +  componentManager.registerFactory(cid, desc, name, aSearch);
   1.212 +
   1.213 +  // Keep the id on the object so we can unregister later
   1.214 +  aSearch.cid = cid; 
   1.215 +}
   1.216 +
   1.217 +
   1.218 +
   1.219 +/** 
   1.220 + * Helper to unregister an AutoCompleteSearch. 
   1.221 + */
   1.222 +function unregisterAutoCompleteSearch(aSearch) {
   1.223 +  var componentManager = Components.manager
   1.224 +                                   .QueryInterface(Ci.nsIComponentRegistrar);  
   1.225 +  componentManager.unregisterFactory(aSearch.cid, aSearch);
   1.226 +}
   1.227 +
   1.228 +
   1.229 +
   1.230 +/** 
   1.231 + * Test AutoComplete with multiple AutoCompleteSearch sources.
   1.232 + */
   1.233 +function run_test() {
   1.234 +  
   1.235 +  // Make an AutoCompleteSearch that always returns nothing
   1.236 +  var emptySearch = new AutoCompleteSearch("test-empty-search", 
   1.237 +                             new AutoCompleteResult([], [], []));
   1.238 +  
   1.239 +  // Make an AutoCompleteSearch that returns two values
   1.240 +  var expectedValues = ["test1", "test2"];
   1.241 +  var regularSearch = new AutoCompleteSearch("test-regular-search",
   1.242 +                             new AutoCompleteResult(expectedValues, [], []));
   1.243 +  
   1.244 +  // Register searches so AutoCompleteController can find them
   1.245 +  registerAutoCompleteSearch(emptySearch);
   1.246 +  registerAutoCompleteSearch(regularSearch);
   1.247 +    
   1.248 +  var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
   1.249 +                   getService(Components.interfaces.nsIAutoCompleteController);  
   1.250 +  
   1.251 +  // Make an AutoCompleteInput that uses our searches
   1.252 +  // and confirms results on search complete
   1.253 +  var input = new AutoCompleteInput([emptySearch.name, regularSearch.name]);
   1.254 +  var numSearchesStarted = 0;
   1.255 +
   1.256 +  input.onSearchBegin = function() {
   1.257 +    numSearchesStarted++;
   1.258 +    do_check_eq(numSearchesStarted, 1);
   1.259 +  };
   1.260 +
   1.261 +  input.onSearchComplete = function() {
   1.262 +
   1.263 +    do_check_eq(numSearchesStarted, 1);
   1.264 +
   1.265 +    do_check_eq(controller.searchStatus, 
   1.266 +                Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
   1.267 +    do_check_eq(controller.matchCount, 2);
   1.268 +
   1.269 +    // Confirm expected result values
   1.270 +    for (var i = 0; i < expectedValues.length; i++) {
   1.271 +      do_check_eq(expectedValues[i], controller.getValueAt(i)); 
   1.272 +    }
   1.273 +
   1.274 +    // Unregister searches
   1.275 +    unregisterAutoCompleteSearch(emptySearch);
   1.276 +    unregisterAutoCompleteSearch(regularSearch);
   1.277 +
   1.278 +    do_test_finished();
   1.279 +  };
   1.280 +
   1.281 +  controller.input = input;
   1.282 +
   1.283 +  // Search is asynchronous, so don't let the test finish immediately
   1.284 +  do_test_pending();
   1.285 +  
   1.286 +  controller.startSearch("test");
   1.287 +}
   1.288 +

mercurial