braille_util_test.unitjs revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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. 6GEN_INCLUDE(['../testing/chromevox_unittest_base.js']); 7 8/** 9 * Test fixture. 10 * @constructor 11 * @extends {ChromeVoxUnitTestBase} 12 */ 13function CvoxBrailleUtilUnitTest() {} 14 15CvoxBrailleUtilUnitTest.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 67TEST_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 */ 114TEST_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 */ 149TEST_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 */ 178TEST_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 */ 203TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldSelection', function() { 204 this.loadHtml( 205 '<input id="1" type="text" value="strawberry">' 206 ); 207 208 var inputElem = $('1'); 209 inputElem.focus(); 210 var input = cvox.CursorSelection.fromNode(inputElem); 211 212 // Selection. 213 inputElem.selectionStart = 2; 214 inputElem.selectionEnd = 5; 215 this.assertBrailleEquals( 216 {text: ': strawberry edtxt', 217 startIndex: 4, 218 endIndex: 7 219 }, this.getBraille_(input, input)); 220 221 // Cursor at end. 222 inputElem.selectionStart = 10; 223 inputElem.selectionEnd = 10; 224 this.assertBrailleEquals( 225 {text: ': strawberry edtxt', 226 startIndex: 12, 227 endIndex: 12 228 }, this.getBraille_(input, input)); 229}); 230 231 232/** 233 * @export 234 */ 235TEST_F('CvoxBrailleUtilUnitTest', 'StateTemplate', function() { 236 this.loadHtml( 237 '<input id="1" type="checkbox" aria-label="Save">'); 238 239 var checkbox = cvox.CursorSelection.fromNode($('1')); 240 241 this.assertBrailleEquals( 242 {text: 'Save ( )', 243 startIndex: 0, 244 endIndex: 1 245 }, this.getBraille_(checkbox, checkbox)); 246 247 $('1').checked = true; 248 249 this.assertBrailleEquals( 250 {text: 'Save (x)', 251 startIndex: 0, 252 endIndex: 1 253 }, this.getBraille_(checkbox, checkbox)); 254}); 255 256 257/** 258 * @export 259 */ 260TEST_F('CvoxBrailleUtilUnitTest', 'AccessKey', function() { 261 this.loadHtml( 262 '<a href="http://www.google.com" id="1" accesskey="g">Google</a>'); 263 264 var link = cvox.CursorSelection.fromNode($('1')); 265 266 this.assertBrailleEquals( 267 {text: 'Google lnk access key:g', 268 startIndex: 0, 269 endIndex: 1 270 }, this.getBraille_(link, link)); 271}); 272 273 274/** 275 * @export 276 */ 277TEST_F('CvoxBrailleUtilUnitTest', 'ContainerTemplate', function() { 278 this.loadHtml( 279 '<h1>' + 280 '<a id="1" href="#menu">Skip To Menu</a>' + 281 '</h1>' 282 ); 283 284 var link = cvox.CursorSelection.fromNode($('1')); 285 286 var navBraille = this.getBraille_( 287 cvox.CursorSelection.fromBody(), link); 288 this.assertBrailleEquals( 289 {text: 'h1 Skip To Menu int lnk', 290 startIndex: 0, 291 endIndex: 1 292 }, navBraille); 293}); 294 295 296/** 297 * @export 298 */ 299TEST_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>'); 302 var link1 = $('1'); 303 var link2 = $('2'); 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)); 314}); 315 316 317/** 318 * @export 319 */ 320TEST_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)); 333}); 334 335 336/** 337 * @export 338 */ 339TEST_F('CvoxBrailleUtilUnitTest', 'GetTemplatedOverride', function() { 340 assertEquals('Menu mnu', 341 cvox.BrailleUtil.getTemplated(null, null, 342 { 'name': 'Menu', 343 'roleMsg': 'aria_role_menu' }). 344 toString()); 345 assertEquals('alrt: Watch out!', 346 cvox.BrailleUtil.getTemplated(null, null, 347 { 'name': 'Watch out!', 348 'roleMsg': 'aria_role_alert' }). 349 toString()); 350 // Test all properties. role, if present, overrides roleMsg. 351 assertEquals('Name Value Role State', 352 cvox.BrailleUtil.getTemplated(null, null, 353 { 'name': 'Name', 354 'role': 'Role', 355 'roleMsg': 'excluded', 356 'value': 'Value', 357 'state': 'State' 358 }).toString()); 359}); 360 361 362/** 363 * @export 364 */ 365TEST_F('CvoxBrailleUtilUnitTest', 'CreateValue', function() { 366 var s; 367 var valueSpan; 368 var selectiponSpan; 369 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)); 377 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)); 383 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)); 389 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)); 395 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)); 401}); 402