OLD | NEW |
| (Empty) |
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 TEXT = 'hello'; | |
9 // Translation of the above string as a hexadecimal sequence of cells. | |
10 var CELLS = '1311070715'; | |
11 | |
12 var pendingCallback = null; | |
13 var pendingMessageId = -1; | |
14 var nextMessageId = 0; | |
15 var naclEmbed = null; | |
16 | |
17 function loadLibrary(callback) { | |
18 var embed = document.createElement('embed'); | |
19 embed.src = 'liblouis_nacl.nmf'; | |
20 embed.type = 'application/x-nacl'; | |
21 embed.width = 0; | |
22 embed.height = 0; | |
23 embed.setAttribute('tablesdir', 'tables'); | |
24 embed.addEventListener('load', function() { | |
25 console.log("liblouis loaded"); | |
26 naclEmbed = embed; | |
27 callback(); | |
28 }, false /* useCapture */); | |
29 embed.addEventListener('error', function() { | |
30 chrome.test.fail('liblouis load error'); | |
31 }, false /* useCapture */); | |
32 embed.addEventListener('message', function(e) { | |
33 var reply = JSON.parse(e.data); | |
34 console.log('Message from liblouis: ' + e.data); | |
35 pendingCallback(reply); | |
36 }, false /* useCapture */); | |
37 document.body.appendChild(embed); | |
38 } | |
39 | |
40 | |
41 function rpc(command, args, callback) { | |
42 var messageId = '' + nextMessageId++; | |
43 args['command'] = command; | |
44 args['message_id'] = messageId; | |
45 var json = JSON.stringify(args) | |
46 console.log('Message to liblouis: ' + json); | |
47 naclEmbed.postMessage(json); | |
48 pendingCallback = callback; | |
49 pendingMessageId = messageId; | |
50 } | |
51 | |
52 | |
53 function expectSuccessReply(callback) { | |
54 return function(reply) { | |
55 chrome.test.assertEq(pendingMessageId, reply['in_reply_to']); | |
56 chrome.test.assertTrue(reply['error'] === undefined); | |
57 chrome.test.assertTrue(reply['success']); | |
58 if (callback) { | |
59 callback(reply); | |
60 } | |
61 }; | |
62 } | |
63 | |
64 | |
65 loadLibrary(function() { | |
66 chrome.test.runTests([ | |
67 function testGetTranslator() { | |
68 rpc('CheckTable', { 'table_name': TABLE_NAME}, | |
69 pass(expectSuccessReply())); | |
70 }, | |
71 | |
72 function testTranslateString() { | |
73 rpc('Translate', { 'table_name': TABLE_NAME, 'text': TEXT}, | |
74 pass(expectSuccessReply(function(reply) { | |
75 chrome.test.assertEq(CELLS, reply['cells']); | |
76 }))); | |
77 }, | |
78 | |
79 function testBackTranslateString() { | |
80 rpc('BackTranslate', { 'table_name': TABLE_NAME, 'cells': CELLS}, | |
81 pass(expectSuccessReply(function(reply) { | |
82 chrome.test.assertEq(TEXT, reply['text']); | |
83 }))); | |
84 }, | |
85 ])}); | |
OLD | NEW |