| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Include test fixture. | 5 // Include test fixture. |
| 6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js', | 6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js', |
| 7 '../testing/assert_additions.js']); | 7 '../testing/assert_additions.js']); |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Test fixture. | 10 * Test fixture. |
| 11 * @constructor | 11 * @constructor |
| 12 * @extends {ChromeVoxUnitTestBase} | 12 * @extends {ChromeVoxUnitTestBase} |
| 13 */ | 13 */ |
| 14 function CvoxExpandingBrailleTranslatorUnitTest() {} | 14 function CvoxExpandingBrailleTranslatorUnitTest() {} |
| 15 | 15 |
| 16 CvoxExpandingBrailleTranslatorUnitTest.prototype = { | 16 CvoxExpandingBrailleTranslatorUnitTest.prototype = { |
| 17 __proto__: ChromeVoxUnitTestBase.prototype, | 17 __proto__: ChromeVoxUnitTestBase.prototype, |
| 18 | 18 |
| 19 /** @override */ | 19 /** @override */ |
| 20 closureModuleDeps: [ | 20 closureModuleDeps: [ |
| 21 'Spannable', | 21 'Spannable', |
| 22 'cvox.BrailleTextStyleSpan', |
| 22 'cvox.ExpandingBrailleTranslator', | 23 'cvox.ExpandingBrailleTranslator', |
| 23 'cvox.LibLouis', | 24 'cvox.LibLouis', |
| 24 'cvox.ValueSelectionSpan', | 25 'cvox.ValueSelectionSpan', |
| 25 'cvox.ValueSpan', | 26 'cvox.ValueSpan', |
| 26 ] | 27 ] |
| 27 }; | 28 }; |
| 28 | 29 |
| 29 /** | 30 /** |
| 30 * An implementation of {@link cvox.LibLouis.Translator} whose translation | 31 * An implementation of {@link cvox.LibLouis.Translator} whose translation |
| 31 * output is an array buffer of the same byte length as the input and where | 32 * output is an array buffer of the same byte length as the input and where |
| 32 * each byte is equal to the character code of {@code resultChar}. The | 33 * each byte is equal to the character code of {@code resultChar}. The |
| 33 * position mappings are one to one in both directions. | 34 * position mappings are one to one in both directions. |
| 34 * @param {string} resultChar A one character string used for each byte of the | 35 * @param {string} resultChar A one character string used for each byte of the |
| 35 * result. | 36 * result. |
| 36 * @constructor | 37 * @constructor |
| 37 * @extends {cvox.LibLouis.Translator} | 38 * @extends {cvox.LibLouis.Translator} |
| 38 */ | 39 */ |
| 39 function FakeTranslator(resultChar) { | 40 function FakeTranslator(resultChar, opt_styleMap) { |
| 40 /** @private {string} */ | 41 /** @private {string} */ |
| 41 this.resultChar_ = resultChar; | 42 this.resultChar_ = resultChar; |
| 43 /** @private {!Object} */ |
| 44 this.styleMap_ = opt_styleMap || {}; |
| 42 } | 45 } |
| 43 | 46 |
| 44 FakeTranslator.prototype = { | 47 FakeTranslator.prototype = { |
| 45 /** @Override */ | 48 /** @Override */ |
| 46 translate: function(text, callback) { | 49 translate: function(text, formTypeMap, callback) { |
| 47 var result = new Uint8Array(text.length); | 50 var result = new Uint8Array(text.length); |
| 48 var textToBraille = []; | 51 var textToBraille = []; |
| 49 var brailleToText = []; | 52 var brailleToText = []; |
| 50 for (var i = 0; i < text.length; ++i) { | 53 for (var i = 0; i < text.length; ++i) { |
| 51 result[i] = this.resultChar_.charCodeAt(0); | 54 var formType = this.styleMap_[formTypeMap[i]]; |
| 55 if (formType) |
| 56 formType = formType.charCodeAt(0); |
| 57 result[i] = formType || this.resultChar_.charCodeAt(0); |
| 52 textToBraille.push(i); | 58 textToBraille.push(i); |
| 53 brailleToText.push(i); | 59 brailleToText.push(i); |
| 54 } | 60 } |
| 55 callback(result.buffer, textToBraille, brailleToText); | 61 callback(result.buffer, textToBraille, brailleToText); |
| 56 } | 62 } |
| 57 }; | 63 }; |
| 58 | 64 |
| 59 /** | 65 /** |
| 60 * Asserts that a array buffer, viewed as an uint8 array, matches | 66 * Asserts that a array buffer, viewed as an uint8 array, matches |
| 61 * the contents of a string. The character code of each character of the | 67 * the contents of a string. The character code of each character of the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 72 } | 78 } |
| 73 } | 79 } |
| 74 | 80 |
| 75 TEST_F('CvoxExpandingBrailleTranslatorUnitTest', 'TranslationError', | 81 TEST_F('CvoxExpandingBrailleTranslatorUnitTest', 'TranslationError', |
| 76 function() { | 82 function() { |
| 77 var text = new Spannable('error ok', new cvox.ValueSpan()); | 83 var text = new Spannable('error ok', new cvox.ValueSpan()); |
| 78 text.setSpan(new cvox.ValueSelectionSpan, 0, 0); | 84 text.setSpan(new cvox.ValueSelectionSpan, 0, 0); |
| 79 var contractedTranslator = new FakeTranslator('c'); | 85 var contractedTranslator = new FakeTranslator('c'); |
| 80 // Translator that always results in an error. | 86 // Translator that always results in an error. |
| 81 var uncontractedTranslator = { | 87 var uncontractedTranslator = { |
| 82 translate: function(text, callback) { | 88 translate: function(text, formTypeMap, callback) { |
| 83 callback(null, null, null); | 89 callback(null, null, null); |
| 84 } | 90 } |
| 85 }; | 91 }; |
| 86 var translationResult = null; | 92 var translationResult = null; |
| 87 | 93 |
| 88 var expandingTranslator = new cvox.ExpandingBrailleTranslator( | 94 var expandingTranslator = new cvox.ExpandingBrailleTranslator( |
| 89 contractedTranslator, uncontractedTranslator); | 95 contractedTranslator, uncontractedTranslator); |
| 90 expandingTranslator.translate( | 96 expandingTranslator.translate( |
| 91 text, cvox.ExpandingBrailleTranslator.ExpansionType.SELECTION, | 97 text, cvox.ExpandingBrailleTranslator.ExpansionType.SELECTION, |
| 92 function(cells, textToBraille, brailleToText) { | 98 function(cells, textToBraille, brailleToText) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 var expectedTextToBraille = []; | 140 var expectedTextToBraille = []; |
| 135 var expectedBrailleToText = []; | 141 var expectedBrailleToText = []; |
| 136 for (var i = 0, pos = 0; i < text.length; ++i, ++pos) { | 142 for (var i = 0, pos = 0; i < text.length; ++i, ++pos) { |
| 137 if (i === extraCellsSpanPos) | 143 if (i === extraCellsSpanPos) |
| 138 ++pos; | 144 ++pos; |
| 139 expectedTextToBraille.push(pos); | 145 expectedTextToBraille.push(pos); |
| 140 expectedBrailleToText.push(i); | 146 expectedBrailleToText.push(i); |
| 141 } | 147 } |
| 142 if (extraCellsSpan) | 148 if (extraCellsSpan) |
| 143 expectedBrailleToText.splice(extraCellsSpanPos, 0, extraCellsSpanPos); | 149 expectedBrailleToText.splice(extraCellsSpanPos, 0, extraCellsSpanPos); |
| 144 | 150 |
| 145 expandingTranslator.translate( | 151 expandingTranslator.translate( |
| 146 text, valueExpansion, function(cells, textToBraille, brailleToText) { | 152 text, valueExpansion, function(cells, textToBraille, brailleToText) { |
| 147 assertArrayBufferMatches(expectedOutput, cells, name); | 153 assertArrayBufferMatches(expectedOutput, cells, name); |
| 148 assertEqualsJSON(expectedTextToBraille, textToBraille, name); | 154 assertEqualsJSON(expectedTextToBraille, textToBraille, name); |
| 149 assertEqualsJSON(expectedBrailleToText, brailleToText, name); | 155 assertEqualsJSON(expectedBrailleToText, brailleToText, name); |
| 150 }); | 156 }); |
| 151 } catch (e) { | 157 } catch (e) { |
| 152 console.error('Subtest ' + name + ' failed.'); | 158 console.error('Subtest ' + name + ' failed.'); |
| 153 throw e; | 159 throw e; |
| 154 } | 160 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 surroundedText, surroundedExpectedOutput); | 225 surroundedText, surroundedExpectedOutput); |
| 220 } | 226 } |
| 221 | 227 |
| 222 /** | 228 /** |
| 223 * Creates a spannable text with optional selection. | 229 * Creates a spannable text with optional selection. |
| 224 * @param {string} text The text. | 230 * @param {string} text The text. |
| 225 * @param {=opt_selectionStart} Selection start or caret position. No | 231 * @param {=opt_selectionStart} Selection start or caret position. No |
| 226 * selection is added if undefined. | 232 * selection is added if undefined. |
| 227 * @param {=opt_selectionEnd} Selection end if selection is not a caret. | 233 * @param {=opt_selectionEnd} Selection end if selection is not a caret. |
| 228 */ | 234 */ |
| 229 function createText(text, opt_selectionStart, opt_selectionEnd) { | 235 function createText(text, opt_selectionStart, opt_selectionEnd, opt_style) { |
| 230 var result = new Spannable(text); | 236 var result = new Spannable(text); |
| 231 | 237 |
| 232 result.setSpan(new cvox.ValueSpan, 0, text.length); | 238 result.setSpan(new cvox.ValueSpan, 0, text.length); |
| 233 if (goog.isDef(opt_selectionStart)) { | 239 if (goog.isDef(opt_selectionStart)) { |
| 234 result.setSpan( | 240 result.setSpan( |
| 235 new cvox.ValueSelectionSpan, | 241 new cvox.ValueSelectionSpan, |
| 236 opt_selectionStart, | 242 opt_selectionStart, |
| 237 goog.isDef(opt_selectionEnd) ? opt_selectionEnd : opt_selectionStart); | 243 goog.isDef(opt_selectionEnd) ? opt_selectionEnd : opt_selectionStart); |
| 238 } | 244 } |
| 245 |
| 246 if (goog.isDef(opt_style)) { |
| 247 result.setSpan( |
| 248 new cvox.BrailleTextStyleSpan(opt_style.formType), |
| 249 opt_style.start, |
| 250 opt_style.end); |
| 251 } |
| 239 return result; | 252 return result; |
| 240 } | 253 } |
| 241 | 254 |
| 242 | 255 |
| 243 var TEXT = 'Hello, world!'; | 256 var TEXT = 'Hello, world!'; |
| 244 | 257 |
| 245 TEST_F('CvoxExpandingBrailleTranslatorUnitTest', 'successfulTranslations', | 258 TEST_F('CvoxExpandingBrailleTranslatorUnitTest', 'successfulTranslations', |
| 246 function() { | 259 function() { |
| 247 /** | 260 /** |
| 248 * Dictionary of test strings, keyed on a descriptive name for the | 261 * Dictionary of test strings, keyed on a descriptive name for the |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 } | 305 } |
| 293 for (var i = 0, testCase; testCase = TESTDATA_WITH_SELECTION[i]; ++i) | 306 for (var i = 0, testCase; testCase = TESTDATA_WITH_SELECTION[i]; ++i) |
| 294 runTranslationTestVariants(testCase, true, expType.SELECTION, true); | 307 runTranslationTestVariants(testCase, true, expType.SELECTION, true); |
| 295 | 308 |
| 296 // Make sure that the logic above runs the tests, adjust when adding more | 309 // Make sure that the logic above runs the tests, adjust when adding more |
| 297 // test variants. | 310 // test variants. |
| 298 var totalExpectedTranslationTests = | 311 var totalExpectedTranslationTests = |
| 299 2 * (TESTDATA.length * 4 + TESTDATA_WITH_SELECTION.length); | 312 2 * (TESTDATA.length * 4 + TESTDATA_WITH_SELECTION.length); |
| 300 assertEquals(totalExpectedTranslationTests, totalRunTranslationTests); | 313 assertEquals(totalExpectedTranslationTests, totalRunTranslationTests); |
| 301 }); | 314 }); |
| 315 |
| 316 TEST_F('CvoxExpandingBrailleTranslatorUnitTest', 'StyleTranslations', |
| 317 function() { |
| 318 var formTypeMap = {}; |
| 319 formTypeMap[cvox.LibLouis.FormType.BOLD] = 'b'; |
| 320 formTypeMap[cvox.LibLouis.FormType.ITALIC] = 'i'; |
| 321 formTypeMap[cvox.LibLouis.FormType.UNDERLINE] = 'u'; |
| 322 var translator = new cvox.ExpandingBrailleTranslator( |
| 323 new FakeTranslator('c', formTypeMap), |
| 324 new FakeTranslator('u')); |
| 325 translator.translate(createText('a test of text', undefined, undefined, { |
| 326 start: 2, end: 6, formType: cvox.LibLouis.FormType.BOLD}), |
| 327 cvox.ExpandingBrailleTranslator.ExpansionType.NONE, |
| 328 function(cells) { |
| 329 assertArrayBufferMatches('ccbbbbcccccccc', cells); |
| 330 }); |
| 331 }); |
| OLD | NEW |