toolkit/components/autocomplete/tests/unit/test_393191.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 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:set ts=2 sw=2 sts=2 et: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 /**
     8  * Unit test for Bug 393191 - AutoComplete crashes if result is null
     9  */
    13 /**
    14  * Dummy nsIAutoCompleteInput source that returns
    15  * the given list of AutoCompleteSearch names. 
    16  * 
    17  * Implements only the methods needed for this test.
    18  */
    19 function AutoCompleteInput(aSearches) {
    20   this.searches = aSearches;
    21 }
    22 AutoCompleteInput.prototype = {
    23   constructor: AutoCompleteInput, 
    25   // Array of AutoCompleteSearch names
    26   searches: null,
    28   minResultsForPopup: 0,
    29   timeout: 10,
    30   searchParam: "",
    31   textValue: "",
    32   disableAutoComplete: false,  
    33   completeDefaultIndex: false,
    35   get searchCount() {
    36     return this.searches.length;
    37   },
    39   getSearchAt: function(aIndex) {
    40     return this.searches[aIndex];
    41   },
    43   onSearchBegin: function() {},
    44   onSearchComplete: function() {},
    46   popupOpen: false,  
    48   popup: { 
    49     setSelectedIndex: function(aIndex) {},
    50     invalidate: function() {},
    52     // nsISupports implementation
    53     QueryInterface: function(iid) {
    54       if (iid.equals(Ci.nsISupports) ||
    55           iid.equals(Ci.nsIAutoCompletePopup))
    56         return this;
    58       throw Components.results.NS_ERROR_NO_INTERFACE;
    59     }    
    60   },
    62   // nsISupports implementation
    63   QueryInterface: function(iid) {
    64     if (iid.equals(Ci.nsISupports) ||
    65         iid.equals(Ci.nsIAutoCompleteInput))
    66       return this;
    68     throw Components.results.NS_ERROR_NO_INTERFACE;
    69   }
    70 }
    74 /** 
    75  * nsIAutoCompleteResult implementation
    76  */
    77 function AutoCompleteResult(aValues, aComments, aStyles) {
    78   this._values = aValues;
    79   this._comments = aComments;
    80   this._styles = aStyles;
    82   if (this._values.length > 0) {
    83     this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
    84   } else {
    85     this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH;
    86   }
    87 }
    88 AutoCompleteResult.prototype = {
    89   constructor: AutoCompleteResult,
    91   // Arrays
    92   _values: null,
    93   _comments: null,
    94   _styles: null,
    96   searchString: "",
    97   searchResult: null,
    99   defaultIndex: 0,
   101   get matchCount() {
   102     return this._values.length;
   103   },
   105   getValueAt: function(aIndex) {
   106     return this._values[aIndex];
   107   },
   109   getLabelAt: function(aIndex) {
   110     return this.getValueAt(aIndex);
   111   },
   113   getCommentAt: function(aIndex) {
   114     return this._comments[aIndex];
   115   },
   117   getStyleAt: function(aIndex) {
   118     return this._styles[aIndex];
   119   },
   121   getImageAt: function(aIndex) {
   122     return "";
   123   },
   125   getFinalCompleteValueAt: function(aIndex) {
   126     return this.getValueAt(aIndex);
   127   },
   129   removeValueAt: function (aRowIndex, aRemoveFromDb) {},
   131   // nsISupports implementation
   132   QueryInterface: function(iid) {
   133     if (iid.equals(Ci.nsISupports) ||
   134         iid.equals(Ci.nsIAutoCompleteResult))
   135       return this;
   137     throw Components.results.NS_ERROR_NO_INTERFACE;
   138   }  
   139 }
   143 /** 
   144  * nsIAutoCompleteSearch implementation that always returns
   145  * the same result set.
   146  */
   147 function AutoCompleteSearch(aName, aResult) {
   148   this.name = aName;
   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 a search that returns a null result
   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   // Register search so AutoCompleteController can find them
   235   registerAutoCompleteSearch(emptySearch);
   237   var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
   238                    getService(Components.interfaces.nsIAutoCompleteController);  
   240   // Make an AutoCompleteInput that uses our search
   241   // and confirms results on search complete
   242   var input = new AutoCompleteInput([emptySearch.name]);
   243   var numSearchesStarted = 0;
   245   input.onSearchBegin = function() {
   246     numSearchesStarted++;
   247     do_check_eq(numSearchesStarted, 1);
   248   };
   250   input.onSearchComplete = function() {
   252     do_check_eq(numSearchesStarted, 1);
   254     do_check_eq(controller.searchStatus, 
   255                 Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH);
   256     do_check_eq(controller.matchCount, 0);
   258     // Unregister searches
   259     unregisterAutoCompleteSearch(emptySearch);
   261     do_test_finished();
   262   };
   264   controller.input = input;
   266   // Search is asynchronous, so don't let the test finish immediately
   267   do_test_pending();
   269   controller.startSearch("test");
   270 }

mercurial