toolkit/components/autocomplete/tests/unit/test_badDefaultIndex.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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 * A results that wants to defaultComplete to 0, but it has no matches,
michael@0 7 * though it notifies SUCCESS to the controller.
michael@0 8 */
michael@0 9 function AutoCompleteNoMatchResult() {
michael@0 10 this.defaultIndex = 0;
michael@0 11 }
michael@0 12 AutoCompleteNoMatchResult.prototype = Object.create(AutoCompleteResultBase.prototype);
michael@0 13
michael@0 14 /**
michael@0 15 * A results that wants to defaultComplete to an index greater than the number
michael@0 16 * of matches.
michael@0 17 */
michael@0 18 function AutoCompleteBadIndexResult(aValues, aDefaultIndex) {
michael@0 19 do_check_true(aValues.length <= aDefaultIndex);
michael@0 20 this._values = aValues;
michael@0 21 this.defaultIndex = aDefaultIndex;
michael@0 22 }
michael@0 23 AutoCompleteBadIndexResult.prototype = Object.create(AutoCompleteResultBase.prototype);
michael@0 24
michael@0 25 add_test(function autocomplete_noMatch_success() {
michael@0 26 const INPUT_STR = "moz";
michael@0 27
michael@0 28 let searchNoMatch =
michael@0 29 new AutoCompleteSearchBase("searchNoMatch",
michael@0 30 new AutoCompleteNoMatchResult());
michael@0 31 registerAutoCompleteSearch(searchNoMatch);
michael@0 32
michael@0 33 // Make an AutoCompleteInput that uses our search and confirms results.
michael@0 34 let input = new AutoCompleteInputBase([searchNoMatch.name]);
michael@0 35 input.completeDefaultIndex = true;
michael@0 36 input.textValue = INPUT_STR;
michael@0 37
michael@0 38 // Caret must be at the end for autoFill to happen.
michael@0 39 let strLen = INPUT_STR.length;
michael@0 40 input.selectTextRange(strLen, strLen);
michael@0 41 do_check_eq(input.selectionStart, strLen);
michael@0 42 do_check_eq(input.selectionEnd, strLen);
michael@0 43
michael@0 44 let controller = Cc["@mozilla.org/autocomplete/controller;1"].
michael@0 45 getService(Ci.nsIAutoCompleteController);
michael@0 46 controller.input = input;
michael@0 47 controller.startSearch(INPUT_STR);
michael@0 48
michael@0 49 input.onSearchComplete = function () {
michael@0 50 // Should not try to autoFill to an empty value.
michael@0 51 do_check_eq(input.textValue, "moz");
michael@0 52
michael@0 53 // Clean up.
michael@0 54 unregisterAutoCompleteSearch(searchNoMatch);
michael@0 55 run_next_test();
michael@0 56 };
michael@0 57 });
michael@0 58
michael@0 59 add_test(function autocomplete_defaultIndex_exceeds_matchCount() {
michael@0 60 const INPUT_STR = "moz";
michael@0 61
michael@0 62 // Result returning matches, but a bad defaultIndex.
michael@0 63 let searchBadIndex =
michael@0 64 new AutoCompleteSearchBase("searchBadIndex",
michael@0 65 new AutoCompleteBadIndexResult(["mozillaTest"], 1));
michael@0 66 registerAutoCompleteSearch(searchBadIndex);
michael@0 67
michael@0 68 // Make an AutoCompleteInput that uses our search and confirms results.
michael@0 69 let input = new AutoCompleteInputBase([searchBadIndex.name]);
michael@0 70 input.completeDefaultIndex = true;
michael@0 71 input.textValue = INPUT_STR;
michael@0 72
michael@0 73 // Caret must be at the end for autoFill to happen.
michael@0 74 let strLen = INPUT_STR.length;
michael@0 75 input.selectTextRange(strLen, strLen);
michael@0 76 do_check_eq(input.selectionStart, strLen);
michael@0 77 do_check_eq(input.selectionEnd, strLen);
michael@0 78
michael@0 79 let controller = Cc["@mozilla.org/autocomplete/controller;1"].
michael@0 80 getService(Ci.nsIAutoCompleteController);
michael@0 81 controller.input = input;
michael@0 82 controller.startSearch(INPUT_STR);
michael@0 83
michael@0 84 input.onSearchComplete = function () {
michael@0 85 // Should not try to autoFill to an empty value.
michael@0 86 do_check_eq(input.textValue, "moz");
michael@0 87
michael@0 88 // Clean up.
michael@0 89 unregisterAutoCompleteSearch(searchBadIndex);
michael@0 90 run_next_test();
michael@0 91 };
michael@0 92 });
michael@0 93
michael@0 94 function run_test() {
michael@0 95 run_next_test();
michael@0 96 }

mercurial