|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 function AutoCompleteResult(aValues) { |
|
5 this._values = aValues; |
|
6 this.defaultIndex = -1; |
|
7 this._typeAheadResult = false; |
|
8 } |
|
9 AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); |
|
10 |
|
11 function AutoCompleteTypeAheadResult(aValues) { |
|
12 this._values = aValues; |
|
13 this.defaultIndex = 0; |
|
14 this._typeAheadResult = true; |
|
15 } |
|
16 AutoCompleteTypeAheadResult.prototype = Object.create(AutoCompleteResultBase.prototype); |
|
17 |
|
18 |
|
19 /** |
|
20 * Test AutoComplete with multiple AutoCompleteSearch sources, with one of them |
|
21 * being hidden from the popup, but can still do typeahead completion. |
|
22 */ |
|
23 function run_test() { |
|
24 do_test_pending(); |
|
25 |
|
26 var inputStr = "moz"; |
|
27 |
|
28 // Type ahead result |
|
29 var searchTypeAhead = new AutoCompleteSearchBase("search1", |
|
30 new AutoCompleteTypeAheadResult(["mozillaTest1"])); |
|
31 // Regular result |
|
32 var searchNormal = new AutoCompleteSearchBase("search2", |
|
33 new AutoCompleteResult(["mozillaTest2"])); |
|
34 |
|
35 // Register searches so AutoCompleteController can find them |
|
36 registerAutoCompleteSearch(searchNormal); |
|
37 registerAutoCompleteSearch(searchTypeAhead); |
|
38 |
|
39 // Make an AutoCompleteInput that uses our searches |
|
40 // and confirms results on search complete. |
|
41 var input = new AutoCompleteInputBase([searchTypeAhead.name, searchNormal.name]); |
|
42 input.completeDefaultIndex = true; |
|
43 input.textValue = inputStr; |
|
44 |
|
45 // Caret must be at the end. Autofill doesn't happen unless you're typing |
|
46 // characters at the end. |
|
47 var strLen = inputStr.length; |
|
48 input.selectTextRange(strLen, strLen); |
|
49 do_check_eq(input.selectionStart, strLen); |
|
50 do_check_eq(input.selectionEnd, strLen); |
|
51 |
|
52 var controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
|
53 getService(Ci.nsIAutoCompleteController); |
|
54 |
|
55 controller.input = input; |
|
56 controller.startSearch(inputStr); |
|
57 |
|
58 input.onSearchComplete = function() { |
|
59 // Hidden results should still be able to do inline autocomplete |
|
60 do_check_eq(input.textValue, "mozillaTest1"); |
|
61 |
|
62 // Now, let's fill the textbox with the first result of the popup. |
|
63 // The first search is marked as hidden, so we must always get the |
|
64 // second search. |
|
65 controller.handleEnter(true); |
|
66 do_check_eq(input.textValue, "mozillaTest2"); |
|
67 |
|
68 // Only one item in the popup. |
|
69 do_check_eq(controller.matchCount, 1); |
|
70 |
|
71 // Unregister searches |
|
72 unregisterAutoCompleteSearch(searchNormal); |
|
73 unregisterAutoCompleteSearch(searchTypeAhead); |
|
74 do_test_finished(); |
|
75 }; |
|
76 } |