summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/braille_ime/braille_ime_unittest.gtestjs
blob: 83740b6c5c9f5e8afe116187ccb2efbfffe966e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Unit test for the Braille IME.
 */

/**
 * Mock Chrome event supporting one listener.
 * @constructor
 */
function MockEvent() {}

MockEvent.prototype = {
  /** @type {Function?} */
  listener: null,

  /**
   * @param {Function} listener
   */
  addListener: function(listener) {
    assertTrue(this.listener === null);
    this.listener = listener;
  },

  /**
   * Dispatches an event to the listener if any.
   * @param {...*} var_args Arguments to pass to the event listener.
   * @return {*} Return value from listener or {@code undefined} if no
   *     listener.
   */
  dispatch: function() {
    if (this.listener) {
      return this.listener.apply(null, arguments);
    }
  }
};

/**
 * Mock port that supports the {@code onMessage} and {@code onDisconnect}
 * events as well as {@code postMessage}.
 * @constructor.
 */
function MockPort() {
  this.onMessage = new MockEvent();
  this.onDisconnect = new MockEvent();
  /** @type {Array.<Object>} */
  this.messages = [];
}

MockPort.prototype = {
  /**
   * Stores {@code message} in this object.
   * @param {Object} message Message to store.
   */
  postMessage: function(message) {
    this.messages.push(message);
  }
};

/**
 * Engine ID as specified in manifest.
 * @const {string}
 */
ENGINE_ID = 'braille';

var localStorage;

/**
 * Test fixture for the braille IME unit test.
 * @constructor
 * @extends {testing.Test}
 */
function BrailleImeUnitTest() {
  testing.Test.call(this);
}

BrailleImeUnitTest.prototype = {
  __proto__: testing.Test.prototype,

  /** @Override */
  extraLibraries: [
    'braille_ime.js'
  ],

  /** @Override */
  setUp: function() {
    chrome = chrome || {};
    chrome.input = chrome.input || {};
    chrome.input.ime = chrome.input.ime || {};
    chrome.runtime = chrome.runtime || {};
    localStorage = {};
    this.lastSentKeyRequestId_ = 0;
    this.lastHandledKeyRequestId_ = undefined;
    this.lastHandledKeyResult_ = undefined;
    chrome.input.ime.keyEventHandled = function(requestId, result) {
      this.lastHandledKeyRequestId_ = Number(requestId);
      this.lastHandledKeyResult_ = result;
    }.bind(this);
    this.createIme();
  },

  createIme: function() {
    var IME_EVENTS = [ 'onActivate', 'onDeactivated', 'onFocus', 'onBlur',
                       'onInputContextUpdate', 'onKeyEvent', 'onReset',
                       'onMenuItemActivated' ];
    for (var i = 0, name; name = IME_EVENTS[i]; ++i) {
      this[name] = chrome.input.ime[name] = new MockEvent();

    }
    chrome.input.ime.setMenuItems = function(parameters) {
      this.menuItems = parameters.items;
    }.bind(this);
    chrome.runtime.connect = function() {
      this.port = new MockPort();
      return this.port;
    }.bind(this);
    this.menuItems = null;
    this.port = null;
    this.ime = new BrailleIme();
    this.ime.init();
  },

  activateIme: function() {
    this.onActivate.dispatch(ENGINE_ID);
    assertThat(this.port.messages,
               eqJSON([{type: 'activeState', active: true}]));
    this.port.messages.length = 0;
  },

  sendKeyEvent_: function(type, code, extra) {
    var event = {type: type,
                 code: code,
                 requestId: (++this.lastSentKeyRequestId_) + ''};
    for (var key in extra) {
      event[key] = extra[key];
    }
    this.onKeyEvent.dispatch(ENGINE_ID, event);
    if (this.lastSentKeyRequestId_ === this.lastHandledKeyRequestId_) {
      return this.lastHandledKeyResult_;
    }
  },

  sendKeyDown: function(code, extra) {
    return this.sendKeyEvent_('keydown', code, extra);
  },

  sendKeyUp: function(code, extra) {
    return this.sendKeyEvent_('keyup', code, extra);
  },
};

TEST_F('BrailleImeUnitTest', 'KeysWhenStandardKeyboardDisabled', function() {
  this.activateIme();
  expectFalse(this.sendKeyDown('KeyF'));
  expectFalse(this.sendKeyDown('KeyD'));
  expectFalse(this.sendKeyUp('KeyD'));
  expectFalse(this.sendKeyUp('KeyF'));
  expectEquals(0, this.port.messages.length);
});

TEST_F('BrailleImeUnitTest', 'KeysWhenStandardKeysEnabled', function() {
  this.activateIme();
  assertFalse(this.menuItems[0].checked);
  this.onMenuItemActivated.dispatch(ENGINE_ID, this.menuItems[0].id);
  assertTrue(this.menuItems[0].checked);
  // Type the letters 'b' and 'c' and verify the right dots get sent.
  expectTrue(this.sendKeyDown('KeyF'));
  expectTrue(this.sendKeyDown('KeyD'));
  expectTrue(this.sendKeyUp('KeyD'));
  expectTrue(this.sendKeyUp('KeyF'));
  expectTrue(this.sendKeyDown('KeyJ'));
  expectTrue(this.sendKeyDown('KeyF'));
  expectTrue(this.sendKeyUp('KeyJ'));
  expectTrue(this.sendKeyUp('KeyF'));

  // Make sure that other keys are not handled, either by themselves or while
  // one of the 'braille keys' is pressed.
  expectFalse(this.sendKeyDown('KeyX'));
  expectFalse(this.sendKeyUp('KeyX'));

  expectTrue(this.sendKeyDown('KeyS'));  // Dot 3
  expectFalse(this.sendKeyDown('KeyG'));  // To the right of dot 1.
  expectTrue(this.sendKeyUp('KeyS'));
  expectFalse(this.sendKeyUp('KeyG'));

  // Keys like Ctrl L should not be handled, despite L being a dot key.
  var ctrlFlag = {ctrlKey: true};
  expectFalse(this.sendKeyDown('ControlLeft', ctrlFlag));
  expectFalse(this.sendKeyDown('KeyL', ctrlFlag));
  expectFalse(this.sendKeyUp('KeyL', ctrlFlag));
  expectFalse(this.sendKeyUp('ControlLeft', ctrlFlag))

  // Space key by itself should send a blank cell.
  expectTrue(this.sendKeyDown('Space'));
  expectTrue(this.sendKeyUp('Space'));

  // Space and braille dots results in no event.
  expectTrue(this.sendKeyDown('Space'));
  expectTrue(this.sendKeyDown('KeyF'));
  expectTrue(this.sendKeyUp('Space'));
  expectTrue(this.sendKeyUp('KeyF'));
  // Send the braille key first, still no event should be produced.
  expectTrue(this.sendKeyDown('KeyF'));
  expectTrue(this.sendKeyDown('Space'));
  expectTrue(this.sendKeyUp('Space'));
  expectTrue(this.sendKeyUp('KeyF'));

  assertThat(this.port.messages,
             eqJSON([{type: 'brailleDots', dots: 0x03},
                     {type: 'brailleDots', dots: 0x09},
                     {type: 'brailleDots', dots: 0}]));
});

TEST_F('BrailleImeUnitTest', 'TestBackspaceKey', function() {
  this.activateIme();
  // Enable standard keyboard feature.
  assertFalse(this.menuItems[0].checked);
  this.onMenuItemActivated.dispatch(ENGINE_ID, this.menuItems[0].id);
  assertTrue(this.menuItems[0].checked);

  expectEquals(undefined, this.sendKeyDown('Backspace'));
  assertThat(this.port.messages,
             eqJSON([{type: 'backspace',
                      requestId: this.lastSentKeyRequestId_ + ''}]));
  this.port.onMessage.dispatch(
      {type: 'keyEventHandled',
       requestId: this.lastSentKeyRequestId_ + '',
       result: true});
  expectEquals(this.lastSentKeyRequestId_, this.lastHandledKeyRequestId_);
  expectTrue(this.lastHandledKeyResult_);
});

TEST_F('BrailleImeUnitTest', 'UseStandardKeyboardSettingPreserved', function() {
  this.activateIme();
  assertFalse(this.menuItems[0].checked);
  this.onMenuItemActivated.dispatch(ENGINE_ID, this.menuItems[0].id);
  assertTrue(this.menuItems[0].checked);
  // Create a new instance and make sure the setting is still turned on.
  this.createIme();
  this.activateIme();
  assertTrue(this.menuItems[0].checked);
});

TEST_F('BrailleImeUnitTest', 'ReplaceText', function() {
  var CONTEXT_ID = 1;
  var hasSelection = false;
  var text = 'Hi, ';
  chrome.input.ime.commitText = function(params) {
    assertEquals(CONTEXT_ID, params.contextID);
    text += params.text;
  };
  chrome.input.ime.deleteSurroundingText = function(params, callback) {
    assertEquals(ENGINE_ID, params.engineID);
    assertEquals(CONTEXT_ID, params.contextID);
    assertEquals(0, params.offset + params.length);
    if (hasSelection) {
      assertEquals(0, params.length);
      hasSelection = false;
    } else {
      text = text.slice(0, params.offset);
    }
    callback();
  };
  var sendReplaceText = function(deleteBefore, newText) {
    this.port.onMessage.dispatch(
        {type: 'replaceText', contextID: CONTEXT_ID,
       deleteBefore: deleteBefore, newText: newText});
  }.bind(this);
  this.activateIme();
  sendReplaceText(0, 'hello!');
  assertEquals('Hi, hello!', text);
  hasSelection = true;
  sendReplaceText('hello!'.length, 'good bye!');
  assertFalse(hasSelection);
  assertEquals('Hi, good bye!', text);
});