Commit c6484806 authored by plundblad's avatar plundblad Committed by Commit bot

Port braille related tests from ChromeVox upstream.

This adds braille tests that excercise functionality that run in the background page, with the exception of an integration test that tests the message passing between content scripts and the background page for braille.

BUG=371692

Review URL: https://codereview.chromium.org/484063003

Cr-Commit-Position: refs/heads/master@{#294160}
parent 46314365
......@@ -53,6 +53,7 @@
'<(js2gtest)',
'<(chromevox_test_deps_js_file)',
'testing/chromevox_unittest_base.js',
'testing/assert_additions.js',
],
'outputs': [
'<(INTERMEDIATE_DIR)/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT)-gen.cc',
......@@ -85,7 +86,8 @@
'<(mock_js)',
'<(test_api_js)',
'<(js2gtest)',
'testing/chromevox_unittest_base.js',
'testing/chromevox_e2e_test_base.js',
'testing/assert_additions.js',
],
'outputs': [
'<(INTERMEDIATE_DIR)/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT)-gen.cc',
......@@ -138,6 +140,11 @@
'common/page_selection_test.unitjs',
'common/selection_util_test.unitjs',
'common/spannable_test.unitjs',
'host/chrome/braille_display_manager_test.unitjs',
'host/chrome/braille_input_handler_test.unitjs',
'host/chrome/braille_integration_test.unitjs',
'host/chrome/braille_table_test.extjs',
'host/chrome/expanding_braille_translator_test.unitjs',
],
'conditions': [
['use_chromevox_next==1', {
......
// 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',
'../../testing/fake_objects.js']);
/**
* Test fixture.
* @constructor
* @extends {ChromeVoxUnitTestBase}
*/
function CvoxBrailleDisplayManagerUnitTest() {}
CvoxBrailleDisplayManagerUnitTest.prototype = {
__proto__: ChromeVoxUnitTestBase.prototype,
/** @override */
closureModuleDeps: [
'cvox.BrailleDisplayManager',
'cvox.BrailleInterface',
'cvox.LibLouis',
'cvox.NavBraille',
],
/** @override */
setUp: function() {
/** @const */
this.NAV_BRAILLE = new cvox.NavBraille({ text: 'Hello, world!' });
this.EMPTY_NAV_BRAILLE = new cvox.NavBraille({ text: '' });
this.translator = new FakeTranslator();
/** @const */
this.DISPLAY_SIZE = 12;
},
addFakeApi: function() {
chrome.brailleDisplayPrivate = {};
chrome.brailleDisplayPrivate.getDisplayState = function(callback) {
callback(this.displayState);
}.bind(this);
this.writtenCells = [];
chrome.brailleDisplayPrivate.writeDots = function(cells) {
this.writtenCells.push(cells);
}.bind(this);
chrome.brailleDisplayPrivate.onDisplayStateChanged = new FakeChromeEvent();
chrome.brailleDisplayPrivate.onKeyEvent = new FakeChromeEvent();
},
displayAvailable: function() {
this.displayState = {available: true, textCellCount: this.DISPLAY_SIZE};
},
/**
* Asserts display pan position and selection markers on the last written
* display content and clears it. There must be exactly one
* set of cells written.
* @param {number} start expected pan position
* @param {number=} opt_selStart first cell (relative to buffer start that
* should have a selection
* @param {number=} opt_selEnd last cell that should have a selection.
*/
assertDisplayPositionAndClear: function(start, opt_selStart, opt_selEnd) {
if (opt_selStart !== undefined && opt_selEnd === undefined) {
opt_selEnd = opt_selStart + 1;
}
assertEquals(1, this.writtenCells.length);
var a = new Uint8Array(this.writtenCells[0]);
this.writtenCells.length = 0;
assertEquals(start, a[0] & ~cvox.BrailleDisplayManager.CURSOR_DOTS_,
'Start mismatch: ' + start + ' vs. ' + a[0]);
if (opt_selStart !== undefined) {
for (var i = opt_selStart; i < opt_selEnd; ++i) {
assertEquals(cvox.BrailleDisplayManager.CURSOR_DOTS_,
a[i] & cvox.BrailleDisplayManager.CURSOR_DOTS_,
'Missing cursor marker at position ' + i);
}
}
},
/**
* Asserts that the last written display content is an empty buffer of
* of cells and clears the list of written cells.
* There must be only one buffer in the list.
*/
assertEmptyDisplayAndClear: function() {
assertEquals(1, this.writtenCells.length);
var content = this.writtenCells[0];
this.writtenCells.length = 0;
assertTrue(content instanceof ArrayBuffer);
assertTrue(content.byteLength == 0);
}
};
/** @extends {cvox.LibLouis.Translator} */
function FakeTranslator() {
}
FakeTranslator.prototype = {
/**
* Does a translation where every other character becomes two cells.
* @override
*/
translate: function(text, callback) {
var buf = new Uint8Array(text.length + text.length / 2);
var textToBraille = [];
var brailleToText = [];
var idx = 0;
for (var i = 0; i < text.length; ++i) {
textToBraille.push(idx);
brailleToText.push(i);
buf[idx] = idx;
idx++;
if ((i % 2) == 1) {
buf[idx] = idx;
idx++;
brailleToText.push(i);
}
}
callback(buf.buffer, textToBraille, brailleToText);
}
};
var chrome = {};
TEST_F('CvoxBrailleDisplayManagerUnitTest', 'NoApi', function() {
var manager = new cvox.BrailleDisplayManager();
manager.setContent(this.NAV_BRAILLE);
manager.setTranslator(this.translator);
manager.setContent(this.NAV_BRAILLE);
});
/**
* Test that we don't write to the display when the API is available, but
* the display is not.
*/
TEST_F('CvoxBrailleDisplayManagerUnitTest', 'NoDisplay', function() {
this.addFakeApi();
this.displayState = {available: false};
var manager = new cvox.BrailleDisplayManager();
manager.setContent(this.NAV_BRAILLE);
manager.setTranslator(this.translator);
manager.setContent(this.NAV_BRAILLE);
assertEquals(0, this.writtenCells.length);
});
/**
* Tests the typical sequence: setContent, setTranslator, setContent.
*/
TEST_F('CvoxBrailleDisplayManagerUnitTest', 'BasicSetContent', function() {
this.addFakeApi();
this.displayAvailable();
var manager = new cvox.BrailleDisplayManager();
this.assertEmptyDisplayAndClear();
manager.setContent(this.NAV_BRAILLE);
manager.setTranslator(this.translator);
this.assertDisplayPositionAndClear(0);
manager.setContent(this.NAV_BRAILLE);
this.assertDisplayPositionAndClear(0);
});
/**
* Tests that setting empty content clears the display.
*/
TEST_F('CvoxBrailleDisplayManagerUnitTest', 'SetEmptyContentWithTranslator',
function() {
this.addFakeApi();
this.displayAvailable();
var manager = new cvox.BrailleDisplayManager();
this.assertEmptyDisplayAndClear();
manager.setContent(this.NAV_BRAILLE);
manager.setTranslator(this.translator);
this.assertDisplayPositionAndClear(0);
manager.setContent(this.EMPTY_NAV_BRAILLE);
this.assertEmptyDisplayAndClear();
});
TEST_F('CvoxBrailleDisplayManagerUnitTest', 'CursorAndPanning', function() {
var text = 'This is a test string';
function createNavBrailleWithCursor(start, end) {
return new cvox.NavBraille({ text: text, startIndex: start,
endIndex: end});
}
var translatedSize = Math.floor(text.length + text.length / 2);
this.addFakeApi();
this.displayAvailable();
var manager = new cvox.BrailleDisplayManager();
this.assertEmptyDisplayAndClear();
manager.setTranslator(this.translator);
this.assertEmptyDisplayAndClear();
// Cursor at beginning of line.
manager.setContent(createNavBrailleWithCursor(0, 0));
this.assertDisplayPositionAndClear(0, 0);
// Cursor at end of line.
manager.setContent(createNavBrailleWithCursor(text.length, text.length));
this.assertDisplayPositionAndClear(
2 * this.DISPLAY_SIZE,
translatedSize % this.DISPLAY_SIZE);
// Selection from the end of what fits on the first display to the end of the
// line.
manager.setContent(createNavBrailleWithCursor(7, text.length));
this.assertDisplayPositionAndClear(0, 10, this.DISPLAY_SIZE);
// Selection on all of the line.
manager.setContent(createNavBrailleWithCursor(0, text.length));
this.assertDisplayPositionAndClear(0, 0, this.DISPLAY_SIZE);
});
// 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',
'../../testing/assert_additions.js']);
// Tests the communication between content script and background page for
// braille.
/**
* Test fixture.
* @constructor
* @extends {ChromeVoxUnitTestBase}
*/
function CvoxBrailleIntegrationUnitTest() {}
CvoxBrailleIntegrationUnitTest.prototype = {
__proto__: ChromeVoxUnitTestBase.prototype,
/** @override */
closureModuleDeps: [
'cvox.BrailleBackground',
'cvox.BrailleInputHandler',
'cvox.BrailleKeyCommand',
'cvox.BrailleUtil',
'cvox.ChromeBraille',
'cvox.ExpandingBrailleTranslator',
],
/** @override */
setUp: function() {
cvox.BrailleTable.getAll = function() {}
this.displayManager = new FakeDisplayManager();
this.inputHandler = new FakeInputHandler();
this.brailleBackground = new cvox.BrailleBackground(
this.displayManager, this.inputHandler);
cvox.ExtensionBridge = new FakeExtensionBridge(this.brailleBackground);
this.braille = new cvox.ChromeBraille();
this.braille.setCommandListener(function(command, content) {
this.lastCommand = command;
this.lastCommandContent = content;
}.bind(this));
this.lastCommand = null;
this.lastCommandContent = null;
// Create convenience objects used in all tests.
this.command1 = {command: cvox.BrailleKeyCommand.PAN_LEFT};
this.content1 = cvox.NavBraille.fromText('text 1');
this.command2 = {command: cvox.BrailleKeyCommand.ROUTING};
this.content2 = cvox.NavBraille.fromText('text 2');
},
/** @Override */
tearDown: function() {
cvox.ExtensionBridge = null;
},
sendCommand: function(command, content) {
this.displayManager.commandListener(command, content);
}
};
function FakeExtensionBridge(brailleBackground) {
/** @private {Function} */
this.messageListener_ = null;
/** @private {cvox.BrailleBackground} */
this.brailleBackground_ = brailleBackground;
}
FakeExtensionBridge.prototype = {
uniqueId: function() { return 1; },
/** @param {Function} listener The listener. */
addMessageListener: function(listener) {
assertEquals(null, this.messageListener_);
this.messageListener_ = listener;
},
send: function(msg) {
if (msg['message'] == 'BRAILLE') {
assertNotEquals(null, this.messageListener_);
this.messageListener_(msg);
} else {
assertEquals('BRAILLE', msg['target']);
this.brailleBackground_.onBrailleMessage(msg);
}
}
};
/** @extends {cvox.BrailleDisplaymanager} */
function FakeDisplayManager() {
/** @type {Function} */
this.commandListener = null;
/** @type {cvox.NavBraille} */
this.content = null;
}
FakeDisplayManager.prototype = {
/** @Override */
setCommandListener: function(listener) {
this.commandListener = listener;
},
/** @Override */
setContent: function(content, expansionType) {
assertEquals(cvox.ExpandingBrailleTranslator.ExpansionType.SELECTION,
expansionType);
this.content = content;
}
};
/** @extends {cvox.BrailleInputHandler} */
function FakeInputHandler() {
}
FakeInputHandler.prototype = {
/** @Override */
init: function() {},
/** @Override */
onBrailleKeyEvent: function() {
return false;
},
/** @Override */
onDisplayContentChanged: function() {},
/** @Override */
getExpansionType: function() {
return cvox.ExpandingBrailleTranslator.ExpansionType.SELECTION;
}
};
TEST_F('CvoxBrailleIntegrationUnitTest', 'Write', function() {
this.braille.write(this.content1);
assertEqualsJSON(this.content1, this.displayManager.content);
});
TEST_F('CvoxBrailleIntegrationUnitTest', 'WriteWithSpans', function() {
var selectionSpan = new cvox.BrailleUtil.ValueSelectionSpan();
var valueSpan = new cvox.BrailleUtil.ValueSpan(20);
var toSend = cvox.NavBraille.fromText(
new cvox.Spannable('Hello', valueSpan));
toSend.text.setSpan(selectionSpan, 0, 0);
toSend.text.setSpan(document.body, 0, toSend.text.getLength());
var expected = cvox.NavBraille.fromText(
new cvox.Spannable(toSend.text.toString(), valueSpan));
expected.text.setSpan(selectionSpan, 0, 0);
this.braille.write(toSend);
assertEqualsJSON(expected, this.displayManager.content);
});
TEST_F('CvoxBrailleIntegrationUnitTest', 'CommandNoContent', function() {
// Commands are only delivered to the content script if the window has focus.
window.focus();
this.sendCommand(this.command1, null);
assertEqualsJSON(this.command1, this.lastCommand);
assertEquals(null, this.lastCommandContent);
});
TEST_F('CvoxBrailleIntegrationUnitTest', 'InterleavedWritesAndCommands',
function() {
window.focus();
this.braille.write(this.content1);
this.sendCommand(this.command1, this.displayManager.content);
assertEqualsJSON(this.command1, this.lastCommand);
assertEqualsJSON(this.content1, this.lastCommandContent);
var savedContent1 = this.displayManager.content;
this.braille.write(this.content2);
// Old content still on display.
this.sendCommand(this.command1, savedContent1);
assertEqualsJSON(this.command1, this.lastCommand);
assertEquals(null, this.lastCommandContent);
this.sendCommand(this.command2, this.displayManager.content);
assertEqualsJSON(this.command2, this.lastCommand);
assertEqualsJSON(this.content2, this.lastCommandContent);
});
TEST_F('CvoxBrailleIntegrationUnitTest', 'CommandAfterBackgroundWrite',
function() {
window.focus();
this.braille.write(this.content1);
this.sendCommand(this.command1, this.displayManager.content);
assertEqualsJSON(this.command1, this.lastCommand);
assertEqualsJSON(this.content1, this.lastCommandContent);
this.brailleBackground.write(this.content2);
assertEqualsJSON(this.content2, this.displayManager.content);
this.sendCommand(this.command2, this.displayManager.content);
assertEqualsJSON(this.command2, this.lastCommand);
assertEquals(null, this.lastCommandContent);
});
TEST_F('CvoxBrailleIntegrationUnitTest', 'CommandAfterOtherTabWrite',
function() {
window.focus();
// Ignore the listener of the braille from the second 'tab'.
cvox.ExtensionBridge.addMessageListener = function() {}
// Create another content script braille object, presumably from another
// tab.
var anotherBraille = new cvox.ChromeBraille();
this.braille.write(this.content1);
this.sendCommand(this.command1, this.displayManager.content);
// Now, this other braille gets a different unique id.
cvox.ExtensionBridge.uniqueId = function() { return 2; }
anotherBraille.write(this.content2);
this.sendCommand(this.command2, this.displayManager.content);
// The first 'tab' still gets the command, but since the second 'tab's\
// braille was on the display, it gets null content.
assertEqualsJSON(this.command2, this.lastCommand);
assertEquals(null, this.lastCommandContent);
});
// 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_e2e_test_base.js',
'../../testing/assert_additions.js']);
/**
* Test fixture for cvox.BrailleTable tests.
* This is an E2E test because there's no easy way to load a data file in
* a webui-style test.
* @constructor
* @extends {ChromeVoxE2ETest}
*/
function CvoxBrailleTableTest() {}
CvoxBrailleTableTest.prototype = {
__proto__: ChromeVoxE2ETest.prototype,
};
/**
* Tests that {@code getAll} can fetch and parse the tables file.
* NOTE: This will need to be adjusted when more tables are added.
*/
TEST_F('CvoxBrailleTableTest', 'testGetAll', function() {
cvox.BrailleTable.getAll(function(tables) {
assertEquals(60, tables.length);
assertNotNullNorUndefined(
cvox.BrailleTable.forId(tables, 'en-US-g1'),
'Can\'t find US English grade 1 table');
testDone();
});
});
/**
* Tests the getUncontracted function.
*/
TEST_F('CvoxBrailleTableTest', 'testGetUncontracted', function() {
cvox.BrailleTable.getAll(function(tables) {
function assertUncontracted(uncontractedId, idToCheck) {
var checkedTable = cvox.BrailleTable.forId(tables, idToCheck);
var uncontractedTable = cvox.BrailleTable.getUncontracted(
tables, checkedTable);
assertNotEquals(null, uncontractedTable);
assertEquals(uncontractedId, uncontractedTable.id);
}
assertUncontracted('en-US-comp8', 'en-US-g2');
assertUncontracted('en-US-comp8', 'en-US-comp8');
assertUncontracted('nb-comp8', 'nb-g0');
assertUncontracted('sv-comp8', 'sv-g1');
assertUncontracted('ar-g1', 'ar-g1');
assertUncontracted('de-comp8', 'de-CH-g2');
testDone();
});
});
// 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',
'../../testing/assert_additions.js']);
/**
* Test fixture.
* @constructor
* @extends {ChromeVoxUnitTestBase}
*/
function CvoxExpandingBrailleTranslatorUnitTest() {}
CvoxExpandingBrailleTranslatorUnitTest.prototype = {
__proto__: ChromeVoxUnitTestBase.prototype,
/** @override */
closureModuleDeps: [
'cvox.BrailleUtil',
'cvox.ExpandingBrailleTranslator',
'cvox.LibLouis',
'cvox.Spannable',
]
};
/**
* An implementation of {@link cvox.LibLouis.Translator} whose translation
* output is an array buffer of the same byte length as the input and where
* each byte is equal to the character code of {@code resultChar}. The
* position mappings are one to one in both directions.
* @param {string} resultChar A one character string used for each byte of the
* result.
* @constructor
* @extends {cvox.LibLouis.Translator}
*/
function FakeTranslator(resultChar) {
/** @private {string} */
this.resultChar_ = resultChar;
}
FakeTranslator.prototype = {
/** @Override */
translate: function(text, callback) {
var result = new Uint8Array(text.length);
var textToBraille = [];
var brailleToText = [];
for (var i = 0; i < text.length; ++i) {
result[i] = this.resultChar_.charCodeAt(0);
textToBraille.push(i);
brailleToText.push(i);
}
callback(result.buffer, textToBraille, brailleToText);
}
};
/**
* Asserts that a array buffer, viewed as an uint8 array, matches
* the contents of a string. The character code of each character of the
* string shall match the corresponding byte in the array buffer.
* @param {ArrayBuffer} actual Actual array buffer.
* @param {string} expected Array of expected bytes.
*/
function assertArrayBufferMatches(expected, actual) {
assertTrue(actual instanceof ArrayBuffer);
var a = new Uint8Array(actual);
assertEquals(expected.length, a.length);
for (var i = 0; i < a.length; ++i) {
assertEquals(expected.charCodeAt(i), a[i], 'Position ' + i);
}
}
TEST_F('CvoxExpandingBrailleTranslatorUnitTest', 'TranslationError',
function() {
var text = new cvox.Spannable('error ok', new cvox.BrailleUtil.ValueSpan());
text.setSpan(new cvox.BrailleUtil.ValueSelectionSpan, 0, 0);
var contractedTranslator = new FakeTranslator('c');
// Translator that always results in an error.
var uncontractedTranslator = {
translate: function(text, callback) {
callback(null, null, null);
}
};
var translationResult = null;
var expandingTranslator = new cvox.ExpandingBrailleTranslator(
contractedTranslator, uncontractedTranslator);
expandingTranslator.translate(
text, cvox.ExpandingBrailleTranslator.ExpansionType.SELECTION,
function(cells, textToBraille, brailleToText) {
// Expect the string ' ok' to be translated using the contracted
// translator. The preceding part isn't included because it resulted
// in a translation error.
assertArrayBufferMatches('ccc', cells);
assertEqualsJSON([0, 0, 0, 0, 0, 0, 1, 2], textToBraille);
assertEqualsJSON([5, 6, 7], brailleToText);
});
});
// Test for many variations of successful translations.
var totalRunTranslationTests = 0;
/**
* Performs the translation and checks the output.
* @param {string} name Name that describes the input for error messages.
* @param {boolean} contracted Whether to use a contracted translator
* in addition to the uncontracted one.
* @param {cvox.ExpandingBrailleTranslator.ExpansionType} valueExpansion
* Value expansion argument to pass to the translator.
* @param {string} text Input string.
* @param {string} expectedOutput Expected output as a string (see
* {@code TESTDATA} below for a description of the format).
*/
function doTranslationTest(name, contracted, valueExpansion, text,
expectedOutput) {
totalRunTranslationTests++;
var uncontractedTranslator = new FakeTranslator('u');
var expandingTranslator;
if (contracted) {
var contractedTranslator = new FakeTranslator('c');
expandingTranslator = new cvox.ExpandingBrailleTranslator(
contractedTranslator, uncontractedTranslator);
} else {
expandingTranslator = new cvox.ExpandingBrailleTranslator(
uncontractedTranslator);
}
var expectedMapping = [];
for (var i = 0; i < expectedOutput.length; ++i) {
expectedMapping[i] = i;
}
expandingTranslator.translate(
text, valueExpansion, function(cells, textToBraille, brailleToText) {
assertArrayBufferMatches(expectedOutput, cells, name);
assertEqualsJSON(expectedMapping, textToBraille, name);
assertEqualsJSON(expectedMapping, brailleToText, name);
});
};
/**
* Runs two tests, one with the given values and one with the given values
* where the text is surrounded by a typical name and role.
* @param {{name: string, input: string, contractedOutput: string}}
* testCase An entry of {@code TESTDATA}.
* @param {boolean} contracted Whether to use both uncontracted
* and contracted translators.
* @param {cvox.ExpandingBrailleTranslation.ExpansionType} valueExpansion
* What kind of value expansion to apply.
* @param {cvox.Spannable} text Input text.
* @param {string=} opt_expectedContractedOutput Expected output (see
* {@code TESTDATA}).
*/
function runTranslationTestVariants(testCase, contracted, valueExpansion) {
var expType = cvox.ExpandingBrailleTranslator.ExpansionType;
// Construct the full name.
var fullName = contracted ? 'Contracted_' : 'Uncontracted_';
fullName += 'Expansion' + valueExpansion + '_';
fullName += testCase.name;
// The expected output depends on the contraction mode and value expansion.
var outputChar = contracted ? 'c' : 'u';
var expectedOutput;
if (contracted && valueExpansion === expType.SELECTION) {
expectedOutput = testCase.contractedOutput;
} else if (contracted && valueExpansion === expType.ALL) {
expectedOutput = new Array(testCase.input.getLength() + 1).join('u');
} else {
expectedOutput =
new Array(testCase.input.getLength() + 1).join(outputChar);
}
doTranslationTest(fullName, contracted, valueExpansion, testCase.input,
expectedOutput);
// Run another test, with the value surrounded by some text.
var surroundedText = new cvox.Spannable('Name: ');
var surroundedExpectedOutput =
new Array('Name: '.length + 1).join(outputChar);
surroundedText.append(testCase.input);
surroundedExpectedOutput += expectedOutput;
if (testCase.input.getLength() > 0) {
surroundedText.append(' ');
surroundedExpectedOutput += outputChar;
}
surroundedText.append('edtxt');
surroundedExpectedOutput +=
new Array('edtxt'.length + 1).join(outputChar);
doTranslationTest(fullName + '_Surrounded', contracted, valueExpansion,
surroundedText, surroundedExpectedOutput);
}
/**
* Creates a spannable text with optional selection.
* @param {string} text The text.
* @param {=opt_selectionStart} Selection start or caret position. No
* selection is added if undefined.
* @param {=opt_selectionEnd} Selection end if selection is not a caret.
*/
function createText(text, opt_selectionStart, opt_selectionEnd) {
var result = new cvox.Spannable(text);
result.setSpan(
new cvox.BrailleUtil.ValueSpan, 0, text.length);
if (goog.isDef(opt_selectionStart)) {
result.setSpan(
new cvox.BrailleUtil.ValueSelectionSpan,
opt_selectionStart,
goog.isDef(opt_selectionEnd) ? opt_selectionEnd : opt_selectionStart);
}
return result;
}
var TEXT = 'Hello, world!';
TEST_F('CvoxExpandingBrailleTranslatorUnitTest', 'successfulTranslations',
function() {
/**
* Dictionary of test strings, keyed on a descriptive name for the
* test case. The value is an array of the input string to the translation
* and the expected output using a translator with both uncontracted
* and contracted underlying translators. The expected output is
* in the form of a string of the same length as the input, where an 'u'
* means that the uncontracted translator was used at this location and a
* 'c' means that the contracted translator was used.
*/
var TESTDATA = [
{ name: 'emptyText',
input: createText(''),
contractedOutput: '' },
{ name: 'emptyTextWithCaret',
input: createText('', 0),
contractedOutput: '' },
{ name: 'textWithNoSelection',
input: createText(TEXT),
contractedOutput: 'ccccccccccccc' },
{ name: 'textWithCaretAtStart',
input: createText(TEXT, 0),
contractedOutput: 'uuuuuuccccccc' },
{ name: 'textWithCaretAtEnd',
input: createText(TEXT, TEXT.length),
contractedOutput: 'cccccccuuuuuu' },
{ name: 'textWithCaretInWhitespace',
input: createText(TEXT, 6),
contractedOutput: 'uuuuuuucccccc' },
{ name: 'textWithSelectionEndInWhitespace',
input: createText(TEXT, 0, 7),
contractedOutput: 'uuuuuuucccccc' },
{ name: 'textWithSelectionInTwoWords',
input: createText(TEXT, 2, 9),
contractedOutput: 'uuuuuucuuuuuu' }
];
var expType = cvox.ExpandingBrailleTranslator.ExpansionType;
for (var i = 0, testCase; testCase = TESTDATA[i]; ++i) {
runTranslationTestVariants(testCase, false, expType.SELECTION);
runTranslationTestVariants(testCase, true, expType.NONE);
runTranslationTestVariants(testCase, true, expType.SELECTION);
runTranslationTestVariants(testCase, true, expType.ALL);
}
// Make sure that the logic above runs the tests, adjust when adding more
// tests.
assertEquals(64, totalRunTranslationTests);
});
......@@ -12,6 +12,18 @@ function assertUndefined(a) {
}
}
/**
* Asserts that the argument is neither null nor undefined.
* @param {object} obj The argument to check.
* @param {string=} opt_message Error message if the condition is not met.
*/
function assertNotNullNorUndefined(obj, opt_message) {
if (obj === undefined || obj === null) {
throw new Error('Can\'t be null or undefined: ' + (opt_message || '') +
'\n' + 'Actual: ' + a);
}
}
/**
* Asserts that a given function call throws an exception.
* @param {string} msg Message to print if exception not thrown.
......@@ -56,13 +68,15 @@ function assertEqualStringArrays(array1, array2) {
/**
* Asserts that two objects have the same JSON serialization.
* @param {Object} obj1 The expected object.
* @param {Object} obj2 The actual object.
* @param {Object} expected The expected object.
* @param {Object} actual The actual object.
* @param {string=} opt_message Message used for errors.
*/
function assertEqualsJSON(obj1, obj2) {
if (!eqJSON(obj1, obj2)) {
throw new Error('Expected ' + JSON.stringify(obj1) +
', got ' + JSON.stringify(obj2));
function assertEqualsJSON(expected, actual, opt_message) {
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
throw new Error((opt_message ? opt_message + '\n' : '') +
'Expected ' + JSON.stringify(expected) + '\n' +
'Got ' + JSON.stringify(actual));
}
}
......
// 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.
/**
* Fakes a Chrome event that supports one listener.
* @constructor
* @extends {ChromeEvent}
*/
function FakeChromeEvent() {
/**
* @private
* @type {Function}
*/
this.listener_ = null;
}
FakeChromeEvent.prototype = {
/**
* Fakes the corresponding call on a Chrome event. Sets the listener and
* fails the test if it is already set.
* @param {Function} listener The new listener.
*/
addListener: function(listener) {
this.assertNoListener();
this.listener_ = listener;
},
/**
* Gets the listener of the event, failing the test if there's none.
* @return {Function} The event's listener.
*/
getListener: function() {
assertNotEquals(null, this.listener_);
return this.listener_;
},
/**
* Asserts that this object doesn't have any listener added.
*/
assertNoListener: function() {
assertEquals(null, this.listener_);
}
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment