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 SendHomeEvent(false); |
| 849 |
| 850 // Place the cursor after "5". |
| 851 textfield_->SetSelectionRange(gfx::Range(6, 6)); |
| 852 |
| 853 // Select word towards right. |
| 854 SendWordEvent(ui::VKEY_RIGHT, true); |
| 855 EXPECT_STR_EQ("67", textfield_->GetSelectedText()); |
| 856 SendWordEvent(ui::VKEY_RIGHT, true); |
| 857 EXPECT_STR_EQ("67 89", textfield_->GetSelectedText()); |
| 858 |
| 859 // Select word towards left. |
| 860 SendWordEvent(ui::VKEY_LEFT, true); |
| 861 EXPECT_STR_EQ("67 ", textfield_->GetSelectedText()); |
| 862 SendWordEvent(ui::VKEY_LEFT, true); |
| 863 |
| 864 // On Mac, the selection should reduce to a caret when the selection direction |
| 865 // changes for a word selection. |
| 866 #if defined(OS_MACOSX) |
| 867 EXPECT_TRUE(textfield_->GetSelectedRange().is_empty()); |
| 868 #else |
| 869 EXPECT_STR_EQ("345", textfield_->GetSelectedText()); |
| 870 EXPECT_EQ(gfx::Range(6, 3), textfield_->GetSelectedRange()); |
| 871 #endif |
| 872 |
| 873 SendWordEvent(ui::VKEY_LEFT, true); |
| 874 #if defined(OS_MACOSX) |
| 875 EXPECT_STR_EQ("345", textfield_->GetSelectedText()); |
| 876 #else |
| 877 EXPECT_STR_EQ("12 345", textfield_->GetSelectedText()); |
| 878 #endif |
| 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; |
| 919 const int kInitialCursorPosition = 6; |
| 920 |
| 921 EXPECT_EQ(gfx::Range(kStringLength), textfield_->GetSelectedRange()); |
| 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. // Todo add comment about default key binding. |
| 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 |
| 1026 // MOVE_PARAGRAPH_[FORWARD/BACKWARD]_AND_MODIFY_SELECTION and the associated |
| 1027 // selection commands should only be enabled on Mac. // Todo add comment about |
| 1028 // default key binding. |
| 1029 #if defined(OS_MACOSX) |
| 1030 textfield_->SetText(ASCIIToUTF16("12 34567 89")); |
| 1031 const int kStringLength = 11; |
| 1032 const int kInitialCursorPosition = 6; |
| 1033 |
| 1034 textfield_->SetSelectionRange(gfx::Range(kInitialCursorPosition)); |
| 1035 EXPECT_TRUE(textfield_->IsTextEditCommandEnabled( |
| 1036 ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION)); |
| 1037 EXPECT_TRUE(textfield_->IsTextEditCommandEnabled( |
| 1038 ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION)); |
| 1039 |
| 1040 test_api_->ExecuteTextEditCommand( |
| 1041 ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION); |
| 1042 EXPECT_EQ(gfx::Range(kInitialCursorPosition, kStringLength), |
| 1043 textfield_->GetSelectedRange()); |
| 1044 |
| 1045 test_api_->ExecuteTextEditCommand( |
| 1046 ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION); |
| 1047 EXPECT_EQ(gfx::Range(kInitialCursorPosition), textfield_->GetSelectedRange()); |
| 1048 |
| 1049 test_api_->ExecuteTextEditCommand( |
| 1050 ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION); |
| 1051 EXPECT_EQ(gfx::Range(kInitialCursorPosition, 0), |
| 1052 textfield_->GetSelectedRange()); |
| 1053 |
| 1054 test_api_->ExecuteTextEditCommand( |
| 1055 ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION); |
| 1056 EXPECT_EQ(gfx::Range(kInitialCursorPosition), textfield_->GetSelectedRange()); |
| 1057 #else |
| 1058 EXPECT_FALSE(textfield_->IsTextEditCommandEnabled( |
| 1059 ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION)); |
| 1060 EXPECT_FALSE(textfield_->IsTextEditCommandEnabled( |
| 1061 ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION)); |
| 1062 #endif |
836 } | 1063 } |
837 | 1064 |
838 TEST_F(TextfieldTest, InsertionDeletionTest) { | 1065 TEST_F(TextfieldTest, InsertionDeletionTest) { |
839 // Insert a test string in a textfield. | 1066 // Insert a test string in a textfield. |
840 InitTextfield(); | 1067 InitTextfield(); |
841 for (size_t i = 0; i < 10; i++) | 1068 for (size_t i = 0; i < 10; i++) |
842 SendKeyEvent(static_cast<ui::KeyboardCode>(ui::VKEY_A + i)); | 1069 SendKeyEvent(static_cast<ui::KeyboardCode>(ui::VKEY_A + i)); |
843 EXPECT_STR_EQ("abcdefghij", textfield_->text()); | 1070 EXPECT_STR_EQ("abcdefghij", textfield_->text()); |
844 | 1071 |
845 // Test the delete and backspace keys. | 1072 // Test the delete and backspace keys. |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1045 SendKeyEvent(ui::VKEY_F20); | 1272 SendKeyEvent(ui::VKEY_F20); |
1046 #if defined(OS_MACOSX) | 1273 #if defined(OS_MACOSX) |
1047 // On Mac, key combinations that don't map to editing commands are forwarded | 1274 // 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. | 1275 // on to the next responder, usually ending up at the window, which will beep. |
1049 EXPECT_FALSE(textfield_->key_received()); | 1276 EXPECT_FALSE(textfield_->key_received()); |
1050 #else | 1277 #else |
1051 EXPECT_TRUE(textfield_->key_received()); | 1278 EXPECT_TRUE(textfield_->key_received()); |
1052 #endif | 1279 #endif |
1053 EXPECT_FALSE(textfield_->key_handled()); | 1280 EXPECT_FALSE(textfield_->key_handled()); |
1054 textfield_->clear(); | 1281 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 } | 1282 } |
1076 | 1283 |
1077 // Tests that default key bindings are handled even with a delegate installed. | 1284 // Tests that default key bindings are handled even with a delegate installed. |
1078 TEST_F(TextfieldTest, OnKeyPressBinding) { | 1285 TEST_F(TextfieldTest, OnKeyPressBinding) { |
1079 InitTextfield(); | 1286 InitTextfield(); |
1080 | 1287 |
1081 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 1288 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
1082 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. | 1289 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. |
1083 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { | 1290 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { |
1084 public: | 1291 public: |
(...skipping 1668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2753 | 2960 |
2754 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | 2961 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
2755 ui::AXViewState state_protected; | 2962 ui::AXViewState state_protected; |
2756 textfield_->GetAccessibleState(&state_protected); | 2963 textfield_->GetAccessibleState(&state_protected); |
2757 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); | 2964 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); |
2758 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); | 2965 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); |
2759 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); | 2966 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); |
2760 } | 2967 } |
2761 | 2968 |
2762 } // namespace views | 2969 } // namespace views |
OLD | NEW |