diff --git a/apps/keyboard/test/unit/imes/latin/worker_test.js b/apps/keyboard/test/unit/imes/latin/worker_test.js index 4b3309a0e9da..f10953c3f6ec 100644 --- a/apps/keyboard/test/unit/imes/latin/worker_test.js +++ b/apps/keyboard/test/unit/imes/latin/worker_test.js @@ -1,9 +1,6 @@ /*global InputMethodDatabaseLoader, WordListConverter, InputMethods, dump */ 'use strict'; -require('/test/unit/setup_engine.js'); -require('/js/imes/latin/latin.js'); - require('/js/settings/word_list_converter.js'); require('/js/keyboard/input_method_database_loader.js'); @@ -11,439 +8,439 @@ suite('Latin worker', function() { var worker; var keymaps = {}; - suiteSetup(function() { - worker = new Worker('../../../../js/imes/latin/worker.js'); - - keymaps.qwerty = InputMethods.latin.generateNearbyKeyMap({ - 'keyboardWidth': 320, - 'keyboardHeight': 205, - 'keyArray': [{ - 'code': 113, - 'x': 0, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 119, - 'x': 32, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 101, - 'x': 64, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 114, - 'x': 96, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 116, - 'x': 128, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 121, - 'x': 160, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 117, - 'x': 192, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 105, - 'x': 224, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 111, - 'x': 256, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 112, - 'x': 288, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 97, - 'x': 16, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 115, - 'x': 48, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 100, - 'x': 80, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 102, - 'x': 112, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 103, - 'x': 144, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 104, - 'x': 176, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 106, - 'x': 208, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 107, - 'x': 240, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 108, - 'x': 272, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 20, - 'x': 0, - 'y': 131, - 'width': 48, - 'height': 43 - }, { - 'code': 122, - 'x': 48, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 120, - 'x': 80, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 99, - 'x': 112, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 118, - 'x': 144, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 98, - 'x': 176, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 110, - 'x': 208, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 109, - 'x': 240, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 8, - 'x': 272, - 'y': 131, - 'width': 48, - 'height': 43 - }, { - 'code': 65534, - 'x': 0, - 'y': 182, - 'width': 48, - 'height': 43 - }, { - 'code': 65533, - 'x': 48, - 'y': 182, - 'width': 32, - 'height': 43 - }, { - 'code': 32, - 'x': 80, - 'y': 182, - 'width': 144, - 'height': 43 - }, { - 'code': 46, - 'x': 224, - 'y': 182, - 'width': 32, - 'height': 43 - }, { - 'code': 13, - 'x': 256, - 'y': 182, - 'width': 64, - 'height': 43 - }], - 'keyWidth': 32, - 'keyHeight': 50 - }); - - keymaps.azerty = InputMethods.latin.generateNearbyKeyMap({ - 'keyboardWidth': 320, - 'keyboardHeight': 205, - 'keyArray': [{ - 'code': 97, - 'x': 0, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 122, - 'x': 32, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 101, - 'x': 64, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 114, - 'x': 96, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 116, - 'x': 128, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 121, - 'x': 160, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 117, - 'x': 192, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 105, - 'x': 224, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 111, - 'x': 256, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 112, - 'x': 288, - 'y': 29, - 'width': 32, - 'height': 43 - }, { - 'code': 113, - 'x': 0, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 115, - 'x': 32, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 100, - 'x': 64, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 102, - 'x': 96, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 103, - 'x': 128, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 104, - 'x': 160, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 106, - 'x': 192, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 107, - 'x': 224, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 108, - 'x': 256, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 109, - 'x': 288, - 'y': 80, - 'width': 32, - 'height': 43 - }, { - 'code': 20, - 'x': 0, - 'y': 131, - 'width': 48, - 'height': 43 - }, { - 'code': 119, - 'x': 48, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 120, - 'x': 80, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 99, - 'x': 112, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 118, - 'x': 144, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 98, - 'x': 176, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 110, - 'x': 208, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 39, - 'x': 240, - 'y': 131, - 'width': 32, - 'height': 43 - }, { - 'code': 8, - 'x': 272, - 'y': 131, - 'width': 48, - 'height': 43 - }, { - 'code': 18, - 'x': 0, - 'y': 182, - 'width': 64, - 'height': 43 - }, { - 'code': -3, - 'x': 64, - 'y': 182, - 'width': 32, - 'height': 43 - }, { - 'code': 32, - 'x': 96, - 'y': 182, - 'width': 128, - 'height': 43 - }, { - 'code': 46, - 'x': 224, - 'y': 182, - 'width': 32, - 'height': 43 - }, { - 'code': 13, - 'x': 256, - 'y': 182, - 'width': 64, - 'height': 43 - }], - 'keyWidth': 32, - 'keyHeight': 51 + suiteSetup(function(done) { + window.InputMethods = {}; + require('/js/imes/latin/latin.js', function() { + keymaps.qwerty = InputMethods.latin.generateNearbyKeyMap({ + 'keyboardWidth': 320, + 'keyboardHeight': 205, + 'keyArray': [{ + 'code': 113, + 'x': 0, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 119, + 'x': 32, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 101, + 'x': 64, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 114, + 'x': 96, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 116, + 'x': 128, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 121, + 'x': 160, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 117, + 'x': 192, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 105, + 'x': 224, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 111, + 'x': 256, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 112, + 'x': 288, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 97, + 'x': 16, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 115, + 'x': 48, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 100, + 'x': 80, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 102, + 'x': 112, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 103, + 'x': 144, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 104, + 'x': 176, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 106, + 'x': 208, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 107, + 'x': 240, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 108, + 'x': 272, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 20, + 'x': 0, + 'y': 131, + 'width': 48, + 'height': 43 + }, { + 'code': 122, + 'x': 48, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 120, + 'x': 80, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 99, + 'x': 112, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 118, + 'x': 144, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 98, + 'x': 176, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 110, + 'x': 208, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 109, + 'x': 240, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 8, + 'x': 272, + 'y': 131, + 'width': 48, + 'height': 43 + }, { + 'code': 65534, + 'x': 0, + 'y': 182, + 'width': 48, + 'height': 43 + }, { + 'code': 65533, + 'x': 48, + 'y': 182, + 'width': 32, + 'height': 43 + }, { + 'code': 32, + 'x': 80, + 'y': 182, + 'width': 144, + 'height': 43 + }, { + 'code': 46, + 'x': 224, + 'y': 182, + 'width': 32, + 'height': 43 + }, { + 'code': 13, + 'x': 256, + 'y': 182, + 'width': 64, + 'height': 43 + }], + 'keyWidth': 32, + 'keyHeight': 50 + }); + + keymaps.azerty = InputMethods.latin.generateNearbyKeyMap({ + 'keyboardWidth': 320, + 'keyboardHeight': 205, + 'keyArray': [{ + 'code': 97, + 'x': 0, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 122, + 'x': 32, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 101, + 'x': 64, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 114, + 'x': 96, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 116, + 'x': 128, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 121, + 'x': 160, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 117, + 'x': 192, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 105, + 'x': 224, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 111, + 'x': 256, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 112, + 'x': 288, + 'y': 29, + 'width': 32, + 'height': 43 + }, { + 'code': 113, + 'x': 0, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 115, + 'x': 32, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 100, + 'x': 64, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 102, + 'x': 96, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 103, + 'x': 128, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 104, + 'x': 160, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 106, + 'x': 192, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 107, + 'x': 224, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 108, + 'x': 256, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 109, + 'x': 288, + 'y': 80, + 'width': 32, + 'height': 43 + }, { + 'code': 20, + 'x': 0, + 'y': 131, + 'width': 48, + 'height': 43 + }, { + 'code': 119, + 'x': 48, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 120, + 'x': 80, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 99, + 'x': 112, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 118, + 'x': 144, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 98, + 'x': 176, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 110, + 'x': 208, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 39, + 'x': 240, + 'y': 131, + 'width': 32, + 'height': 43 + }, { + 'code': 8, + 'x': 272, + 'y': 131, + 'width': 48, + 'height': 43 + }, { + 'code': 18, + 'x': 0, + 'y': 182, + 'width': 64, + 'height': 43 + }, { + 'code': -3, + 'x': 64, + 'y': 182, + 'width': 32, + 'height': 43 + }, { + 'code': 32, + 'x': 96, + 'y': 182, + 'width': 128, + 'height': 43 + }, { + 'code': 46, + 'x': 224, + 'y': 182, + 'width': 32, + 'height': 43 + }, { + 'code': 13, + 'x': 256, + 'y': 182, + 'width': 64, + 'height': 43 + }], + 'keyWidth': 32, + 'keyHeight': 51 + }); + + done(); }); }); setup(function() { - worker.onmessage = sinon.stub(); + worker = new Worker('../../../../js/imes/latin/worker.js'); }); - test('Worker should throw if language doesnt exist', function(next) { - worker.postMessage({ cmd: 'setLanguage', args: ['no-existente'] }); - onWorkerMessage(function(data) { - - assert.equal(data.cmd, 'error'); - next(); - }); + teardown(function() { + worker.terminate(); + worker.onmessage = null; + worker = null; }); function onWorkerMessage(filter, callback) { @@ -451,22 +448,13 @@ suite('Latin worker', function() { callback = filter; filter = undefined; } - var occ = worker.onmessage.callCount; - var iv = setInterval(function() { - if (worker.onmessage.callCount > occ) { - var ev = worker.onmessage.args[worker.onmessage.callCount - 1][0]; - // console.log(ev.data); - - occ = worker.onmessage.callCount; - - if (filter !== undefined && ev.data.cmd !== filter) { - return; - } - - clearInterval(iv); - callback(ev.data); + worker.onmessage = function handleWorkerMessage(evt) { + if (filter !== undefined && evt.data.cmd !== filter) { + return; } - }, 20); + + callback(evt.data); + }; } function prediction(input, expected, next) { @@ -517,7 +505,7 @@ suite('Latin worker', function() { var successCount = 0; worker.onmessage = function(e) { if (e.data.cmd !== 'success') { - dump('worker.onmessage unexpected result ' + e.message + '\n'); + dump('worker.onmessage unexpected result ' + e.data.message + '\n'); } assert.equal(e.data.cmd, 'success'); @@ -527,14 +515,23 @@ suite('Latin worker', function() { successCount++; if (successCount === 4) { + worker.onmessage = null; next(); } } }; } + test('Worker should throw if no dictData is passed', function(done) { + worker.postMessage({ cmd: 'setLanguage', args: ['en_us', null] }); + onWorkerMessage('error', function(data) { + assert.equal(data.cmd, 'error'); + done(); + }); + }); + suite('en_us predictions', function() { - suiteSetup(function(next) { + setup(function(next) { setupLanguage('en_us', keymaps.qwerty, undefined, next); }); @@ -626,8 +623,8 @@ suite('Latin worker', function() { test('Should not suggest word in offensive list #1', function(next) { worker.postMessage({ cmd: 'predict', args: ['fuc'] }); - onWorkerMessage(function() { - var suggestions = worker.onmessage.args[0][0].data.suggestions; + onWorkerMessage(function(data) { + var suggestions = data.suggestions; assert.notEqual(suggestions[0][0], 'fuck'); assert.notEqual(suggestions[1][0], 'fuck'); @@ -640,8 +637,8 @@ suite('Latin worker', function() { test('Should not suggest word in offensive list #2', function(next) { worker.postMessage({ cmd: 'predict', args: ['peni'] }); - onWorkerMessage(function() { - var suggestions = worker.onmessage.args[0][0].data.suggestions; + onWorkerMessage(function(data) { + var suggestions = data.suggestions; assert.notEqual(suggestions[0][0], 'penis'); assert.notEqual(suggestions[1][0], 'penis'); @@ -718,14 +715,14 @@ suite('Latin worker', function() { // alternately. suite('predictions in conjuction with user dictionary', function() { var blob; - suiteSetup(function() { + setup(function() { blob = new WordListConverter( ['Mozilla', 'MozSpace', 'MozTrap', 'mozSettings', 'Mozillian']) .toBlob(); }); suite('with user dictionary blob at setupLanguage', function() { - suiteSetup(function(next) { + setup(function(next) { setupLanguage('en_gb', keymaps.qwerty, blob, next); }); @@ -741,7 +738,7 @@ suite('Latin worker', function() { }); suite('with user dictionary blob supplied later', function() { - suiteSetup(function(next) { + setup(function(next) { setupLanguage('en_us', keymaps.qwerty, undefined, function() { worker.addEventListener('message', function onMessage(e) { if ('success' === e.data.cmd && 'setUserDictionary' === e.data.fn) { @@ -769,7 +766,7 @@ suite('Latin worker', function() { suite('with user dictionary blob nullified later', function() { - suiteSetup(function(next) { + setup(function(next) { setupLanguage('en_gb', keymaps.qwerty, blob, function() { worker.addEventListener('message', function onMessage(e) { if ('success' === e.data.cmd && 'setUserDictionary' === e.data.fn) { @@ -797,7 +794,7 @@ suite('Latin worker', function() { }); suite('fr predictions', function() { - suiteSetup(function(next) { + setup(function(next) { setupLanguage('fr', keymaps.azerty, undefined, next); }); @@ -807,7 +804,7 @@ suite('Latin worker', function() { }); suite('validChars() is tolerant on small dicts', function() { - suiteSetup(function(next) { + setup(function(next) { var dictData = new WordListConverter(['Ápple']).toBlob(); worker.postMessage({