Commit 9510b484 authored by David Tseng's avatar David Tseng Committed by Commit Bot

Modernize c/b/r/c/a/braille_ime

Change-Id: I286fe466283e329c5e71fdc4454bd521a32ee2e8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2039810Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Commit-Queue: David Tseng <dtseng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#739089}
parent 9fa63c8d
...@@ -51,106 +51,104 @@ ...@@ -51,106 +51,104 @@
* through the normal event handling pipeline. * through the normal event handling pipeline.
*/ */
/** class BrailleIme {
* @constructor constructor() {
*/ /**
var BrailleIme = function() {}; * Whether to enable extra debug logging for the IME.
* @const {boolean}
BrailleIme.prototype = { * @private
/** */
* Whether to enable extra debug logging for the IME. this.DEBUG = false;
* @const {boolean}
* @private /**
*/ * ChromeVox extension ID.
DEBUG: false, * @const {string}
* @private
/** */
* ChromeVox extension ID. this.CHROMEVOX_EXTENSION_ID_ = 'mndnfokpggljbaajbnioimlmbfngpief';
* @const {string}
* @private /**
*/ * Name of the port used for communication with ChromeVox.
CHROMEVOX_EXTENSION_ID_: 'mndnfokpggljbaajbnioimlmbfngpief', * @const {string}
* @private
/** */
* Name of the port used for communication with ChromeVox. this.PORT_NAME = 'BrailleIme.Port';
* @const {string}
* @private /**
*/ * Identifier for the use standard keyboard option used
PORT_NAME: 'BrailleIme.Port', * in the menu and
* {@code localStorage}. This can be switched on to
/** * type braille using the standard keyboard, or off
* Identifier for the use standard keyboard option used in the menu and * (default) for the usual keyboard behaviour.
* {@code localStorage}. This can be switched on to type braille using the * @const {string}
* standard keyboard, or off (default) for the usual keyboard behaviour. */
* @const {string} this.USE_STANDARD_KEYBOARD_ID = 'useStandardKeyboard';
*/
USE_STANDARD_KEYBOARD_ID: 'useStandardKeyboard', /** @private {boolean} */
this.useStandardKeyboard_ = false;
// State related to the support for typing braille using a standrad
// (qwerty) keyboard. /**
* Braille dots for keys that are currently pressed.
/** @private {boolean} */ * @private {number}
useStandardKeyboard_: false, */
this.pressed_ = 0;
/**
* Braille dots for keys that are currently pressed.
* @private {number}
*/
pressed_: 0,
/**
* Dots that have been pressed at some point since {@code pressed_} was last
* {@code 0}.
* @private {number}
*/
accumulated_: 0,
/**
* Bit in {@code pressed_} and {@code accumulated_} that represent
* the space key.
* @const {number}
*/
SPACE: 0x100,
/** /**
* Maps key codes on a standard keyboard to the correspodning dots. * Dots that have been pressed at some point
* Keys on the 'home row' correspond to the keys on a Perkins-style keyboard. * since {@code pressed_} was last
* Note that the mapping below is arranged like the dots in a braille cell. * {@code 0}.
* Only 6 dot input is supported. * @private {number}
* @private */
* @const {Object<number>} this.accumulated_ = 0;
*/
CODE_TO_DOT_: {
'KeyF': 0x01,
'KeyJ': 0x08,
'KeyD': 0x02,
'KeyK': 0x10,
'KeyS': 0x04,
'KeyL': 0x20,
'Space': 0x100
},
/** /**
* The current engine ID as set by {@code onActivate}, or the empty string if * Bit in {@code pressed_} and {@code accumulated_} that
* the IME is not active. * represent the space key.
* @type {string} * @const {number}
* @private */
*/ this.SPACE = 0x100;
engineID_: '',
/**
* Maps key codes on a standard keyboard to the
* correspodning dots. Keys on the 'home row' correspond
* to the keys on a Perkins-style keyboard. Note that
* the mapping below is arranged like the dots in a
* braille cell. Only 6 dot input is supported.
* @private
* @const {Object<number>}
*/
this.CODE_TO_DOT_ = {
'KeyF': 0x01,
'KeyJ': 0x08,
'KeyD': 0x02,
'KeyK': 0x10,
'KeyS': 0x04,
'KeyL': 0x20,
'Space': 0x100
};
/**
* The current engine ID as set by {@code onActivate}, or the empty string
* if the IME is not active.
* @type {string}
* @private
*/
this.engineID_ = '';
/** /**
* The port used to communicate with ChromeVox. * The port used to communicate with ChromeVox.
* @type {Port} port_ * @type {Port} port_
* @private * @private
*/ */
port_: null, this.port_ = null;
/** /**
* Uncommitted text and context ID. * Uncommitted text and context ID.
* @type {?{contextID: number, text: string}} * @type {?{contextID: number, text: string}}
* @private * @private
*/ */
uncommitted_: null, this.uncommitted_ = null;
}
/** /**
* Registers event listeners in the chrome IME API. * Registers event listeners in the chrome IME API.
...@@ -168,7 +166,7 @@ BrailleIme.prototype = { ...@@ -168,7 +166,7 @@ BrailleIme.prototype = {
chrome.input.ime.onMenuItemActivated.addListener( chrome.input.ime.onMenuItemActivated.addListener(
this.onMenuItemActivated_.bind(this)); this.onMenuItemActivated_.bind(this));
this.connectChromeVox_(); this.connectChromeVox_();
}, }
/** /**
* Called by the IME framework when this IME is activated. * Called by the IME framework when this IME is activated.
...@@ -187,7 +185,7 @@ BrailleIme.prototype = { ...@@ -187,7 +185,7 @@ BrailleIme.prototype = {
this.pressed_ = 0; this.pressed_ = 0;
this.updateMenuItems_(); this.updateMenuItems_();
this.sendActiveState_(); this.sendActiveState_();
}, }
/** /**
* Called by the IME framework when this IME is deactivated. * Called by the IME framework when this IME is deactivated.
...@@ -198,7 +196,7 @@ BrailleIme.prototype = { ...@@ -198,7 +196,7 @@ BrailleIme.prototype = {
this.log_('onDectivated', engineID); this.log_('onDectivated', engineID);
this.engineID_ = ''; this.engineID_ = '';
this.sendActiveState_(); this.sendActiveState_();
}, }
/** /**
* Called by the IME framework when a text field receives focus. * Called by the IME framework when a text field receives focus.
...@@ -208,7 +206,7 @@ BrailleIme.prototype = { ...@@ -208,7 +206,7 @@ BrailleIme.prototype = {
onFocus_(context) { onFocus_(context) {
this.log_('onFocus', context); this.log_('onFocus', context);
this.sendInputContext_(context); this.sendInputContext_(context);
}, }
/** /**
* Called by the IME framework when a text field looses focus. * Called by the IME framework when a text field looses focus.
...@@ -218,7 +216,7 @@ BrailleIme.prototype = { ...@@ -218,7 +216,7 @@ BrailleIme.prototype = {
onBlur_(contextID) { onBlur_(contextID) {
this.log_('onBlur', contextID + ''); this.log_('onBlur', contextID + '');
this.sendInputContext_(null); this.sendInputContext_(null);
}, }
/** /**
* Called by the IME framework when the current input context is updated. * Called by the IME framework when the current input context is updated.
...@@ -228,10 +226,11 @@ BrailleIme.prototype = { ...@@ -228,10 +226,11 @@ BrailleIme.prototype = {
onInputContextUpdate_(context) { onInputContextUpdate_(context) {
this.log_('onInputContextUpdate', context); this.log_('onInputContextUpdate', context);
this.sendInputContext_(context); this.sendInputContext_(context);
}, }
/** /**
* Called by the system when this IME is active and a key event is generated. * Called by the system when this IME is active and a key event is
* generated.
* @param {string} engineID Engine ID, should be 'braille'. * @param {string} engineID Engine ID, should be 'braille'.
* @param {!ChromeKeyboardEvent} event The keyboard event. * @param {!ChromeKeyboardEvent} event The keyboard event.
* @private * @private
...@@ -241,7 +240,7 @@ BrailleIme.prototype = { ...@@ -241,7 +240,7 @@ BrailleIme.prototype = {
if (result !== undefined) { if (result !== undefined) {
this.keyEventHandled_(event.requestId, event.type, result); this.keyEventHandled_(event.requestId, event.type, result);
} }
}, }
/** /**
* Called when chrome ends the current text input session. * Called when chrome ends the current text input session.
...@@ -252,7 +251,7 @@ BrailleIme.prototype = { ...@@ -252,7 +251,7 @@ BrailleIme.prototype = {
this.log_('onReset', engineID); this.log_('onReset', engineID);
this.engineID_ = engineID; this.engineID_ = engineID;
this.sendToChromeVox_({type: 'reset'}); this.sendToChromeVox_({type: 'reset'});
}, }
/** /**
* Called by the IME framework when a menu item is activated. * Called by the IME framework when a menu item is activated.
...@@ -272,7 +271,7 @@ BrailleIme.prototype = { ...@@ -272,7 +271,7 @@ BrailleIme.prototype = {
} }
this.updateMenuItems_(); this.updateMenuItems_();
} }
}, }
/** /**
* Outputs a log message to the console, only if {@link BrailleIme.DEBUG} * Outputs a log message to the console, only if {@link BrailleIme.DEBUG}
...@@ -288,7 +287,7 @@ BrailleIme.prototype = { ...@@ -288,7 +287,7 @@ BrailleIme.prototype = {
} }
console.log('BrailleIme.' + func + ': ' + message); console.log('BrailleIme.' + func + ': ' + message);
} }
}, }
/** /**
* Handles a qwerty key on the home row as a braille key. * Handles a qwerty key on the home row as a braille key.
...@@ -336,7 +335,7 @@ BrailleIme.prototype = { ...@@ -336,7 +335,7 @@ BrailleIme.prototype = {
return true; return true;
} }
return false; return false;
}, }
/** /**
* Connects to the ChromeVox extension for message passing. * Connects to the ChromeVox extension for message passing.
...@@ -351,7 +350,7 @@ BrailleIme.prototype = { ...@@ -351,7 +350,7 @@ BrailleIme.prototype = {
this.CHROMEVOX_EXTENSION_ID_, {name: this.PORT_NAME}); this.CHROMEVOX_EXTENSION_ID_, {name: this.PORT_NAME});
this.port_.onMessage.addListener(this.onChromeVoxMessage_.bind(this)); this.port_.onMessage.addListener(this.onChromeVoxMessage_.bind(this));
this.port_.onDisconnect.addListener(this.onChromeVoxDisconnect_.bind(this)); this.port_.onDisconnect.addListener(this.onChromeVoxDisconnect_.bind(this));
}, }
/** /**
* Handles a message from the ChromeVox extension. * Handles a message from the ChromeVox extension.
...@@ -392,7 +391,7 @@ BrailleIme.prototype = { ...@@ -392,7 +391,7 @@ BrailleIme.prototype = {
'Unknown message from ChromeVox: ' + JSON.stringify(message)); 'Unknown message from ChromeVox: ' + JSON.stringify(message));
break; break;
} }
}, }
/** /**
* Handles a disconnect event from the ChromeVox side. * Handles a disconnect event from the ChromeVox side.
...@@ -401,7 +400,7 @@ BrailleIme.prototype = { ...@@ -401,7 +400,7 @@ BrailleIme.prototype = {
onChromeVoxDisconnect_() { onChromeVoxDisconnect_() {
this.port_ = null; this.port_ = null;
this.log_('onChromeVoxDisconnect', chrome.runtime.lastError); this.log_('onChromeVoxDisconnect', chrome.runtime.lastError);
}, }
/** /**
* Sends a message to the ChromeVox extension. * Sends a message to the ChromeVox extension.
...@@ -412,17 +411,18 @@ BrailleIme.prototype = { ...@@ -412,17 +411,18 @@ BrailleIme.prototype = {
if (this.port_) { if (this.port_) {
this.port_.postMessage(message); this.port_.postMessage(message);
} }
}, }
/** /**
* Sends the given input context to ChromeVox. * Sends the given input context to ChromeVox.
* @param {chrome.input.ime.InputContext} context Input context, or null when * @param {chrome.input.ime.InputContext} context Input context, or null
* when
* there's no input context. * there's no input context.
* @private * @private
*/ */
sendInputContext_(context) { sendInputContext_(context) {
this.sendToChromeVox_({type: 'inputContext', context}); this.sendToChromeVox_({type: 'inputContext', context});
}, }
/** /**
* Sends the active state to ChromeVox. * Sends the active state to ChromeVox.
...@@ -431,7 +431,7 @@ BrailleIme.prototype = { ...@@ -431,7 +431,7 @@ BrailleIme.prototype = {
sendActiveState_() { sendActiveState_() {
this.sendToChromeVox_( this.sendToChromeVox_(
{type: 'activeState', active: this.engineID_.length > 0}); {type: 'activeState', active: this.engineID_.length > 0});
}, }
/** /**
* Replaces text in the current text field. * Replaces text in the current text field.
...@@ -461,7 +461,7 @@ BrailleIme.prototype = { ...@@ -461,7 +461,7 @@ BrailleIme.prototype = {
} else { } else {
addText(); addText();
} }
}, }
/** /**
* Responds to an asynchronous key event, indicating whether it was handled * Responds to an asynchronous key event, indicating whether it was handled
...@@ -477,7 +477,7 @@ BrailleIme.prototype = { ...@@ -477,7 +477,7 @@ BrailleIme.prototype = {
this.sendToChromeVox_({type: 'reset'}); this.sendToChromeVox_({type: 'reset'});
} }
chrome.input.ime.keyEventHandled(requestId, response); chrome.input.ime.keyEventHandled(requestId, response);
}, }
/** /**
* Stores uncommitted text that will be committed on any key press or * Stores uncommitted text that will be committed on any key press or
...@@ -487,7 +487,7 @@ BrailleIme.prototype = { ...@@ -487,7 +487,7 @@ BrailleIme.prototype = {
*/ */
setUncommitted_(contextID, text) { setUncommitted_(contextID, text) {
this.uncommitted_ = {contextID, text}; this.uncommitted_ = {contextID, text};
}, }
/** /**
* Commits the last set uncommitted text if it matches the given context id. * Commits the last set uncommitted text if it matches the given context id.
...@@ -498,7 +498,7 @@ BrailleIme.prototype = { ...@@ -498,7 +498,7 @@ BrailleIme.prototype = {
chrome.input.ime.commitText(this.uncommitted_); chrome.input.ime.commitText(this.uncommitted_);
} }
this.uncommitted_ = null; this.uncommitted_ = null;
}, }
/** /**
* Updates the menu items for this IME. * Updates the menu items for this IME.
...@@ -517,4 +517,4 @@ BrailleIme.prototype = { ...@@ -517,4 +517,4 @@ BrailleIme.prototype = {
}] }]
}); });
} }
}; }
...@@ -8,13 +8,12 @@ ...@@ -8,13 +8,12 @@
/** /**
* Mock Chrome event supporting one listener. * Mock Chrome event supporting one listener.
* @constructor
*/ */
function MockEvent() {} class MockEvent {
constructor() {
MockEvent.prototype = { /** @type {Function?} */
/** @type {Function?} */ this.listener = null;
listener: null, }
/** /**
* @param {Function} listener * @param {Function} listener
...@@ -22,7 +21,7 @@ MockEvent.prototype = { ...@@ -22,7 +21,7 @@ MockEvent.prototype = {
addListener(listener) { addListener(listener) {
assertTrue(this.listener === null); assertTrue(this.listener === null);
this.listener = listener; this.listener = listener;
}, }
/** /**
* Dispatches an event to the listener if any. * Dispatches an event to the listener if any.
...@@ -35,21 +34,21 @@ MockEvent.prototype = { ...@@ -35,21 +34,21 @@ MockEvent.prototype = {
return this.listener.apply(null, arguments); return this.listener.apply(null, arguments);
} }
} }
}; }
/** /**
* Mock port that supports the {@code onMessage} and {@code onDisconnect} * Mock port that supports the {@code onMessage} and {@code onDisconnect}
* events as well as {@code postMessage}. * events as well as {@code postMessage}.
* @constructor.
*/ */
function MockPort() { class MockPort {
this.onMessage = new MockEvent(); constructor() {
this.onDisconnect = new MockEvent(); this.onMessage = new MockEvent();
/** @type {Array<Object>} */ this.onDisconnect = new MockEvent();
this.messages = []; /** @type {Array<Object>} */
} this.messages = [];
}
MockPort.prototype = {
/** /**
* Stores {@code message} in this object. * Stores {@code message} in this object.
* @param {Object} message Message to store. * @param {Object} message Message to store.
...@@ -57,7 +56,8 @@ MockPort.prototype = { ...@@ -57,7 +56,8 @@ MockPort.prototype = {
postMessage(message) { postMessage(message) {
this.messages.push(message); this.messages.push(message);
} }
}; }
/** /**
* Engine ID as specified in manifest. * Engine ID as specified in manifest.
...@@ -69,19 +69,8 @@ var localStorage; ...@@ -69,19 +69,8 @@ var localStorage;
/** /**
* Test fixture for the braille IME unit test. * Test fixture for the braille IME unit test.
* @constructor
* @extends {testing.Test}
*/ */
function BrailleImeUnitTest() { BrailleImeUnitTest = class extends testing.Test {
testing.Test.call(this);
}
BrailleImeUnitTest.prototype = {
__proto__: testing.Test.prototype,
/** @Override */
extraLibraries: ['braille_ime.js'],
/** @Override */ /** @Override */
setUp() { setUp() {
chrome = chrome || {}; chrome = chrome || {};
...@@ -97,7 +86,7 @@ BrailleImeUnitTest.prototype = { ...@@ -97,7 +86,7 @@ BrailleImeUnitTest.prototype = {
this.lastHandledKeyResult_ = result; this.lastHandledKeyResult_ = result;
}.bind(this); }.bind(this);
this.createIme(); this.createIme();
}, }
createIme() { createIme() {
var IME_EVENTS = [ var IME_EVENTS = [
...@@ -118,14 +107,14 @@ BrailleImeUnitTest.prototype = { ...@@ -118,14 +107,14 @@ BrailleImeUnitTest.prototype = {
this.port = null; this.port = null;
this.ime = new BrailleIme(); this.ime = new BrailleIme();
this.ime.init(); this.ime.init();
}, }
activateIme() { activateIme() {
this.onActivate.dispatch(ENGINE_ID); this.onActivate.dispatch(ENGINE_ID);
assertThat( assertThat(
this.port.messages, eqJSON([{type: 'activeState', active: true}])); this.port.messages, eqJSON([{type: 'activeState', active: true}]));
this.port.messages.length = 0; this.port.messages.length = 0;
}, }
sendKeyEvent_(type, code, extra) { sendKeyEvent_(type, code, extra) {
var event = {type, code, requestId: (++this.lastSentKeyRequestId_) + ''}; var event = {type, code, requestId: (++this.lastSentKeyRequestId_) + ''};
...@@ -136,17 +125,21 @@ BrailleImeUnitTest.prototype = { ...@@ -136,17 +125,21 @@ BrailleImeUnitTest.prototype = {
if (this.lastSentKeyRequestId_ === this.lastHandledKeyRequestId_) { if (this.lastSentKeyRequestId_ === this.lastHandledKeyRequestId_) {
return this.lastHandledKeyResult_; return this.lastHandledKeyResult_;
} }
}, }
sendKeyDown(code, extra) { sendKeyDown(code, extra) {
return this.sendKeyEvent_('keydown', code, extra); return this.sendKeyEvent_('keydown', code, extra);
}, }
sendKeyUp(code, extra) { sendKeyUp(code, extra) {
return this.sendKeyEvent_('keyup', code, extra); return this.sendKeyEvent_('keyup', code, extra);
}, }
}; };
/** @Override */
BrailleImeUnitTest.prototype.extraLibraries = ['braille_ime.js'];
TEST_F('BrailleImeUnitTest', 'KeysWhenStandardKeyboardDisabled', function() { TEST_F('BrailleImeUnitTest', 'KeysWhenStandardKeyboardDisabled', function() {
this.activateIme(); this.activateIme();
expectFalse(this.sendKeyDown('KeyF')); expectFalse(this.sendKeyDown('KeyF'));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment