OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/base/ime/remote_input_method_win.h" |
| 6 |
| 7 #include <InputScope.h> |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/string16.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/base/ime/dummy_text_input_client.h" |
| 15 #include "ui/base/ime/input_method.h" |
| 16 #include "ui/base/ime/input_method_delegate.h" |
| 17 #include "ui/base/ime/remote_input_method_delegate_win.h" |
| 18 #include "ui/events/event.h" |
| 19 |
| 20 namespace ui { |
| 21 namespace { |
| 22 |
| 23 class MockTextInputClient : public DummyTextInputClient { |
| 24 public: |
| 25 MockTextInputClient() |
| 26 : text_input_type_(TEXT_INPUT_TYPE_NONE), |
| 27 text_input_mode_(TEXT_INPUT_MODE_DEFAULT) { |
| 28 } |
| 29 |
| 30 const base::string16& inserted_text() const { |
| 31 return inserted_text_; |
| 32 } |
| 33 void Reset() { |
| 34 inserted_text_.clear(); |
| 35 text_input_type_ = TEXT_INPUT_TYPE_NONE; |
| 36 text_input_mode_ = TEXT_INPUT_MODE_DEFAULT; |
| 37 caret_bounds_ = gfx::Rect(); |
| 38 composition_character_bounds_.clear(); |
| 39 } |
| 40 void set_text_input_type(ui::TextInputType type) { |
| 41 text_input_type_ = type; |
| 42 } |
| 43 void set_text_input_mode(ui::TextInputMode mode) { |
| 44 text_input_mode_ = mode; |
| 45 } |
| 46 void set_caret_bounds(const gfx::Rect& caret_bounds) { |
| 47 caret_bounds_ = caret_bounds; |
| 48 } |
| 49 void set_composition_character_bounds( |
| 50 const std::vector<gfx::Rect>& composition_character_bounds) { |
| 51 composition_character_bounds_ = composition_character_bounds; |
| 52 } |
| 53 |
| 54 private: |
| 55 virtual void InsertChar(char16 ch, int flags) OVERRIDE{ |
| 56 inserted_text_.append(1, ch); |
| 57 } |
| 58 virtual void InsertText(const string16& text) OVERRIDE{ |
| 59 EXPECT_TRUE(false) << "RemoteInputMethodWin does not use this method"; |
| 60 } |
| 61 virtual ui::TextInputType GetTextInputType() const OVERRIDE { |
| 62 return text_input_type_; |
| 63 } |
| 64 virtual ui::TextInputMode GetTextInputMode() const OVERRIDE { |
| 65 return text_input_mode_; |
| 66 } |
| 67 virtual gfx::Rect GetCaretBounds() const { |
| 68 return caret_bounds_; |
| 69 } |
| 70 virtual bool GetCompositionCharacterBounds(uint32 index, |
| 71 gfx::Rect* rect) const OVERRIDE { |
| 72 if (!rect || composition_character_bounds_.size() <= index) |
| 73 return false; |
| 74 *rect = composition_character_bounds_[index]; |
| 75 return true; |
| 76 } |
| 77 virtual bool HasCompositionText() const OVERRIDE { |
| 78 return !composition_character_bounds_.empty(); |
| 79 } |
| 80 |
| 81 ui::TextInputType text_input_type_; |
| 82 ui::TextInputMode text_input_mode_; |
| 83 gfx::Rect caret_bounds_; |
| 84 std::vector<gfx::Rect> composition_character_bounds_; |
| 85 base::string16 inserted_text_; |
| 86 DISALLOW_COPY_AND_ASSIGN(MockTextInputClient); |
| 87 }; |
| 88 |
| 89 class MockInputMethodDelegate : public internal::InputMethodDelegate { |
| 90 public: |
| 91 MockInputMethodDelegate() {} |
| 92 |
| 93 const std::vector<ui::KeyboardCode>& fabricated_key_events() const { |
| 94 return fabricated_key_events_; |
| 95 } |
| 96 void Reset() { |
| 97 fabricated_key_events_.clear(); |
| 98 } |
| 99 |
| 100 private: |
| 101 virtual bool DispatchKeyEventPostIME( |
| 102 const base::NativeEvent& native_key_event) OVERRIDE { |
| 103 EXPECT_TRUE(false) << "Not reach here"; |
| 104 return true; |
| 105 } |
| 106 virtual bool DispatchFabricatedKeyEventPostIME(ui::EventType type, |
| 107 ui::KeyboardCode key_code, |
| 108 int flags) OVERRIDE { |
| 109 fabricated_key_events_.push_back(key_code); |
| 110 return true; |
| 111 } |
| 112 |
| 113 std::vector<ui::KeyboardCode> fabricated_key_events_; |
| 114 DISALLOW_COPY_AND_ASSIGN(MockInputMethodDelegate); |
| 115 }; |
| 116 |
| 117 class MockRemoteInputMethodDelegateWin |
| 118 : public internal::RemoteInputMethodDelegateWin { |
| 119 public: |
| 120 MockRemoteInputMethodDelegateWin() |
| 121 : cancel_composition_called_(false), |
| 122 text_input_client_updated_called_(false) { |
| 123 } |
| 124 |
| 125 bool cancel_composition_called() const { |
| 126 return cancel_composition_called_; |
| 127 } |
| 128 bool text_input_client_updated_called() const { |
| 129 return text_input_client_updated_called_; |
| 130 } |
| 131 const std::vector<int32>& input_scopes() const { |
| 132 return input_scopes_; |
| 133 } |
| 134 const std::vector<gfx::Rect>& composition_character_bounds() const { |
| 135 return composition_character_bounds_; |
| 136 } |
| 137 void Reset() { |
| 138 cancel_composition_called_ = false; |
| 139 text_input_client_updated_called_ = false; |
| 140 input_scopes_.clear(); |
| 141 composition_character_bounds_.clear(); |
| 142 } |
| 143 |
| 144 private: |
| 145 virtual void CancelComposition() OVERRIDE { |
| 146 cancel_composition_called_ = true; |
| 147 } |
| 148 |
| 149 virtual void OnTextInputClientUpdated( |
| 150 const std::vector<int32>& input_scopes, |
| 151 const std::vector<gfx::Rect>& composition_character_bounds) OVERRIDE { |
| 152 text_input_client_updated_called_ = true; |
| 153 input_scopes_ = input_scopes; |
| 154 composition_character_bounds_ = composition_character_bounds; |
| 155 } |
| 156 |
| 157 bool cancel_composition_called_; |
| 158 bool text_input_client_updated_called_; |
| 159 std::vector<int32> input_scopes_; |
| 160 std::vector<gfx::Rect> composition_character_bounds_; |
| 161 DISALLOW_COPY_AND_ASSIGN(MockRemoteInputMethodDelegateWin); |
| 162 }; |
| 163 |
| 164 TEST(RemoteInputMethodWinTest, RemoteInputMethodPrivateWin) { |
| 165 InputMethod* other_ptr = static_cast<InputMethod*>(NULL) + 1; |
| 166 |
| 167 // Use typed NULL to make EXPECT_NE happy until nullptr becomes available. |
| 168 RemoteInputMethodPrivateWin* kNull = |
| 169 static_cast<RemoteInputMethodPrivateWin*>(NULL); |
| 170 EXPECT_EQ(kNull, RemoteInputMethodPrivateWin::Get(other_ptr)); |
| 171 |
| 172 MockInputMethodDelegate delegate_; |
| 173 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 174 EXPECT_NE(kNull, RemoteInputMethodPrivateWin::Get(input_method.get())); |
| 175 |
| 176 InputMethod* dangling_ptr = input_method.get(); |
| 177 input_method.reset(NULL); |
| 178 EXPECT_EQ(kNull, RemoteInputMethodPrivateWin::Get(dangling_ptr)); |
| 179 } |
| 180 |
| 181 TEST(RemoteInputMethodWinTest, OnInputSourceChanged) { |
| 182 MockInputMethodDelegate delegate_; |
| 183 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 184 RemoteInputMethodPrivateWin* private_ptr = |
| 185 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 186 ASSERT_TRUE(private_ptr != NULL); |
| 187 |
| 188 private_ptr->OnInputSourceChanged( |
| 189 MAKELANGID(LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN), true); |
| 190 EXPECT_EQ("ja-JP", input_method->GetInputLocale()); |
| 191 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, |
| 192 input_method->GetInputTextDirection()); |
| 193 |
| 194 private_ptr->OnInputSourceChanged( |
| 195 MAKELANGID(LANG_ARABIC, SUBLANG_ARABIC_QATAR), true); |
| 196 EXPECT_EQ("ar-QA", input_method->GetInputLocale()); |
| 197 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, |
| 198 input_method->GetInputTextDirection()); |
| 199 } |
| 200 |
| 201 TEST(RemoteInputMethodWinTest, OnCandidatePopupChanged) { |
| 202 MockInputMethodDelegate delegate_; |
| 203 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 204 RemoteInputMethodPrivateWin* private_ptr = |
| 205 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 206 ASSERT_TRUE(private_ptr != NULL); |
| 207 |
| 208 // Initial value |
| 209 EXPECT_FALSE(input_method->IsCandidatePopupOpen()); |
| 210 |
| 211 private_ptr->OnCandidatePopupChanged(true); |
| 212 EXPECT_TRUE(input_method->IsCandidatePopupOpen()); |
| 213 |
| 214 private_ptr->OnCandidatePopupChanged(false); |
| 215 EXPECT_FALSE(input_method->IsCandidatePopupOpen()); |
| 216 } |
| 217 |
| 218 TEST(RemoteInputMethodWinTest, CancelComposition) { |
| 219 MockInputMethodDelegate delegate_; |
| 220 MockTextInputClient mock_text_input_client; |
| 221 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 222 |
| 223 // This must not cause a crash. |
| 224 input_method->CancelComposition(&mock_text_input_client); |
| 225 |
| 226 RemoteInputMethodPrivateWin* private_ptr = |
| 227 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 228 ASSERT_TRUE(private_ptr != NULL); |
| 229 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 230 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 231 |
| 232 input_method->CancelComposition(&mock_text_input_client); |
| 233 EXPECT_FALSE(mock_remote_delegate.cancel_composition_called()); |
| 234 |
| 235 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 236 input_method->CancelComposition(&mock_text_input_client); |
| 237 EXPECT_TRUE(mock_remote_delegate.cancel_composition_called()); |
| 238 } |
| 239 |
| 240 TEST(RemoteInputMethodWinTest, SetFocusedTextInputClient) { |
| 241 MockInputMethodDelegate delegate_; |
| 242 MockTextInputClient mock_text_input_client; |
| 243 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 244 |
| 245 mock_text_input_client.set_caret_bounds(gfx::Rect(10, 0, 10, 20)); |
| 246 mock_text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_URL); |
| 247 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 248 |
| 249 RemoteInputMethodPrivateWin* private_ptr = |
| 250 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 251 ASSERT_TRUE(private_ptr != NULL); |
| 252 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 253 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 254 |
| 255 // Initial state must be synced. |
| 256 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 257 ASSERT_EQ(1, mock_remote_delegate.composition_character_bounds().size()); |
| 258 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), |
| 259 mock_remote_delegate.composition_character_bounds()[0]); |
| 260 ASSERT_EQ(1, mock_remote_delegate.input_scopes().size()); |
| 261 EXPECT_EQ(IS_URL, mock_remote_delegate.input_scopes()[0]); |
| 262 |
| 263 // State must be cleared by SetFocusedTextInputClient(NULL). |
| 264 mock_remote_delegate.Reset(); |
| 265 input_method->SetFocusedTextInputClient(NULL); |
| 266 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 267 EXPECT_TRUE(mock_remote_delegate.composition_character_bounds().empty()); |
| 268 EXPECT_TRUE(mock_remote_delegate.input_scopes().empty()); |
| 269 } |
| 270 |
| 271 TEST(RemoteInputMethodWinTest, DetachTextInputClient) { |
| 272 MockInputMethodDelegate delegate_; |
| 273 MockTextInputClient mock_text_input_client; |
| 274 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 275 |
| 276 mock_text_input_client.set_caret_bounds(gfx::Rect(10, 0, 10, 20)); |
| 277 mock_text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_URL); |
| 278 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 279 |
| 280 RemoteInputMethodPrivateWin* private_ptr = |
| 281 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 282 ASSERT_TRUE(private_ptr != NULL); |
| 283 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 284 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 285 |
| 286 // Initial state must be synced. |
| 287 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 288 ASSERT_EQ(1, mock_remote_delegate.composition_character_bounds().size()); |
| 289 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), |
| 290 mock_remote_delegate.composition_character_bounds()[0]); |
| 291 ASSERT_EQ(1, mock_remote_delegate.input_scopes().size()); |
| 292 EXPECT_EQ(IS_URL, mock_remote_delegate.input_scopes()[0]); |
| 293 |
| 294 // State must be cleared by DetachTextInputClient |
| 295 mock_remote_delegate.Reset(); |
| 296 input_method->DetachTextInputClient(&mock_text_input_client); |
| 297 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 298 EXPECT_TRUE(mock_remote_delegate.composition_character_bounds().empty()); |
| 299 EXPECT_TRUE(mock_remote_delegate.input_scopes().empty()); |
| 300 } |
| 301 TEST(RemoteInputMethodWinTest, OnCaretBoundsChanged) { |
| 302 MockInputMethodDelegate delegate_; |
| 303 MockTextInputClient mock_text_input_client; |
| 304 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 305 |
| 306 // This must not cause a crash. |
| 307 input_method->OnCaretBoundsChanged(&mock_text_input_client); |
| 308 |
| 309 mock_text_input_client.set_caret_bounds(gfx::Rect(10, 0, 10, 20)); |
| 310 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 311 |
| 312 RemoteInputMethodPrivateWin* private_ptr = |
| 313 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 314 ASSERT_TRUE(private_ptr != NULL); |
| 315 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 316 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 317 |
| 318 // Initial state must be synced. |
| 319 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 320 ASSERT_EQ(1, mock_remote_delegate.composition_character_bounds().size()); |
| 321 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), |
| 322 mock_remote_delegate.composition_character_bounds()[0]); |
| 323 |
| 324 // Redundant OnCaretBoundsChanged must be ignored. |
| 325 mock_remote_delegate.Reset(); |
| 326 input_method->OnCaretBoundsChanged(&mock_text_input_client); |
| 327 EXPECT_FALSE(mock_remote_delegate.text_input_client_updated_called()); |
| 328 |
| 329 // Check OnCaretBoundsChanged is handled. (w/o composition) |
| 330 mock_remote_delegate.Reset(); |
| 331 mock_text_input_client.Reset(); |
| 332 mock_text_input_client.set_caret_bounds(gfx::Rect(10, 20, 30, 40)); |
| 333 input_method->OnCaretBoundsChanged(&mock_text_input_client); |
| 334 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 335 ASSERT_EQ(1, mock_remote_delegate.composition_character_bounds().size()); |
| 336 EXPECT_EQ(gfx::Rect(10, 20, 30, 40), |
| 337 mock_remote_delegate.composition_character_bounds()[0]); |
| 338 |
| 339 // Check OnCaretBoundsChanged is handled. (w/ composition) |
| 340 { |
| 341 mock_remote_delegate.Reset(); |
| 342 mock_text_input_client.Reset(); |
| 343 |
| 344 std::vector<gfx::Rect> bounds; |
| 345 bounds.push_back(gfx::Rect(10, 20, 30, 40)); |
| 346 bounds.push_back(gfx::Rect(40, 30, 20, 10)); |
| 347 mock_text_input_client.set_composition_character_bounds(bounds); |
| 348 input_method->OnCaretBoundsChanged(&mock_text_input_client); |
| 349 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 350 EXPECT_EQ(bounds, mock_remote_delegate.composition_character_bounds()); |
| 351 } |
| 352 } |
| 353 |
| 354 TEST(RemoteInputMethodWinTest, OnTextInputTypeChanged) { |
| 355 MockInputMethodDelegate delegate_; |
| 356 MockTextInputClient mock_text_input_client; |
| 357 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 358 |
| 359 // This must not cause a crash. |
| 360 input_method->OnCaretBoundsChanged(&mock_text_input_client); |
| 361 |
| 362 mock_text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_URL); |
| 363 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 364 |
| 365 RemoteInputMethodPrivateWin* private_ptr = |
| 366 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 367 ASSERT_TRUE(private_ptr != NULL); |
| 368 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 369 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 370 |
| 371 // Initial state must be synced. |
| 372 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 373 ASSERT_EQ(1, mock_remote_delegate.input_scopes().size()); |
| 374 EXPECT_EQ(IS_URL, mock_remote_delegate.input_scopes()[0]); |
| 375 |
| 376 // Check TEXT_INPUT_TYPE_NONE is handled. |
| 377 mock_remote_delegate.Reset(); |
| 378 mock_text_input_client.Reset(); |
| 379 mock_text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_NONE); |
| 380 mock_text_input_client.set_text_input_mode(ui::TEXT_INPUT_MODE_KATAKANA); |
| 381 input_method->OnTextInputTypeChanged(&mock_text_input_client); |
| 382 EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called()); |
| 383 EXPECT_TRUE(mock_remote_delegate.input_scopes().empty()); |
| 384 |
| 385 // Redundant OnTextInputTypeChanged must be ignored. |
| 386 mock_remote_delegate.Reset(); |
| 387 input_method->OnTextInputTypeChanged(&mock_text_input_client); |
| 388 EXPECT_FALSE(mock_remote_delegate.text_input_client_updated_called()); |
| 389 |
| 390 mock_remote_delegate.Reset(); |
| 391 mock_text_input_client.Reset(); |
| 392 mock_text_input_client.set_caret_bounds(gfx::Rect(10, 20, 30, 40)); |
| 393 input_method->OnCaretBoundsChanged(&mock_text_input_client); |
| 394 } |
| 395 |
| 396 TEST(RemoteInputMethodWinTest, DispatchKeyEvent_NativeKeyEvent) { |
| 397 // Basically RemoteInputMethodWin does not handle native keydown event. |
| 398 |
| 399 MockInputMethodDelegate delegate_; |
| 400 MockTextInputClient mock_text_input_client; |
| 401 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 402 |
| 403 const MSG wm_keydown = { NULL, WM_KEYDOWN, ui::VKEY_A }; |
| 404 ui::KeyEvent native_keydown(wm_keydown, false); |
| 405 |
| 406 // This must not cause a crash. |
| 407 EXPECT_FALSE(input_method->DispatchKeyEvent(native_keydown)); |
| 408 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 409 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 410 delegate_.Reset(); |
| 411 mock_text_input_client.Reset(); |
| 412 |
| 413 RemoteInputMethodPrivateWin* private_ptr = |
| 414 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 415 ASSERT_TRUE(private_ptr != NULL); |
| 416 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 417 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 418 |
| 419 // TextInputClient is not focused yet here. |
| 420 |
| 421 EXPECT_FALSE(input_method->DispatchKeyEvent(native_keydown)); |
| 422 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 423 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 424 delegate_.Reset(); |
| 425 mock_text_input_client.Reset(); |
| 426 |
| 427 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 428 |
| 429 // TextInputClient is now focused here. |
| 430 |
| 431 EXPECT_FALSE(input_method->DispatchKeyEvent(native_keydown)); |
| 432 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 433 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 434 delegate_.Reset(); |
| 435 mock_text_input_client.Reset(); |
| 436 } |
| 437 |
| 438 TEST(RemoteInputMethodWinTest, DispatchKeyEvent_NativeCharEvent) { |
| 439 // RemoteInputMethodWin handles native char event if possible. |
| 440 |
| 441 MockInputMethodDelegate delegate_; |
| 442 MockTextInputClient mock_text_input_client; |
| 443 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 444 |
| 445 const MSG wm_char = { NULL, WM_CHAR, 'A', 0 }; |
| 446 ui::KeyEvent native_char(wm_char, true); |
| 447 |
| 448 // This must not cause a crash. |
| 449 EXPECT_FALSE(input_method->DispatchKeyEvent(native_char)); |
| 450 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 451 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 452 delegate_.Reset(); |
| 453 mock_text_input_client.Reset(); |
| 454 |
| 455 RemoteInputMethodPrivateWin* private_ptr = |
| 456 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 457 ASSERT_TRUE(private_ptr != NULL); |
| 458 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 459 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 460 |
| 461 // TextInputClient is not focused yet here. |
| 462 |
| 463 EXPECT_FALSE(input_method->DispatchKeyEvent(native_char)); |
| 464 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 465 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 466 delegate_.Reset(); |
| 467 mock_text_input_client.Reset(); |
| 468 |
| 469 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 470 |
| 471 // TextInputClient is now focused here. |
| 472 |
| 473 EXPECT_TRUE(input_method->DispatchKeyEvent(native_char)); |
| 474 EXPECT_EQ(L"A", mock_text_input_client.inserted_text()); |
| 475 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 476 delegate_.Reset(); |
| 477 mock_text_input_client.Reset(); |
| 478 } |
| 479 |
| 480 TEST(RemoteInputMethodWinTest, DispatchKeyEvent_FabricatedKeyDown) { |
| 481 // Fabricated non-char event will be delegated to |
| 482 // InputMethodDelegate::DispatchFabricatedKeyEventPostIME as long as the |
| 483 // delegate is installed. |
| 484 |
| 485 MockInputMethodDelegate delegate_; |
| 486 MockTextInputClient mock_text_input_client; |
| 487 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 488 |
| 489 ui::KeyEvent fabricated_keydown(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, false); |
| 490 fabricated_keydown.set_character(L'A'); |
| 491 |
| 492 // This must not cause a crash. |
| 493 EXPECT_TRUE(input_method->DispatchKeyEvent(fabricated_keydown)); |
| 494 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 495 ASSERT_EQ(1, delegate_.fabricated_key_events().size()); |
| 496 EXPECT_EQ(L'A', delegate_.fabricated_key_events()[0]); |
| 497 delegate_.Reset(); |
| 498 mock_text_input_client.Reset(); |
| 499 |
| 500 RemoteInputMethodPrivateWin* private_ptr = |
| 501 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 502 ASSERT_TRUE(private_ptr != NULL); |
| 503 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 504 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 505 |
| 506 // TextInputClient is not focused yet here. |
| 507 |
| 508 EXPECT_TRUE(input_method->DispatchKeyEvent(fabricated_keydown)); |
| 509 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 510 ASSERT_EQ(1, delegate_.fabricated_key_events().size()); |
| 511 EXPECT_EQ(L'A', delegate_.fabricated_key_events()[0]); |
| 512 delegate_.Reset(); |
| 513 mock_text_input_client.Reset(); |
| 514 |
| 515 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 516 // TextInputClient is now focused here. |
| 517 |
| 518 EXPECT_TRUE(input_method->DispatchKeyEvent(fabricated_keydown)); |
| 519 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 520 ASSERT_EQ(1, delegate_.fabricated_key_events().size()); |
| 521 EXPECT_EQ(L'A', delegate_.fabricated_key_events()[0]); |
| 522 delegate_.Reset(); |
| 523 mock_text_input_client.Reset(); |
| 524 |
| 525 input_method->SetDelegate(NULL); |
| 526 // RemoteInputMethodDelegateWin is no longer set here. |
| 527 |
| 528 EXPECT_FALSE(input_method->DispatchKeyEvent(fabricated_keydown)); |
| 529 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 530 } |
| 531 |
| 532 TEST(RemoteInputMethodWinTest, DispatchKeyEvent_FabricatedChar) { |
| 533 // Note: RemoteInputMethodWin::DispatchKeyEvent should always return true |
| 534 // for fabricated character events. |
| 535 |
| 536 MockInputMethodDelegate delegate_; |
| 537 MockTextInputClient mock_text_input_client; |
| 538 scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_)); |
| 539 |
| 540 ui::KeyEvent fabricated_char(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, true); |
| 541 fabricated_char.set_character(L'A'); |
| 542 |
| 543 // This must not cause a crash. |
| 544 EXPECT_TRUE(input_method->DispatchKeyEvent(fabricated_char)); |
| 545 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 546 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 547 delegate_.Reset(); |
| 548 mock_text_input_client.Reset(); |
| 549 |
| 550 RemoteInputMethodPrivateWin* private_ptr = |
| 551 RemoteInputMethodPrivateWin::Get(input_method.get()); |
| 552 ASSERT_TRUE(private_ptr != NULL); |
| 553 MockRemoteInputMethodDelegateWin mock_remote_delegate; |
| 554 private_ptr->SetRemoteDelegate(&mock_remote_delegate); |
| 555 |
| 556 // TextInputClient is not focused yet here. |
| 557 |
| 558 EXPECT_TRUE(input_method->DispatchKeyEvent(fabricated_char)); |
| 559 EXPECT_TRUE(mock_text_input_client.inserted_text().empty()); |
| 560 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 561 delegate_.Reset(); |
| 562 mock_text_input_client.Reset(); |
| 563 |
| 564 input_method->SetFocusedTextInputClient(&mock_text_input_client); |
| 565 |
| 566 // TextInputClient is now focused here. |
| 567 |
| 568 EXPECT_TRUE(input_method->DispatchKeyEvent(fabricated_char)); |
| 569 EXPECT_EQ(L"A", mock_text_input_client.inserted_text()); |
| 570 EXPECT_TRUE(delegate_.fabricated_key_events().empty()); |
| 571 delegate_.Reset(); |
| 572 mock_text_input_client.Reset(); |
| 573 } |
| 574 |
| 575 } // namespace |
| 576 } // namespace ui |
OLD | NEW |