Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(338)

Side by Side Diff: chrome/browser/resources/chromeos/chromevox/common/braille_util_test.js

Issue 342153003: Add ChromeVox braille_util_test to chromevox_tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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 CvoxBrailleUtilUnitTest() {}
14
15 CvoxBrailleUtilUnitTest.prototype = {
16 __proto__: ChromeVoxUnitTestBase.prototype,
17
18 /** @override */
19 closureModuleDeps: [
20 'cvox.BrailleUtil',
21 'cvox.CursorSelection',
22 'cvox.NavigationShifter',
23 'cvox.TestMsgs',
24 ],
25
26 /** @override */
27 setUp: function() {
28 cvox.ChromeVox.msgs = new cvox.TestMsgs();
29 },
30
31 /**
32 * @param {!Node} expectedParent Expected parent node.
33 * @param {!Node} node Node to examine.
34 * @private
35 */
36 assertTextNodeChildOf_: function(expectedParent, node) {
37 assertEquals(Node.TEXT_NODE, node.nodeType);
38 assertEquals(expectedParent, node.parentNode);
39 },
40
41 /**
42 * Helper to retrieve braille for testing.
43 * @param {!cvox.CursorSelection} prevSel Previous selection.
44 * @param {!cvox.CursorSelection} sel Current selection.
45 * @return {!cvox.NavBraille} Resulting braille.
46 * @private
47 */
48 getBraille_: function(prevSel, sel) {
49 return (new cvox.NavigationShifter).getBraille(prevSel, sel);
50 },
51
52 /**
53 * Asserts that two NavBraille objects are equal, ignoring spans.
54 * @param {Object} expected Expected result, should have no spans.
55 * @param {cvox.NavBraille} actual Actual result.
56 */
57 assertBrailleEquals: function(expected, actual) {
58 actual = new cvox.NavBraille({
59 text: actual.text.toString(),
60 startIndex: actual.startIndex,
61 endIndex: actual.endIndex
62 });
63 assertThat(new cvox.NavBraille(expected), eqJSON(actual));
64 }
65 };
66
67 TEST_F('CvoxBrailleUtilUnitTest', 'BrailleName', function() {
68 this.loadHtml(
69 '<div id="navbar">' +
70 '<a id="1" href="one.com">one</a>' +
71 '<a id="2" href="two.com">two</a>' +
72 '<a id="3" href="three.com">three</a>' +
73 '</div>');
74 var navbar = cvox.CursorSelection.fromNode($('navbar'));
75 var braille = this.getBraille_(navbar, navbar);
76 this.assertBrailleEquals(
77 {text: 'one lnk two lnk three lnk',
78 startIndex: 0,
79 endIndex: 1
80 }, braille);
81
82 var one =
83 cvox.CursorSelection.fromNode($('1').firstChild);
84 braille = this.getBraille_(one, one);
85 this.assertBrailleEquals(
86 {text: 'one lnk two lnk three lnk',
87 startIndex: 0,
88 endIndex: 1
89 }, braille);
90
91 var two =
92 cvox.CursorSelection.fromNode($('2').firstChild);
93 braille = this.getBraille_(one, two);
94 this.assertBrailleEquals(
95 {text: 'one lnk two lnk three lnk',
96 startIndex: 8,
97 endIndex: 9
98 }, braille);
99
100 var three =
101 cvox.CursorSelection.fromNode($('3').firstChild);
102 braille = this.getBraille_(two, three);
103 this.assertBrailleEquals(
104 {text: 'one lnk two lnk three lnk',
105 startIndex: 16,
106 endIndex: 17
107 }, braille);
108 });
109
110
111 /**
112 * @export
113 */
114 TEST_F('CvoxBrailleUtilUnitTest', 'NameTemplate', function() {
115 this.loadHtml(
116 '<button id="1">Submit</button>' +
117 '<input id="2" type="text" aria-label="Search">'
118 );
119
120 var button = cvox.CursorSelection.fromNode($('1'));
121
122 this.assertBrailleEquals(
123 {text: '[Submit]',
124 startIndex: 0,
125 endIndex: 1
126 }, this.getBraille_(button, button));
127
128 var inputElement = $('2');
129 var input = cvox.CursorSelection.fromNode(inputElement);
130
131 // Note: the cursor appears where text would be typed.
132 this.assertBrailleEquals(
133 {text: 'Search: edtxt',
134 startIndex: 0,
135 endIndex: 1
136 }, this.getBraille_(input, input));
137 inputElement.focus();
138 this.assertBrailleEquals(
139 {text: 'Search: edtxt',
140 startIndex: 8,
141 endIndex: 8
142 }, this.getBraille_(input, input));
143 });
144
145
146 /**
147 * @export
148 */
149 TEST_F('CvoxBrailleUtilUnitTest', 'TextField', function() {
150 this.loadHtml(
151 '<input id="1" type="text" aria-label="Search" value="larry">'
152 );
153
154 var inputElement = $('1');
155 var input = cvox.CursorSelection.fromNode(inputElement);
156
157 // Note: the cursor appears where text would be typed.
158 // The cursor is at the beginning by default.
159 this.assertBrailleEquals(
160 {text: 'Search: larry edtxt',
161 startIndex: 0,
162 endIndex: 1
163 }, this.getBraille_(input, input));
164 inputElement.focus();
165 inputElement.selectionStart = 0;
166 inputElement.selectionEnd = 5;
167 this.assertBrailleEquals(
168 {text: 'Search: larry edtxt',
169 startIndex: 8,
170 endIndex: 13
171 }, this.getBraille_(input, input));
172 });
173
174
175 /**
176 * @export
177 */
178 TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldEmpty', function() {
179 this.loadHtml(
180 '<input id="1" type="text">'
181 );
182
183 var inputElement = $('1');
184 var input = cvox.CursorSelection.fromNode($('1'));
185
186 this.assertBrailleEquals(
187 {text: ': edtxt',
188 startIndex: 0,
189 endIndex: 1
190 }, this.getBraille_(input, input));
191 inputElement.focus();
192 this.assertBrailleEquals(
193 {text: ': edtxt',
194 startIndex: 2,
195 endIndex: 2
196 }, this.getBraille_(input, input));
197 });
198
199
200 /**
201 * @export
202 */
203 TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldSelection', function() {
204 this.loadHtml(
205 '<input id="1" type="text" value="strawberry">'
206 );
207
208 var inputElem = $('1');
209 var input = cvox.CursorSelection.fromNode(inputElem);
210
211 // Selection.
212 inputElem.selectionStart = 2;
213 inputElem.selectionEnd = 5;
214 this.assertBrailleEquals(
215 {text: ': strawberry edtxt',
216 startIndex: 4,
217 endIndex: 7
218 }, this.getBraille_(input, input));
219
220 // Cursor at end.
221 inputElem.selectionStart = 10;
222 inputElem.selectionEnd = 10;
223 this.assertBrailleEquals(
224 {text: ': strawberry edtxt',
225 startIndex: 12,
226 endIndex: 12
227 }, this.getBraille_(input, input));
228 });
229
230
231 /**
232 * @export
233 */
234 TEST_F('CvoxBrailleUtilUnitTest', 'StateTemplate', function() {
235 this.loadHtml(
236 '<input id="1" type="checkbox" aria-label="Save">');
237
238 var checkbox = cvox.CursorSelection.fromNode($('1'));
239
240 this.assertBrailleEquals(
241 {text: 'Save ( )',
242 startIndex: 0,
243 endIndex: 1
244 }, this.getBraille_(checkbox, checkbox));
245
246 $('1').checked = true;
247
248 this.assertBrailleEquals(
249 {text: 'Save (x)',
250 startIndex: 0,
251 endIndex: 1
252 }, this.getBraille_(checkbox, checkbox));
253 });
254
255
256 /**
257 * @export
258 */
259 TEST_F('CvoxBrailleUtilUnitTest', 'AccessKey', function() {
260 this.loadHtml(
261 '<a href="http://www.google.com" id="1" accesskey="g">Google</a>');
262
263 var link = cvox.CursorSelection.fromNode($('1'));
264
265 this.assertBrailleEquals(
266 {text: 'Google lnk access key:g',
267 startIndex: 0,
268 endIndex: 1
269 }, this.getBraille_(link, link));
270 });
271
272
273 /**
274 * @export
275 */
276 TEST_F('CvoxBrailleUtilUnitTest', 'ContainerTemplate', function() {
277 this.loadHtml(
278 '<h1>' +
279 '<a id="1" href="#menu">Skip To Menu</a>' +
280 '</h1>'
281 );
282
283 var link = cvox.CursorSelection.fromNode($('1'));
284
285 var navBraille = this.getBraille_(
286 cvox.CursorSelection.fromBody(), link);
287 this.assertBrailleEquals(
288 {text: 'h1 Skip To Menu int lnk',
289 startIndex: 0,
290 endIndex: 1
291 }, navBraille);
292 });
293
294
295 /**
296 * @export
297 */
298 TEST_F('CvoxBrailleUtilUnitTest', 'LinkSpans', function() {
299 this.loadHtml('<p><a id="1" href="#1">Hello</a> from' +
300 ' <a id="2" href="//www.google.com/">ChromeVox</a>');
301 var link1 = $('1');
302 var link2 = $('2');
303 var navBraille = this.getBraille_(
304 cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(link1));
305 assertEquals('Hello int lnk from ChromeVox lnk',
306 navBraille.text.toString());
307 assertEquals(link1, navBraille.text.getSpan(0));
308 assertEquals(link1, navBraille.text.getSpan(12));
309 assertEquals('undefined', typeof navBraille.text.getSpan(13));
310 assertEquals('undefined', typeof navBraille.text.getSpan(18));
311 assertEquals(link2, navBraille.text.getSpan(19));
312 assertEquals(link2, navBraille.text.getSpan(31));
313 });
314
315
316 /**
317 * @export
318 */
319 TEST_F('CvoxBrailleUtilUnitTest', 'NestedElements', function() {
320 this.loadHtml('<h1 id="test-h1">Larry, ' +
321 '<a href="#batman" id="batman-link">Sergey</a> and Eric</h1>');
322 var h1 = $('test-h1');
323 var link = $('batman-link');
324 var navBraille = this.getBraille_(
325 cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(h1));
326 assertEquals('h1 Larry, Sergey int lnk and Eric',
327 navBraille.text.toString());
328 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(0));
329 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(5));
330 assertEquals(link, navBraille.text.getSpan(15));
331 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(30));
332 });
333
334
335 /**
336 * @export
337 */
338 TEST_F('CvoxBrailleUtilUnitTest', 'GetTemplatedOverride', function() {
339 assertEquals('Menu mnu',
340 cvox.BrailleUtil.getTemplated(null, null,
341 { 'name': 'Menu',
342 'roleMsg': 'aria_role_menu' }).
343 toString());
344 assertEquals('alrt: Watch out!',
345 cvox.BrailleUtil.getTemplated(null, null,
346 { 'name': 'Watch out!',
347 'roleMsg': 'aria_role_alert' }).
348 toString());
349 // Test all properties. role, if present, overrides roleMsg.
350 assertEquals('Name Value Role State',
351 cvox.BrailleUtil.getTemplated(null, null,
352 { 'name': 'Name',
353 'role': 'Role',
354 'roleMsg': 'excluded',
355 'value': 'Value',
356 'state': 'State'
357 }).toString());
358 });
359
360
361 /**
362 * @export
363 */
364 TEST_F('CvoxBrailleUtilUnitTest', 'CreateValue', function() {
365 var s;
366 var valueSpan;
367 var selectiponSpan;
368
369 // Value without a selection.
370 s = cvox.BrailleUtil.createValue('value');
371 assertEquals('value', s.toString());
372 assertUndefined(s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan));
373 valueSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSpan);
374 assertEquals(0, s.getSpanStart(valueSpan));
375 assertEquals(s.getLength(), s.getSpanEnd(valueSpan));
376
377 // Value with a carret at the start of the text.
378 s = cvox.BrailleUtil.createValue('value', 0);
379 selectionSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan);
380 assertEquals(0, s.getSpanStart(selectionSpan));
381 assertEquals(0, s.getSpanEnd(selectionSpan));
382
383 // Value with a carret inside the text.
384 s = cvox.BrailleUtil.createValue('value', 1);
385 selectionSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan);
386 assertEquals(1, s.getSpanStart(selectionSpan));
387 assertEquals(1, s.getSpanEnd(selectionSpan));
388
389 // Value with a carret at the end of the text.
390 s = cvox.BrailleUtil.createValue('value', 5);
391 selectionSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan);
392 assertEquals(5, s.getSpanStart(selectionSpan));
393 assertEquals(5, s.getSpanEnd(selectionSpan));
394
395 // All of the value selected selected with reversed start and end.
396 s = cvox.BrailleUtil.createValue('value', 5, 0);
397 selectionSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan);
398 assertEquals(0, s.getSpanStart(selectionSpan));
399 assertEquals(5, s.getSpanEnd(selectionSpan));
400 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698