|
1 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 const TEST_URI = "data:text/html;charset=utf-8,<p>bug 585991 - autocomplete popup keyboard usage test"; |
|
7 let HUD, popup, jsterm, inputNode, completeNode; |
|
8 |
|
9 function test() { |
|
10 addTab(TEST_URI); |
|
11 browser.addEventListener("load", function onLoad() { |
|
12 browser.removeEventListener("load", onLoad, true); |
|
13 openConsole(null, consoleOpened); |
|
14 }, true); |
|
15 } |
|
16 |
|
17 function consoleOpened(aHud) { |
|
18 HUD = aHud; |
|
19 info("web console opened"); |
|
20 |
|
21 jsterm = HUD.jsterm; |
|
22 |
|
23 jsterm.execute("window.foobarBug585991={" + |
|
24 "'item0': 'value0'," + |
|
25 "'item1': 'value1'," + |
|
26 "'item2': 'value2'," + |
|
27 "'item3': 'value3'" + |
|
28 "}"); |
|
29 popup = jsterm.autocompletePopup; |
|
30 completeNode = jsterm.completeNode; |
|
31 inputNode = jsterm.inputNode; |
|
32 |
|
33 ok(!popup.isOpen, "popup is not open"); |
|
34 |
|
35 popup._panel.addEventListener("popupshown", function onShown() { |
|
36 popup._panel.removeEventListener("popupshown", onShown, false); |
|
37 |
|
38 ok(popup.isOpen, "popup is open"); |
|
39 |
|
40 // 4 values, and the following properties: |
|
41 // __defineGetter__ __defineSetter__ __lookupGetter__ __lookupSetter__ |
|
42 // hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString |
|
43 // toSource unwatch valueOf watch constructor. |
|
44 is(popup.itemCount, 18, "popup.itemCount is correct"); |
|
45 |
|
46 let sameItems = popup.getItems().reverse().map(function(e) {return e.label;}); |
|
47 ok(sameItems.every(function(prop, index) { |
|
48 return [ |
|
49 "__defineGetter__", |
|
50 "__defineSetter__", |
|
51 "__lookupGetter__", |
|
52 "__lookupSetter__", |
|
53 "constructor", |
|
54 "hasOwnProperty", |
|
55 "isPrototypeOf", |
|
56 "item0", |
|
57 "item1", |
|
58 "item2", |
|
59 "item3", |
|
60 "propertyIsEnumerable", |
|
61 "toLocaleString", |
|
62 "toSource", |
|
63 "toString", |
|
64 "unwatch", |
|
65 "valueOf", |
|
66 "watch", |
|
67 ][index] === prop}), "getItems returns the items we expect"); |
|
68 |
|
69 is(popup.selectedIndex, 17, |
|
70 "Index of the first item from bottom is selected."); |
|
71 EventUtils.synthesizeKey("VK_DOWN", {}); |
|
72 |
|
73 let prefix = jsterm.inputNode.value.replace(/[\S]/g, " "); |
|
74 |
|
75 is(popup.selectedIndex, 0, "index 0 is selected"); |
|
76 is(popup.selectedItem.label, "watch", "watch is selected"); |
|
77 is(completeNode.value, prefix + "watch", |
|
78 "completeNode.value holds watch"); |
|
79 |
|
80 EventUtils.synthesizeKey("VK_DOWN", {}); |
|
81 |
|
82 is(popup.selectedIndex, 1, "index 1 is selected"); |
|
83 is(popup.selectedItem.label, "valueOf", "valueOf is selected"); |
|
84 is(completeNode.value, prefix + "valueOf", |
|
85 "completeNode.value holds valueOf"); |
|
86 |
|
87 EventUtils.synthesizeKey("VK_UP", {}); |
|
88 |
|
89 is(popup.selectedIndex, 0, "index 0 is selected"); |
|
90 is(popup.selectedItem.label, "watch", "watch is selected"); |
|
91 is(completeNode.value, prefix + "watch", |
|
92 "completeNode.value holds watch"); |
|
93 |
|
94 let currentSelectionIndex = popup.selectedIndex; |
|
95 |
|
96 EventUtils.synthesizeKey("VK_PAGE_DOWN", {}); |
|
97 |
|
98 ok(popup.selectedIndex > currentSelectionIndex, |
|
99 "Index is greater after PGDN"); |
|
100 |
|
101 currentSelectionIndex = popup.selectedIndex; |
|
102 EventUtils.synthesizeKey("VK_PAGE_UP", {}); |
|
103 |
|
104 ok(popup.selectedIndex < currentSelectionIndex, "Index is less after Page UP"); |
|
105 |
|
106 info("press Tab and wait for popup to hide"); |
|
107 popup._panel.addEventListener("popuphidden", popupHideAfterTab, false); |
|
108 EventUtils.synthesizeKey("VK_TAB", {}); |
|
109 }, false); |
|
110 |
|
111 info("wait for completion: window.foobarBug585991."); |
|
112 jsterm.setInputValue("window.foobarBug585991"); |
|
113 EventUtils.synthesizeKey(".", {}); |
|
114 } |
|
115 |
|
116 function popupHideAfterTab() |
|
117 { |
|
118 // At this point the completion suggestion should be accepted. |
|
119 popup._panel.removeEventListener("popuphidden", popupHideAfterTab, false); |
|
120 |
|
121 ok(!popup.isOpen, "popup is not open"); |
|
122 |
|
123 is(inputNode.value, "window.foobarBug585991.watch", |
|
124 "completion was successful after VK_TAB"); |
|
125 |
|
126 ok(!completeNode.value, "completeNode is empty"); |
|
127 |
|
128 popup._panel.addEventListener("popupshown", function onShown() { |
|
129 popup._panel.removeEventListener("popupshown", onShown, false); |
|
130 |
|
131 ok(popup.isOpen, "popup is open"); |
|
132 |
|
133 is(popup.itemCount, 18, "popup.itemCount is correct"); |
|
134 |
|
135 is(popup.selectedIndex, 17, "First index from bottom is selected"); |
|
136 EventUtils.synthesizeKey("VK_DOWN", {}); |
|
137 |
|
138 let prefix = jsterm.inputNode.value.replace(/[\S]/g, " "); |
|
139 |
|
140 is(popup.selectedIndex, 0, "index 0 is selected"); |
|
141 is(popup.selectedItem.label, "watch", "watch is selected"); |
|
142 is(completeNode.value, prefix + "watch", |
|
143 "completeNode.value holds watch"); |
|
144 |
|
145 popup._panel.addEventListener("popuphidden", function onHidden() { |
|
146 popup._panel.removeEventListener("popuphidden", onHidden, false); |
|
147 |
|
148 ok(!popup.isOpen, "popup is not open after VK_ESCAPE"); |
|
149 |
|
150 is(inputNode.value, "window.foobarBug585991.", |
|
151 "completion was cancelled"); |
|
152 |
|
153 ok(!completeNode.value, "completeNode is empty"); |
|
154 |
|
155 executeSoon(testReturnKey); |
|
156 }, false); |
|
157 |
|
158 info("press Escape to close the popup"); |
|
159 executeSoon(function() { |
|
160 EventUtils.synthesizeKey("VK_ESCAPE", {}); |
|
161 }); |
|
162 }, false); |
|
163 |
|
164 info("wait for completion: window.foobarBug585991."); |
|
165 executeSoon(function() { |
|
166 jsterm.setInputValue("window.foobarBug585991"); |
|
167 EventUtils.synthesizeKey(".", {}); |
|
168 }); |
|
169 } |
|
170 |
|
171 function testReturnKey() |
|
172 { |
|
173 popup._panel.addEventListener("popupshown", function onShown() { |
|
174 popup._panel.removeEventListener("popupshown", onShown, false); |
|
175 |
|
176 ok(popup.isOpen, "popup is open"); |
|
177 |
|
178 is(popup.itemCount, 18, "popup.itemCount is correct"); |
|
179 |
|
180 is(popup.selectedIndex, 17, "First index from bottom is selected"); |
|
181 EventUtils.synthesizeKey("VK_DOWN", {}); |
|
182 |
|
183 let prefix = jsterm.inputNode.value.replace(/[\S]/g, " "); |
|
184 |
|
185 is(popup.selectedIndex, 0, "index 0 is selected"); |
|
186 is(popup.selectedItem.label, "watch", "watch is selected"); |
|
187 is(completeNode.value, prefix + "watch", |
|
188 "completeNode.value holds watch"); |
|
189 |
|
190 EventUtils.synthesizeKey("VK_DOWN", {}); |
|
191 |
|
192 is(popup.selectedIndex, 1, "index 1 is selected"); |
|
193 is(popup.selectedItem.label, "valueOf", "valueOf is selected"); |
|
194 is(completeNode.value, prefix + "valueOf", |
|
195 "completeNode.value holds valueOf"); |
|
196 |
|
197 popup._panel.addEventListener("popuphidden", function onHidden() { |
|
198 popup._panel.removeEventListener("popuphidden", onHidden, false); |
|
199 |
|
200 ok(!popup.isOpen, "popup is not open after VK_RETURN"); |
|
201 |
|
202 is(inputNode.value, "window.foobarBug585991.valueOf", |
|
203 "completion was successful after VK_RETURN"); |
|
204 |
|
205 ok(!completeNode.value, "completeNode is empty"); |
|
206 |
|
207 dontShowArrayNumbers(); |
|
208 }, false); |
|
209 |
|
210 info("press Return to accept suggestion. wait for popup to hide"); |
|
211 |
|
212 executeSoon(() => EventUtils.synthesizeKey("VK_RETURN", {})); |
|
213 }, false); |
|
214 |
|
215 info("wait for completion suggestions: window.foobarBug585991."); |
|
216 |
|
217 executeSoon(function() { |
|
218 jsterm.setInputValue("window.foobarBug58599"); |
|
219 EventUtils.synthesizeKey("1", {}); |
|
220 EventUtils.synthesizeKey(".", {}); |
|
221 }); |
|
222 } |
|
223 |
|
224 function dontShowArrayNumbers() |
|
225 { |
|
226 info("dontShowArrayNumbers"); |
|
227 content.wrappedJSObject.foobarBug585991 = ["Sherlock Holmes"]; |
|
228 |
|
229 let jsterm = HUD.jsterm; |
|
230 let popup = jsterm.autocompletePopup; |
|
231 let completeNode = jsterm.completeNode; |
|
232 |
|
233 popup._panel.addEventListener("popupshown", function onShown() { |
|
234 popup._panel.removeEventListener("popupshown", onShown, false); |
|
235 |
|
236 let sameItems = popup.getItems().map(function(e) {return e.label;}); |
|
237 ok(!sameItems.some(function(prop, index) { prop === "0"; }), |
|
238 "Completing on an array doesn't show numbers."); |
|
239 |
|
240 popup._panel.addEventListener("popuphidden", testReturnWithNoSelection, false); |
|
241 |
|
242 info("wait for popup to hide"); |
|
243 executeSoon(() => EventUtils.synthesizeKey("VK_ESCAPE", {})); |
|
244 }, false); |
|
245 |
|
246 info("wait for popup to show"); |
|
247 executeSoon(() => { |
|
248 jsterm.setInputValue("window.foobarBug585991"); |
|
249 EventUtils.synthesizeKey(".", {}); |
|
250 }); |
|
251 } |
|
252 |
|
253 function testReturnWithNoSelection() |
|
254 { |
|
255 popup._panel.removeEventListener("popuphidden", testReturnWithNoSelection, false); |
|
256 |
|
257 info("test pressing return with open popup, but no selection, see bug 873250"); |
|
258 content.wrappedJSObject.testBug873250a = "hello world"; |
|
259 content.wrappedJSObject.testBug873250b = "hello world 2"; |
|
260 |
|
261 popup._panel.addEventListener("popupshown", function onShown() { |
|
262 popup._panel.removeEventListener("popupshown", onShown); |
|
263 |
|
264 ok(popup.isOpen, "popup is open"); |
|
265 is(popup.itemCount, 2, "popup.itemCount is correct"); |
|
266 isnot(popup.selectedIndex, -1, "popup.selectedIndex is correct"); |
|
267 |
|
268 info("press Return and wait for popup to hide"); |
|
269 popup._panel.addEventListener("popuphidden", popupHideAfterReturnWithNoSelection); |
|
270 executeSoon(() => EventUtils.synthesizeKey("VK_RETURN", {})); |
|
271 }); |
|
272 |
|
273 executeSoon(() => { |
|
274 info("wait for popup to show"); |
|
275 jsterm.setInputValue("window.testBu"); |
|
276 EventUtils.synthesizeKey("g", {}); |
|
277 }); |
|
278 } |
|
279 |
|
280 function popupHideAfterReturnWithNoSelection() |
|
281 { |
|
282 popup._panel.removeEventListener("popuphidden", popupHideAfterReturnWithNoSelection); |
|
283 |
|
284 ok(!popup.isOpen, "popup is not open after VK_RETURN"); |
|
285 |
|
286 is(inputNode.value, "", "inputNode is empty after VK_RETURN"); |
|
287 is(completeNode.value, "", "completeNode is empty"); |
|
288 is(jsterm.history[jsterm.history.length-1], "window.testBug", |
|
289 "jsterm history is correct"); |
|
290 |
|
291 executeSoon(testCompletionInText); |
|
292 } |
|
293 |
|
294 function testCompletionInText() |
|
295 { |
|
296 info("test that completion works inside text, see bug 812618"); |
|
297 |
|
298 popup._panel.addEventListener("popupshown", function onShown() { |
|
299 popup._panel.removeEventListener("popupshown", onShown); |
|
300 |
|
301 ok(popup.isOpen, "popup is open"); |
|
302 is(popup.itemCount, 2, "popup.itemCount is correct"); |
|
303 |
|
304 EventUtils.synthesizeKey("VK_DOWN", {}); |
|
305 is(popup.selectedIndex, 0, "popup.selectedIndex is correct"); |
|
306 ok(!completeNode.value, "completeNode.value is empty"); |
|
307 |
|
308 let items = popup.getItems().reverse().map(e => e.label); |
|
309 let sameItems = items.every((prop, index) => |
|
310 ["testBug873250a", "testBug873250b"][index] === prop); |
|
311 ok(sameItems, "getItems returns the items we expect"); |
|
312 |
|
313 info("press Tab and wait for popup to hide"); |
|
314 popup._panel.addEventListener("popuphidden", popupHideAfterCompletionInText); |
|
315 EventUtils.synthesizeKey("VK_TAB", {}); |
|
316 }); |
|
317 |
|
318 jsterm.setInputValue("dump(window.testBu)"); |
|
319 inputNode.selectionStart = inputNode.selectionEnd = 18; |
|
320 EventUtils.synthesizeKey("g", {}); |
|
321 } |
|
322 |
|
323 function popupHideAfterCompletionInText() |
|
324 { |
|
325 // At this point the completion suggestion should be accepted. |
|
326 popup._panel.removeEventListener("popuphidden", popupHideAfterCompletionInText); |
|
327 |
|
328 ok(!popup.isOpen, "popup is not open"); |
|
329 is(inputNode.value, "dump(window.testBug873250b)", |
|
330 "completion was successful after VK_TAB"); |
|
331 is(inputNode.selectionStart, 26, "cursor location is correct"); |
|
332 is(inputNode.selectionStart, inputNode.selectionEnd, "cursor location (confirmed)"); |
|
333 ok(!completeNode.value, "completeNode is empty"); |
|
334 |
|
335 finishUp(); |
|
336 } |
|
337 |
|
338 function finishUp() { |
|
339 HUD = popup = jsterm = inputNode = completeNode = null; |
|
340 finishTest(); |
|
341 } |