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. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 * @param {boolean} contracted Whether to use a contracted translator | 109 * @param {boolean} contracted Whether to use a contracted translator |
110 * in addition to the uncontracted one. | 110 * in addition to the uncontracted one. |
111 * @param {cvox.ExpandingBrailleTranslator.ExpansionType} valueExpansion | 111 * @param {cvox.ExpandingBrailleTranslator.ExpansionType} valueExpansion |
112 * Value expansion argument to pass to the translator. | 112 * Value expansion argument to pass to the translator. |
113 * @param {string} text Input string. | 113 * @param {string} text Input string. |
114 * @param {string} expectedOutput Expected output as a string (see | 114 * @param {string} expectedOutput Expected output as a string (see |
115 * {@code TESTDATA} below for a description of the format). | 115 * {@code TESTDATA} below for a description of the format). |
116 */ | 116 */ |
117 function doTranslationTest(name, contracted, valueExpansion, text, | 117 function doTranslationTest(name, contracted, valueExpansion, text, |
118 expectedOutput) { | 118 expectedOutput) { |
119 totalRunTranslationTests++; | 119 try { |
120 var uncontractedTranslator = new FakeTranslator('u'); | 120 totalRunTranslationTests++; |
121 var expandingTranslator; | 121 var uncontractedTranslator = new FakeTranslator('u'); |
122 if (contracted) { | 122 var expandingTranslator; |
123 var contractedTranslator = new FakeTranslator('c'); | 123 if (contracted) { |
124 expandingTranslator = new cvox.ExpandingBrailleTranslator( | 124 var contractedTranslator = new FakeTranslator('c'); |
125 contractedTranslator, uncontractedTranslator); | 125 expandingTranslator = new cvox.ExpandingBrailleTranslator( |
126 } else { | 126 contractedTranslator, uncontractedTranslator); |
127 expandingTranslator = new cvox.ExpandingBrailleTranslator( | 127 } else { |
128 uncontractedTranslator); | 128 expandingTranslator = new cvox.ExpandingBrailleTranslator( |
| 129 uncontractedTranslator); |
| 130 } |
| 131 var extraCellsSpan = text.getSpanInstanceOf(cvox.ExtraCellsSpan); |
| 132 if (extraCellsSpan) |
| 133 var extraCellsSpanPos = text.getSpanStart(extraCellsSpan); |
| 134 var expectedTextToBraille = []; |
| 135 var expectedBrailleToText = []; |
| 136 for (var i = 0, pos = 0; i < text.getLength(); ++i, ++pos) { |
| 137 if (i === extraCellsSpanPos) |
| 138 ++pos; |
| 139 expectedTextToBraille.push(pos); |
| 140 expectedBrailleToText.push(i); |
| 141 } |
| 142 if (extraCellsSpan) |
| 143 expectedBrailleToText.splice(extraCellsSpanPos, 0, extraCellsSpanPos); |
| 144 |
| 145 expandingTranslator.translate( |
| 146 text, valueExpansion, function(cells, textToBraille, brailleToText) { |
| 147 assertArrayBufferMatches(expectedOutput, cells, name); |
| 148 assertEqualsJSON(expectedTextToBraille, textToBraille, name); |
| 149 assertEqualsJSON(expectedBrailleToText, brailleToText, name); |
| 150 }); |
| 151 } catch (e) { |
| 152 console.error('Subtest ' + name + ' failed.'); |
| 153 throw e; |
129 } | 154 } |
130 var expectedMapping = []; | |
131 for (var i = 0; i < expectedOutput.length; ++i) { | |
132 expectedMapping[i] = i; | |
133 } | |
134 | |
135 expandingTranslator.translate( | |
136 text, valueExpansion, function(cells, textToBraille, brailleToText) { | |
137 assertArrayBufferMatches(expectedOutput, cells, name); | |
138 assertEqualsJSON(expectedMapping, textToBraille, name); | |
139 assertEqualsJSON(expectedMapping, brailleToText, name); | |
140 }); | |
141 }; | 155 }; |
142 | 156 |
143 /** | 157 /** |
144 * Runs two tests, one with the given values and one with the given values | 158 * Runs two tests, one with the given values and one with the given values |
145 * where the text is surrounded by a typical name and role. | 159 * where the text is surrounded by a typical name and role. |
146 * @param {{name: string, input: string, contractedOutput: string}} | 160 * @param {{name: string, input: string, contractedOutput: string}} |
147 * testCase An entry of {@code TESTDATA}. | 161 * testCase An entry of {@code TESTDATA}. |
148 * @param {boolean} contracted Whether to use both uncontracted | 162 * @param {boolean} contracted Whether to use both uncontracted |
149 * and contracted translators. | 163 * and contracted translators. |
150 * @param {cvox.ExpandingBrailleTranslation.ExpansionType} valueExpansion | 164 * @param {cvox.ExpandingBrailleTranslation.ExpansionType} valueExpansion |
151 * What kind of value expansion to apply. | 165 * What kind of value expansion to apply. |
152 * @param {cvox.Spannable} text Input text. | 166 * @param {boolean} withExtraCells Whether to insert an extra cells span |
153 * @param {string=} opt_expectedContractedOutput Expected output (see | 167 * right before the selection in the input. |
154 * {@code TESTDATA}). | |
155 */ | 168 */ |
156 function runTranslationTestVariants(testCase, contracted, valueExpansion) { | 169 function runTranslationTestVariants(testCase, contracted, valueExpansion, |
| 170 withExtraCells) { |
157 var expType = cvox.ExpandingBrailleTranslator.ExpansionType; | 171 var expType = cvox.ExpandingBrailleTranslator.ExpansionType; |
158 // Construct the full name. | 172 // Construct the full name. |
159 var fullName = contracted ? 'Contracted_' : 'Uncontracted_'; | 173 var fullName = contracted ? 'Contracted_' : 'Uncontracted_'; |
160 fullName += 'Expansion' + valueExpansion + '_'; | 174 fullName += 'Expansion' + valueExpansion + '_'; |
| 175 if (withExtraCells) |
| 176 fullName += 'ExtraCells_'; |
161 fullName += testCase.name; | 177 fullName += testCase.name; |
| 178 var input = testCase.input; |
| 179 if (withExtraCells) { |
| 180 input = input.substring(0); // Shallow copy. |
| 181 var selectionStart = input.getSpanStart( |
| 182 input.getSpanInstanceOf(cvox.ValueSelectionSpan)); |
| 183 var extraCellsSpan = new cvox.ExtraCellsSpan(); |
| 184 extraCellsSpan.cells = new Uint8Array(['e'.charCodeAt(0)]).buffer; |
| 185 input.setSpan(extraCellsSpan, selectionStart, selectionStart); |
| 186 } |
162 // The expected output depends on the contraction mode and value expansion. | 187 // The expected output depends on the contraction mode and value expansion. |
163 var outputChar = contracted ? 'c' : 'u'; | 188 var outputChar = contracted ? 'c' : 'u'; |
164 var expectedOutput; | 189 var expectedOutput; |
165 if (contracted && valueExpansion === expType.SELECTION) { | 190 if (contracted && valueExpansion === expType.SELECTION) { |
166 expectedOutput = testCase.contractedOutput; | 191 expectedOutput = testCase.contractedOutput; |
167 } else if (contracted && valueExpansion === expType.ALL) { | 192 } else if (contracted && valueExpansion === expType.ALL) { |
168 expectedOutput = new Array(testCase.input.getLength() + 1).join('u'); | 193 expectedOutput = new Array(testCase.input.getLength() + 1).join('u'); |
169 } else { | 194 } else { |
170 expectedOutput = | 195 expectedOutput = |
171 new Array(testCase.input.getLength() + 1).join(outputChar); | 196 new Array(testCase.input.getLength() + 1).join(outputChar); |
172 } | 197 } |
173 doTranslationTest(fullName, contracted, valueExpansion, testCase.input, | 198 if (withExtraCells) { |
| 199 expectedOutput = expectedOutput.substring(0, selectionStart) + 'e' + |
| 200 expectedOutput.substring(selectionStart); |
| 201 } |
| 202 doTranslationTest(fullName, contracted, valueExpansion, input, |
174 expectedOutput); | 203 expectedOutput); |
175 | 204 |
176 // Run another test, with the value surrounded by some text. | 205 // Run another test, with the value surrounded by some text. |
177 var surroundedText = new cvox.Spannable('Name: '); | 206 var surroundedText = new cvox.Spannable('Name: '); |
178 var surroundedExpectedOutput = | 207 var surroundedExpectedOutput = |
179 new Array('Name: '.length + 1).join(outputChar); | 208 new Array('Name: '.length + 1).join(outputChar); |
180 surroundedText.append(testCase.input); | 209 surroundedText.append(input); |
181 surroundedExpectedOutput += expectedOutput; | 210 surroundedExpectedOutput += expectedOutput; |
182 if (testCase.input.getLength() > 0) { | 211 if (testCase.input.getLength() > 0) { |
183 surroundedText.append(' '); | 212 surroundedText.append(' '); |
184 surroundedExpectedOutput += outputChar; | 213 surroundedExpectedOutput += outputChar; |
185 } | 214 } |
186 surroundedText.append('edtxt'); | 215 surroundedText.append('edtxt'); |
187 surroundedExpectedOutput += | 216 surroundedExpectedOutput += |
188 new Array('edtxt'.length + 1).join(outputChar); | 217 new Array('edtxt'.length + 1).join(outputChar); |
189 doTranslationTest(fullName + '_Surrounded', contracted, valueExpansion, | 218 doTranslationTest(fullName + '_Surrounded', contracted, valueExpansion, |
190 surroundedText, surroundedExpectedOutput); | 219 surroundedText, surroundedExpectedOutput); |
191 } | 220 } |
192 | 221 |
193 /** | 222 /** |
194 * Creates a spannable text with optional selection. | 223 * Creates a spannable text with optional selection. |
195 * @param {string} text The text. | 224 * @param {string} text The text. |
196 * @param {=opt_selectionStart} Selection start or caret position. No | 225 * @param {=opt_selectionStart} Selection start or caret position. No |
197 * selection is added if undefined. | 226 * selection is added if undefined. |
198 * @param {=opt_selectionEnd} Selection end if selection is not a caret. | 227 * @param {=opt_selectionEnd} Selection end if selection is not a caret. |
199 */ | 228 */ |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 { name: 'textWithCaretInWhitespace', | 272 { name: 'textWithCaretInWhitespace', |
244 input: createText(TEXT, 6), | 273 input: createText(TEXT, 6), |
245 contractedOutput: 'uuuuuuucccccc' }, | 274 contractedOutput: 'uuuuuuucccccc' }, |
246 { name: 'textWithSelectionEndInWhitespace', | 275 { name: 'textWithSelectionEndInWhitespace', |
247 input: createText(TEXT, 0, 7), | 276 input: createText(TEXT, 0, 7), |
248 contractedOutput: 'uuuuuuucccccc' }, | 277 contractedOutput: 'uuuuuuucccccc' }, |
249 { name: 'textWithSelectionInTwoWords', | 278 { name: 'textWithSelectionInTwoWords', |
250 input: createText(TEXT, 2, 9), | 279 input: createText(TEXT, 2, 9), |
251 contractedOutput: 'uuuuuucuuuuuu' } | 280 contractedOutput: 'uuuuuucuuuuuu' } |
252 ]; | 281 ]; |
| 282 var TESTDATA_WITH_SELECTION = TESTDATA.filter(function(testCase) { |
| 283 return testCase.input.getSpanInstanceOf(cvox.ValueSelectionSpan); |
| 284 }); |
253 | 285 |
254 var expType = cvox.ExpandingBrailleTranslator.ExpansionType; | 286 var expType = cvox.ExpandingBrailleTranslator.ExpansionType; |
255 for (var i = 0, testCase; testCase = TESTDATA[i]; ++i) { | 287 for (var i = 0, testCase; testCase = TESTDATA[i]; ++i) { |
256 runTranslationTestVariants(testCase, false, expType.SELECTION); | 288 runTranslationTestVariants(testCase, false, expType.SELECTION, false); |
257 runTranslationTestVariants(testCase, true, expType.NONE); | 289 runTranslationTestVariants(testCase, true, expType.NONE, false); |
258 runTranslationTestVariants(testCase, true, expType.SELECTION); | 290 runTranslationTestVariants(testCase, true, expType.SELECTION, false); |
259 runTranslationTestVariants(testCase, true, expType.ALL); | 291 runTranslationTestVariants(testCase, true, expType.ALL, false); |
260 } | 292 } |
| 293 for (var i = 0, testCase; testCase = TESTDATA_WITH_SELECTION[i]; ++i) |
| 294 runTranslationTestVariants(testCase, true, expType.SELECTION, true); |
| 295 |
261 // Make sure that the logic above runs the tests, adjust when adding more | 296 // Make sure that the logic above runs the tests, adjust when adding more |
262 // tests. | 297 // test variants. |
263 assertEquals(64, totalRunTranslationTests); | 298 var totalExpectedTranslationTests = |
| 299 2 * (TESTDATA.length * 4 + TESTDATA_WITH_SELECTION.length); |
| 300 assertEquals(totalExpectedTranslationTests, totalRunTranslationTests); |
264 }); | 301 }); |
OLD | NEW |