| 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 SimpleMockInputMethodBase : public InputMethodBase { | 113 class MockInputMethodBase : public InputMethodBase { |
| 114 public: | 114 public: |
| 115 SimpleMockInputMethodBase() { | 115 // Note: this class does not take the ownership of |verifier|. |
| 116 MockInputMethodBase(ClientChangeVerifier* verifier) |
| 117 : verifier_(verifier) { |
| 116 } | 118 } |
| 117 virtual ~SimpleMockInputMethodBase() { | 119 virtual ~MockInputMethodBase() { |
| 118 } | 120 } |
| 119 | 121 |
| 120 private: | 122 private: |
| 121 // Overriden from InputMethod. | 123 // Overriden from InputMethod. |
| 122 virtual bool OnUntranslatedIMEMessage( | 124 virtual bool OnUntranslatedIMEMessage( |
| 123 const base::NativeEvent& event, | 125 const base::NativeEvent& event, |
| 124 InputMethod::NativeEventResult* result) OVERRIDE { | 126 InputMethod::NativeEventResult* result) OVERRIDE { |
| 125 return false; | 127 return false; |
| 126 } | 128 } |
| 127 virtual bool DispatchKeyEvent(const ui::KeyEvent&) OVERRIDE { | 129 virtual bool DispatchKeyEvent(const ui::KeyEvent&) OVERRIDE { |
| 128 return false; | 130 return false; |
| 129 } | 131 } |
| 132 virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE { |
| 133 } |
| 130 virtual void CancelComposition(const TextInputClient* client) OVERRIDE { | 134 virtual void CancelComposition(const TextInputClient* client) OVERRIDE { |
| 131 } | 135 } |
| 132 virtual void OnInputLocaleChanged() OVERRIDE { | 136 virtual void OnInputLocaleChanged() OVERRIDE { |
| 133 } | 137 } |
| 134 virtual std::string GetInputLocale() OVERRIDE{ | 138 virtual std::string GetInputLocale() OVERRIDE{ |
| 135 return ""; | 139 return ""; |
| 136 } | 140 } |
| 137 virtual base::i18n::TextDirection GetInputTextDirection() OVERRIDE { | 141 virtual base::i18n::TextDirection GetInputTextDirection() OVERRIDE { |
| 138 return base::i18n::UNKNOWN_DIRECTION; | 142 return base::i18n::UNKNOWN_DIRECTION; |
| 139 } | 143 } |
| 140 virtual bool IsActive() OVERRIDE { | 144 virtual bool IsActive() OVERRIDE { |
| 141 return false; | 145 return false; |
| 142 } | 146 } |
| 143 virtual bool IsCandidatePopupOpen() const OVERRIDE { | 147 virtual bool IsCandidatePopupOpen() const OVERRIDE { |
| 144 return false; | 148 return false; |
| 145 } | 149 } |
| 146 DISALLOW_COPY_AND_ASSIGN(SimpleMockInputMethodBase); | |
| 147 }; | |
| 148 | |
| 149 class MockInputMethodBase : public SimpleMockInputMethodBase { | |
| 150 public: | |
| 151 // Note: this class does not take the ownership of |verifier|. | |
| 152 explicit MockInputMethodBase(ClientChangeVerifier* verifier) | |
| 153 : verifier_(verifier) { | |
| 154 } | |
| 155 virtual ~MockInputMethodBase() { | |
| 156 } | |
| 157 | |
| 158 private: | |
| 159 // Overriden from InputMethodBase. | 150 // Overriden from InputMethodBase. |
| 160 virtual void OnWillChangeFocusedClient(TextInputClient* focused_before, | 151 virtual void OnWillChangeFocusedClient(TextInputClient* focused_before, |
| 161 TextInputClient* focused) OVERRIDE { | 152 TextInputClient* focused) OVERRIDE { |
| 162 verifier_->OnWillChangeFocusedClient(focused_before, focused); | 153 verifier_->OnWillChangeFocusedClient(focused_before, focused); |
| 163 } | 154 } |
| 164 | 155 |
| 165 virtual void OnDidChangeFocusedClient(TextInputClient* focused_before, | 156 virtual void OnDidChangeFocusedClient(TextInputClient* focused_before, |
| 166 TextInputClient* focused) OVERRIDE { | 157 TextInputClient* focused) OVERRIDE { |
| 167 verifier_->OnDidChangeFocusedClient(focused_before, focused); | 158 verifier_->OnDidChangeFocusedClient(focused_before, focused); |
| 168 } | 159 } |
| 169 | 160 |
| 170 ClientChangeVerifier* verifier_; | 161 ClientChangeVerifier* verifier_; |
| 171 DISALLOW_COPY_AND_ASSIGN(MockInputMethodBase); | 162 DISALLOW_COPY_AND_ASSIGN(MockInputMethodBase); |
| 172 }; | 163 }; |
| 173 | 164 |
| 174 class SimpleMockInputMethodObserver : public InputMethodObserver { | 165 class MockInputMethodObserver : public InputMethodObserver { |
| 175 public: | |
| 176 SimpleMockInputMethodObserver() | |
| 177 : on_caret_bounds_changed_(0), | |
| 178 on_input_locale_changed_(0) { | |
| 179 } | |
| 180 virtual ~SimpleMockInputMethodObserver() { | |
| 181 } | |
| 182 void Reset() { | |
| 183 on_caret_bounds_changed_ = 0; | |
| 184 on_input_locale_changed_ = 0; | |
| 185 } | |
| 186 size_t on_caret_bounds_changed() const { | |
| 187 return on_caret_bounds_changed_; | |
| 188 } | |
| 189 | |
| 190 private: | |
| 191 // Overriden from InputMethodObserver. | |
| 192 virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE{ | |
| 193 } | |
| 194 virtual void OnFocus() OVERRIDE{ | |
| 195 } | |
| 196 virtual void OnBlur() OVERRIDE{ | |
| 197 } | |
| 198 virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE{ | |
| 199 ++on_caret_bounds_changed_; | |
| 200 } | |
| 201 virtual void OnTextInputStateChanged(const TextInputClient* client) OVERRIDE{ | |
| 202 } | |
| 203 virtual void OnInputMethodDestroyed(const InputMethod* client) OVERRIDE{ | |
| 204 } | |
| 205 | |
| 206 size_t on_caret_bounds_changed_; | |
| 207 size_t on_input_locale_changed_; | |
| 208 DISALLOW_COPY_AND_ASSIGN(SimpleMockInputMethodObserver); | |
| 209 }; | |
| 210 | |
| 211 class MockInputMethodObserver : public SimpleMockInputMethodObserver { | |
| 212 public: | 166 public: |
| 213 // Note: this class does not take the ownership of |verifier|. | 167 // Note: this class does not take the ownership of |verifier|. |
| 214 explicit MockInputMethodObserver(ClientChangeVerifier* verifier) | 168 explicit MockInputMethodObserver(ClientChangeVerifier* verifier) |
| 215 : verifier_(verifier) { | 169 : verifier_(verifier) { |
| 216 } | 170 } |
| 217 virtual ~MockInputMethodObserver() { | 171 virtual ~MockInputMethodObserver() { |
| 218 } | 172 } |
| 219 | 173 |
| 220 private: | 174 private: |
| 221 // Overriden from SimpleMockInputMethodObserver. | 175 virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE { |
| 222 virtual void OnTextInputStateChanged(const TextInputClient* client) OVERRIDE{ | 176 } |
| 177 virtual void OnFocus() OVERRIDE { |
| 178 } |
| 179 virtual void OnBlur() OVERRIDE { |
| 180 } |
| 181 virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE { |
| 182 } |
| 183 virtual void OnTextInputStateChanged(const TextInputClient* client) OVERRIDE { |
| 223 verifier_->OnTextInputStateChanged(client); | 184 verifier_->OnTextInputStateChanged(client); |
| 224 } | 185 } |
| 186 virtual void OnInputMethodDestroyed(const InputMethod* client) OVERRIDE { |
| 187 } |
| 225 | 188 |
| 226 ClientChangeVerifier* verifier_; | 189 ClientChangeVerifier* verifier_; |
| 227 DISALLOW_COPY_AND_ASSIGN(MockInputMethodObserver); | 190 DISALLOW_COPY_AND_ASSIGN(MockInputMethodObserver); |
| 228 }; | 191 }; |
| 229 | 192 |
| 230 typedef ScopedObserver<InputMethod, InputMethodObserver> | 193 typedef ScopedObserver<InputMethod, InputMethodObserver> |
| 231 InputMethodScopedObserver; | 194 InputMethodScopedObserver; |
| 232 | 195 |
| 233 TEST(InputMethodBaseTest, SetFocusedTextInputClient) { | 196 TEST(InputMethodBaseTest, SetFocusedTextInputClient) { |
| 234 DummyTextInputClient text_input_client_1st; | 197 DummyTextInputClient text_input_client_1st; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 ASSERT_EQ(&text_input_client, input_method.GetTextInputClient()); | 288 ASSERT_EQ(&text_input_client, input_method.GetTextInputClient()); |
| 326 input_method.OnBlur(); | 289 input_method.OnBlur(); |
| 327 input_method.OnFocus(); | 290 input_method.OnFocus(); |
| 328 verifier.ExpectClientChange(&text_input_client, NULL); | 291 verifier.ExpectClientChange(&text_input_client, NULL); |
| 329 input_method.DetachTextInputClient(&text_input_client); | 292 input_method.DetachTextInputClient(&text_input_client); |
| 330 EXPECT_EQ(NULL, input_method.GetTextInputClient()); | 293 EXPECT_EQ(NULL, input_method.GetTextInputClient()); |
| 331 verifier.Verify(); | 294 verifier.Verify(); |
| 332 } | 295 } |
| 333 } | 296 } |
| 334 | 297 |
| 335 TEST(InputMethodBaseTest, OnCaretBoundsChanged) { | |
| 336 DummyTextInputClient text_input_client; | |
| 337 DummyTextInputClient text_input_client_the_other; | |
| 338 | |
| 339 SimpleMockInputMethodBase input_method; | |
| 340 SimpleMockInputMethodObserver input_method_observer; | |
| 341 InputMethodScopedObserver scoped_observer(&input_method_observer); | |
| 342 scoped_observer.Add(&input_method); | |
| 343 | |
| 344 // Assume that the top-level-widget gains focus. | |
| 345 input_method.OnFocus(); | |
| 346 | |
| 347 { | |
| 348 SCOPED_TRACE("OnCaretBoundsChanged callback must not be fired when no text " | |
| 349 "input client is focused"); | |
| 350 ASSERT_EQ(NULL, input_method.GetTextInputClient()); | |
| 351 | |
| 352 input_method_observer.Reset(); | |
| 353 input_method.OnCaretBoundsChanged(&text_input_client); | |
| 354 EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); | |
| 355 input_method.OnCaretBoundsChanged(NULL); | |
| 356 EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); | |
| 357 } | |
| 358 | |
| 359 { | |
| 360 SCOPED_TRACE("OnCaretBoundsChanged callback must be fired when and only " | |
| 361 "the event is notified from the focused text input client"); | |
| 362 | |
| 363 input_method.SetFocusedTextInputClient(&text_input_client); | |
| 364 ASSERT_EQ(&text_input_client, input_method.GetTextInputClient()); | |
| 365 | |
| 366 // Must fire the event | |
| 367 input_method_observer.Reset(); | |
| 368 input_method.OnCaretBoundsChanged(&text_input_client); | |
| 369 EXPECT_EQ(1u, input_method_observer.on_caret_bounds_changed()); | |
| 370 | |
| 371 // Must not fire the event | |
| 372 input_method_observer.Reset(); | |
| 373 input_method.OnCaretBoundsChanged(NULL); | |
| 374 EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); | |
| 375 | |
| 376 // Must not fire the event | |
| 377 input_method_observer.Reset(); | |
| 378 input_method.OnCaretBoundsChanged(&text_input_client_the_other); | |
| 379 EXPECT_EQ(0u, input_method_observer.on_caret_bounds_changed()); | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 } // namespace | 298 } // namespace |
| 384 } // namespace ui | 299 } // namespace ui |
| OLD | NEW |