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.
6 * @fileoverview Tests for the liblouis Native Client wrapper, as seen from
7 * the JavaScript interface.
10 // Include test fixture.
11 GEN_INCLUDE(['../testing/chromevox_e2e_test_base.js',
12 '../testing/assert_additions.js']);
16 * @extends {ChromeVoxE2ETest}
18 function CvoxLibLouisTest() {
19 ChromeVoxE2ETest.call(this);
22 CvoxLibLouisTest.prototype = {
23 __proto__: ChromeVoxE2ETest.prototype,
25 createLiblouis: function() {
26 return new cvox.LibLouis(
27 chrome.extension.getURL('braille/liblouis_nacl.nmf'),
28 chrome.extension.getURL('braille/tables'));
31 createAndAttachLiblouis: function() {
32 var liblouis = this.createLiblouis();
33 liblouis.attachToElement(document.body);
37 withTranslator: function(liblouis, tableNames, callback) {
38 liblouis.getTranslator(tableNames, this.newCallback(callback));
42 function assertEqualsUint8Array(expected, actual) {
44 var uint8array = new Uint8Array(actual);
45 for (var i = 0; i < uint8array.length; ++i) {
46 as_array[i] = uint8array[i];
48 assertEqualsJSON(expected, as_array);
51 TEST_F('CvoxLibLouisTest', 'checkAllTables', function() {
52 var liblouis = this.createAndAttachLiblouis();
53 cvox.BrailleTable.getAll(this.newCallback(function(tables) {
55 var checkNextTable = function() {
56 var table = tables[i++];
58 this.withTranslator(liblouis, table.fileNames, function(translator) {
59 assertNotEquals(null, translator,
60 'Table ' + table + ' should be valid');
69 TEST_F('CvoxLibLouisTest', 'testTranslateComputerBraille', function() {
70 var liblouis = this.createAndAttachLiblouis();
71 this.withTranslator(liblouis, 'en-us-comp8.ctb', function(translator) {
72 translator.translate('Hello!', this.newCallback(
73 function(cells, textToBraille, brailleToText) {
74 assertEqualsUint8Array([0x53, 0x11, 0x07, 0x07, 0x15, 0x2e], cells);
75 assertEqualsJSON([0, 1, 2, 3, 4, 5], textToBraille);
76 assertEqualsJSON([0, 1, 2, 3, 4, 5], brailleToText);
81 TEST_F('CvoxLibLouisTest', 'testBackTranslateComputerBraille', function() {
82 var liblouis = this.createAndAttachLiblouis();
83 this.withTranslator(liblouis, 'en-us-comp8.ctb', function(translator) {
84 var cells = new Uint8Array([0x53, 0x11, 0x07, 0x07, 0x15, 0x2e]);
85 translator.backTranslate(cells.buffer, this.newCallback(function(text) {
86 assertEquals('Hello!', text);
91 TEST_F('CvoxLibLouisTest', 'testTranslateGermanGrade2Braille', function() {
92 var liblouis = this.createAndAttachLiblouis();
93 // This is one of the moderately large tables.
94 this.withTranslator(liblouis, 'de-de-g2.ctb', function(translator) {
95 translator.translate('München', this.newCallback(
96 function(cells, textToBraille, brailleToText) {
97 assertEqualsUint8Array([0x0d, 0x33, 0x1d, 0x39, 0x09], cells);
98 assertEqualsJSON([0, 1, 2, 3, 3, 4, 4], textToBraille);
99 assertEqualsJSON([0, 1, 2, 3, 5], brailleToText);
104 TEST_F('CvoxLibLouisTest', 'testBackTranslateGermanComputerBraille', function() {
105 var liblouis = this.createAndAttachLiblouis();
106 this.withTranslator(liblouis, 'de-de-comp8.ctb', function(translator) {
107 var cells = new Uint8Array([0xb3]);
108 translator.backTranslate(cells.buffer, this.newCallback(function(text) {
109 assertEquals('ü', text);
114 TEST_F('CvoxLibLouisTest', 'testBackTranslateEmptyCells', function() {
115 var liblouis = this.createAndAttachLiblouis();
116 this.withTranslator(liblouis, 'de-de-comp8.ctb', function(translator) {
117 translator.backTranslate(
118 new Uint8Array().buffer,
119 this.newCallback(function(text) {
120 assertNotEquals(null, text);
121 assertEquals(0, text.length);
126 TEST_F('CvoxLibLouisTest', 'testGetTranslatorBeforeAttach', function() {
127 var liblouis = this.createLiblouis();
128 assertFalse(liblouis.isAttached());
129 this.withTranslator(liblouis, 'en-us-comp8.ctb', function(translator) {
130 assertEquals(null, translator);
134 TEST_F('CvoxLibLouisTest', 'testGetInvalidTranslator', function() {
135 var liblouis = this.createAndAttachLiblouis();
136 this.withTranslator(liblouis, 'nonexistant-table', function(translator) {
137 assertEquals(null, translator);
141 TEST_F('CvoxLibLouisTest', 'testTranslateAfterDetach', function() {
142 var liblouis = this.createAndAttachLiblouis();
143 this.withTranslator(liblouis, 'de-de-comp8.ctb', function(translator) {
145 translator.translate('Hamburg', this.newCallback(
146 function(cells, textToBraille, brailleToText) {
147 assertEquals(null, cells);
148 assertEquals(null, textToBraille);
149 assertEquals(null, brailleToText);
154 TEST_F('CvoxLibLouisTest', 'testDetachWithOutstandingCallbacks', function() {
155 var liblouis = this.createAndAttachLiblouis();
156 this.withTranslator(liblouis, 'de-de-comp8.ctb', function(translator) {
158 translator.translate('Berlin', this.newCallback(
159 function(cells, textToBraille, brailleToText) {
160 assertEquals(null, cells);
161 assertEquals(null, textToBraille);
162 assertEquals(null, brailleToText);