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.
5 // Include test fixture.
6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js']);
11 * @extends {ChromeVoxUnitTestBase}
13 function CvoxBrailleUtilUnitTest() {}
15 CvoxBrailleUtilUnitTest.prototype = {
16 __proto__: ChromeVoxUnitTestBase.prototype,
21 'cvox.CursorSelection',
22 'cvox.NavigationShifter',
28 cvox.ChromeVox.msgs = new cvox.TestMsgs();
32 * @param {!Node} expectedParent Expected parent node.
33 * @param {!Node} node Node to examine.
36 assertTextNodeChildOf_: function(expectedParent, node) {
37 assertEquals(Node.TEXT_NODE, node.nodeType);
38 assertEquals(expectedParent, node.parentNode);
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.
48 getBraille_: function(prevSel, sel) {
49 return (new cvox.NavigationShifter).getBraille(prevSel, sel);
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.
57 assertBrailleEquals: function(expected, actual) {
58 actual = new cvox.NavBraille({
59 text: actual.text.toString(),
60 startIndex: actual.startIndex,
61 endIndex: actual.endIndex
63 assertThat(actual, eqJSON(new cvox.NavBraille(expected)));
67 TEST_F('CvoxBrailleUtilUnitTest', 'BrailleName', function() {
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>' +
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',
83 cvox.CursorSelection.fromNode($('1').firstChild);
84 braille = this.getBraille_(one, one);
85 this.assertBrailleEquals(
86 {text: 'one lnk two lnk three lnk',
92 cvox.CursorSelection.fromNode($('2').firstChild);
93 braille = this.getBraille_(one, two);
94 this.assertBrailleEquals(
95 {text: 'one lnk two lnk three lnk',
101 cvox.CursorSelection.fromNode($('3').firstChild);
102 braille = this.getBraille_(two, three);
103 this.assertBrailleEquals(
104 {text: 'one lnk two lnk three lnk',
114 TEST_F('CvoxBrailleUtilUnitTest', 'NameTemplate', function() {
116 '<button id="1">Submit</button>' +
117 '<input id="2" type="text" aria-label="Search">'
120 var button = cvox.CursorSelection.fromNode($('1'));
122 this.assertBrailleEquals(
126 }, this.getBraille_(button, button));
128 var inputElement = $('2');
129 var input = cvox.CursorSelection.fromNode(inputElement);
131 // Note: the cursor appears where text would be typed.
132 this.assertBrailleEquals(
136 }, this.getBraille_(input, input));
137 inputElement.focus();
138 this.assertBrailleEquals(
142 }, this.getBraille_(input, input));
149 TEST_F('CvoxBrailleUtilUnitTest', 'TextField', function() {
151 '<input id="1" type="text" aria-label="Search" value="larry">'
154 var inputElement = $('1');
155 var input = cvox.CursorSelection.fromNode(inputElement);
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 ed',
163 }, this.getBraille_(input, input));
164 inputElement.focus();
165 inputElement.selectionStart = 0;
166 inputElement.selectionEnd = 5;
167 this.assertBrailleEquals(
168 {text: 'Search: larry ed',
171 }, this.getBraille_(input, input));
178 TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldEmpty', function() {
180 '<input id="1" type="text">'
183 var inputElement = $('1');
184 var input = cvox.CursorSelection.fromNode($('1'));
186 this.assertBrailleEquals(
190 }, this.getBraille_(input, input));
191 inputElement.focus();
192 this.assertBrailleEquals(
196 }, this.getBraille_(input, input));
203 TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldSelection', function() {
205 '<input id="1" type="text" value="strawberry">'
208 var inputElem = $('1');
210 var input = cvox.CursorSelection.fromNode(inputElem);
213 inputElem.selectionStart = 2;
214 inputElem.selectionEnd = 5;
215 this.assertBrailleEquals(
216 {text: ': strawberry ed',
219 }, this.getBraille_(input, input));
222 inputElem.selectionStart = 10;
223 inputElem.selectionEnd = 10;
224 this.assertBrailleEquals(
225 {text: ': strawberry ed',
228 }, this.getBraille_(input, input));
235 TEST_F('CvoxBrailleUtilUnitTest', 'StateTemplate', function() {
237 '<input id="1" type="checkbox" aria-label="Save">');
239 var checkbox = cvox.CursorSelection.fromNode($('1'));
241 this.assertBrailleEquals(
242 {text: 'Save chk ( )',
245 }, this.getBraille_(checkbox, checkbox));
247 $('1').checked = true;
249 this.assertBrailleEquals(
250 {text: 'Save chk (x)',
253 }, this.getBraille_(checkbox, checkbox));
260 TEST_F('CvoxBrailleUtilUnitTest', 'AccessKey', function() {
262 '<a href="http://www.google.com" id="1" accesskey="g">Google</a>');
264 var link = cvox.CursorSelection.fromNode($('1'));
266 this.assertBrailleEquals(
267 {text: 'Google lnk access key:g',
270 }, this.getBraille_(link, link));
277 TEST_F('CvoxBrailleUtilUnitTest', 'ContainerTemplate', function() {
280 '<a id="1" href="#menu">Skip To Menu</a>' +
284 var link = cvox.CursorSelection.fromNode($('1'));
286 var navBraille = this.getBraille_(
287 cvox.CursorSelection.fromBody(), link);
288 this.assertBrailleEquals(
289 {text: 'h1 Skip To Menu intlnk',
299 TEST_F('CvoxBrailleUtilUnitTest', 'LinkSpans', function() {
300 this.loadHtml('<p><a id="1" href="#1">Hello</a> from' +
301 ' <a id="2" href="//www.google.com/">ChromeVox</a>');
304 var navBraille = this.getBraille_(
305 cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(link1));
306 assertEquals('Hello intlnk from ChromeVox lnk',
307 navBraille.text.toString());
308 assertEquals(link1, navBraille.text.getSpan(0));
309 assertEquals(link1, navBraille.text.getSpan(11));
310 assertEquals('undefined', typeof navBraille.text.getSpan(12));
311 assertEquals('undefined', typeof navBraille.text.getSpan(17));
312 assertEquals(link2, navBraille.text.getSpan(18));
313 assertEquals(link2, navBraille.text.getSpan(30));
317 TEST_F('CvoxBrailleUtilUnitTest', 'VisitedLink', function() {
318 this.loadHtml('<p><a id="1" href="http://visited.link">Hello</a> there.');
320 var navBraille = this.getBraille_(
321 cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(link));
322 this.assertBrailleEquals({text: 'Hello lnk there.',
326 cvox.ChromeVox.visitedUrls[link.href] = true;
327 navBraille = this.getBraille_(
328 cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(link));
329 this.assertBrailleEquals({text: 'Hello vlnk there.',
339 TEST_F('CvoxBrailleUtilUnitTest', 'NestedElements', function() {
340 this.loadHtml('<h1 id="test-h1">Larry, ' +
341 '<a href="#batman" id="batman-link">Sergey</a> and Eric</h1>');
342 var h1 = $('test-h1');
343 var link = $('batman-link');
344 var navBraille = this.getBraille_(
345 cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(h1));
346 assertEquals('h1 Larry, Sergey intlnk and Eric',
347 navBraille.text.toString());
348 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(0));
349 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(5));
350 assertEquals(link, navBraille.text.getSpan(15));
351 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(30));
358 TEST_F('CvoxBrailleUtilUnitTest', 'GetTemplatedOverride', function() {
359 assertEquals('Menu mnu',
360 cvox.BrailleUtil.getTemplated(null, null,
362 'roleMsg': 'aria_role_menu' }).
364 assertEquals('alrt: Watch out!',
365 cvox.BrailleUtil.getTemplated(null, null,
366 { 'name': 'Watch out!',
367 'roleMsg': 'aria_role_alert' }).
369 // Test all properties. role, if present, overrides roleMsg.
370 assertEquals('Name Value Role State',
371 cvox.BrailleUtil.getTemplated(null, null,
374 'roleMsg': 'excluded',
384 TEST_F('CvoxBrailleUtilUnitTest', 'CreateValue', function() {
389 // Value without a selection.
390 s = cvox.BrailleUtil.createValue('value');
391 assertEquals('value', s.toString());
392 assertUndefined(s.getSpanInstanceOf(cvox.ValueSelectionSpan));
393 valueSpan = s.getSpanInstanceOf(cvox.ValueSpan);
394 assertEquals(0, s.getSpanStart(valueSpan));
395 assertEquals(s.getLength(), s.getSpanEnd(valueSpan));
397 // Value with a carret at the start of the text.
398 s = cvox.BrailleUtil.createValue('value', 0);
399 selectionSpan = s.getSpanInstanceOf(cvox.ValueSelectionSpan);
400 assertEquals(0, s.getSpanStart(selectionSpan));
401 assertEquals(0, s.getSpanEnd(selectionSpan));
403 // Value with a carret inside the text.
404 s = cvox.BrailleUtil.createValue('value', 1);
405 selectionSpan = s.getSpanInstanceOf(cvox.ValueSelectionSpan);
406 assertEquals(1, s.getSpanStart(selectionSpan));
407 assertEquals(1, s.getSpanEnd(selectionSpan));
409 // Value with a carret at the end of the text.
410 s = cvox.BrailleUtil.createValue('value', 5);
411 selectionSpan = s.getSpanInstanceOf(cvox.ValueSelectionSpan);
412 assertEquals(5, s.getSpanStart(selectionSpan));
413 assertEquals(5, s.getSpanEnd(selectionSpan));
415 // All of the value selected selected with reversed start and end.
416 s = cvox.BrailleUtil.createValue('value', 5, 0);
417 selectionSpan = s.getSpanInstanceOf(cvox.ValueSelectionSpan);
418 assertEquals(0, s.getSpanStart(selectionSpan));
419 assertEquals(5, s.getSpanEnd(selectionSpan));