Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Side by Side Diff: ui/base/ime/input_method_auralinux_unittest.cc

Issue 1068093002: Refactoring for InputMethodAuraLinux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed nona's comments. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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++) {
yukawa 2015/04/08 22:26:49 for (const auto& action : actions_) {
Shu Chen 2015/04/09 01:16:33 Done.
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);
yukawa 2015/04/08 22:26:49 Wrong indent?
Shu Chen 2015/04/09 01:16:33 Done.
112 };
113
114 class InputMethodDelegateForTesting : public internal::InputMethodDelegate {
115 public:
116 InputMethodDelegateForTesting()
yukawa 2015/04/08 22:26:49 Wrong indent offset?
Shu Chen 2015/04/09 01:16:33 Done.
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),
yukawa 2015/04/08 22:26:49 Wrong indent offset?
Shu Chen 2015/04/09 01:16:33 Done. I've done git cl format.
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) {
yukawa 2015/04/08 22:26:49 Wrong indent offset?
Shu Chen 2015/04/09 01:16:33 Done.
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 scoped_ptr<TextInputClientForTesting> client(
250 new TextInputClientForTesting(TEXT_INPUT_TYPE_TEXT));
251 input_method_auralinux_->SetFocusedTextInputClient(client.get());
252 input_method_auralinux_->OnTextInputTypeChanged(client.get());
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 input_method_auralinux_->DetachTextInputClient(client.get());
267 client.reset(new TextInputClientForTesting(TEXT_INPUT_TYPE_PASSWORD));
268 context_simple_->SetSyncMode(true);
269 context_simple_->SetEatKey(false);
270
271 input_method_auralinux_->SetFocusedTextInputClient(client.get());
272 input_method_auralinux_->OnTextInputTypeChanged(client.get());
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
284 TEST_F(InputMethodAuraLinuxTest, BasicAsyncModeTest) {
285 context_->SetSyncMode(false);
286 context_->SetEatKey(true);
287
288 scoped_ptr<TextInputClientForTesting> client(
289 new TextInputClientForTesting(TEXT_INPUT_TYPE_TEXT));
290 input_method_auralinux_->SetFocusedTextInputClient(client.get());
291 input_method_auralinux_->OnTextInputTypeChanged(client.get());
292 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0);
293 key.set_character(L'a');
294 input_method_auralinux_->DispatchKeyEvent(key);
295 input_method_auralinux_->OnCommit(base::ASCIIToUTF16("a"));
296
297 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
298 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code);
299 EXPECT_EQ(0, delegate_->key_flags);
300 EXPECT_TRUE(client->called_insert_text);
301 EXPECT_FALSE(client->called_insert_char);
302 EXPECT_EQ(1U, client->result_text.size());
303 EXPECT_EQ(key.GetCharacter(), client->result_text[0]);
304
305 input_method_auralinux_->DetachTextInputClient(client.get());
306 client.reset(new TextInputClientForTesting(TEXT_INPUT_TYPE_PASSWORD));
307 context_simple_->SetSyncMode(false);
308 context_simple_->SetEatKey(false);
309
310 input_method_auralinux_->SetFocusedTextInputClient(client.get());
311 input_method_auralinux_->OnTextInputTypeChanged(client.get());
312 input_method_auralinux_->DispatchKeyEvent(key);
313
314 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
315 EXPECT_EQ(VKEY_A, delegate_->key_code);
316 EXPECT_EQ(0, delegate_->key_flags);
317 EXPECT_FALSE(client->called_insert_text);
318 EXPECT_TRUE(client->called_insert_char);
319 EXPECT_EQ(1U, client->result_text.size());
320 EXPECT_EQ(key.GetCharacter(), client->result_text[0]);
321 }
322
323 TEST_F(InputMethodAuraLinuxTest, IBusUSTest) {
324 context_->SetSyncMode(false);
325 context_->SetEatKey(true);
326
327 scoped_ptr<TextInputClientForTesting> client(
328 new TextInputClientForTesting(TEXT_INPUT_TYPE_TEXT));
329 input_method_auralinux_->SetFocusedTextInputClient(client.get());
330 input_method_auralinux_->OnTextInputTypeChanged(client.get());
331 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0);
332 key.set_character(L'a');
333 input_method_auralinux_->DispatchKeyEvent(key);
334
335 // iBus mutes the key down.
yukawa 2015/04/08 22:26:49 nit: I heard IBus is an official name. Ditto for
Shu Chen 2015/04/09 01:16:33 Done.
336 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
337 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code);
338 EXPECT_FALSE(client->called_insert_text);
339 EXPECT_FALSE(client->called_insert_char);
340
341 // iBus simulates a faked key down and handle it in sync mode.
342 context_->SetSyncMode(true);
343 context_->AddAction("C:a");
344 input_method_auralinux_->DispatchKeyEvent(key);
345
346 EXPECT_EQ(VKEY_A, delegate_->key_code);
347 EXPECT_FALSE(client->called_insert_text);
348 EXPECT_TRUE(client->called_insert_char);
349 EXPECT_EQ(1U, client->result_text.size());
350 EXPECT_EQ(key.GetCharacter(), client->result_text[0]);
351
352 // iBus does NOT handle the key up.
353 client->Clear();
354 context_->SetEatKey(false);
355 input_method_auralinux_->DispatchKeyEvent(KeyEvent(
356 ET_KEY_RELEASED, VKEY_A, 0));
357
358 EXPECT_EQ(ET_KEY_RELEASED, delegate_->key_type);
359 EXPECT_EQ(VKEY_A, delegate_->key_code);
360 EXPECT_FALSE(client->called_insert_text);
361 EXPECT_FALSE(client->called_insert_char);
362 }
363
364 TEST_F(InputMethodAuraLinuxTest, IBusPinyinTest) {
365 context_->SetSyncMode(false);
366 context_->SetEatKey(true);
367
368 scoped_ptr<TextInputClientForTesting> client(
369 new TextInputClientForTesting(TEXT_INPUT_TYPE_TEXT));
370 input_method_auralinux_->SetFocusedTextInputClient(client.get());
371 input_method_auralinux_->OnTextInputTypeChanged(client.get());
372 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0);
373 key.set_character(L'a');
374 input_method_auralinux_->DispatchKeyEvent(key);
375
376 // iBus mutes the key down.
377 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
378 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code);
379 EXPECT_FALSE(client->called_insert_text);
380 EXPECT_FALSE(client->called_insert_char);
381 delegate_->Clear();
382
383 // iBus issues a standalone set_composition action.
384 input_method_auralinux_->OnPreeditStart();
385 CompositionText comp;
386 comp.text = base::ASCIIToUTF16("a");
387 input_method_auralinux_->OnPreeditChanged(comp);
388
389 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
390 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code);
391 EXPECT_TRUE(client->called_set_composition);
392 EXPECT_EQ(1U, client->composition_text.size());
393 EXPECT_EQ(L'a', client->composition_text[0]);
394 delegate_->Clear();
395 // iBus issues a commit text with composition after muting the space key down.
396 input_method_auralinux_->DispatchKeyEvent(KeyEvent(
397 ET_KEY_PRESSED, VKEY_SPACE, 0));
398 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
399 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code);
400 EXPECT_FALSE(client->called_insert_text);
401 EXPECT_FALSE(client->called_insert_char);
402 delegate_->Clear();
403
404 input_method_auralinux_->OnPreeditEnd();
405 input_method_auralinux_->OnCommit(base::ASCIIToUTF16("A"));
406
407 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
408 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code);
409 EXPECT_TRUE(client->called_clear_composition);
410 EXPECT_TRUE(client->called_insert_text);
411 EXPECT_EQ(1U, client->result_text.size());
412 EXPECT_EQ(L'A', client->result_text[0]);
413 }
414
415 // crbug.com/463491
416 TEST_F(InputMethodAuraLinuxTest, DeadKeyTest) {
417 context_simple_->SetSyncMode(true);
418 context_simple_->SetEatKey(true);
419
420 scoped_ptr<TextInputClientForTesting> client(
421 new TextInputClientForTesting(TEXT_INPUT_TYPE_NONE));
422 input_method_auralinux_->SetFocusedTextInputClient(client.get());
423 input_method_auralinux_->OnTextInputTypeChanged(client.get());
424 KeyEvent dead_key(ET_KEY_PRESSED, VKEY_OEM_7, 0);
425 dead_key.set_character(L'\'');
426 input_method_auralinux_->DispatchKeyEvent(dead_key);
427
428 // The single quote key is muted.
429 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
430 EXPECT_EQ(VKEY_OEM_7, delegate_->key_code);
431 EXPECT_FALSE(client->called_insert_text);
432 EXPECT_FALSE(client->called_insert_char);
433 delegate_->Clear();
434
435 context_simple_->AddAction("C:X");
436 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0);
437 key.set_character(L'a');
438 input_method_auralinux_->DispatchKeyEvent(key);
439
440 // The following A key generates the accent key: รก.
441 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
442 EXPECT_EQ(VKEY_A, delegate_->key_code);
443 EXPECT_FALSE(client->called_insert_text);
444 EXPECT_TRUE(client->called_insert_char);
445 EXPECT_EQ(1U, client->result_text.size());
446 EXPECT_EQ(L'X', client->result_text[0]);
447 }
448
449 TEST_F(InputMethodAuraLinuxTest, MultiCommitsTest) {
450 context_->SetSyncMode(true);
451 context_->SetEatKey(true);
452 context_->AddAction("C:a");
453 context_->AddAction("C:b");
454 context_->AddAction("C:c");
455
456 scoped_ptr<TextInputClientForTesting> client(
457 new TextInputClientForTesting(TEXT_INPUT_TYPE_TEXT));
458 input_method_auralinux_->SetFocusedTextInputClient(client.get());
459 input_method_auralinux_->OnTextInputTypeChanged(client.get());
460
461 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0);
462 key.set_character(L'a');
463 input_method_auralinux_->DispatchKeyEvent(key);
464
465 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
466 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code);
467 EXPECT_TRUE(client->called_insert_text);
468 EXPECT_FALSE(client->called_insert_char);
469 EXPECT_EQ(base::ASCIIToUTF16("abc"), client->result_text);
470 }
471
472 TEST_F(InputMethodAuraLinuxTest, MixedCompositionAndCommitTest) {
473 context_->SetSyncMode(true);
474 context_->SetEatKey(true);
475 context_->AddAction("C:a");
476 context_->AddAction("S");
477 context_->AddAction("U:b");
478 context_->AddAction("C:c");
479 context_->AddAction("U:d");
480
481 scoped_ptr<TextInputClientForTesting> client(
482 new TextInputClientForTesting(TEXT_INPUT_TYPE_TEXT));
483 input_method_auralinux_->SetFocusedTextInputClient(client.get());
484 input_method_auralinux_->OnTextInputTypeChanged(client.get());
485
486 KeyEvent key(ET_KEY_PRESSED, VKEY_A, 0);
487 key.set_character(L'a');
488 input_method_auralinux_->DispatchKeyEvent(key);
489
490 EXPECT_EQ(ET_KEY_PRESSED, delegate_->key_type);
491 EXPECT_EQ(VKEY_PROCESSKEY, delegate_->key_code);
492 EXPECT_TRUE(client->called_set_composition);
493 EXPECT_TRUE(client->called_insert_text);
494 EXPECT_FALSE(client->called_insert_char);
495 EXPECT_EQ(base::ASCIIToUTF16("d"), client->composition_text);
496 EXPECT_EQ(base::ASCIIToUTF16("ac"), client->result_text);
497 }
498
499 } // namespace
500 } // namespace ui
501
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698