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),
28 MockInputMethod::MockInputMethod(internal::InputMethodDelegate
* delegate
)
29 : composition_changed_(false),
30 focus_changed_(false),
31 untranslated_ime_message_called_(false),
32 text_input_type_changed_(false),
33 caret_bounds_changed_(false),
34 cancel_composition_called_(false),
35 input_locale_changed_(false),
38 SetDelegate(delegate
);
41 MockInputMethod::~MockInputMethod() {
44 void MockInputMethod::Init(Widget
* widget
) {
45 InputMethodBase::Init(widget
);
48 void MockInputMethod::OnFocus() {}
50 void MockInputMethod::OnBlur() {}
52 bool MockInputMethod::OnUntranslatedIMEMessage(
53 const base::NativeEvent
& event
,
54 NativeEventResult
* result
) {
55 untranslated_ime_message_called_
= true;
57 *result
= InputMethod::NativeEventResult();
61 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent
& key
) {
62 bool handled
= (composition_changed_
|| result_text_
.length()) &&
63 !IsTextInputTypeNone();
67 ui::KeyEvent
mock_key(ui::ET_KEY_PRESSED
,
71 DispatchKeyEventPostIME(mock_key
);
73 DispatchKeyEventPostIME(key
);
79 ui::TextInputClient
* client
= GetTextInputClient();
82 if (result_text_
.length())
83 client
->InsertText(result_text_
);
84 if (composition_changed_
) {
85 if (composition_
.text
.length())
86 client
->SetCompositionText(composition_
);
88 client
->ClearCompositionText();
90 } else if (key
.type() == ui::ET_KEY_PRESSED
) {
91 base::char16 ch
= key
.GetCharacter();
92 client
->InsertChar(ch
, key
.flags());
99 void MockInputMethod::OnTextInputTypeChanged(View
* view
) {
100 if (IsViewFocused(view
))
101 text_input_type_changed_
= true;
102 InputMethodBase::OnTextInputTypeChanged(view
);
105 void MockInputMethod::OnCaretBoundsChanged(View
* view
) {
106 if (IsViewFocused(view
))
107 caret_bounds_changed_
= true;
110 void MockInputMethod::CancelComposition(View
* view
) {
111 if (IsViewFocused(view
)) {
112 cancel_composition_called_
= true;
117 void MockInputMethod::OnInputLocaleChanged() {
118 input_locale_changed_
= true;
121 std::string
MockInputMethod::GetInputLocale() {
125 bool MockInputMethod::IsActive() {
129 bool MockInputMethod::IsCandidatePopupOpen() const {
133 void MockInputMethod::ShowImeIfNeeded() {
136 bool MockInputMethod::IsMock() const {
140 void MockInputMethod::OnWillChangeFocus(View
* focused_before
, View
* focused
) {
141 ui::TextInputClient
* client
= GetTextInputClient();
142 if (client
&& client
->HasCompositionText())
143 client
->ConfirmCompositionText();
144 focus_changed_
= true;
148 void MockInputMethod::Clear() {
153 void MockInputMethod::SetCompositionTextForNextKey(
154 const ui::CompositionText
& composition
) {
155 composition_changed_
= true;
156 composition_
= composition
;
159 void MockInputMethod::SetResultTextForNextKey(const base::string16
& result
) {
160 result_text_
= result
;
163 void MockInputMethod::SetInputLocale(const std::string
& locale
) {
164 if (locale_
!= locale
) {
166 OnInputMethodChanged();
170 void MockInputMethod::SetActive(bool active
) {
171 if (active_
!= active
) {
173 OnInputMethodChanged();
177 void MockInputMethod::ClearStates() {
178 focus_changed_
= false;
179 untranslated_ime_message_called_
= false;
180 text_input_type_changed_
= false;
181 caret_bounds_changed_
= false;
182 cancel_composition_called_
= false;
183 input_locale_changed_
= false;
186 void MockInputMethod::ClearResult() {
187 composition_
.Clear();
188 composition_changed_
= false;
189 result_text_
.clear();