OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/base/ime/input_method_auralinux.h" | |
6 | |
7 #include "base/strings/string_split.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 #include "ui/base/ime/dummy_text_input_client.h" | |
11 #include "ui/base/ime/input_method_delegate.h" | |
12 #include "ui/base/ime/input_method_initializer.h" | |
13 #include "ui/base/ime/linux/fake_input_method_context.h" | |
14 #include "ui/base/ime/linux/linux_input_method_context_factory.h" | |
15 #include "ui/events/event.h" | |
16 | |
17 namespace ui { | |
18 namespace { | |
19 | |
20 const base::char16 kActionCommit = L'C'; | |
21 const base::char16 kActionCompositionStart = L'S'; | |
22 const base::char16 kActionCompositionUpdate = L'U'; | |
23 const base::char16 kActionCompositionEnd = L'E'; | |
24 | |
25 class LinuxInputMethodContextForTesting : public LinuxInputMethodContext { | |
26 public: | |
27 LinuxInputMethodContextForTesting( | |
28 LinuxInputMethodContextDelegate* delegate, bool is_simple) | |
29 : delegate_(delegate), is_simple_(is_simple), is_sync_mode_(false), | |
30 eat_key_(false), focused_(false) {} | |
31 | |
32 void SetSyncMode(bool is_sync_mode) { is_sync_mode_ = is_sync_mode; } | |
33 void SetEatKey(bool eat_key) { eat_key_ = eat_key; } | |
34 | |
35 void AddAction(const std::string& action) { | |
36 actions_.push_back(base::ASCIIToUTF16(action)); | |
37 } | |
38 | |
39 protected: | |
40 bool DispatchKeyEvent(const ui::KeyEvent& key_event) override { | |
41 if (!is_sync_mode_) { | |
42 actions_.clear(); | |
43 return eat_key_; | |
44 } | |
45 | |
46 for (size_t i = 0; i < actions_.size(); i++) { | |
47 base::string16 action = actions_[i]; | |
48 std::vector<base::string16> parts; | |
49 base::SplitString(action, L':', &parts); | |
50 base::char16 id = parts[0][0]; | |
51 base::string16 param; | |
52 if (parts.size() > 1) | |
53 param = parts[1]; | |
54 if (id == kActionCommit) { | |
55 delegate_->OnCommit(param); | |
56 } else if (id == kActionCompositionStart) { | |
57 delegate_->OnPreeditStart(); | |
58 } else if (id == kActionCompositionUpdate) { | |
59 CompositionText comp; | |
60 comp.text = param; | |
61 delegate_->OnPreeditChanged(comp); | |
62 } else if (id == kActionCompositionEnd) { | |
63 delegate_->OnPreeditEnd(); | |
64 } | |
65 } | |
66 | |
67 actions_.clear(); | |
68 return eat_key_; | |
69 } | |
70 | |
71 void Reset() override { | |
72 } | |
73 | |
74 void Focus() override { | |
75 focused_ = true; | |
76 } | |
77 | |
78 void Blur() override { | |
79 focused_ = false; | |
80 } | |
81 | |
82 void SetCursorLocation(const gfx::Rect& rect) override { | |
83 cursor_position_ = rect; | |
84 } | |
85 | |
86 private: | |
87 LinuxInputMethodContextDelegate* delegate_; | |
88 std::vector<base::string16> actions_; | |
89 bool is_simple_; | |
90 bool is_sync_mode_; | |
91 bool eat_key_; | |
92 bool focused_; | |
93 gfx::Rect cursor_position_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(LinuxInputMethodContextForTesting); | |
96 }; | |
97 | |
98 class LinuxInputMethodContextFactoryForTesting : | |
99 public LinuxInputMethodContextFactory { | |
100 public: | |
101 LinuxInputMethodContextFactoryForTesting() {}; | |
102 | |
103 scoped_ptr<LinuxInputMethodContext> CreateInputMethodContext( | |
104 LinuxInputMethodContextDelegate* delegate, | |
105 bool is_simple) const override { | |
106 return scoped_ptr<ui::LinuxInputMethodContext>( | |
107 new LinuxInputMethodContextForTesting(delegate, is_simple)); | |
108 }; | |
109 | |
110 private: | |
111 DISALLOW_COPY_AND_ASSIGN(LinuxInputMethodContextFactoryForTesting); | |
112 }; | |
113 | |
114 class InputMethodDelegateForTesting : public internal::InputMethodDelegate { | |
115 public: | |
116 InputMethodDelegateForTesting() | |
117 : key_type(ET_UNKNOWN), key_code(VKEY_UNKNOWN), key_flags(0) {}; | |
118 ~InputMethodDelegateForTesting() override {}; | |
119 | |
120 bool DispatchKeyEventPostIME(const ui::KeyEvent& key_event) override { | |
121 key_type = key_event.type(); | |
122 key_code = key_event.key_code(); | |
123 key_flags = key_event.flags(); | |
124 return false; | |
125 } | |
126 | |
127 void Clear() { | |
128 key_type = ET_UNKNOWN; | |
129 key_code = VKEY_UNKNOWN; | |
130 key_flags = 0; | |
131 } | |
132 | |
133 EventType key_type; | |
134 KeyboardCode key_code; | |
135 int key_flags; | |
136 | |
137 private: | |
138 DISALLOW_COPY_AND_ASSIGN(InputMethodDelegateForTesting); | |
139 }; | |
140 | |
141 class TextInputClientForTesting : public DummyTextInputClient { | |
142 public: | |
143 explicit TextInputClientForTesting(TextInputType text_input_type) | |
144 : DummyTextInputClient(text_input_type), | |
145 called_clear_composition(false), called_confirm_composition(false), | |
146 called_set_composition(false), called_insert_text(false), | |
147 called_insert_char(false) {}; | |
148 | |
149 void Clear() { | |
150 composition_text.clear(); | |
151 result_text.clear(); | |
152 called_clear_composition = called_confirm_composition = | |
153 called_set_composition = called_insert_text = called_insert_char = | |
154 false; | |
155 } | |
156 | |
157 base::string16 composition_text; | |
158 base::string16 result_text; | |
159 bool called_clear_composition; | |
160 bool called_confirm_composition; | |
161 bool called_set_composition; | |
162 bool called_insert_text; | |
163 bool called_insert_char; | |
164 | |
165 protected: | |
166 void SetCompositionText(const CompositionText& composition) override { | |
167 composition_text = composition.text; | |
168 called_set_composition = true; | |
169 } | |
170 | |
171 bool HasCompositionText() const override { | |
172 return !composition_text.empty(); | |
173 } | |
174 | |
175 void ConfirmCompositionText() override { | |
176 composition_text.clear(); | |
177 called_confirm_composition = true; | |
178 } | |
179 | |
180 void ClearCompositionText() override { | |
181 composition_text.clear(); | |
182 called_clear_composition = true; | |
183 } | |
184 | |
185 void InsertText(const base::string16& text) override { | |
186 result_text = text; | |
187 called_insert_text = true; | |
188 } | |
189 | |
190 void InsertChar(base::char16 ch, int flags) override { | |
191 result_text = ch; | |
192 called_insert_char = true; | |
193 } | |
194 }; | |
195 | |
196 class InputMethodAuraLinuxTest : public testing::Test { | |
197 protected: | |
198 InputMethodAuraLinuxTest() | |
199 : factory_(NULL), input_method_auralinux_(NULL), delegate_(NULL), | |
200 context_(NULL), context_simple_(NULL) { | |
201 factory_ = new LinuxInputMethodContextFactoryForTesting(); | |
202 LinuxInputMethodContextFactory::SetInstance(factory_); | |
203 } | |
204 ~InputMethodAuraLinuxTest() override { | |
205 delete factory_; | |
206 factory_ = NULL; | |
207 } | |
208 | |
209 void SetUp() override { | |
210 delegate_ = new InputMethodDelegateForTesting(); | |
211 input_method_auralinux_ = new InputMethodAuraLinux(delegate_); | |
212 input_method_auralinux_->OnFocus(); | |
213 context_ = static_cast<LinuxInputMethodContextForTesting*>( | |
214 input_method_auralinux_->GetContextForTesting(false)); | |
215 context_simple_ = static_cast<LinuxInputMethodContextForTesting*>( | |
216 input_method_auralinux_->GetContextForTesting(true)); | |
217 } | |
218 | |
219 void TearDown() override { | |
220 context_->SetSyncMode(false); | |
221 context_->SetEatKey(false); | |
222 | |
223 context_simple_->SetSyncMode(false); | |
224 context_simple_->SetEatKey(false); | |
225 | |
226 context_ = NULL; | |
227 context_simple_ = NULL; | |
228 | |
229 delete input_method_auralinux_; | |
230 input_method_auralinux_ = NULL; | |
231 delete delegate_; | |
232 delegate_ = NULL; | |
233 } | |
234 | |
235 LinuxInputMethodContextFactoryForTesting* factory_; | |
236 InputMethodAuraLinux* input_method_auralinux_; | |
237 InputMethodDelegateForTesting* delegate_; | |
238 LinuxInputMethodContextForTesting* context_; | |
239 LinuxInputMethodContextForTesting* context_simple_; | |
240 | |
241 DISALLOW_COPY_AND_ASSIGN(InputMethodAuraLinuxTest); | |
242 }; | |
243 | |
244 TEST_F(InputMethodAuraLinuxTest, BasicSyncModeTest) { | |
245 context_->SetSyncMode(true); | |
246 context_->SetEatKey(true); | |
247 context_->AddAction("C:a"); | |
248 | |
249 TextInputClientForTesting* client = new TextInputClientForTesting( | |
Seigo Nonaka
2015/04/08 07:14:58
Optional: I recommend you to use scoped_ptr for th
Shu Chen
2015/04/08 08:24:12
Done.
| |
250 TEXT_INPUT_TYPE_TEXT); | |
251 input_method_auralinux_->SetFocusedTextInputClient(client); | |
252 input_method_auralinux_->OnTextInputTypeChanged(client); | |
253 | |
254 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0); | |
255 key.set_character(L'a'); | |
256 input_method_auralinux_->DispatchKeyEvent(key); | |
257 | |
258 EXPECT_EQ(key.type(), delegate_->key_type); | |
259 EXPECT_EQ(key.key_code(), delegate_->key_code); | |
260 EXPECT_EQ(key.flags(), delegate_->key_flags); | |
261 EXPECT_FALSE(client->called_insert_text); | |
262 EXPECT_TRUE(client->called_insert_char); | |
263 EXPECT_EQ(1U, client->result_text.size()); | |
264 EXPECT_EQ(key.GetCharacter(), client->result_text[0]); | |
265 | |
266 delete client; | |
267 client = new TextInputClientForTesting(TEXT_INPUT_TYPE_PASSWORD); | |
268 context_simple_->SetSyncMode(true); | |
269 context_simple_->SetEatKey(false); | |
270 | |
271 input_method_auralinux_->SetFocusedTextInputClient(client); | |
272 input_method_auralinux_->OnTextInputTypeChanged(client); | |
273 input_method_auralinux_->DispatchKeyEvent(key); | |
274 | |
275 EXPECT_EQ(key.type(), delegate_->key_type); | |
276 EXPECT_EQ(key.key_code(), delegate_->key_code); | |
277 EXPECT_EQ(key.flags(), delegate_->key_flags); | |
278 EXPECT_FALSE(client->called_insert_text); | |
279 EXPECT_TRUE(client->called_insert_char); | |
280 EXPECT_EQ(1U, client->result_text.size()); | |
281 EXPECT_EQ(key.GetCharacter(), client->result_text[0]); | |
282 | |
283 delete client; | |
284 } | |
285 | |
286 TEST_F(InputMethodAuraLinuxTest, BasicAsyncModeTest) { | |
287 context_->SetSyncMode(false); | |
288 context_->SetEatKey(true); | |
289 | |
290 TextInputClientForTesting* client = new TextInputClientForTesting( | |
291 TEXT_INPUT_TYPE_TEXT); | |
292 input_method_auralinux_->SetFocusedTextInputClient(client); | |
293 input_method_auralinux_->OnTextInputTypeChanged(client); | |
294 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0); | |
295 key.set_character(L'a'); | |
296 input_method_auralinux_->DispatchKeyEvent(key); | |
297 input_method_auralinux_->OnCommit(base::ASCIIToUTF16("a")); | |
298 | |
299 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
300 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code); | |
301 EXPECT_EQ(0, delegate_->key_flags); | |
302 EXPECT_TRUE(client->called_insert_text); | |
303 EXPECT_FALSE(client->called_insert_char); | |
304 EXPECT_EQ(1U, client->result_text.size()); | |
305 EXPECT_EQ(key.GetCharacter(), client->result_text[0]); | |
306 | |
307 delete client; | |
308 client = new TextInputClientForTesting(TEXT_INPUT_TYPE_PASSWORD); | |
309 context_simple_->SetSyncMode(false); | |
310 context_simple_->SetEatKey(false); | |
311 | |
312 input_method_auralinux_->SetFocusedTextInputClient(client); | |
313 input_method_auralinux_->OnTextInputTypeChanged(client); | |
314 input_method_auralinux_->DispatchKeyEvent(key); | |
315 | |
316 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
317 EXPECT_EQ(VKEY_A, delegate_->key_code); | |
318 EXPECT_EQ(0, delegate_->key_flags); | |
319 EXPECT_FALSE(client->called_insert_text); | |
320 EXPECT_TRUE(client->called_insert_char); | |
321 EXPECT_EQ(1U, client->result_text.size()); | |
322 EXPECT_EQ(key.GetCharacter(), client->result_text[0]); | |
323 | |
324 delete client; | |
325 } | |
326 | |
327 TEST_F(InputMethodAuraLinuxTest, IBusUSTest) { | |
328 context_->SetSyncMode(false); | |
329 context_->SetEatKey(true); | |
330 | |
331 TextInputClientForTesting* client = new TextInputClientForTesting( | |
332 TEXT_INPUT_TYPE_TEXT); | |
333 input_method_auralinux_->SetFocusedTextInputClient(client); | |
334 input_method_auralinux_->OnTextInputTypeChanged(client); | |
335 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0); | |
336 key.set_character(L'a'); | |
337 input_method_auralinux_->DispatchKeyEvent(key); | |
338 | |
339 // iBus mutes the key down. | |
340 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
341 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code); | |
342 EXPECT_FALSE(client->called_insert_text); | |
343 EXPECT_FALSE(client->called_insert_char); | |
344 | |
345 // iBus simulates a faked key down and handle it in sync mode. | |
346 context_->SetSyncMode(true); | |
347 context_->AddAction("C:a"); | |
348 input_method_auralinux_->DispatchKeyEvent(key); | |
349 | |
350 EXPECT_EQ(VKEY_A, delegate_->key_code); | |
351 EXPECT_FALSE(client->called_insert_text); | |
352 EXPECT_TRUE(client->called_insert_char); | |
353 EXPECT_EQ(1U, client->result_text.size()); | |
354 EXPECT_EQ(key.GetCharacter(), client->result_text[0]); | |
355 | |
356 // iBus does NOT handle the key up. | |
357 client->Clear(); | |
358 context_->SetEatKey(false); | |
359 input_method_auralinux_->DispatchKeyEvent(KeyEvent( | |
360 ET_KEY_RELEASED, VKEY_A, 0)); | |
361 | |
362 EXPECT_EQ(ET_KEY_RELEASED, delegate_->key_type); | |
363 EXPECT_EQ(VKEY_A, delegate_->key_code); | |
364 EXPECT_FALSE(client->called_insert_text); | |
365 EXPECT_FALSE(client->called_insert_char); | |
366 | |
367 delete client; | |
368 } | |
369 | |
370 TEST_F(InputMethodAuraLinuxTest, IBusPinyinTest) { | |
371 context_->SetSyncMode(false); | |
372 context_->SetEatKey(true); | |
373 | |
374 TextInputClientForTesting* client = new TextInputClientForTesting( | |
375 TEXT_INPUT_TYPE_TEXT); | |
376 input_method_auralinux_->SetFocusedTextInputClient(client); | |
377 input_method_auralinux_->OnTextInputTypeChanged(client); | |
378 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0); | |
379 key.set_character(L'a'); | |
380 input_method_auralinux_->DispatchKeyEvent(key); | |
381 | |
382 // iBus mutes the key down. | |
383 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
384 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code); | |
385 EXPECT_FALSE(client->called_insert_text); | |
386 EXPECT_FALSE(client->called_insert_char); | |
387 delegate_->Clear(); | |
388 | |
389 // iBus issues a standalone set_composition action. | |
390 input_method_auralinux_->OnPreeditStart(); | |
391 CompositionText comp; | |
392 comp.text = base::ASCIIToUTF16("a"); | |
393 input_method_auralinux_->OnPreeditChanged(comp); | |
394 | |
395 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
396 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code); | |
397 EXPECT_TRUE(client->called_set_composition); | |
398 EXPECT_EQ(1U, client->composition_text.size()); | |
399 EXPECT_EQ(L'a', client->composition_text[0]); | |
400 delegate_->Clear(); | |
401 // iBus issues a commit text with composition after muting the space key down. | |
402 input_method_auralinux_->DispatchKeyEvent(KeyEvent( | |
403 ET_KEY_PRESSED, VKEY_SPACE, 0)); | |
404 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
405 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code); | |
406 EXPECT_FALSE(client->called_insert_text); | |
407 EXPECT_FALSE(client->called_insert_char); | |
408 delegate_->Clear(); | |
409 | |
410 input_method_auralinux_->OnPreeditEnd(); | |
411 input_method_auralinux_->OnCommit(base::ASCIIToUTF16("A")); | |
412 | |
413 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
414 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code); | |
415 EXPECT_TRUE(client->called_clear_composition); | |
416 EXPECT_TRUE(client->called_insert_text); | |
417 EXPECT_EQ(1U, client->result_text.size()); | |
418 EXPECT_EQ(L'A', client->result_text[0]); | |
419 | |
420 delete client; | |
421 } | |
422 | |
423 // crbug.com/463491 | |
424 TEST_F(InputMethodAuraLinuxTest, DeadKeyTest) { | |
425 context_simple_->SetSyncMode(true); | |
426 context_simple_->SetEatKey(true); | |
427 | |
428 TextInputClientForTesting* client = new TextInputClientForTesting( | |
429 TEXT_INPUT_TYPE_NONE); | |
430 input_method_auralinux_->SetFocusedTextInputClient(client); | |
431 input_method_auralinux_->OnTextInputTypeChanged(client); | |
432 KeyEvent dead_key(ET_KEY_PRESSED, VKEY_OEM_7, 0); | |
433 dead_key.set_character(L'\''); | |
434 input_method_auralinux_->DispatchKeyEvent(dead_key); | |
435 | |
436 // The single quote key is muted. | |
437 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
438 EXPECT_EQ(VKEY_OEM_7, delegate_->key_code); | |
439 EXPECT_FALSE(client->called_insert_text); | |
440 EXPECT_FALSE(client->called_insert_char); | |
441 delegate_->Clear(); | |
442 | |
443 context_simple_->AddAction("C:X"); | |
444 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0); | |
445 key.set_character(L'a'); | |
446 input_method_auralinux_->DispatchKeyEvent(key); | |
447 | |
448 // The following A key generates the accent key: รก. | |
449 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
450 EXPECT_EQ(VKEY_A, delegate_->key_code); | |
451 EXPECT_FALSE(client->called_insert_text); | |
452 EXPECT_TRUE(client->called_insert_char); | |
453 EXPECT_EQ(1U, client->result_text.size()); | |
454 EXPECT_EQ(L'X', client->result_text[0]); | |
455 | |
456 delete client; | |
457 } | |
458 | |
459 TEST_F(InputMethodAuraLinuxTest, MultiCommitsTest) { | |
460 context_->SetSyncMode(true); | |
461 context_->SetEatKey(true); | |
462 context_->AddAction("C:a"); | |
463 context_->AddAction("C:b"); | |
464 context_->AddAction("C:c"); | |
465 | |
466 TextInputClientForTesting* client = new TextInputClientForTesting( | |
467 TEXT_INPUT_TYPE_TEXT); | |
468 input_method_auralinux_->SetFocusedTextInputClient(client); | |
469 input_method_auralinux_->OnTextInputTypeChanged(client); | |
470 | |
471 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0); | |
472 key.set_character(L'a'); | |
473 input_method_auralinux_->DispatchKeyEvent(key); | |
474 | |
475 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
476 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code); | |
477 EXPECT_TRUE(client->called_insert_text); | |
478 EXPECT_FALSE(client->called_insert_char); | |
479 EXPECT_EQ(base::ASCIIToUTF16("abc"), client->result_text); | |
480 | |
481 delete client; | |
482 } | |
483 | |
484 TEST_F(InputMethodAuraLinuxTest, MixedCompositionAndCommitTest) { | |
485 context_->SetSyncMode(true); | |
486 context_->SetEatKey(true); | |
487 context_->AddAction("C:a"); | |
488 context_->AddAction("S"); | |
489 context_->AddAction("U:b"); | |
490 context_->AddAction("C:c"); | |
491 context_->AddAction("U:d"); | |
492 | |
493 TextInputClientForTesting* client = new TextInputClientForTesting( | |
494 TEXT_INPUT_TYPE_TEXT); | |
495 input_method_auralinux_->SetFocusedTextInputClient(client); | |
496 input_method_auralinux_->OnTextInputTypeChanged(client); | |
497 | |
498 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0); | |
499 key.set_character(L'a'); | |
500 input_method_auralinux_->DispatchKeyEvent(key); | |
501 | |
502 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type); | |
503 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code); | |
504 EXPECT_TRUE(client->called_set_composition); | |
505 EXPECT_TRUE(client->called_insert_text); | |
506 EXPECT_FALSE(client->called_insert_char); | |
507 EXPECT_EQ(base::ASCIIToUTF16("d"), client->composition_text); | |
508 EXPECT_EQ(base::ASCIIToUTF16("ac"), client->result_text); | |
509 | |
510 delete client; | |
511 } | |
512 | |
513 } // namespace | |
514 } // namespace ui | |
515 | |
OLD | NEW |