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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/format_macros.h" | 15 #include "base/format_macros.h" |
16 #include "base/i18n/rtl.h" | 16 #include "base/i18n/rtl.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/pickle.h" | 18 #include "base/pickle.h" |
19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
23 #include "ui/accessibility/ax_view_state.h" | 23 #include "ui/accessibility/ax_view_state.h" |
24 #include "ui/base/clipboard/clipboard.h" | 24 #include "ui/base/clipboard/clipboard.h" |
25 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 25 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
26 #include "ui/base/dragdrop/drag_drop_types.h" | 26 #include "ui/base/dragdrop/drag_drop_types.h" |
27 #include "ui/base/ime/input_method_base.h" | 27 #include "ui/base/ime/input_method_base.h" |
28 #include "ui/base/ime/input_method_delegate.h" | 28 #include "ui/base/ime/input_method_delegate.h" |
29 #include "ui/base/ime/input_method_factory.h" | 29 #include "ui/base/ime/input_method_factory.h" |
30 #include "ui/base/ime/text_edit_commands.h" | |
30 #include "ui/base/ime/text_input_client.h" | 31 #include "ui/base/ime/text_input_client.h" |
31 #include "ui/base/ui_base_switches.h" | 32 #include "ui/base/ui_base_switches.h" |
32 #include "ui/base/ui_base_switches_util.h" | 33 #include "ui/base/ui_base_switches_util.h" |
33 #include "ui/events/event.h" | 34 #include "ui/events/event.h" |
34 #include "ui/events/event_processor.h" | 35 #include "ui/events/event_processor.h" |
35 #include "ui/events/event_utils.h" | 36 #include "ui/events/event_utils.h" |
36 #include "ui/events/keycodes/keyboard_codes.h" | 37 #include "ui/events/keycodes/keyboard_codes.h" |
37 #include "ui/events/test/event_generator.h" | 38 #include "ui/events/test/event_generator.h" |
38 #include "ui/gfx/render_text.h" | 39 #include "ui/gfx/render_text.h" |
39 #include "ui/strings/grit/ui_strings.h" | 40 #include "ui/strings/grit/ui_strings.h" |
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
825 SendKeyEvent(ui::VKEY_Z, true, false); | 826 SendKeyEvent(ui::VKEY_Z, true, false); |
826 SendKeyEvent(ui::VKEY_E, true, false); | 827 SendKeyEvent(ui::VKEY_E, true, false); |
827 SendKeyEvent(ui::VKEY_R, true, false); | 828 SendKeyEvent(ui::VKEY_R, true, false); |
828 SendKeyEvent(ui::VKEY_O, true, false); | 829 SendKeyEvent(ui::VKEY_O, true, false); |
829 SendKeyEvent(ui::VKEY_SPACE, false, false); | 830 SendKeyEvent(ui::VKEY_SPACE, false, false); |
830 EXPECT_STR_EQ("ZERO two three", textfield_->text()); | 831 EXPECT_STR_EQ("ZERO two three", textfield_->text()); |
831 | 832 |
832 SendEndEvent(true); | 833 SendEndEvent(true); |
833 EXPECT_STR_EQ("two three", textfield_->GetSelectedText()); | 834 EXPECT_STR_EQ("two three", textfield_->GetSelectedText()); |
834 SendHomeEvent(true); | 835 SendHomeEvent(true); |
836 | |
837 // On Mac, the existing selection should be extended. | |
838 #if defined(OS_MACOSX) | |
839 EXPECT_STR_EQ("ZERO two three", textfield_->GetSelectedText()); | |
840 #else | |
835 EXPECT_STR_EQ("ZERO ", textfield_->GetSelectedText()); | 841 EXPECT_STR_EQ("ZERO ", textfield_->GetSelectedText()); |
842 #endif | |
843 } | |
844 | |
845 TEST_F(TextfieldTest, WordSelection) { | |
846 InitTextfield(); | |
847 textfield_->SetText(ASCIIToUTF16("12 34567 89")); | |
848 | |
849 // Place the cursor after "5". | |
850 textfield_->SetSelectionRange(gfx::Range(6)); | |
851 | |
852 // Select word towards right. | |
853 SendWordEvent(ui::VKEY_RIGHT, true); | |
854 EXPECT_STR_EQ("67", textfield_->GetSelectedText()); | |
855 SendWordEvent(ui::VKEY_RIGHT, true); | |
856 EXPECT_STR_EQ("67 89", textfield_->GetSelectedText()); | |
857 | |
858 // Select word towards left. | |
859 SendWordEvent(ui::VKEY_LEFT, true); | |
860 EXPECT_STR_EQ("67 ", textfield_->GetSelectedText()); | |
861 SendWordEvent(ui::VKEY_LEFT, true); | |
862 | |
863 // On Mac, the selection should reduce to a caret when the selection direction | |
864 // changes for a word selection. | |
865 #if defined(OS_MACOSX) | |
866 EXPECT_EQ(gfx::Range(6), textfield_->GetSelectedRange()); | |
867 #else | |
868 EXPECT_STR_EQ("345", textfield_->GetSelectedText()); | |
869 EXPECT_EQ(gfx::Range(6, 3), textfield_->GetSelectedRange()); | |
870 #endif | |
871 | |
872 SendWordEvent(ui::VKEY_LEFT, true); | |
873 #if defined(OS_MACOSX) | |
874 EXPECT_STR_EQ("345", textfield_->GetSelectedText()); | |
875 #else | |
876 EXPECT_STR_EQ("12 345", textfield_->GetSelectedText()); | |
877 #endif | |
878 EXPECT_TRUE(textfield_->GetSelectedRange().is_reversed()); | |
879 | |
880 SendWordEvent(ui::VKEY_LEFT, true); | |
881 EXPECT_STR_EQ("12 345", textfield_->GetSelectedText()); | |
882 } | |
883 | |
884 TEST_F(TextfieldTest, LineSelection) { | |
885 InitTextfield(); | |
886 textfield_->SetText(ASCIIToUTF16("12 34567 89")); | |
887 | |
888 // Place the cursor after "5". | |
889 textfield_->SetSelectionRange(gfx::Range(6)); | |
890 | |
891 // Select line towards right. | |
892 SendEndEvent(true); | |
893 EXPECT_STR_EQ("67 89", textfield_->GetSelectedText()); | |
894 | |
895 // Select line towards left. On Mac, the existing selection should be extended | |
896 // to cover the whole line. | |
897 SendHomeEvent(true); | |
898 #if defined(OS_MACOSX) | |
899 EXPECT_EQ(textfield_->text(), textfield_->GetSelectedText()); | |
900 #else | |
901 EXPECT_STR_EQ("12 345", textfield_->GetSelectedText()); | |
902 #endif | |
903 EXPECT_TRUE(textfield_->GetSelectedRange().is_reversed()); | |
904 | |
905 // Select line towards right. | |
906 SendEndEvent(true); | |
907 #if defined(OS_MACOSX) | |
908 EXPECT_EQ(textfield_->text(), textfield_->GetSelectedText()); | |
909 #else | |
910 EXPECT_STR_EQ("67 89", textfield_->GetSelectedText()); | |
911 #endif | |
912 EXPECT_FALSE(textfield_->GetSelectedRange().is_reversed()); | |
913 } | |
914 | |
915 TEST_F(TextfieldTest, MoveUpDownAndModifySelection) { | |
916 InitTextfield(); | |
917 textfield_->SetText(ASCIIToUTF16("12 34567 89")); | |
918 const int kStringLength = 11; | |
msw
2016/08/16 18:37:58
optional nit: I think the test is more readable if
karandeepb
2016/08/17 04:37:23
Done.
| |
919 const int kInitialCursorPosition = 6; | |
920 | |
921 EXPECT_EQ(gfx::Range(kStringLength), textfield_->GetSelectedRange()); | |
msw
2016/08/16 18:37:58
nit: remove this (seems unnecessary)
karandeepb
2016/08/17 04:37:23
Done.
| |
922 textfield_->SetSelectionRange(gfx::Range(kInitialCursorPosition)); | |
923 | |
924 // Up/Down keys won't be handled except on Mac where they map to move | |
925 // commands. | |
926 SendKeyEvent(ui::VKEY_UP); | |
927 EXPECT_TRUE(textfield_->key_received()); | |
928 #if defined(OS_MACOSX) | |
929 EXPECT_TRUE(textfield_->key_handled()); | |
930 EXPECT_EQ(gfx::Range(0), textfield_->GetSelectedRange()); | |
931 #else | |
932 EXPECT_FALSE(textfield_->key_handled()); | |
933 #endif | |
934 textfield_->clear(); | |
935 | |
936 SendKeyEvent(ui::VKEY_DOWN); | |
937 EXPECT_TRUE(textfield_->key_received()); | |
938 #if defined(OS_MACOSX) | |
939 EXPECT_TRUE(textfield_->key_handled()); | |
940 EXPECT_EQ(gfx::Range(kStringLength), textfield_->GetSelectedRange()); | |
941 #else | |
942 EXPECT_FALSE(textfield_->key_handled()); | |
943 #endif | |
944 textfield_->clear(); | |
945 | |
946 textfield_->SetSelectionRange(gfx::Range(kInitialCursorPosition)); | |
947 | |
948 // Shift+[Up/Down] on Mac should execute the command | |
949 // MOVE_[UP/DOWN]_AND_MODIFY_SELECTION. On other platforms, textfield won't | |
950 // handle these events. | |
951 SendKeyEvent(ui::VKEY_UP, true /* shift */, false /* command */); | |
952 EXPECT_TRUE(textfield_->key_received()); | |
953 #if defined(OS_MACOSX) | |
954 EXPECT_TRUE(textfield_->key_handled()); | |
955 EXPECT_EQ(gfx::Range(kInitialCursorPosition, 0), | |
956 textfield_->GetSelectedRange()); | |
957 #else | |
958 EXPECT_FALSE(textfield_->key_handled()); | |
959 #endif | |
960 textfield_->clear(); | |
961 | |
962 SendKeyEvent(ui::VKEY_DOWN, true /* shift */, false /* command */); | |
963 EXPECT_TRUE(textfield_->key_received()); | |
964 #if defined(OS_MACOSX) | |
965 EXPECT_TRUE(textfield_->key_handled()); | |
966 EXPECT_EQ(gfx::Range(kInitialCursorPosition, kStringLength), | |
967 textfield_->GetSelectedRange()); | |
968 #else | |
969 EXPECT_FALSE(textfield_->key_handled()); | |
970 #endif | |
971 textfield_->clear(); | |
972 } | |
973 | |
974 TEST_F(TextfieldTest, MovePageUpDownAndModifySelection) { | |
975 InitTextfield(); | |
976 | |
977 // MOVE_PAGE_[UP/DOWN] and the associated selection commands should only be | |
978 // enabled on Mac. | |
979 #if defined(OS_MACOSX) | |
980 textfield_->SetText(ASCIIToUTF16("12 34567 89")); | |
981 const int kStringLength = 11; | |
982 const int kInitialCursorPosition = 6; | |
983 | |
984 textfield_->SetSelectionRange(gfx::Range(kInitialCursorPosition)); | |
985 | |
986 EXPECT_TRUE( | |
987 textfield_->IsTextEditCommandEnabled(ui::TextEditCommand::MOVE_PAGE_UP)); | |
988 EXPECT_TRUE(textfield_->IsTextEditCommandEnabled( | |
989 ui::TextEditCommand::MOVE_PAGE_DOWN)); | |
990 EXPECT_TRUE(textfield_->IsTextEditCommandEnabled( | |
991 ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION)); | |
992 EXPECT_TRUE(textfield_->IsTextEditCommandEnabled( | |
993 ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION)); | |
994 | |
995 test_api_->ExecuteTextEditCommand(ui::TextEditCommand::MOVE_PAGE_UP); | |
996 EXPECT_EQ(gfx::Range(0), textfield_->GetSelectedRange()); | |
997 | |
998 test_api_->ExecuteTextEditCommand(ui::TextEditCommand::MOVE_PAGE_DOWN); | |
999 EXPECT_EQ(gfx::Range(kStringLength), textfield_->GetSelectedRange()); | |
1000 | |
1001 textfield_->SetSelectionRange(gfx::Range(kInitialCursorPosition)); | |
1002 test_api_->ExecuteTextEditCommand( | |
1003 ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION); | |
1004 EXPECT_EQ(gfx::Range(kInitialCursorPosition, 0), | |
1005 textfield_->GetSelectedRange()); | |
1006 | |
1007 test_api_->ExecuteTextEditCommand( | |
1008 ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION); | |
1009 EXPECT_EQ(gfx::Range(kInitialCursorPosition, kStringLength), | |
1010 textfield_->GetSelectedRange()); | |
1011 #else | |
1012 EXPECT_FALSE( | |
1013 textfield_->IsTextEditCommandEnabled(ui::TextEditCommand::MOVE_PAGE_UP)); | |
1014 EXPECT_FALSE(textfield_->IsTextEditCommandEnabled( | |
1015 ui::TextEditCommand::MOVE_PAGE_DOWN)); | |
1016 EXPECT_FALSE(textfield_->IsTextEditCommandEnabled( | |
1017 ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION)); | |
1018 EXPECT_FALSE(textfield_->IsTextEditCommandEnabled( | |
1019 ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION)); | |
1020 #endif | |
1021 } | |
1022 | |
1023 TEST_F(TextfieldTest, MoveParagraphForwardBackwardAndModifySelection) { | |
1024 InitTextfield(); | |
1025 textfield_->SetText(ASCIIToUTF16("12 34567 89")); | |
1026 const int kStringLength = 11; | |
1027 const int kInitialCursorPosition = 6; | |
1028 | |
1029 textfield_->SetSelectionRange(gfx::Range(kInitialCursorPosition)); | |
1030 | |
1031 test_api_->ExecuteTextEditCommand( | |
1032 ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION); | |
1033 EXPECT_EQ(gfx::Range(kInitialCursorPosition, kStringLength), | |
1034 textfield_->GetSelectedRange()); | |
1035 | |
1036 test_api_->ExecuteTextEditCommand( | |
1037 ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION); | |
1038 EXPECT_EQ(gfx::Range(kInitialCursorPosition), textfield_->GetSelectedRange()); | |
msw
2016/08/16 18:37:58
You'll need to update this test if you address my
karandeepb
2016/08/17 04:37:23
Done.
| |
1039 | |
1040 test_api_->ExecuteTextEditCommand( | |
1041 ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION); | |
1042 EXPECT_EQ(gfx::Range(kInitialCursorPosition, 0), | |
1043 textfield_->GetSelectedRange()); | |
1044 | |
1045 test_api_->ExecuteTextEditCommand( | |
1046 ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION); | |
1047 EXPECT_EQ(gfx::Range(kInitialCursorPosition), textfield_->GetSelectedRange()); | |
836 } | 1048 } |
837 | 1049 |
838 TEST_F(TextfieldTest, InsertionDeletionTest) { | 1050 TEST_F(TextfieldTest, InsertionDeletionTest) { |
839 // Insert a test string in a textfield. | 1051 // Insert a test string in a textfield. |
840 InitTextfield(); | 1052 InitTextfield(); |
841 for (size_t i = 0; i < 10; i++) | 1053 for (size_t i = 0; i < 10; i++) |
842 SendKeyEvent(static_cast<ui::KeyboardCode>(ui::VKEY_A + i)); | 1054 SendKeyEvent(static_cast<ui::KeyboardCode>(ui::VKEY_A + i)); |
843 EXPECT_STR_EQ("abcdefghij", textfield_->text()); | 1055 EXPECT_STR_EQ("abcdefghij", textfield_->text()); |
844 | 1056 |
845 // Test the delete and backspace keys. | 1057 // Test the delete and backspace keys. |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1045 SendKeyEvent(ui::VKEY_F20); | 1257 SendKeyEvent(ui::VKEY_F20); |
1046 #if defined(OS_MACOSX) | 1258 #if defined(OS_MACOSX) |
1047 // On Mac, key combinations that don't map to editing commands are forwarded | 1259 // On Mac, key combinations that don't map to editing commands are forwarded |
1048 // on to the next responder, usually ending up at the window, which will beep. | 1260 // on to the next responder, usually ending up at the window, which will beep. |
1049 EXPECT_FALSE(textfield_->key_received()); | 1261 EXPECT_FALSE(textfield_->key_received()); |
1050 #else | 1262 #else |
1051 EXPECT_TRUE(textfield_->key_received()); | 1263 EXPECT_TRUE(textfield_->key_received()); |
1052 #endif | 1264 #endif |
1053 EXPECT_FALSE(textfield_->key_handled()); | 1265 EXPECT_FALSE(textfield_->key_handled()); |
1054 textfield_->clear(); | 1266 textfield_->clear(); |
1055 | |
1056 // Up/Down keys won't be handled except on Mac where they map to move | |
1057 // commands. | |
1058 SendKeyEvent(ui::VKEY_UP); | |
1059 EXPECT_TRUE(textfield_->key_received()); | |
1060 #if defined(OS_MACOSX) | |
1061 EXPECT_TRUE(textfield_->key_handled()); | |
1062 #else | |
1063 EXPECT_FALSE(textfield_->key_handled()); | |
1064 #endif | |
1065 textfield_->clear(); | |
1066 | |
1067 SendKeyEvent(ui::VKEY_DOWN); | |
1068 EXPECT_TRUE(textfield_->key_received()); | |
1069 #if defined(OS_MACOSX) | |
1070 EXPECT_TRUE(textfield_->key_handled()); | |
1071 #else | |
1072 EXPECT_FALSE(textfield_->key_handled()); | |
1073 #endif | |
1074 textfield_->clear(); | |
1075 } | 1267 } |
1076 | 1268 |
1077 // Tests that default key bindings are handled even with a delegate installed. | 1269 // Tests that default key bindings are handled even with a delegate installed. |
1078 TEST_F(TextfieldTest, OnKeyPressBinding) { | 1270 TEST_F(TextfieldTest, OnKeyPressBinding) { |
1079 InitTextfield(); | 1271 InitTextfield(); |
1080 | 1272 |
1081 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 1273 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
1082 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. | 1274 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. |
1083 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { | 1275 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { |
1084 public: | 1276 public: |
(...skipping 1668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2753 | 2945 |
2754 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | 2946 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
2755 ui::AXViewState state_protected; | 2947 ui::AXViewState state_protected; |
2756 textfield_->GetAccessibleState(&state_protected); | 2948 textfield_->GetAccessibleState(&state_protected); |
2757 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); | 2949 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); |
2758 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); | 2950 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); |
2759 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); | 2951 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); |
2760 } | 2952 } |
2761 | 2953 |
2762 } // namespace views | 2954 } // namespace views |
OLD | NEW |