Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / chromeos / liblouis_nacl / test.js
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.
4
5 var pass = chrome.test.callbackPass;
6
7 var TABLE_NAME = 'en-us-comp8.ctb';
8 var CONTRACTED_TABLE_NAME = 'en-us-g2.ctb';
9 var TEXT = 'hello';
10 // Translation of the above string as a hexadecimal sequence of cells.
11 var CELLS = '1311070715';
12
13 var pendingCallback = null;
14 var pendingMessageId = -1;
15 var nextMessageId = 0;
16 var naclEmbed = null;
17
18 function loadLibrary(callback) {
19   var embed = document.createElement('embed');
20   embed.src = 'liblouis_nacl.nmf';
21   embed.type = 'application/x-nacl';
22   embed.width = 0;
23   embed.height = 0;
24   embed.setAttribute('tablesdir', 'tables');
25   embed.addEventListener('load', function() {
26     console.log("liblouis loaded");
27     naclEmbed = embed;
28     callback();
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);
39 }
40
41
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;
51 }
52
53
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']);
59     if (callback) {
60       callback(reply);
61     }
62   };
63 }
64
65
66 loadLibrary(function() {
67   chrome.test.runTests([
68   function testGetTranslator() {
69     rpc('CheckTable', { 'table_names': TABLE_NAME},
70        pass(expectSuccessReply()));
71   },
72
73   function testTranslateString() {
74     rpc('Translate', { 'table_names': TABLE_NAME, 'text': TEXT},
75         pass(expectSuccessReply(function(reply) {
76           chrome.test.assertEq(CELLS, reply['cells']);
77         })));
78   },
79
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']);
88         })));
89   },
90
91   function testBackTranslateString() {
92     rpc('BackTranslate', { 'table_names': TABLE_NAME, 'cells': CELLS},
93         pass(expectSuccessReply(function(reply) {
94           chrome.test.assertEq(TEXT, reply['text']);
95         })));
96   },
97
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']);
105         })));
106   },
107 ])});