1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_badDefaultIndex.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 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 +/** 1.9 + * A results that wants to defaultComplete to 0, but it has no matches, 1.10 + * though it notifies SUCCESS to the controller. 1.11 + */ 1.12 +function AutoCompleteNoMatchResult() { 1.13 + this.defaultIndex = 0; 1.14 +} 1.15 +AutoCompleteNoMatchResult.prototype = Object.create(AutoCompleteResultBase.prototype); 1.16 + 1.17 +/** 1.18 + * A results that wants to defaultComplete to an index greater than the number 1.19 + * of matches. 1.20 + */ 1.21 +function AutoCompleteBadIndexResult(aValues, aDefaultIndex) { 1.22 + do_check_true(aValues.length <= aDefaultIndex); 1.23 + this._values = aValues; 1.24 + this.defaultIndex = aDefaultIndex; 1.25 +} 1.26 +AutoCompleteBadIndexResult.prototype = Object.create(AutoCompleteResultBase.prototype); 1.27 + 1.28 +add_test(function autocomplete_noMatch_success() { 1.29 + const INPUT_STR = "moz"; 1.30 + 1.31 + let searchNoMatch = 1.32 + new AutoCompleteSearchBase("searchNoMatch", 1.33 + new AutoCompleteNoMatchResult()); 1.34 + registerAutoCompleteSearch(searchNoMatch); 1.35 + 1.36 + // Make an AutoCompleteInput that uses our search and confirms results. 1.37 + let input = new AutoCompleteInputBase([searchNoMatch.name]); 1.38 + input.completeDefaultIndex = true; 1.39 + input.textValue = INPUT_STR; 1.40 + 1.41 + // Caret must be at the end for autoFill to happen. 1.42 + let strLen = INPUT_STR.length; 1.43 + input.selectTextRange(strLen, strLen); 1.44 + do_check_eq(input.selectionStart, strLen); 1.45 + do_check_eq(input.selectionEnd, strLen); 1.46 + 1.47 + let controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.48 + getService(Ci.nsIAutoCompleteController); 1.49 + controller.input = input; 1.50 + controller.startSearch(INPUT_STR); 1.51 + 1.52 + input.onSearchComplete = function () { 1.53 + // Should not try to autoFill to an empty value. 1.54 + do_check_eq(input.textValue, "moz"); 1.55 + 1.56 + // Clean up. 1.57 + unregisterAutoCompleteSearch(searchNoMatch); 1.58 + run_next_test(); 1.59 + }; 1.60 +}); 1.61 + 1.62 +add_test(function autocomplete_defaultIndex_exceeds_matchCount() { 1.63 + const INPUT_STR = "moz"; 1.64 + 1.65 + // Result returning matches, but a bad defaultIndex. 1.66 + let searchBadIndex = 1.67 + new AutoCompleteSearchBase("searchBadIndex", 1.68 + new AutoCompleteBadIndexResult(["mozillaTest"], 1)); 1.69 + registerAutoCompleteSearch(searchBadIndex); 1.70 + 1.71 + // Make an AutoCompleteInput that uses our search and confirms results. 1.72 + let input = new AutoCompleteInputBase([searchBadIndex.name]); 1.73 + input.completeDefaultIndex = true; 1.74 + input.textValue = INPUT_STR; 1.75 + 1.76 + // Caret must be at the end for autoFill to happen. 1.77 + let strLen = INPUT_STR.length; 1.78 + input.selectTextRange(strLen, strLen); 1.79 + do_check_eq(input.selectionStart, strLen); 1.80 + do_check_eq(input.selectionEnd, strLen); 1.81 + 1.82 + let controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.83 + getService(Ci.nsIAutoCompleteController); 1.84 + controller.input = input; 1.85 + controller.startSearch(INPUT_STR); 1.86 + 1.87 + input.onSearchComplete = function () { 1.88 + // Should not try to autoFill to an empty value. 1.89 + do_check_eq(input.textValue, "moz"); 1.90 + 1.91 + // Clean up. 1.92 + unregisterAutoCompleteSearch(searchBadIndex); 1.93 + run_next_test(); 1.94 + }; 1.95 +}); 1.96 + 1.97 +function run_test() { 1.98 + run_next_test(); 1.99 +}