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 : untranslated_ime_message_called_(false),
18 text_input_type_changed_(false),
19 cancel_composition_called_(false) {
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
);
29 MockInputMethod::~MockInputMethod() {
32 void MockInputMethod::OnFocus() {}
34 void MockInputMethod::OnBlur() {}
36 bool MockInputMethod::OnUntranslatedIMEMessage(
37 const base::NativeEvent
& event
,
38 NativeEventResult
* result
) {
39 untranslated_ime_message_called_
= true;
41 *result
= InputMethod::NativeEventResult();
45 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent
& key
) {
46 bool handled
= !IsTextInputTypeNone() && HasComposition();
50 DCHECK(!key
.is_char());
51 ui::KeyEvent
mock_key(ui::ET_KEY_PRESSED
,
54 DispatchKeyEventPostIME(mock_key
);
56 DispatchKeyEventPostIME(key
);
59 ui::TextInputClient
* client
= GetTextInputClient();
62 if (result_text_
.length())
63 client
->InsertText(result_text_
);
64 if (composition_
.text
.length())
65 client
->SetCompositionText(composition_
);
67 client
->ClearCompositionText();
68 } else if (key
.type() == ui::ET_KEY_PRESSED
) {
69 base::char16 ch
= key
.GetCharacter();
70 client
->InsertChar(ch
, key
.flags());
77 void MockInputMethod::OnTextInputTypeChanged(View
* view
) {
78 if (IsViewFocused(view
))
79 text_input_type_changed_
= true;
80 InputMethodBase::OnTextInputTypeChanged(view
);
83 void MockInputMethod::OnCaretBoundsChanged(View
* view
) {
86 void MockInputMethod::CancelComposition(View
* view
) {
87 if (IsViewFocused(view
)) {
88 cancel_composition_called_
= true;
93 void MockInputMethod::OnInputLocaleChanged() {
96 std::string
MockInputMethod::GetInputLocale() {
100 bool MockInputMethod::IsActive() {
104 bool MockInputMethod::IsCandidatePopupOpen() const {
108 void MockInputMethod::ShowImeIfNeeded() {
111 void MockInputMethod::OnWillChangeFocus(View
* focused_before
, View
* focused
) {
112 ui::TextInputClient
* client
= GetTextInputClient();
113 if (client
&& client
->HasCompositionText())
114 client
->ConfirmCompositionText();
118 void MockInputMethod::Clear() {
123 void MockInputMethod::SetCompositionTextForNextKey(
124 const ui::CompositionText
& composition
) {
125 composition_
= composition
;
128 void MockInputMethod::SetResultTextForNextKey(const base::string16
& result
) {
129 result_text_
= result
;
132 void MockInputMethod::ClearStates() {
133 untranslated_ime_message_called_
= false;
134 text_input_type_changed_
= false;
135 cancel_composition_called_
= false;
138 bool MockInputMethod::HasComposition() {
139 return composition_
.text
.length() || result_text_
.length();
142 void MockInputMethod::ClearComposition() {
143 composition_
.Clear();
144 result_text_
.clear();