OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/views/ime/mock_input_method.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/logging.h" | |
9 #include "ui/base/ime/text_input_client.h" | |
10 #include "ui/events/event.h" | |
11 #include "ui/events/keycodes/keyboard_codes.h" | |
12 #include "ui/views/widget/widget.h" | |
13 | |
14 namespace views { | |
15 | |
16 MockInputMethod::MockInputMethod() | |
17 : untranslated_ime_message_called_(false), | |
18 text_input_type_changed_(false), | |
19 cancel_composition_called_(false) { | |
20 } | |
21 | |
22 MockInputMethod::MockInputMethod(internal::InputMethodDelegate* delegate) | |
23 : untranslated_ime_message_called_(false), | |
24 text_input_type_changed_(false), | |
25 cancel_composition_called_(false) { | |
26 SetDelegate(delegate); | |
27 } | |
28 | |
29 MockInputMethod::~MockInputMethod() { | |
30 } | |
31 | |
32 void MockInputMethod::OnFocus() {} | |
33 | |
34 void MockInputMethod::OnBlur() {} | |
35 | |
36 bool MockInputMethod::OnUntranslatedIMEMessage( | |
37 const base::NativeEvent& event, | |
38 NativeEventResult* result) { | |
39 untranslated_ime_message_called_ = true; | |
40 if (result) | |
41 *result = InputMethod::NativeEventResult(); | |
42 return false; | |
43 } | |
44 | |
45 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent& key) { | |
46 bool handled = !IsTextInputTypeNone() && HasComposition(); | |
47 | |
48 ClearStates(); | |
49 if (handled) { | |
50 DCHECK(!key.is_char()); | |
51 ui::KeyEvent mock_key(ui::ET_KEY_PRESSED, | |
52 ui::VKEY_PROCESSKEY, | |
53 key.flags()); | |
54 DispatchKeyEventPostIME(mock_key); | |
55 } else { | |
56 DispatchKeyEventPostIME(key); | |
57 } | |
58 | |
59 ui::TextInputClient* client = GetTextInputClient(); | |
60 if (client) { | |
61 if (handled) { | |
62 if (result_text_.length()) | |
63 client->InsertText(result_text_); | |
64 if (composition_.text.length()) | |
65 client->SetCompositionText(composition_); | |
66 else | |
67 client->ClearCompositionText(); | |
68 } else if (key.type() == ui::ET_KEY_PRESSED) { | |
69 base::char16 ch = key.GetCharacter(); | |
70 client->InsertChar(ch, key.flags()); | |
71 } | |
72 } | |
73 | |
74 ClearComposition(); | |
75 } | |
76 | |
77 void MockInputMethod::OnTextInputTypeChanged(View* view) { | |
78 if (IsViewFocused(view)) | |
79 text_input_type_changed_ = true; | |
80 InputMethodBase::OnTextInputTypeChanged(view); | |
81 } | |
82 | |
83 void MockInputMethod::OnCaretBoundsChanged(View* view) { | |
84 } | |
85 | |
86 void MockInputMethod::CancelComposition(View* view) { | |
87 if (IsViewFocused(view)) { | |
88 cancel_composition_called_ = true; | |
89 ClearComposition(); | |
90 } | |
91 } | |
92 | |
93 void MockInputMethod::OnInputLocaleChanged() { | |
94 } | |
95 | |
96 std::string MockInputMethod::GetInputLocale() { | |
97 return "en-US"; | |
98 } | |
99 | |
100 bool MockInputMethod::IsActive() { | |
101 return true; | |
102 } | |
103 | |
104 bool MockInputMethod::IsCandidatePopupOpen() const { | |
105 return false; | |
106 } | |
107 | |
108 void MockInputMethod::ShowImeIfNeeded() { | |
109 } | |
110 | |
111 void MockInputMethod::OnWillChangeFocus(View* focused_before, View* focused) { | |
112 ui::TextInputClient* client = GetTextInputClient(); | |
113 if (client && client->HasCompositionText()) | |
114 client->ConfirmCompositionText(); | |
115 ClearComposition(); | |
116 } | |
117 | |
118 void MockInputMethod::Clear() { | |
119 ClearStates(); | |
120 ClearComposition(); | |
121 } | |
122 | |
123 void MockInputMethod::SetCompositionTextForNextKey( | |
124 const ui::CompositionText& composition) { | |
125 composition_ = composition; | |
126 } | |
127 | |
128 void MockInputMethod::SetResultTextForNextKey(const base::string16& result) { | |
129 result_text_ = result; | |
130 } | |
131 | |
132 void MockInputMethod::ClearStates() { | |
133 untranslated_ime_message_called_ = false; | |
134 text_input_type_changed_ = false; | |
135 cancel_composition_called_ = false; | |
136 } | |
137 | |
138 bool MockInputMethod::HasComposition() { | |
139 return composition_.text.length() || result_text_.length(); | |
140 } | |
141 | |
142 void MockInputMethod::ClearComposition() { | |
143 composition_.Clear(); | |
144 result_text_.clear(); | |
145 } | |
146 | |
147 } // namespace views | |
OLD | NEW |