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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 bool key_received_; | 96 bool key_received_; |
97 | 97 |
98 base::WeakPtrFactory<TestTextfield> weak_ptr_factory_; | 98 base::WeakPtrFactory<TestTextfield> weak_ptr_factory_; |
99 | 99 |
100 DISALLOW_COPY_AND_ASSIGN(TestTextfield); | 100 DISALLOW_COPY_AND_ASSIGN(TestTextfield); |
101 }; | 101 }; |
102 | 102 |
103 // Convenience to make constructing a GestureEvent simpler. | 103 // Convenience to make constructing a GestureEvent simpler. |
104 class GestureEventForTest : public ui::GestureEvent { | 104 class GestureEventForTest : public ui::GestureEvent { |
105 public: | 105 public: |
106 GestureEventForTest(ui::EventType type, | 106 GestureEventForTest(int x, int y, ui::GestureEventDetails details) |
107 int x, | 107 : GestureEvent(x, y, 0, base::TimeDelta(), details) {} |
108 int y, | |
109 float delta_x, | |
110 float delta_y) | |
111 : GestureEvent(x, | |
112 y, | |
113 0, | |
114 base::TimeDelta(), | |
115 ui::GestureEventDetails(type, delta_x, delta_y)) {} | |
116 | 108 |
117 private: | 109 private: |
118 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest); | 110 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest); |
119 }; | 111 }; |
120 | 112 |
121 // This controller will happily destroy the target textfield passed on | 113 // This controller will happily destroy the target textfield passed on |
122 // construction when a key event is triggered. | 114 // construction when a key event is triggered. |
123 class TextfieldDestroyerController : public views::TextfieldController { | 115 class TextfieldDestroyerController : public views::TextfieldController { |
124 public: | 116 public: |
125 explicit TextfieldDestroyerController(views::Textfield* target) | 117 explicit TextfieldDestroyerController(views::Textfield* target) |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 textfield_->OnMousePressed(click); | 315 textfield_->OnMousePressed(click); |
324 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), | 316 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), |
325 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_NON_CLIENT, | 317 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_NON_CLIENT, |
326 ui::EF_LEFT_MOUSE_BUTTON); | 318 ui::EF_LEFT_MOUSE_BUTTON); |
327 textfield_->OnMouseReleased(release); | 319 textfield_->OnMouseReleased(release); |
328 } | 320 } |
329 | 321 |
330 // Simulates a complete tap. | 322 // Simulates a complete tap. |
331 void Tap(const gfx::Point& point) { | 323 void Tap(const gfx::Point& point) { |
332 GestureEventForTest begin( | 324 GestureEventForTest begin( |
333 ui::ET_GESTURE_BEGIN, point.x(), point.y(), 0.0f, 0.0f); | 325 point.x(), point.y(), ui::GestureEventDetails(ui::ET_GESTURE_BEGIN)); |
334 textfield_->OnGestureEvent(&begin); | 326 textfield_->OnGestureEvent(&begin); |
335 | 327 |
336 GestureEventForTest tap_down( | 328 GestureEventForTest tap_down( |
337 ui::ET_GESTURE_TAP_DOWN, point.x(), point.y(), 0.0f, 0.0f); | 329 point.x(), point.y(), ui::GestureEventDetails(ui::ET_GESTURE_TAP_DOWN)); |
338 textfield_->OnGestureEvent(&tap_down); | 330 textfield_->OnGestureEvent(&tap_down); |
339 | 331 |
340 GestureEventForTest show_press( | 332 GestureEventForTest show_press( |
341 ui::ET_GESTURE_SHOW_PRESS, point.x(), point.y(), 0.0f, 0.0f); | 333 point.x(), |
| 334 point.y(), |
| 335 ui::GestureEventDetails(ui::ET_GESTURE_SHOW_PRESS)); |
342 textfield_->OnGestureEvent(&show_press); | 336 textfield_->OnGestureEvent(&show_press); |
343 | 337 |
344 GestureEventForTest tap( | 338 ui::GestureEventDetails tap_details(ui::ET_GESTURE_TAP); |
345 ui::ET_GESTURE_TAP, point.x(), point.y(), 1.0f, 0.0f); | 339 tap_details.set_tap_count(1); |
| 340 GestureEventForTest tap(point.x(), point.y(), tap_details); |
346 textfield_->OnGestureEvent(&tap); | 341 textfield_->OnGestureEvent(&tap); |
347 | 342 |
348 GestureEventForTest end( | 343 GestureEventForTest end( |
349 ui::ET_GESTURE_END, point.x(), point.y(), 0.0f, 0.0f); | 344 point.x(), point.y(), ui::GestureEventDetails(ui::ET_GESTURE_END)); |
350 textfield_->OnGestureEvent(&end); | 345 textfield_->OnGestureEvent(&end); |
351 } | 346 } |
352 | 347 |
353 void VerifyTextfieldContextMenuContents(bool textfield_has_selection, | 348 void VerifyTextfieldContextMenuContents(bool textfield_has_selection, |
354 bool can_undo, | 349 bool can_undo, |
355 ui::MenuModel* menu) { | 350 ui::MenuModel* menu) { |
356 EXPECT_EQ(can_undo, menu->IsEnabledAt(0 /* UNDO */)); | 351 EXPECT_EQ(can_undo, menu->IsEnabledAt(0 /* UNDO */)); |
357 EXPECT_TRUE(menu->IsEnabledAt(1 /* Separator */)); | 352 EXPECT_TRUE(menu->IsEnabledAt(1 /* Separator */)); |
358 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(2 /* CUT */)); | 353 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(2 /* CUT */)); |
359 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(3 /* COPY */)); | 354 EXPECT_EQ(textfield_has_selection, menu->IsEnabledAt(3 /* COPY */)); |
(...skipping 1590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1950 // Touch selection and dragging currently only works for chromeos. | 1945 // Touch selection and dragging currently only works for chromeos. |
1951 #if defined(OS_CHROMEOS) | 1946 #if defined(OS_CHROMEOS) |
1952 TEST_F(TextfieldTest, TouchSelectionAndDraggingTest) { | 1947 TEST_F(TextfieldTest, TouchSelectionAndDraggingTest) { |
1953 InitTextfield(); | 1948 InitTextfield(); |
1954 textfield_->SetText(ASCIIToUTF16("hello world")); | 1949 textfield_->SetText(ASCIIToUTF16("hello world")); |
1955 EXPECT_FALSE(test_api_->touch_selection_controller()); | 1950 EXPECT_FALSE(test_api_->touch_selection_controller()); |
1956 const int x = GetCursorPositionX(2); | 1951 const int x = GetCursorPositionX(2); |
1957 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableTouchEditing); | 1952 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableTouchEditing); |
1958 | 1953 |
1959 // Tapping on the textfield should turn on the TouchSelectionController. | 1954 // Tapping on the textfield should turn on the TouchSelectionController. |
1960 GestureEventForTest tap(ui::ET_GESTURE_TAP, x, 0, 1.0f, 0.0f); | 1955 ui::GestureEventDetails tap_details(ui::ET_GESTURE_TAP); |
| 1956 tap_details.set_tap_count(1); |
| 1957 GestureEventForTest tap(x, 0, tap_details); |
1961 textfield_->OnGestureEvent(&tap); | 1958 textfield_->OnGestureEvent(&tap); |
1962 EXPECT_TRUE(test_api_->touch_selection_controller()); | 1959 EXPECT_TRUE(test_api_->touch_selection_controller()); |
1963 | 1960 |
1964 // Un-focusing the textfield should reset the TouchSelectionController | 1961 // Un-focusing the textfield should reset the TouchSelectionController |
1965 textfield_->GetFocusManager()->ClearFocus(); | 1962 textfield_->GetFocusManager()->ClearFocus(); |
1966 EXPECT_FALSE(test_api_->touch_selection_controller()); | 1963 EXPECT_FALSE(test_api_->touch_selection_controller()); |
1967 textfield_->RequestFocus(); | 1964 textfield_->RequestFocus(); |
1968 | 1965 |
1969 // With touch editing enabled, long press should not show context menu. | 1966 // With touch editing enabled, long press should not show context menu. |
1970 // Instead, select word and invoke TouchSelectionController. | 1967 // Instead, select word and invoke TouchSelectionController. |
1971 GestureEventForTest long_press_1(ui::ET_GESTURE_LONG_PRESS, x, 0, 0.0f, 0.0f); | 1968 GestureEventForTest long_press_1( |
| 1969 x, 0, ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS)); |
1972 textfield_->OnGestureEvent(&long_press_1); | 1970 textfield_->OnGestureEvent(&long_press_1); |
1973 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); | 1971 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); |
1974 EXPECT_TRUE(test_api_->touch_selection_controller()); | 1972 EXPECT_TRUE(test_api_->touch_selection_controller()); |
1975 EXPECT_TRUE(long_press_1.handled()); | 1973 EXPECT_TRUE(long_press_1.handled()); |
1976 | 1974 |
1977 // With touch drag drop enabled, long pressing in the selected region should | 1975 // With touch drag drop enabled, long pressing in the selected region should |
1978 // start a drag and remove TouchSelectionController. | 1976 // start a drag and remove TouchSelectionController. |
1979 ASSERT_TRUE(switches::IsTouchDragDropEnabled()); | 1977 ASSERT_TRUE(switches::IsTouchDragDropEnabled()); |
1980 GestureEventForTest long_press_2(ui::ET_GESTURE_LONG_PRESS, x, 0, 0.0f, 0.0f); | 1978 GestureEventForTest long_press_2( |
| 1979 x, 0, ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS)); |
1981 textfield_->OnGestureEvent(&long_press_2); | 1980 textfield_->OnGestureEvent(&long_press_2); |
1982 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); | 1981 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); |
1983 EXPECT_FALSE(test_api_->touch_selection_controller()); | 1982 EXPECT_FALSE(test_api_->touch_selection_controller()); |
1984 EXPECT_FALSE(long_press_2.handled()); | 1983 EXPECT_FALSE(long_press_2.handled()); |
1985 | 1984 |
1986 // After disabling touch drag drop, long pressing again in the selection | 1985 // After disabling touch drag drop, long pressing again in the selection |
1987 // region should not do anything. | 1986 // region should not do anything. |
1988 CommandLine::ForCurrentProcess()->AppendSwitch( | 1987 CommandLine::ForCurrentProcess()->AppendSwitch( |
1989 switches::kDisableTouchDragDrop); | 1988 switches::kDisableTouchDragDrop); |
1990 ASSERT_FALSE(switches::IsTouchDragDropEnabled()); | 1989 ASSERT_FALSE(switches::IsTouchDragDropEnabled()); |
1991 GestureEventForTest long_press_3(ui::ET_GESTURE_LONG_PRESS, x, 0, 0.0f, 0.0f); | 1990 GestureEventForTest long_press_3( |
| 1991 x, 0, ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS)); |
1992 textfield_->OnGestureEvent(&long_press_3); | 1992 textfield_->OnGestureEvent(&long_press_3); |
1993 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); | 1993 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); |
1994 EXPECT_FALSE(test_api_->touch_selection_controller()); | 1994 EXPECT_FALSE(test_api_->touch_selection_controller()); |
1995 EXPECT_FALSE(long_press_3.handled()); | 1995 EXPECT_FALSE(long_press_3.handled()); |
1996 } | 1996 } |
1997 #endif | 1997 #endif |
1998 | 1998 |
1999 TEST_F(TextfieldTest, TouchSelectionInUnfocusableTextfield) { | 1999 TEST_F(TextfieldTest, TouchSelectionInUnfocusableTextfield) { |
2000 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableTouchEditing); | 2000 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableTouchEditing); |
2001 | 2001 |
(...skipping 24 matching lines...) Expand all Loading... |
2026 | 2026 |
2027 // Ensure the textfield will provide selected text for drag data. | 2027 // Ensure the textfield will provide selected text for drag data. |
2028 textfield_->SelectRange(gfx::Range(6, 12)); | 2028 textfield_->SelectRange(gfx::Range(6, 12)); |
2029 const gfx::Point kStringPoint(GetCursorPositionX(9), 0); | 2029 const gfx::Point kStringPoint(GetCursorPositionX(9), 0); |
2030 | 2030 |
2031 // Enable touch-drag-drop to make long press effective. | 2031 // Enable touch-drag-drop to make long press effective. |
2032 CommandLine::ForCurrentProcess()->AppendSwitch( | 2032 CommandLine::ForCurrentProcess()->AppendSwitch( |
2033 switches::kEnableTouchDragDrop); | 2033 switches::kEnableTouchDragDrop); |
2034 | 2034 |
2035 // Create a long press event in the selected region should start a drag. | 2035 // Create a long press event in the selected region should start a drag. |
2036 GestureEventForTest long_press(ui::ET_GESTURE_LONG_PRESS, kStringPoint.x(), | 2036 GestureEventForTest long_press( |
2037 kStringPoint.y(), 0.0f, 0.0f); | 2037 kStringPoint.x(), |
| 2038 kStringPoint.y(), |
| 2039 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS)); |
2038 textfield_->OnGestureEvent(&long_press); | 2040 textfield_->OnGestureEvent(&long_press); |
2039 EXPECT_TRUE(textfield_->CanStartDragForView(NULL, kStringPoint, | 2041 EXPECT_TRUE(textfield_->CanStartDragForView(NULL, kStringPoint, |
2040 kStringPoint)); | 2042 kStringPoint)); |
2041 } | 2043 } |
2042 | 2044 |
2043 TEST_F(TextfieldTest, GetTextfieldBaseline_FontFallbackTest) { | 2045 TEST_F(TextfieldTest, GetTextfieldBaseline_FontFallbackTest) { |
2044 InitTextfield(); | 2046 InitTextfield(); |
2045 textfield_->SetText(UTF8ToUTF16("abc")); | 2047 textfield_->SetText(UTF8ToUTF16("abc")); |
2046 const int old_baseline = textfield_->GetBaseline(); | 2048 const int old_baseline = textfield_->GetBaseline(); |
2047 | 2049 |
(...skipping 15 matching lines...) Expand all Loading... |
2063 TextfieldDestroyerController controller(textfield_); | 2065 TextfieldDestroyerController controller(textfield_); |
2064 EXPECT_TRUE(controller.target()); | 2066 EXPECT_TRUE(controller.target()); |
2065 | 2067 |
2066 // Send a key to trigger OnKeyEvent(). | 2068 // Send a key to trigger OnKeyEvent(). |
2067 SendKeyEvent('X'); | 2069 SendKeyEvent('X'); |
2068 | 2070 |
2069 EXPECT_FALSE(controller.target()); | 2071 EXPECT_FALSE(controller.target()); |
2070 } | 2072 } |
2071 | 2073 |
2072 } // namespace views | 2074 } // namespace views |
OLD | NEW |