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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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  * Dummy nsIAutoCompleteInput source that returns
     9  * the given list of AutoCompleteSearch names. 
    10  * 
    11  * Implements only the methods needed for this test.
    12  */
    13 function AutoCompleteInput(aSearches) {
    14   this.searches = aSearches;
    15 }
    16 AutoCompleteInput.prototype = {
    17   constructor: AutoCompleteInput, 
    19   // Array of AutoCompleteSearch names
    20   searches: null,
    22   minResultsForPopup: 0,
    23   timeout: 10,
    24   searchParam: "",
    25   textValue: "",
    26   disableAutoComplete: false,  
    27   completeDefaultIndex: false,
    29   get searchCount() {
    30     return this.searches.length;
    31   },
    33   getSearchAt: function(aIndex) {
    34     return this.searches[aIndex];
    35   },
    37   onSearchBegin: function() {},
    38   onSearchComplete: function() {},
    40   popupOpen: false,  
    42   popup: { 
    43     setSelectedIndex: function(aIndex) {},
    44     invalidate: function() {},
    46     // nsISupports implementation
    47     QueryInterface: function(iid) {
    48       if (iid.equals(Ci.nsISupports) ||
    49           iid.equals(Ci.nsIAutoCompletePopup))
    50         return this;
    52       throw Components.results.NS_ERROR_NO_INTERFACE;
    53     }    
    54   },
    56   // nsISupports implementation
    57   QueryInterface: function(iid) {
    58     if (iid.equals(Ci.nsISupports) ||
    59         iid.equals(Ci.nsIAutoCompleteInput))
    60       return this;
    62     throw Components.results.NS_ERROR_NO_INTERFACE;
    63   }
    64 }
    68 /** 
    69  * nsIAutoCompleteResult implementation
    70  */
    71 function AutoCompleteResult(aValues, aComments, aStyles) {
    72   this._values = aValues;
    73   this._comments = aComments;
    74   this._styles = aStyles;
    75 }
    76 AutoCompleteResult.prototype = {
    77   constructor: AutoCompleteResult,
    79   // Arrays
    80   _values: null,
    81   _comments: null,
    82   _styles: null,
    84   searchString: "",
    85   searchResult: null,
    87   defaultIndex: 0,
    89   get matchCount() {
    90     return this._values.length;
    91   },
    93   getValueAt: function(aIndex) {
    94     return this._values[aIndex];
    95   },
    97   getLabelAt: function(aIndex) {
    98     return this.getValueAt(aIndex);
    99   },
   101   getCommentAt: function(aIndex) {
   102     return this._comments[aIndex];
   103   },
   105   getStyleAt: function(aIndex) {
   106     return this._styles[aIndex];
   107   },
   109   getImageAt: function(aIndex) {
   110     return "";
   111   },
   113   getFinalCompleteValueAt: function(aIndex) {
   114     return this.getValueAt(aIndex);
   115   },
   117   removeValueAt: function (aRowIndex, aRemoveFromDb) {},
   119   // nsISupports implementation
   120   QueryInterface: function(iid) {
   121     if (iid.equals(Ci.nsISupports) ||
   122         iid.equals(Ci.nsIAutoCompleteResult))
   123       return this;
   125     throw Components.results.NS_ERROR_NO_INTERFACE;
   126   }  
   127 }
   131 /** 
   132  * nsIAutoCompleteSearch implementation that always returns
   133  * the same result set.
   134  */
   135 function AutoCompleteSearch(aName, aResult) {
   136   this.name = aName;
   137   this._result = aResult;
   138 }
   139 AutoCompleteSearch.prototype = {
   140   constructor: AutoCompleteSearch,
   142   // Search name. Used by AutoCompleteController
   143   name: null,
   145   // AutoCompleteResult
   146   _result:null,  
   149   /**
   150    * Return the same result set for every search
   151    */
   152   startSearch: function(aSearchString, 
   153                         aSearchParam, 
   154                         aPreviousResult, 
   155                         aListener) 
   156   {
   157     var result = this._result;
   158     if (result._values.length > 0) {
   159       result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING;
   160     } else {
   161       result.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH_ONGOING;
   162     }
   163     aListener.onSearchResult(this, result);
   165     if (result._values.length > 0) {
   166       result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
   167     } else {
   168       result.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH;
   169     }
   170     aListener.onSearchResult(this, result);
   171   },
   173   stopSearch: function() {},
   175   // nsISupports implementation
   176   QueryInterface: function(iid) {
   177     if (iid.equals(Ci.nsISupports) ||
   178         iid.equals(Ci.nsIFactory) ||
   179         iid.equals(Ci.nsIAutoCompleteSearch))
   180       return this;
   182     throw Components.results.NS_ERROR_NO_INTERFACE;
   183   },
   185   // nsIFactory implementation
   186   createInstance: function(outer, iid) {
   187     return this.QueryInterface(iid);
   188   }
   189 }
   193 /** 
   194  * Helper to register an AutoCompleteSearch with the given name.
   195  * Allows the AutoCompleteController to find the search.
   196  */
   197 function registerAutoCompleteSearch(aSearch) {
   198   var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
   200   var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].
   201                       getService(Ci.nsIUUIDGenerator);
   202   var cid = uuidGenerator.generateUUID();
   204   var desc = "Test AutoCompleteSearch";
   206   var componentManager = Components.manager
   207                                    .QueryInterface(Ci.nsIComponentRegistrar);
   208   componentManager.registerFactory(cid, desc, name, aSearch);
   210   // Keep the id on the object so we can unregister later
   211   aSearch.cid = cid; 
   212 }
   216 /** 
   217  * Helper to unregister an AutoCompleteSearch. 
   218  */
   219 function unregisterAutoCompleteSearch(aSearch) {
   220   var componentManager = Components.manager
   221                                    .QueryInterface(Ci.nsIComponentRegistrar);  
   222   componentManager.unregisterFactory(aSearch.cid, aSearch);
   223 }
   227 /** 
   228  * Test AutoComplete with multiple AutoCompleteSearch sources.
   229  */
   230 function run_test() {
   231   var expected1 = ["1","2","3"];
   232   var expected2 = ["a","b","c"];
   233   var search1 = new AutoCompleteSearch("search1", 
   234                              new AutoCompleteResult(expected1, [], []));
   235   var search2 = new AutoCompleteSearch("search2",
   236                              new AutoCompleteResult(expected2, [], []));
   238   // Register searches so AutoCompleteController can find them
   239   registerAutoCompleteSearch(search1);
   240   registerAutoCompleteSearch(search2);
   242   var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
   243                    getService(Components.interfaces.nsIAutoCompleteController);  
   245   // Make an AutoCompleteInput that uses our searches
   246   // and confirms results on search complete
   247   var input = new AutoCompleteInput([search1.name, search2.name]);
   248   var numSearchesStarted = 0;
   250   input.onSearchBegin = function() {
   251     numSearchesStarted++;
   252     do_check_eq(numSearchesStarted, 1);
   253   };
   255   input.onSearchComplete = function() {
   257     do_check_eq(numSearchesStarted, 1);
   259     do_check_eq(controller.searchStatus, 
   260                 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
   261     do_check_eq(controller.matchCount, expected1.length + expected2.length);
   263     // Unregister searches
   264     unregisterAutoCompleteSearch(search1);
   265     unregisterAutoCompleteSearch(search2);
   267     do_test_finished();
   268   };
   270   controller.input = input;
   272   // Search is asynchronous, so don't let the test finish immediately
   273   do_test_pending();
   275   controller.startSearch("test");
   276 }

mercurial