| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/views/controls/textfield/textfield.h" | 5 #include "ui/views/controls/textfield/textfield.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 textfield_->SelectAll(false); | 423 textfield_->SelectAll(false); |
| 424 SendKeyEvent(ui::VKEY_K); | 424 SendKeyEvent(ui::VKEY_K); |
| 425 EXPECT_STR_EQ("k", textfield_->text()); | 425 EXPECT_STR_EQ("k", textfield_->text()); |
| 426 | 426 |
| 427 // Delete the previous word from cursor. | 427 // Delete the previous word from cursor. |
| 428 textfield_->SetText(ASCIIToUTF16("one two three four")); | 428 textfield_->SetText(ASCIIToUTF16("one two three four")); |
| 429 SendKeyEvent(ui::VKEY_END); | 429 SendKeyEvent(ui::VKEY_END); |
| 430 SendKeyEvent(ui::VKEY_BACK, false, false, true, false); | 430 SendKeyEvent(ui::VKEY_BACK, false, false, true, false); |
| 431 EXPECT_STR_EQ("one two three ", textfield_->text()); | 431 EXPECT_STR_EQ("one two three ", textfield_->text()); |
| 432 | 432 |
| 433 // Delete to a line break on Linux and ChromeOS, no-op on Windows. | 433 // Delete to a line break on Linux and ChromeOS, to a word break on Windows. |
| 434 SendKeyEvent(ui::VKEY_LEFT, false, false, true, false); | 434 SendKeyEvent(ui::VKEY_LEFT, false, false, true, false); |
| 435 SendKeyEvent(ui::VKEY_BACK, false, true, true, false); | 435 SendKeyEvent(ui::VKEY_BACK, false, true, true, false); |
| 436 #if defined(OS_LINUX) | 436 #if defined(OS_LINUX) |
| 437 EXPECT_STR_EQ("three ", textfield_->text()); | 437 EXPECT_STR_EQ("three ", textfield_->text()); |
| 438 #else | 438 #else |
| 439 EXPECT_STR_EQ("one two three ", textfield_->text()); | 439 EXPECT_STR_EQ("one three ", textfield_->text()); |
| 440 #endif | 440 #endif |
| 441 | 441 |
| 442 // Delete the next word from cursor. | 442 // Delete the next word from cursor. |
| 443 textfield_->SetText(ASCIIToUTF16("one two three four")); | 443 textfield_->SetText(ASCIIToUTF16("one two three four")); |
| 444 SendKeyEvent(ui::VKEY_HOME); | 444 SendKeyEvent(ui::VKEY_HOME); |
| 445 SendKeyEvent(ui::VKEY_DELETE, false, false, true, false); | 445 SendKeyEvent(ui::VKEY_DELETE, false, false, true, false); |
| 446 EXPECT_STR_EQ(" two three four", textfield_->text()); | 446 EXPECT_STR_EQ(" two three four", textfield_->text()); |
| 447 | 447 |
| 448 // Delete to a line break on Linux and ChromeOS, no-op on Windows. | 448 // Delete to a line break on Linux and ChromeOS, to a word break on Windows. |
| 449 SendKeyEvent(ui::VKEY_RIGHT, false, false, true, false); | 449 SendKeyEvent(ui::VKEY_RIGHT, false, false, true, false); |
| 450 SendKeyEvent(ui::VKEY_DELETE, false, true, true, false); | 450 SendKeyEvent(ui::VKEY_DELETE, false, true, true, false); |
| 451 #if defined(OS_LINUX) | 451 #if defined(OS_LINUX) |
| 452 EXPECT_STR_EQ(" two", textfield_->text()); | 452 EXPECT_STR_EQ(" two", textfield_->text()); |
| 453 #else | 453 #else |
| 454 EXPECT_STR_EQ(" two three four", textfield_->text()); | 454 EXPECT_STR_EQ(" two four", textfield_->text()); |
| 455 #endif | 455 #endif |
| 456 } | 456 } |
| 457 | 457 |
| 458 TEST_F(TextfieldTest, PasswordTest) { | 458 TEST_F(TextfieldTest, PasswordTest) { |
| 459 InitTextfield(); | 459 InitTextfield(); |
| 460 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | 460 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 461 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); | 461 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); |
| 462 EXPECT_TRUE(textfield_->enabled()); | 462 EXPECT_TRUE(textfield_->enabled()); |
| 463 EXPECT_TRUE(textfield_->IsFocusable()); | 463 EXPECT_TRUE(textfield_->IsFocusable()); |
| 464 | 464 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 textfield_->SetEnabled(false); | 516 textfield_->SetEnabled(false); |
| 517 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, textfield_->GetTextInputType()); | 517 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, textfield_->GetTextInputType()); |
| 518 | 518 |
| 519 textfield_->SetEnabled(true); | 519 textfield_->SetEnabled(true); |
| 520 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); | 520 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); |
| 521 } | 521 } |
| 522 | 522 |
| 523 TEST_F(TextfieldTest, OnKeyPressReturnValueTest) { | 523 TEST_F(TextfieldTest, OnKeyPressReturnValueTest) { |
| 524 InitTextfield(); | 524 InitTextfield(); |
| 525 | 525 |
| 526 // Character keys will be handled by input method. | 526 // Character keys are handled by the input method. |
| 527 SendKeyEvent(ui::VKEY_A); | 527 SendKeyEvent(ui::VKEY_A); |
| 528 EXPECT_TRUE(textfield_->key_received()); | 528 EXPECT_TRUE(textfield_->key_received()); |
| 529 EXPECT_FALSE(textfield_->key_handled()); | 529 EXPECT_FALSE(textfield_->key_handled()); |
| 530 textfield_->clear(); | 530 textfield_->clear(); |
| 531 | 531 |
| 532 // Home will be handled. | 532 // Arrow keys and home/end are handled by the textfield. |
| 533 SendKeyEvent(ui::VKEY_LEFT); |
| 534 EXPECT_TRUE(textfield_->key_received()); |
| 535 EXPECT_TRUE(textfield_->key_handled()); |
| 536 textfield_->clear(); |
| 537 |
| 538 SendKeyEvent(ui::VKEY_RIGHT); |
| 539 EXPECT_TRUE(textfield_->key_received()); |
| 540 EXPECT_TRUE(textfield_->key_handled()); |
| 541 textfield_->clear(); |
| 542 |
| 533 SendKeyEvent(ui::VKEY_HOME); | 543 SendKeyEvent(ui::VKEY_HOME); |
| 534 EXPECT_TRUE(textfield_->key_received()); | 544 EXPECT_TRUE(textfield_->key_received()); |
| 535 EXPECT_TRUE(textfield_->key_handled()); | 545 EXPECT_TRUE(textfield_->key_handled()); |
| 536 textfield_->clear(); | 546 textfield_->clear(); |
| 537 | 547 |
| 548 SendKeyEvent(ui::VKEY_END); |
| 549 EXPECT_TRUE(textfield_->key_received()); |
| 550 EXPECT_TRUE(textfield_->key_handled()); |
| 551 textfield_->clear(); |
| 552 |
| 538 // F24, up/down key won't be handled. | 553 // F24, up/down key won't be handled. |
| 539 SendKeyEvent(ui::VKEY_F24); | 554 SendKeyEvent(ui::VKEY_F24); |
| 540 EXPECT_TRUE(textfield_->key_received()); | 555 EXPECT_TRUE(textfield_->key_received()); |
| 541 EXPECT_FALSE(textfield_->key_handled()); | 556 EXPECT_FALSE(textfield_->key_handled()); |
| 542 textfield_->clear(); | 557 textfield_->clear(); |
| 543 | 558 |
| 544 SendKeyEvent(ui::VKEY_UP); | 559 SendKeyEvent(ui::VKEY_UP); |
| 545 EXPECT_TRUE(textfield_->key_received()); | 560 EXPECT_TRUE(textfield_->key_received()); |
| 546 EXPECT_FALSE(textfield_->key_handled()); | 561 EXPECT_FALSE(textfield_->key_handled()); |
| 547 textfield_->clear(); | 562 textfield_->clear(); |
| 548 | 563 |
| 549 SendKeyEvent(ui::VKEY_DOWN); | 564 SendKeyEvent(ui::VKEY_DOWN); |
| 550 EXPECT_TRUE(textfield_->key_received()); | 565 EXPECT_TRUE(textfield_->key_received()); |
| 551 EXPECT_FALSE(textfield_->key_handled()); | 566 EXPECT_FALSE(textfield_->key_handled()); |
| 552 textfield_->clear(); | 567 textfield_->clear(); |
| 553 | |
| 554 // Empty Textfield does not handle left/right. | |
| 555 textfield_->SetText(base::string16()); | |
| 556 SendKeyEvent(ui::VKEY_LEFT); | |
| 557 EXPECT_TRUE(textfield_->key_received()); | |
| 558 EXPECT_FALSE(textfield_->key_handled()); | |
| 559 textfield_->clear(); | |
| 560 | |
| 561 SendKeyEvent(ui::VKEY_RIGHT); | |
| 562 EXPECT_TRUE(textfield_->key_received()); | |
| 563 EXPECT_FALSE(textfield_->key_handled()); | |
| 564 textfield_->clear(); | |
| 565 | |
| 566 // Add a char. Right key should not be handled when cursor is at the end. | |
| 567 SendKeyEvent(ui::VKEY_B); | |
| 568 SendKeyEvent(ui::VKEY_RIGHT); | |
| 569 EXPECT_TRUE(textfield_->key_received()); | |
| 570 EXPECT_FALSE(textfield_->key_handled()); | |
| 571 textfield_->clear(); | |
| 572 | |
| 573 // First left key is handled to move cursor left to the beginning. | |
| 574 SendKeyEvent(ui::VKEY_LEFT); | |
| 575 EXPECT_TRUE(textfield_->key_received()); | |
| 576 EXPECT_TRUE(textfield_->key_handled()); | |
| 577 textfield_->clear(); | |
| 578 | |
| 579 // Now left key should not be handled. | |
| 580 SendKeyEvent(ui::VKEY_LEFT); | |
| 581 EXPECT_TRUE(textfield_->key_received()); | |
| 582 EXPECT_FALSE(textfield_->key_handled()); | |
| 583 textfield_->clear(); | |
| 584 } | 568 } |
| 585 | 569 |
| 586 TEST_F(TextfieldTest, CursorMovement) { | 570 TEST_F(TextfieldTest, CursorMovement) { |
| 587 InitTextfield(); | 571 InitTextfield(); |
| 588 | 572 |
| 589 // Test with trailing whitespace. | 573 // Test with trailing whitespace. |
| 590 textfield_->SetText(ASCIIToUTF16("one two hre ")); | 574 textfield_->SetText(ASCIIToUTF16("one two hre ")); |
| 591 | 575 |
| 592 // Send the cursor at the end. | 576 // Send the cursor at the end. |
| 593 SendKeyEvent(ui::VKEY_END); | 577 SendKeyEvent(ui::VKEY_END); |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1099 textfield_->clear(); | 1083 textfield_->clear(); |
| 1100 | 1084 |
| 1101 on_before_user_action_ = on_after_user_action_ = 0; | 1085 on_before_user_action_ = on_after_user_action_ = 0; |
| 1102 SendKeyEvent(ui::VKEY_A); | 1086 SendKeyEvent(ui::VKEY_A); |
| 1103 EXPECT_TRUE(textfield_->key_received()); | 1087 EXPECT_TRUE(textfield_->key_received()); |
| 1104 EXPECT_FALSE(textfield_->key_handled()); | 1088 EXPECT_FALSE(textfield_->key_handled()); |
| 1105 EXPECT_TRUE(client->HasCompositionText()); | 1089 EXPECT_TRUE(client->HasCompositionText()); |
| 1106 EXPECT_TRUE(client->GetCompositionTextRange(&range)); | 1090 EXPECT_TRUE(client->GetCompositionTextRange(&range)); |
| 1107 EXPECT_STR_EQ("0321456789", textfield_->text()); | 1091 EXPECT_STR_EQ("0321456789", textfield_->text()); |
| 1108 EXPECT_EQ(gfx::Range(1, 4), range); | 1092 EXPECT_EQ(gfx::Range(1, 4), range); |
| 1109 EXPECT_EQ(2, on_before_user_action_); | 1093 EXPECT_EQ(1, on_before_user_action_); |
| 1110 EXPECT_EQ(2, on_after_user_action_); | 1094 EXPECT_EQ(1, on_after_user_action_); |
| 1111 | 1095 |
| 1112 input_method_->SetResultTextForNextKey(UTF8ToUTF16("123")); | 1096 input_method_->SetResultTextForNextKey(UTF8ToUTF16("123")); |
| 1113 on_before_user_action_ = on_after_user_action_ = 0; | 1097 on_before_user_action_ = on_after_user_action_ = 0; |
| 1114 textfield_->clear(); | 1098 textfield_->clear(); |
| 1115 SendKeyEvent(ui::VKEY_A); | 1099 SendKeyEvent(ui::VKEY_A); |
| 1116 EXPECT_TRUE(textfield_->key_received()); | 1100 EXPECT_TRUE(textfield_->key_received()); |
| 1117 EXPECT_FALSE(textfield_->key_handled()); | 1101 EXPECT_FALSE(textfield_->key_handled()); |
| 1118 EXPECT_FALSE(client->HasCompositionText()); | 1102 EXPECT_FALSE(client->HasCompositionText()); |
| 1119 EXPECT_FALSE(input_method_->cancel_composition_called()); | 1103 EXPECT_FALSE(input_method_->cancel_composition_called()); |
| 1120 EXPECT_STR_EQ("0123456789", textfield_->text()); | 1104 EXPECT_STR_EQ("0123456789", textfield_->text()); |
| 1121 EXPECT_EQ(2, on_before_user_action_); | 1105 EXPECT_EQ(1, on_before_user_action_); |
| 1122 EXPECT_EQ(2, on_after_user_action_); | 1106 EXPECT_EQ(1, on_after_user_action_); |
| 1123 | 1107 |
| 1124 input_method_->Clear(); | 1108 input_method_->Clear(); |
| 1125 input_method_->SetCompositionTextForNextKey(composition); | 1109 input_method_->SetCompositionTextForNextKey(composition); |
| 1126 textfield_->clear(); | 1110 textfield_->clear(); |
| 1127 SendKeyEvent(ui::VKEY_A); | 1111 SendKeyEvent(ui::VKEY_A); |
| 1128 EXPECT_TRUE(client->HasCompositionText()); | 1112 EXPECT_TRUE(client->HasCompositionText()); |
| 1129 EXPECT_STR_EQ("0123321456789", textfield_->text()); | 1113 EXPECT_STR_EQ("0123321456789", textfield_->text()); |
| 1130 | 1114 |
| 1131 on_before_user_action_ = on_after_user_action_ = 0; | 1115 on_before_user_action_ = on_after_user_action_ = 0; |
| 1132 textfield_->clear(); | 1116 textfield_->clear(); |
| (...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1960 // Set text which may fall back to a font which has taller baseline than | 1944 // Set text which may fall back to a font which has taller baseline than |
| 1961 // the default font. | 1945 // the default font. |
| 1962 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); | 1946 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); |
| 1963 const int new_baseline = textfield_->GetBaseline(); | 1947 const int new_baseline = textfield_->GetBaseline(); |
| 1964 | 1948 |
| 1965 // Regardless of the text, the baseline must be the same. | 1949 // Regardless of the text, the baseline must be the same. |
| 1966 EXPECT_EQ(new_baseline, old_baseline); | 1950 EXPECT_EQ(new_baseline, old_baseline); |
| 1967 } | 1951 } |
| 1968 | 1952 |
| 1969 } // namespace views | 1953 } // namespace views |
| OLD | NEW |