toolkit/components/autocomplete/tests/unit/test_378079.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  * Unit test for Bug 378079 - AutoComplete returns invalid rows when
     9  * more than one AutoCompleteSearch is used.
    10  */
    14 /**
    15  * Dummy nsIAutoCompleteInput source that returns
    16  * the given list of AutoCompleteSearch names. 
    17  * 
    18  * Implements only the methods needed for this test.
    19  */
    20 function AutoCompleteInput(aSearches) {
    21   this.searches = aSearches;
    22 }
    23 AutoCompleteInput.prototype = {
    24   constructor: AutoCompleteInput, 
    26   // Array of AutoCompleteSearch names
    27   searches: null,
    29   minResultsForPopup: 0,
    30   timeout: 10,
    31   searchParam: "",
    32   textValue: "",
    33   disableAutoComplete: false,  
    34   completeDefaultIndex: false,
    36   get searchCount() {
    37     return this.searches.length;
    38   },
    40   getSearchAt: function(aIndex) {
    41     return this.searches[aIndex];
    42   },
    44   onSearchBegin: function() {},
    45   onSearchComplete: function() {},
    47   popupOpen: false,  
    49   popup: { 
    50     setSelectedIndex: function(aIndex) {},
    51     invalidate: function() {},
    53     // nsISupports implementation
    54     QueryInterface: function(iid) {
    55       if (iid.equals(Ci.nsISupports) ||
    56           iid.equals(Ci.nsIAutoCompletePopup))
    57         return this;
    59       throw Components.results.NS_ERROR_NO_INTERFACE;
    60     }    
    61   },
    63   // nsISupports implementation
    64   QueryInterface: function(iid) {
    65     if (iid.equals(Ci.nsISupports) ||
    66         iid.equals(Ci.nsIAutoCompleteInput))
    67       return this;
    69     throw Components.results.NS_ERROR_NO_INTERFACE;
    70   }
    71 }
    75 /** 
    76  * nsIAutoCompleteResult implementation
    77  */
    78 function AutoCompleteResult(aValues, aComments, aStyles) {
    79   this._values = aValues;
    80   this._comments = aComments;
    81   this._styles = aStyles;
    83   if (this._values.length > 0) {
    84     this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
    85   } else {
    86     this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH;
    87   }
    88 }
    89 AutoCompleteResult.prototype = {
    90   constructor: AutoCompleteResult,
    92   // Arrays
    93   _values: null,
    94   _comments: null,
    95   _styles: null,
    97   searchString: "",
    98   searchResult: null,
   100   defaultIndex: 0,
   102   get matchCount() {
   103     return this._values.length;
   104   },
   106   getValueAt: function(aIndex) {
   107     return this._values[aIndex];
   108   },
   110   getLabelAt: function(aIndex) {
   111     return this.getValueAt(aIndex);
   112   },
   114   getCommentAt: function(aIndex) {
   115     return this._comments[aIndex];
   116   },
   118   getStyleAt: function(aIndex) {
   119     return this._styles[aIndex];
   120   },
   122   getImageAt: function(aIndex) {
   123     return "";
   124   },
   126   getFinalCompleteValueAt: function(aIndex) {
   127     return this.getValueAt(aIndex);
   128   },
   130   removeValueAt: function (aRowIndex, aRemoveFromDb) {},
   132   // nsISupports implementation
   133   QueryInterface: function(iid) {
   134     if (iid.equals(Ci.nsISupports) ||
   135         iid.equals(Ci.nsIAutoCompleteResult))
   136       return this;
   138     throw Components.results.NS_ERROR_NO_INTERFACE;
   139   }  
   140 }
   144 /** 
   145  * nsIAutoCompleteSearch implementation that always returns
   146  * the same result set.
   147  */
   148 function AutoCompleteSearch(aName, aResult) {
   149   this.name = aName;
   150   this._result = aResult;
   151 }
   152 AutoCompleteSearch.prototype = {
   153   constructor: AutoCompleteSearch,
   155   // Search name. Used by AutoCompleteController
   156   name: null,
   158   // AutoCompleteResult
   159   _result:null,  
   162   /**
   163    * Return the same result set for every search
   164    */
   165   startSearch: function(aSearchString, 
   166                         aSearchParam, 
   167                         aPreviousResult, 
   168                         aListener) 
   169   {
   170     aListener.onSearchResult(this, 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() {
   232   // Make an AutoCompleteSearch that always returns nothing
   233   var emptySearch = new AutoCompleteSearch("test-empty-search", 
   234                              new AutoCompleteResult([], [], []));
   236   // Make an AutoCompleteSearch that returns two values
   237   var expectedValues = ["test1", "test2"];
   238   var regularSearch = new AutoCompleteSearch("test-regular-search",
   239                              new AutoCompleteResult(expectedValues, [], []));
   241   // Register searches so AutoCompleteController can find them
   242   registerAutoCompleteSearch(emptySearch);
   243   registerAutoCompleteSearch(regularSearch);
   245   var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
   246                    getService(Components.interfaces.nsIAutoCompleteController);  
   248   // Make an AutoCompleteInput that uses our searches
   249   // and confirms results on search complete
   250   var input = new AutoCompleteInput([emptySearch.name, regularSearch.name]);
   251   var numSearchesStarted = 0;
   253   input.onSearchBegin = function() {
   254     numSearchesStarted++;
   255     do_check_eq(numSearchesStarted, 1);
   256   };
   258   input.onSearchComplete = function() {
   260     do_check_eq(numSearchesStarted, 1);
   262     do_check_eq(controller.searchStatus, 
   263                 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
   264     do_check_eq(controller.matchCount, 2);
   266     // Confirm expected result values
   267     for (var i = 0; i < expectedValues.length; i++) {
   268       do_check_eq(expectedValues[i], controller.getValueAt(i)); 
   269     }
   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