// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Include test fixture. GEN_INCLUDE(['../testing/chromevox_unittest_base.js']); /** * Test fixture. * @constructor * @extends {ChromeVoxUnitTestBase} */ function CvoxBrailleUtilUnitTest() {} CvoxBrailleUtilUnitTest.prototype = { __proto__: ChromeVoxUnitTestBase.prototype, /** @override */ closureModuleDeps: [ 'cvox.BrailleUtil', 'cvox.CursorSelection', 'cvox.NavigationShifter', 'cvox.TestMsgs', ], /** @override */ setUp: function() { cvox.ChromeVox.msgs = new cvox.TestMsgs(); }, /** * @param {!Node} expectedParent Expected parent node. * @param {!Node} node Node to examine. * @private */ assertTextNodeChildOf_: function(expectedParent, node) { assertEquals(Node.TEXT_NODE, node.nodeType); assertEquals(expectedParent, node.parentNode); }, /** * Helper to retrieve braille for testing. * @param {!cvox.CursorSelection} prevSel Previous selection. * @param {!cvox.CursorSelection} sel Current selection. * @return {!cvox.NavBraille} Resulting braille. * @private */ getBraille_: function(prevSel, sel) { return (new cvox.NavigationShifter).getBraille(prevSel, sel); }, /** * Asserts that two NavBraille objects are equal, ignoring spans. * @param {Object} expected Expected result, should have no spans. * @param {cvox.NavBraille} actual Actual result. */ assertBrailleEquals: function(expected, actual) { actual = new cvox.NavBraille({ text: actual.text.toString(), startIndex: actual.startIndex, endIndex: actual.endIndex }); assertThat(new cvox.NavBraille(expected), eqJSON(actual)); } }; TEST_F('CvoxBrailleUtilUnitTest', 'BrailleName', function() { this.loadHtml( '
'); var navbar = cvox.CursorSelection.fromNode($('navbar')); var braille = this.getBraille_(navbar, navbar); this.assertBrailleEquals( {text: 'one lnk two lnk three lnk', startIndex: 0, endIndex: 1 }, braille); var one = cvox.CursorSelection.fromNode($('1').firstChild); braille = this.getBraille_(one, one); this.assertBrailleEquals( {text: 'one lnk two lnk three lnk', startIndex: 0, endIndex: 1 }, braille); var two = cvox.CursorSelection.fromNode($('2').firstChild); braille = this.getBraille_(one, two); this.assertBrailleEquals( {text: 'one lnk two lnk three lnk', startIndex: 8, endIndex: 9 }, braille); var three = cvox.CursorSelection.fromNode($('3').firstChild); braille = this.getBraille_(two, three); this.assertBrailleEquals( {text: 'one lnk two lnk three lnk', startIndex: 16, endIndex: 17 }, braille); }); /** * @export */ TEST_F('CvoxBrailleUtilUnitTest', 'NameTemplate', function() { this.loadHtml( '' + '' ); var button = cvox.CursorSelection.fromNode($('1')); this.assertBrailleEquals( {text: '[Submit]', startIndex: 0, endIndex: 1 }, this.getBraille_(button, button)); var inputElement = $('2'); var input = cvox.CursorSelection.fromNode(inputElement); // Note: the cursor appears where text would be typed. this.assertBrailleEquals( {text: 'Search: edtxt', startIndex: 0, endIndex: 1 }, this.getBraille_(input, input)); inputElement.focus(); this.assertBrailleEquals( {text: 'Search: edtxt', startIndex: 8, endIndex: 8 }, this.getBraille_(input, input)); }); /** * @export */ TEST_F('CvoxBrailleUtilUnitTest', 'TextField', function() { this.loadHtml( '' ); var inputElement = $('1'); var input = cvox.CursorSelection.fromNode(inputElement); // Note: the cursor appears where text would be typed. // The cursor is at the beginning by default. this.assertBrailleEquals( {text: 'Search: larry edtxt', startIndex: 0, endIndex: 1 }, this.getBraille_(input, input)); inputElement.focus(); inputElement.selectionStart = 0; inputElement.selectionEnd = 5; this.assertBrailleEquals( {text: 'Search: larry edtxt', startIndex: 8, endIndex: 13 }, this.getBraille_(input, input)); }); /** * @export */ TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldEmpty', function() { this.loadHtml( '' ); var inputElement = $('1'); var input = cvox.CursorSelection.fromNode($('1')); this.assertBrailleEquals( {text: ': edtxt', startIndex: 0, endIndex: 1 }, this.getBraille_(input, input)); inputElement.focus(); this.assertBrailleEquals( {text: ': edtxt', startIndex: 2, endIndex: 2 }, this.getBraille_(input, input)); }); /** * @export */ TEST_F('CvoxBrailleUtilUnitTest', 'TextFieldSelection', function() { this.loadHtml( '' ); var inputElem = $('1'); inputElem.focus(); var input = cvox.CursorSelection.fromNode(inputElem); // Selection. inputElem.selectionStart = 2; inputElem.selectionEnd = 5; this.assertBrailleEquals( {text: ': strawberry edtxt', startIndex: 4, endIndex: 7 }, this.getBraille_(input, input)); // Cursor at end. inputElem.selectionStart = 10; inputElem.selectionEnd = 10; this.assertBrailleEquals( {text: ': strawberry edtxt', startIndex: 12, endIndex: 12 }, this.getBraille_(input, input)); }); /** * @export */ TEST_F('CvoxBrailleUtilUnitTest', 'StateTemplate', function() { this.loadHtml( ''); var checkbox = cvox.CursorSelection.fromNode($('1')); this.assertBrailleEquals( {text: 'Save ( )', startIndex: 0, endIndex: 1 }, this.getBraille_(checkbox, checkbox)); $('1').checked = true; this.assertBrailleEquals( {text: 'Save (x)', startIndex: 0, endIndex: 1 }, this.getBraille_(checkbox, checkbox)); }); /** * @export */ TEST_F('CvoxBrailleUtilUnitTest', 'AccessKey', function() { this.loadHtml( 'Google'); var link = cvox.CursorSelection.fromNode($('1')); this.assertBrailleEquals( {text: 'Google lnk access key:g', startIndex: 0, endIndex: 1 }, this.getBraille_(link, link)); }); /** * @export */ TEST_F('CvoxBrailleUtilUnitTest', 'ContainerTemplate', function() { this.loadHtml( 'Hello from' + ' ChromeVox'); var link1 = $('1'); var link2 = $('2'); var navBraille = this.getBraille_( cvox.CursorSelection.fromBody(), cvox.CursorSelection.fromNode(link1)); assertEquals('Hello int lnk from ChromeVox lnk', navBraille.text.toString()); assertEquals(link1, navBraille.text.getSpan(0)); assertEquals(link1, navBraille.text.getSpan(12)); assertEquals('undefined', typeof navBraille.text.getSpan(13)); assertEquals('undefined', typeof navBraille.text.getSpan(18)); assertEquals(link2, navBraille.text.getSpan(19)); assertEquals(link2, navBraille.text.getSpan(31)); }); /** * @export */ TEST_F('CvoxBrailleUtilUnitTest', 'NestedElements', function() { this.loadHtml('