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: /** michael@0: * A results that wants to defaultComplete to 0, but it has no matches, michael@0: * though it notifies SUCCESS to the controller. michael@0: */ michael@0: function AutoCompleteNoMatchResult() { michael@0: this.defaultIndex = 0; michael@0: } michael@0: AutoCompleteNoMatchResult.prototype = Object.create(AutoCompleteResultBase.prototype); michael@0: michael@0: /** michael@0: * A results that wants to defaultComplete to an index greater than the number michael@0: * of matches. michael@0: */ michael@0: function AutoCompleteBadIndexResult(aValues, aDefaultIndex) { michael@0: do_check_true(aValues.length <= aDefaultIndex); michael@0: this._values = aValues; michael@0: this.defaultIndex = aDefaultIndex; michael@0: } michael@0: AutoCompleteBadIndexResult.prototype = Object.create(AutoCompleteResultBase.prototype); michael@0: michael@0: add_test(function autocomplete_noMatch_success() { michael@0: const INPUT_STR = "moz"; michael@0: michael@0: let searchNoMatch = michael@0: new AutoCompleteSearchBase("searchNoMatch", michael@0: new AutoCompleteNoMatchResult()); michael@0: registerAutoCompleteSearch(searchNoMatch); michael@0: michael@0: // Make an AutoCompleteInput that uses our search and confirms results. michael@0: let input = new AutoCompleteInputBase([searchNoMatch.name]); michael@0: input.completeDefaultIndex = true; michael@0: input.textValue = INPUT_STR; michael@0: michael@0: // Caret must be at the end for autoFill to happen. michael@0: let strLen = INPUT_STR.length; michael@0: input.selectTextRange(strLen, strLen); michael@0: do_check_eq(input.selectionStart, strLen); michael@0: do_check_eq(input.selectionEnd, strLen); michael@0: michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: controller.input = input; michael@0: controller.startSearch(INPUT_STR); michael@0: michael@0: input.onSearchComplete = function () { michael@0: // Should not try to autoFill to an empty value. michael@0: do_check_eq(input.textValue, "moz"); michael@0: michael@0: // Clean up. michael@0: unregisterAutoCompleteSearch(searchNoMatch); michael@0: run_next_test(); michael@0: }; michael@0: }); michael@0: michael@0: add_test(function autocomplete_defaultIndex_exceeds_matchCount() { michael@0: const INPUT_STR = "moz"; michael@0: michael@0: // Result returning matches, but a bad defaultIndex. michael@0: let searchBadIndex = michael@0: new AutoCompleteSearchBase("searchBadIndex", michael@0: new AutoCompleteBadIndexResult(["mozillaTest"], 1)); michael@0: registerAutoCompleteSearch(searchBadIndex); michael@0: michael@0: // Make an AutoCompleteInput that uses our search and confirms results. michael@0: let input = new AutoCompleteInputBase([searchBadIndex.name]); michael@0: input.completeDefaultIndex = true; michael@0: input.textValue = INPUT_STR; michael@0: michael@0: // Caret must be at the end for autoFill to happen. michael@0: let strLen = INPUT_STR.length; michael@0: input.selectTextRange(strLen, strLen); michael@0: do_check_eq(input.selectionStart, strLen); michael@0: do_check_eq(input.selectionEnd, strLen); michael@0: michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: controller.input = input; michael@0: controller.startSearch(INPUT_STR); michael@0: michael@0: input.onSearchComplete = function () { michael@0: // Should not try to autoFill to an empty value. michael@0: do_check_eq(input.textValue, "moz"); michael@0: michael@0: // Clean up. michael@0: unregisterAutoCompleteSearch(searchBadIndex); michael@0: run_next_test(); michael@0: }; michael@0: }); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: }