File: liblouis_test.js

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 5,013 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 The Chromium Authors
// 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 wasm wrapper, as seen from
 *    the JavaScript interface.
 */

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

ChromeVoxLibLouisTest = class extends ChromeVoxE2ETest {
  /** @override */
  async setUpDeferred() {
    await super.setUpDeferred();

    this.liblouis = BrailleTranslatorManager.instance.getLibLouisForTest();
    await new Promise((resolve) => this.waitForLibLouisLoad(resolve));
  }

  async backTranslate(tableNames, buffer) {
    const translator = await this.liblouis.getTranslator(tableNames);
    return new Promise(resolve => translator.backTranslate(buffer, resolve));
  }

  async translate(tableNames, text) {
    const translator = await this.liblouis.getTranslator(tableNames);
    return new Promise(
        resolve => translator.translate(text, [], (...args) => resolve(args)));
  }

  async waitForLibLouisLoad(resolve) {
    if (this.liblouis.isLoaded()) {
      resolve()
    } else {
      setTimeout(() => this.waitForLibLouisLoad(resolve), 500);
    }
  }
};

function assertEqualsUint8Array(expected, actual) {
  const asArray = Array.from(new Uint8Array(actual));
  assertEqualsJSON(expected, asArray);
}

AX_TEST_F(
    'ChromeVoxLibLouisTest', 'TranslateComputerBraille', async function() {
      const [cells, textToBraille, brailleToText] =
          await this.translate('en-us-comp8.ctb', 'Hello!');
      assertEqualsUint8Array([0x53, 0x11, 0x07, 0x07, 0x15, 0x2e], cells);
      assertEqualsJSON([0, 1, 2, 3, 4, 5], textToBraille);
      assertEqualsJSON([0, 1, 2, 3, 4, 5], brailleToText);
    });

AX_TEST_F('ChromeVoxLibLouisTest', 'MAYBE_CheckAllTables', async function() {
  const tables = await new Promise(resolve => BrailleTable.getAll(resolve));
  for (const table of tables) {
    const translator = await this.liblouis.getTranslator(table.fileNames);
    assertNotEquals(
        null, translator,
        'Table ' + JSON.stringify(table) + ' should be valid');
  }
}, `
#if defined(MEMORY_SANITIZER)
#define MAYBE_CheckAllTables DISABLED_CheckAllTables
#else
#define MAYBE_CheckAllTables CheckAllTables
#endif
`);

AX_TEST_F(
    'ChromeVoxLibLouisTest', 'BackTranslateComputerBraille', async function() {
      const cells = new Uint8Array([0x53, 0x11, 0x07, 0x07, 0x15, 0x2e]);
      const text = await this.backTranslate('en-us-comp8.ctb', cells.buffer);
      assertEquals('Hello!', text);
    });

AX_TEST_F(
    'ChromeVoxLibLouisTest', 'TranslateGermanGrade2Braille', async function() {
      // This is one of the moderately large tables.
      const [cells, textToBraille, brailleToText] =
          await this.translate('de-g2.ctb', 'München');
      assertEqualsUint8Array([0x0d, 0x33, 0x1d, 0x39, 0x09], cells);
      assertEqualsJSON([0, 1, 2, 3, 3, 4, 4], textToBraille);
      assertEqualsJSON([0, 1, 2, 3, 5], brailleToText);
    });

AX_TEST_F(
    'ChromeVoxLibLouisTest', 'TranslateSpaceIsNotDropped', async function() {
      const [cells, textToBraille, brailleToText] =
          await this.translate('en-ueb-g2.ctb', ' ');
      assertEqualsUint8Array([0x0], cells);
    });

AX_TEST_F(
    'ChromeVoxLibLouisTest', 'BackTranslateGermanComputerBraille',
    async function() {
      const cells = new Uint8Array([0xb3]);
      const text = await this.backTranslate('de-de-comp8.ctb', cells.buffer);
      assertEquals('ü', text);
    });

AX_TEST_F(
    'ChromeVoxLibLouisTest',
    'BackTranslateUSEnglishGrade2PreservesTrailingSpace', async function() {
      // A full braille cell (dots 1-6) is 'for' when backtranslated.
      const cells = new Uint8Array([0b111111, 0]);
      const text = await this.backTranslate('en-ueb-g2.ctb', cells.buffer);
      assertNotEquals(null, text);
      assertEquals('for ', text);
    });

AX_TEST_F('ChromeVoxLibLouisTest', 'BackTranslateEmptyCells', async function() {
  const text =
      await this.backTranslate('de-de-comp8.ctb', new Uint8Array().buffer);
  assertNotEquals(null, text);
  assertEquals(0, text.length);
});

AX_TEST_F('ChromeVoxLibLouisTest', 'GetInvalidTranslator', async function() {
  console.log('Expecting an error from liblouis');
  const translator = await this.liblouis.getTranslator('nonexistant-table');
  assertEquals(null, translator);
});

AX_TEST_F('ChromeVoxLibLouisTest', 'KeyEventStaticData', async function() {
  const [cells, textToBraille, brailleToText] = await this.translate(
      'en-us-comp8.ctb', 'abcdefghijklmnopqrstuvwxyz 0123456789');
  // A-Z.
  const view = new Uint8Array(cells);
  for (let i = 0; i < 26; i++) {
    assertEquals(
        String.fromCharCode(i + 65),
        BrailleKeyEvent.brailleDotsToStandardKeyCode[view[i]]);
  }

  // 0-9.
  for (let i = 27; i < 37; i++) {
    assertEquals(
        String.fromCharCode(i + 21),
        BrailleKeyEvent.brailleDotsToStandardKeyCode[view[i]]);
  }
});