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

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

Issue 1865063004: views: support vertical-drag-to-end on textfields (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test fixups for new API Created 4 years, 8 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.cc ('k') | ui/views/style/platform_style.h » ('j') | 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>
(...skipping 21 matching lines...) Expand all
32 #include "ui/events/event_processor.h" 32 #include "ui/events/event_processor.h"
33 #include "ui/events/event_utils.h" 33 #include "ui/events/event_utils.h"
34 #include "ui/events/keycodes/keyboard_codes.h" 34 #include "ui/events/keycodes/keyboard_codes.h"
35 #include "ui/events/test/event_generator.h" 35 #include "ui/events/test/event_generator.h"
36 #include "ui/gfx/render_text.h" 36 #include "ui/gfx/render_text.h"
37 #include "ui/strings/grit/ui_strings.h" 37 #include "ui/strings/grit/ui_strings.h"
38 #include "ui/views/controls/textfield/textfield_controller.h" 38 #include "ui/views/controls/textfield/textfield_controller.h"
39 #include "ui/views/controls/textfield/textfield_model.h" 39 #include "ui/views/controls/textfield/textfield_model.h"
40 #include "ui/views/controls/textfield/textfield_test_api.h" 40 #include "ui/views/controls/textfield/textfield_test_api.h"
41 #include "ui/views/focus/focus_manager.h" 41 #include "ui/views/focus/focus_manager.h"
42 #include "ui/views/style/platform_style.h"
42 #include "ui/views/test/test_views_delegate.h" 43 #include "ui/views/test/test_views_delegate.h"
43 #include "ui/views/test/views_test_base.h" 44 #include "ui/views/test/views_test_base.h"
44 #include "ui/views/test/widget_test.h" 45 #include "ui/views/test/widget_test.h"
45 #include "ui/views/widget/widget.h" 46 #include "ui/views/widget/widget.h"
46 #include "url/gurl.h" 47 #include "url/gurl.h"
47 48
48 #if defined(OS_WIN) 49 #if defined(OS_WIN)
49 #include "base/win/windows_version.h" 50 #include "base/win/windows_version.h"
50 #endif 51 #endif
51 52
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 EXPECT_TRUE(menu->IsEnabledAt(1 /* Separator */)); 628 EXPECT_TRUE(menu->IsEnabledAt(1 /* Separator */));
628 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(2 /* CUT */)); 629 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(2 /* CUT */));
629 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(3 /* COPY */)); 630 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(3 /* COPY */));
630 EXPECT_NE(GetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE).empty(), 631 EXPECT_NE(GetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE).empty(),
631 menu->IsEnabledAt(4 /* PASTE */)); 632 menu->IsEnabledAt(4 /* PASTE */));
632 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(5 /* DELETE */)); 633 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(5 /* DELETE */));
633 EXPECT_TRUE(menu->IsEnabledAt(6 /* Separator */)); 634 EXPECT_TRUE(menu->IsEnabledAt(6 /* Separator */));
634 EXPECT_TRUE(menu->IsEnabledAt(7 /* SELECT ALL */)); 635 EXPECT_TRUE(menu->IsEnabledAt(7 /* SELECT ALL */));
635 } 636 }
636 637
638 void PressLeftMouseButton(int extra_flags) {
639 ui::MouseEvent click(ui::ET_MOUSE_PRESSED, mouse_position_, mouse_position_,
640 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
641 ui::EF_LEFT_MOUSE_BUTTON | extra_flags);
642 textfield_->OnMousePressed(click);
643 }
644
645 void PressLeftMouseButton() {
tapted 2016/04/18 01:12:51 (note style guide now allows default arguments if
646 PressLeftMouseButton(0);
647 }
648
649 void ReleaseLeftMouseButton() {
650 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, mouse_position_,
651 mouse_position_, ui::EventTimeForNow(),
652 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
653 textfield_->OnMouseReleased(release);
654 }
655
656 void ClickLeftMouseButton(int extra_flags) {
657 PressLeftMouseButton(extra_flags);
658 ReleaseLeftMouseButton();
659 }
660
661 void ClickLeftMouseButton() {
662 ClickLeftMouseButton(0);
663 }
664
665 void DragMouseTo(const gfx::Point& where) {
666 mouse_position_ = where;
667 ui::MouseEvent drag(ui::ET_MOUSE_DRAGGED, where, where,
668 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
669 textfield_->OnMouseDragged(drag);
670 }
671
672 // Textfield does not listen to OnMouseMoved, so this function does not send
673 // an event when it updates the cursor position.
674 void MoveMouseTo(const gfx::Point& where) { mouse_position_ = where; }
675
637 // We need widget to populate wrapper class. 676 // We need widget to populate wrapper class.
638 Widget* widget_; 677 Widget* widget_;
639 678
640 TestTextfield* textfield_; 679 TestTextfield* textfield_;
641 std::unique_ptr<TextfieldTestApi> test_api_; 680 std::unique_ptr<TextfieldTestApi> test_api_;
642 TextfieldModel* model_; 681 TextfieldModel* model_;
643 682
644 // The string from Controller::ContentsChanged callback. 683 // The string from Controller::ContentsChanged callback.
645 base::string16 last_contents_; 684 base::string16 last_contents_;
646 685
647 // For testing input method related behaviors. 686 // For testing input method related behaviors.
648 MockInputMethod* input_method_; 687 MockInputMethod* input_method_;
649 688
650 // Indicates how many times OnBeforeUserAction() is called. 689 // Indicates how many times OnBeforeUserAction() is called.
651 int on_before_user_action_; 690 int on_before_user_action_;
652 691
653 // Indicates how many times OnAfterUserAction() is called. 692 // Indicates how many times OnAfterUserAction() is called.
654 int on_after_user_action_; 693 int on_after_user_action_;
655 694
656 private: 695 private:
696 // Position of the mouse for synthetic mouse events.
697 gfx::Point mouse_position_;
657 ui::ClipboardType copied_to_clipboard_; 698 ui::ClipboardType copied_to_clipboard_;
658 std::unique_ptr<ui::test::EventGenerator> event_generator_; 699 std::unique_ptr<ui::test::EventGenerator> event_generator_;
659 DISALLOW_COPY_AND_ASSIGN(TextfieldTest); 700 DISALLOW_COPY_AND_ASSIGN(TextfieldTest);
660 }; 701 };
661 702
662 TEST_F(TextfieldTest, ModelChangesTest) { 703 TEST_F(TextfieldTest, ModelChangesTest) {
663 InitTextfield(); 704 InitTextfield();
664 705
665 // TextfieldController::ContentsChanged() shouldn't be called when changing 706 // TextfieldController::ContentsChanged() shouldn't be called when changing
666 // text programmatically. 707 // text programmatically.
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
1066 widget_->GetFocusManager()->AdvanceFocus(true); 1107 widget_->GetFocusManager()->AdvanceFocus(true);
1067 EXPECT_EQ(3, GetFocusedView()->id()); 1108 EXPECT_EQ(3, GetFocusedView()->id());
1068 1109
1069 // Request focus should still work. 1110 // Request focus should still work.
1070 textfield_->RequestFocus(); 1111 textfield_->RequestFocus();
1071 EXPECT_EQ(1, GetFocusedView()->id()); 1112 EXPECT_EQ(1, GetFocusedView()->id());
1072 1113
1073 // Test if clicking on textfield view sets the focus. 1114 // Test if clicking on textfield view sets the focus.
1074 widget_->GetFocusManager()->AdvanceFocus(true); 1115 widget_->GetFocusManager()->AdvanceFocus(true);
1075 EXPECT_EQ(3, GetFocusedView()->id()); 1116 EXPECT_EQ(3, GetFocusedView()->id());
1076 ui::MouseEvent click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), 1117 MoveMouseTo(gfx::Point());
1077 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 1118 ClickLeftMouseButton();
1078 ui::EF_LEFT_MOUSE_BUTTON);
1079 textfield_->OnMousePressed(click);
1080 EXPECT_EQ(1, GetFocusedView()->id()); 1119 EXPECT_EQ(1, GetFocusedView()->id());
1081 1120
1082 // Tab/Shift+Tab should also cycle focus, not insert a tab character. 1121 // Tab/Shift+Tab should also cycle focus, not insert a tab character.
1083 SendKeyEvent(ui::VKEY_TAB, false, false); 1122 SendKeyEvent(ui::VKEY_TAB, false, false);
1084 EXPECT_EQ(2, GetFocusedView()->id()); 1123 EXPECT_EQ(2, GetFocusedView()->id());
1085 SendKeyEvent(ui::VKEY_TAB, false, false); 1124 SendKeyEvent(ui::VKEY_TAB, false, false);
1086 EXPECT_EQ(3, GetFocusedView()->id()); 1125 EXPECT_EQ(3, GetFocusedView()->id());
1087 // Cycle back to the first textfield. 1126 // Cycle back to the first textfield.
1088 SendKeyEvent(ui::VKEY_TAB, false, false); 1127 SendKeyEvent(ui::VKEY_TAB, false, false);
1089 EXPECT_EQ(1, GetFocusedView()->id()); 1128 EXPECT_EQ(1, GetFocusedView()->id());
(...skipping 28 matching lines...) Expand all
1118 VerifyTextfieldContextMenuContents(true, true, GetContextMenuModel()); 1157 VerifyTextfieldContextMenuContents(true, true, GetContextMenuModel());
1119 1158
1120 // Exercise the "paste enabled?" check in the verifier. 1159 // Exercise the "paste enabled?" check in the verifier.
1121 SetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE, "Test"); 1160 SetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE, "Test");
1122 VerifyTextfieldContextMenuContents(true, true, GetContextMenuModel()); 1161 VerifyTextfieldContextMenuContents(true, true, GetContextMenuModel());
1123 } 1162 }
1124 1163
1125 TEST_F(TextfieldTest, DoubleAndTripleClickTest) { 1164 TEST_F(TextfieldTest, DoubleAndTripleClickTest) {
1126 InitTextfield(); 1165 InitTextfield();
1127 textfield_->SetText(ASCIIToUTF16("hello world")); 1166 textfield_->SetText(ASCIIToUTF16("hello world"));
1128 ui::MouseEvent click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
1129 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
1130 ui::EF_LEFT_MOUSE_BUTTON);
1131 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(),
1132 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
1133 ui::EF_LEFT_MOUSE_BUTTON);
1134 ui::MouseEvent double_click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
1135 ui::EventTimeForNow(),
1136 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK,
1137 ui::EF_LEFT_MOUSE_BUTTON);
1138 1167
1139 // Test for double click. 1168 // Test for double click.
1140 textfield_->OnMousePressed(click); 1169 MoveMouseTo(gfx::Point());
1141 textfield_->OnMouseReleased(release); 1170 ClickLeftMouseButton();
1142 EXPECT_TRUE(textfield_->GetSelectedText().empty()); 1171 EXPECT_TRUE(textfield_->GetSelectedText().empty());
1143 textfield_->OnMousePressed(double_click); 1172 ClickLeftMouseButton(ui::EF_IS_DOUBLE_CLICK);
1144 textfield_->OnMouseReleased(release);
1145 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); 1173 EXPECT_STR_EQ("hello", textfield_->GetSelectedText());
1146 1174
1147 // Test for triple click. 1175 // Test for triple click.
1148 textfield_->OnMousePressed(click); 1176 ClickLeftMouseButton();
1149 textfield_->OnMouseReleased(release);
1150 EXPECT_STR_EQ("hello world", textfield_->GetSelectedText()); 1177 EXPECT_STR_EQ("hello world", textfield_->GetSelectedText());
1151 1178
1152 // Another click should reset back to double click. 1179 // Another click should reset back to double click.
1153 textfield_->OnMousePressed(click); 1180 ClickLeftMouseButton();
1154 textfield_->OnMouseReleased(release);
1155 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); 1181 EXPECT_STR_EQ("hello", textfield_->GetSelectedText());
1156 } 1182 }
1157 1183
1158 TEST_F(TextfieldTest, DragToSelect) { 1184 TEST_F(TextfieldTest, DragToSelect) {
1159 InitTextfield(); 1185 InitTextfield();
1160 textfield_->SetText(ASCIIToUTF16("hello world")); 1186 textfield_->SetText(ASCIIToUTF16("hello world"));
1161 const int kStart = GetCursorPositionX(5); 1187 const int kStart = GetCursorPositionX(5);
1162 const int kEnd = 500; 1188 const int kEnd = 500;
1163 gfx::Point start_point(kStart, 0); 1189 gfx::Point start_point(kStart, 0);
1164 gfx::Point end_point(kEnd, 0); 1190 gfx::Point end_point(kEnd, 0);
1165 ui::MouseEvent click_a(ui::ET_MOUSE_PRESSED, start_point, start_point, 1191
1166 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 1192 MoveMouseTo(start_point);
1167 ui::EF_LEFT_MOUSE_BUTTON); 1193 PressLeftMouseButton();
1168 ui::MouseEvent click_b(ui::ET_MOUSE_PRESSED, end_point, end_point,
1169 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
1170 ui::EF_LEFT_MOUSE_BUTTON);
1171 ui::MouseEvent drag_left(ui::ET_MOUSE_DRAGGED, gfx::Point(), gfx::Point(),
1172 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
1173 ui::MouseEvent drag_right(ui::ET_MOUSE_DRAGGED, end_point, end_point,
1174 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
1175 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, end_point, end_point,
1176 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
1177 ui::EF_LEFT_MOUSE_BUTTON);
1178 textfield_->OnMousePressed(click_a);
1179 EXPECT_TRUE(textfield_->GetSelectedText().empty()); 1194 EXPECT_TRUE(textfield_->GetSelectedText().empty());
1195
1180 // Check that dragging left selects the beginning of the string. 1196 // Check that dragging left selects the beginning of the string.
1181 textfield_->OnMouseDragged(drag_left); 1197 DragMouseTo(gfx::Point());
1182 base::string16 text_left = textfield_->GetSelectedText(); 1198 base::string16 text_left = textfield_->GetSelectedText();
1183 EXPECT_STR_EQ("hello", text_left); 1199 EXPECT_STR_EQ("hello", text_left);
1200
1184 // Check that dragging right selects the rest of the string. 1201 // Check that dragging right selects the rest of the string.
1185 textfield_->OnMouseDragged(drag_right); 1202 DragMouseTo(end_point);
1186 base::string16 text_right = textfield_->GetSelectedText(); 1203 base::string16 text_right = textfield_->GetSelectedText();
1187 EXPECT_STR_EQ(" world", text_right); 1204 EXPECT_STR_EQ(" world", text_right);
1205
1188 // Check that releasing in the same location does not alter the selection. 1206 // Check that releasing in the same location does not alter the selection.
1189 textfield_->OnMouseReleased(release); 1207 ReleaseLeftMouseButton();
1190 EXPECT_EQ(text_right, textfield_->GetSelectedText()); 1208 EXPECT_EQ(text_right, textfield_->GetSelectedText());
1209
1191 // Check that dragging from beyond the text length works too. 1210 // Check that dragging from beyond the text length works too.
1192 textfield_->OnMousePressed(click_b); 1211 MoveMouseTo(end_point);
1193 textfield_->OnMouseDragged(drag_left); 1212 PressLeftMouseButton();
1194 textfield_->OnMouseReleased(release); 1213 DragMouseTo(gfx::Point());
1214 ReleaseLeftMouseButton();
1195 EXPECT_EQ(textfield_->text(), textfield_->GetSelectedText()); 1215 EXPECT_EQ(textfield_->text(), textfield_->GetSelectedText());
1196 } 1216 }
1197 1217
1218 // This test checks that dragging above the textfield selects to the beginning
1219 // and dragging below the textfield selects to the end, but only on platforms
1220 // where that is the expected behavior.
1221 TEST_F(TextfieldTest, DragUpOrDownSelectsToEnd) {
1222 InitTextfield();
1223 textfield_->SetText(ASCIIToUTF16("hello world"));
1224 const base::string16 expected_up = base::ASCIIToUTF16(
1225 PlatformStyle::kTextfieldDragVerticallyDragsToEnd ? "hello" : "lo");
1226 const base::string16 expected_down = base::ASCIIToUTF16(
1227 PlatformStyle::kTextfieldDragVerticallyDragsToEnd ? " world" : " w");
1228 const int kStartX = GetCursorPositionX(5);
1229 const int kDownX = GetCursorPositionX(7);
1230 const int kUpX = GetCursorPositionX(3);
1231 gfx::Point start_point(kStartX, 0);
1232 gfx::Point down_point(kDownX, 500);
1233 gfx::Point up_point(kUpX, -500);
1234
1235 MoveMouseTo(start_point);
1236 PressLeftMouseButton();
1237 DragMouseTo(up_point);
1238 ReleaseLeftMouseButton();
1239 EXPECT_EQ(textfield_->GetSelectedText(), expected_up);
1240
1241 MoveMouseTo(start_point);
1242 PressLeftMouseButton();
1243 DragMouseTo(down_point);
1244 ReleaseLeftMouseButton();
1245 EXPECT_EQ(textfield_->GetSelectedText(), expected_down);
1246 }
1247
1198 #if defined(OS_WIN) 1248 #if defined(OS_WIN)
1199 TEST_F(TextfieldTest, DragAndDrop_AcceptDrop) { 1249 TEST_F(TextfieldTest, DragAndDrop_AcceptDrop) {
1200 InitTextfield(); 1250 InitTextfield();
1201 textfield_->SetText(ASCIIToUTF16("hello world")); 1251 textfield_->SetText(ASCIIToUTF16("hello world"));
1202 1252
1203 ui::OSExchangeData data; 1253 ui::OSExchangeData data;
1204 base::string16 string(ASCIIToUTF16("string ")); 1254 base::string16 string(ASCIIToUTF16("string "));
1205 data.SetString(string); 1255 data.SetString(string);
1206 int formats = 0; 1256 int formats = 0;
1207 std::set<ui::Clipboard::FormatType> format_types; 1257 std::set<ui::Clipboard::FormatType> format_types;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1270 // Ensure that textfields without selections do not support drag operations. 1320 // Ensure that textfields without selections do not support drag operations.
1271 textfield_->ClearSelection(); 1321 textfield_->ClearSelection();
1272 EXPECT_EQ(ui::DragDropTypes::DRAG_NONE, 1322 EXPECT_EQ(ui::DragDropTypes::DRAG_NONE,
1273 textfield_->GetDragOperationsForView(NULL, kStringPoint)); 1323 textfield_->GetDragOperationsForView(NULL, kStringPoint));
1274 textfield_->SelectRange(kStringRange); 1324 textfield_->SelectRange(kStringRange);
1275 // Ensure that password textfields do not support drag operations. 1325 // Ensure that password textfields do not support drag operations.
1276 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); 1326 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
1277 EXPECT_EQ(ui::DragDropTypes::DRAG_NONE, 1327 EXPECT_EQ(ui::DragDropTypes::DRAG_NONE,
1278 textfield_->GetDragOperationsForView(NULL, kStringPoint)); 1328 textfield_->GetDragOperationsForView(NULL, kStringPoint));
1279 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT); 1329 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT);
1330 MoveMouseTo(kStringPoint);
1331 PressLeftMouseButton();
1280 // Ensure that textfields only initiate drag operations inside the selection. 1332 // Ensure that textfields only initiate drag operations inside the selection.
1281 ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, kStringPoint, kStringPoint,
1282 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
1283 ui::EF_LEFT_MOUSE_BUTTON);
1284 textfield_->OnMousePressed(press_event);
1285 EXPECT_EQ(ui::DragDropTypes::DRAG_NONE, 1333 EXPECT_EQ(ui::DragDropTypes::DRAG_NONE,
1286 textfield_->GetDragOperationsForView(NULL, gfx::Point())); 1334 textfield_->GetDragOperationsForView(NULL, gfx::Point()));
1287 EXPECT_FALSE(textfield_->CanStartDragForView(NULL, gfx::Point(), 1335 EXPECT_FALSE(textfield_->CanStartDragForView(NULL, gfx::Point(),
1288 gfx::Point())); 1336 gfx::Point()));
1289 EXPECT_EQ(ui::DragDropTypes::DRAG_COPY, 1337 EXPECT_EQ(ui::DragDropTypes::DRAG_COPY,
1290 textfield_->GetDragOperationsForView(NULL, kStringPoint)); 1338 textfield_->GetDragOperationsForView(NULL, kStringPoint));
1291 EXPECT_TRUE(textfield_->CanStartDragForView(NULL, kStringPoint, 1339 EXPECT_TRUE(textfield_->CanStartDragForView(NULL, kStringPoint,
1292 gfx::Point())); 1340 gfx::Point()));
1293 // Ensure that textfields support local moves. 1341 // Ensure that textfields support local moves.
1294 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY, 1342 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY,
1295 textfield_->GetDragOperationsForView(textfield_, kStringPoint)); 1343 textfield_->GetDragOperationsForView(textfield_, kStringPoint));
1296 } 1344 }
1297 1345
1298 TEST_F(TextfieldTest, DragAndDrop_ToTheRight) { 1346 TEST_F(TextfieldTest, DragAndDrop_ToTheRight) {
1299 InitTextfield(); 1347 InitTextfield();
1300 textfield_->SetText(ASCIIToUTF16("hello world")); 1348 textfield_->SetText(ASCIIToUTF16("hello world"));
1301 1349
1302 base::string16 string; 1350 base::string16 string;
1303 ui::OSExchangeData data; 1351 ui::OSExchangeData data;
1304 int formats = 0; 1352 int formats = 0;
1305 int operations = 0; 1353 int operations = 0;
1306 std::set<ui::Clipboard::FormatType> format_types; 1354 std::set<ui::Clipboard::FormatType> format_types;
1307 1355
1308 // Start dragging "ello". 1356 // Start dragging "ello".
1309 textfield_->SelectRange(gfx::Range(1, 5)); 1357 textfield_->SelectRange(gfx::Range(1, 5));
1310 gfx::Point point(GetCursorPositionX(3), 0); 1358 gfx::Point point(GetCursorPositionX(3), 0);
1311 ui::MouseEvent click_a(ui::ET_MOUSE_PRESSED, point, point, 1359 MoveMouseTo(point);
1312 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 1360 PressLeftMouseButton();
1313 ui::EF_LEFT_MOUSE_BUTTON); 1361 EXPECT_TRUE(textfield_->CanStartDragForView(textfield_, point, point));
1314 textfield_->OnMousePressed(click_a); 1362 operations = textfield_->GetDragOperationsForView(textfield_, point);
1315 EXPECT_TRUE(textfield_->CanStartDragForView(textfield_, click_a.location(),
1316 gfx::Point()));
1317 operations = textfield_->GetDragOperationsForView(textfield_,
1318 click_a.location());
1319 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY, 1363 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY,
1320 operations); 1364 operations);
1321 textfield_->WriteDragDataForView(NULL, click_a.location(), &data); 1365 textfield_->WriteDragDataForView(nullptr, point, &data);
1322 EXPECT_TRUE(data.GetString(&string)); 1366 EXPECT_TRUE(data.GetString(&string));
1323 EXPECT_EQ(textfield_->GetSelectedText(), string); 1367 EXPECT_EQ(textfield_->GetSelectedText(), string);
1324 EXPECT_TRUE(textfield_->GetDropFormats(&formats, &format_types)); 1368 EXPECT_TRUE(textfield_->GetDropFormats(&formats, &format_types));
1325 EXPECT_EQ(ui::OSExchangeData::STRING, formats); 1369 EXPECT_EQ(ui::OSExchangeData::STRING, formats);
1326 EXPECT_TRUE(format_types.empty()); 1370 EXPECT_TRUE(format_types.empty());
1327 1371
1328 // Drop "ello" after "w". 1372 // Drop "ello" after "w".
1329 const gfx::Point kDropPoint(GetCursorPositionX(7), 0); 1373 const gfx::Point kDropPoint(GetCursorPositionX(7), 0);
1330 EXPECT_TRUE(textfield_->CanDrop(data)); 1374 EXPECT_TRUE(textfield_->CanDrop(data));
1331 ui::DropTargetEvent drop_a(data, kDropPoint, kDropPoint, operations); 1375 ui::DropTargetEvent drop_a(data, kDropPoint, kDropPoint, operations);
(...skipping 23 matching lines...) Expand all
1355 1399
1356 base::string16 string; 1400 base::string16 string;
1357 ui::OSExchangeData data; 1401 ui::OSExchangeData data;
1358 int formats = 0; 1402 int formats = 0;
1359 int operations = 0; 1403 int operations = 0;
1360 std::set<ui::Clipboard::FormatType> format_types; 1404 std::set<ui::Clipboard::FormatType> format_types;
1361 1405
1362 // Start dragging " worl". 1406 // Start dragging " worl".
1363 textfield_->SelectRange(gfx::Range(5, 10)); 1407 textfield_->SelectRange(gfx::Range(5, 10));
1364 gfx::Point point(GetCursorPositionX(7), 0); 1408 gfx::Point point(GetCursorPositionX(7), 0);
1365 ui::MouseEvent click_a(ui::ET_MOUSE_PRESSED, point, point, 1409 MoveMouseTo(point);
1366 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 1410 PressLeftMouseButton();
1367 ui::EF_LEFT_MOUSE_BUTTON); 1411 EXPECT_TRUE(textfield_->CanStartDragForView(textfield_, point, gfx::Point()));
1368 textfield_->OnMousePressed(click_a); 1412 operations = textfield_->GetDragOperationsForView(textfield_, point);
1369 EXPECT_TRUE(textfield_->CanStartDragForView(textfield_, click_a.location(),
1370 gfx::Point()));
1371 operations = textfield_->GetDragOperationsForView(textfield_,
1372 click_a.location());
1373 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY, 1413 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY,
1374 operations); 1414 operations);
1375 textfield_->WriteDragDataForView(NULL, click_a.location(), &data); 1415 textfield_->WriteDragDataForView(nullptr, point, &data);
1376 EXPECT_TRUE(data.GetString(&string)); 1416 EXPECT_TRUE(data.GetString(&string));
1377 EXPECT_EQ(textfield_->GetSelectedText(), string); 1417 EXPECT_EQ(textfield_->GetSelectedText(), string);
1378 EXPECT_TRUE(textfield_->GetDropFormats(&formats, &format_types)); 1418 EXPECT_TRUE(textfield_->GetDropFormats(&formats, &format_types));
1379 EXPECT_EQ(ui::OSExchangeData::STRING, formats); 1419 EXPECT_EQ(ui::OSExchangeData::STRING, formats);
1380 EXPECT_TRUE(format_types.empty()); 1420 EXPECT_TRUE(format_types.empty());
1381 1421
1382 // Drop " worl" after "h". 1422 // Drop " worl" after "h".
1383 EXPECT_TRUE(textfield_->CanDrop(data)); 1423 EXPECT_TRUE(textfield_->CanDrop(data));
1384 gfx::Point drop_point(GetCursorPositionX(1), 0); 1424 gfx::Point drop_point(GetCursorPositionX(1), 0);
1385 ui::DropTargetEvent drop_a(data, drop_point, drop_point, operations); 1425 ui::DropTargetEvent drop_a(data, drop_point, drop_point, operations);
(...skipping 17 matching lines...) Expand all
1403 EXPECT_STR_EQ("h worlellod", textfield_->text()); 1443 EXPECT_STR_EQ("h worlellod", textfield_->text());
1404 } 1444 }
1405 1445
1406 TEST_F(TextfieldTest, DragAndDrop_Canceled) { 1446 TEST_F(TextfieldTest, DragAndDrop_Canceled) {
1407 InitTextfield(); 1447 InitTextfield();
1408 textfield_->SetText(ASCIIToUTF16("hello world")); 1448 textfield_->SetText(ASCIIToUTF16("hello world"));
1409 1449
1410 // Start dragging "worl". 1450 // Start dragging "worl".
1411 textfield_->SelectRange(gfx::Range(6, 10)); 1451 textfield_->SelectRange(gfx::Range(6, 10));
1412 gfx::Point point(GetCursorPositionX(8), 0); 1452 gfx::Point point(GetCursorPositionX(8), 0);
1413 ui::MouseEvent click(ui::ET_MOUSE_PRESSED, point, point, 1453 MoveMouseTo(point);
1414 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 1454 PressLeftMouseButton();
1415 ui::EF_LEFT_MOUSE_BUTTON);
1416 textfield_->OnMousePressed(click);
1417 ui::OSExchangeData data; 1455 ui::OSExchangeData data;
1418 textfield_->WriteDragDataForView(NULL, click.location(), &data); 1456 textfield_->WriteDragDataForView(nullptr, point, &data);
1419 EXPECT_TRUE(textfield_->CanDrop(data)); 1457 EXPECT_TRUE(textfield_->CanDrop(data));
1420 // Drag the text over somewhere valid, outside the current selection. 1458 // Drag the text over somewhere valid, outside the current selection.
1421 gfx::Point drop_point(GetCursorPositionX(2), 0); 1459 gfx::Point drop_point(GetCursorPositionX(2), 0);
1422 ui::DropTargetEvent drop(data, drop_point, drop_point, 1460 ui::DropTargetEvent drop(data, drop_point, drop_point,
1423 ui::DragDropTypes::DRAG_MOVE); 1461 ui::DragDropTypes::DRAG_MOVE);
1424 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE, textfield_->OnDragUpdated(drop)); 1462 EXPECT_EQ(ui::DragDropTypes::DRAG_MOVE, textfield_->OnDragUpdated(drop));
1425 // "Cancel" the drag, via move and release over the selection, and OnDragDone. 1463 // "Cancel" the drag, via move and release over the selection, and OnDragDone.
1426 gfx::Point drag_point(GetCursorPositionX(9), 0); 1464 gfx::Point drag_point(GetCursorPositionX(9), 0);
1427 ui::MouseEvent drag(ui::ET_MOUSE_DRAGGED, drag_point, drag_point, 1465 DragMouseTo(drag_point);
1428 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0); 1466 ReleaseLeftMouseButton();
1429 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, drag_point, drag_point,
1430 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
1431 ui::EF_LEFT_MOUSE_BUTTON);
1432 textfield_->OnMouseDragged(drag);
1433 textfield_->OnMouseReleased(release);
1434 textfield_->OnDragDone();
1435 EXPECT_EQ(ASCIIToUTF16("hello world"), textfield_->text()); 1467 EXPECT_EQ(ASCIIToUTF16("hello world"), textfield_->text());
1436 } 1468 }
1437 1469
1438 TEST_F(TextfieldTest, ReadOnlyTest) { 1470 TEST_F(TextfieldTest, ReadOnlyTest) {
1439 InitTextfield(); 1471 InitTextfield();
1440 textfield_->SetText(ASCIIToUTF16("read only")); 1472 textfield_->SetText(ASCIIToUTF16("read only"));
1441 textfield_->SetReadOnly(true); 1473 textfield_->SetReadOnly(true);
1442 EXPECT_TRUE(textfield_->enabled()); 1474 EXPECT_TRUE(textfield_->enabled());
1443 EXPECT_TRUE(textfield_->IsFocusable()); 1475 EXPECT_TRUE(textfield_->IsFocusable());
1444 1476
(...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after
2198 2230
2199 textfield_->SelectRange(gfx::Range(5, 5)); 2231 textfield_->SelectRange(gfx::Range(5, 5));
2200 const gfx::Rect middle_cursor = GetCursorBounds(); 2232 const gfx::Rect middle_cursor = GetCursorBounds();
2201 textfield_->SelectRange(gfx::Range(0, 0)); 2233 textfield_->SelectRange(gfx::Range(0, 0));
2202 const gfx::Point beginning = GetCursorBounds().origin(); 2234 const gfx::Point beginning = GetCursorBounds().origin();
2203 2235
2204 // Double click, but do not release the left button. 2236 // Double click, but do not release the left button.
2205 MouseClick(middle_cursor, 0); 2237 MouseClick(middle_cursor, 0);
2206 const gfx::Point middle(middle_cursor.x(), 2238 const gfx::Point middle(middle_cursor.x(),
2207 middle_cursor.y() + middle_cursor.height() / 2); 2239 middle_cursor.y() + middle_cursor.height() / 2);
2208 ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, middle, middle, 2240 MoveMouseTo(middle);
2209 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 2241 PressLeftMouseButton();
2210 ui::EF_LEFT_MOUSE_BUTTON);
2211 textfield_->OnMousePressed(press_event);
2212 EXPECT_EQ(gfx::Range(4, 7), textfield_->GetSelectedRange()); 2242 EXPECT_EQ(gfx::Range(4, 7), textfield_->GetSelectedRange());
2213 2243
2214 // Drag the mouse to the beginning of the textfield. 2244 // Drag the mouse to the beginning of the textfield.
2215 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED, beginning, beginning, 2245 DragMouseTo(beginning);
2216 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
2217 textfield_->OnMouseDragged(drag_event);
2218 EXPECT_EQ(gfx::Range(7, 0), textfield_->GetSelectedRange()); 2246 EXPECT_EQ(gfx::Range(7, 0), textfield_->GetSelectedRange());
2219 } 2247 }
2220 2248
2221 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 2249 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
2222 // flaky: http://crbug.com/396477 2250 // flaky: http://crbug.com/396477
2223 TEST_F(TextfieldTest, DISABLED_SelectionClipboard) { 2251 TEST_F(TextfieldTest, DISABLED_SelectionClipboard) {
2224 InitTextfield(); 2252 InitTextfield();
2225 textfield_->SetText(ASCIIToUTF16("0123")); 2253 textfield_->SetText(ASCIIToUTF16("0123"));
2226 gfx::Point point_1(GetCursorPositionX(1), 0); 2254 gfx::Point point_1(GetCursorPositionX(1), 0);
2227 gfx::Point point_2(GetCursorPositionX(2), 0); 2255 gfx::Point point_2(GetCursorPositionX(2), 0);
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
2569 2597
2570 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); 2598 textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
2571 ui::AXViewState state_protected; 2599 ui::AXViewState state_protected;
2572 textfield_->GetAccessibleState(&state_protected); 2600 textfield_->GetAccessibleState(&state_protected);
2573 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role); 2601 EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, state_protected.role);
2574 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value); 2602 EXPECT_EQ(ASCIIToUTF16("********"), state_protected.value);
2575 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED)); 2603 EXPECT_TRUE(state_protected.HasStateFlag(ui::AX_STATE_PROTECTED));
2576 } 2604 }
2577 2605
2578 } // namespace views 2606 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/textfield/textfield.cc ('k') | ui/views/style/platform_style.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698