|
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 // Tests that code completion works properly. |
|
7 |
|
8 const TEST_URI = "data:text/html;charset=utf8,<p>test code completion"; |
|
9 |
|
10 let testDriver; |
|
11 |
|
12 function test() { |
|
13 addTab(TEST_URI); |
|
14 browser.addEventListener("load", function onLoad() { |
|
15 browser.removeEventListener("load", onLoad, true); |
|
16 openConsole(null, function(hud) { |
|
17 testDriver = testCompletion(hud); |
|
18 testDriver.next(); |
|
19 }); |
|
20 }, true); |
|
21 } |
|
22 |
|
23 function testNext() { |
|
24 executeSoon(function() { |
|
25 testDriver.next(); |
|
26 }); |
|
27 } |
|
28 |
|
29 function testCompletion(hud) { |
|
30 let jsterm = hud.jsterm; |
|
31 let input = jsterm.inputNode; |
|
32 |
|
33 // Test typing 'docu'. |
|
34 input.value = "docu"; |
|
35 input.setSelectionRange(4, 4); |
|
36 jsterm.complete(jsterm.COMPLETE_HINT_ONLY, testNext); |
|
37 yield undefined; |
|
38 |
|
39 is(input.value, "docu", "'docu' completion (input.value)"); |
|
40 is(jsterm.completeNode.value, " ment", "'docu' completion (completeNode)"); |
|
41 |
|
42 // Test typing 'docu' and press tab. |
|
43 input.value = "docu"; |
|
44 input.setSelectionRange(4, 4); |
|
45 jsterm.complete(jsterm.COMPLETE_FORWARD, testNext); |
|
46 yield undefined; |
|
47 |
|
48 is(input.value, "document", "'docu' tab completion"); |
|
49 is(input.selectionStart, 8, "start selection is alright"); |
|
50 is(input.selectionEnd, 8, "end selection is alright"); |
|
51 is(jsterm.completeNode.value.replace(/ /g, ""), "", "'docu' completed"); |
|
52 |
|
53 // Test typing 'window.Ob' and press tab. Just 'window.O' is |
|
54 // ambiguous: could be window.Object, window.Option, etc. |
|
55 input.value = "window.Ob"; |
|
56 input.setSelectionRange(9, 9); |
|
57 jsterm.complete(jsterm.COMPLETE_FORWARD, testNext); |
|
58 yield undefined; |
|
59 |
|
60 is(input.value, "window.Object", "'window.Ob' tab completion"); |
|
61 |
|
62 // Test typing 'document.getElem'. |
|
63 input.value = "document.getElem"; |
|
64 input.setSelectionRange(16, 16); |
|
65 jsterm.complete(jsterm.COMPLETE_FORWARD, testNext); |
|
66 yield undefined; |
|
67 |
|
68 is(input.value, "document.getElem", "'document.getElem' completion"); |
|
69 is(jsterm.completeNode.value, " entsByTagNameNS", "'document.getElem' completion"); |
|
70 |
|
71 // Test pressing tab another time. |
|
72 jsterm.complete(jsterm.COMPLETE_FORWARD, testNext); |
|
73 yield undefined; |
|
74 |
|
75 is(input.value, "document.getElem", "'document.getElem' completion"); |
|
76 is(jsterm.completeNode.value, " entsByTagName", "'document.getElem' another tab completion"); |
|
77 |
|
78 // Test pressing shift_tab. |
|
79 jsterm.complete(jsterm.COMPLETE_BACKWARD, testNext); |
|
80 yield undefined; |
|
81 |
|
82 is(input.value, "document.getElem", "'document.getElem' untab completion"); |
|
83 is(jsterm.completeNode.value, " entsByTagNameNS", "'document.getElem' completion"); |
|
84 |
|
85 jsterm.clearOutput(); |
|
86 |
|
87 input.value = "docu"; |
|
88 jsterm.complete(jsterm.COMPLETE_HINT_ONLY, testNext); |
|
89 yield undefined; |
|
90 |
|
91 is(jsterm.completeNode.value, " ment", "'docu' completion"); |
|
92 jsterm.execute(); |
|
93 is(jsterm.completeNode.value, "", "clear completion on execute()"); |
|
94 |
|
95 // Test multi-line completion works |
|
96 input.value = "console.log('one');\nconsol"; |
|
97 jsterm.complete(jsterm.COMPLETE_HINT_ONLY, testNext); |
|
98 yield undefined; |
|
99 |
|
100 is(jsterm.completeNode.value, " \n e", "multi-line completion"); |
|
101 |
|
102 // Test non-object autocompletion. |
|
103 input.value = "Object.name.sl"; |
|
104 jsterm.complete(jsterm.COMPLETE_HINT_ONLY, testNext); |
|
105 yield undefined; |
|
106 |
|
107 is(jsterm.completeNode.value, " ice", "non-object completion"); |
|
108 |
|
109 // Test string literal autocompletion. |
|
110 input.value = "'Asimov'.sl"; |
|
111 jsterm.complete(jsterm.COMPLETE_HINT_ONLY, testNext); |
|
112 yield undefined; |
|
113 |
|
114 is(jsterm.completeNode.value, " ice", "string literal completion"); |
|
115 |
|
116 testDriver = jsterm = input = null; |
|
117 executeSoon(finishTest); |
|
118 yield undefined; |
|
119 } |
|
120 |