OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/utf_string_conversions.h" | |
6 #include "ui/gfx/point.h" | |
7 #include "ui/gfx/rect.h" | |
8 #include "ui/gfx/render_text.h" | |
9 #include "views/controls/textfield/native_textfield_views.h" | |
10 #include "views/controls/textfield/textfield.h" | |
11 #include "views/test/views_test_base.h" | |
12 #include "views/touchui/touch_selection_controller.h" | |
13 #include "views/touchui/touch_selection_controller_impl.h" | |
14 #include "views/widget/widget.h" | |
15 | |
16 namespace views { | |
17 | |
18 class TouchSelectionControllerImplTest : public ViewsTestBase { | |
19 public: | |
20 TouchSelectionControllerImplTest() | |
21 : widget_(NULL), | |
22 textfield_(NULL), | |
23 textfield_view_(NULL) { | |
24 } | |
25 | |
26 virtual void TearDown() { | |
27 if (widget_) | |
28 widget_->Close(); | |
29 ViewsTestBase::TearDown(); | |
30 } | |
31 | |
32 void CreateTextfield() { | |
33 textfield_ = new Textfield(); | |
34 widget_ = new Widget; | |
35 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
36 params.bounds = gfx::Rect(0, 0, 200, 200); | |
37 widget_->Init(params); | |
38 View* container = new View(); | |
39 widget_->SetContentsView(container); | |
40 container->AddChildView(textfield_); | |
41 | |
42 textfield_view_ = static_cast<NativeTextfieldViews*>( | |
43 textfield_->GetNativeWrapperForTesting()); | |
44 textfield_view_->SetBoundsRect(params.bounds); | |
45 textfield_->set_id(1); | |
46 | |
47 DCHECK(textfield_view_); | |
48 textfield_->RequestFocus(); | |
49 } | |
50 | |
51 protected: | |
52 gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) { | |
53 gfx::RenderText* render_text = textfield_view_->GetRenderText(); | |
54 gfx::Rect cursor_bounds = render_text->GetCursorBounds(sel, true); | |
55 return gfx::Point(cursor_bounds.x(), cursor_bounds.bottom() - 1); | |
56 } | |
57 | |
58 TouchSelectionControllerImpl* GetSelectionController() { | |
59 return static_cast<TouchSelectionControllerImpl*>( | |
60 textfield_view_->touch_selection_controller_.get()); | |
61 } | |
62 | |
63 void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { | |
64 TouchSelectionControllerImpl* controller = GetSelectionController(); | |
65 // Do the work of OnMousePressed(). | |
66 if (selection_handle == 1) | |
67 controller->dragging_handle_ = controller->selection_handle_1_.get(); | |
68 else | |
69 controller->dragging_handle_ = controller->selection_handle_2_.get(); | |
70 | |
71 controller->SelectionHandleDragged(p); | |
72 | |
73 // Do the work of OnMouseReleased(). | |
74 controller->dragging_handle_ = NULL; | |
75 } | |
76 | |
77 // If textfield has selection, this method verifies that the selection handles | |
78 // are visible and at the correct positions (at the end points of selection). | |
79 // |cursor_at_selection_handle_1| is used to decide whether selection | |
80 // handle 1's position is matched against the start of selection or the end. | |
81 void VerifySelectionHandlePositions(bool cursor_at_selection_handle_1) { | |
82 if (textfield_->HasSelection()) { | |
83 EXPECT_TRUE(GetSelectionController()->IsSelectionHandle1Visible()); | |
84 EXPECT_TRUE(GetSelectionController()->IsSelectionHandle2Visible()); | |
85 gfx::SelectionModel sel; | |
86 textfield_view_->GetSelectionModel(&sel); | |
87 gfx::SelectionModel sel_start = textfield_view_->GetRenderText()-> | |
88 GetSelectionModelForSelectionStart(); | |
89 gfx::Point selection_start = GetCursorPosition(sel_start); | |
90 gfx::Point selection_end = GetCursorPosition(sel); | |
91 gfx::Point sh1 = GetSelectionController()->GetSelectionHandle1Position(); | |
92 gfx::Point sh2 = GetSelectionController()->GetSelectionHandle2Position(); | |
93 sh1.Offset(10, 0); // offset by kSelectionHandleRadius. | |
94 sh2.Offset(10, 0); | |
95 | |
96 if (cursor_at_selection_handle_1) { | |
97 EXPECT_EQ(sh1, selection_end); | |
98 EXPECT_EQ(sh2, selection_start); | |
99 } else { | |
100 EXPECT_EQ(sh1, selection_start); | |
101 EXPECT_EQ(sh2, selection_end); | |
102 } | |
103 } else { | |
104 EXPECT_FALSE(GetSelectionController()->IsSelectionHandle1Visible()); | |
105 EXPECT_FALSE(GetSelectionController()->IsSelectionHandle2Visible()); | |
106 } | |
107 } | |
108 | |
109 Widget* widget_; | |
110 | |
111 Textfield* textfield_; | |
112 NativeTextfieldViews* textfield_view_; | |
113 | |
114 private: | |
115 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest); | |
116 }; | |
117 | |
118 // Tests that the selection handles are placed appropriately when selection in | |
119 // a Textfield changes. | |
120 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { | |
121 CreateTextfield(); | |
122 textfield_->SetText(ASCIIToUTF16("some text")); | |
123 | |
124 // Test selecting a range. | |
125 textfield_->SelectRange(ui::Range(3, 7)); | |
126 VerifySelectionHandlePositions(false); | |
127 | |
128 // Test selecting everything. | |
129 textfield_->SelectAll(); | |
130 VerifySelectionHandlePositions(false); | |
131 | |
132 // Test with no selection. | |
133 textfield_->ClearSelection(); | |
134 VerifySelectionHandlePositions(false); | |
135 | |
136 // Test with lost focus. | |
137 widget_->GetFocusManager()->ClearFocus(); | |
138 VerifySelectionHandlePositions(false); | |
139 | |
140 // Test with focus re-gained. | |
141 widget_->GetFocusManager()->SetFocusedView(textfield_); | |
142 VerifySelectionHandlePositions(false); | |
143 } | |
144 | |
145 // Tests that the selection handles are placed appropriately in bidi text. | |
146 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { | |
147 CreateTextfield(); | |
148 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); | |
149 | |
150 // Test cursor at run boundary and with empty selection. | |
151 textfield_->SelectSelectionModel( | |
152 gfx::SelectionModel(3, 2, gfx::SelectionModel::TRAILING)); | |
153 VerifySelectionHandlePositions(false); | |
154 | |
155 // Test selection range inside one run and starts or ends at run boundary. | |
156 textfield_->SelectRange(ui::Range(2, 3)); | |
157 VerifySelectionHandlePositions(false); | |
158 | |
159 textfield_->SelectRange(ui::Range(3, 2)); | |
160 VerifySelectionHandlePositions(false); | |
161 | |
162 textfield_->SelectRange(ui::Range(3, 4)); | |
163 VerifySelectionHandlePositions(false); | |
164 | |
165 textfield_->SelectRange(ui::Range(4, 3)); | |
166 VerifySelectionHandlePositions(false); | |
167 | |
168 textfield_->SelectRange(ui::Range(3, 6)); | |
169 VerifySelectionHandlePositions(false); | |
170 | |
171 textfield_->SelectRange(ui::Range(6, 3)); | |
172 VerifySelectionHandlePositions(false); | |
173 | |
174 // Test selection range accross runs. | |
175 textfield_->SelectRange(ui::Range(0, 6)); | |
176 VerifySelectionHandlePositions(false); | |
177 | |
178 textfield_->SelectRange(ui::Range(6, 0)); | |
179 VerifySelectionHandlePositions(false); | |
180 | |
181 textfield_->SelectRange(ui::Range(1, 4)); | |
182 VerifySelectionHandlePositions(false); | |
183 | |
184 textfield_->SelectRange(ui::Range(4, 1)); | |
185 VerifySelectionHandlePositions(false); | |
186 } | |
187 | |
188 // Tests if the SelectRect callback is called appropriately when selection | |
189 // handles are moved. | |
190 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { | |
191 CreateTextfield(); | |
192 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); | |
193 textfield_->SelectRange(ui::Range(3, 7)); | |
194 | |
195 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); | |
196 VerifySelectionHandlePositions(false); | |
197 | |
198 // Drag selection handle 2 to right by 3 chars. | |
199 int x = textfield_->font().GetStringWidth(ASCIIToUTF16("ld ")); | |
200 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | |
201 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield "); | |
202 VerifySelectionHandlePositions(false); | |
203 | |
204 // Drag selection handle 1 to the left by a large amount (selection should | |
205 // just stick to the beginning of the textfield). | |
206 SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1); | |
207 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield "); | |
208 VerifySelectionHandlePositions(true); | |
209 | |
210 // Drag selection handle 1 across selection handle 2. | |
211 x = textfield_->font().GetStringWidth(ASCIIToUTF16("textfield with ")); | |
212 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | |
213 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with "); | |
214 VerifySelectionHandlePositions(true); | |
215 | |
216 // Drag selection handle 2 across selection handle 1. | |
217 x = textfield_->font().GetStringWidth(ASCIIToUTF16("with selected ")); | |
218 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | |
219 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); | |
220 VerifySelectionHandlePositions(false); | |
221 } | |
222 | |
223 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) { | |
224 CreateTextfield(); | |
225 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3"L"def")); | |
226 | |
227 // Select [c] from left to right. | |
228 textfield_->SelectRange(ui::Range(2, 3)); | |
229 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); | |
230 VerifySelectionHandlePositions(false); | |
231 | |
232 // Drag selection handle 2 to right by 1 char. | |
233 int x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e3")); | |
234 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | |
235 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | |
236 VerifySelectionHandlePositions(false); | |
237 | |
238 // Drag selection handle 1 to left by 1 char. | |
239 x = textfield_->font().GetStringWidth(WideToUTF16(L"b")); | |
240 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | |
241 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); | |
242 VerifySelectionHandlePositions(true); | |
243 | |
244 // Select [c] from right to left. | |
245 textfield_->SelectRange(ui::Range(3, 2)); | |
246 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); | |
247 VerifySelectionHandlePositions(false); | |
248 | |
249 // Drag selection handle 1 to right by 1 char. | |
250 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e3")); | |
251 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | |
252 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | |
253 VerifySelectionHandlePositions(true); | |
254 | |
255 // Drag selection handle 2 to left by 1 char. | |
256 x = textfield_->font().GetStringWidth(WideToUTF16(L"b")); | |
257 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | |
258 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); | |
259 VerifySelectionHandlePositions(false); | |
260 | |
261 // Select [\x5e1] from right to left. | |
262 textfield_->SelectRange(ui::Range(3, 4)); | |
263 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); | |
264 VerifySelectionHandlePositions(false); | |
265 | |
266 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click | |
267 right of 'D' and select [D] then move the left selection handle to left | |
268 by one character, it should select [ED], instead it selects [F]. | |
269 Reason: click right of 'D' and left of 'h' return the same x-axis position, | |
270 pass this position to FindCursorPosition() returns index of 'h'. which | |
271 means the selection start changed from 3 to 6. | |
272 Need further investigation on whether this is a bug in Pango and how to | |
273 work around it. | |
274 // Drag selection handle 2 to left by 1 char. | |
275 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); | |
276 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | |
277 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); | |
278 VerifySelectionHandlePositions(false); | |
279 */ | |
280 | |
281 // Drag selection handle 1 to right by 1 char. | |
282 x = textfield_->font().GetStringWidth(WideToUTF16(L"d")); | |
283 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | |
284 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3"L"d"), textfield_->GetSelectedText()); | |
285 VerifySelectionHandlePositions(true); | |
286 | |
287 // Select [\x5e1] from left to right. | |
288 textfield_->SelectRange(ui::Range(4, 3)); | |
289 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); | |
290 VerifySelectionHandlePositions(false); | |
291 | |
292 /* TODO(xji): see detail of above commented out test case. | |
293 // Drag selection handle 1 to left by 1 char. | |
294 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); | |
295 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | |
296 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); | |
297 VerifySelectionHandlePositions(true); | |
298 */ | |
299 | |
300 // Drag selection handle 2 to right by 1 char. | |
301 x = textfield_->font().GetStringWidth(WideToUTF16(L"d")); | |
302 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | |
303 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3"L"d"), textfield_->GetSelectedText()); | |
304 VerifySelectionHandlePositions(false); | |
305 | |
306 // Select [\x05r3] from right to left. | |
307 textfield_->SelectRange(ui::Range(5, 6)); | |
308 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); | |
309 VerifySelectionHandlePositions(false); | |
310 | |
311 // Drag selection handle 2 to left by 1 char. | |
312 x = textfield_->font().GetStringWidth(WideToUTF16(L"c")); | |
313 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | |
314 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | |
315 VerifySelectionHandlePositions(false); | |
316 | |
317 // Drag selection handle 1 to right by 1 char. | |
318 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); | |
319 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | |
320 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); | |
321 VerifySelectionHandlePositions(true); | |
322 | |
323 // Select [\x05r3] from left to right. | |
324 textfield_->SelectRange(ui::Range(6, 5)); | |
325 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); | |
326 VerifySelectionHandlePositions(false); | |
327 | |
328 // Drag selection handle 1 to left by 1 char. | |
329 x = textfield_->font().GetStringWidth(WideToUTF16(L"c")); | |
330 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | |
331 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | |
332 VerifySelectionHandlePositions(true); | |
333 | |
334 // Drag selection handle 2 to right by 1 char. | |
335 x = textfield_->font().GetStringWidth(WideToUTF16(L"\x05e2")); | |
336 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | |
337 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); | |
338 VerifySelectionHandlePositions(false); | |
339 } | |
340 | |
341 } // namespace views | |
OLD | NEW |