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 10 matching lines...) Expand all Loading... |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "ui/base/clipboard/clipboard.h" | 22 #include "ui/base/clipboard/clipboard.h" |
23 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 23 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
24 #include "ui/base/dragdrop/drag_drop_types.h" | 24 #include "ui/base/dragdrop/drag_drop_types.h" |
25 #include "ui/base/ime/text_input_client.h" | 25 #include "ui/base/ime/text_input_client.h" |
26 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
27 #include "ui/base/ui_base_switches.h" | 27 #include "ui/base/ui_base_switches.h" |
28 #include "ui/base/ui_base_switches_util.h" | 28 #include "ui/base/ui_base_switches_util.h" |
29 #include "ui/events/event.h" | 29 #include "ui/events/event.h" |
30 #include "ui/events/keycodes/keyboard_codes.h" | 30 #include "ui/events/keycodes/keyboard_codes.h" |
| 31 #include "ui/events/platform/platform_event_source.h" |
31 #include "ui/gfx/render_text.h" | 32 #include "ui/gfx/render_text.h" |
32 #include "ui/views/controls/textfield/textfield_controller.h" | 33 #include "ui/views/controls/textfield/textfield_controller.h" |
33 #include "ui/views/controls/textfield/textfield_model.h" | 34 #include "ui/views/controls/textfield/textfield_model.h" |
34 #include "ui/views/focus/focus_manager.h" | 35 #include "ui/views/focus/focus_manager.h" |
35 #include "ui/views/ime/mock_input_method.h" | 36 #include "ui/views/ime/mock_input_method.h" |
36 #include "ui/views/test/test_views_delegate.h" | 37 #include "ui/views/test/test_views_delegate.h" |
37 #include "ui/views/test/views_test_base.h" | 38 #include "ui/views/test/views_test_base.h" |
38 #include "ui/views/widget/native_widget_private.h" | 39 #include "ui/views/widget/native_widget_private.h" |
39 #include "ui/views/widget/widget.h" | 40 #include "ui/views/widget/widget.h" |
40 #include "url/gurl.h" | 41 #include "url/gurl.h" |
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 // Test if clicking on textfield view sets the focus. | 648 // Test if clicking on textfield view sets the focus. |
648 widget_->GetFocusManager()->AdvanceFocus(true); | 649 widget_->GetFocusManager()->AdvanceFocus(true); |
649 EXPECT_EQ(3, GetFocusedView()->id()); | 650 EXPECT_EQ(3, GetFocusedView()->id()); |
650 ui::MouseEvent click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | 651 ui::MouseEvent click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
651 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); | 652 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
652 textfield_->OnMousePressed(click); | 653 textfield_->OnMousePressed(click); |
653 EXPECT_EQ(1, GetFocusedView()->id()); | 654 EXPECT_EQ(1, GetFocusedView()->id()); |
654 } | 655 } |
655 | 656 |
656 TEST_F(TextfieldTest, ContextMenuDisplayTest) { | 657 TEST_F(TextfieldTest, ContextMenuDisplayTest) { |
| 658 scoped_ptr<ui::PlatformEventSource> event_source = |
| 659 ui::PlatformEventSource::CreateDefault(); |
657 InitTextfield(); | 660 InitTextfield(); |
658 EXPECT_TRUE(textfield_->context_menu_controller()); | 661 EXPECT_TRUE(textfield_->context_menu_controller()); |
659 textfield_->SetText(ASCIIToUTF16("hello world")); | 662 textfield_->SetText(ASCIIToUTF16("hello world")); |
660 ui::Clipboard::GetForCurrentThread()->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE); | 663 ui::Clipboard::GetForCurrentThread()->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE); |
661 textfield_->ClearEditHistory(); | 664 textfield_->ClearEditHistory(); |
662 EXPECT_TRUE(GetContextMenuModel()); | 665 EXPECT_TRUE(GetContextMenuModel()); |
663 VerifyTextfieldContextMenuContents(false, false, GetContextMenuModel()); | 666 VerifyTextfieldContextMenuContents(false, false, GetContextMenuModel()); |
664 | 667 |
665 textfield_->SelectAll(false); | 668 textfield_->SelectAll(false); |
666 VerifyTextfieldContextMenuContents(true, false, GetContextMenuModel()); | 669 VerifyTextfieldContextMenuContents(true, false, GetContextMenuModel()); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 // Check that releasing in the same location does not alter the selection. | 740 // Check that releasing in the same location does not alter the selection. |
738 textfield_->OnMouseReleased(release); | 741 textfield_->OnMouseReleased(release); |
739 EXPECT_EQ(text_right, textfield_->GetSelectedText()); | 742 EXPECT_EQ(text_right, textfield_->GetSelectedText()); |
740 // Check that dragging from beyond the text length works too. | 743 // Check that dragging from beyond the text length works too. |
741 textfield_->OnMousePressed(click_b); | 744 textfield_->OnMousePressed(click_b); |
742 textfield_->OnMouseDragged(drag_left); | 745 textfield_->OnMouseDragged(drag_left); |
743 textfield_->OnMouseReleased(release); | 746 textfield_->OnMouseReleased(release); |
744 EXPECT_EQ(textfield_->text(), textfield_->GetSelectedText()); | 747 EXPECT_EQ(textfield_->text(), textfield_->GetSelectedText()); |
745 } | 748 } |
746 | 749 |
| 750 class TextfieldDragAndDropTest : public TextfieldTest { |
| 751 public: |
| 752 TextfieldDragAndDropTest() {} |
| 753 virtual ~TextfieldDragAndDropTest() {} |
| 754 |
| 755 // TextfieldTest: |
| 756 virtual void SetUp() OVERRIDE { |
| 757 event_source_ = ui::PlatformEventSource::CreateDefault(); |
| 758 TextfieldTest::SetUp(); |
| 759 } |
| 760 |
| 761 private: |
| 762 scoped_ptr<ui::PlatformEventSource> event_source_; |
| 763 |
| 764 DISALLOW_COPY_AND_ASSIGN(TextfieldDragAndDropTest); |
| 765 }; |
| 766 |
747 #if defined(OS_WIN) | 767 #if defined(OS_WIN) |
748 TEST_F(TextfieldTest, DragAndDrop_AcceptDrop) { | 768 TEST_F(TextfieldDragAndDropTest, DragAndDrop_AcceptDrop) { |
749 InitTextfield(); | 769 InitTextfield(); |
750 textfield_->SetText(ASCIIToUTF16("hello world")); | 770 textfield_->SetText(ASCIIToUTF16("hello world")); |
751 | 771 |
752 ui::OSExchangeData data; | 772 ui::OSExchangeData data; |
753 base::string16 string(ASCIIToUTF16("string ")); | 773 base::string16 string(ASCIIToUTF16("string ")); |
754 data.SetString(string); | 774 data.SetString(string); |
755 int formats = 0; | 775 int formats = 0; |
756 std::set<OSExchangeData::CustomFormat> custom_formats; | 776 std::set<OSExchangeData::CustomFormat> custom_formats; |
757 | 777 |
758 // Ensure that disabled textfields do not accept drops. | 778 // Ensure that disabled textfields do not accept drops. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 ui::OSExchangeData::CustomFormat fmt = ui::Clipboard::GetBitmapFormatType(); | 810 ui::OSExchangeData::CustomFormat fmt = ui::Clipboard::GetBitmapFormatType(); |
791 bad_data.SetPickledData(fmt, Pickle()); | 811 bad_data.SetPickledData(fmt, Pickle()); |
792 bad_data.SetFileContents(base::FilePath(L"x"), "x"); | 812 bad_data.SetFileContents(base::FilePath(L"x"), "x"); |
793 bad_data.SetHtml(base::string16(ASCIIToUTF16("x")), GURL("x.org")); | 813 bad_data.SetHtml(base::string16(ASCIIToUTF16("x")), GURL("x.org")); |
794 ui::OSExchangeData::DownloadFileInfo download(base::FilePath(), NULL); | 814 ui::OSExchangeData::DownloadFileInfo download(base::FilePath(), NULL); |
795 bad_data.SetDownloadFileInfo(download); | 815 bad_data.SetDownloadFileInfo(download); |
796 EXPECT_FALSE(textfield_->CanDrop(bad_data)); | 816 EXPECT_FALSE(textfield_->CanDrop(bad_data)); |
797 } | 817 } |
798 #endif | 818 #endif |
799 | 819 |
800 TEST_F(TextfieldTest, DragAndDrop_InitiateDrag) { | 820 TEST_F(TextfieldDragAndDropTest, DragAndDrop_InitiateDrag) { |
801 InitTextfield(); | 821 InitTextfield(); |
802 textfield_->SetText(ASCIIToUTF16("hello string world")); | 822 textfield_->SetText(ASCIIToUTF16("hello string world")); |
803 | 823 |
804 // Ensure the textfield will provide selected text for drag data. | 824 // Ensure the textfield will provide selected text for drag data. |
805 base::string16 string; | 825 base::string16 string; |
806 ui::OSExchangeData data; | 826 ui::OSExchangeData data; |
807 const gfx::Range kStringRange(6, 12); | 827 const gfx::Range kStringRange(6, 12); |
808 textfield_->SelectRange(kStringRange); | 828 textfield_->SelectRange(kStringRange); |
809 const gfx::Point kStringPoint(GetCursorPositionX(9), 0); | 829 const gfx::Point kStringPoint(GetCursorPositionX(9), 0); |
810 textfield_->WriteDragDataForView(NULL, kStringPoint, &data); | 830 textfield_->WriteDragDataForView(NULL, kStringPoint, &data); |
(...skipping 26 matching lines...) Expand all Loading... |
837 gfx::Point())); | 857 gfx::Point())); |
838 EXPECT_EQ(ui::DragDropTypes::DRAG_COPY, | 858 EXPECT_EQ(ui::DragDropTypes::DRAG_COPY, |
839 textfield_->GetDragOperationsForView(NULL, kStringPoint)); | 859 textfield_->GetDragOperationsForView(NULL, kStringPoint)); |
840 EXPECT_TRUE(textfield_->CanStartDragForView(NULL, kStringPoint, | 860 EXPECT_TRUE(textfield_->CanStartDragForView(NULL, kStringPoint, |
841 gfx::Point())); | 861 gfx::Point())); |
842 // Ensure that textfields support local moves. | 862 // Ensure that textfields support local moves. |
843 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY, | 863 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY, |
844 textfield_->GetDragOperationsForView(textfield_, kStringPoint)); | 864 textfield_->GetDragOperationsForView(textfield_, kStringPoint)); |
845 } | 865 } |
846 | 866 |
847 TEST_F(TextfieldTest, DragAndDrop_ToTheRight) { | 867 TEST_F(TextfieldDragAndDropTest, DragAndDrop_ToTheRight) { |
848 InitTextfield(); | 868 InitTextfield(); |
849 textfield_->SetText(ASCIIToUTF16("hello world")); | 869 textfield_->SetText(ASCIIToUTF16("hello world")); |
850 | 870 |
851 base::string16 string; | 871 base::string16 string; |
852 ui::OSExchangeData data; | 872 ui::OSExchangeData data; |
853 int formats = 0; | 873 int formats = 0; |
854 int operations = 0; | 874 int operations = 0; |
855 std::set<OSExchangeData::CustomFormat> custom_formats; | 875 std::set<OSExchangeData::CustomFormat> custom_formats; |
856 | 876 |
857 // Start dragging "ello". | 877 // Start dragging "ello". |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
890 SendKeyEvent(ui::VKEY_Z, false, true); | 910 SendKeyEvent(ui::VKEY_Z, false, true); |
891 EXPECT_STR_EQ("", textfield_->text()); | 911 EXPECT_STR_EQ("", textfield_->text()); |
892 SendKeyEvent(ui::VKEY_Y, false, true); | 912 SendKeyEvent(ui::VKEY_Y, false, true); |
893 EXPECT_STR_EQ("hello world", textfield_->text()); | 913 EXPECT_STR_EQ("hello world", textfield_->text()); |
894 SendKeyEvent(ui::VKEY_Y, false, true); | 914 SendKeyEvent(ui::VKEY_Y, false, true); |
895 EXPECT_STR_EQ("h welloorld", textfield_->text()); | 915 EXPECT_STR_EQ("h welloorld", textfield_->text()); |
896 SendKeyEvent(ui::VKEY_Y, false, true); | 916 SendKeyEvent(ui::VKEY_Y, false, true); |
897 EXPECT_STR_EQ("h welloorld", textfield_->text()); | 917 EXPECT_STR_EQ("h welloorld", textfield_->text()); |
898 } | 918 } |
899 | 919 |
900 TEST_F(TextfieldTest, DragAndDrop_ToTheLeft) { | 920 TEST_F(TextfieldDragAndDropTest, DragAndDrop_ToTheLeft) { |
901 InitTextfield(); | 921 InitTextfield(); |
902 textfield_->SetText(ASCIIToUTF16("hello world")); | 922 textfield_->SetText(ASCIIToUTF16("hello world")); |
903 | 923 |
904 base::string16 string; | 924 base::string16 string; |
905 ui::OSExchangeData data; | 925 ui::OSExchangeData data; |
906 int formats = 0; | 926 int formats = 0; |
907 int operations = 0; | 927 int operations = 0; |
908 std::set<OSExchangeData::CustomFormat> custom_formats; | 928 std::set<OSExchangeData::CustomFormat> custom_formats; |
909 | 929 |
910 // Start dragging " worl". | 930 // Start dragging " worl". |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
943 SendKeyEvent(ui::VKEY_Z, false, true); | 963 SendKeyEvent(ui::VKEY_Z, false, true); |
944 EXPECT_STR_EQ("", textfield_->text()); | 964 EXPECT_STR_EQ("", textfield_->text()); |
945 SendKeyEvent(ui::VKEY_Y, false, true); | 965 SendKeyEvent(ui::VKEY_Y, false, true); |
946 EXPECT_STR_EQ("hello world", textfield_->text()); | 966 EXPECT_STR_EQ("hello world", textfield_->text()); |
947 SendKeyEvent(ui::VKEY_Y, false, true); | 967 SendKeyEvent(ui::VKEY_Y, false, true); |
948 EXPECT_STR_EQ("h worlellod", textfield_->text()); | 968 EXPECT_STR_EQ("h worlellod", textfield_->text()); |
949 SendKeyEvent(ui::VKEY_Y, false, true); | 969 SendKeyEvent(ui::VKEY_Y, false, true); |
950 EXPECT_STR_EQ("h worlellod", textfield_->text()); | 970 EXPECT_STR_EQ("h worlellod", textfield_->text()); |
951 } | 971 } |
952 | 972 |
953 TEST_F(TextfieldTest, DragAndDrop_Canceled) { | 973 TEST_F(TextfieldDragAndDropTest, DragAndDrop_Canceled) { |
954 InitTextfield(); | 974 InitTextfield(); |
955 textfield_->SetText(ASCIIToUTF16("hello world")); | 975 textfield_->SetText(ASCIIToUTF16("hello world")); |
956 | 976 |
957 // Start dragging "worl". | 977 // Start dragging "worl". |
958 textfield_->SelectRange(gfx::Range(6, 10)); | 978 textfield_->SelectRange(gfx::Range(6, 10)); |
959 gfx::Point point(GetCursorPositionX(8), 0); | 979 gfx::Point point(GetCursorPositionX(8), 0); |
960 ui::MouseEvent click(ui::ET_MOUSE_PRESSED, point, point, | 980 ui::MouseEvent click(ui::ET_MOUSE_PRESSED, point, point, |
961 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); | 981 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
962 textfield_->OnMousePressed(click); | 982 textfield_->OnMousePressed(click); |
963 ui::OSExchangeData data; | 983 ui::OSExchangeData data; |
(...skipping 980 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1944 // Set text which may fall back to a font which has taller baseline than | 1964 // Set text which may fall back to a font which has taller baseline than |
1945 // the default font. | 1965 // the default font. |
1946 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); | 1966 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); |
1947 const int new_baseline = textfield_->GetBaseline(); | 1967 const int new_baseline = textfield_->GetBaseline(); |
1948 | 1968 |
1949 // Regardless of the text, the baseline must be the same. | 1969 // Regardless of the text, the baseline must be the same. |
1950 EXPECT_EQ(new_baseline, old_baseline); | 1970 EXPECT_EQ(new_baseline, old_baseline); |
1951 } | 1971 } |
1952 | 1972 |
1953 } // namespace views | 1973 } // namespace views |
OLD | NEW |