1 // Copyright 2013 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 var pass = chrome.test.callbackPass;
7 var TABLE_NAME = 'en-us-comp8.ctb';
8 var CONTRACTED_TABLE_NAME = 'en-us-g2.ctb';
10 // Translation of the above string as a hexadecimal sequence of cells.
11 var CELLS = '1311070715';
13 var pendingCallback = null;
14 var pendingMessageId = -1;
15 var nextMessageId = 0;
18 function loadLibrary(callback) {
19 var embed = document.createElement('embed');
20 embed.src = 'liblouis_nacl.nmf';
21 embed.type = 'application/x-nacl';
24 embed.setAttribute('tablesdir', 'tables');
25 embed.addEventListener('load', function() {
26 console.log("liblouis loaded");
29 }, false /* useCapture */);
30 embed.addEventListener('error', function() {
31 chrome.test.fail('liblouis load error');
32 }, false /* useCapture */);
33 embed.addEventListener('message', function(e) {
34 var reply = JSON.parse(e.data);
35 console.log('Message from liblouis: ' + e.data);
36 pendingCallback(reply);
37 }, false /* useCapture */);
38 document.body.appendChild(embed);
42 function rpc(command, args, callback) {
43 var messageId = '' + nextMessageId++;
44 args['command'] = command;
45 args['message_id'] = messageId;
46 var json = JSON.stringify(args);
47 console.log('Message to liblouis: ' + json);
48 naclEmbed.postMessage(json);
49 pendingCallback = callback;
50 pendingMessageId = messageId;
54 function expectSuccessReply(callback) {
55 return function(reply) {
56 chrome.test.assertEq(pendingMessageId, reply['in_reply_to']);
57 chrome.test.assertTrue(reply['error'] === undefined);
58 chrome.test.assertTrue(reply['success']);
66 loadLibrary(function() {
67 chrome.test.runTests([
68 function testGetTranslator() {
69 rpc('CheckTable', { 'table_names': TABLE_NAME},
70 pass(expectSuccessReply()));
73 function testTranslateString() {
74 rpc('Translate', { 'table_names': TABLE_NAME, 'text': TEXT},
75 pass(expectSuccessReply(function(reply) {
76 chrome.test.assertEq(CELLS, reply['cells']);
80 // Regression test for the case where the translated result is more than
81 // the double size of the input. In this particular case, a single capital
82 // letter 'T' should be translated to 3 cells in US English grade 2
83 // braille (dots 56, 6, 2345).
84 function testTranslateGrade2SingleCapital() {
85 rpc('Translate', { 'table_names': CONTRACTED_TABLE_NAME, 'text': 'T'},
86 pass(expectSuccessReply(function(reply) {
87 chrome.test.assertEq('30201e', reply['cells']);
91 function testBackTranslateString() {
92 rpc('BackTranslate', { 'table_names': TABLE_NAME, 'cells': CELLS},
93 pass(expectSuccessReply(function(reply) {
94 chrome.test.assertEq(TEXT, reply['text']);
98 // Backtranslate a one-letter contraction that expands to a much larger
99 // string (k->knowledge).
100 function testBackTranslateContracted() {
101 rpc('BackTranslate', { 'table_names': CONTRACTED_TABLE_NAME,
102 'cells': '05'}, // dots 1 and 3
103 pass(expectSuccessReply(function(reply) {
104 chrome.test.assertEq('knowledge', reply['text']);