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(new cvox.NavBraille(expected), eqJSON(actual));
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(
133 {text: 'Search: edtxt',
136 }, this.getBraille_(input, input));
137 inputElement.focus();
138 this.assertBrailleEquals(
139 {text: 'Search: edtxt',
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 edtxt',
163 }, this.getBraille_(input, input));
164 inputElement.focus();
165 inputElement.selectionStart = 0;
166 inputElement.selectionEnd = 5;
167 this.assertBrailleEquals(
168 {text: 'Search: larry edtxt',
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 edtxt',
219 }, this.getBraille_(input, input));
222 inputElem.selectionStart = 10;
223 inputElem.selectionEnd = 10;
224 this.assertBrailleEquals(
225 {text: ': strawberry edtxt',
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(
245 }, this.getBraille_(checkbox, checkbox));
247 $('1').checked = true;
249 this.assertBrailleEquals(
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 int lnk',
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 int lnk from ChromeVox lnk',
307 navBraille.text.toString());
308 assertEquals(link1, navBraille.text.getSpan(0));
309 assertEquals(link1, navBraille.text.getSpan(12));
310 assertEquals('undefined', typeof navBraille.text.getSpan(13));
311 assertEquals('undefined', typeof navBraille.text.getSpan(18));
312 assertEquals(link2, navBraille.text.getSpan(19));
313 assertEquals(link2, navBraille.text.getSpan(31));
320 TEST_F('CvoxBrailleUtilUnitTest', 'NestedElements', function() {
321 this.loadHtml('<h1 id="test-h1">Larry, ' +
322 '<a href="#batman" id="batman-link">Sergey</a> and Eric</h1>');
323 var h1 = $('test-h1');
324 var link = $('batman-link');
325 var navBraille = this.getBraille_(
326 cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(h1));
327 assertEquals('h1 Larry, Sergey int lnk and Eric',
328 navBraille.text.toString());
329 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(0));
330 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(5));
331 assertEquals(link, navBraille.text.getSpan(15));
332 this.assertTextNodeChildOf_(h1, navBraille.text.getSpan(30));
339 TEST_F('CvoxBrailleUtilUnitTest', 'GetTemplatedOverride', function() {
340 assertEquals('Menu mnu',
341 cvox.BrailleUtil.getTemplated(null, null,
343 'roleMsg': 'aria_role_menu' }).
345 assertEquals('alrt: Watch out!',
346 cvox.BrailleUtil.getTemplated(null, null,
347 { 'name': 'Watch out!',
348 'roleMsg': 'aria_role_alert' }).
350 // Test all properties. role, if present, overrides roleMsg.
351 assertEquals('Name Value Role State',
352 cvox.BrailleUtil.getTemplated(null, null,
355 'roleMsg': 'excluded',
365 TEST_F('CvoxBrailleUtilUnitTest', 'CreateValue', function() {
370 // Value without a selection.
371 s = cvox.BrailleUtil.createValue('value');
372 assertEquals('value', s.toString());
373 assertUndefined(s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan));
374 valueSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSpan);
375 assertEquals(0, s.getSpanStart(valueSpan));
376 assertEquals(s.getLength(), s.getSpanEnd(valueSpan));
378 // Value with a carret at the start of the text.
379 s = cvox.BrailleUtil.createValue('value', 0);
380 selectionSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan);
381 assertEquals(0, s.getSpanStart(selectionSpan));
382 assertEquals(0, s.getSpanEnd(selectionSpan));
384 // Value with a carret inside the text.
385 s = cvox.BrailleUtil.createValue('value', 1);
386 selectionSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan);
387 assertEquals(1, s.getSpanStart(selectionSpan));
388 assertEquals(1, s.getSpanEnd(selectionSpan));
390 // Value with a carret at the end of the text.
391 s = cvox.BrailleUtil.createValue('value', 5);
392 selectionSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan);
393 assertEquals(5, s.getSpanStart(selectionSpan));
394 assertEquals(5, s.getSpanEnd(selectionSpan));
396 // All of the value selected selected with reversed start and end.
397 s = cvox.BrailleUtil.createValue('value', 5, 0);
398 selectionSpan = s.getSpanInstanceOf(cvox.BrailleUtil.ValueSelectionSpan);
399 assertEquals(0, s.getSpanStart(selectionSpan));
400 assertEquals(5, s.getSpanEnd(selectionSpan));