michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function AutoCompleteResult(aValues, aFinalCompleteValues) { michael@0: this._values = aValues; michael@0: this._finalCompleteValues = aFinalCompleteValues; michael@0: this.defaultIndex = 0; michael@0: } michael@0: AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); michael@0: michael@0: function AutoCompleteInput(aSearches) { michael@0: this.searches = aSearches; michael@0: this.popup.selectedIndex = -1; michael@0: this.completeDefaultIndex = true; michael@0: } michael@0: AutoCompleteInput.prototype = Object.create(AutoCompleteInputBase.prototype); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_keyNavigation() { michael@0: doSearch("moz", "mozilla.com", "http://www.mozilla.com", function(aController) { michael@0: do_check_eq(aController.input.textValue, "mozilla.com"); michael@0: aController.handleKeyNavigation(Ci.nsIDOMKeyEvent.DOM_VK_RIGHT); michael@0: do_check_eq(aController.input.textValue, "mozilla.com"); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_handleEnter() { michael@0: doSearch("moz", "mozilla.com", "http://www.mozilla.com", function(aController) { michael@0: do_check_eq(aController.input.textValue, "mozilla.com"); michael@0: aController.handleEnter(false); michael@0: do_check_eq(aController.input.textValue, "http://www.mozilla.com"); michael@0: }); michael@0: }); michael@0: michael@0: function doSearch(aSearchString, aResultValue, aFinalCompleteValue, aOnCompleteCallback) { michael@0: let search = new AutoCompleteSearchBase( michael@0: "search", michael@0: new AutoCompleteResult([ aResultValue ], [ aFinalCompleteValue ]) michael@0: ); michael@0: registerAutoCompleteSearch(search); michael@0: michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: michael@0: // Make an AutoCompleteInput that uses our searches and confirms results. michael@0: let input = new AutoCompleteInput([ search.name ]); michael@0: input.textValue = aSearchString; michael@0: michael@0: // Caret must be at the end for autofill to happen. michael@0: let strLen = aSearchString.length; michael@0: input.selectTextRange(strLen, strLen); michael@0: controller.input = input; michael@0: controller.startSearch(aSearchString); michael@0: michael@0: input.onSearchComplete = function onSearchComplete() { michael@0: aOnCompleteCallback(controller); michael@0: michael@0: // Clean up. michael@0: unregisterAutoCompleteSearch(search); michael@0: run_next_test(); michael@0: }; michael@0: }