OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/native_textfield_views.h" | 5 #include "ui/views/controls/textfield/native_textfield_views.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 92 |
93 private: | 93 private: |
94 string16 text_; | 94 string16 text_; |
95 | 95 |
96 DISALLOW_COPY_AND_ASSIGN(GetTextHelper); | 96 DISALLOW_COPY_AND_ASSIGN(GetTextHelper); |
97 }; | 97 }; |
98 | 98 |
99 // Convenience to make constructing a GestureEvent simpler. | 99 // Convenience to make constructing a GestureEvent simpler. |
100 class GestureEventForTest : public ui::GestureEvent { | 100 class GestureEventForTest : public ui::GestureEvent { |
101 public: | 101 public: |
102 GestureEventForTest(ui::EventType type, int x, int y, int flags) | 102 GestureEventForTest(ui::EventType type, int x, int y, float delta_x, |
103 : GestureEvent(type, x, y, flags, base::TimeDelta(), | 103 float delta_y) |
104 ui::GestureEventDetails(type, 0.0f, 0.0f), 0) { | 104 : GestureEvent(type, x, y, 0, base::TimeDelta(), |
| 105 ui::GestureEventDetails(type, delta_x, delta_y), 0) { |
105 } | 106 } |
106 | 107 |
107 private: | 108 private: |
108 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest); | 109 DISALLOW_COPY_AND_ASSIGN(GestureEventForTest); |
109 }; | 110 }; |
110 | 111 |
111 } // namespace | 112 } // namespace |
112 | 113 |
113 namespace views { | 114 namespace views { |
114 | 115 |
(...skipping 1714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1829 #if defined(OS_CHROMEOS) | 1830 #if defined(OS_CHROMEOS) |
1830 TEST_F(NativeTextfieldViewsTest, TouchSelectionAndDraggingTest) { | 1831 TEST_F(NativeTextfieldViewsTest, TouchSelectionAndDraggingTest) { |
1831 InitTextfield(Textfield::STYLE_DEFAULT); | 1832 InitTextfield(Textfield::STYLE_DEFAULT); |
1832 textfield_->SetText(ASCIIToUTF16("hello world")); | 1833 textfield_->SetText(ASCIIToUTF16("hello world")); |
1833 EXPECT_FALSE(GetTouchSelectionController()); | 1834 EXPECT_FALSE(GetTouchSelectionController()); |
1834 const int eventX = GetCursorPositionX(2); | 1835 const int eventX = GetCursorPositionX(2); |
1835 const int eventY = 0; | 1836 const int eventY = 0; |
1836 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableTouchEditing); | 1837 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableTouchEditing); |
1837 | 1838 |
1838 // Tapping on the textfield should turn on the TouchSelectionController. | 1839 // Tapping on the textfield should turn on the TouchSelectionController. |
1839 ui::GestureEvent tap(ui::ET_GESTURE_TAP, eventX, eventY, 0, base::TimeDelta(), | 1840 GestureEventForTest tap(ui::ET_GESTURE_TAP, eventX, eventY, 1.0f, 0.0f); |
1840 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), | |
1841 0); | |
1842 textfield_view_->OnGestureEvent(&tap); | 1841 textfield_view_->OnGestureEvent(&tap); |
1843 EXPECT_TRUE(GetTouchSelectionController()); | 1842 EXPECT_TRUE(GetTouchSelectionController()); |
1844 | 1843 |
1845 // Un-focusing the textfield should reset the TouchSelectionController | 1844 // Un-focusing the textfield should reset the TouchSelectionController |
1846 textfield_view_->GetFocusManager()->ClearFocus(); | 1845 textfield_view_->GetFocusManager()->ClearFocus(); |
1847 EXPECT_FALSE(GetTouchSelectionController()); | 1846 EXPECT_FALSE(GetTouchSelectionController()); |
1848 | 1847 |
1849 // With touch editing enabled, long press should not show context menu. | 1848 // With touch editing enabled, long press should not show context menu. |
1850 // Instead, select word and invoke TouchSelectionController. | 1849 // Instead, select word and invoke TouchSelectionController. |
1851 GestureEventForTest tap_down(ui::ET_GESTURE_TAP_DOWN, eventX, eventY, 0); | 1850 GestureEventForTest tap_down(ui::ET_GESTURE_TAP_DOWN, eventX, eventY, 0.0f, |
| 1851 0.0f); |
1852 textfield_view_->OnGestureEvent(&tap_down); | 1852 textfield_view_->OnGestureEvent(&tap_down); |
1853 GestureEventForTest long_press(ui::ET_GESTURE_LONG_PRESS, eventX, eventY, 0); | 1853 GestureEventForTest long_press(ui::ET_GESTURE_LONG_PRESS, eventX, eventY, |
| 1854 0.0f, 0.0f); |
1854 textfield_view_->OnGestureEvent(&long_press); | 1855 textfield_view_->OnGestureEvent(&long_press); |
1855 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); | 1856 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); |
1856 EXPECT_TRUE(GetTouchSelectionController()); | 1857 EXPECT_TRUE(GetTouchSelectionController()); |
1857 | 1858 |
1858 // Long pressing again in the selecting region should not do anything since | 1859 // Long pressing again in the selecting region should not do anything since |
1859 // touch drag drop is not yet enabled. | 1860 // touch drag drop is not yet enabled. |
1860 textfield_view_->OnGestureEvent(&tap_down); | 1861 textfield_view_->OnGestureEvent(&tap_down); |
1861 textfield_view_->OnGestureEvent(&long_press); | 1862 textfield_view_->OnGestureEvent(&long_press); |
1862 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); | 1863 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); |
1863 EXPECT_TRUE(GetTouchSelectionController()); | 1864 EXPECT_TRUE(GetTouchSelectionController()); |
1864 EXPECT_TRUE(long_press.handled()); | 1865 EXPECT_TRUE(long_press.handled()); |
1865 | 1866 |
1866 // After enabling touch drag drop, long pressing in the selected region should | 1867 // After enabling touch drag drop, long pressing in the selected region should |
1867 // start a drag and remove TouchSelectionController. | 1868 // start a drag and remove TouchSelectionController. |
1868 CommandLine::ForCurrentProcess()->AppendSwitch( | 1869 CommandLine::ForCurrentProcess()->AppendSwitch( |
1869 switches::kEnableTouchDragDrop); | 1870 switches::kEnableTouchDragDrop); |
1870 textfield_view_->OnGestureEvent(&tap_down); | 1871 textfield_view_->OnGestureEvent(&tap_down); |
1871 | 1872 |
1872 // Create a new long press event since the previous one is not marked handled. | 1873 // Create a new long press event since the previous one is not marked handled. |
1873 GestureEventForTest long_press2(ui::ET_GESTURE_LONG_PRESS, eventX, eventY, 0); | 1874 GestureEventForTest long_press2(ui::ET_GESTURE_LONG_PRESS, eventX, eventY, |
| 1875 0.0f, 0.0f); |
1874 textfield_view_->OnGestureEvent(&long_press2); | 1876 textfield_view_->OnGestureEvent(&long_press2); |
1875 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); | 1877 EXPECT_STR_EQ("hello", textfield_->GetSelectedText()); |
1876 EXPECT_FALSE(GetTouchSelectionController()); | 1878 EXPECT_FALSE(GetTouchSelectionController()); |
1877 } | 1879 } |
| 1880 |
| 1881 TEST_F(NativeTextfieldViewsTest, TouchScrubbingSelection) { |
| 1882 InitTextfield(Textfield::STYLE_DEFAULT); |
| 1883 textfield_->SetText(ASCIIToUTF16("hello world")); |
| 1884 EXPECT_FALSE(GetTouchSelectionController()); |
| 1885 |
| 1886 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableTouchEditing); |
| 1887 |
| 1888 // Simulate touch-scrubbing. |
| 1889 int scrubbing_start = GetCursorPositionX(1); |
| 1890 int scrubbing_end = GetCursorPositionX(6); |
| 1891 |
| 1892 GestureEventForTest tap_down(ui::ET_GESTURE_TAP_DOWN, scrubbing_start, 0, |
| 1893 0.0f, 0.0f); |
| 1894 textfield_view_->OnGestureEvent(&tap_down); |
| 1895 |
| 1896 GestureEventForTest tap_cancel(ui::ET_GESTURE_TAP_CANCEL, scrubbing_start, 0, |
| 1897 0.0f, 0.0f); |
| 1898 textfield_view_->OnGestureEvent(&tap_cancel); |
| 1899 |
| 1900 GestureEventForTest scroll_begin(ui::ET_GESTURE_SCROLL_BEGIN, scrubbing_start, |
| 1901 0, 0.0f, 0.0f); |
| 1902 textfield_view_->OnGestureEvent(&scroll_begin); |
| 1903 |
| 1904 GestureEventForTest scroll_update(ui::ET_GESTURE_SCROLL_UPDATE, scrubbing_end, |
| 1905 0, scrubbing_end - scrubbing_start, 0.0f); |
| 1906 textfield_view_->OnGestureEvent(&scroll_update); |
| 1907 |
| 1908 GestureEventForTest scroll_end(ui::ET_GESTURE_SCROLL_END, scrubbing_end, 0, |
| 1909 0.0f, 0.0f); |
| 1910 textfield_view_->OnGestureEvent(&scroll_end); |
| 1911 |
| 1912 GestureEventForTest end(ui::ET_GESTURE_END, scrubbing_end, 0, 0.0f, 0.0f); |
| 1913 textfield_view_->OnGestureEvent(&end); |
| 1914 |
| 1915 // In the end, part of text should have been selected and handles should have |
| 1916 // appeared. |
| 1917 EXPECT_STR_EQ("ello ", textfield_->GetSelectedText()); |
| 1918 EXPECT_TRUE(GetTouchSelectionController()); |
| 1919 } |
1878 #endif | 1920 #endif |
1879 | 1921 |
1880 // Long_Press gesture in NativeTextfieldViews can initiate a drag and drop now. | 1922 // Long_Press gesture in NativeTextfieldViews can initiate a drag and drop now. |
1881 TEST_F(NativeTextfieldViewsTest, TestLongPressInitiatesDragDrop) { | 1923 TEST_F(NativeTextfieldViewsTest, TestLongPressInitiatesDragDrop) { |
1882 InitTextfield(Textfield::STYLE_DEFAULT); | 1924 InitTextfield(Textfield::STYLE_DEFAULT); |
1883 textfield_->SetText(ASCIIToUTF16("Hello string world")); | 1925 textfield_->SetText(ASCIIToUTF16("Hello string world")); |
1884 | 1926 |
1885 // Ensure the textfield will provide selected text for drag data. | 1927 // Ensure the textfield will provide selected text for drag data. |
1886 textfield_->SelectRange(gfx::Range(6, 12)); | 1928 textfield_->SelectRange(gfx::Range(6, 12)); |
1887 const gfx::Point kStringPoint(GetCursorPositionX(9), 0); | 1929 const gfx::Point kStringPoint(GetCursorPositionX(9), 0); |
1888 | 1930 |
1889 // Enable touch-drag-drop to make long press effective. | 1931 // Enable touch-drag-drop to make long press effective. |
1890 CommandLine::ForCurrentProcess()->AppendSwitch( | 1932 CommandLine::ForCurrentProcess()->AppendSwitch( |
1891 switches::kEnableTouchDragDrop); | 1933 switches::kEnableTouchDragDrop); |
1892 | 1934 |
1893 // Create a long press event in the selected region should start a drag. | 1935 // Create a long press event in the selected region should start a drag. |
1894 GestureEventForTest long_press(ui::ET_GESTURE_LONG_PRESS, | 1936 GestureEventForTest long_press(ui::ET_GESTURE_LONG_PRESS, kStringPoint.x(), |
1895 kStringPoint.x(), kStringPoint.y(), 0); | 1937 kStringPoint.y(), 0.0f, 0.0f); |
1896 textfield_view_->OnGestureEvent(&long_press); | 1938 textfield_view_->OnGestureEvent(&long_press); |
1897 EXPECT_TRUE(textfield_view_->CanStartDragForView(NULL, | 1939 EXPECT_TRUE(textfield_view_->CanStartDragForView(NULL, |
1898 kStringPoint, kStringPoint)); | 1940 kStringPoint, kStringPoint)); |
1899 } | 1941 } |
1900 | 1942 |
1901 TEST_F(NativeTextfieldViewsTest, GetTextfieldBaseline_FontFallbackTest) { | 1943 TEST_F(NativeTextfieldViewsTest, GetTextfieldBaseline_FontFallbackTest) { |
1902 InitTextfield(Textfield::STYLE_DEFAULT); | 1944 InitTextfield(Textfield::STYLE_DEFAULT); |
1903 textfield_->SetText(UTF8ToUTF16("abc")); | 1945 textfield_->SetText(UTF8ToUTF16("abc")); |
1904 const int old_baseline = textfield_->GetBaseline(); | 1946 const int old_baseline = textfield_->GetBaseline(); |
1905 | 1947 |
1906 // Set text which may fall back to a font which has taller baseline than | 1948 // Set text which may fall back to a font which has taller baseline than |
1907 // the default font. | 1949 // the default font. |
1908 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); | 1950 textfield_->SetText(UTF8ToUTF16("\xE0\xB9\x91")); |
1909 const int new_baseline = textfield_->GetBaseline(); | 1951 const int new_baseline = textfield_->GetBaseline(); |
1910 | 1952 |
1911 // Regardless of the text, the baseline must be the same. | 1953 // Regardless of the text, the baseline must be the same. |
1912 EXPECT_EQ(new_baseline, old_baseline); | 1954 EXPECT_EQ(new_baseline, old_baseline); |
1913 } | 1955 } |
1914 | 1956 |
1915 } // namespace views | 1957 } // namespace views |
OLD | NEW |