Index: ui/base/ime/remote_input_method_win_unittest.cc |
diff --git a/ui/base/ime/remote_input_method_win_unittest.cc b/ui/base/ime/remote_input_method_win_unittest.cc |
index bc171df3005012d5bf6bcc3e8cd61f64c0bcc328..39490b9a246928efd4356f4c546d8172ea9cc4ad 100644 |
--- a/ui/base/ime/remote_input_method_win_unittest.cc |
+++ b/ui/base/ime/remote_input_method_win_unittest.cc |
@@ -9,11 +9,13 @@ |
#include <vector> |
#include "base/memory/scoped_ptr.h" |
+#include "base/scoped_observer.h" |
#include "base/strings/string16.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "ui/base/ime/dummy_text_input_client.h" |
#include "ui/base/ime/input_method.h" |
#include "ui/base/ime/input_method_delegate.h" |
+#include "ui/base/ime/input_method_observer.h" |
#include "ui/base/ime/remote_input_method_delegate_win.h" |
#include "ui/events/event.h" |
@@ -161,6 +163,76 @@ class MockRemoteInputMethodDelegateWin |
DISALLOW_COPY_AND_ASSIGN(MockRemoteInputMethodDelegateWin); |
}; |
+class MockInputMethodObserver : public InputMethodObserver { |
+ public: |
+ MockInputMethodObserver() |
+ : on_text_input_type_changed_(0), |
+ on_caret_bounds_changed_(0), |
+ on_input_locale_changed_(0), |
+ on_text_input_state_changed_(0), |
+ on_input_method_destroyed_changed_(0) { |
+ } |
+ virtual ~MockInputMethodObserver() { |
+ } |
+ void Reset() { |
+ on_text_input_type_changed_ = 0; |
+ on_caret_bounds_changed_ = 0; |
+ on_input_locale_changed_ = 0; |
+ on_text_input_state_changed_ = 0; |
+ on_input_method_destroyed_changed_ = 0; |
+ } |
+ size_t on_text_input_type_changed() const { |
+ return on_text_input_type_changed_; |
+ } |
+ size_t on_caret_bounds_changed() const { |
+ return on_caret_bounds_changed_; |
+ } |
+ size_t on_input_locale_changed() const { |
+ return on_input_locale_changed_; |
+ } |
+ size_t on_text_input_state_changed() const { |
+ return on_text_input_state_changed_; |
+ } |
+ size_t on_input_method_destroyed_changed() const { |
+ return on_input_method_destroyed_changed_; |
+ } |
+ |
+ private: |
+ // Overriden from InputMethodObserver. |
+ virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE { |
+ ++on_text_input_type_changed_; |
+ } |
+ virtual void OnFocus() OVERRIDE { |
+ } |
+ virtual void OnBlur() OVERRIDE { |
+ } |
+ virtual void OnUntranslatedIMEMessage( |
+ const base::NativeEvent& event) OVERRIDE { |
+ } |
+ virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE { |
+ ++on_caret_bounds_changed_; |
+ } |
+ virtual void OnInputLocaleChanged() OVERRIDE { |
+ ++on_input_locale_changed_; |
+ } |
+ virtual void OnTextInputStateChanged(const TextInputClient* client) OVERRIDE { |
+ ++on_text_input_state_changed_; |
+ } |
+ virtual void OnInputMethodDestroyed(const InputMethod* client) OVERRIDE { |
+ ++on_input_method_destroyed_changed_; |
+ } |
+ |
+ size_t on_text_input_type_changed_; |
+ size_t on_caret_bounds_changed_; |
+ size_t on_input_locale_changed_; |
+ size_t on_text_input_state_changed_; |
+ size_t on_input_method_destroyed_changed_; |
+ DISALLOW_COPY_AND_ASSIGN(MockInputMethodObserver); |
+}; |
+ |
+typedef ScopedObserver<InputMethod, InputMethodObserver> |
+ InputMethodScopedObserver; |
+ |
TEST(RemoteInputMethodWinTest, RemoteInputMethodPrivateWin) { |
InputMethod* other_ptr = static_cast<InputMethod*>(NULL) + 1; |
@@ -298,6 +370,7 @@ TEST(RemoteInputMethodWinTest, DetachTextInputClient) { |
EXPECT_TRUE(mock_remote_delegate.composition_character_bounds().empty()); |
EXPECT_TRUE(mock_remote_delegate.input_scopes().empty()); |
} |
+ |
TEST(RemoteInputMethodWinTest, OnCaretBoundsChanged) { |
MockInputMethodDelegate delegate_; |
MockTextInputClient mock_text_input_client; |
@@ -572,5 +645,124 @@ TEST(RemoteInputMethodWinTest, DispatchKeyEvent_FabricatedChar) { |
mock_text_input_client.Reset(); |
} |
+TEST(RemoteInputMethodWinTest, OnTextInputStateChanged_Observer) { |
+ DummyTextInputClient text_input_client; |
+ DummyTextInputClient text_input_client_the_other; |
+ |
+ MockInputMethodObserver input_method_observer; |
+ MockInputMethodDelegate delegate_; |
+ scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
+ InputMethodScopedObserver scoped_observer(&input_method_observer); |
+ scoped_observer.Add(input_method.get()); |
+ |
+ input_method->SetFocusedTextInputClient(&text_input_client); |
+ ASSERT_EQ(&text_input_client, input_method->GetTextInputClient()); |
+ EXPECT_EQ(1u, input_method_observer.on_text_input_state_changed()); |
+ input_method_observer.Reset(); |
+ |
+ input_method->SetFocusedTextInputClient(&text_input_client); |
+ ASSERT_EQ(&text_input_client, input_method->GetTextInputClient()); |
+ EXPECT_EQ(0u, input_method_observer.on_text_input_state_changed()); |
+ input_method_observer.Reset(); |
+ |
+ input_method->SetFocusedTextInputClient(&text_input_client_the_other); |
+ ASSERT_EQ(&text_input_client_the_other, input_method->GetTextInputClient()); |
+ EXPECT_EQ(1u, input_method_observer.on_text_input_state_changed()); |
+ input_method_observer.Reset(); |
+ |
+ input_method->DetachTextInputClient(&text_input_client_the_other); |
+ ASSERT_TRUE(input_method->GetTextInputClient() == NULL); |
+ EXPECT_EQ(1u, input_method_observer.on_text_input_state_changed()); |
+ input_method_observer.Reset(); |
+} |
+ |
+TEST(RemoteInputMethodWinTest, OnCaretBoundsChanged_Observer) { |
+ DummyTextInputClient text_input_client; |
+ DummyTextInputClient text_input_client_the_other; |
+ |
+ MockInputMethodObserver input_method_observer; |
+ MockInputMethodDelegate delegate_; |
+ scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
+ InputMethodScopedObserver scoped_observer(&input_method_observer); |
+ scoped_observer.Add(input_method.get()); |
+ |
+ { |
+ SCOPED_TRACE("OnCaretBoundsChanged callback must not be fired when no text " |
+ "input client is focused"); |
+ ASSERT_EQ(NULL, input_method->GetTextInputClient()); |
+ |
+ input_method_observer.Reset(); |
+ input_method->OnCaretBoundsChanged(&text_input_client); |
+ EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); |
+ input_method->OnCaretBoundsChanged(NULL); |
+ EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); |
+ } |
+ |
+ { |
+ SCOPED_TRACE("OnCaretBoundsChanged callback must be fired when and only " |
+ "the event is notified from the focused text input client"); |
+ |
+ input_method->SetFocusedTextInputClient(&text_input_client); |
+ ASSERT_EQ(&text_input_client, input_method->GetTextInputClient()); |
+ |
+ // Must fire the event |
+ input_method_observer.Reset(); |
+ input_method->OnCaretBoundsChanged(&text_input_client); |
+ EXPECT_EQ(1u, input_method_observer.on_caret_bounds_changed()); |
+ |
+ // Must not fire the event |
+ input_method_observer.Reset(); |
+ input_method->OnCaretBoundsChanged(NULL); |
+ EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); |
+ |
+ // Must not fire the event |
+ input_method_observer.Reset(); |
+ input_method->OnCaretBoundsChanged(&text_input_client_the_other); |
+ EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); |
+ } |
+} |
+ |
+TEST(RemoteInputMethodWinTest, OnInputLocaleChanged_Observer) { |
+ DummyTextInputClient text_input_client; |
+ |
+ MockInputMethodObserver input_method_observer; |
+ |
+ MockInputMethodDelegate delegate_; |
+ scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
+ InputMethodScopedObserver scoped_observer(&input_method_observer); |
+ scoped_observer.Add(input_method.get()); |
+ |
+ { |
+ SCOPED_TRACE("OnInputLocaleChanged callback can be fired even when no text " |
+ "input client is focused"); |
+ ASSERT_EQ(NULL, input_method->GetTextInputClient()); |
+ |
+ input_method_observer.Reset(); |
+ input_method->OnInputLocaleChanged(); |
+ EXPECT_EQ(1u, input_method_observer.on_input_locale_changed()); |
+ |
+ input_method->SetFocusedTextInputClient(&text_input_client); |
+ input_method_observer.Reset(); |
+ input_method->OnInputLocaleChanged(); |
+ EXPECT_EQ(1u, input_method_observer.on_input_locale_changed()); |
+ } |
+} |
+ |
+TEST(RemoteInputMethodWinTest, OnInputMethodDestroyed_Observer) { |
+ DummyTextInputClient text_input_client; |
+ DummyTextInputClient text_input_client_the_other; |
+ |
+ MockInputMethodObserver input_method_observer; |
+ InputMethodScopedObserver scoped_observer(&input_method_observer); |
+ |
+ MockInputMethodDelegate delegate_; |
+ scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
+ input_method->AddObserver(&input_method_observer); |
+ |
+ EXPECT_EQ(0u, input_method_observer.on_input_method_destroyed_changed()); |
+ input_method.reset(); |
+ EXPECT_EQ(1u, input_method_observer.on_input_method_destroyed_changed()); |
+} |
+ |
} // namespace |
} // namespace ui |