| 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 19 matching lines...) Expand all Loading... |
| 30 #include "ui/views/ime/mock_input_method.h" | 30 #include "ui/views/ime/mock_input_method.h" |
| 31 #include "ui/views/test/test_views_delegate.h" | 31 #include "ui/views/test/test_views_delegate.h" |
| 32 #include "ui/views/test/views_test_base.h" | 32 #include "ui/views/test/views_test_base.h" |
| 33 #include "ui/views/widget/widget.h" | 33 #include "ui/views/widget/widget.h" |
| 34 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 35 | 35 |
| 36 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
| 37 #include "base/win/windows_version.h" | 37 #include "base/win/windows_version.h" |
| 38 #endif | 38 #endif |
| 39 | 39 |
| 40 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 41 #include "ui/events/linux/text_edit_key_bindings_delegate_auralinux.h" |
| 42 #endif |
| 43 |
| 40 using base::ASCIIToUTF16; | 44 using base::ASCIIToUTF16; |
| 41 using base::UTF8ToUTF16; | 45 using base::UTF8ToUTF16; |
| 42 using base::WideToUTF16; | 46 using base::WideToUTF16; |
| 43 | 47 |
| 44 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) | 48 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) |
| 45 | 49 |
| 46 namespace { | 50 namespace { |
| 47 | 51 |
| 48 const base::char16 kHebrewLetterSamekh = 0x05E1; | 52 const base::char16 kHebrewLetterSamekh = 0x05E1; |
| 49 | 53 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 139 |
| 136 // TextfieldController: | 140 // TextfieldController: |
| 137 virtual void ContentsChanged(Textfield* sender, | 141 virtual void ContentsChanged(Textfield* sender, |
| 138 const base::string16& new_contents) OVERRIDE { | 142 const base::string16& new_contents) OVERRIDE { |
| 139 // Paste calls TextfieldController::ContentsChanged() explicitly even if the | 143 // Paste calls TextfieldController::ContentsChanged() explicitly even if the |
| 140 // paste action did not change the content. So |new_contents| may match | 144 // paste action did not change the content. So |new_contents| may match |
| 141 // |last_contents_|. For more info, see http://crbug.com/79002 | 145 // |last_contents_|. For more info, see http://crbug.com/79002 |
| 142 last_contents_ = new_contents; | 146 last_contents_ = new_contents; |
| 143 } | 147 } |
| 144 | 148 |
| 145 virtual bool HandleKeyEvent(Textfield* sender, | |
| 146 const ui::KeyEvent& key_event) OVERRIDE { | |
| 147 // TODO(oshima): figure out how to test the keystroke. | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 virtual void OnBeforeUserAction(Textfield* sender) OVERRIDE { | 149 virtual void OnBeforeUserAction(Textfield* sender) OVERRIDE { |
| 152 ++on_before_user_action_; | 150 ++on_before_user_action_; |
| 153 } | 151 } |
| 154 | 152 |
| 155 virtual void OnAfterUserAction(Textfield* sender) OVERRIDE { | 153 virtual void OnAfterUserAction(Textfield* sender) OVERRIDE { |
| 156 ++on_after_user_action_; | 154 ++on_after_user_action_; |
| 157 } | 155 } |
| 158 | 156 |
| 159 virtual void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) OVERRIDE { | 157 virtual void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) OVERRIDE { |
| 160 copied_to_clipboard_ = clipboard_type; | 158 copied_to_clipboard_ = clipboard_type; |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); | 502 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); |
| 505 | 503 |
| 506 // As do disabled textfields | 504 // As do disabled textfields |
| 507 textfield_->SetEnabled(false); | 505 textfield_->SetEnabled(false); |
| 508 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, textfield_->GetTextInputType()); | 506 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, textfield_->GetTextInputType()); |
| 509 | 507 |
| 510 textfield_->SetEnabled(true); | 508 textfield_->SetEnabled(true); |
| 511 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); | 509 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); |
| 512 } | 510 } |
| 513 | 511 |
| 514 TEST_F(TextfieldTest, OnKeyPressReturnValueTest) { | 512 TEST_F(TextfieldTest, OnKeyPress) { |
| 515 InitTextfield(); | 513 InitTextfield(); |
| 516 | 514 |
| 517 // Character keys are handled by the input method. | 515 // Character keys are handled by the input method. |
| 518 SendKeyEvent(ui::VKEY_A); | 516 SendKeyEvent(ui::VKEY_A); |
| 519 EXPECT_TRUE(textfield_->key_received()); | 517 EXPECT_TRUE(textfield_->key_received()); |
| 520 EXPECT_FALSE(textfield_->key_handled()); | 518 EXPECT_FALSE(textfield_->key_handled()); |
| 521 textfield_->clear(); | 519 textfield_->clear(); |
| 522 | 520 |
| 523 // Arrow keys and home/end are handled by the textfield. | 521 // Arrow keys and home/end are handled by the textfield. |
| 524 SendKeyEvent(ui::VKEY_LEFT); | 522 SendKeyEvent(ui::VKEY_LEFT); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 551 EXPECT_TRUE(textfield_->key_received()); | 549 EXPECT_TRUE(textfield_->key_received()); |
| 552 EXPECT_FALSE(textfield_->key_handled()); | 550 EXPECT_FALSE(textfield_->key_handled()); |
| 553 textfield_->clear(); | 551 textfield_->clear(); |
| 554 | 552 |
| 555 SendKeyEvent(ui::VKEY_DOWN); | 553 SendKeyEvent(ui::VKEY_DOWN); |
| 556 EXPECT_TRUE(textfield_->key_received()); | 554 EXPECT_TRUE(textfield_->key_received()); |
| 557 EXPECT_FALSE(textfield_->key_handled()); | 555 EXPECT_FALSE(textfield_->key_handled()); |
| 558 textfield_->clear(); | 556 textfield_->clear(); |
| 559 } | 557 } |
| 560 | 558 |
| 559 // Tests that default key bindings are handled even with a delegate installed. |
| 560 TEST_F(TextfieldTest, OnKeyPressBinding) { |
| 561 InitTextfield(); |
| 562 |
| 563 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 564 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. |
| 565 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { |
| 566 public: |
| 567 TestDelegate() {} |
| 568 virtual ~TestDelegate() {} |
| 569 |
| 570 virtual bool MatchEvent( |
| 571 const ui::Event& event, |
| 572 std::vector<ui::TextEditCommandAuraLinux>* commands) OVERRIDE { |
| 573 return false; |
| 574 } |
| 575 |
| 576 private: |
| 577 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 578 }; |
| 579 |
| 580 TestDelegate delegate; |
| 581 ui::SetTextEditKeyBindingsDelegate(&delegate); |
| 582 #endif |
| 583 |
| 584 SendKeyEvent(ui::VKEY_A, false, false); |
| 585 EXPECT_STR_EQ("a", textfield_->text()); |
| 586 textfield_->clear(); |
| 587 |
| 588 // Undo/Redo command keys are handled by the textfield. |
| 589 SendKeyEvent(ui::VKEY_Z, false, true); |
| 590 EXPECT_TRUE(textfield_->key_received()); |
| 591 EXPECT_TRUE(textfield_->key_handled()); |
| 592 EXPECT_TRUE(textfield_->text().empty()); |
| 593 textfield_->clear(); |
| 594 |
| 595 SendKeyEvent(ui::VKEY_Z, true, true); |
| 596 EXPECT_TRUE(textfield_->key_received()); |
| 597 EXPECT_TRUE(textfield_->key_handled()); |
| 598 EXPECT_STR_EQ("a", textfield_->text()); |
| 599 textfield_->clear(); |
| 600 |
| 601 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 602 ui::SetTextEditKeyBindingsDelegate(NULL); |
| 603 #endif |
| 604 } |
| 605 |
| 561 TEST_F(TextfieldTest, CursorMovement) { | 606 TEST_F(TextfieldTest, CursorMovement) { |
| 562 InitTextfield(); | 607 InitTextfield(); |
| 563 | 608 |
| 564 // Test with trailing whitespace. | 609 // Test with trailing whitespace. |
| 565 textfield_->SetText(ASCIIToUTF16("one two hre ")); | 610 textfield_->SetText(ASCIIToUTF16("one two hre ")); |
| 566 | 611 |
| 567 // Send the cursor at the end. | 612 // Send the cursor at the end. |
| 568 SendKeyEvent(ui::VKEY_END); | 613 SendKeyEvent(ui::VKEY_END); |
| 569 | 614 |
| 570 // Ctrl+Left should move the cursor just before the last word. | 615 // Ctrl+Left should move the cursor just before the last word. |
| (...skipping 1387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1958 // Set text which may fall back to a font which has taller baseline than | 2003 // Set text which may fall back to a font which has taller baseline than |
| 1959 // the default font. | 2004 // the default font. |
| 1960 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); | 2005 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); |
| 1961 const int new_baseline = textfield_->GetBaseline(); | 2006 const int new_baseline = textfield_->GetBaseline(); |
| 1962 | 2007 |
| 1963 // Regardless of the text, the baseline must be the same. | 2008 // Regardless of the text, the baseline must be the same. |
| 1964 EXPECT_EQ(new_baseline, old_baseline); | 2009 EXPECT_EQ(new_baseline, old_baseline); |
| 1965 } | 2010 } |
| 1966 | 2011 |
| 1967 } // namespace views | 2012 } // namespace views |
| OLD | NEW |