|
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 function AutoCompleteImmediateSearch(aName, aResult) { |
|
7 this.name = aName; |
|
8 this._result = aResult; |
|
9 } |
|
10 AutoCompleteImmediateSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); |
|
11 AutoCompleteImmediateSearch.prototype.searchType = |
|
12 Ci.nsIAutoCompleteSearchDescriptor.SEARCH_TYPE_IMMEDIATE; |
|
13 AutoCompleteImmediateSearch.prototype.QueryInterface = |
|
14 XPCOMUtils.generateQI([Ci.nsIFactory, |
|
15 Ci.nsIAutoCompleteSearch, |
|
16 Ci.nsIAutoCompleteSearchDescriptor]); |
|
17 |
|
18 function AutoCompleteDelayedSearch(aName, aResult) { |
|
19 this.name = aName; |
|
20 this._result = aResult; |
|
21 } |
|
22 AutoCompleteDelayedSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); |
|
23 |
|
24 function AutoCompleteResult(aValues, aDefaultIndex) { |
|
25 this._values = aValues; |
|
26 this.defaultIndex = aDefaultIndex; |
|
27 } |
|
28 AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); |
|
29 |
|
30 function run_test() { |
|
31 run_next_test(); |
|
32 } |
|
33 |
|
34 /** |
|
35 * An immediate search should be executed synchronously. |
|
36 */ |
|
37 add_test(function test_immediate_search() { |
|
38 let immediateResults = ["mozillaTest"]; |
|
39 let inputStr = "moz"; |
|
40 |
|
41 let immediateSearch = new AutoCompleteImmediateSearch( |
|
42 "immediate", new AutoCompleteResult(["moz-immediate"], 0)); |
|
43 registerAutoCompleteSearch(immediateSearch); |
|
44 let delayedSearch = new AutoCompleteDelayedSearch( |
|
45 "delayed", new AutoCompleteResult(["moz-delayed"], 0)); |
|
46 registerAutoCompleteSearch(delayedSearch); |
|
47 |
|
48 let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
|
49 getService(Ci.nsIAutoCompleteController); |
|
50 |
|
51 let input = new AutoCompleteInputBase([delayedSearch.name, |
|
52 immediateSearch.name]); |
|
53 input.completeDefaultIndex = true; |
|
54 input.textValue = inputStr; |
|
55 |
|
56 // Caret must be at the end. Autofill doesn't happen unless you're typing |
|
57 // characters at the end. |
|
58 let strLen = inputStr.length; |
|
59 input.selectTextRange(strLen, strLen); |
|
60 |
|
61 controller.input = input; |
|
62 controller.startSearch(inputStr); |
|
63 |
|
64 // Immediately check the result, the immediate search should have finished. |
|
65 do_check_eq(input.textValue, "moz-immediate"); |
|
66 |
|
67 // Wait for both queries to finish. |
|
68 input.onSearchComplete = function() { |
|
69 // Sanity check. |
|
70 do_check_eq(input.textValue, "moz-immediate"); |
|
71 |
|
72 unregisterAutoCompleteSearch(immediateSearch); |
|
73 unregisterAutoCompleteSearch(delayedSearch); |
|
74 run_next_test(); |
|
75 }; |
|
76 }); |
|
77 |
|
78 /** |
|
79 * An immediate search should be executed before any delayed search. |
|
80 */ |
|
81 add_test(function test_immediate_search_notimeout() { |
|
82 let immediateResults = ["mozillaTest"]; |
|
83 let inputStr = "moz"; |
|
84 |
|
85 let immediateSearch = new AutoCompleteImmediateSearch( |
|
86 "immediate", new AutoCompleteResult(["moz-immediate"], 0)); |
|
87 registerAutoCompleteSearch(immediateSearch); |
|
88 |
|
89 let delayedSearch = new AutoCompleteDelayedSearch( |
|
90 "delayed", new AutoCompleteResult(["moz-delayed"], 0)); |
|
91 registerAutoCompleteSearch(delayedSearch); |
|
92 |
|
93 let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
|
94 getService(Ci.nsIAutoCompleteController); |
|
95 |
|
96 let input = new AutoCompleteInputBase([delayedSearch.name, |
|
97 immediateSearch.name]); |
|
98 input.completeDefaultIndex = true; |
|
99 input.textValue = inputStr; |
|
100 input.timeout = 0; |
|
101 |
|
102 // Caret must be at the end. Autofill doesn't happen unless you're typing |
|
103 // characters at the end. |
|
104 let strLen = inputStr.length; |
|
105 input.selectTextRange(strLen, strLen); |
|
106 |
|
107 controller.input = input; |
|
108 let complete = false; |
|
109 input.onSearchComplete = function() { |
|
110 complete = true; |
|
111 }; |
|
112 controller.startSearch(inputStr); |
|
113 do_check_true(complete); |
|
114 |
|
115 // Immediately check the result, the immediate search should have finished. |
|
116 do_check_eq(input.textValue, "moz-immediate"); |
|
117 |
|
118 unregisterAutoCompleteSearch(immediateSearch); |
|
119 unregisterAutoCompleteSearch(delayedSearch); |
|
120 run_next_test(); |
|
121 }); |
|
122 |
|
123 /** |
|
124 * A delayed search should be executed synchronously with a zero timeout. |
|
125 */ |
|
126 add_test(function test_delayed_search_notimeout() { |
|
127 let immediateResults = ["mozillaTest"]; |
|
128 let inputStr = "moz"; |
|
129 |
|
130 let delayedSearch = new AutoCompleteDelayedSearch( |
|
131 "delayed", new AutoCompleteResult(["moz-delayed"], 0)); |
|
132 registerAutoCompleteSearch(delayedSearch); |
|
133 |
|
134 let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
|
135 getService(Ci.nsIAutoCompleteController); |
|
136 |
|
137 let input = new AutoCompleteInputBase([delayedSearch.name]); |
|
138 input.completeDefaultIndex = true; |
|
139 input.textValue = inputStr; |
|
140 input.timeout = 0; |
|
141 |
|
142 // Caret must be at the end. Autofill doesn't happen unless you're typing |
|
143 // characters at the end. |
|
144 let strLen = inputStr.length; |
|
145 input.selectTextRange(strLen, strLen); |
|
146 |
|
147 controller.input = input; |
|
148 let complete = false; |
|
149 input.onSearchComplete = function() { |
|
150 complete = true; |
|
151 }; |
|
152 controller.startSearch(inputStr); |
|
153 do_check_true(complete); |
|
154 |
|
155 // Immediately check the result, the delayed search should have finished. |
|
156 do_check_eq(input.textValue, "moz-delayed"); |
|
157 |
|
158 unregisterAutoCompleteSearch(delayedSearch); |
|
159 run_next_test(); |
|
160 }); |