Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / views / ime / mock_input_method.cc
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     : 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) {
26 }
27
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);
39 }
40
41 MockInputMethod::~MockInputMethod() {
42 }
43
44 void MockInputMethod::Init(Widget* widget) {
45   InputMethodBase::Init(widget);
46 }
47
48 void MockInputMethod::OnFocus() {}
49
50 void MockInputMethod::OnBlur() {}
51
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;
59 }
60
61 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent& key) {
62   bool handled = (composition_changed_ || result_text_.length()) &&
63       !IsTextInputTypeNone();
64
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);
74   }
75
76   if (focus_changed_)
77     return;
78
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();
89       }
90     } else if (key.type() == ui::ET_KEY_PRESSED) {
91       base::char16 ch = key.GetCharacter();
92       client->InsertChar(ch, key.flags());
93     }
94   }
95
96   ClearResult();
97 }
98
99 void MockInputMethod::OnTextInputTypeChanged(View* view) {
100   if (IsViewFocused(view))
101     text_input_type_changed_ = true;
102   InputMethodBase::OnTextInputTypeChanged(view);
103 }
104
105 void MockInputMethod::OnCaretBoundsChanged(View* view) {
106   if (IsViewFocused(view))
107     caret_bounds_changed_ = true;
108 }
109
110 void MockInputMethod::CancelComposition(View* view) {
111   if (IsViewFocused(view)) {
112     cancel_composition_called_ = true;
113     ClearResult();
114   }
115 }
116
117 void MockInputMethod::OnInputLocaleChanged() {
118   input_locale_changed_ = true;
119 }
120
121 std::string MockInputMethod::GetInputLocale() {
122   return locale_;
123 }
124
125 bool MockInputMethod::IsActive() {
126   return active_;
127 }
128
129 bool MockInputMethod::IsCandidatePopupOpen() const {
130   return false;
131 }
132
133 void MockInputMethod::ShowImeIfNeeded() {
134 }
135
136 bool MockInputMethod::IsMock() const {
137   return true;
138 }
139
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();
146 }
147
148 void MockInputMethod::Clear() {
149   ClearStates();
150   ClearResult();
151 }
152
153 void MockInputMethod::SetCompositionTextForNextKey(
154     const ui::CompositionText& composition) {
155   composition_changed_ = true;
156   composition_ = composition;
157 }
158
159 void MockInputMethod::SetResultTextForNextKey(const base::string16& result) {
160   result_text_ = result;
161 }
162
163 void MockInputMethod::SetInputLocale(const std::string& locale) {
164   if (locale_ != locale) {
165     locale_ = locale;
166     OnInputMethodChanged();
167   }
168 }
169
170 void MockInputMethod::SetActive(bool active) {
171   if (active_ != active) {
172     active_ = active;
173     OnInputMethodChanged();
174   }
175 }
176
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;
184 }
185
186 void MockInputMethod::ClearResult() {
187   composition_.Clear();
188   composition_changed_ = false;
189   result_text_.clear();
190 }
191
192 }  // namespace views