OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Include test fixture. | |
6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js']); | |
7 | |
8 /** | |
9 * Test fixture. | |
10 * @constructor | |
11 * @extends {ChromeVoxUnitTestBase} | |
12 */ | |
13 function CvoxContentEditableExtractorUnitTest() {} | |
14 | |
15 CvoxContentEditableExtractorUnitTest.prototype = { | |
16 __proto__: ChromeVoxUnitTestBase.prototype, | |
17 | |
18 /** @override */ | |
19 closureModuleDeps: [ | |
20 'cvox.ContentEditableExtractor', | |
21 ] | |
22 }; | |
23 | |
24 TEST_F('CvoxContentEditableExtractorUnitTest', 'EmptyElement', function() { | |
25 this.loadDoc(function() {/*! | |
26 <div> | |
27 <div id="textbox" contentEditable="true"></div> | |
28 </div> | |
29 */}); | |
30 | |
31 var textbox = $('textbox'); | |
32 var extractor = new cvox.ContentEditableExtractor(); | |
33 extractor.update(textbox); | |
34 assertEquals('', extractor.getText()); | |
35 assertEquals(0, extractor.getStartIndex()); | |
36 assertEquals(0, extractor.getEndIndex(0)); | |
37 assertEquals(0, extractor.getLineIndex(0)); | |
38 assertEquals(0, extractor.getLineStart(0)); | |
39 assertEquals(0, extractor.getLineEnd(0)); | |
40 }); | |
41 | |
42 /** | |
43 * Test getting text and selections from a single contenteditable node. | |
44 */ | |
45 TEST_F('CvoxContentEditableExtractorUnitTest', 'SingleTextNode', function() { | |
46 this.loadDoc(function() {/*! | |
47 <div> | |
48 <div id="textbox" contentEditable="true">Hello</div> | |
49 </div> | |
50 */}); | |
51 var textbox = $('textbox'); | |
52 | |
53 var extractor = new cvox.ContentEditableExtractor(); | |
54 extractor.update(textbox); | |
55 assertEquals('Hello', extractor.getText()); | |
56 assertEquals(0, extractor.getLineIndex(0)); | |
57 assertEquals(0, extractor.getLineStart(0)); | |
58 assertEquals(5, extractor.getLineEnd(0)); | |
59 assertEquals(5, extractor.getStartIndex()); | |
60 assertEquals(5, extractor.getEndIndex()); | |
61 | |
62 // Test all possible cursor positions. | |
63 for (var i = 0; i <= 5; i++) { | |
64 setSelection(textbox.firstChild, i, textbox.firstChild, i); | |
65 extractor.update(textbox); | |
66 assertEquals(i, extractor.getStartIndex()); | |
67 assertEquals(i, extractor.getEndIndex()); | |
68 } | |
69 | |
70 // Test all possible ways to select one character. | |
71 for (i = 0; i < 5; i++) { | |
72 setSelection(textbox.firstChild, i, textbox.firstChild, i + 1); | |
73 extractor.update(textbox); | |
74 assertEquals(i, extractor.getStartIndex()); | |
75 assertEquals(i + 1, extractor.getEndIndex()); | |
76 } | |
77 | |
78 // Test selecting everything. | |
79 setSelection(textbox.firstChild, 0, textbox.firstChild, 5); | |
80 extractor.update(textbox); | |
81 assertEquals(0, extractor.getStartIndex()); | |
82 assertEquals(5, extractor.getEndIndex()); | |
83 }); | |
84 | |
85 /** | |
86 * Test getting text and selections from a contenteditable node with | |
87 * nonprinted whitespace. | |
88 */ | |
89 TEST_F('CvoxContentEditableExtractorUnitTest', 'TextWithWhitespace', | |
90 function() { | |
91 this.loadDoc(function() {/*! | |
92 <div> | |
93 <div id="textbox" contentEditable="true"> Hello World </div> | |
94 </div> | |
95 */}); | |
96 var textbox = $('textbox'); | |
97 | |
98 var extractor = new cvox.ContentEditableExtractor(); | |
99 extractor.update(textbox); | |
100 assertEquals('Hello World', extractor.getText()); | |
101 assertEquals(0, extractor.getLineIndex(0)); | |
102 assertEquals(0, extractor.getLineStart(0)); | |
103 assertEquals(11, extractor.getLineEnd(0)); | |
104 assertEquals(11, extractor.getStartIndex()); | |
105 assertEquals(11, extractor.getEndIndex()); | |
106 | |
107 // Test all *reasonable* indexes of a selection into this text node | |
108 // and the logical index into the text that these should result in. | |
109 var expectedIndexMap = { | |
110 0: 0, | |
111 1: 0, | |
112 2: 1, | |
113 3: 2, | |
114 4: 3, | |
115 5: 4, | |
116 6: 5, | |
117 // Note: index=7 should never happen | |
118 8: 6, | |
119 9: 7, | |
120 10: 8, | |
121 11: 9, | |
122 12: 10, | |
123 13: 11, | |
124 14: 11 | |
125 }; | |
126 for (var srcIndex in expectedIndexMap) { | |
127 var dstIndex = expectedIndexMap[srcIndex]; | |
128 setSelection(textbox.firstChild, srcIndex, textbox.firstChild, srcIndex); | |
129 extractor.update(textbox); | |
130 assertEquals(dstIndex, extractor.getStartIndex()); | |
131 assertEquals(dstIndex, extractor.getEndIndex()); | |
132 } | |
133 }); | |
134 | |
135 /** | |
136 * Test getting text and selections from a contenteditable node with | |
137 * preformatted text. | |
138 */ | |
139 TEST_F('CvoxContentEditableExtractorUnitTest', 'Preformatted', function() { | |
140 this.loadDoc(function() {/*! | |
141 <div> | |
142 <pre id="textbox" contentEditable="true">aaaaaaaaaa | |
143 bbbbbbbbbb | |
144 cccccccccc</pre> | |
145 </div> | |
146 */}); | |
147 var textbox = $('textbox'); | |
148 | |
149 var extractor = new cvox.ContentEditableExtractor(); | |
150 extractor.update(textbox); | |
151 assertEquals('aaaaaaaaaa\nbbbbbbbbbb\ncccccccccc', extractor.getText()); | |
152 assertEquals(0, extractor.getLineStart(0)); | |
153 assertEquals(11, extractor.getLineEnd(0)); | |
154 assertEquals(11, extractor.getLineStart(1)); | |
155 assertEquals(22, extractor.getLineEnd(1)); | |
156 assertEquals(22, extractor.getLineStart(2)); | |
157 assertEquals(32, extractor.getLineEnd(2)); | |
158 | |
159 // Test all possible cursor positions. | |
160 for (var i = 0; i <= 32; i++) { | |
161 setSelection(textbox.firstChild, i, textbox.firstChild, i); | |
162 extractor.update(textbox); | |
163 assertEquals(i, extractor.getStartIndex()); | |
164 assertEquals(i, extractor.getEndIndex()); | |
165 } | |
166 }); | |
167 | |
168 /** | |
169 * Test getting text and selections from a contenteditable node with | |
170 * wrapping. | |
171 */ | |
172 TEST_F('CvoxContentEditableExtractorUnitTest', 'WordWrap', function() { | |
173 this.loadDoc(function() {/*! | |
174 <div> | |
175 <div id="textbox" | |
176 style="width: 1em; word-wrap: normal" | |
177 contentEditable="true">One two three</div> | |
178 </div> | |
179 */}); | |
180 var textbox = $('textbox'); | |
181 | |
182 var extractor = new cvox.ContentEditableExtractor(); | |
183 extractor.update(textbox); | |
184 assertEquals('One\ntwo\nthree', extractor.getText()); | |
185 assertEquals(0, extractor.getLineStart(0)); | |
186 assertEquals(4, extractor.getLineEnd(0)); | |
187 assertEquals(4, extractor.getLineStart(1)); | |
188 assertEquals(8, extractor.getLineEnd(1)); | |
189 assertEquals(8, extractor.getLineStart(2)); | |
190 assertEquals(13, extractor.getLineEnd(2)); | |
191 | |
192 // Test all possible cursor positions. | |
193 for (var i = 0; i <= 13; i++) { | |
194 setSelection(textbox.firstChild, i, textbox.firstChild, i); | |
195 extractor.update(textbox); | |
196 assertEquals(i, extractor.getStartIndex()); | |
197 assertEquals(i, extractor.getEndIndex()); | |
198 } | |
199 }); | |
200 | |
201 /** | |
202 * Test getting text and lines from a contenteditable region | |
203 * containing two paragraphs and an explicit line break. | |
204 */ | |
205 TEST_F('CvoxContentEditableExtractorUnitTest', 'TwoParas', function() { | |
206 this.loadDoc(function() {/*! | |
207 <div> | |
208 <div id="textbox" contentEditable="true"> | |
209 <p>One</p> | |
210 <p>Two<br>Three</p> | |
211 </div> | |
212 </div> | |
213 */}); | |
214 var textbox = $('textbox'); | |
215 | |
216 var extractor = new cvox.ContentEditableExtractor(); | |
217 extractor.update(textbox); | |
218 assertEquals('One\nTwo\nThree', | |
219 extractor.getText()); | |
220 assertEquals(0, extractor.getLineStart(0)); | |
221 assertEquals(4, extractor.getLineEnd(0)); | |
222 assertEquals(4, extractor.getLineStart(1)); | |
223 assertEquals(8, extractor.getLineEnd(1)); | |
224 assertEquals(8, extractor.getLineStart(2)); | |
225 assertEquals(13, extractor.getLineEnd(2)); | |
226 }); | |
227 | |
228 /** | |
229 * Test getting text and lines from a contenteditable region | |
230 * containing two paragraphs, this time with added whitespace. | |
231 */ | |
232 TEST_F('CvoxContentEditableExtractorUnitTest', 'TwoParasWithWhitespace', | |
233 function() { | |
234 this.loadDoc(function() {/*! | |
235 <div> | |
236 <div id="textbox" contentEditable="true"> | |
237 <p> One </p> | |
238 <p> Two <br> Three </p> | |
239 </div> | |
240 </div> | |
241 */}); | |
242 var textbox = $('textbox'); | |
243 | |
244 var extractor = new cvox.ContentEditableExtractor(); | |
245 extractor.update(textbox); | |
246 assertEquals('One\nTwo Three', | |
247 extractor.getText()); | |
248 assertEquals(0, extractor.getLineStart(0)); | |
249 assertEquals(4, extractor.getLineEnd(0)); | |
250 assertEquals(4, extractor.getLineStart(1)); | |
251 assertEquals(8, extractor.getLineEnd(1)); | |
252 assertEquals(8, extractor.getLineStart(2)); | |
253 assertEquals(13, extractor.getLineEnd(2)); | |
254 }); | |
255 | |
256 /** | |
257 * Test getting text and lines from a contenteditable region | |
258 * containing some raw text and then some text in a block-level element. | |
259 */ | |
260 TEST_F('CvoxContentEditableExtractorUnitTest', 'NodePlusElement', function() { | |
261 this.loadDoc(function() {/*! | |
262 <div> | |
263 <div id="textbox" | |
264 contentEditable="true">One<div>Two<br>Three</div></div> | |
265 </div> | |
266 */}); | |
267 var textbox = $('textbox'); | |
268 | |
269 var extractor = new cvox.ContentEditableExtractor(); | |
270 extractor.update(textbox); | |
271 assertEquals('One\nTwo\nThree', | |
272 extractor.getText()); | |
273 assertEquals(0, extractor.getLineStart(0)); | |
274 assertEquals(4, extractor.getLineEnd(0)); | |
275 assertEquals(4, extractor.getLineStart(1)); | |
276 assertEquals(8, extractor.getLineEnd(1)); | |
277 assertEquals(8, extractor.getLineStart(2)); | |
278 assertEquals(13, extractor.getLineEnd(2)); | |
279 | |
280 var oneTextNode = textbox.firstChild; | |
281 assertEquals('One', oneTextNode.data); | |
282 var twoTextNode = textbox.firstElementChild.firstChild; | |
283 assertEquals('Two', twoTextNode.data); | |
284 var threeTextNode = twoTextNode.nextSibling.nextSibling; | |
285 assertEquals('Three', threeTextNode.data); | |
286 | |
287 // End of first line. | |
288 setSelection(oneTextNode, 3, oneTextNode, 3); | |
289 extractor.update(textbox); | |
290 assertEquals(3, extractor.getStartIndex()); | |
291 assertEquals(3, extractor.getEndIndex()); | |
292 | |
293 // Beginning of second line. | |
294 setSelection(twoTextNode, 0, twoTextNode, 0); | |
295 extractor.update(textbox); | |
296 assertEquals(4, extractor.getStartIndex()); | |
297 assertEquals(4, extractor.getEndIndex()); | |
298 | |
299 // End of second line. | |
300 setSelection(twoTextNode, 3, twoTextNode, 3); | |
301 extractor.update(textbox); | |
302 assertEquals(7, extractor.getStartIndex()); | |
303 assertEquals(7, extractor.getEndIndex()); | |
304 | |
305 // Beginning of third line. | |
306 setSelection(threeTextNode, 0, threeTextNode, 0); | |
307 extractor.update(textbox); | |
308 assertEquals(8, extractor.getStartIndex()); | |
309 assertEquals(8, extractor.getEndIndex()); | |
310 }); | |
OLD | NEW |