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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "grit/ui_resources.h" | 7 #include "grit/ui_resources.h" |
8 #include "ui/base/resource/resource_bundle.h" | 8 #include "ui/base/resource/resource_bundle.h" |
9 #include "ui/base/touch/touch_editing_controller.h" | 9 #include "ui/base/touch/touch_editing_controller.h" |
10 #include "ui/base/ui_base_switches.h" | 10 #include "ui/base/ui_base_switches.h" |
| 11 #include "ui/gfx/canvas.h" |
11 #include "ui/gfx/point.h" | 12 #include "ui/gfx/point.h" |
12 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
13 #include "ui/gfx/render_text.h" | 14 #include "ui/gfx/render_text.h" |
14 #include "ui/views/controls/textfield/native_textfield_views.h" | |
15 #include "ui/views/controls/textfield/textfield.h" | 15 #include "ui/views/controls/textfield/textfield.h" |
16 #include "ui/views/test/views_test_base.h" | 16 #include "ui/views/test/views_test_base.h" |
17 #include "ui/views/touchui/touch_selection_controller_impl.h" | 17 #include "ui/views/touchui/touch_selection_controller_impl.h" |
18 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
19 | 19 |
20 #if defined(USE_AURA) | 20 #if defined(USE_AURA) |
21 #include "ui/aura/test/event_generator.h" | 21 #include "ui/aura/test/event_generator.h" |
22 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
23 #endif | 23 #endif |
24 | 24 |
(...skipping 19 matching lines...) Expand all Loading... |
44 } | 44 } |
45 } // namespace | 45 } // namespace |
46 | 46 |
47 namespace views { | 47 namespace views { |
48 | 48 |
49 class TouchSelectionControllerImplTest : public ViewsTestBase { | 49 class TouchSelectionControllerImplTest : public ViewsTestBase { |
50 public: | 50 public: |
51 TouchSelectionControllerImplTest() | 51 TouchSelectionControllerImplTest() |
52 : widget_(NULL), | 52 : widget_(NULL), |
53 textfield_(NULL), | 53 textfield_(NULL), |
54 textfield_view_(NULL), | |
55 views_tsc_factory_(new ViewsTouchSelectionControllerFactory) { | 54 views_tsc_factory_(new ViewsTouchSelectionControllerFactory) { |
56 CommandLine::ForCurrentProcess()->AppendSwitch( | 55 CommandLine::ForCurrentProcess()->AppendSwitch( |
57 switches::kEnableTouchEditing); | 56 switches::kEnableTouchEditing); |
58 ui::TouchSelectionControllerFactory::SetInstance(views_tsc_factory_.get()); | 57 ui::TouchSelectionControllerFactory::SetInstance(views_tsc_factory_.get()); |
59 } | 58 } |
60 | 59 |
61 virtual ~TouchSelectionControllerImplTest() { | 60 virtual ~TouchSelectionControllerImplTest() { |
62 ui::TouchSelectionControllerFactory::SetInstance(NULL); | 61 ui::TouchSelectionControllerFactory::SetInstance(NULL); |
63 } | 62 } |
64 | 63 |
65 virtual void TearDown() { | 64 virtual void TearDown() { |
66 if (widget_) | 65 if (widget_) |
67 widget_->Close(); | 66 widget_->Close(); |
68 ViewsTestBase::TearDown(); | 67 ViewsTestBase::TearDown(); |
69 } | 68 } |
70 | 69 |
71 void CreateTextfield() { | 70 void CreateTextfield() { |
72 textfield_ = new Textfield(); | 71 textfield_ = new Textfield(); |
73 widget_ = new Widget; | 72 widget_ = new Widget; |
74 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 73 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
75 params.bounds = gfx::Rect(0, 0, 200, 200); | 74 params.bounds = gfx::Rect(0, 0, 200, 200); |
76 widget_->Init(params); | 75 widget_->Init(params); |
77 View* container = new View(); | 76 View* container = new View(); |
78 widget_->SetContentsView(container); | 77 widget_->SetContentsView(container); |
79 container->AddChildView(textfield_); | 78 container->AddChildView(textfield_); |
80 | 79 |
81 textfield_view_ = textfield_->GetTextfieldViewForTesting(); | |
82 textfield_->SetBoundsRect(params.bounds); | 80 textfield_->SetBoundsRect(params.bounds); |
83 textfield_view_->SetBoundsRect(params.bounds); | |
84 textfield_->set_id(1); | 81 textfield_->set_id(1); |
85 widget_->Show(); | 82 widget_->Show(); |
86 | 83 |
87 DCHECK(textfield_view_); | |
88 textfield_->RequestFocus(); | 84 textfield_->RequestFocus(); |
89 } | 85 } |
90 | 86 |
91 protected: | 87 protected: |
92 gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) { | 88 gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) { |
93 gfx::RenderText* render_text = textfield_view_->GetRenderText(); | 89 gfx::RenderText* render_text = textfield_->GetRenderText(); |
94 gfx::Rect cursor_bounds = render_text->GetCursorBounds(sel, true); | 90 gfx::Rect cursor_bounds = render_text->GetCursorBounds(sel, true); |
95 return gfx::Point(cursor_bounds.x(), cursor_bounds.y()); | 91 return gfx::Point(cursor_bounds.x(), cursor_bounds.y()); |
96 } | 92 } |
97 | 93 |
98 TouchSelectionControllerImpl* GetSelectionController() { | 94 TouchSelectionControllerImpl* GetSelectionController() { |
99 return static_cast<TouchSelectionControllerImpl*>( | 95 return static_cast<TouchSelectionControllerImpl*>( |
100 textfield_view_->touch_selection_controller_.get()); | 96 textfield_->touch_selection_controller_.get()); |
101 } | 97 } |
102 | 98 |
103 void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { | 99 void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { |
104 TouchSelectionControllerImpl* controller = GetSelectionController(); | 100 TouchSelectionControllerImpl* controller = GetSelectionController(); |
105 // Do the work of OnMousePressed(). | 101 // Do the work of OnMousePressed(). |
106 if (selection_handle == 1) | 102 if (selection_handle == 1) |
107 controller->SetDraggingHandle(controller->selection_handle_1_.get()); | 103 controller->SetDraggingHandle(controller->selection_handle_1_.get()); |
108 else | 104 else |
109 controller->SetDraggingHandle(controller->selection_handle_2_.get()); | 105 controller->SetDraggingHandle(controller->selection_handle_2_.get()); |
110 | 106 |
(...skipping 24 matching lines...) Expand all Loading... |
135 | 131 |
136 bool IsSelectionHandle2Visible() { | 132 bool IsSelectionHandle2Visible() { |
137 return GetSelectionController()->IsSelectionHandle2Visible(); | 133 return GetSelectionController()->IsSelectionHandle2Visible(); |
138 } | 134 } |
139 | 135 |
140 bool IsCursorHandleVisible() { | 136 bool IsCursorHandleVisible() { |
141 return GetSelectionController()->IsCursorHandleVisible(); | 137 return GetSelectionController()->IsCursorHandleVisible(); |
142 } | 138 } |
143 | 139 |
144 gfx::RenderText* GetRenderText() { | 140 gfx::RenderText* GetRenderText() { |
145 return textfield_view_->GetRenderText(); | 141 return textfield_->GetRenderText(); |
146 } | 142 } |
147 | 143 |
148 Widget* widget_; | 144 Widget* widget_; |
149 | 145 |
150 Textfield* textfield_; | 146 Textfield* textfield_; |
151 NativeTextfieldViews* textfield_view_; | |
152 scoped_ptr<ViewsTouchSelectionControllerFactory> views_tsc_factory_; | 147 scoped_ptr<ViewsTouchSelectionControllerFactory> views_tsc_factory_; |
153 | 148 |
154 private: | 149 private: |
155 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest); | 150 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest); |
156 }; | 151 }; |
157 | 152 |
158 // If textfield has selection, this macro verifies that the selection handles | 153 // If textfield has selection, this macro verifies that the selection handles |
159 // are visible and at the correct positions (at the end points of selection). | 154 // are visible and at the correct positions (at the end points of selection). |
160 // |cursor_at_selection_handle_1| is used to decide whether selection | 155 // |cursor_at_selection_handle_1| is used to decide whether selection |
161 // handle 1's position is matched against the start of selection or the end. | 156 // handle 1's position is matched against the start of selection or the end. |
162 #define VERIFY_HANDLE_POSITIONS(cursor_at_selection_handle_1) \ | 157 #define VERIFY_HANDLE_POSITIONS(cursor_at_selection_handle_1) \ |
163 { \ | 158 { \ |
164 gfx::SelectionModel sel = textfield_view_->GetSelectionModel(); \ | 159 gfx::SelectionModel sel = textfield_->GetSelectionModel(); \ |
165 if (textfield_->HasSelection()) { \ | 160 if (textfield_->HasSelection()) { \ |
166 EXPECT_TRUE(IsSelectionHandle1Visible()); \ | 161 EXPECT_TRUE(IsSelectionHandle1Visible()); \ |
167 EXPECT_TRUE(IsSelectionHandle2Visible()); \ | 162 EXPECT_TRUE(IsSelectionHandle2Visible()); \ |
168 EXPECT_FALSE(IsCursorHandleVisible()); \ | 163 EXPECT_FALSE(IsCursorHandleVisible()); \ |
169 gfx::SelectionModel sel_start = GetRenderText()-> \ | 164 gfx::SelectionModel sel_start = GetRenderText()-> \ |
170 GetSelectionModelForSelectionStart(); \ | 165 GetSelectionModelForSelectionStart(); \ |
171 gfx::Point selection_start = GetCursorPosition(sel_start); \ | 166 gfx::Point selection_start = GetCursorPosition(sel_start); \ |
172 gfx::Point selection_end = GetCursorPosition(sel); \ | 167 gfx::Point selection_end = GetCursorPosition(sel); \ |
173 gfx::Point sh1 = GetSelectionHandle1Position(); \ | 168 gfx::Point sh1 = GetSelectionHandle1Position(); \ |
174 gfx::Point sh2 = GetSelectionHandle2Position(); \ | 169 gfx::Point sh2 = GetSelectionHandle2Position(); \ |
(...skipping 18 matching lines...) Expand all Loading... |
193 } | 188 } |
194 | 189 |
195 // Tests that the selection handles are placed appropriately when selection in | 190 // Tests that the selection handles are placed appropriately when selection in |
196 // a Textfield changes. | 191 // a Textfield changes. |
197 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { | 192 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { |
198 CreateTextfield(); | 193 CreateTextfield(); |
199 textfield_->SetText(ASCIIToUTF16("some text")); | 194 textfield_->SetText(ASCIIToUTF16("some text")); |
200 // Tap the textfield to invoke touch selection. | 195 // Tap the textfield to invoke touch selection. |
201 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), | 196 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), |
202 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); | 197 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); |
203 textfield_view_->OnGestureEvent(&tap); | 198 textfield_->OnGestureEvent(&tap); |
204 | 199 |
205 // Test selecting a range. | 200 // Test selecting a range. |
206 textfield_->SelectRange(gfx::Range(3, 7)); | 201 textfield_->SelectRange(gfx::Range(3, 7)); |
207 VERIFY_HANDLE_POSITIONS(false); | 202 VERIFY_HANDLE_POSITIONS(false); |
208 | 203 |
209 // Test selecting everything. | 204 // Test selecting everything. |
210 textfield_->SelectAll(false); | 205 textfield_->SelectAll(false); |
211 VERIFY_HANDLE_POSITIONS(false); | 206 VERIFY_HANDLE_POSITIONS(false); |
212 | 207 |
213 // Test with no selection. | 208 // Test with no selection. |
214 textfield_->ClearSelection(); | 209 textfield_->ClearSelection(); |
215 VERIFY_HANDLE_POSITIONS(false); | 210 VERIFY_HANDLE_POSITIONS(false); |
216 | 211 |
217 // Test with lost focus. | 212 // Test with lost focus. |
218 widget_->GetFocusManager()->ClearFocus(); | 213 widget_->GetFocusManager()->ClearFocus(); |
219 EXPECT_FALSE(GetSelectionController()); | 214 EXPECT_FALSE(GetSelectionController()); |
220 | 215 |
221 // Test with focus re-gained. | 216 // Test with focus re-gained. |
222 widget_->GetFocusManager()->SetFocusedView(textfield_); | 217 widget_->GetFocusManager()->SetFocusedView(textfield_); |
223 EXPECT_FALSE(GetSelectionController()); | 218 EXPECT_FALSE(GetSelectionController()); |
224 textfield_view_->OnGestureEvent(&tap); | 219 textfield_->OnGestureEvent(&tap); |
225 VERIFY_HANDLE_POSITIONS(false); | 220 VERIFY_HANDLE_POSITIONS(false); |
226 } | 221 } |
227 | 222 |
228 // Tests that the selection handles are placed appropriately in bidi text. | 223 // Tests that the selection handles are placed appropriately in bidi text. |
229 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { | 224 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { |
230 CreateTextfield(); | 225 CreateTextfield(); |
231 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); | 226 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); |
232 // Tap the textfield to invoke touch selection. | 227 // Tap the textfield to invoke touch selection. |
233 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), | 228 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), |
234 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); | 229 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); |
235 textfield_view_->OnGestureEvent(&tap); | 230 textfield_->OnGestureEvent(&tap); |
236 | 231 |
237 // Test cursor at run boundary and with empty selection. | 232 // Test cursor at run boundary and with empty selection. |
238 textfield_->SelectSelectionModel( | 233 textfield_->SelectSelectionModel( |
239 gfx::SelectionModel(3, gfx::CURSOR_BACKWARD)); | 234 gfx::SelectionModel(3, gfx::CURSOR_BACKWARD)); |
240 VERIFY_HANDLE_POSITIONS(false); | 235 VERIFY_HANDLE_POSITIONS(false); |
241 | 236 |
242 // Test selection range inside one run and starts or ends at run boundary. | 237 // Test selection range inside one run and starts or ends at run boundary. |
243 textfield_->SelectRange(gfx::Range(2, 3)); | 238 textfield_->SelectRange(gfx::Range(2, 3)); |
244 VERIFY_HANDLE_POSITIONS(false); | 239 VERIFY_HANDLE_POSITIONS(false); |
245 | 240 |
(...skipping 27 matching lines...) Expand all Loading... |
273 } | 268 } |
274 | 269 |
275 // Tests if the SelectRect callback is called appropriately when selection | 270 // Tests if the SelectRect callback is called appropriately when selection |
276 // handles are moved. | 271 // handles are moved. |
277 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { | 272 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { |
278 CreateTextfield(); | 273 CreateTextfield(); |
279 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); | 274 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); |
280 // Tap the textfield to invoke touch selection. | 275 // Tap the textfield to invoke touch selection. |
281 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), | 276 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), |
282 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); | 277 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); |
283 textfield_view_->OnGestureEvent(&tap); | 278 textfield_->OnGestureEvent(&tap); |
284 textfield_->SelectRange(gfx::Range(3, 7)); | 279 textfield_->SelectRange(gfx::Range(3, 7)); |
285 | 280 |
286 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); | 281 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); |
287 VERIFY_HANDLE_POSITIONS(false); | 282 VERIFY_HANDLE_POSITIONS(false); |
288 | 283 |
289 // Drag selection handle 2 to right by 3 chars. | 284 // Drag selection handle 2 to right by 3 chars. |
290 const gfx::Font& font = textfield_->GetPrimaryFont(); | 285 const gfx::FontList& font_list = textfield_->GetFontList(); |
291 int x = font.GetStringWidth(ASCIIToUTF16("ld ")); | 286 int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list); |
292 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 287 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
293 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield "); | 288 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield "); |
294 VERIFY_HANDLE_POSITIONS(false); | 289 VERIFY_HANDLE_POSITIONS(false); |
295 | 290 |
296 // Drag selection handle 1 to the left by a large amount (selection should | 291 // Drag selection handle 1 to the left by a large amount (selection should |
297 // just stick to the beginning of the textfield). | 292 // just stick to the beginning of the textfield). |
298 SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1); | 293 SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1); |
299 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield "); | 294 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield "); |
300 VERIFY_HANDLE_POSITIONS(true); | 295 VERIFY_HANDLE_POSITIONS(true); |
301 | 296 |
302 // Drag selection handle 1 across selection handle 2. | 297 // Drag selection handle 1 across selection handle 2. |
303 x = font.GetStringWidth(ASCIIToUTF16("textfield with ")); | 298 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list); |
304 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 299 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); |
305 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with "); | 300 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with "); |
306 VERIFY_HANDLE_POSITIONS(true); | 301 VERIFY_HANDLE_POSITIONS(true); |
307 | 302 |
308 // Drag selection handle 2 across selection handle 1. | 303 // Drag selection handle 2 across selection handle 1. |
309 x = font.GetStringWidth(ASCIIToUTF16("with selected ")); | 304 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list); |
310 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 305 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
311 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); | 306 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); |
312 VERIFY_HANDLE_POSITIONS(false); | 307 VERIFY_HANDLE_POSITIONS(false); |
313 } | 308 } |
314 | 309 |
315 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) { | 310 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) { |
316 CreateTextfield(); | 311 CreateTextfield(); |
317 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def")); | 312 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def")); |
318 // Tap the textfield to invoke touch selection. | 313 // Tap the textfield to invoke touch selection. |
319 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), | 314 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), |
320 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); | 315 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); |
321 textfield_view_->OnGestureEvent(&tap); | 316 textfield_->OnGestureEvent(&tap); |
322 | 317 |
323 // Select [c] from left to right. | 318 // Select [c] from left to right. |
324 textfield_->SelectRange(gfx::Range(2, 3)); | 319 textfield_->SelectRange(gfx::Range(2, 3)); |
325 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); | 320 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); |
326 VERIFY_HANDLE_POSITIONS(false); | 321 VERIFY_HANDLE_POSITIONS(false); |
327 | 322 |
328 // Drag selection handle 2 to right by 1 char. | 323 // Drag selection handle 2 to right by 1 char. |
329 const gfx::Font& font = textfield_->GetPrimaryFont(); | 324 const gfx::FontList& font_list = textfield_->GetFontList(); |
330 int x = font.GetStringWidth(WideToUTF16(L"\x05e3")); | 325 int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list); |
331 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 326 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
332 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | 327 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
333 VERIFY_HANDLE_POSITIONS(false); | 328 VERIFY_HANDLE_POSITIONS(false); |
334 | 329 |
335 // Drag selection handle 1 to left by 1 char. | 330 // Drag selection handle 1 to left by 1 char. |
336 x = font.GetStringWidth(WideToUTF16(L"b")); | 331 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list); |
337 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | 332 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); |
338 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); | 333 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); |
339 VERIFY_HANDLE_POSITIONS(true); | 334 VERIFY_HANDLE_POSITIONS(true); |
340 | 335 |
341 // Select [c] from right to left. | 336 // Select [c] from right to left. |
342 textfield_->SelectRange(gfx::Range(3, 2)); | 337 textfield_->SelectRange(gfx::Range(3, 2)); |
343 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); | 338 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); |
344 VERIFY_HANDLE_POSITIONS(false); | 339 VERIFY_HANDLE_POSITIONS(false); |
345 | 340 |
346 // Drag selection handle 1 to right by 1 char. | 341 // Drag selection handle 1 to right by 1 char. |
347 x = font.GetStringWidth(WideToUTF16(L"\x05e3")); | 342 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list); |
348 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 343 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); |
349 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | 344 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
350 VERIFY_HANDLE_POSITIONS(true); | 345 VERIFY_HANDLE_POSITIONS(true); |
351 | 346 |
352 // Drag selection handle 2 to left by 1 char. | 347 // Drag selection handle 2 to left by 1 char. |
353 x = font.GetStringWidth(WideToUTF16(L"b")); | 348 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list); |
354 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | 349 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); |
355 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); | 350 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); |
356 VERIFY_HANDLE_POSITIONS(false); | 351 VERIFY_HANDLE_POSITIONS(false); |
357 | 352 |
358 // Select [\x5e1] from right to left. | 353 // Select [\x5e1] from right to left. |
359 textfield_->SelectRange(gfx::Range(3, 4)); | 354 textfield_->SelectRange(gfx::Range(3, 4)); |
360 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); | 355 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); |
361 VERIFY_HANDLE_POSITIONS(false); | 356 VERIFY_HANDLE_POSITIONS(false); |
362 | 357 |
363 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click | 358 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click |
364 right of 'D' and select [D] then move the left selection handle to left | 359 right of 'D' and select [D] then move the left selection handle to left |
365 by one character, it should select [ED], instead it selects [F]. | 360 by one character, it should select [ED], instead it selects [F]. |
366 Reason: click right of 'D' and left of 'h' return the same x-axis position, | 361 Reason: click right of 'D' and left of 'h' return the same x-axis position, |
367 pass this position to FindCursorPosition() returns index of 'h'. which | 362 pass this position to FindCursorPosition() returns index of 'h'. which |
368 means the selection start changed from 3 to 6. | 363 means the selection start changed from 3 to 6. |
369 Need further investigation on whether this is a bug in Pango and how to | 364 Need further investigation on whether this is a bug in Pango and how to |
370 work around it. | 365 work around it. |
371 // Drag selection handle 2 to left by 1 char. | 366 // Drag selection handle 2 to left by 1 char. |
372 x = font.GetStringWidth(WideToUTF16(L"\x05e2")); | 367 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); |
373 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | 368 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); |
374 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); | 369 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); |
375 VERIFY_HANDLE_POSITIONS(false); | 370 VERIFY_HANDLE_POSITIONS(false); |
376 */ | 371 */ |
377 | 372 |
378 // Drag selection handle 1 to right by 1 char. | 373 // Drag selection handle 1 to right by 1 char. |
379 x = font.GetStringWidth(WideToUTF16(L"d")); | 374 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list); |
380 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 375 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); |
381 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText()); | 376 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText()); |
382 VERIFY_HANDLE_POSITIONS(true); | 377 VERIFY_HANDLE_POSITIONS(true); |
383 | 378 |
384 // Select [\x5e1] from left to right. | 379 // Select [\x5e1] from left to right. |
385 textfield_->SelectRange(gfx::Range(4, 3)); | 380 textfield_->SelectRange(gfx::Range(4, 3)); |
386 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); | 381 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); |
387 VERIFY_HANDLE_POSITIONS(false); | 382 VERIFY_HANDLE_POSITIONS(false); |
388 | 383 |
389 /* TODO(xji): see detail of above commented out test case. | 384 /* TODO(xji): see detail of above commented out test case. |
390 // Drag selection handle 1 to left by 1 char. | 385 // Drag selection handle 1 to left by 1 char. |
391 x = font.GetStringWidth(WideToUTF16(L"\x05e2")); | 386 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); |
392 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | 387 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); |
393 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); | 388 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); |
394 VERIFY_HANDLE_POSITIONS(true); | 389 VERIFY_HANDLE_POSITIONS(true); |
395 */ | 390 */ |
396 | 391 |
397 // Drag selection handle 2 to right by 1 char. | 392 // Drag selection handle 2 to right by 1 char. |
398 x = font.GetStringWidth(WideToUTF16(L"d")); | 393 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list); |
399 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 394 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
400 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText()); | 395 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText()); |
401 VERIFY_HANDLE_POSITIONS(false); | 396 VERIFY_HANDLE_POSITIONS(false); |
402 | 397 |
403 // Select [\x05r3] from right to left. | 398 // Select [\x05r3] from right to left. |
404 textfield_->SelectRange(gfx::Range(5, 6)); | 399 textfield_->SelectRange(gfx::Range(5, 6)); |
405 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); | 400 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); |
406 VERIFY_HANDLE_POSITIONS(false); | 401 VERIFY_HANDLE_POSITIONS(false); |
407 | 402 |
408 // Drag selection handle 2 to left by 1 char. | 403 // Drag selection handle 2 to left by 1 char. |
409 x = font.GetStringWidth(WideToUTF16(L"c")); | 404 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list); |
410 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | 405 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); |
411 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | 406 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
412 VERIFY_HANDLE_POSITIONS(false); | 407 VERIFY_HANDLE_POSITIONS(false); |
413 | 408 |
414 // Drag selection handle 1 to right by 1 char. | 409 // Drag selection handle 1 to right by 1 char. |
415 x = font.GetStringWidth(WideToUTF16(L"\x05e2")); | 410 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); |
416 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 411 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); |
417 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); | 412 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); |
418 VERIFY_HANDLE_POSITIONS(true); | 413 VERIFY_HANDLE_POSITIONS(true); |
419 | 414 |
420 // Select [\x05r3] from left to right. | 415 // Select [\x05r3] from left to right. |
421 textfield_->SelectRange(gfx::Range(6, 5)); | 416 textfield_->SelectRange(gfx::Range(6, 5)); |
422 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); | 417 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); |
423 VERIFY_HANDLE_POSITIONS(false); | 418 VERIFY_HANDLE_POSITIONS(false); |
424 | 419 |
425 // Drag selection handle 1 to left by 1 char. | 420 // Drag selection handle 1 to left by 1 char. |
426 x = font.GetStringWidth(WideToUTF16(L"c")); | 421 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list); |
427 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | 422 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); |
428 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | 423 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
429 VERIFY_HANDLE_POSITIONS(true); | 424 VERIFY_HANDLE_POSITIONS(true); |
430 | 425 |
431 // Drag selection handle 2 to right by 1 char. | 426 // Drag selection handle 2 to right by 1 char. |
432 x = font.GetStringWidth(WideToUTF16(L"\x05e2")); | 427 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); |
433 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 428 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); |
434 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); | 429 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); |
435 VERIFY_HANDLE_POSITIONS(false); | 430 VERIFY_HANDLE_POSITIONS(false); |
436 } | 431 } |
437 | 432 |
438 TEST_F(TouchSelectionControllerImplTest, | 433 TEST_F(TouchSelectionControllerImplTest, |
439 HiddenSelectionHandleRetainsCursorPosition) { | 434 HiddenSelectionHandleRetainsCursorPosition) { |
440 // Create a textfield with lots of text in it. | 435 // Create a textfield with lots of text in it. |
441 CreateTextfield(); | 436 CreateTextfield(); |
442 std::string textfield_text("some text"); | 437 std::string textfield_text("some text"); |
443 for (int i = 0; i < 10; ++i) | 438 for (int i = 0; i < 10; ++i) |
444 textfield_text += textfield_text; | 439 textfield_text += textfield_text; |
445 textfield_->SetText(ASCIIToUTF16(textfield_text)); | 440 textfield_->SetText(ASCIIToUTF16(textfield_text)); |
446 | 441 |
447 // Tap the textfield to invoke selection. | 442 // Tap the textfield to invoke selection. |
448 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), | 443 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(), |
449 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); | 444 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 1.0f, 0.0f), 0); |
450 textfield_view_->OnGestureEvent(&tap); | 445 textfield_->OnGestureEvent(&tap); |
451 | 446 |
452 // Select some text such that one handle is hidden. | 447 // Select some text such that one handle is hidden. |
453 textfield_->SelectRange(gfx::Range(10, textfield_text.length())); | 448 textfield_->SelectRange(gfx::Range(10, textfield_text.length())); |
454 | 449 |
455 // Check that one selection handle is hidden. | 450 // Check that one selection handle is hidden. |
456 EXPECT_FALSE(IsSelectionHandle1Visible()); | 451 EXPECT_FALSE(IsSelectionHandle1Visible()); |
457 EXPECT_TRUE(IsSelectionHandle2Visible()); | 452 EXPECT_TRUE(IsSelectionHandle2Visible()); |
458 EXPECT_EQ(gfx::Range(10, textfield_text.length()), | 453 EXPECT_EQ(gfx::Range(10, textfield_text.length()), |
459 textfield_->GetSelectedRange()); | 454 textfield_->GetSelectedRange()); |
460 | 455 |
(...skipping 30 matching lines...) Expand all Loading... |
491 gfx::Point cursor_pos = GetCursorHandlePosition(); | 486 gfx::Point cursor_pos = GetCursorHandlePosition(); |
492 cursor_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); | 487 cursor_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); |
493 generator.GestureTapAt(cursor_pos); | 488 generator.GestureTapAt(cursor_pos); |
494 generator.GestureTapAt(cursor_pos); | 489 generator.GestureTapAt(cursor_pos); |
495 EXPECT_TRUE(textfield_->HasSelection()); | 490 EXPECT_TRUE(textfield_->HasSelection()); |
496 VERIFY_HANDLE_POSITIONS(false); | 491 VERIFY_HANDLE_POSITIONS(false); |
497 } | 492 } |
498 #endif | 493 #endif |
499 | 494 |
500 } // namespace views | 495 } // namespace views |
OLD | NEW |