OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Unit test for the Braille IME. | 6 * @fileoverview Unit test for the Braille IME. |
7 */ | 7 */ |
8 | 8 |
9 /** | 9 /** |
10 * Mock Chrome event supporting one listener. | 10 * Mock Chrome event supporting one listener. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 'braille_ime.js' | 84 'braille_ime.js' |
85 ], | 85 ], |
86 | 86 |
87 /** @Override */ | 87 /** @Override */ |
88 setUp: function() { | 88 setUp: function() { |
89 chrome = chrome || {}; | 89 chrome = chrome || {}; |
90 chrome.input = chrome.input || {}; | 90 chrome.input = chrome.input || {}; |
91 chrome.input.ime = chrome.input.ime || {}; | 91 chrome.input.ime = chrome.input.ime || {}; |
92 chrome.runtime = chrome.runtime || {}; | 92 chrome.runtime = chrome.runtime || {}; |
93 localStorage = {}; | 93 localStorage = {}; |
| 94 this.lastSentKeyRequestId_ = 0; |
| 95 this.lastHandledKeyRequestId_ = undefined; |
| 96 this.lastHandledKeyResult_ = undefined; |
| 97 chrome.input.ime.keyEventHandled = function(requestId, result) { |
| 98 this.lastHandledKeyRequestId_ = Number(requestId); |
| 99 this.lastHandledKeyResult_ = result; |
| 100 }.bind(this); |
94 this.createIme(); | 101 this.createIme(); |
95 }, | 102 }, |
96 | 103 |
97 createIme: function() { | 104 createIme: function() { |
98 var IME_EVENTS = [ 'onActivate', 'onDeactivated', 'onFocus', 'onBlur', | 105 var IME_EVENTS = [ 'onActivate', 'onDeactivated', 'onFocus', 'onBlur', |
99 'onInputContextUpdate', 'onKeyEvent', 'onReset', | 106 'onInputContextUpdate', 'onKeyEvent', 'onReset', |
100 'onMenuItemActivated' ]; | 107 'onMenuItemActivated' ]; |
101 for (var i = 0, name; name = IME_EVENTS[i]; ++i) { | 108 for (var i = 0, name; name = IME_EVENTS[i]; ++i) { |
102 this[name] = chrome.input.ime[name] = new MockEvent(); | 109 this[name] = chrome.input.ime[name] = new MockEvent(); |
103 | 110 |
(...skipping 11 matching lines...) Expand all Loading... |
115 this.ime.init(); | 122 this.ime.init(); |
116 }, | 123 }, |
117 | 124 |
118 activateIme: function() { | 125 activateIme: function() { |
119 this.onActivate.dispatch(ENGINE_ID); | 126 this.onActivate.dispatch(ENGINE_ID); |
120 assertThat(this.port.messages, | 127 assertThat(this.port.messages, |
121 eqJSON([{type: 'activeState', active: true}])); | 128 eqJSON([{type: 'activeState', active: true}])); |
122 this.port.messages.length = 0; | 129 this.port.messages.length = 0; |
123 }, | 130 }, |
124 | 131 |
125 sendKeyDown: function(code, extra) { | 132 sendKeyEvent_: function(type, code, extra) { |
126 var event = {code: code, type: 'keydown'}; | 133 var event = {type: type, |
| 134 code: code, |
| 135 requestId: (++this.lastSentKeyRequestId_) + ''}; |
127 for (var key in extra) { | 136 for (var key in extra) { |
128 event[key] = extra[key]; | 137 event[key] = extra[key]; |
129 } | 138 } |
130 return this.onKeyEvent.dispatch(ENGINE_ID, event); | 139 this.onKeyEvent.dispatch(ENGINE_ID, event); |
| 140 if (this.lastSentKeyRequestId_ === this.lastHandledKeyRequestId_) { |
| 141 return this.lastHandledKeyResult_; |
| 142 } |
| 143 }, |
| 144 |
| 145 sendKeyDown: function(code, extra) { |
| 146 return this.sendKeyEvent_('keydown', code, extra); |
131 }, | 147 }, |
132 | 148 |
133 sendKeyUp: function(code, extra) { | 149 sendKeyUp: function(code, extra) { |
134 var event = {code: code, type: 'keyup'}; | 150 return this.sendKeyEvent_('keyup', code, extra); |
135 for (var key in extra) { | |
136 event[key] = extra[key]; | |
137 } | |
138 return this.onKeyEvent.dispatch(ENGINE_ID, event); | |
139 }, | 151 }, |
140 }; | 152 }; |
141 | 153 |
142 TEST_F('BrailleImeUnitTest', 'KeysWhenStandardKeyboardDisabled', function() { | 154 TEST_F('BrailleImeUnitTest', 'KeysWhenStandardKeyboardDisabled', function() { |
143 this.activateIme(); | 155 this.activateIme(); |
144 expectFalse(this.sendKeyDown('KeyF')); | 156 expectFalse(this.sendKeyDown('KeyF')); |
145 expectFalse(this.sendKeyDown('KeyD')); | 157 expectFalse(this.sendKeyDown('KeyD')); |
146 expectFalse(this.sendKeyUp('KeyD')); | 158 expectFalse(this.sendKeyUp('KeyD')); |
147 expectFalse(this.sendKeyUp('KeyF')); | 159 expectFalse(this.sendKeyUp('KeyF')); |
148 expectEquals(0, this.port.messages.length); | 160 expectEquals(0, this.port.messages.length); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 expectTrue(this.sendKeyDown('Space')); | 206 expectTrue(this.sendKeyDown('Space')); |
195 expectTrue(this.sendKeyUp('Space')); | 207 expectTrue(this.sendKeyUp('Space')); |
196 expectTrue(this.sendKeyUp('KeyF')); | 208 expectTrue(this.sendKeyUp('KeyF')); |
197 | 209 |
198 assertThat(this.port.messages, | 210 assertThat(this.port.messages, |
199 eqJSON([{type: 'brailleDots', dots: 0x03}, | 211 eqJSON([{type: 'brailleDots', dots: 0x03}, |
200 {type: 'brailleDots', dots: 0x09}, | 212 {type: 'brailleDots', dots: 0x09}, |
201 {type: 'brailleDots', dots: 0}])); | 213 {type: 'brailleDots', dots: 0}])); |
202 }); | 214 }); |
203 | 215 |
| 216 TEST_F('BrailleImeUnitTest', 'TestBackspaceKey', function() { |
| 217 this.activateIme(); |
| 218 // Enable standard keyboard feature. |
| 219 assertFalse(this.menuItems[0].checked); |
| 220 this.onMenuItemActivated.dispatch(ENGINE_ID, this.menuItems[0].id); |
| 221 assertTrue(this.menuItems[0].checked); |
| 222 |
| 223 expectEquals(undefined, this.sendKeyDown('Backspace')); |
| 224 assertThat(this.port.messages, |
| 225 eqJSON([{type: 'backspace', |
| 226 requestId: this.lastSentKeyRequestId_ + ''}])); |
| 227 this.port.onMessage.dispatch( |
| 228 {type: 'keyEventHandled', |
| 229 requestId: this.lastSentKeyRequestId_ + '', |
| 230 result: true}); |
| 231 expectEquals(this.lastSentKeyRequestId_, this.lastHandledKeyRequestId_); |
| 232 expectTrue(this.lastHandledKeyResult_); |
| 233 }); |
| 234 |
204 TEST_F('BrailleImeUnitTest', 'UseStandardKeyboardSettingPreserved', function() { | 235 TEST_F('BrailleImeUnitTest', 'UseStandardKeyboardSettingPreserved', function() { |
205 this.activateIme(); | 236 this.activateIme(); |
206 assertFalse(this.menuItems[0].checked); | 237 assertFalse(this.menuItems[0].checked); |
207 this.onMenuItemActivated.dispatch(ENGINE_ID, this.menuItems[0].id); | 238 this.onMenuItemActivated.dispatch(ENGINE_ID, this.menuItems[0].id); |
208 assertTrue(this.menuItems[0].checked); | 239 assertTrue(this.menuItems[0].checked); |
209 // Create a new instance and make sure the setting is still turned on. | 240 // Create a new instance and make sure the setting is still turned on. |
210 this.createIme(); | 241 this.createIme(); |
211 this.activateIme(); | 242 this.activateIme(); |
212 assertTrue(this.menuItems[0].checked); | 243 assertTrue(this.menuItems[0].checked); |
213 }); | 244 }); |
(...skipping 24 matching lines...) Expand all Loading... |
238 deleteBefore: deleteBefore, newText: newText}); | 269 deleteBefore: deleteBefore, newText: newText}); |
239 }.bind(this); | 270 }.bind(this); |
240 this.activateIme(); | 271 this.activateIme(); |
241 sendReplaceText(0, 'hello!'); | 272 sendReplaceText(0, 'hello!'); |
242 assertEquals('Hi, hello!', text); | 273 assertEquals('Hi, hello!', text); |
243 hasSelection = true; | 274 hasSelection = true; |
244 sendReplaceText('hello!'.length, 'good bye!'); | 275 sendReplaceText('hello!'.length, 'good bye!'); |
245 assertFalse(hasSelection); | 276 assertFalse(hasSelection); |
246 assertEquals('Hi, good bye!', text); | 277 assertEquals('Hi, good bye!', text); |
247 }); | 278 }); |
OLD | NEW |