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.
5 // Include test fixture.
6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js',
7 '../testing/fake_objects.js']);
12 * @extends {ChromeVoxUnitTestBase}
14 function CvoxBrailleDisplayManagerUnitTest() {}
16 CvoxBrailleDisplayManagerUnitTest.prototype = {
17 __proto__: ChromeVoxUnitTestBase.prototype,
21 'cvox.BrailleDisplayManager',
22 'cvox.BrailleInterface',
30 this.NAV_BRAILLE = new cvox.NavBraille({ text: 'Hello, world!' });
31 this.EMPTY_NAV_BRAILLE = new cvox.NavBraille({ text: '' });
32 this.translator = new FakeTranslator();
33 this.translatorManager = new FakeTranslatorManager();
35 this.DISPLAY_SIZE = 12;
38 addFakeApi: function() {
39 chrome.brailleDisplayPrivate = {};
40 chrome.brailleDisplayPrivate.getDisplayState = function(callback) {
41 callback(this.displayState);
43 this.writtenCells = [];
44 chrome.brailleDisplayPrivate.writeDots = function(cells) {
45 this.writtenCells.push(cells);
47 chrome.brailleDisplayPrivate.onDisplayStateChanged = new FakeChromeEvent();
48 chrome.brailleDisplayPrivate.onKeyEvent = new FakeChromeEvent();
51 displayAvailable: function() {
52 this.displayState = {available: true, textCellCount: this.DISPLAY_SIZE};
56 * Asserts display pan position and selection markers on the last written
57 * display content and clears it. There must be exactly one
58 * set of cells written.
59 * @param {number} start expected pan position
60 * @param {number=} opt_selStart first cell (relative to buffer start that
61 * should have a selection
62 * @param {number=} opt_selEnd last cell that should have a selection.
64 assertDisplayPositionAndClear: function(start, opt_selStart, opt_selEnd) {
65 if (opt_selStart !== undefined && opt_selEnd === undefined) {
66 opt_selEnd = opt_selStart + 1;
68 assertEquals(1, this.writtenCells.length);
69 var a = new Uint8Array(this.writtenCells[0]);
70 this.writtenCells.length = 0;
71 var firstCell = a[0] & ~cvox.BrailleDisplayManager.CURSOR_DOTS_;
72 assertEquals(start, firstCell,
73 'Start mismatch: ' + start + ' vs. ' + firstCell);
74 if (opt_selStart !== undefined) {
75 for (var i = opt_selStart; i < opt_selEnd; ++i) {
76 assertEquals(cvox.BrailleDisplayManager.CURSOR_DOTS_,
77 a[i] & cvox.BrailleDisplayManager.CURSOR_DOTS_,
78 'Missing cursor marker at position ' + i);
84 * Asserts that the last written display content is an empty buffer of
85 * of cells and clears the list of written cells.
86 * There must be only one buffer in the list.
88 assertEmptyDisplayAndClear: function() {
89 assertEquals(1, this.writtenCells.length);
90 var content = this.writtenCells[0];
91 this.writtenCells.length = 0;
92 assertTrue(content instanceof ArrayBuffer);
93 assertTrue(content.byteLength == 0);
97 /** @extends {cvox.ExpandingBrailleTranslator} */
98 function FakeTranslator() {
101 FakeTranslator.prototype = {
103 * Does a translation where every other character becomes two cells.
106 translate: function(spannable, expansionType, callback) {
107 text = spannable.toString();
108 var buf = new Uint8Array(text.length + text.length / 2);
109 var textToBraille = [];
110 var brailleToText = [];
112 for (var i = 0; i < text.length; ++i) {
113 textToBraille.push(idx);
114 brailleToText.push(i);
120 brailleToText.push(i);
123 callback(buf.buffer, textToBraille, brailleToText);
127 /** @extends {cvox.BrailleTranslatorManager} */
128 function FakeTranslatorManager() {
131 FakeTranslatorManager.prototype = {
132 changeListener: null,
135 setTranslator: function(translator) {
136 this.translator = translator;
137 if (this.changeListener) {
138 this.changeListener();
142 addChangeListener: function(listener) {
143 assertEquals(null, this.changeListener);
144 this.changeListener = listener;
147 getExpandingTranslator: function() {
148 return this.translator;
154 // Fake chrome.storage API.
156 onChanged: new FakeChromeEvent(),
159 get: function(object, callback) {
160 callback({brailleWordWrap: false});
166 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'NoApi', function() {
167 var manager = new cvox.BrailleDisplayManager(this.translatorManager);
168 manager.setContent(this.NAV_BRAILLE);
169 this.translatorManager.setTranslator(this.translator);
170 manager.setContent(this.NAV_BRAILLE);
174 * Test that we don't write to the display when the API is available, but
175 * the display is not.
177 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'NoDisplay', function() {
179 this.displayState = {available: false};
181 var manager = new cvox.BrailleDisplayManager(this.translatorManager);
182 manager.setContent(this.NAV_BRAILLE);
183 this.translatorManager.setTranslator(this.translator);
184 manager.setContent(this.NAV_BRAILLE);
185 assertEquals(0, this.writtenCells.length);
190 * Tests the typical sequence: setContent, setTranslator, setContent.
192 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'BasicSetContent', function() {
194 this.displayAvailable();
195 var manager = new cvox.BrailleDisplayManager(this.translatorManager);
196 this.assertEmptyDisplayAndClear();
197 manager.setContent(this.NAV_BRAILLE);
198 this.assertEmptyDisplayAndClear();
199 this.translatorManager.setTranslator(this.translator);
200 this.assertDisplayPositionAndClear(0);
201 manager.setContent(this.NAV_BRAILLE);
202 this.assertDisplayPositionAndClear(0);
207 * Tests that setting empty content clears the display.
209 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'SetEmptyContentWithTranslator',
212 this.displayAvailable();
214 var manager = new cvox.BrailleDisplayManager(this.translatorManager);
215 this.assertEmptyDisplayAndClear();
216 manager.setContent(this.NAV_BRAILLE);
217 this.assertEmptyDisplayAndClear();
218 this.translatorManager.setTranslator(this.translator);
219 this.assertDisplayPositionAndClear(0);
220 manager.setContent(this.EMPTY_NAV_BRAILLE);
221 this.assertEmptyDisplayAndClear();
225 TEST_F('CvoxBrailleDisplayManagerUnitTest', 'CursorAndPanning', function() {
226 var text = 'This is a test string';
227 function createNavBrailleWithCursor(start, end) {
228 return new cvox.NavBraille({ text: text, startIndex: start,
232 var translatedSize = Math.floor(text.length + text.length / 2);
235 this.displayAvailable();
237 var manager = new cvox.BrailleDisplayManager(this.translatorManager);
238 this.assertEmptyDisplayAndClear();
239 this.translatorManager.setTranslator(this.translator);
240 this.assertEmptyDisplayAndClear();
242 // Cursor at beginning of line.
243 manager.setContent(createNavBrailleWithCursor(0, 0));
244 this.assertDisplayPositionAndClear(0, 0);
245 // Cursor at end of line.
246 manager.setContent(createNavBrailleWithCursor(text.length, text.length));
247 this.assertDisplayPositionAndClear(
248 2 * this.DISPLAY_SIZE,
249 translatedSize % this.DISPLAY_SIZE);
250 // Selection from the end of what fits on the first display to the end of the
252 manager.setContent(createNavBrailleWithCursor(7, text.length));
253 this.assertDisplayPositionAndClear(0, 10, this.DISPLAY_SIZE);
254 // Selection on all of the line.
255 manager.setContent(createNavBrailleWithCursor(0, text.length));
256 this.assertDisplayPositionAndClear(0, 0, this.DISPLAY_SIZE);