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

mercurial