toolkit/components/autocomplete/tests/unit/test_378079.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.

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

mercurial