toolkit/components/autocomplete/tests/unit/test_previousResult.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 438861 - Previous search results not returned to multiple
michael@0 9 * searches.
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 if (this._values.length > 0) {
michael@0 82 this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
michael@0 83 } else {
michael@0 84 this.searchResult = Ci.nsIAutoCompleteResult.NOMATCH;
michael@0 85 }
michael@0 86 }
michael@0 87 AutoCompleteResult.prototype = {
michael@0 88 constructor: AutoCompleteResult,
michael@0 89
michael@0 90 // Arrays
michael@0 91 _values: null,
michael@0 92 _comments: null,
michael@0 93 _styles: null,
michael@0 94
michael@0 95 searchString: "",
michael@0 96 searchResult: null,
michael@0 97
michael@0 98 defaultIndex: 0,
michael@0 99
michael@0 100 get matchCount() {
michael@0 101 return this._values.length;
michael@0 102 },
michael@0 103
michael@0 104 getValueAt: function(aIndex) {
michael@0 105 return this._values[aIndex];
michael@0 106 },
michael@0 107
michael@0 108 getLabelAt: function(aIndex) {
michael@0 109 return this.getValueAt(aIndex);
michael@0 110 },
michael@0 111
michael@0 112 getCommentAt: function(aIndex) {
michael@0 113 return this._comments[aIndex];
michael@0 114 },
michael@0 115
michael@0 116 getStyleAt: function(aIndex) {
michael@0 117 return this._styles[aIndex];
michael@0 118 },
michael@0 119
michael@0 120 getImageAt: function(aIndex) {
michael@0 121 return "";
michael@0 122 },
michael@0 123
michael@0 124 getFinalCompleteValueAt: function(aIndex) {
michael@0 125 return this.getValueAt(aIndex);
michael@0 126 },
michael@0 127
michael@0 128 removeValueAt: function (aRowIndex, aRemoveFromDb) {},
michael@0 129
michael@0 130 // nsISupports implementation
michael@0 131 QueryInterface: function(iid) {
michael@0 132 if (iid.equals(Ci.nsISupports) ||
michael@0 133 iid.equals(Ci.nsIAutoCompleteResult))
michael@0 134 return this;
michael@0 135
michael@0 136 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 137 }
michael@0 138 }
michael@0 139
michael@0 140
michael@0 141 /**
michael@0 142 * nsIAutoCompleteSearch implementation that always returns
michael@0 143 * the same result set.
michael@0 144 */
michael@0 145 function AutoCompleteSearch(aName, aResult) {
michael@0 146 this.name = aName;
michael@0 147 this._result = aResult;
michael@0 148 }
michael@0 149 AutoCompleteSearch.prototype = {
michael@0 150 constructor: AutoCompleteSearch,
michael@0 151
michael@0 152 // Search name. Used by AutoCompleteController
michael@0 153 name: null,
michael@0 154
michael@0 155 // AutoCompleteResult
michael@0 156 _result: null,
michael@0 157
michael@0 158 _previousResult: null,
michael@0 159
michael@0 160
michael@0 161 /**
michael@0 162 * Return the same result set for every search
michael@0 163 */
michael@0 164 startSearch: function(aSearchString,
michael@0 165 aSearchParam,
michael@0 166 aPreviousResult,
michael@0 167 aListener)
michael@0 168 {
michael@0 169 this._previousResult = aPreviousResult;
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 * Helper to register an AutoCompleteSearch with the given name.
michael@0 194 * Allows the AutoCompleteController to find the search.
michael@0 195 */
michael@0 196 function registerAutoCompleteSearch(aSearch) {
michael@0 197 var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
michael@0 198
michael@0 199 var uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].
michael@0 200 getService(Ci.nsIUUIDGenerator);
michael@0 201 var cid = uuidGenerator.generateUUID();
michael@0 202
michael@0 203 var desc = "Test AutoCompleteSearch";
michael@0 204
michael@0 205 var componentManager = Components.manager
michael@0 206 .QueryInterface(Ci.nsIComponentRegistrar);
michael@0 207 componentManager.registerFactory(cid, desc, name, aSearch);
michael@0 208
michael@0 209 // Keep the id on the object so we can unregister later
michael@0 210 aSearch.cid = cid;
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 function run_test() {
michael@0 227 // Make an AutoCompleteSearch that always returns nothing
michael@0 228 var search1 = new AutoCompleteSearch("test-previous-result1",
michael@0 229 new AutoCompleteResult(["hello1"], [""], [""]));
michael@0 230
michael@0 231 var search2 = new AutoCompleteSearch("test-previous-result2",
michael@0 232 new AutoCompleteResult(["hello2"], [""], [""]));
michael@0 233
michael@0 234 // Register search so AutoCompleteController can find them
michael@0 235 registerAutoCompleteSearch(search1);
michael@0 236 registerAutoCompleteSearch(search2);
michael@0 237
michael@0 238 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
michael@0 239 getService(Components.interfaces.nsIAutoCompleteController);
michael@0 240
michael@0 241 // Make an AutoCompleteInput that uses our search
michael@0 242 // and confirms results on search complete
michael@0 243 var input = new AutoCompleteInput([search1.name,
michael@0 244 search2.name]);
michael@0 245 var numSearchesStarted = 0;
michael@0 246 var previousResult = null;
michael@0 247
michael@0 248 input.onSearchBegin = function() {
michael@0 249 numSearchesStarted++;
michael@0 250 };
michael@0 251
michael@0 252 input.onSearchComplete = function() {
michael@0 253 do_check_eq(controller.searchStatus,
michael@0 254 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
michael@0 255 do_check_eq(controller.matchCount, 2);
michael@0 256
michael@0 257 if (numSearchesStarted == 1) {
michael@0 258 do_check_eq(search1._previousResult, null);
michael@0 259 do_check_eq(search2._previousResult, null);
michael@0 260
michael@0 261 // Now start it again
michael@0 262 controller.startSearch("test");
michael@0 263 return;
michael@0 264 }
michael@0 265 else {
michael@0 266 do_check_neq(search1._previousResult, null);
michael@0 267 do_check_neq(search2._previousResult, null);
michael@0 268 }
michael@0 269
michael@0 270 // Unregister searches
michael@0 271 unregisterAutoCompleteSearch(search1);
michael@0 272 unregisterAutoCompleteSearch(search2);
michael@0 273
michael@0 274 do_test_finished();
michael@0 275 };
michael@0 276
michael@0 277 controller.input = input;
michael@0 278
michael@0 279 // Search is asynchronous, so don't let the test finish immediately
michael@0 280 do_test_pending();
michael@0 281
michael@0 282 controller.startSearch("test");
michael@0 283 }
michael@0 284

mercurial