Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / views / ime / mock_input_method.cc
bloba56c96a732f944578cf921c3f34cfd2eaa2fe973
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 : 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;
40 if (result)
41 *result = InputMethod::NativeEventResult();
42 return false;
45 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent& key) {
46 bool handled = !IsTextInputTypeNone() && HasComposition();
48 ClearStates();
49 if (handled) {
50 DCHECK(!key.is_char());
51 ui::KeyEvent mock_key(ui::ET_KEY_PRESSED,
52 ui::VKEY_PROCESSKEY,
53 key.flags());
54 DispatchKeyEventPostIME(mock_key);
55 } else {
56 DispatchKeyEventPostIME(key);
59 ui::TextInputClient* client = GetTextInputClient();
60 if (client) {
61 if (handled) {
62 if (result_text_.length())
63 client->InsertText(result_text_);
64 if (composition_.text.length())
65 client->SetCompositionText(composition_);
66 else
67 client->ClearCompositionText();
68 } else if (key.type() == ui::ET_KEY_PRESSED) {
69 base::char16 ch = key.GetCharacter();
70 client->InsertChar(ch, key.flags());
74 ClearComposition();
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;
89 ClearComposition();
93 void MockInputMethod::OnInputLocaleChanged() {
96 std::string MockInputMethod::GetInputLocale() {
97 return "en-US";
100 bool MockInputMethod::IsActive() {
101 return true;
104 bool MockInputMethod::IsCandidatePopupOpen() const {
105 return false;
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();
115 ClearComposition();
118 void MockInputMethod::Clear() {
119 ClearStates();
120 ClearComposition();
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();
147 } // namespace views