Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #21807 from RudyLu/keyboard/Bug1038131-remove_sele…
Browse files Browse the repository at this point in the history
…ctionchange_listener

Bug 1038131 - latin.js failed to deactivate and continue to respond to
r=timdream.
  • Loading branch information
RudyLu committed Jul 18, 2014
2 parents 3aa82ec + 40101dd commit ee728cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 7 additions & 2 deletions apps/keyboard/js/imes/latin/latin.js
Expand Up @@ -130,11 +130,11 @@
*/
var inputSequencePromise = Promise.resolve();


// Flag to stop updating suggestions for selectionchange when we're going
// to do some actions that will cause selectionchange, such as sendKey()
// or replaceSurroundingText().
var pendingSelectionChange = 0;
var inputContext = null;

// keyboard.js calls this to pass us the interface object we need
// to communicate with it
Expand Down Expand Up @@ -199,7 +199,8 @@
correcting = (options.correct && inputMode !== 'verbatim');

if (state.inputContext) {
state.inputContext.addEventListener('selectionchange', this);
inputContext = state.inputContext;
inputContext.addEventListener('selectionchange', this);
}

// Reset our state
Expand Down Expand Up @@ -230,6 +231,10 @@
}

function deactivate() {
if (inputContext) {
inputContext.removeEventListener('selectionchange', this);
}

if (!worker || idleTimer)
return;
idleTimer = setTimeout(terminateWorker, workerTimeout);
Expand Down
16 changes: 15 additions & 1 deletion apps/keyboard/test/unit/latin_test.js
Expand Up @@ -338,6 +338,7 @@ suite('latin input method capitalization and punctuation', function() {
var keyboardGlue = Object.create(defaultKeyboardGlue);
var _windowWorker;
var workers = [];
var handleEventSpy = null;

function activateIME() {
im.activate('en', {
Expand Down Expand Up @@ -365,22 +366,35 @@ suite('latin input method capitalization and punctuation', function() {
};

worker.prototype.postMessage = function() {};

handleEventSpy = sinon.spy(im, 'handleEvent');
});

teardown(function() {
window.Worker = _windowWorker;
handleEventSpy.restore();
});

test('should listen to selectionchange', function() {
im.init(keyboardGlue);
activateIME();

var handleEventSpy = sinon.spy(im, 'handleEvent');
inputContext.dispatchEvent(new Event('selectionchange'));

sinon.assert.calledOnce(handleEventSpy);
});

test('should stop listening to selectionchange when' +
' deactivated', function() {
im.init(keyboardGlue);
activateIME();

im.deactivate();

inputContext.dispatchEvent(new Event('selectionchange'));
sinon.assert.notCalled(handleEventSpy);
});

test('wll clear the suggestions if selectionchange', function() {
im = InputMethods.latin;
keyboardGlue.sendCandidates = sinon.stub();
Expand Down

0 comments on commit ee728cc

Please sign in to comment.