Index: views/ime/mock_input_method.cc |
diff --git a/views/ime/mock_input_method.cc b/views/ime/mock_input_method.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..84c315b207153c434175f777f7a4d7bb1823c766 |
--- /dev/null |
+++ b/views/ime/mock_input_method.cc |
@@ -0,0 +1,193 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "views/ime/mock_input_method.h" |
+ |
+#include "base/basictypes.h" |
+#include "base/logging.h" |
+#include "ui/base/keycodes/keyboard_codes.h" |
+#include "views/events/event.h" |
+#include "views/widget/widget.h" |
+ |
+namespace views { |
+ |
+MockInputMethod::MockInputMethod() |
+ : delegate_(NULL), |
+ widget_(NULL), |
+ focused_view_(NULL), |
+ composition_changed_(false), |
+ focus_changed_(false), |
+ text_input_type_changed_(false), |
+ caret_bounds_changed_(false), |
+ cancel_composition_called_(false), |
+ locale_("en-US"), |
+ direction_(base::i18n::LEFT_TO_RIGHT), |
+ active_(true) { |
+} |
+ |
+MockInputMethod::~MockInputMethod() { |
+ if (widget_) { |
+ widget_->GetFocusManager()->RemoveFocusChangeListener(this); |
+ widget_ = NULL; |
+ } |
+} |
+ |
+void MockInputMethod::set_delegate(internal::InputMethodDelegate* delegate) { |
+ delegate_ = delegate; |
+} |
+ |
+void MockInputMethod::Init(Widget* widget) { |
+ DCHECK(widget); |
+ DCHECK(widget->GetFocusManager()); |
+ |
+ if (widget_) { |
+ NOTREACHED() << "The input method is already initialized."; |
+ return; |
+ } |
+ |
+ widget_ = widget; |
+ widget->GetFocusManager()->AddFocusChangeListener(this); |
+} |
+ |
+void MockInputMethod::DispatchKeyEvent(const KeyEvent& key) { |
+ bool handled = (composition_changed_ || result_text_.length()) && |
+ GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE; |
+ |
+ ClearStates(); |
+ if (delegate_) { |
+ if (handled) { |
+ KeyEvent mock_key(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, key.flags()); |
+ delegate_->DispatchKeyEventPostIME(mock_key); |
+ } else { |
+ delegate_->DispatchKeyEventPostIME(key); |
+ } |
+ } |
+ |
+ if (focus_changed_) |
+ return; |
+ |
+ TextInputClient* client = GetTextInputClient(); |
+ if (client) { |
+ if (handled) { |
+ if (result_text_.length()) |
+ client->InsertText(result_text_); |
+ if (composition_changed_) { |
+ if (composition_.text.length()) |
+ client->SetComposition(composition_); |
+ else |
+ client->ClearComposition(); |
+ } |
+ } else { |
+ char16 ch = key.GetCharacter(); |
+ client->InsertChar(ch, key.flags()); |
+ } |
+ } |
+ |
+ ClearResult(); |
+} |
+ |
+void MockInputMethod::OnTextInputTypeChanged(View* view) { |
+ DCHECK_EQ(focused_view_, view); |
+ text_input_type_changed_ = true; |
+} |
+ |
+void MockInputMethod::OnCaretBoundsChanged(View* view) { |
+ DCHECK_EQ(focused_view_, view); |
+ caret_bounds_changed_ = true; |
+} |
+ |
+void MockInputMethod::CancelComposition(View* view) { |
+ DCHECK_EQ(focused_view_, view); |
+ cancel_composition_called_ = true; |
+ ClearResult(); |
+} |
+ |
+std::string MockInputMethod::GetInputLocale() { |
+ return locale_; |
+} |
+ |
+base::i18n::TextDirection MockInputMethod::GetInputTextDirection() { |
+ return direction_; |
+} |
+ |
+bool MockInputMethod::IsActive() { |
+ return active_; |
+} |
+ |
+void MockInputMethod::FocusWillChange(View* focused_before, View* focused) { |
+ DCHECK_EQ(focused_view_, focused_before); |
+ TextInputClient* client = GetTextInputClient(); |
+ if (client && client->HasComposition()) |
+ client->ConfirmComposition(); |
+ focused_view_ = focused; |
+ focus_changed_ = true; |
+ ClearResult(); |
+} |
+ |
+ui::TextInputType MockInputMethod::GetTextInputType() const { |
+ TextInputClient* client = GetTextInputClient(); |
+ return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; |
+} |
+ |
+void MockInputMethod::Clear() { |
+ ClearStates(); |
+ ClearResult(); |
+} |
+ |
+void MockInputMethod::SetCompositionForNextKey( |
+ const ui::Composition& composition) { |
+ composition_changed_ = true; |
+ composition_ = composition; |
+} |
+ |
+void MockInputMethod::SetResultForNextKey(const string16& result) { |
+ result_text_ = result; |
+} |
+ |
+void MockInputMethod::SetInputLocale(const std::string& locale) { |
+ if (locale_ != locale) { |
+ locale_ = locale; |
+ OnInputMethodChanged(); |
+ } |
+} |
+ |
+void MockInputMethod::SetInputTextDirection( |
+ base::i18n::TextDirection direction) { |
+ if (direction_ != direction) { |
+ direction_ = direction; |
+ OnInputMethodChanged(); |
+ } |
+} |
+ |
+void MockInputMethod::SetActive(bool active) { |
+ if (active_ != active) { |
+ active_ = active; |
+ OnInputMethodChanged(); |
+ } |
+} |
+ |
+void MockInputMethod::ClearStates() { |
+ focus_changed_ = false; |
+ text_input_type_changed_ = false; |
+ caret_bounds_changed_ = false; |
+ cancel_composition_called_ = false; |
+} |
+ |
+void MockInputMethod::ClearResult() { |
+ composition_.Clear(); |
+ composition_changed_ = false; |
+ result_text_.clear(); |
+} |
+ |
+TextInputClient* MockInputMethod::GetTextInputClient() const { |
+ return focused_view_ ? focused_view_->GetTextInputClient() : NULL; |
+} |
+ |
+void MockInputMethod::OnInputMethodChanged() const { |
+ TextInputClient* client = GetTextInputClient(); |
+ if (client) |
+ client->OnInputMethodChanged(); |
+} |
+ |
+} // namespace views |