OLD | NEW |
(Empty) | |
| 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 "views/ime/mock_input_method.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" |
| 9 #include "ui/base/keycodes/keyboard_codes.h" |
| 10 #include "views/events/event.h" |
| 11 #include "views/widget/widget.h" |
| 12 |
| 13 namespace views { |
| 14 |
| 15 MockInputMethod::MockInputMethod() |
| 16 : composition_changed_(false), |
| 17 focus_changed_(false), |
| 18 text_input_type_changed_(false), |
| 19 caret_bounds_changed_(false), |
| 20 cancel_composition_called_(false), |
| 21 locale_("en-US"), |
| 22 direction_(base::i18n::LEFT_TO_RIGHT), |
| 23 active_(true) { |
| 24 } |
| 25 |
| 26 MockInputMethod::~MockInputMethod() { |
| 27 } |
| 28 |
| 29 void MockInputMethod::Init(Widget* widget) { |
| 30 InputMethodBase::Init(widget); |
| 31 |
| 32 // Assumes the Widget is always focused. |
| 33 set_widget_focused(true); |
| 34 } |
| 35 |
| 36 void MockInputMethod::DispatchKeyEvent(const KeyEvent& key) { |
| 37 bool handled = (composition_changed_ || result_text_.length()) && |
| 38 !IsTextInputTypeNone(); |
| 39 |
| 40 ClearStates(); |
| 41 if (handled) { |
| 42 KeyEvent mock_key(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, key.flags()); |
| 43 DispatchKeyEventPostIME(mock_key); |
| 44 } else { |
| 45 DispatchKeyEventPostIME(key); |
| 46 } |
| 47 |
| 48 if (focus_changed_) |
| 49 return; |
| 50 |
| 51 TextInputClient* client = GetTextInputClient(); |
| 52 if (client) { |
| 53 if (handled) { |
| 54 if (result_text_.length()) |
| 55 client->InsertText(result_text_); |
| 56 if (composition_changed_) { |
| 57 if (composition_.text.length()) |
| 58 client->SetCompositionText(composition_); |
| 59 else |
| 60 client->ClearCompositionText(); |
| 61 } |
| 62 } else if (key.type() == ui::ET_KEY_PRESSED) { |
| 63 char16 ch = key.GetCharacter(); |
| 64 client->InsertChar(ch, key.flags()); |
| 65 } |
| 66 } |
| 67 |
| 68 ClearResult(); |
| 69 } |
| 70 |
| 71 void MockInputMethod::OnTextInputTypeChanged(View* view) { |
| 72 DCHECK(IsViewFocused(view)); |
| 73 text_input_type_changed_ = true; |
| 74 } |
| 75 |
| 76 void MockInputMethod::OnCaretBoundsChanged(View* view) { |
| 77 DCHECK(IsViewFocused(view)); |
| 78 caret_bounds_changed_ = true; |
| 79 } |
| 80 |
| 81 void MockInputMethod::CancelComposition(View* view) { |
| 82 DCHECK(IsViewFocused(view)); |
| 83 cancel_composition_called_ = true; |
| 84 ClearResult(); |
| 85 } |
| 86 |
| 87 std::string MockInputMethod::GetInputLocale() { |
| 88 return locale_; |
| 89 } |
| 90 |
| 91 base::i18n::TextDirection MockInputMethod::GetInputTextDirection() { |
| 92 return direction_; |
| 93 } |
| 94 |
| 95 bool MockInputMethod::IsActive() { |
| 96 return active_; |
| 97 } |
| 98 |
| 99 void MockInputMethod::FocusedViewWillChange() { |
| 100 TextInputClient* client = GetTextInputClient(); |
| 101 if (client && client->HasCompositionText()) |
| 102 client->ConfirmCompositionText(); |
| 103 focus_changed_ = true; |
| 104 ClearResult(); |
| 105 } |
| 106 |
| 107 void MockInputMethod::Clear() { |
| 108 ClearStates(); |
| 109 ClearResult(); |
| 110 } |
| 111 |
| 112 void MockInputMethod::SetCompositionTextForNextKey( |
| 113 const ui::CompositionText& composition) { |
| 114 composition_changed_ = true; |
| 115 composition_ = composition; |
| 116 } |
| 117 |
| 118 void MockInputMethod::SetResultTextForNextKey(const string16& result) { |
| 119 result_text_ = result; |
| 120 } |
| 121 |
| 122 void MockInputMethod::SetInputLocale(const std::string& locale) { |
| 123 if (locale_ != locale) { |
| 124 locale_ = locale; |
| 125 OnInputMethodChanged(); |
| 126 } |
| 127 } |
| 128 |
| 129 void MockInputMethod::SetInputTextDirection( |
| 130 base::i18n::TextDirection direction) { |
| 131 if (direction_ != direction) { |
| 132 direction_ = direction; |
| 133 OnInputMethodChanged(); |
| 134 } |
| 135 } |
| 136 |
| 137 void MockInputMethod::SetActive(bool active) { |
| 138 if (active_ != active) { |
| 139 active_ = active; |
| 140 OnInputMethodChanged(); |
| 141 } |
| 142 } |
| 143 |
| 144 void MockInputMethod::ClearStates() { |
| 145 focus_changed_ = false; |
| 146 text_input_type_changed_ = false; |
| 147 caret_bounds_changed_ = false; |
| 148 cancel_composition_called_ = false; |
| 149 } |
| 150 |
| 151 void MockInputMethod::ClearResult() { |
| 152 composition_.Clear(); |
| 153 composition_changed_ = false; |
| 154 result_text_.clear(); |
| 155 } |
| 156 |
| 157 } // namespace views |
OLD | NEW |