Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: ui/views/controls/textfield/textfield_unittest.cc

Issue 2228833002: MacViews: Fix behavior of move and select commands when selection direction changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@use_text_commands
Patch Set: Address review comments. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/textfield/textfield_test_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 textfield_->SetSelectionRange(gfx::Range(6));
919
920 // Up/Down keys won't be handled except on Mac where they map to move
921 // commands.
922 SendKeyEvent(ui::VKEY_UP);
923 EXPECT_TRUE(textfield_->key_received());
924 #if defined(OS_MACOSX)
925 EXPECT_TRUE(textfield_->key_handled());
926 EXPECT_EQ(gfx::Range(0), textfield_->GetSelectedRange());
927 #else
928 EXPECT_FALSE(textfield_->key_handled());
929 #endif
930 textfield_->clear();
931
932 SendKeyEvent(ui::VKEY_DOWN);
933 EXPECT_TRUE(textfield_->key_received());
934 #if defined(OS_MACOSX)
935 EXPECT_TRUE(textfield_->key_handled());
936 EXPECT_EQ(gfx::Range(11), textfield_->GetSelectedRange());
937 #else
938 EXPECT_FALSE(textfield_->key_handled());
939 #endif
940 textfield_->clear();
941
942 textfield_->SetSelectionRange(gfx::Range(6));
943
944 // Shift+[Up/Down] on Mac should execute the command
945 // MOVE_[UP/DOWN]_AND_MODIFY_SELECTION. On other platforms, textfield won't
946 // handle these events.
947 SendKeyEvent(ui::VKEY_UP, true /* shift */, false /* command */);
948 EXPECT_TRUE(textfield_->key_received());
949 #if defined(OS_MACOSX)
950 EXPECT_TRUE(textfield_->key_handled());
951 EXPECT_EQ(gfx::Range(6, 0), textfield_->GetSelectedRange());
952 #else
953 EXPECT_FALSE(textfield_->key_handled());
954 #endif
955 textfield_->clear();
956
957 SendKeyEvent(ui::VKEY_DOWN, true /* shift */, false /* command */);
958 EXPECT_TRUE(textfield_->key_received());
959 #if defined(OS_MACOSX)
960 EXPECT_TRUE(textfield_->key_handled());
961 EXPECT_EQ(gfx::Range(6, 11), textfield_->GetSelectedRange());
962 #else
963 EXPECT_FALSE(textfield_->key_handled());
964 #endif
965 textfield_->clear();
966 }
967
968 TEST_F(TextfieldTest, MovePageUpDownAndModifySelection) {
969 InitTextfield();
970
971 // MOVE_PAGE_[UP/DOWN] and the associated selection commands should only be
972 // enabled on Mac.
973 #if defined(OS_MACOSX)
974 textfield_->SetText(ASCIIToUTF16("12 34567 89"));
975 textfield_->SetSelectionRange(gfx::Range(6));
976
977 EXPECT_TRUE(
978 textfield_->IsTextEditCommandEnabled(ui::TextEditCommand::MOVE_PAGE_UP));
979 EXPECT_TRUE(textfield_->IsTextEditCommandEnabled(
980 ui::TextEditCommand::MOVE_PAGE_DOWN));
981 EXPECT_TRUE(textfield_->IsTextEditCommandEnabled(
982 ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION));
983 EXPECT_TRUE(textfield_->IsTextEditCommandEnabled(
984 ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION));
985
986 test_api_->ExecuteTextEditCommand(ui::TextEditCommand::MOVE_PAGE_UP);
987 EXPECT_EQ(gfx::Range(0), textfield_->GetSelectedRange());
988
989 test_api_->ExecuteTextEditCommand(ui::TextEditCommand::MOVE_PAGE_DOWN);
990 EXPECT_EQ(gfx::Range(11), textfield_->GetSelectedRange());
991
992 textfield_->SetSelectionRange(gfx::Range(6));
993 test_api_->ExecuteTextEditCommand(
994 ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION);
995 EXPECT_EQ(gfx::Range(6, 0), textfield_->GetSelectedRange());
996
997 test_api_->ExecuteTextEditCommand(
998 ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION);
999 EXPECT_EQ(gfx::Range(6, 11), textfield_->GetSelectedRange());
1000 #else
1001 EXPECT_FALSE(
1002 textfield_->IsTextEditCommandEnabled(ui::TextEditCommand::MOVE_PAGE_UP));
1003 EXPECT_FALSE(textfield_->IsTextEditCommandEnabled(
1004 ui::TextEditCommand::MOVE_PAGE_DOWN));
1005 EXPECT_FALSE(textfield_->IsTextEditCommandEnabled(
1006 ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION));
1007 EXPECT_FALSE(textfield_->IsTextEditCommandEnabled(
1008 ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION));
1009 #endif
1010 }
1011
1012 TEST_F(TextfieldTest, MoveParagraphForwardBackwardAndModifySelection) {
1013 InitTextfield();
1014 textfield_->SetText(ASCIIToUTF16("12 34567 89"));
1015 textfield_->SetSelectionRange(gfx::Range(6));
1016
1017 test_api_->ExecuteTextEditCommand(
1018 ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION);
1019 EXPECT_EQ(gfx::Range(6, 11), textfield_->GetSelectedRange());
1020
1021 test_api_->ExecuteTextEditCommand(
1022 ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION);
1023 // On Mac, the selection should reduce to a caret when the selection direction
1024 // is reversed for MOVE_PARAGRAPH_[FORWARD/BACKWARD]_AND_MODIFY_SELECTION.
1025 #if defined(OS_MACOSX)
1026 EXPECT_EQ(gfx::Range(6), textfield_->GetSelectedRange());
1027 #else
1028 EXPECT_EQ(gfx::Range(6, 0), textfield_->GetSelectedRange());
1029 #endif
1030
1031 test_api_->ExecuteTextEditCommand(
1032 ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION);
1033 EXPECT_EQ(gfx::Range(6, 0), textfield_->GetSelectedRange());
1034
1035 test_api_->ExecuteTextEditCommand(
1036 ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION);
1037 #if defined(OS_MACOSX)
1038 EXPECT_EQ(gfx::Range(6), textfield_->GetSelectedRange());
1039 #else
1040 EXPECT_EQ(gfx::Range(6, 11), textfield_->GetSelectedRange());
1041 #endif
836 } 1042 }
837 1043
838 TEST_F(TextfieldTest, InsertionDeletionTest) { 1044 TEST_F(TextfieldTest, InsertionDeletionTest) {
839 // Insert a test string in a textfield. 1045 // Insert a test string in a textfield.
840 InitTextfield(); 1046 InitTextfield();
841 for (size_t i = 0; i < 10; i++) 1047 for (size_t i = 0; i < 10; i++)
842 SendKeyEvent(static_cast<ui::KeyboardCode>(ui::VKEY_A + i)); 1048 SendKeyEvent(static_cast<ui::KeyboardCode>(ui::VKEY_A + i));
843 EXPECT_STR_EQ("abcdefghij", textfield_->text()); 1049 EXPECT_STR_EQ("abcdefghij", textfield_->text());
844 1050
845 // Test the delete and backspace keys. 1051 // Test the delete and backspace keys.
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 SendKeyEvent(ui::VKEY_F20); 1251 SendKeyEvent(ui::VKEY_F20);
1046 #if defined(OS_MACOSX) 1252 #if defined(OS_MACOSX)
1047 // On Mac, key combinations that don't map to editing commands are forwarded 1253 // 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. 1254 // on to the next responder, usually ending up at the window, which will beep.
1049 EXPECT_FALSE(textfield_->key_received()); 1255 EXPECT_FALSE(textfield_->key_received());
1050 #else 1256 #else
1051 EXPECT_TRUE(textfield_->key_received()); 1257 EXPECT_TRUE(textfield_->key_received());
1052 #endif 1258 #endif
1053 EXPECT_FALSE(textfield_->key_handled()); 1259 EXPECT_FALSE(textfield_->key_handled());
1054 textfield_->clear(); 1260 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 } 1261 }
1076 1262
1077 // Tests that default key bindings are handled even with a delegate installed. 1263 // Tests that default key bindings are handled even with a delegate installed.
1078 TEST_F(TextfieldTest, OnKeyPressBinding) { 1264 TEST_F(TextfieldTest, OnKeyPressBinding) {
1079 InitTextfield(); 1265 InitTextfield();
1080 1266
1081 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 1267 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1082 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing. 1268 // Install a TextEditKeyBindingsDelegateAuraLinux that does nothing.
1083 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux { 1269 class TestDelegate : public ui::TextEditKeyBindingsDelegateAuraLinux {
1084 public: 1270 public:
(...skipping 1668 matching lines...) Expand 10 before | Expand all | Expand 10 after
2753 2939
2754 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); 2940 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
2755 ui::AXViewState state_protected; 2941 ui::AXViewState state_protected;
2756 textfield_->GetAccessibleState(&state_protected); 2942 textfield_->GetAccessibleState(&state_protected);
2757 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); 2943 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role);
2758 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); 2944 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value);
2759 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); 2945 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED));
2760 } 2946 }
2761 2947
2762 } // namespace views 2948 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/textfield/textfield_test_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698