Chromium Code Reviews| 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 24 matching lines...) Expand all Loading... | |
| 35 #include "ui/views/focus/focus_manager.h" | 35 #include "ui/views/focus/focus_manager.h" |
| 36 #include "ui/views/ime/mock_input_method.h" | 36 #include "ui/views/ime/mock_input_method.h" |
| 37 #include "ui/views/test/test_views_delegate.h" | 37 #include "ui/views/test/test_views_delegate.h" |
| 38 #include "ui/views/test/views_test_base.h" | 38 #include "ui/views/test/views_test_base.h" |
| 39 #include "ui/views/widget/native_widget_private.h" | 39 #include "ui/views/widget/native_widget_private.h" |
| 40 #include "ui/views/widget/widget.h" | 40 #include "ui/views/widget/widget.h" |
| 41 #include "url/gurl.h" | 41 #include "url/gurl.h" |
| 42 | 42 |
| 43 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
| 44 #include "base/win/windows_version.h" | 44 #include "base/win/windows_version.h" |
| 45 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) | |
|
oshima
2014/06/04 00:56:15
I somehow thought we recommend if/endif for each c
msw
2014/06/04 01:02:54
Changed to match that pattern.
| |
| 46 #include "ui/events/linux/text_edit_key_bindings_delegate_auralinux.h" | |
| 45 #endif | 47 #endif |
| 46 | 48 |
| 47 using base::ASCIIToUTF16; | 49 using base::ASCIIToUTF16; |
| 48 using base::UTF8ToUTF16; | 50 using base::UTF8ToUTF16; |
| 49 using base::WideToUTF16; | 51 using base::WideToUTF16; |
| 50 | 52 |
| 51 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) | 53 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) |
| 52 | 54 |
| 53 namespace { | 55 namespace { |
| 54 | 56 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 | 158 |
| 157 // TextfieldController: | 159 // TextfieldController: |
| 158 virtual void ContentsChanged(Textfield* sender, | 160 virtual void ContentsChanged(Textfield* sender, |
| 159 const base::string16& new_contents) OVERRIDE { | 161 const base::string16& new_contents) OVERRIDE { |
| 160 // Paste calls TextfieldController::ContentsChanged() explicitly even if the | 162 // Paste calls TextfieldController::ContentsChanged() explicitly even if the |
| 161 // paste action did not change the content. So |new_contents| may match | 163 // paste action did not change the content. So |new_contents| may match |
| 162 // |last_contents_|. For more info, see http://crbug.com/79002 | 164 // |last_contents_|. For more info, see http://crbug.com/79002 |
| 163 last_contents_ = new_contents; | 165 last_contents_ = new_contents; |
| 164 } | 166 } |
| 165 | 167 |
| 166 virtual bool HandleKeyEvent(Textfield* sender, | |
| 167 const ui::KeyEvent& key_event) OVERRIDE { | |
| 168 // TODO(oshima): figure out how to test the keystroke. | |
| 169 return false; | |
| 170 } | |
| 171 | |
| 172 virtual void OnBeforeUserAction(Textfield* sender) OVERRIDE { | 168 virtual void OnBeforeUserAction(Textfield* sender) OVERRIDE { |
| 173 ++on_before_user_action_; | 169 ++on_before_user_action_; |
| 174 } | 170 } |
| 175 | 171 |
| 176 virtual void OnAfterUserAction(Textfield* sender) OVERRIDE { | 172 virtual void OnAfterUserAction(Textfield* sender) OVERRIDE { |
| 177 ++on_after_user_action_; | 173 ++on_after_user_action_; |
| 178 } | 174 } |
| 179 | 175 |
| 180 virtual void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) OVERRIDE { | 176 virtual void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) OVERRIDE { |
| 181 copied_to_clipboard_ = clipboard_type; | 177 copied_to_clipboard_ = clipboard_type; |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); | 521 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); |
| 526 | 522 |
| 527 // As do disabled textfields | 523 // As do disabled textfields |
| 528 textfield_->SetEnabled(false); | 524 textfield_->SetEnabled(false); |
| 529 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, textfield_->GetTextInputType()); | 525 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE, textfield_->GetTextInputType()); |
| 530 | 526 |
| 531 textfield_->SetEnabled(true); | 527 textfield_->SetEnabled(true); |
| 532 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); | 528 EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, textfield_->GetTextInputType()); |
| 533 } | 529 } |
| 534 | 530 |
| 535 TEST_F(TextfieldTest, OnKeyPressReturnValueTest) { | 531 TEST_F(TextfieldTest, OnKeyPress) { |
| 536 InitTextfield(); | 532 InitTextfield(); |
| 537 | 533 |
| 538 // Character keys are handled by the input method. | 534 // Character keys are handled by the input method. |
| 539 SendKeyEvent(ui::VKEY_A); | 535 SendKeyEvent(ui::VKEY_A); |
| 540 EXPECT_TRUE(textfield_->key_received()); | 536 EXPECT_TRUE(textfield_->key_received()); |
| 541 EXPECT_FALSE(textfield_->key_handled()); | 537 EXPECT_FALSE(textfield_->key_handled()); |
| 542 textfield_->clear(); | 538 textfield_->clear(); |
| 543 | 539 |
| 544 // Arrow keys and home/end are handled by the textfield. | 540 // Arrow keys and home/end are handled by the textfield. |
| 545 SendKeyEvent(ui::VKEY_LEFT); | 541 SendKeyEvent(ui::VKEY_LEFT); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 572 EXPECT_TRUE(textfield_->key_received()); | 568 EXPECT_TRUE(textfield_->key_received()); |
| 573 EXPECT_FALSE(textfield_->key_handled()); | 569 EXPECT_FALSE(textfield_->key_handled()); |
| 574 textfield_->clear(); | 570 textfield_->clear(); |
| 575 | 571 |
| 576 SendKeyEvent(ui::VKEY_DOWN); | 572 SendKeyEvent(ui::VKEY_DOWN); |
| 577 EXPECT_TRUE(textfield_->key_received()); | 573 EXPECT_TRUE(textfield_->key_received()); |
| 578 EXPECT_FALSE(textfield_->key_handled()); | 574 EXPECT_FALSE(textfield_->key_handled()); |
| 579 textfield_->clear(); | 575 textfield_->clear(); |
| 580 } | 576 } |
| 581 | 577 |
| 578 // Tests that default key bindings are handled even with a delegate installed. | |
| 579 TEST_F(TextfieldTest, OnKeyPressBinding) { | |
| 580 InitTextfield(); | |
| 581 | |
| 582 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | |
| 583 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. | |
| 584 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { | |
| 585 public: | |
| 586 TestDelegate() {} | |
| 587 virtual ~TestDelegate() {} | |
| 588 | |
| 589 virtual bool MatchEvent( | |
| 590 const ui::Event& event, | |
| 591 std::vector<ui::TextEditCommandAuraLinux>* commands) OVERRIDE { | |
| 592 return false; | |
| 593 } | |
| 594 | |
| 595 private: | |
| 596 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
| 597 }; | |
| 598 | |
| 599 TestDelegate delegate; | |
| 600 ui::SetTextEditKeyBindingsDelegate(&delegate); | |
| 601 #endif | |
| 602 | |
| 603 SendKeyEvent(ui::VKEY_A, false, false); | |
| 604 EXPECT_STR_EQ("a", textfield_->text()); | |
| 605 textfield_->clear(); | |
| 606 | |
| 607 // Undo/Redo command keys are handled by the textfield. | |
| 608 SendKeyEvent(ui::VKEY_Z, false, true); | |
| 609 EXPECT_TRUE(textfield_->key_received()); | |
| 610 EXPECT_TRUE(textfield_->key_handled()); | |
| 611 EXPECT_TRUE(textfield_->text().empty()); | |
| 612 textfield_->clear(); | |
| 613 | |
| 614 SendKeyEvent(ui::VKEY_Z, true, true); | |
| 615 EXPECT_TRUE(textfield_->key_received()); | |
| 616 EXPECT_TRUE(textfield_->key_handled()); | |
| 617 EXPECT_STR_EQ("a", textfield_->text()); | |
| 618 textfield_->clear(); | |
| 619 | |
| 620 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | |
| 621 ui::SetTextEditKeyBindingsDelegate(NULL); | |
|
oshima
2014/06/04 00:56:15
optional: consider using scoped object to set/rese
msw
2014/06/04 01:02:54
Not worth it for one test, IMO.
| |
| 622 #endif | |
| 623 } | |
| 624 | |
| 582 TEST_F(TextfieldTest, CursorMovement) { | 625 TEST_F(TextfieldTest, CursorMovement) { |
| 583 InitTextfield(); | 626 InitTextfield(); |
| 584 | 627 |
| 585 // Test with trailing whitespace. | 628 // Test with trailing whitespace. |
| 586 textfield_->SetText(ASCIIToUTF16("one two hre ")); | 629 textfield_->SetText(ASCIIToUTF16("one two hre ")); |
| 587 | 630 |
| 588 // Send the cursor at the end. | 631 // Send the cursor at the end. |
| 589 SendKeyEvent(ui::VKEY_END); | 632 SendKeyEvent(ui::VKEY_END); |
| 590 | 633 |
| 591 // Ctrl+Left should move the cursor just before the last word. | 634 // Ctrl+Left should move the cursor just before the last word. |
| (...skipping 1422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2014 // Set text which may fall back to a font which has taller baseline than | 2057 // Set text which may fall back to a font which has taller baseline than |
| 2015 // the default font. | 2058 // the default font. |
| 2016 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); | 2059 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); |
| 2017 const int new_baseline = textfield_->GetBaseline(); | 2060 const int new_baseline = textfield_->GetBaseline(); |
| 2018 | 2061 |
| 2019 // Regardless of the text, the baseline must be the same. | 2062 // Regardless of the text, the baseline must be the same. |
| 2020 EXPECT_EQ(new_baseline, old_baseline); | 2063 EXPECT_EQ(new_baseline, old_baseline); |
| 2021 } | 2064 } |
| 2022 | 2065 |
| 2023 } // namespace views | 2066 } // namespace views |
| OLD | NEW |