base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / views / ime / mock_input_method.cc
blobb7611dae4f413793eb20c7044d7ebc972e61afd2
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"
14 namespace views {
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),
24 locale_("en-US"),
25 active_(true) {
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),
36 locale_("en-US"),
37 active_(true) {
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;
56 if (result)
57 *result = InputMethod::NativeEventResult();
58 return false;
61 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent& key) {
62 bool handled = (composition_changed_ || result_text_.length()) &&
63 !IsTextInputTypeNone();
65 ClearStates();
66 if (handled) {
67 DCHECK(!key.is_char());
68 ui::KeyEvent mock_key(ui::ET_KEY_PRESSED,
69 ui::VKEY_PROCESSKEY,
70 key.flags());
71 DispatchKeyEventPostIME(mock_key);
72 } else {
73 DispatchKeyEventPostIME(key);
76 if (focus_changed_)
77 return;
79 ui::TextInputClient* client = GetTextInputClient();
80 if (client) {
81 if (handled) {
82 if (result_text_.length())
83 client->InsertText(result_text_);
84 if (composition_changed_) {
85 if (composition_.text.length())
86 client->SetCompositionText(composition_);
87 else
88 client->ClearCompositionText();
90 } else if (key.type() == ui::ET_KEY_PRESSED) {
91 base::char16 ch = key.GetCharacter();
92 client->InsertChar(ch, key.flags());
96 ClearResult();
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;
113 ClearResult();
117 void MockInputMethod::OnInputLocaleChanged() {
118 input_locale_changed_ = true;
121 std::string MockInputMethod::GetInputLocale() {
122 return locale_;
125 bool MockInputMethod::IsActive() {
126 return active_;
129 bool MockInputMethod::IsCandidatePopupOpen() const {
130 return false;
133 void MockInputMethod::ShowImeIfNeeded() {
136 bool MockInputMethod::IsMock() const {
137 return true;
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;
145 ClearResult();
148 void MockInputMethod::Clear() {
149 ClearStates();
150 ClearResult();
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) {
165 locale_ = locale;
166 OnInputMethodChanged();
170 void MockInputMethod::SetActive(bool active) {
171 if (active_ != active) {
172 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();
192 } // namespace views