Chromium Code Reviews| 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/fake_objects.js']); | 7 '../testing/fake_objects.js']); |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Test fixture. | 10 * Test fixture. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 ], | 25 ], |
| 26 | 26 |
| 27 /** @override */ | 27 /** @override */ |
| 28 setUp: function() { | 28 setUp: function() { |
| 29 /** @const */ | 29 /** @const */ |
| 30 this.NAV_BRAILLE = new cvox.NavBraille({ text: 'Hello, world!' }); | 30 this.NAV_BRAILLE = new cvox.NavBraille({ text: 'Hello, world!' }); |
| 31 this.EMPTY_NAV_BRAILLE = new cvox.NavBraille({ text: '' }); | 31 this.EMPTY_NAV_BRAILLE = new cvox.NavBraille({ text: '' }); |
| 32 this.translator = new FakeTranslator(); | 32 this.translator = new FakeTranslator(); |
| 33 this.translatorManager = new FakeTranslatorManager(); | 33 this.translatorManager = new FakeTranslatorManager(); |
| 34 /** @const */ | 34 /** @const */ |
| 35 this.DISPLAY_SIZE = 12; | 35 this.DISPLAY_ROW_SIZE = 1; |
| 36 this.DISPLAY_COLUMN_SIZE = 12; | |
| 36 }, | 37 }, |
| 37 | 38 |
| 38 addFakeApi: function() { | 39 addFakeApi: function() { |
| 39 chrome.brailleDisplayPrivate = {}; | 40 chrome.brailleDisplayPrivate = {}; |
| 40 chrome.brailleDisplayPrivate.getDisplayState = function(callback) { | 41 chrome.brailleDisplayPrivate.getDisplayState = function(callback) { |
| 41 callback(this.displayState); | 42 callback(this.displayState); |
| 42 }.bind(this); | 43 }.bind(this); |
| 43 this.writtenCells = []; | 44 this.writtenCells = []; |
| 44 chrome.brailleDisplayPrivate.writeDots = function(cells) { | 45 chrome.brailleDisplayPrivate.writeDots = function(cells) { |
| 45 this.writtenCells.push(cells); | 46 this.writtenCells.push(cells); |
| 46 }.bind(this); | 47 }.bind(this); |
| 47 chrome.brailleDisplayPrivate.onDisplayStateChanged = new FakeChromeEvent(); | 48 chrome.brailleDisplayPrivate.onDisplayStateChanged = new FakeChromeEvent(); |
| 48 chrome.brailleDisplayPrivate.onKeyEvent = new FakeChromeEvent(); | 49 chrome.brailleDisplayPrivate.onKeyEvent = new FakeChromeEvent(); |
| 49 }, | 50 }, |
| 50 | 51 |
| 51 displayAvailable: function() { | 52 displayAvailable: function() { |
| 52 this.displayState = {available: true, textRowCount: 1, textColumnCount: this .DISPLAY_SIZE}; | 53 this.displayState = {available: true, textRowCount: this.DISPLAY_ROW_SIZE, |
| 54 » textColumnCount: this.DISPLAY_COLUMN_SIZE}; | |
| 53 }, | 55 }, |
| 54 | 56 |
| 55 /** | 57 /** |
| 56 * Asserts display pan position and selection markers on the last written | 58 * Asserts display pan position and selection markers on the last written |
| 57 * display content and clears it. There must be exactly one | 59 * display content and clears it. There must be exactly one |
| 58 * set of cells written. | 60 * set of cells written. |
| 59 * @param {number} start expected pan position | 61 * @param {number} start expected pan position in the braille display |
| 60 * @param {number=} opt_selStart first cell (relative to buffer start that | 62 * @param {number=} opt_selStart first cell (relative to buffer start) that |
| 61 * should have a selection | 63 * should have a selection |
| 62 * @param {number=} opt_selEnd last cell that should have a selection. | 64 * @param {number=} opt_selEnd last cell that should have a selection. |
| 63 */ | 65 */ |
| 64 assertDisplayPositionAndClear: function(start, opt_selStart, opt_selEnd) { | 66 assertDisplayPositionAndClear: function(start, opt_selStart, opt_selEnd) { |
| 65 if (opt_selStart !== undefined && opt_selEnd === undefined) { | 67 if (opt_selStart !== undefined && opt_selEnd === undefined) { |
| 66 opt_selEnd = opt_selStart + 1; | 68 opt_selEnd = opt_selStart + 1; |
| 67 } | 69 } |
| 68 assertEquals(1, this.writtenCells.length); | 70 assertEquals(1, this.writtenCells.length); |
| 69 var a = new Uint8Array(this.writtenCells[0]); | 71 var a = new Uint8Array(this.writtenCells[0]); |
| 70 this.writtenCells.length = 0; | 72 this.writtenCells.length = 0; |
| 71 var firstCell = a[0] & ~cvox.BrailleDisplayManager.CURSOR_DOTS_; | 73 var firstCell = a[0] & ~cvox.BrailleDisplayManager.CURSOR_DOTS_; |
| 74 // We are asserting that start, which is an index, and firstCell, | |
| 75 // which is a value, are the same because the fakeTranslator generates | |
| 76 // the values of the braille cells based on indices. | |
| 77 console.log(a[2]); | |
|
David Tseng
2016/11/30 22:11:54
nit: remove debug logging.
ultimatedbz
2016/12/01 05:12:15
Done.
| |
| 72 assertEquals(start, firstCell, | 78 assertEquals(start, firstCell, |
| 73 'Start mismatch: ' + start + ' vs. ' + firstCell); | 79 ' Start mismatch: ' + start + ' vs. ' + firstCell); |
| 74 if (opt_selStart !== undefined) { | 80 if (opt_selStart !== undefined) { |
| 75 for (var i = opt_selStart; i < opt_selEnd; ++i) { | 81 for (var i = opt_selStart; i < opt_selEnd; ++i) { |
| 76 assertEquals(cvox.BrailleDisplayManager.CURSOR_DOTS_, | 82 assertEquals(cvox.BrailleDisplayManager.CURSOR_DOTS_, |
| 77 a[i] & cvox.BrailleDisplayManager.CURSOR_DOTS_, | 83 a[i] & cvox.BrailleDisplayManager.CURSOR_DOTS_, |
| 78 'Missing cursor marker at position ' + i); | 84 'Missing cursor marker at position ' + i); |
| 79 } | 85 } |
| 80 } | 86 } |
| 81 }, | 87 }, |
| 82 | 88 |
| 83 /** | 89 /** |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 101 } | 107 } |
| 102 }; | 108 }; |
| 103 | 109 |
| 104 /** @extends {cvox.ExpandingBrailleTranslator} */ | 110 /** @extends {cvox.ExpandingBrailleTranslator} */ |
| 105 function FakeTranslator() { | 111 function FakeTranslator() { |
| 106 } | 112 } |
| 107 | 113 |
| 108 FakeTranslator.prototype = { | 114 FakeTranslator.prototype = { |
| 109 /** | 115 /** |
| 110 * Does a translation where every other character becomes two cells. | 116 * Does a translation where every other character becomes two cells. |
| 117 * The translated text does not correspond with the actual content of | |
| 118 * the original text, but instead uses the indices. Each even index of the | |
| 119 * original text is mapped to one translated cell, while each odd index is | |
| 120 * mapped to two translated cells. | |
| 111 * @override | 121 * @override |
| 112 */ | 122 */ |
| 113 translate: function(spannable, expansionType, callback) { | 123 translate: function(spannable, expansionType, callback) { |
| 114 text = spannable.toString(); | 124 text = spannable.toString(); |
| 115 var buf = new Uint8Array(text.length + text.length / 2); | 125 var buf = new Uint8Array(text.length + text.length / 2); |
| 116 var textToBraille = []; | 126 var textToBraille = []; |
| 117 var brailleToText = []; | 127 var brailleToText = []; |
| 118 var idx = 0; | 128 var idx = 0; |
| 119 for (var i = 0; i < text.length; ++i) { | 129 for (var i = 0; i < text.length; ++i) { |
| 120 textToBraille.push(idx); | 130 textToBraille.push(idx); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 this.displayAvailable(); | 250 this.displayAvailable(); |
| 241 | 251 |
| 242 var manager = new cvox.BrailleDisplayManager(this.translatorManager); | 252 var manager = new cvox.BrailleDisplayManager(this.translatorManager); |
| 243 this.assertEmptyDisplayAndClear(); | 253 this.assertEmptyDisplayAndClear(); |
| 244 this.translatorManager.setTranslator(this.translator); | 254 this.translatorManager.setTranslator(this.translator); |
| 245 this.assertEmptyDisplayAndClear(); | 255 this.assertEmptyDisplayAndClear(); |
| 246 | 256 |
| 247 // Cursor at beginning of line. | 257 // Cursor at beginning of line. |
| 248 manager.setContent(createNavBrailleWithCursor(0, 0)); | 258 manager.setContent(createNavBrailleWithCursor(0, 0)); |
| 249 this.assertDisplayPositionAndClear(0, 0); | 259 this.assertDisplayPositionAndClear(0, 0); |
| 250 // Cursor at end of line. | 260 // When cursor at end of line. |
| 251 manager.setContent(createNavBrailleWithCursor(text.length, text.length)); | 261 manager.setContent(createNavBrailleWithCursor(text.length, text.length)); |
| 262 // The first braille cell should be the result of the equation below. | |
| 252 this.assertDisplayPositionAndClear( | 263 this.assertDisplayPositionAndClear( |
| 253 2 * this.DISPLAY_SIZE, | 264 Math.floor(translatedSize / this.DISPLAY_COLUMN_SIZE) * |
| 254 translatedSize % this.DISPLAY_SIZE); | 265 this.DISPLAY_COLUMN_SIZE, |
| 266 translatedSize % this.DISPLAY_COLUMN_SIZE); | |
| 255 // Selection from the end of what fits on the first display to the end of the | 267 // Selection from the end of what fits on the first display to the end of the |
| 256 // line. | 268 // line. |
| 257 manager.setContent(createNavBrailleWithCursor(7, text.length)); | 269 manager.setContent(createNavBrailleWithCursor(7, text.length)); |
| 258 this.assertDisplayPositionAndClear(0, 10, this.DISPLAY_SIZE); | 270 this.assertDisplayPositionAndClear(0, 10, this.DISPLAY_COLUMN_SIZE); |
| 259 // Selection on all of the line. | 271 // Selection on all of the line. |
| 260 manager.setContent(createNavBrailleWithCursor(0, text.length)); | 272 manager.setContent(createNavBrailleWithCursor(0, text.length)); |
| 261 this.assertDisplayPositionAndClear(0, 0, this.DISPLAY_SIZE); | 273 this.assertDisplayPositionAndClear(0, 0, this.DISPLAY_COLUMN_SIZE); |
| 262 }); | 274 }); |
| 263 | 275 |
| 264 /** | 276 /** |
| 265 * Tests that the grouping algorithm works with one text character that maps | 277 * Tests that the grouping algorithm works with one text character that maps |
| 266 * to one braille cell. | 278 * to one braille cell. |
| 267 */ | 279 */ |
| 268 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'BasicGroup', function() { | 280 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'BasicGroup', function() { |
| 269 var text = 'a'; | 281 var text = 'a'; |
| 270 var translated = '1'; | 282 var translated = '1'; |
| 271 var mapping = [0]; | 283 var mapping = [0]; |
| 272 var expected = [['a','1']]; | 284 var expected = [['a','1']]; |
| 285 var offsets = {brailleOffset: 0, textOffset: 0}; | |
| 273 | 286 |
| 274 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( | 287 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( |
| 275 translated, text, mapping); | 288 » » » » » » translated, text, mapping, offse ts); |
| 276 this.assertGroupsValid(groups, expected); | 289 this.assertGroupsValid(groups, expected); |
| 277 }); | 290 }); |
| 278 | 291 |
| 279 /** | 292 /** |
| 280 * Tests that the grouping algorithm works with one text character that maps | 293 * Tests that the grouping algorithm works with one text character that maps |
| 281 * to multiple braille cells. | 294 * to multiple braille cells. |
| 282 */ | 295 */ |
| 283 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'OneRtoManyB', function() { | 296 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'OneRtoManyB', function() { |
| 284 var text = 'A'; | 297 var text = 'A'; |
| 285 var translated = '11'; | 298 var translated = '11'; |
| 286 var mapping = [0,0]; | 299 var mapping = [0,0]; |
| 287 var expected = [['A', '11']]; | 300 var expected = [['A', '11']]; |
| 301 var offsets = {brailleOffset: 0, textOffset: 0}; | |
| 288 | 302 |
| 289 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( | 303 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( |
| 290 translated, text, mapping); | 304 » » » » » translated, text, mapping, offsets); |
| 291 this.assertGroupsValid(groups, expected); | 305 this.assertGroupsValid(groups, expected); |
| 292 }); | 306 }); |
| 293 | 307 |
| 294 /** | 308 /** |
| 295 * Tests that the grouping algorithm works with one braille cell that maps | 309 * Tests that the grouping algorithm works with one braille cell that maps |
| 296 * to multiple text characters. | 310 * to multiple text characters. |
| 297 */ | 311 */ |
| 298 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'OneBtoManyR', function() { | 312 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'OneBtoManyR', function() { |
| 299 var text = 'knowledge'; | 313 var text = 'knowledge'; |
| 300 var translated = '1'; | 314 var translated = '1'; |
| 301 var mapping = [0]; | 315 var mapping = [0]; |
| 302 var expected = [['knowledge', '1']]; | 316 var expected = [['knowledge', '1']]; |
| 317 var offsets = {brailleOffset: 0, textOffset: 0}; | |
| 303 | 318 |
| 304 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( | 319 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( |
| 305 translated, text, mapping); | 320 » » » » » translated, text, mapping, offsets) ; |
| 306 this.assertGroupsValid(groups, expected); | 321 this.assertGroupsValid(groups, expected); |
| 307 }); | 322 }); |
| 308 | 323 |
| 309 /** | 324 /** |
| 310 * Tests that the grouping algorithm works with one string that on both ends, | 325 * Tests that the grouping algorithm works with one string that on both ends, |
| 311 * have text characters that map to multiple braille cells. | 326 * have text characters that map to multiple braille cells. |
| 312 */ | 327 */ |
| 313 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'OneRtoManyB_BothEnds', function() { | 328 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'OneRtoManyB_BothEnds', function() { |
| 314 var text = 'AbbC'; | 329 var text = 'AbbC'; |
| 315 var translated = 'X122X3'; | 330 var translated = 'X122X3'; |
| 316 var mapping = [0,0,1,2,3,3]; | 331 var mapping = [0,0,1,2,3,3]; |
| 317 var expected = [['A', 'X1'], ['b', '2'],['b', '2'], ['C', 'X3']]; | 332 var expected = [['A', 'X1'], ['b', '2'],['b', '2'], ['C', 'X3']]; |
| 333 var offsets = {brailleOffset: 0, textOffset: 0}; | |
| 318 | 334 |
| 319 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( | 335 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( |
| 320 translated, text, mapping); | 336 » » » » » translated, text, mapping, offsets) ; |
| 321 this.assertGroupsValid(groups, expected); | 337 this.assertGroupsValid(groups, expected); |
| 322 }); | 338 }); |
| 323 | 339 |
| 324 /** | 340 /** |
| 325 * Tests that the grouping algorithm works with one string that on both ends, | 341 * Tests that the grouping algorithm works with one string that on both ends, |
| 326 * have braille cells that map to multiple text characters. | 342 * have braille cells that map to multiple text characters. |
| 327 */ | 343 */ |
| 328 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'OneBtoManyR_BothEnds', function() { | 344 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'OneBtoManyR_BothEnds', function() { |
| 329 var text = 'knowledgehappych'; | 345 var text = 'knowledgehappych'; |
| 330 var translated = '1234456'; | 346 var translated = '1234456'; |
| 331 var mapping = [0, 9, 10, 11, 12, 13, 14]; | 347 var mapping = [0, 9, 10, 11, 12, 13, 14]; |
| 332 var expected = [['knowledge', '1'], ['h', '2'], ['a', '3'], ['p', '4'], | 348 var expected = [['knowledge', '1'], ['h', '2'], ['a', '3'], ['p', '4'], |
| 333 ['p', '4'], ['y', '5'], ['ch', '6']]; | 349 ['p', '4'], ['y', '5'], ['ch', '6']]; |
| 350 var offsets = {brailleOffset: 0, textOffset: 0}; | |
| 334 | 351 |
| 335 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText(translated, | 352 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText(translated, |
| 336 text, mapping); | 353 » » » » » » » text, mapping, offsets) ; |
| 337 this.assertGroupsValid(groups, expected); | 354 this.assertGroupsValid(groups, expected); |
| 338 }); | 355 }); |
| 339 | 356 |
| 340 /** | 357 /** |
| 341 * Tests that the grouping algorithm works with one string that has both types | 358 * Tests that the grouping algorithm works with one string that has both types |
| 342 * of mapping. | 359 * of mapping. |
| 343 */ | 360 */ |
| 344 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'RandB_Random', function() { | 361 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'RandB_Random', function() { |
| 345 var text = 'knowledgeIsPower'; | 362 var text = 'knowledgeIsPower'; |
| 346 var translated = '1X23X45678'; | 363 var translated = '1X23X45678'; |
| 347 var mapping = [0, 9, 9, 10, 11, 11, 12, 13, 14, 15]; | 364 var mapping = [0, 9, 9, 10, 11, 11, 12, 13, 14, 15]; |
| 348 var expected = [['knowledge', '1'], ['I', 'X2'], ['s', '3'], ['P', 'X4'], | 365 var expected = [['knowledge', '1'], ['I', 'X2'], ['s', '3'], ['P', 'X4'], |
| 349 ['o', '5'], ['w', '6'], ['e', '7'], ['r', '8']]; | 366 ['o', '5'], ['w', '6'], ['e', '7'], ['r', '8']]; |
| 367 var offsets = {brailleOffset: 0, textOffset: 0}; | |
| 350 | 368 |
| 351 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( | 369 var groups = cvox.BrailleCaptionsBackground.groupBrailleAndText( |
| 352 translated, text, mapping); | 370 » » » » » translated, text, mapping, offsets) ; |
| 353 this.assertGroupsValid(groups, expected); | 371 this.assertGroupsValid(groups, expected); |
| 354 }); | 372 }); |
| OLD | NEW |