mock_input_method.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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/base/ime/mock_input_method.h" 6 7#include "base/logging.h" 8#include "base/string16.h" 9#include "ui/base/events/event.h" 10#include "ui/base/events/event_constants.h" 11#include "ui/base/events/event_utils.h" 12#include "ui/base/glib/glib_integers.h" 13#include "ui/base/ime/input_method_delegate.h" 14#include "ui/base/ime/text_input_client.h" 15#include "ui/base/keycodes/keyboard_code_conversion.h" 16 17#if defined(USE_X11) 18#include <X11/X.h> 19#include <X11/Xlib.h> 20#include <X11/Xutil.h> 21#include "ui/base/keycodes/keyboard_code_conversion_x.h" 22#endif 23 24namespace { 25 26#if defined(USE_X11) 27uint32 EventFlagsFromXFlags(unsigned int flags) { 28 return (flags & LockMask ? ui::EF_CAPS_LOCK_DOWN : 0U) | 29 (flags & ControlMask ? ui::EF_CONTROL_DOWN : 0U) | 30 (flags & ShiftMask ? ui::EF_SHIFT_DOWN : 0U) | 31 (flags & Mod1Mask ? ui::EF_ALT_DOWN : 0U); 32} 33#endif 34 35} // namespace 36 37namespace ui { 38 39MockInputMethod::MockInputMethod(internal::InputMethodDelegate* delegate) 40 : delegate_(NULL), 41 text_input_client_(NULL) { 42 SetDelegate(delegate); 43} 44 45MockInputMethod::~MockInputMethod() { 46} 47 48void MockInputMethod::SetDelegate(internal::InputMethodDelegate* delegate) { 49 delegate_ = delegate; 50} 51 52void MockInputMethod::SetFocusedTextInputClient(TextInputClient* client) { 53 text_input_client_ = client; 54} 55 56TextInputClient* MockInputMethod::GetTextInputClient() const { 57 return text_input_client_; 58} 59 60void MockInputMethod::DispatchKeyEvent(const base::NativeEvent& native_event) { 61#if defined(OS_WIN) 62 if (native_event.message == WM_CHAR) { 63 if (text_input_client_) { 64 text_input_client_->InsertChar(ui::KeyboardCodeFromNative(native_event), 65 ui::EventFlagsFromNative(native_event)); 66 } 67 } else { 68 delegate_->DispatchKeyEventPostIME(native_event); 69 } 70#elif defined(USE_X11) 71 DCHECK(native_event); 72 if (native_event->type == KeyRelease) { 73 // On key release, just dispatch it. 74 delegate_->DispatchKeyEventPostIME(native_event); 75 } else { 76 const uint32 state = EventFlagsFromXFlags(native_event->xkey.state); 77 // Send a RawKeyDown event first, 78 delegate_->DispatchKeyEventPostIME(native_event); 79 if (text_input_client_) { 80 // then send a Char event via ui::TextInputClient. 81 const KeyboardCode key_code = ui::KeyboardCodeFromNative(native_event); 82 uint16 ch = 0; 83 if (!(state & ui::EF_CONTROL_DOWN)) 84 ch = ui::GetCharacterFromXEvent(native_event); 85 if (!ch) 86 ch = ui::GetCharacterFromKeyCode(key_code, state); 87 if (ch) 88 text_input_client_->InsertChar(ch, state); 89 } 90 } 91#else 92 // TODO(yusukes): Support other platforms. Call InsertChar() when necessary. 93 delegate_->DispatchKeyEventPostIME(native_event); 94#endif 95} 96 97void MockInputMethod::DispatchFabricatedKeyEvent(const ui::KeyEvent& event) { 98#if defined(OS_WIN) 99 if (event.is_char()) { 100 if (GetTextInputClient()) { 101 GetTextInputClient()->InsertChar(event.key_code(), 102 ui::GetModifiersFromKeyState()); 103 return; 104 } 105 } 106 delegate_->DispatchFabricatedKeyEventPostIME(event.type(), 107 event.key_code(), 108 event.flags()); 109#endif 110} 111 112void MockInputMethod::Init(bool focused) {} 113void MockInputMethod::OnFocus() {} 114void MockInputMethod::OnBlur() {} 115void MockInputMethod::OnTextInputTypeChanged(const TextInputClient* client) {} 116void MockInputMethod::OnCaretBoundsChanged(const TextInputClient* client) {} 117void MockInputMethod::CancelComposition(const TextInputClient* client) {} 118 119std::string MockInputMethod::GetInputLocale() { 120 return ""; 121} 122 123base::i18n::TextDirection MockInputMethod::GetInputTextDirection() { 124 return base::i18n::UNKNOWN_DIRECTION; 125} 126 127bool MockInputMethod::IsActive() { 128 return true; 129} 130 131ui::TextInputType MockInputMethod::GetTextInputType() const { 132 return ui::TEXT_INPUT_TYPE_NONE; 133} 134 135bool MockInputMethod::CanComposeInline() const { 136 return true; 137} 138 139} // namespace ui 140