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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /**
     6  * Unit test for Bug 440866 - First AutoCompleteSearch that returns
     7  * RESULT_NOMATCH cancels all other searches when popup is open
     8  */
    12 /**
    13  * Dummy nsIAutoCompleteInput source that returns
    14  * the given list of AutoCompleteSearch names.
    15  *
    16  * Implements only the methods needed for this test.
    17  */
    18 function AutoCompleteInput(aSearches) {
    19   this.searches = aSearches;
    20 }
    21 AutoCompleteInput.prototype = {
    22   constructor: AutoCompleteInput,
    24   // Array of AutoCompleteSearch names
    25   searches: null,
    27   minResultsForPopup: 0,
    28   timeout: 10,
    29   searchParam: "",
    30   textValue: "",
    31   disableAutoComplete: false,
    32   completeDefaultIndex: false,
    34   get searchCount() {
    35     return this.searches.length;
    36   },
    38   getSearchAt: function(aIndex) {
    39     return this.searches[aIndex];
    40   },
    42   onSearchBegin: function() {},
    43   onSearchComplete: function() {},
    45   popupOpen: false,
    47   popup: {
    48     setSelectedIndex: function(aIndex) {},
    49     invalidate: function() {},
    51     // nsISupports implementation
    52     QueryInterface: function(iid) {
    53       if (iid.equals(Ci.nsISupports) ||
    54           iid.equals(Ci.nsIAutoCompletePopup))
    55         return this;
    57       throw Components.results.NS_ERROR_NO_INTERFACE;
    58     }
    59   },
    61   // nsISupports implementation
    62   QueryInterface: function(iid) {
    63     if (iid.equals(Ci.nsISupports) ||
    64         iid.equals(Ci.nsIAutoCompleteInput))
    65       return this;
    67     throw Components.results.NS_ERROR_NO_INTERFACE;
    68   }
    69 }
    73 /**
    74  * nsIAutoCompleteResult implementation
    75  */
    76 function AutoCompleteResult(aValues, aComments, aStyles) {
    77   this._values = aValues;
    78   this._comments = aComments;
    79   this._styles = aStyles;
    81   if (this._values.length > 0) {
    82     this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
    83   } else {
    84     this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH;
    85   }
    86 }
    87 AutoCompleteResult.prototype = {
    88   constructor: AutoCompleteResult,
    90   // Arrays
    91   _values: null,
    92   _comments: null,
    93   _styles: null,
    95   searchString: "",
    96   searchResult: null,
    98   defaultIndex: 0,
   100   get matchCount() {
   101     return this._values.length;
   102   },
   104   getValueAt: function(aIndex) {
   105     return this._values[aIndex];
   106   },
   108   getLabelAt: function(aIndex) {
   109     return this.getValueAt(aIndex);
   110   },
   112   getCommentAt: function(aIndex) {
   113     return this._comments[aIndex];
   114   },
   116   getStyleAt: function(aIndex) {
   117     return this._styles[aIndex];
   118   },
   120   getImageAt: function(aIndex) {
   121     return "";
   122   },
   124   getFinalCompleteValueAt: function(aIndex) {
   125     return this.getValueAt(aIndex);
   126   },
   128   removeValueAt: function (aRowIndex, aRemoveFromDb) {},
   130   // nsISupports implementation
   131   QueryInterface: function(iid) {
   132     if (iid.equals(Ci.nsISupports) ||
   133         iid.equals(Ci.nsIAutoCompleteResult))
   134       return this;
   136     throw Components.results.NS_ERROR_NO_INTERFACE;
   137   }
   138 }
   142 /**
   143  * nsIAutoCompleteSearch implementation that always returns
   144  * the same result set.
   145  */
   146 function AutoCompleteSearch(aName, aResult) {
   147   this.name = aName;
   148   this._result = aResult;
   149 }
   150 AutoCompleteSearch.prototype = {
   151   constructor: AutoCompleteSearch,
   153   // Search name. Used by AutoCompleteController
   154   name: null,
   156   // AutoCompleteResult
   157   _result:null,
   160   /**
   161    * Return the same result set for every search
   162    */
   163   startSearch: function(aSearchString,
   164                         aSearchParam,
   165                         aPreviousResult,
   166                         aListener)
   167   {
   168     aListener.onSearchResult(this, this._result);
   169   },
   171   stopSearch: function() {},
   173   // nsISupports implementation
   174   QueryInterface: function(iid) {
   175     if (iid.equals(Ci.nsISupports) ||
   176         iid.equals(Ci.nsIFactory) ||
   177         iid.equals(Ci.nsIAutoCompleteSearch))
   178       return this;
   180     throw Components.results.NS_ERROR_NO_INTERFACE;
   181   },
   183   // nsIFactory implementation
   184   createInstance: function(outer, iid) {
   185     return this.QueryInterface(iid);
   186   }
   187 }
   191 /**
   192  * Helper to register an AutoCompleteSearch with the given name.
   193  * Allows the AutoCompleteController to find the search.
   194  */
   195 function registerAutoCompleteSearch(aSearch) {
   196   var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
   198   var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].
   199                       getService(Ci.nsIUUIDGenerator);
   200   var cid = uuidGenerator.generateUUID();
   202   var desc = "Test AutoCompleteSearch";
   204   var componentManager = Components.manager
   205                                    .QueryInterface(Ci.nsIComponentRegistrar);
   206   componentManager.registerFactory(cid, desc, name, aSearch);
   208   // Keep the id on the object so we can unregister later
   209   aSearch.cid = cid;
   210 }
   214 /**
   215  * Helper to unregister an AutoCompleteSearch.
   216  */
   217 function unregisterAutoCompleteSearch(aSearch) {
   218   var componentManager = Components.manager
   219                                    .QueryInterface(Ci.nsIComponentRegistrar);
   220   componentManager.unregisterFactory(aSearch.cid, aSearch);
   221 }
   225 /**
   226  * Test AutoComplete with multiple AutoCompleteSearch sources.
   227  */
   228 function run_test() {
   230   // Make an AutoCompleteSearch that always returns nothing
   231   var emptySearch = new AutoCompleteSearch("test-empty-search",
   232                              new AutoCompleteResult([], [], []));
   234   // Make an AutoCompleteSearch that returns two values
   235   var expectedValues = ["test1", "test2"];
   236   var regularSearch = new AutoCompleteSearch("test-regular-search",
   237                              new AutoCompleteResult(expectedValues, [], []));
   239   // Register searches so AutoCompleteController can find them
   240   registerAutoCompleteSearch(emptySearch);
   241   registerAutoCompleteSearch(regularSearch);
   243   var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
   244                    getService(Components.interfaces.nsIAutoCompleteController);
   246   // Make an AutoCompleteInput that uses our searches
   247   // and confirms results on search complete
   248   var input = new AutoCompleteInput([emptySearch.name, regularSearch.name]);
   249   var numSearchesStarted = 0;
   251   input.onSearchBegin = function() {
   252     numSearchesStarted++;
   253     do_check_eq(numSearchesStarted, 1);
   254     do_check_eq(input.searchCount, 2);
   255   };
   257   input.onSearchComplete = function() {
   258     do_check_eq(numSearchesStarted, 1);
   260     do_check_eq(controller.searchStatus,
   261                 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
   262     do_check_eq(controller.matchCount, 2);
   264     // Confirm expected result values
   265     for (var i = 0; i < expectedValues.length; i++) {
   266       do_check_eq(expectedValues[i], controller.getValueAt(i));
   267     }
   269     do_check_true(input.popupOpen);
   271     // Unregister searches
   272     unregisterAutoCompleteSearch(emptySearch);
   273     unregisterAutoCompleteSearch(regularSearch);
   275     do_test_finished();
   276   };
   278   controller.input = input;
   280   // Search is asynchronous, so don't let the test finish immediately
   281   do_test_pending();
   283   controller.startSearch("test");
   284 }

mercurial