1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_finalDefaultCompleteValue.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,66 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function AutoCompleteResult(aValues, aFinalCompleteValues) { 1.9 + this._values = aValues; 1.10 + this._finalCompleteValues = aFinalCompleteValues; 1.11 + this.defaultIndex = 0; 1.12 +} 1.13 +AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); 1.14 + 1.15 +function AutoCompleteInput(aSearches) { 1.16 + this.searches = aSearches; 1.17 + this.popup.selectedIndex = -1; 1.18 + this.completeDefaultIndex = true; 1.19 +} 1.20 +AutoCompleteInput.prototype = Object.create(AutoCompleteInputBase.prototype); 1.21 + 1.22 +function run_test() { 1.23 + run_next_test(); 1.24 +} 1.25 + 1.26 +add_test(function test_keyNavigation() { 1.27 + doSearch("moz", "mozilla.com", "http://www.mozilla.com", function(aController) { 1.28 + do_check_eq(aController.input.textValue, "mozilla.com"); 1.29 + aController.handleKeyNavigation(Ci.nsIDOMKeyEvent.DOM_VK_RIGHT); 1.30 + do_check_eq(aController.input.textValue, "mozilla.com"); 1.31 + }); 1.32 +}); 1.33 + 1.34 +add_test(function test_handleEnter() { 1.35 + doSearch("moz", "mozilla.com", "http://www.mozilla.com", function(aController) { 1.36 + do_check_eq(aController.input.textValue, "mozilla.com"); 1.37 + aController.handleEnter(false); 1.38 + do_check_eq(aController.input.textValue, "http://www.mozilla.com"); 1.39 + }); 1.40 +}); 1.41 + 1.42 +function doSearch(aSearchString, aResultValue, aFinalCompleteValue, aOnCompleteCallback) { 1.43 + let search = new AutoCompleteSearchBase( 1.44 + "search", 1.45 + new AutoCompleteResult([ aResultValue ], [ aFinalCompleteValue ]) 1.46 + ); 1.47 + registerAutoCompleteSearch(search); 1.48 + 1.49 + let controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.50 + getService(Ci.nsIAutoCompleteController); 1.51 + 1.52 + // Make an AutoCompleteInput that uses our searches and confirms results. 1.53 + let input = new AutoCompleteInput([ search.name ]); 1.54 + input.textValue = aSearchString; 1.55 + 1.56 + // Caret must be at the end for autofill to happen. 1.57 + let strLen = aSearchString.length; 1.58 + input.selectTextRange(strLen, strLen); 1.59 + controller.input = input; 1.60 + controller.startSearch(aSearchString); 1.61 + 1.62 + input.onSearchComplete = function onSearchComplete() { 1.63 + aOnCompleteCallback(controller); 1.64 + 1.65 + // Clean up. 1.66 + unregisterAutoCompleteSearch(search); 1.67 + run_next_test(); 1.68 + }; 1.69 +}