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.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * Dummy nsIAutoCompleteInput source that returns
michael@0 9 * the given list of AutoCompleteSearch names.
michael@0 10 *
michael@0 11 * Implements only the methods needed for this test.
michael@0 12 */
michael@0 13 function AutoCompleteInput(aSearches) {
michael@0 14 this.searches = aSearches;
michael@0 15 }
michael@0 16 AutoCompleteInput.prototype = {
michael@0 17 constructor: AutoCompleteInput,
michael@0 18
michael@0 19 // Array of AutoCompleteSearch names
michael@0 20 searches: null,
michael@0 21
michael@0 22 minResultsForPopup: 0,
michael@0 23 timeout: 10,
michael@0 24 searchParam: "",
michael@0 25 textValue: "",
michael@0 26 disableAutoComplete: false,
michael@0 27 completeDefaultIndex: false,
michael@0 28
michael@0 29 get searchCount() {
michael@0 30 return this.searches.length;
michael@0 31 },
michael@0 32
michael@0 33 getSearchAt: function(aIndex) {
michael@0 34 return this.searches[aIndex];
michael@0 35 },
michael@0 36
michael@0 37 onSearchBegin: function() {},
michael@0 38 onSearchComplete: function() {},
michael@0 39
michael@0 40 popupOpen: false,
michael@0 41
michael@0 42 popup: {
michael@0 43 setSelectedIndex: function(aIndex) {},
michael@0 44 invalidate: function() {},
michael@0 45
michael@0 46 // nsISupports implementation
michael@0 47 QueryInterface: function(iid) {
michael@0 48 if (iid.equals(Ci.nsISupports) ||
michael@0 49 iid.equals(Ci.nsIAutoCompletePopup))
michael@0 50 return this;
michael@0 51
michael@0 52 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 53 }
michael@0 54 },
michael@0 55
michael@0 56 // nsISupports implementation
michael@0 57 QueryInterface: function(iid) {
michael@0 58 if (iid.equals(Ci.nsISupports) ||
michael@0 59 iid.equals(Ci.nsIAutoCompleteInput))
michael@0 60 return this;
michael@0 61
michael@0 62 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66
michael@0 67
michael@0 68 /**
michael@0 69 * nsIAutoCompleteResult implementation
michael@0 70 */
michael@0 71 function AutoCompleteResult(aValues, aComments, aStyles) {
michael@0 72 this._values = aValues;
michael@0 73 this._comments = aComments;
michael@0 74 this._styles = aStyles;
michael@0 75 }
michael@0 76 AutoCompleteResult.prototype = {
michael@0 77 constructor: AutoCompleteResult,
michael@0 78
michael@0 79 // Arrays
michael@0 80 _values: null,
michael@0 81 _comments: null,
michael@0 82 _styles: null,
michael@0 83
michael@0 84 searchString: "",
michael@0 85 searchResult: null,
michael@0 86
michael@0 87 defaultIndex: 0,
michael@0 88
michael@0 89 get matchCount() {
michael@0 90 return this._values.length;
michael@0 91 },
michael@0 92
michael@0 93 getValueAt: function(aIndex) {
michael@0 94 return this._values[aIndex];
michael@0 95 },
michael@0 96
michael@0 97 getLabelAt: function(aIndex) {
michael@0 98 return this.getValueAt(aIndex);
michael@0 99 },
michael@0 100
michael@0 101 getCommentAt: function(aIndex) {
michael@0 102 return this._comments[aIndex];
michael@0 103 },
michael@0 104
michael@0 105 getStyleAt: function(aIndex) {
michael@0 106 return this._styles[aIndex];
michael@0 107 },
michael@0 108
michael@0 109 getImageAt: function(aIndex) {
michael@0 110 return "";
michael@0 111 },
michael@0 112
michael@0 113 getFinalCompleteValueAt: function(aIndex) {
michael@0 114 return this.getValueAt(aIndex);
michael@0 115 },
michael@0 116
michael@0 117 removeValueAt: function (aRowIndex, aRemoveFromDb) {},
michael@0 118
michael@0 119 // nsISupports implementation
michael@0 120 QueryInterface: function(iid) {
michael@0 121 if (iid.equals(Ci.nsISupports) ||
michael@0 122 iid.equals(Ci.nsIAutoCompleteResult))
michael@0 123 return this;
michael@0 124
michael@0 125 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129
michael@0 130
michael@0 131 /**
michael@0 132 * nsIAutoCompleteSearch implementation that always returns
michael@0 133 * the same result set.
michael@0 134 */
michael@0 135 function AutoCompleteSearch(aName, aResult) {
michael@0 136 this.name = aName;
michael@0 137 this._result = aResult;
michael@0 138 }
michael@0 139 AutoCompleteSearch.prototype = {
michael@0 140 constructor: AutoCompleteSearch,
michael@0 141
michael@0 142 // Search name. Used by AutoCompleteController
michael@0 143 name: null,
michael@0 144
michael@0 145 // AutoCompleteResult
michael@0 146 _result:null,
michael@0 147
michael@0 148
michael@0 149 /**
michael@0 150 * Return the same result set for every search
michael@0 151 */
michael@0 152 startSearch: function(aSearchString,
michael@0 153 aSearchParam,
michael@0 154 aPreviousResult,
michael@0 155 aListener)
michael@0 156 {
michael@0 157 var result = this._result;
michael@0 158 if (result._values.length > 0) {
michael@0 159 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING;
michael@0 160 } else {
michael@0 161 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH_ONGOING;
michael@0 162 }
michael@0 163 aListener.onSearchResult(this, result);
michael@0 164
michael@0 165 if (result._values.length > 0) {
michael@0 166 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
michael@0 167 } else {
michael@0 168 result.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH;
michael@0 169 }
michael@0 170 aListener.onSearchResult(this, result);
michael@0 171 },
michael@0 172
michael@0 173 stopSearch: function() {},
michael@0 174
michael@0 175 // nsISupports implementation
michael@0 176 QueryInterface: function(iid) {
michael@0 177 if (iid.equals(Ci.nsISupports) ||
michael@0 178 iid.equals(Ci.nsIFactory) ||
michael@0 179 iid.equals(Ci.nsIAutoCompleteSearch))
michael@0 180 return this;
michael@0 181
michael@0 182 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 183 },
michael@0 184
michael@0 185 // nsIFactory implementation
michael@0 186 createInstance: function(outer, iid) {
michael@0 187 return this.QueryInterface(iid);
michael@0 188 }
michael@0 189 }
michael@0 190
michael@0 191
michael@0 192
michael@0 193 /**
michael@0 194 * Helper to register an AutoCompleteSearch with the given name.
michael@0 195 * Allows the AutoCompleteController to find the search.
michael@0 196 */
michael@0 197 function registerAutoCompleteSearch(aSearch) {
michael@0 198 var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
michael@0 199
michael@0 200 var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].
michael@0 201 getService(Ci.nsIUUIDGenerator);
michael@0 202 var cid = uuidGenerator.generateUUID();
michael@0 203
michael@0 204 var desc = "Test AutoCompleteSearch";
michael@0 205
michael@0 206 var componentManager = Components.manager
michael@0 207 .QueryInterface(Ci.nsIComponentRegistrar);
michael@0 208 componentManager.registerFactory(cid, desc, name, aSearch);
michael@0 209
michael@0 210 // Keep the id on the object so we can unregister later
michael@0 211 aSearch.cid = cid;
michael@0 212 }
michael@0 213
michael@0 214
michael@0 215
michael@0 216 /**
michael@0 217 * Helper to unregister an AutoCompleteSearch.
michael@0 218 */
michael@0 219 function unregisterAutoCompleteSearch(aSearch) {
michael@0 220 var componentManager = Components.manager
michael@0 221 .QueryInterface(Ci.nsIComponentRegistrar);
michael@0 222 componentManager.unregisterFactory(aSearch.cid, aSearch);
michael@0 223 }
michael@0 224
michael@0 225
michael@0 226
michael@0 227 /**
michael@0 228 * Test AutoComplete with multiple AutoCompleteSearch sources.
michael@0 229 */
michael@0 230 function run_test() {
michael@0 231 var expected1 = ["1","2","3"];
michael@0 232 var expected2 = ["a","b","c"];
michael@0 233 var search1 = new AutoCompleteSearch("search1",
michael@0 234 new AutoCompleteResult(expected1, [], []));
michael@0 235 var search2 = new AutoCompleteSearch("search2",
michael@0 236 new AutoCompleteResult(expected2, [], []));
michael@0 237
michael@0 238 // Register searches so AutoCompleteController can find them
michael@0 239 registerAutoCompleteSearch(search1);
michael@0 240 registerAutoCompleteSearch(search2);
michael@0 241
michael@0 242 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
michael@0 243 getService(Components.interfaces.nsIAutoCompleteController);
michael@0 244
michael@0 245 // Make an AutoCompleteInput that uses our searches
michael@0 246 // and confirms results on search complete
michael@0 247 var input = new AutoCompleteInput([search1.name, search2.name]);
michael@0 248 var numSearchesStarted = 0;
michael@0 249
michael@0 250 input.onSearchBegin = function() {
michael@0 251 numSearchesStarted++;
michael@0 252 do_check_eq(numSearchesStarted, 1);
michael@0 253 };
michael@0 254
michael@0 255 input.onSearchComplete = function() {
michael@0 256
michael@0 257 do_check_eq(numSearchesStarted, 1);
michael@0 258
michael@0 259 do_check_eq(controller.searchStatus,
michael@0 260 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
michael@0 261 do_check_eq(controller.matchCount, expected1.length + expected2.length);
michael@0 262
michael@0 263 // Unregister searches
michael@0 264 unregisterAutoCompleteSearch(search1);
michael@0 265 unregisterAutoCompleteSearch(search2);
michael@0 266
michael@0 267 do_test_finished();
michael@0 268 };
michael@0 269
michael@0 270 controller.input = input;
michael@0 271
michael@0 272 // Search is asynchronous, so don't let the test finish immediately
michael@0 273 do_test_pending();
michael@0 274
michael@0 275 controller.startSearch("test");
michael@0 276 }

mercurial