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

Side by Side Diff: chrome/browser/chromeos/input_method/input_method_manager_impl_unittest.cc

Issue 9999018: chrome/browser/chromeos/input_method/ refactoring [part 6 of 6] (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review fix Created 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/input_method/input_method_manager_impl.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/chromeos/language_preferences.h"
Seigo Nonaka 2012/04/13 09:44:22 nit: alphabetic order if possible.
Yusuke Sato 2012/04/16 05:54:31 Done.
12 #include "chrome/browser/chromeos/input_method/mock_candidate_window.h"
13 #include "chrome/browser/chromeos/input_method/mock_ibus_controller.h"
14 #include "chrome/browser/chromeos/input_method/mock_xkeyboard.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/accelerators/accelerator.h"
17 #include "ui/base/keycodes/keyboard_codes.h"
18
19 namespace chromeos {
20
21 extern const char* kExtensionImePrefix;
22
23 namespace input_method {
24 namespace {
25
26 class InputMethodManagerImplTest : public testing::Test {
27 public:
28 InputMethodManagerImplTest()
29 : controller_(NULL),
30 candidate_window_controller_(NULL) {
31 }
32 virtual ~InputMethodManagerImplTest() {}
33
34 virtual void SetUp() {
35 manager_.reset(InputMethodManagerImpl::GetInstanceForTesting());
36 controller_ = new MockIBusController;
37 manager_->SetIBusControllerForTesting(controller_);
38 candidate_window_controller_ = new MockCandidateWindowController;
39 manager_->SetCandidateWindowControllerForTesting(
40 candidate_window_controller_);
41 xkeyboard_ = new MockXKeyboard;
42 manager_->SetXKeyboardForTesting(xkeyboard_);
43 }
44
45 virtual void TearDown() {
46 manager_.reset();
47 controller_ = NULL;
48 candidate_window_controller_ = NULL;
49 xkeyboard_ = NULL;
50 }
51
52 void SetHardwareKeyboardLayout(const std::string& input_method_id) {
53 manager_->GetInputMethodUtil()->SetHardwareInputMethodIdForTesting(
54 input_method_id);
55 }
56
57 protected:
58 scoped_ptr<InputMethodManagerImpl> manager_;
59 MockIBusController* controller_;
60 MockCandidateWindowController* candidate_window_controller_;
61 MockXKeyboard* xkeyboard_;
62
63 private:
64 DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImplTest);
65 };
66
67 class TestObserver : public InputMethodManager::Observer {
68 public:
69 TestObserver()
70 : input_method_changed_count_(0),
71 input_method_property_changed_count_(0) {
72 }
73 virtual ~TestObserver() {}
74
75 virtual void InputMethodChanged(InputMethodManager* manager) OVERRIDE {
76 ++input_method_changed_count_;
77 }
78 virtual void InputMethodPropertyChanged(
79 InputMethodManager* manager) OVERRIDE {
80 ++input_method_property_changed_count_;
81 }
82
83 int input_method_changed_count_;
84 int input_method_property_changed_count_;
85
86 private:
87 DISALLOW_COPY_AND_ASSIGN(TestObserver);
88 };
89
90 class TestCandidateWindowObserver
91 : public InputMethodManager::CandidateWindowObserver {
92 public:
93 TestCandidateWindowObserver()
94 : candidate_window_opened_count_(0),
95 candidate_window_closed_count_(0) {
96 }
97 virtual ~TestCandidateWindowObserver() {}
98
99 virtual void CandidateWindowOpened(InputMethodManager* manager) OVERRIDE {
100 ++candidate_window_opened_count_;
101 }
102 virtual void CandidateWindowClosed(InputMethodManager* manager) OVERRIDE {
103 ++candidate_window_closed_count_;
104 }
105
106 int candidate_window_opened_count_;
107 int candidate_window_closed_count_;
108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(TestCandidateWindowObserver);
111 };
112
113 } // namespace
114
115 TEST_F(InputMethodManagerImplTest, TestGetXKeyboard) {
116 EXPECT_TRUE(manager_->GetXKeyboard());
117 EXPECT_EQ(xkeyboard_, manager_->GetXKeyboard());
118 }
119
120 TEST_F(InputMethodManagerImplTest, TestGetInputMethodUtil) {
121 EXPECT_TRUE(manager_->GetInputMethodUtil());
122 SetHardwareKeyboardLayout("xkb:fr::fra"); // check this does not crash.
123 }
124
125 TEST_F(InputMethodManagerImplTest, TestCandidateWindowObserver) {
126 TestCandidateWindowObserver observer;
127 candidate_window_controller_->NotifyCandidateWindowOpened(); // nop
128 candidate_window_controller_->NotifyCandidateWindowClosed(); // nop
129 manager_->AddCandidateWindowObserver(&observer);
130 candidate_window_controller_->NotifyCandidateWindowOpened();
131 EXPECT_EQ(1, observer.candidate_window_opened_count_);
132 candidate_window_controller_->NotifyCandidateWindowClosed();
133 EXPECT_EQ(1, observer.candidate_window_closed_count_);
134 candidate_window_controller_->NotifyCandidateWindowOpened();
135 EXPECT_EQ(2, observer.candidate_window_opened_count_);
136 candidate_window_controller_->NotifyCandidateWindowClosed();
137 EXPECT_EQ(2, observer.candidate_window_closed_count_);
138 manager_->RemoveCandidateWindowObserver(&observer);
139 }
140
141 TEST_F(InputMethodManagerImplTest, TestObserver) {
142 // For http://crbug.com/19655#c11 - (3). browser_state_monitor_unittest.cc is
143 // also for the scenario.
144 TestObserver observer;
145 manager_->AddObserver(&observer);
146 EXPECT_EQ(0, observer.input_method_changed_count_);
147 manager_->EnableLayouts("en-US", "xkb:us::eng");
148 EXPECT_EQ(1, observer.input_method_changed_count_);
149 EXPECT_EQ(1, observer.input_method_property_changed_count_);
150 manager_->ChangeInputMethod("xkb:us:dvorak:eng");
151 EXPECT_EQ(2, observer.input_method_changed_count_);
152 EXPECT_EQ(2, observer.input_method_property_changed_count_);
153 manager_->ChangeInputMethod("xkb:us:dvorak:eng");
154 // The observer is always notified even when the same input method ID is
155 // passed to ChangeInputMethod() more than twice.
156 EXPECT_EQ(3, observer.input_method_changed_count_);
157 EXPECT_EQ(3, observer.input_method_property_changed_count_);
158
159 controller_->NotifyPropertyChangedForTesting();
160 EXPECT_EQ(4, observer.input_method_property_changed_count_);
161 controller_->NotifyPropertyChangedForTesting();
162 EXPECT_EQ(5, observer.input_method_property_changed_count_);
163 manager_->RemoveObserver(&observer);
164 }
165
166 TEST_F(InputMethodManagerImplTest, TestGetSupportedInputMethods) {
167 scoped_ptr<InputMethodDescriptors> methods(
168 manager_->GetSupportedInputMethods());
169 ASSERT_TRUE(methods.get());
170 // Try to find random 4-5 layuts and IMEs to make sure the returned list is
171 // correct.
172 const InputMethodDescriptor* id_to_find =
173 manager_->GetInputMethodUtil()->GetInputMethodDescriptorFromId("mozc");
174 EXPECT_NE(methods->end(),
175 std::find(methods->begin(), methods->end(), *id_to_find));
176 id_to_find = manager_->GetInputMethodUtil()->GetInputMethodDescriptorFromId(
177 "mozc-chewing");
178 id_to_find = manager_->GetInputMethodUtil()->GetInputMethodDescriptorFromId(
179 "xkb:us::eng");
180 EXPECT_NE(methods->end(),
181 std::find(methods->begin(), methods->end(), *id_to_find));
182 id_to_find = manager_->GetInputMethodUtil()->GetInputMethodDescriptorFromId(
183 "xkb:us:dvorak:eng");
184 EXPECT_NE(methods->end(),
185 std::find(methods->begin(), methods->end(), *id_to_find));
186 id_to_find = manager_->GetInputMethodUtil()->GetInputMethodDescriptorFromId(
187 "xkb:fr::fra");
188 EXPECT_NE(methods->end(),
189 std::find(methods->begin(), methods->end(), *id_to_find));
190 }
191
192 TEST_F(InputMethodManagerImplTest, TestEnableLayouts) {
193 // Currently 5 keyboard layouts are supported for en-US, and 1 for ja. See
194 // ibus_input_method.txt.
195 manager_->EnableLayouts("en-US", "");
196 EXPECT_EQ(5U, manager_->GetNumActiveInputMethods());
197 {
198 // For http://crbug.com/19655#c11 - (1)
199 scoped_ptr<InputMethodDescriptors> methods(
200 manager_->GetActiveInputMethods());
201 const InputMethodDescriptor* id_to_find =
202 manager_->GetInputMethodUtil()->GetInputMethodDescriptorFromId(
203 "english-m"); // The "English Mystery" IME.
204 EXPECT_EQ(methods->end(),
205 std::find(methods->begin(), methods->end(), *id_to_find));
206 }
207 // For http://crbug.com/19655#c11 - (2)
208 EXPECT_EQ(0, controller_->start_count_);
209
210 // For http://crbug.com/19655#c11 - (5)
211 // The hardware keyboard layout "xkb:us::eng" is always active, hence 2U.
212 manager_->EnableLayouts("ja", ""); // Japanese
213 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
214 EXPECT_EQ(0, controller_->start_count_);
215 }
216
217 TEST_F(InputMethodManagerImplTest, TestEnableLayoutsNonUsHardwareKeyboard) {
218 // The physical layout is French.
219 SetHardwareKeyboardLayout("xkb:fr::fra");
220 manager_->EnableLayouts("en-US", "");
221 EXPECT_EQ(6U, manager_->GetNumActiveInputMethods()); // 5 + French
222 // The physical layout is Japanese.
223 SetHardwareKeyboardLayout("xkb:jp::jpn");
224 manager_->EnableLayouts("ja", "");
225 // "xkb:us::eng" is not needed, hence 1.
226 EXPECT_EQ(1U, manager_->GetNumActiveInputMethods());
227 }
228
229 TEST_F(InputMethodManagerImplTest, TestActiveInputMethods) {
230 manager_->EnableLayouts("ko", ""); // Korean
231 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
232 scoped_ptr<InputMethodDescriptors> methods(
233 manager_->GetActiveInputMethods());
234 ASSERT_TRUE(methods.get());
235 EXPECT_EQ(2U, methods->size());
236 const InputMethodDescriptor* id_to_find =
237 manager_->GetInputMethodUtil()->GetInputMethodDescriptorFromId(
238 "xkb:us::eng");
239 EXPECT_NE(methods->end(),
240 std::find(methods->begin(), methods->end(), *id_to_find));
241 id_to_find = manager_->GetInputMethodUtil()->GetInputMethodDescriptorFromId(
242 "xkb:kr:kr104:kor");
243 EXPECT_NE(methods->end(),
244 std::find(methods->begin(), methods->end(), *id_to_find));
245 }
246
247 TEST_F(InputMethodManagerImplTest, TestSetInputMethodConfig) {
248 InputMethodConfigValue config;
249 config.type = InputMethodConfigValue::kValueTypeString;
250 config.string_value = "string";
251 EXPECT_EQ(0, controller_->set_input_method_config_internal_count_);
252 EXPECT_TRUE(manager_->SetInputMethodConfig("section", "name", config));
253 EXPECT_EQ(1, controller_->set_input_method_config_internal_count_);
254 EXPECT_EQ("section",
255 controller_->set_input_method_config_internal_key_.first);
256 EXPECT_EQ("name",
257 controller_->set_input_method_config_internal_key_.second);
258 EXPECT_EQ(config.type,
259 controller_->set_input_method_config_internal_value_.type);
260 EXPECT_EQ(config.string_value,
261 controller_->set_input_method_config_internal_value_.string_value);
262
263 // SetInputMethodConfig should be no-op in STATE_TERMINATING.
264 manager_->SetState(InputMethodManager::STATE_TERMINATING);
265 EXPECT_FALSE(manager_->SetInputMethodConfig("section", "name", config));
266 EXPECT_EQ(1, controller_->set_input_method_config_internal_count_);
267 }
268
269 TEST_F(InputMethodManagerImplTest, TestEnableTwoLayouts) {
270 // For http://crbug.com/19655#c11 - (8), step 6.
271 TestObserver observer;
272 manager_->AddObserver(&observer);
273 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
274 std::vector<std::string> ids;
275 ids.push_back("xkb:us:dvorak:eng");
276 ids.push_back("xkb:us:colemak:eng");
277 EXPECT_TRUE(manager_->EnableInputMethods(ids));
278 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
279 // Since all the IDs added avobe are keyboard layouts, Start() should not be
280 // called.
281 EXPECT_EQ(0, controller_->start_count_);
282 EXPECT_EQ(1, observer.input_method_changed_count_);
283 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
284 // Disable Dvorak.
285 ids.erase(ids.begin());
286 EXPECT_TRUE(manager_->EnableInputMethods(ids));
287 EXPECT_EQ(1U, manager_->GetNumActiveInputMethods());
288 EXPECT_EQ(2, observer.input_method_changed_count_);
289 EXPECT_EQ(ids[0], // colemak
290 manager_->GetCurrentInputMethod().id());
291 manager_->RemoveObserver(&observer);
292 }
293
294 TEST_F(InputMethodManagerImplTest, TestEnableThreeLayouts) {
295 // For http://crbug.com/19655#c11 - (9).
296 TestObserver observer;
297 manager_->AddObserver(&observer);
298 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
299 std::vector<std::string> ids;
300 ids.push_back("xkb:us::eng");
301 ids.push_back("xkb:us:dvorak:eng");
302 ids.push_back("xkb:us:colemak:eng");
303 EXPECT_TRUE(manager_->EnableInputMethods(ids));
304 EXPECT_EQ(3U, manager_->GetNumActiveInputMethods());
305 EXPECT_EQ(1, observer.input_method_changed_count_);
306 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
307 // Switch to Dvorak.
308 manager_->SwitchToNextInputMethod();
309 EXPECT_EQ(2, observer.input_method_changed_count_);
310 EXPECT_EQ(ids[1], manager_->GetCurrentInputMethod().id());
311 // Disable Dvorak.
312 ids.erase(ids.begin() + 1);
313 EXPECT_TRUE(manager_->EnableInputMethods(ids));
314 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
315 EXPECT_EQ(3, observer.input_method_changed_count_);
316 EXPECT_EQ(ids[0], // US Qwerty
317 manager_->GetCurrentInputMethod().id());
318 manager_->RemoveObserver(&observer);
319 }
320
321 TEST_F(InputMethodManagerImplTest, TestEnableLayoutAndIme) {
322 // For http://crbug.com/19655#c11 - (10).
323 TestObserver observer;
324 manager_->AddObserver(&observer);
325 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
326 std::vector<std::string> ids;
327 ids.push_back("xkb:us:dvorak:eng");
328 ids.push_back("mozc-dv");
329 EXPECT_TRUE(manager_->EnableInputMethods(ids));
330 EXPECT_EQ(1, controller_->start_count_);
331 EXPECT_EQ(1, observer.input_method_changed_count_);
332 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
333
334 // Switch to Mozc
335 manager_->SwitchToNextInputMethod();
336 EXPECT_EQ(2, observer.input_method_changed_count_);
337 EXPECT_EQ(ids[1], manager_->GetCurrentInputMethod().id());
338
339 // Disable Mozc.
340 ids.erase(ids.begin() + 1);
341 EXPECT_TRUE(manager_->EnableInputMethods(ids));
342 EXPECT_EQ(1U, manager_->GetNumActiveInputMethods());
343 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
344 // Currently, to work around a crash issue at crosbug.com/27051,
345 // controller_->Stop(); is NOT called when all IMEs are disabled.
346 EXPECT_EQ(0, controller_->stop_count_);
347
348 // However, IME should always be stopped on shutdown.
349 manager_->SetState(InputMethodManager::STATE_TERMINATING);
350 EXPECT_EQ(1, controller_->stop_count_);
351 manager_->RemoveObserver(&observer);
352 }
353
354 TEST_F(InputMethodManagerImplTest, TestEnableLayoutAndIme2) {
355 // For http://crbug.com/19655#c11 - (11).
356 TestObserver observer;
357 manager_->AddObserver(&observer);
358 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
359 std::vector<std::string> ids;
360 ids.push_back("xkb:us:dvorak:eng");
361 ids.push_back("mozc-dv");
362 EXPECT_TRUE(manager_->EnableInputMethods(ids));
363 EXPECT_EQ(1, controller_->start_count_);
364 EXPECT_EQ(1, observer.input_method_changed_count_);
365 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
366
367 // Disable Dvorak.
368 ids.erase(ids.begin());
369 EXPECT_TRUE(manager_->EnableInputMethods(ids));
370 EXPECT_EQ(1U, manager_->GetNumActiveInputMethods());
371 EXPECT_EQ(ids[0], // Mozc
372 manager_->GetCurrentInputMethod().id());
373 manager_->RemoveObserver(&observer);
374 }
375
376 TEST_F(InputMethodManagerImplTest, TestEnableImes) {
377 TestObserver observer;
378 manager_->AddObserver(&observer);
379 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
380 std::vector<std::string> ids;
381 ids.push_back("mozc-chewing");
382 ids.push_back("mozc");
383 EXPECT_TRUE(manager_->EnableInputMethods(ids));
384 EXPECT_EQ(1, controller_->start_count_);
385 EXPECT_EQ(1, observer.input_method_changed_count_);
386 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
387 manager_->RemoveObserver(&observer);
388 }
389
390 TEST_F(InputMethodManagerImplTest, TestEnableUnknownIds) {
391 TestObserver observer;
392 manager_->AddObserver(&observer);
393 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
394 std::vector<std::string> ids;
395 ids.push_back("xkb:tl::tlh"); // Klingon, which is not supported.
396 ids.push_back("unknown-super-cool-ime");
397 EXPECT_FALSE(manager_->EnableInputMethods(ids));
398
399 // TODO(yusukes): Should we fall back to the hardware keyboard layout in this
400 // case?
401 EXPECT_EQ(0, observer.input_method_changed_count_);
402
403 manager_->RemoveObserver(&observer);
404 }
405
406 TEST_F(InputMethodManagerImplTest, TestEnableLayoutsThenLock) {
407 // For http://crbug.com/19655#c11 - (14).
408 TestObserver observer;
409 manager_->AddObserver(&observer);
410 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
411 std::vector<std::string> ids;
412 ids.push_back("xkb:us::eng");
413 ids.push_back("xkb:us:dvorak:eng");
414 EXPECT_TRUE(manager_->EnableInputMethods(ids));
415 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
416 EXPECT_EQ(1, observer.input_method_changed_count_);
417 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
418
419 // Switch to Dvorak.
420 manager_->SwitchToNextInputMethod();
421 EXPECT_EQ(2, observer.input_method_changed_count_);
422 EXPECT_EQ(ids[1], manager_->GetCurrentInputMethod().id());
423
424 // Lock screen
425 manager_->SetState(InputMethodManager::STATE_LOCK_SCREEN);
426 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
427 EXPECT_EQ(ids[1], // still Dvorak
428 manager_->GetCurrentInputMethod().id());
429 // Switch back to Qwerty.
430 manager_->SwitchToNextInputMethod();
431 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
432
433 // Unlock screen. The original state, Dvorak, is restored.
434 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
435 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
436 EXPECT_EQ(ids[1], manager_->GetCurrentInputMethod().id());
437
438 manager_->RemoveObserver(&observer);
439 }
440
441 TEST_F(InputMethodManagerImplTest, TestEnableLayoutAndImeThenLock) {
442 // For http://crbug.com/19655#c11 - (15).
443 TestObserver observer;
444 manager_->AddObserver(&observer);
445 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
446 std::vector<std::string> ids;
447 ids.push_back("xkb:us:dvorak:eng");
448 ids.push_back("mozc-dv");
449 ids.push_back("mozc-chewing");
450 EXPECT_TRUE(manager_->EnableInputMethods(ids));
451 EXPECT_EQ(3U, manager_->GetNumActiveInputMethods());
452 EXPECT_EQ(1, observer.input_method_changed_count_);
453 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
454
455 // Switch to Mozc.
456 manager_->SwitchToNextInputMethod();
457 EXPECT_EQ(2, observer.input_method_changed_count_);
458 EXPECT_EQ(ids[1], manager_->GetCurrentInputMethod().id());
459
460 // Lock screen
461 manager_->SetState(InputMethodManager::STATE_LOCK_SCREEN);
462 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods()); // Qwerty+Dvorak.
463 EXPECT_EQ("xkb:us:dvorak:eng",
464 manager_->GetCurrentInputMethod().id());
465 // controller_->Stop() should never be called when the screen is locked even
466 // after crosbug.com/27051 is fixed.
467 EXPECT_EQ(0, controller_->stop_count_);
468 manager_->SwitchToNextInputMethod();
469 EXPECT_EQ("xkb:us::eng", // The hardware keyboard layout.
470 manager_->GetCurrentInputMethod().id());
471
472 // Unlock screen. The original state, mozc-dv, is restored.
473 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
474 EXPECT_EQ(3U, manager_->GetNumActiveInputMethods()); // Dvorak and 2 IMEs.
475 EXPECT_EQ(ids[1], manager_->GetCurrentInputMethod().id());
476
477 manager_->RemoveObserver(&observer);
478 }
479
480 TEST_F(InputMethodManagerImplTest, TestXkbSetting) {
481 // For http://crbug.com/19655#c11 - (8), step 7-11.
482 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
483 std::vector<std::string> ids;
484 ids.push_back("xkb:us:dvorak:eng");
485 ids.push_back("xkb:us:colemak:eng");
486 ids.push_back("mozc-jp");
487 ids.push_back("mozc");
488 EXPECT_TRUE(manager_->EnableInputMethods(ids));
489 EXPECT_EQ(4U, manager_->GetNumActiveInputMethods());
490 EXPECT_EQ(1, xkeyboard_->set_current_keyboard_layout_by_name_count_);
491 // See input_methods.txt for an expected XKB layout name.
492 EXPECT_EQ("us(dvorak)", xkeyboard_->last_layout_);
493 manager_->SwitchToNextInputMethod();
494 EXPECT_EQ(2, xkeyboard_->set_current_keyboard_layout_by_name_count_);
495 EXPECT_EQ("us(colemak)", xkeyboard_->last_layout_);
496 manager_->SwitchToNextInputMethod();
497 EXPECT_EQ(3, xkeyboard_->set_current_keyboard_layout_by_name_count_);
498 EXPECT_EQ("jp", xkeyboard_->last_layout_);
499 manager_->SwitchToNextInputMethod();
500 EXPECT_EQ(4, xkeyboard_->set_current_keyboard_layout_by_name_count_);
501 EXPECT_EQ("us", xkeyboard_->last_layout_);
502 manager_->SwitchToNextInputMethod();
503 EXPECT_EQ(5, xkeyboard_->set_current_keyboard_layout_by_name_count_);
504 EXPECT_EQ("us(dvorak)", xkeyboard_->last_layout_);
505 // Disable Dvorak.
506 ids.erase(ids.begin());
507 EXPECT_TRUE(manager_->EnableInputMethods(ids));
508 EXPECT_EQ(3U, manager_->GetNumActiveInputMethods());
509 EXPECT_EQ(6, xkeyboard_->set_current_keyboard_layout_by_name_count_);
510 EXPECT_EQ("us(colemak)", xkeyboard_->last_layout_);
511 }
512
513 TEST_F(InputMethodManagerImplTest, TestActivateInputMethodProperty) {
514 manager_->ActivateInputMethodProperty("key");
515 EXPECT_EQ(1, controller_->activate_input_method_property_count_);
516 EXPECT_EQ("key", controller_->activate_input_method_property_key_);
517 manager_->ActivateInputMethodProperty("key2");
518 EXPECT_EQ(2, controller_->activate_input_method_property_count_);
519 EXPECT_EQ("key2", controller_->activate_input_method_property_key_);
520 }
521
522 TEST_F(InputMethodManagerImplTest, TestGetCurrentInputMethodProperties) {
523 EXPECT_TRUE(manager_->GetCurrentInputMethodProperties().empty());
524
525 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
526 std::vector<std::string> ids;
527 ids.push_back("xkb:us::eng");
528 ids.push_back("mozc");
529 EXPECT_TRUE(manager_->EnableInputMethods(ids));
530 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
531 EXPECT_TRUE(manager_->GetCurrentInputMethodProperties().empty());
532 manager_->ChangeInputMethod("mozc");
533
534 InputMethodPropertyList current_property_list;
535 current_property_list.push_back(InputMethodProperty("key",
536 "label",
537 false,
538 false,
539 -1));
540 controller_->SetCurrentPropertiesForTesting(current_property_list);
541 controller_->NotifyPropertyChangedForTesting();
542
543 ASSERT_EQ(1U, manager_->GetCurrentInputMethodProperties().size());
544 EXPECT_EQ("key", manager_->GetCurrentInputMethodProperties().at(0).key);
545
546 manager_->ChangeInputMethod("xkb:us::eng");
547 EXPECT_TRUE(manager_->GetCurrentInputMethodProperties().empty());
548
549 // Delayed asynchronous property update signal from the Mozc IME.
550 controller_->NotifyPropertyChangedForTesting();
551 // When XKB layout is in use, GetCurrentInputMethodProperties() should always
552 // return an empty list.
553 EXPECT_TRUE(manager_->GetCurrentInputMethodProperties().empty());
554 }
555
556 TEST_F(InputMethodManagerImplTest, TestGetCurrentInputMethodPropertiesTwoImes) {
557 EXPECT_TRUE(manager_->GetCurrentInputMethodProperties().empty());
558
559 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
560 std::vector<std::string> ids;
561 ids.push_back("mozc"); // Japanese
562 ids.push_back("mozc-chewing"); // T-Chinese
563 EXPECT_TRUE(manager_->EnableInputMethods(ids));
564 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
565 EXPECT_TRUE(manager_->GetCurrentInputMethodProperties().empty());
566
567 InputMethodPropertyList current_property_list;
568 current_property_list.push_back(InputMethodProperty("key-mozc",
569 "label",
570 false,
571 false,
572 -1));
573 controller_->SetCurrentPropertiesForTesting(current_property_list);
574 controller_->NotifyPropertyChangedForTesting();
575
576 ASSERT_EQ(1U, manager_->GetCurrentInputMethodProperties().size());
577 EXPECT_EQ("key-mozc", manager_->GetCurrentInputMethodProperties().at(0).key);
578
579 manager_->ChangeInputMethod("mozc-chewing");
580 // Since the IME is changed, the property for mozc Japanese should be hidden.
581 EXPECT_TRUE(manager_->GetCurrentInputMethodProperties().empty());
582
583 // Asynchronous property update signal from mozc-chewing.
584 current_property_list.clear();
585 current_property_list.push_back(InputMethodProperty("key-chewing",
586 "label",
587 false,
588 false,
589 -1));
590 controller_->SetCurrentPropertiesForTesting(current_property_list);
591 controller_->NotifyPropertyChangedForTesting();
592 ASSERT_EQ(1U, manager_->GetCurrentInputMethodProperties().size());
593 EXPECT_EQ("key-chewing",
594 manager_->GetCurrentInputMethodProperties().at(0).key);
595 }
596
597 TEST_F(InputMethodManagerImplTest, TestNextInputMethod) {
598 // For http://crbug.com/19655#c11 - (1)
599 manager_->EnableLayouts("en-US", "xkb:us::eng");
600 EXPECT_EQ(5U, manager_->GetNumActiveInputMethods());
601 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
602 manager_->SwitchToNextInputMethod();
603 EXPECT_EQ("xkb:us:intl:eng", manager_->GetCurrentInputMethod().id());
604 manager_->SwitchToNextInputMethod();
605 EXPECT_EQ("xkb:us:altgr-intl:eng", manager_->GetCurrentInputMethod().id());
606 manager_->SwitchToNextInputMethod();
607 EXPECT_EQ("xkb:us:dvorak:eng", manager_->GetCurrentInputMethod().id());
608 manager_->SwitchToNextInputMethod();
609 EXPECT_EQ("xkb:us:colemak:eng", manager_->GetCurrentInputMethod().id());
610 manager_->SwitchToNextInputMethod();
611 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
612 }
613
614 TEST_F(InputMethodManagerImplTest, TestPreviousInputMethod) {
615 manager_->EnableLayouts("en-US", "xkb:us::eng");
616 EXPECT_EQ(5U, manager_->GetNumActiveInputMethods());
617 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
618 manager_->SwitchToNextInputMethod();
619 EXPECT_EQ("xkb:us:intl:eng", manager_->GetCurrentInputMethod().id());
620 manager_->SwitchToPreviousInputMethod();
621 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
622 manager_->SwitchToPreviousInputMethod();
623 EXPECT_EQ("xkb:us:intl:eng", manager_->GetCurrentInputMethod().id());
624 manager_->SwitchToPreviousInputMethod();
625 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
626 manager_->SwitchToNextInputMethod();
627 EXPECT_EQ("xkb:us:intl:eng", manager_->GetCurrentInputMethod().id());
628 manager_->SwitchToNextInputMethod();
629 EXPECT_EQ("xkb:us:altgr-intl:eng", manager_->GetCurrentInputMethod().id());
630 manager_->SwitchToPreviousInputMethod();
631 EXPECT_EQ("xkb:us:intl:eng", manager_->GetCurrentInputMethod().id());
632 manager_->SwitchToPreviousInputMethod();
633 EXPECT_EQ("xkb:us:altgr-intl:eng", manager_->GetCurrentInputMethod().id());
634 }
635
636 TEST_F(InputMethodManagerImplTest, TestSwitchInputMethodWithUsLayouts) {
637 manager_->EnableLayouts("en-US", "xkb:us::eng");
638 EXPECT_EQ(5U, manager_->GetNumActiveInputMethods());
639 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
640
641 // Henkan, Muhenkan, ZenkakuHankaku should be ignored when no Japanese IMEs
642 // and keyboards are enabled.
643 EXPECT_FALSE(manager_->SwitchInputMethod(
644 ui::Accelerator(ui::VKEY_CONVERT, false, false, false)));
645 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
646 EXPECT_FALSE(manager_->SwitchInputMethod(
647 ui::Accelerator(ui::VKEY_NONCONVERT, false, false, false)));
648 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
649 EXPECT_FALSE(manager_->SwitchInputMethod(
650 ui::Accelerator(ui::VKEY_DBE_SBCSCHAR, false, false, false)));
651 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
652 EXPECT_FALSE(manager_->SwitchInputMethod(
653 ui::Accelerator(ui::VKEY_DBE_DBCSCHAR, false, false, false)));
654 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
655
656 // Do the same tests for Korean.
657 EXPECT_FALSE(manager_->SwitchInputMethod(
658 ui::Accelerator(ui::VKEY_HANGUL, false, false, false)));
659 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
660 EXPECT_FALSE(manager_->SwitchInputMethod(
661 ui::Accelerator(ui::VKEY_SPACE, true, false, false)));
662 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
663 }
664
665 TEST_F(InputMethodManagerImplTest, TestSwitchInputMethodWithJpLayout) {
666 // Enable "xkb:jp::jpn" and press Muhenkan/ZenkakuHankaku.
667 manager_->EnableLayouts("ja", "xkb:us::eng");
668 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
669 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
670 EXPECT_TRUE(manager_->SwitchInputMethod(
671 ui::Accelerator(ui::VKEY_NONCONVERT, false, false, false)));
672 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
673 manager_->SwitchToPreviousInputMethod();
674 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
675 EXPECT_TRUE(manager_->SwitchInputMethod(
676 ui::Accelerator(ui::VKEY_DBE_SBCSCHAR, false, false, false)));
677 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
678 manager_->SwitchToPreviousInputMethod();
679 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
680 EXPECT_TRUE(manager_->SwitchInputMethod(
681 ui::Accelerator(ui::VKEY_DBE_DBCSCHAR, false, false, false)));
682 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
683 }
684
685 TEST_F(InputMethodManagerImplTest, TestSwitchInputMethodWithKoLayout) {
686 // Do the same tests for Korean.
687 manager_->EnableLayouts("ko", "xkb:us::eng");
688 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
689 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
690 EXPECT_TRUE(manager_->SwitchInputMethod(
691 ui::Accelerator(ui::VKEY_HANGUL, false, false, false)));
692 EXPECT_EQ("xkb:kr:kr104:kor", manager_->GetCurrentInputMethod().id());
693 manager_->SwitchToPreviousInputMethod();
694 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
695 EXPECT_TRUE(manager_->SwitchInputMethod(
696 ui::Accelerator(ui::VKEY_SPACE, true, false, false)));
697 EXPECT_EQ("xkb:kr:kr104:kor", manager_->GetCurrentInputMethod().id());
698 }
699
700 TEST_F(InputMethodManagerImplTest, TestSwitchInputMethodWithJpIme) {
701 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
702 std::vector<std::string> ids;
703 ids.push_back("xkb:jp::jpn");
704 ids.push_back("mozc-jp");
705 EXPECT_TRUE(manager_->EnableInputMethods(ids));
706 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
707 EXPECT_TRUE(manager_->SwitchInputMethod(
708 ui::Accelerator(ui::VKEY_DBE_DBCSCHAR, false, false, false)));
709 EXPECT_EQ("mozc-jp", manager_->GetCurrentInputMethod().id());
710 EXPECT_TRUE(manager_->SwitchInputMethod(
711 ui::Accelerator(ui::VKEY_DBE_DBCSCHAR, false, false, false)));
712 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
713 EXPECT_TRUE(manager_->SwitchInputMethod(
714 ui::Accelerator(ui::VKEY_CONVERT, false, false, false)));
715 EXPECT_EQ("mozc-jp", manager_->GetCurrentInputMethod().id());
716 EXPECT_TRUE(manager_->SwitchInputMethod(
717 ui::Accelerator(ui::VKEY_CONVERT, false, false, false)));
718 EXPECT_EQ("mozc-jp", manager_->GetCurrentInputMethod().id());
719 EXPECT_TRUE(manager_->SwitchInputMethod(
720 ui::Accelerator(ui::VKEY_NONCONVERT, false, false, false)));
721 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
722 EXPECT_TRUE(manager_->SwitchInputMethod(
723 ui::Accelerator(ui::VKEY_NONCONVERT, false, false, false)));
724 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
725
726 // Add Dvorak.
727 ids.push_back("xkb:us:dvorak:eng");
728 EXPECT_TRUE(manager_->EnableInputMethods(ids));
729 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
730 EXPECT_TRUE(manager_->SwitchInputMethod(
731 ui::Accelerator(ui::VKEY_DBE_SBCSCHAR, false, false, false)));
732 EXPECT_EQ("mozc-jp", manager_->GetCurrentInputMethod().id());
733 EXPECT_TRUE(manager_->SwitchInputMethod(
734 ui::Accelerator(ui::VKEY_DBE_SBCSCHAR, false, false, false)));
735 EXPECT_EQ("xkb:jp::jpn", manager_->GetCurrentInputMethod().id());
736 }
737
738 TEST_F(InputMethodManagerImplTest, TestSwitchInputMethodWithKoIme) {
739 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
740 std::vector<std::string> ids;
741 ids.push_back("xkb:kr:kr104:kor");
742 ids.push_back("mozc-hangul");
743 EXPECT_TRUE(manager_->EnableInputMethods(ids));
744 EXPECT_EQ("xkb:kr:kr104:kor", manager_->GetCurrentInputMethod().id());
745 EXPECT_TRUE(manager_->SwitchInputMethod(
746 ui::Accelerator(ui::VKEY_HANGUL, false, false, false)));
747 EXPECT_EQ("mozc-hangul", manager_->GetCurrentInputMethod().id());
748 EXPECT_TRUE(manager_->SwitchInputMethod(
749 ui::Accelerator(ui::VKEY_HANGUL, false, false, false)));
750 EXPECT_EQ("xkb:kr:kr104:kor", manager_->GetCurrentInputMethod().id());
751
752 // Add Dvorak.
753 ids.push_back("xkb:us:dvorak:eng");
754 EXPECT_TRUE(manager_->EnableInputMethods(ids));
755 EXPECT_EQ("xkb:kr:kr104:kor", manager_->GetCurrentInputMethod().id());
756 EXPECT_TRUE(manager_->SwitchInputMethod(
757 ui::Accelerator(ui::VKEY_SPACE, true, false, false)));
758 EXPECT_EQ("mozc-hangul", manager_->GetCurrentInputMethod().id());
759 EXPECT_TRUE(manager_->SwitchInputMethod(
760 ui::Accelerator(ui::VKEY_SPACE, true, false, false)));
761 EXPECT_EQ("xkb:kr:kr104:kor", manager_->GetCurrentInputMethod().id());
762 }
763
764 TEST_F(InputMethodManagerImplTest, TestEnableDisableHotkeys) {
765 manager_->EnableLayouts("ja", "xkb:us::eng");
766 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
767 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
768 manager_->DisableHotkeys();
769 EXPECT_FALSE(manager_->SwitchToNextInputMethod());
770 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
771 EXPECT_FALSE(manager_->SwitchToPreviousInputMethod());
772 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
773 EXPECT_FALSE(manager_->SwitchInputMethod(
774 ui::Accelerator(ui::VKEY_NONCONVERT, false, false, false)));
775 EXPECT_EQ("xkb:us::eng", manager_->GetCurrentInputMethod().id());
776 manager_->EnableHotkeys();
777 EXPECT_TRUE(manager_->SwitchToNextInputMethod());
778 EXPECT_NE("xkb:us::eng", manager_->GetCurrentInputMethod().id());
779 }
780
781 TEST_F(InputMethodManagerImplTest, TestAddRemoveExtensionInputMethods) {
782 TestObserver observer;
783 manager_->AddObserver(&observer);
784 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
785 std::vector<std::string> ids;
786 ids.push_back("xkb:us:dvorak:eng");
787 EXPECT_TRUE(manager_->EnableInputMethods(ids));
788 EXPECT_EQ(1U, manager_->GetNumActiveInputMethods());
789 EXPECT_EQ(0, controller_->start_count_);
790 EXPECT_EQ(1, observer.input_method_changed_count_);
791 EXPECT_EQ(ids[0],
792 manager_->GetCurrentInputMethod().id());
793
794 // Add two Extension IMEs.
795 std::vector<std::string> layouts;
796 layouts.push_back("us");
797 manager_->AddInputMethodExtension(
798 std::string(kExtensionImePrefix) + "deadbeef",
799 "deadbeef input method",
800 layouts,
801 "en-US");
802 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
803 EXPECT_EQ(1, controller_->start_count_); // should be started.
804 {
805 scoped_ptr<InputMethodDescriptors> methods(
806 manager_->GetActiveInputMethods());
807 ASSERT_EQ(2U, methods->size());
808 EXPECT_EQ(std::string(kExtensionImePrefix) + "deadbeef",
809 // Ext IMEs should be at the end of the list.
810 methods->at(1).id());
811 }
812 manager_->AddInputMethodExtension(
813 std::string(kExtensionImePrefix) + "cafebabe",
814 "cafebabe input method",
815 layouts,
816 "en-US");
817 EXPECT_EQ(3U, manager_->GetNumActiveInputMethods());
818 {
819 scoped_ptr<InputMethodDescriptors> methods(
820 manager_->GetActiveInputMethods());
821 ASSERT_EQ(3U, methods->size());
822 EXPECT_EQ(std::string(kExtensionImePrefix) + "deadbeef",
823 // Ext IMEs should be at the end of the list.
824 methods->at(1).id());
825 }
826
827 // Remove them.
828 manager_->RemoveInputMethodExtension(
829 std::string(kExtensionImePrefix) + "deadbeef");
830 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
831 manager_->RemoveInputMethodExtension(
832 std::string(kExtensionImePrefix) + "cafebabe");
833 EXPECT_EQ(1U, manager_->GetNumActiveInputMethods());
834 // Currently, to work around a crash issue at crosbug.com/27051,
835 // controller_->Stop(); is NOT called when all (extension) IMEs are disabled.
836 EXPECT_EQ(0, controller_->stop_count_);
837
838 manager_->RemoveObserver(&observer);
839 }
840
841 TEST_F(InputMethodManagerImplTest, TestAddExtensionInputThenLockScreen) {
842 TestObserver observer;
843 manager_->AddObserver(&observer);
844 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
845 std::vector<std::string> ids;
846 ids.push_back("xkb:us::eng");
847 EXPECT_TRUE(manager_->EnableInputMethods(ids));
848 EXPECT_EQ(1U, manager_->GetNumActiveInputMethods());
849 EXPECT_EQ(1, observer.input_method_changed_count_);
850 EXPECT_EQ(ids[0], manager_->GetCurrentInputMethod().id());
851
852 // Add an Extension IME.
853 std::vector<std::string> layouts;
854 layouts.push_back("us");
855 manager_->AddInputMethodExtension(
856 std::string(kExtensionImePrefix) + "deadbeef",
857 "deadbeef input method",
858 layouts,
859 "en-US");
860 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
861 EXPECT_EQ(1, observer.input_method_changed_count_);
862
863 // Switch to the IME.
864 manager_->SwitchToNextInputMethod();
865 EXPECT_EQ(2, observer.input_method_changed_count_);
866 EXPECT_EQ(std::string(kExtensionImePrefix) + "deadbeef",
867 manager_->GetCurrentInputMethod().id());
868
869 // Lock the screen. This is for crosbug.com/27049.
870 manager_->SetState(InputMethodManager::STATE_LOCK_SCREEN);
871 EXPECT_EQ(1U, manager_->GetNumActiveInputMethods()); // Qwerty. No Ext. IME
872 EXPECT_EQ("xkb:us::eng",
873 manager_->GetCurrentInputMethod().id());
874 EXPECT_EQ(0, controller_->stop_count_);
875
876 // Unlock the screen.
877 manager_->SetState(InputMethodManager::STATE_BROWSER_SCREEN);
878 EXPECT_EQ(2U, manager_->GetNumActiveInputMethods());
879 EXPECT_EQ(std::string(kExtensionImePrefix) + "deadbeef",
880 manager_->GetCurrentInputMethod().id());
881 {
882 // This is for crosbug.com/27052.
883 scoped_ptr<InputMethodDescriptors> methods(
884 manager_->GetActiveInputMethods());
885 ASSERT_EQ(2U, methods->size());
886 EXPECT_EQ(std::string(kExtensionImePrefix) + "deadbeef",
887 // Ext. IMEs should be at the end of the list.
888 methods->at(1).id());
889 }
890 manager_->RemoveObserver(&observer);
891 }
892
893 } // namespace input_method
894 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698