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.
5 #include "ui/views/ime/mock_input_method.h"
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"
16 MockInputMethod::MockInputMethod()
17 : composition_changed_(false),
18 focus_changed_(false),
19 untranslated_ime_message_called_(false),
20 text_input_type_changed_(false),
21 caret_bounds_changed_(false),
22 cancel_composition_called_(false),
23 input_locale_changed_(false),
25 direction_(base::i18n::LEFT_TO_RIGHT),
29 MockInputMethod::MockInputMethod(internal::InputMethodDelegate* delegate)
30 : composition_changed_(false),
31 focus_changed_(false),
32 untranslated_ime_message_called_(false),
33 text_input_type_changed_(false),
34 caret_bounds_changed_(false),
35 cancel_composition_called_(false),
36 input_locale_changed_(false),
38 direction_(base::i18n::LEFT_TO_RIGHT),
40 SetDelegate(delegate);
43 MockInputMethod::~MockInputMethod() {
46 void MockInputMethod::Init(Widget* widget) {
47 InputMethodBase::Init(widget);
50 void MockInputMethod::OnFocus() {}
52 void MockInputMethod::OnBlur() {}
54 bool MockInputMethod::OnUntranslatedIMEMessage(
55 const base::NativeEvent& event,
56 NativeEventResult* result) {
57 untranslated_ime_message_called_ = true;
59 *result = InputMethod::NativeEventResult();
63 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent& key) {
64 bool handled = (composition_changed_ || result_text_.length()) &&
65 !IsTextInputTypeNone();
69 ui::KeyEvent mock_key(ui::ET_KEY_PRESSED,
73 DispatchKeyEventPostIME(mock_key);
75 DispatchKeyEventPostIME(key);
81 ui::TextInputClient* client = GetTextInputClient();
84 if (result_text_.length())
85 client->InsertText(result_text_);
86 if (composition_changed_) {
87 if (composition_.text.length())
88 client->SetCompositionText(composition_);
90 client->ClearCompositionText();
92 } else if (key.type() == ui::ET_KEY_PRESSED) {
93 char16 ch = key.GetCharacter();
94 client->InsertChar(ch, key.flags());
101 void MockInputMethod::OnTextInputTypeChanged(View* view) {
102 if (IsViewFocused(view))
103 text_input_type_changed_ = true;
104 InputMethodBase::OnTextInputTypeChanged(view);
107 void MockInputMethod::OnCaretBoundsChanged(View* view) {
108 if (IsViewFocused(view))
109 caret_bounds_changed_ = true;
112 void MockInputMethod::CancelComposition(View* view) {
113 if (IsViewFocused(view)) {
114 cancel_composition_called_ = true;
119 void MockInputMethod::OnInputLocaleChanged() {
120 input_locale_changed_ = true;
123 std::string MockInputMethod::GetInputLocale() {
127 base::i18n::TextDirection MockInputMethod::GetInputTextDirection() {
131 bool MockInputMethod::IsActive() {
135 bool MockInputMethod::IsCandidatePopupOpen() const {
139 bool MockInputMethod::IsMock() const {
143 void MockInputMethod::OnWillChangeFocus(View* focused_before, View* focused) {
144 ui::TextInputClient* client = GetTextInputClient();
145 if (client && client->HasCompositionText())
146 client->ConfirmCompositionText();
147 focus_changed_ = true;
151 void MockInputMethod::Clear() {
156 void MockInputMethod::SetCompositionTextForNextKey(
157 const ui::CompositionText& composition) {
158 composition_changed_ = true;
159 composition_ = composition;
162 void MockInputMethod::SetResultTextForNextKey(const string16& result) {
163 result_text_ = result;
166 void MockInputMethod::SetInputLocale(const std::string& locale) {
167 if (locale_ != locale) {
169 OnInputMethodChanged();
173 void MockInputMethod::SetInputTextDirection(
174 base::i18n::TextDirection direction) {
175 if (direction_ != direction) {
176 direction_ = direction;
177 OnInputMethodChanged();
181 void MockInputMethod::SetActive(bool active) {
182 if (active_ != active) {
184 OnInputMethodChanged();
188 void MockInputMethod::ClearStates() {
189 focus_changed_ = false;
190 untranslated_ime_message_called_ = false;
191 text_input_type_changed_ = false;
192 caret_bounds_changed_ = false;
193 cancel_composition_called_ = false;
194 input_locale_changed_ = false;
197 void MockInputMethod::ClearResult() {
198 composition_.Clear();
199 composition_changed_ = false;
200 result_text_.clear();