| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/ime/input_method_base.h" | 5 #include "ui/base/ime/input_method_base.h" |
| 6 | 6 |
| 7 #include "base/scoped_observer.h" | 7 #include "base/scoped_observer.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/base/ime/dummy_text_input_client.h" | 9 #include "ui/base/ime/dummy_text_input_client.h" |
| 10 #include "ui/base/ime/input_method_observer.h" | 10 #include "ui/base/ime/input_method_observer.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 TextInputClient* previous_client_; | 103 TextInputClient* previous_client_; |
| 104 TextInputClient* next_client_; | 104 TextInputClient* next_client_; |
| 105 bool call_expected_; | 105 bool call_expected_; |
| 106 bool on_will_change_focused_client_called_; | 106 bool on_will_change_focused_client_called_; |
| 107 bool on_did_change_focused_client_called_; | 107 bool on_did_change_focused_client_called_; |
| 108 bool on_text_input_state_changed_; | 108 bool on_text_input_state_changed_; |
| 109 | 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(ClientChangeVerifier); | 110 DISALLOW_COPY_AND_ASSIGN(ClientChangeVerifier); |
| 111 }; | 111 }; |
| 112 | 112 |
| 113 class MockInputMethodBase : public InputMethodBase { | 113 class SimpleMockInputMethodBase : public InputMethodBase { |
| 114 public: | 114 public: |
| 115 // Note: this class does not take the ownership of |verifier|. | 115 SimpleMockInputMethodBase() { |
| 116 explicit MockInputMethodBase(ClientChangeVerifier* verifier) | |
| 117 : verifier_(verifier) { | |
| 118 } | 116 } |
| 119 virtual ~MockInputMethodBase() { | 117 virtual ~SimpleMockInputMethodBase() { |
| 120 } | 118 } |
| 121 | 119 |
| 122 private: | 120 private: |
| 123 // Overriden from InputMethod. | 121 // Overriden from InputMethod. |
| 124 virtual bool OnUntranslatedIMEMessage( | 122 virtual bool OnUntranslatedIMEMessage( |
| 125 const base::NativeEvent& event, | 123 const base::NativeEvent& event, |
| 126 InputMethod::NativeEventResult* result) OVERRIDE { | 124 InputMethod::NativeEventResult* result) OVERRIDE { |
| 127 return false; | 125 return false; |
| 128 } | 126 } |
| 129 virtual bool DispatchKeyEvent(const base::NativeEvent&) OVERRIDE { | 127 virtual bool DispatchKeyEvent(const base::NativeEvent&) OVERRIDE { |
| 130 return false; | 128 return false; |
| 131 } | 129 } |
| 132 virtual bool DispatchFabricatedKeyEvent(const ui::KeyEvent&) OVERRIDE { | 130 virtual bool DispatchFabricatedKeyEvent(const ui::KeyEvent&) OVERRIDE { |
| 133 return false; | 131 return false; |
| 134 } | 132 } |
| 135 virtual void OnCaretBoundsChanged(const TextInputClient* clien) OVERRIDE { | 133 virtual void CancelComposition(const TextInputClient* client) OVERRIDE { |
| 136 } | 134 } |
| 137 virtual void CancelComposition(const TextInputClient* clien) OVERRIDE { | 135 virtual std::string GetInputLocale() OVERRIDE{ |
| 138 } | |
| 139 virtual void OnInputLocaleChanged() OVERRIDE { | |
| 140 } | |
| 141 virtual std::string GetInputLocale() OVERRIDE { | |
| 142 return ""; | 136 return ""; |
| 143 } | 137 } |
| 144 virtual base::i18n::TextDirection GetInputTextDirection() OVERRIDE { | 138 virtual base::i18n::TextDirection GetInputTextDirection() OVERRIDE { |
| 145 return base::i18n::UNKNOWN_DIRECTION; | 139 return base::i18n::UNKNOWN_DIRECTION; |
| 146 } | 140 } |
| 147 virtual bool IsActive() OVERRIDE { | 141 virtual bool IsActive() OVERRIDE { |
| 148 return false; | 142 return false; |
| 149 } | 143 } |
| 150 virtual bool IsCandidatePopupOpen() const OVERRIDE { | 144 virtual bool IsCandidatePopupOpen() const OVERRIDE { |
| 151 return false; | 145 return false; |
| 152 } | 146 } |
| 147 DISALLOW_COPY_AND_ASSIGN(SimpleMockInputMethodBase); |
| 148 }; |
| 153 | 149 |
| 150 class MockInputMethodBase : public SimpleMockInputMethodBase { |
| 151 public: |
| 152 // Note: this class does not take the ownership of |verifier|. |
| 153 explicit MockInputMethodBase(ClientChangeVerifier* verifier) |
| 154 : verifier_(verifier) { |
| 155 } |
| 156 virtual ~MockInputMethodBase() { |
| 157 } |
| 158 |
| 159 private: |
| 154 // Overriden from InputMethodBase. | 160 // Overriden from InputMethodBase. |
| 155 virtual void OnWillChangeFocusedClient(TextInputClient* focused_before, | 161 virtual void OnWillChangeFocusedClient(TextInputClient* focused_before, |
| 156 TextInputClient* focused) OVERRIDE { | 162 TextInputClient* focused) OVERRIDE { |
| 157 verifier_->OnWillChangeFocusedClient(focused_before, focused); | 163 verifier_->OnWillChangeFocusedClient(focused_before, focused); |
| 158 } | 164 } |
| 159 | 165 |
| 160 virtual void OnDidChangeFocusedClient(TextInputClient* focused_before, | 166 virtual void OnDidChangeFocusedClient(TextInputClient* focused_before, |
| 161 TextInputClient* focused) OVERRIDE { | 167 TextInputClient* focused) OVERRIDE { |
| 162 verifier_->OnDidChangeFocusedClient(focused_before, focused); | 168 verifier_->OnDidChangeFocusedClient(focused_before, focused); |
| 163 } | 169 } |
| 164 | 170 |
| 165 ClientChangeVerifier* verifier_; | 171 ClientChangeVerifier* verifier_; |
| 166 DISALLOW_COPY_AND_ASSIGN(MockInputMethodBase); | 172 DISALLOW_COPY_AND_ASSIGN(MockInputMethodBase); |
| 167 }; | 173 }; |
| 168 | 174 |
| 169 class MockInputMethodObserver : public InputMethodObserver { | 175 class SimpleMockInputMethodObserver : public InputMethodObserver { |
| 176 public: |
| 177 SimpleMockInputMethodObserver() |
| 178 : on_caret_bounds_changed_(0), |
| 179 on_input_locale_changed_(0) { |
| 180 } |
| 181 virtual ~SimpleMockInputMethodObserver() { |
| 182 } |
| 183 void Reset() { |
| 184 on_caret_bounds_changed_ = 0; |
| 185 on_input_locale_changed_ = 0; |
| 186 } |
| 187 size_t on_caret_bounds_changed() const { |
| 188 return on_caret_bounds_changed_; |
| 189 } |
| 190 size_t on_input_locale_changed() const { |
| 191 return on_input_locale_changed_; |
| 192 } |
| 193 |
| 194 private: |
| 195 // Overriden from InputMethodObserver. |
| 196 virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE{ |
| 197 } |
| 198 virtual void OnFocus() OVERRIDE{ |
| 199 } |
| 200 virtual void OnBlur() OVERRIDE{ |
| 201 } |
| 202 virtual void OnUntranslatedIMEMessage( |
| 203 const base::NativeEvent& event) OVERRIDE{ |
| 204 } |
| 205 virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE{ |
| 206 ++on_caret_bounds_changed_; |
| 207 } |
| 208 virtual void OnInputLocaleChanged() OVERRIDE{ |
| 209 ++on_input_locale_changed_; |
| 210 } |
| 211 virtual void OnTextInputStateChanged(const TextInputClient* client) OVERRIDE{ |
| 212 } |
| 213 virtual void OnInputMethodDestroyed(const InputMethod* client) OVERRIDE{ |
| 214 } |
| 215 |
| 216 size_t on_caret_bounds_changed_; |
| 217 size_t on_input_locale_changed_; |
| 218 DISALLOW_COPY_AND_ASSIGN(SimpleMockInputMethodObserver); |
| 219 }; |
| 220 |
| 221 class MockInputMethodObserver : public SimpleMockInputMethodObserver { |
| 170 public: | 222 public: |
| 171 // Note: this class does not take the ownership of |verifier|. | 223 // Note: this class does not take the ownership of |verifier|. |
| 172 explicit MockInputMethodObserver(ClientChangeVerifier* verifier) | 224 explicit MockInputMethodObserver(ClientChangeVerifier* verifier) |
| 173 : verifier_(verifier) { | 225 : verifier_(verifier) { |
| 174 } | 226 } |
| 175 virtual ~MockInputMethodObserver() { | 227 virtual ~MockInputMethodObserver() { |
| 176 } | 228 } |
| 177 | 229 |
| 178 private: | 230 private: |
| 179 virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE { | 231 // Overriden from SimpleMockInputMethodObserver. |
| 180 } | 232 virtual void OnTextInputStateChanged(const TextInputClient* client) OVERRIDE{ |
| 181 virtual void OnFocus() OVERRIDE { | |
| 182 } | |
| 183 virtual void OnBlur() OVERRIDE { | |
| 184 } | |
| 185 virtual void OnUntranslatedIMEMessage( | |
| 186 const base::NativeEvent& event) OVERRIDE { | |
| 187 } | |
| 188 virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE { | |
| 189 } | |
| 190 virtual void OnInputLocaleChanged() OVERRIDE { | |
| 191 } | |
| 192 virtual void OnTextInputStateChanged(const TextInputClient* client) OVERRIDE { | |
| 193 verifier_->OnTextInputStateChanged(client); | 233 verifier_->OnTextInputStateChanged(client); |
| 194 } | 234 } |
| 195 virtual void OnInputMethodDestroyed(const InputMethod* client) OVERRIDE { | |
| 196 } | |
| 197 | 235 |
| 198 ClientChangeVerifier* verifier_; | 236 ClientChangeVerifier* verifier_; |
| 199 DISALLOW_COPY_AND_ASSIGN(MockInputMethodObserver); | 237 DISALLOW_COPY_AND_ASSIGN(MockInputMethodObserver); |
| 200 }; | 238 }; |
| 201 | 239 |
| 202 typedef ScopedObserver<InputMethod, InputMethodObserver> | 240 typedef ScopedObserver<InputMethod, InputMethodObserver> |
| 203 InputMethodScopedObserver; | 241 InputMethodScopedObserver; |
| 204 | 242 |
| 205 TEST(InputMethodBaseTest, SetFocusedTextInputClient) { | 243 TEST(InputMethodBaseTest, SetFocusedTextInputClient) { |
| 206 DummyTextInputClient text_input_client_1st; | 244 DummyTextInputClient text_input_client_1st; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 SCOPED_TRACE("DetachTextInputClient must be supported for sticky client"); | 423 SCOPED_TRACE("DetachTextInputClient must be supported for sticky client"); |
| 386 | 424 |
| 387 ASSERT_EQ(&sticky_client, input_method.GetTextInputClient()); | 425 ASSERT_EQ(&sticky_client, input_method.GetTextInputClient()); |
| 388 verifier.ExpectClientChange(&sticky_client, NULL); | 426 verifier.ExpectClientChange(&sticky_client, NULL); |
| 389 input_method.DetachTextInputClient(&sticky_client); | 427 input_method.DetachTextInputClient(&sticky_client); |
| 390 EXPECT_EQ(NULL, input_method.GetTextInputClient()); | 428 EXPECT_EQ(NULL, input_method.GetTextInputClient()); |
| 391 verifier.Verify(); | 429 verifier.Verify(); |
| 392 } | 430 } |
| 393 } | 431 } |
| 394 | 432 |
| 433 TEST(InputMethodBaseTest, OnCaretBoundsChanged) { |
| 434 DummyTextInputClient text_input_client; |
| 435 DummyTextInputClient text_input_client_the_other; |
| 436 |
| 437 SimpleMockInputMethodBase input_method; |
| 438 SimpleMockInputMethodObserver input_method_observer; |
| 439 InputMethodScopedObserver scoped_observer(&input_method_observer); |
| 440 scoped_observer.Add(&input_method); |
| 441 |
| 442 // Assume that the top-level-widget gains focus. |
| 443 input_method.OnFocus(); |
| 444 |
| 445 { |
| 446 SCOPED_TRACE("OnCaretBoundsChanged callback must not be fired when no text " |
| 447 "input client is focused"); |
| 448 ASSERT_EQ(NULL, input_method.GetTextInputClient()); |
| 449 |
| 450 input_method_observer.Reset(); |
| 451 input_method.OnCaretBoundsChanged(&text_input_client); |
| 452 EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); |
| 453 input_method.OnCaretBoundsChanged(NULL); |
| 454 EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); |
| 455 } |
| 456 |
| 457 { |
| 458 SCOPED_TRACE("OnCaretBoundsChanged callback must be fired when and only " |
| 459 "the event is notified from the focused text input client"); |
| 460 |
| 461 input_method.SetFocusedTextInputClient(&text_input_client); |
| 462 ASSERT_EQ(&text_input_client, input_method.GetTextInputClient()); |
| 463 |
| 464 // Must fire the event |
| 465 input_method_observer.Reset(); |
| 466 input_method.OnCaretBoundsChanged(&text_input_client); |
| 467 EXPECT_EQ(1u, input_method_observer.on_caret_bounds_changed()); |
| 468 |
| 469 // Must not fire the event |
| 470 input_method_observer.Reset(); |
| 471 input_method.OnCaretBoundsChanged(NULL); |
| 472 EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); |
| 473 |
| 474 // Must not fire the event |
| 475 input_method_observer.Reset(); |
| 476 input_method.OnCaretBoundsChanged(&text_input_client_the_other); |
| 477 EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); |
| 478 } |
| 479 } |
| 480 |
| 481 TEST(InputMethodBaseTest, OnInputLocaleChanged) { |
| 482 DummyTextInputClient text_input_client; |
| 483 |
| 484 SimpleMockInputMethodBase input_method; |
| 485 SimpleMockInputMethodObserver input_method_observer; |
| 486 InputMethodScopedObserver scoped_observer(&input_method_observer); |
| 487 scoped_observer.Add(&input_method); |
| 488 |
| 489 // Assume that the top-level-widget gains focus. |
| 490 input_method.OnFocus(); |
| 491 |
| 492 { |
| 493 SCOPED_TRACE("OnInputLocaleChanged callback can be fired even when no text " |
| 494 "input client is focused"); |
| 495 ASSERT_EQ(NULL, input_method.GetTextInputClient()); |
| 496 |
| 497 input_method_observer.Reset(); |
| 498 input_method.OnInputLocaleChanged(); |
| 499 EXPECT_EQ(1u, input_method_observer.on_input_locale_changed()); |
| 500 |
| 501 input_method.SetFocusedTextInputClient(&text_input_client); |
| 502 input_method_observer.Reset(); |
| 503 input_method.OnInputLocaleChanged(); |
| 504 EXPECT_EQ(1u, input_method_observer.on_input_locale_changed()); |
| 505 } |
| 506 } |
| 507 |
| 395 } // namespace | 508 } // namespace |
| 396 } // namespace ui | 509 } // namespace ui |
| OLD | NEW |