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