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