summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/braille/liblouis_test.extjs
blob: 38d1e4b92db833eab32d6e576e78d2ffce5a905c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// 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.

/**
 * @fileoverview Tests for the liblouis Native Client wrapper, as seen from
 *    the JavaScript interface.
 */

// Include test fixture.
GEN_INCLUDE(['../testing/chromevox_e2e_test_base.js',
             '../testing/assert_additions.js']);

/**
 * @constructor
 * @extends {ChromeVoxE2ETest}
 */
function CvoxLibLouisTest() {
  ChromeVoxE2ETest.call(this);
}

CvoxLibLouisTest.prototype = {
  __proto__: ChromeVoxE2ETest.prototype,

  createLiblouis: function() {
    return new cvox.LibLouis(
        chrome.extension.getURL('braille/liblouis_nacl.nmf'),
        chrome.extension.getURL('braille/tables'));
  },

  createAndAttachLiblouis: function() {
    var liblouis = this.createLiblouis();
    liblouis.attachToElement(document.body);
    return liblouis;
  },

  withTranslator: function(liblouis, tableNames, callback) {
    liblouis.getTranslator(tableNames, this.newCallback(callback));
  },
};

function assertEqualsUint8Array(expected, actual) {
  var as_array = [];
  var uint8array = new Uint8Array(actual);
  for (var i = 0; i < uint8array.length; ++i) {
    as_array[i] = uint8array[i];
  }
  assertEqualsJSON(expected, as_array);
}

TEST_F('CvoxLibLouisTest', 'checkAllTables', function() {
  var liblouis = this.createAndAttachLiblouis();
  cvox.BrailleTable.getAll(this.newCallback(function(tables) {
    var i = 0;
    var checkNextTable = function() {
      var table = tables[i++];
      if (table) {
        this.withTranslator(liblouis, table.fileNames, function(translator) {
          assertNotEquals(null, translator,
                             'Table ' + table + ' should be valid');
          checkNextTable();
        });
      }
    }.bind(this);
    checkNextTable();
  }.bind(this)));
});

TEST_F('CvoxLibLouisTest', 'testTranslateComputerBraille', function() {
  var liblouis = this.createAndAttachLiblouis();
  this.withTranslator(liblouis, 'en-us-comp8.ctb', function(translator) {
    translator.translate('Hello!', this.newCallback(
        function(cells, textToBraille, brailleToText) {
          assertEqualsUint8Array([0x53, 0x11, 0x07, 0x07, 0x15, 0x2e], cells);
          assertEqualsJSON([0, 1, 2, 3, 4, 5], textToBraille);
          assertEqualsJSON([0, 1, 2, 3, 4, 5], brailleToText);
        }));
  });
});

TEST_F('CvoxLibLouisTest', 'testBackTranslateComputerBraille', function() {
  var liblouis = this.createAndAttachLiblouis();
  this.withTranslator(liblouis, 'en-us-comp8.ctb', function(translator) {
    var cells = new Uint8Array([0x53, 0x11, 0x07, 0x07, 0x15, 0x2e]);
    translator.backTranslate(cells.buffer, this.newCallback(function(text) {
      assertEquals('Hello!', text);
    }));
  });
});

TEST_F('CvoxLibLouisTest', 'testTranslateGermanGrade2Braille', function() {
  var liblouis = this.createAndAttachLiblouis();
  // This is one of the moderately large tables.
  this.withTranslator(liblouis, 'de-de-g2.ctb', function(translator) {
    translator.translate('München', this.newCallback(
        function(cells, textToBraille, brailleToText) {
          assertEqualsUint8Array([0x0d, 0x33, 0x1d, 0x39, 0x09], cells);
          assertEqualsJSON([0, 1, 2, 3, 3, 4, 4], textToBraille);
          assertEqualsJSON([0, 1, 2, 3, 5], brailleToText);
        }));
  });
});

TEST_F('CvoxLibLouisTest', 'testBackTranslateGermanComputerBraille', function() {
  var liblouis = this.createAndAttachLiblouis();
  this.withTranslator(liblouis, 'de-de-comp8.ctb', function(translator) {
    var cells = new Uint8Array([0xb3]);
    translator.backTranslate(cells.buffer, this.newCallback(function(text) {
      assertEquals('ü', text);
    }));
  });
});

TEST_F('CvoxLibLouisTest', 'testBackTranslateEmptyCells', function() {
  var liblouis = this.createAndAttachLiblouis();
  this.withTranslator(liblouis, 'de-de-comp8.ctb', function(translator) {
       translator.backTranslate(
           new Uint8Array().buffer,
           this.newCallback(function(text) {
             assertNotEquals(null, text);
             assertEquals(0, text.length);
           }));
  });
});

TEST_F('CvoxLibLouisTest', 'testGetTranslatorBeforeAttach', function() {
  var liblouis = this.createLiblouis();
  assertFalse(liblouis.isAttached());
  this.withTranslator(liblouis, 'en-us-comp8.ctb', function(translator) {
    assertEquals(null, translator);
  });
});

TEST_F('CvoxLibLouisTest', 'testGetInvalidTranslator', function() {
  var liblouis = this.createAndAttachLiblouis();
  this.withTranslator(liblouis, 'nonexistant-table', function(translator) {
    assertEquals(null, translator);
  });
});

TEST_F('CvoxLibLouisTest', 'testTranslateAfterDetach', function() {
  var liblouis = this.createAndAttachLiblouis();
  this.withTranslator(liblouis, 'de-de-comp8.ctb', function(translator) {
    liblouis.detach();
    translator.translate('Hamburg', this.newCallback(
        function(cells, textToBraille, brailleToText) {
          assertEquals(null, cells);
          assertEquals(null, textToBraille);
          assertEquals(null, brailleToText);
        }));
  });
});

TEST_F('CvoxLibLouisTest', 'testDetachWithOutstandingCallbacks', function() {
  var liblouis = this.createAndAttachLiblouis();
  this.withTranslator(liblouis, 'de-de-comp8.ctb', function(translator) {
    var called = false;
    translator.translate('Berlin', this.newCallback(
        function(cells, textToBraille, brailleToText) {
          assertEquals(null, cells);
          assertEquals(null, textToBraille);
          assertEquals(null, brailleToText);
          called = true;
        }));
    assertFalse(called);
    liblouis.detach();
  });
});