Add ICU message format support
[chromium-blink-merge.git] / ui / views / touchui / touch_selection_controller_impl_unittest.cc
blobb9df2ec3b32a83aae4a2ab32c8722e4915b46da4
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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "ui/aura/client/screen_position_client.h"
8 #include "ui/aura/test/test_cursor_client.h"
9 #include "ui/aura/window.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/base/touch/touch_editing_controller.h"
12 #include "ui/base/ui_base_switches.h"
13 #include "ui/events/event_utils.h"
14 #include "ui/events/test/event_generator.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/geometry/point.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/render_text.h"
19 #include "ui/resources/grit/ui_resources.h"
20 #include "ui/views/controls/textfield/textfield.h"
21 #include "ui/views/controls/textfield/textfield_test_api.h"
22 #include "ui/views/resources/grit/views_resources.h"
23 #include "ui/views/test/views_test_base.h"
24 #include "ui/views/touchui/touch_selection_controller_impl.h"
25 #include "ui/views/views_touch_selection_controller_factory.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/views/widget/widget_delegate.h"
29 using base::ASCIIToUTF16;
30 using base::UTF16ToUTF8;
31 using base::WideToUTF16;
33 namespace {
34 // Should match kSelectionHandleBarMinHeight in touch_selection_controller.
35 const int kBarMinHeight = 5;
37 // Should match kSelectionHandleBarBottomAllowance in
38 // touch_selection_controller.
39 const int kBarBottomAllowance = 3;
41 // For selection bounds |b1| and |b2| in a paragraph of text, returns -1 if |b1|
42 // is physically before |b2|, +1 if |b2| is before |b1|, and 0 if they are at
43 // the same location.
44 int CompareTextSelectionBounds(const ui::SelectionBound& b1,
45 const ui::SelectionBound& b2) {
46 if (b1.edge_top().y() < b2.edge_top().y() ||
47 b1.edge_top().x() < b2.edge_top().x()) {
48 return -1;
50 if (b1 == b2)
51 return 0;
52 return 1;
55 } // namespace
57 namespace views {
59 class TouchSelectionControllerImplTest : public ViewsTestBase {
60 public:
61 TouchSelectionControllerImplTest()
62 : textfield_widget_(nullptr),
63 widget_(nullptr),
64 textfield_(nullptr),
65 views_tsc_factory_(new ViewsTouchEditingControllerFactory) {
66 base::CommandLine::ForCurrentProcess()->AppendSwitch(
67 switches::kEnableTouchEditing);
68 ui::TouchEditingControllerFactory::SetInstance(views_tsc_factory_.get());
71 ~TouchSelectionControllerImplTest() override {
72 ui::TouchEditingControllerFactory::SetInstance(nullptr);
75 void SetUp() override {
76 ViewsTestBase::SetUp();
77 test_cursor_client_.reset(new aura::test::TestCursorClient(GetContext()));
80 void TearDown() override {
81 test_cursor_client_.reset();
82 if (textfield_widget_ && !textfield_widget_->IsClosed())
83 textfield_widget_->Close();
84 if (widget_ && !widget_->IsClosed())
85 widget_->Close();
86 ViewsTestBase::TearDown();
89 void CreateTextfield() {
90 textfield_ = new Textfield();
91 textfield_widget_ = new Widget;
92 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
93 params.bounds = gfx::Rect(0, 0, 200, 200);
94 textfield_widget_->Init(params);
95 View* container = new View();
96 textfield_widget_->SetContentsView(container);
97 container->AddChildView(textfield_);
99 textfield_->SetBoundsRect(gfx::Rect(0, 0, 200, 20));
100 textfield_->set_id(1);
101 textfield_widget_->Show();
103 textfield_->RequestFocus();
104 textfield_test_api_.reset(new TextfieldTestApi(textfield_));
107 void CreateWidget() {
108 widget_ = new Widget;
109 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
110 params.bounds = gfx::Rect(0, 0, 200, 200);
111 widget_->Init(params);
112 widget_->Show();
115 protected:
116 static bool IsCursorHandleVisibleFor(
117 ui::TouchEditingControllerDeprecated* controller) {
118 TouchSelectionControllerImpl* impl =
119 static_cast<TouchSelectionControllerImpl*>(controller);
120 return impl->IsCursorHandleVisible();
123 gfx::Rect GetCursorRect(const gfx::SelectionModel& sel) {
124 return textfield_test_api_->GetRenderText()->GetCursorBounds(sel, true);
127 gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) {
128 gfx::Rect cursor_bounds = GetCursorRect(sel);
129 return gfx::Point(cursor_bounds.x(), cursor_bounds.y());
132 TouchSelectionControllerImpl* GetSelectionController() {
133 return static_cast<TouchSelectionControllerImpl*>(
134 textfield_test_api_->touch_selection_controller());
137 void StartTouchEditing() {
138 textfield_test_api_->CreateTouchSelectionControllerAndNotifyIt();
141 void EndTouchEditing() {
142 textfield_test_api_->ResetTouchSelectionController();
145 void SimulateSelectionHandleDrag(gfx::Vector2d v, int selection_handle) {
146 TouchSelectionControllerImpl* controller = GetSelectionController();
147 views::WidgetDelegateView* handle = nullptr;
148 if (selection_handle == 1)
149 handle = controller->GetHandle1View();
150 else
151 handle = controller->GetHandle2View();
153 gfx::Point grip_location = gfx::Point(handle->size().width() / 2,
154 handle->size().height() / 2);
155 base::TimeDelta time_stamp = base::TimeDelta();
157 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_BEGIN);
158 ui::GestureEvent scroll_begin(
159 grip_location.x(), grip_location.y(), 0, time_stamp, details);
160 handle->OnGestureEvent(&scroll_begin);
162 test_cursor_client_->DisableMouseEvents();
164 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_UPDATE);
165 gfx::Point update_location = grip_location + v;
166 ui::GestureEvent scroll_update(
167 update_location.x(), update_location.y(), 0, time_stamp, details);
168 handle->OnGestureEvent(&scroll_update);
171 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_END);
172 ui::GestureEvent scroll_end(
173 grip_location.x(), grip_location.y(), 0, time_stamp, details);
174 handle->OnGestureEvent(&scroll_end);
176 test_cursor_client_->EnableMouseEvents();
179 gfx::NativeView GetCursorHandleNativeView() {
180 return GetSelectionController()->GetCursorHandleNativeView();
183 gfx::Rect GetSelectionHandle1Bounds() {
184 return GetSelectionController()->GetSelectionHandle1Bounds();
187 gfx::Rect GetSelectionHandle2Bounds() {
188 return GetSelectionController()->GetSelectionHandle2Bounds();
191 gfx::Rect GetCursorHandleBounds() {
192 return GetSelectionController()->GetCursorHandleBounds();
195 gfx::Rect GetExpectedHandleBounds(const ui::SelectionBound& bound) {
196 return GetSelectionController()->GetExpectedHandleBounds(bound);
199 bool IsSelectionHandle1Visible() {
200 return GetSelectionController()->IsSelectionHandle1Visible();
203 bool IsSelectionHandle2Visible() {
204 return GetSelectionController()->IsSelectionHandle2Visible();
207 bool IsCursorHandleVisible() {
208 return GetSelectionController()->IsCursorHandleVisible();
211 gfx::RenderText* GetRenderText() {
212 return textfield_test_api_->GetRenderText();
215 gfx::Point GetCursorHandleDragPoint() {
216 gfx::Rect rect = GetCursorHandleBounds();
217 const gfx::SelectionModel& sel = textfield_->GetSelectionModel();
218 int cursor_height = GetCursorRect(sel).height();
219 gfx::Point point = rect.CenterPoint();
220 point.Offset(0, cursor_height);
221 return point;
224 // If textfield has selection, this verifies that the selection handles
225 // are visible, at the correct positions (at the end points of selection), and
226 // (if |check_direction| is set to true), that they have the correct
227 // directionality.
228 // |cursor_at_selection_handle_1| is used to decide whether selection
229 // handle 1's position is matched against the start of selection or the end.
230 void VerifyHandlePositions(bool cursor_at_selection_handle_1,
231 bool check_direction,
232 const tracked_objects::Location& from_here) {
233 ui::SelectionBound anchor, focus;
234 textfield_->GetSelectionEndPoints(&anchor, &focus);
235 std::string from_str = from_here.ToString();
236 if (textfield_->HasSelection()) {
237 EXPECT_TRUE(IsSelectionHandle1Visible()) << from_str;
238 EXPECT_TRUE(IsSelectionHandle2Visible()) << from_str;
239 EXPECT_FALSE(IsCursorHandleVisible());
240 gfx::Rect sh1_bounds = GetSelectionHandle1Bounds();
241 gfx::Rect sh2_bounds = GetSelectionHandle2Bounds();
242 if (cursor_at_selection_handle_1) {
243 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(focus)) << from_str;
244 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(anchor)) << from_str;
245 } else {
246 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(anchor)) << from_str;
247 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(focus)) << from_str;
249 } else {
250 EXPECT_FALSE(IsSelectionHandle1Visible()) << from_str;
251 EXPECT_FALSE(IsSelectionHandle2Visible()) << from_str;
252 EXPECT_TRUE(IsCursorHandleVisible());
253 gfx::Rect cursor_bounds = GetCursorHandleBounds();
254 DCHECK(anchor == focus);
255 EXPECT_EQ(cursor_bounds, GetExpectedHandleBounds(anchor)) << from_str;
257 if (check_direction) {
258 if (CompareTextSelectionBounds(anchor, focus) < 0) {
259 EXPECT_EQ(ui::SelectionBound::LEFT, anchor.type()) << from_str;
260 EXPECT_EQ(ui::SelectionBound::RIGHT, focus.type()) << from_str;
261 } else if (CompareTextSelectionBounds(anchor, focus) > 0) {
262 EXPECT_EQ(ui::SelectionBound::LEFT, focus.type()) << from_str;
263 EXPECT_EQ(ui::SelectionBound::RIGHT, anchor.type()) << from_str;
264 } else {
265 EXPECT_EQ(ui::SelectionBound::CENTER, focus.type()) << from_str;
266 EXPECT_EQ(ui::SelectionBound::CENTER, anchor.type()) << from_str;
271 Widget* textfield_widget_;
272 Widget* widget_;
274 Textfield* textfield_;
275 scoped_ptr<TextfieldTestApi> textfield_test_api_;
276 scoped_ptr<ViewsTouchEditingControllerFactory> views_tsc_factory_;
277 scoped_ptr<aura::test::TestCursorClient> test_cursor_client_;
279 private:
280 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest);
283 // Tests that the selection handles are placed appropriately when selection in
284 // a Textfield changes.
285 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) {
286 CreateTextfield();
287 textfield_->SetText(ASCIIToUTF16("some text"));
288 // Tap the textfield to invoke touch selection.
289 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
290 details.set_tap_count(1);
291 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
292 textfield_->OnGestureEvent(&tap);
294 // Test selecting a range.
295 textfield_->SelectRange(gfx::Range(3, 7));
296 VerifyHandlePositions(false, true, FROM_HERE);
298 // Test selecting everything.
299 textfield_->SelectAll(false);
300 VerifyHandlePositions(false, true, FROM_HERE);
302 // Test with no selection.
303 textfield_->ClearSelection();
304 VerifyHandlePositions(false, true, FROM_HERE);
306 // Test with lost focus.
307 textfield_widget_->GetFocusManager()->ClearFocus();
308 EXPECT_FALSE(GetSelectionController());
310 // Test with focus re-gained.
311 textfield_widget_->GetFocusManager()->SetFocusedView(textfield_);
312 EXPECT_FALSE(GetSelectionController());
313 textfield_->OnGestureEvent(&tap);
314 VerifyHandlePositions(false, true, FROM_HERE);
317 // Tests that the selection handles are placed appropriately in bidi text.
318 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) {
319 CreateTextfield();
320 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
321 // Tap the textfield to invoke touch selection.
322 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
323 details.set_tap_count(1);
324 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
325 textfield_->OnGestureEvent(&tap);
327 // Test cursor at run boundary and with empty selection.
328 textfield_->SelectSelectionModel(
329 gfx::SelectionModel(3, gfx::CURSOR_BACKWARD));
330 VerifyHandlePositions(false, true, FROM_HERE);
332 // Test selection range inside one run and starts or ends at run boundary.
333 textfield_->SelectRange(gfx::Range(2, 3));
334 VerifyHandlePositions(false, true, FROM_HERE);
336 textfield_->SelectRange(gfx::Range(3, 2));
337 VerifyHandlePositions(false, true, FROM_HERE);
339 // TODO(mfomitchev): crbug.com/429705
340 // The correct behavior for handles in mixed ltr/rtl text line is not known,
341 // so passing false for |check_direction| in some of these tests.
342 textfield_->SelectRange(gfx::Range(3, 4));
343 VerifyHandlePositions(false, false, FROM_HERE);
345 textfield_->SelectRange(gfx::Range(4, 3));
346 VerifyHandlePositions(false, false, FROM_HERE);
348 textfield_->SelectRange(gfx::Range(3, 6));
349 VerifyHandlePositions(false, false, FROM_HERE);
351 textfield_->SelectRange(gfx::Range(6, 3));
352 VerifyHandlePositions(false, false, FROM_HERE);
354 // Test selection range accross runs.
355 textfield_->SelectRange(gfx::Range(0, 6));
356 VerifyHandlePositions(false, true, FROM_HERE);
358 textfield_->SelectRange(gfx::Range(6, 0));
359 VerifyHandlePositions(false, true, FROM_HERE);
361 textfield_->SelectRange(gfx::Range(1, 4));
362 VerifyHandlePositions(false, true, FROM_HERE);
364 textfield_->SelectRange(gfx::Range(4, 1));
365 VerifyHandlePositions(false, true, FROM_HERE);
368 // Tests if the SelectRect callback is called appropriately when selection
369 // handles are moved.
370 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) {
371 CreateTextfield();
372 textfield_->SetText(ASCIIToUTF16("textfield with selected text"));
373 // Tap the textfield to invoke touch selection.
374 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
375 details.set_tap_count(1);
376 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
377 textfield_->OnGestureEvent(&tap);
378 textfield_->SelectRange(gfx::Range(3, 7));
380 gfx::Point textfield_origin;
381 textfield_->ConvertPointToScreen(&textfield_origin);
383 EXPECT_EQ("tfie", UTF16ToUTF8(textfield_->GetSelectedText()));
384 VerifyHandlePositions(false, true, FROM_HERE);
386 // Drag selection handle 2 to right by 3 chars.
387 const gfx::FontList& font_list = textfield_->GetFontList();
388 int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list);
389 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
390 EXPECT_EQ("tfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
391 VerifyHandlePositions(false, true, FROM_HERE);
393 // Drag selection handle 1 to the left by a large amount (selection should
394 // just stick to the beginning of the textfield).
395 SimulateSelectionHandleDrag(gfx::Vector2d(-50, 0), 1);
396 EXPECT_EQ("textfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
397 VerifyHandlePositions(true, true, FROM_HERE);
399 // Drag selection handle 1 across selection handle 2.
400 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list);
401 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
402 EXPECT_EQ("with ", UTF16ToUTF8(textfield_->GetSelectedText()));
403 VerifyHandlePositions(true, true, FROM_HERE);
405 // Drag selection handle 2 across selection handle 1.
406 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list);
407 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
408 EXPECT_EQ("selected ", UTF16ToUTF8(textfield_->GetSelectedText()));
409 VerifyHandlePositions(false, true, FROM_HERE);
412 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) {
413 CreateTextfield();
414 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def"));
415 // Tap the textfield to invoke touch selection.
416 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
417 details.set_tap_count(1);
418 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
419 textfield_->OnGestureEvent(&tap);
421 // Select [c] from left to right.
422 textfield_->SelectRange(gfx::Range(2, 3));
423 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
424 VerifyHandlePositions(false, true, FROM_HERE);
426 // Drag selection handle 2 to right by 1 char.
427 const gfx::FontList& font_list = textfield_->GetFontList();
428 int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
429 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
430 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
431 VerifyHandlePositions(false, true, FROM_HERE);
433 // Drag selection handle 1 to left by 1 char.
434 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
435 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
436 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
437 VerifyHandlePositions(true, true, FROM_HERE);
439 // Select [c] from right to left.
440 textfield_->SelectRange(gfx::Range(3, 2));
441 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
442 VerifyHandlePositions(false, true, FROM_HERE);
444 // Drag selection handle 1 to right by 1 char.
445 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
446 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
447 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
448 VerifyHandlePositions(true, true, FROM_HERE);
450 // Drag selection handle 2 to left by 1 char.
451 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
452 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
453 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
454 VerifyHandlePositions(false, true, FROM_HERE);
456 // Select [\x5e1] from right to left.
457 textfield_->SelectRange(gfx::Range(3, 4));
458 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
459 // TODO(mfomitchev): crbug.com/429705
460 // The correct behavior for handles in mixed ltr/rtl text line is not known,
461 // so passing false for |check_direction| in some of these tests.
462 VerifyHandlePositions(false, false, FROM_HERE);
464 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click
465 right of 'D' and select [D] then move the left selection handle to left
466 by one character, it should select [ED], instead it selects [F].
467 Reason: click right of 'D' and left of 'h' return the same x-axis position,
468 pass this position to FindCursorPosition() returns index of 'h'. which
469 means the selection start changed from 3 to 6.
470 Need further investigation on whether this is a bug in Pango and how to
471 work around it.
472 // Drag selection handle 2 to left by 1 char.
473 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
474 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
475 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
476 VERIFY_HANDLE_POSITIONS(false);
479 // Drag selection handle 1 to right by 1 char.
480 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
481 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
482 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
483 VerifyHandlePositions(true, true, FROM_HERE);
485 // Select [\x5e1] from left to right.
486 textfield_->SelectRange(gfx::Range(4, 3));
487 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
488 VerifyHandlePositions(false, false, FROM_HERE);
490 /* TODO(xji): see detail of above commented out test case.
491 // Drag selection handle 1 to left by 1 char.
492 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
493 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
494 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
495 VERIFY_HANDLE_POSITIONS(true);
498 // Drag selection handle 2 to right by 1 char.
499 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
500 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
501 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
502 VerifyHandlePositions(false, true, FROM_HERE);
504 // Select [\x05r3] from right to left.
505 textfield_->SelectRange(gfx::Range(5, 6));
506 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
507 VerifyHandlePositions(false, false, FROM_HERE);
509 // Drag selection handle 2 to left by 1 char.
510 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
511 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
512 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
513 VerifyHandlePositions(false, true, FROM_HERE);
515 // Drag selection handle 1 to right by 1 char.
516 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
517 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
518 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
519 VerifyHandlePositions(true, true, FROM_HERE);
521 // Select [\x05r3] from left to right.
522 textfield_->SelectRange(gfx::Range(6, 5));
523 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
524 VerifyHandlePositions(false, false, FROM_HERE);
526 // Drag selection handle 1 to left by 1 char.
527 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
528 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
529 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
530 VerifyHandlePositions(true, true, FROM_HERE);
532 // Drag selection handle 2 to right by 1 char.
533 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
534 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
535 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
536 VerifyHandlePositions(false, false, FROM_HERE);
539 TEST_F(TouchSelectionControllerImplTest,
540 HiddenSelectionHandleRetainsCursorPosition) {
541 // Create a textfield with lots of text in it.
542 CreateTextfield();
543 std::string textfield_text("some text");
544 for (int i = 0; i < 10; ++i)
545 textfield_text += textfield_text;
546 textfield_->SetText(ASCIIToUTF16(textfield_text));
548 // Tap the textfield to invoke selection.
549 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
550 details.set_tap_count(1);
551 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
552 textfield_->OnGestureEvent(&tap);
554 // Select some text such that one handle is hidden.
555 textfield_->SelectRange(gfx::Range(10, textfield_text.length()));
557 // Check that one selection handle is hidden.
558 EXPECT_FALSE(IsSelectionHandle1Visible());
559 EXPECT_TRUE(IsSelectionHandle2Visible());
560 EXPECT_EQ(gfx::Range(10, textfield_text.length()),
561 textfield_->GetSelectedRange());
563 // Drag the visible handle around and make sure the selection end point of the
564 // invisible handle does not change.
565 size_t visible_handle_position = textfield_->GetSelectedRange().end();
566 for (int i = 0; i < 10; ++i) {
567 static const int drag_diff = -10;
568 SimulateSelectionHandleDrag(gfx::Vector2d(drag_diff, 0), 2);
569 // Make sure that the visible handle is being dragged.
570 EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end());
571 visible_handle_position = textfield_->GetSelectedRange().end();
572 EXPECT_EQ((size_t) 10, textfield_->GetSelectedRange().start());
576 TEST_F(TouchSelectionControllerImplTest,
577 DoubleTapInTextfieldWithCursorHandleShouldSelectText) {
578 CreateTextfield();
579 textfield_->SetText(ASCIIToUTF16("some text"));
580 ui::test::EventGenerator generator(
581 textfield_->GetWidget()->GetNativeView()->GetRootWindow());
583 // Tap the textfield to invoke touch selection.
584 generator.GestureTapAt(gfx::Point(10, 10));
586 // Cursor handle should be visible.
587 EXPECT_FALSE(textfield_->HasSelection());
588 VerifyHandlePositions(false, true, FROM_HERE);
590 // Double tap on the cursor handle position. We want to check that the cursor
591 // handle is not eating the event and that the event is falling through to the
592 // textfield.
593 gfx::Point cursor_pos = GetCursorHandleBounds().origin();
594 cursor_pos.Offset(GetCursorHandleBounds().width() / 2, 0);
595 generator.GestureTapAt(cursor_pos);
596 generator.GestureTapAt(cursor_pos);
597 EXPECT_TRUE(textfield_->HasSelection());
600 // A simple implementation of TouchEditable that allows faking cursor position
601 // inside its boundaries.
602 class TestTouchEditable : public ui::TouchEditable {
603 public:
604 explicit TestTouchEditable(aura::Window* window)
605 : window_(window) {
606 DCHECK(window);
609 void set_bounds(const gfx::Rect& bounds) {
610 bounds_ = bounds;
613 void set_cursor_rect(const gfx::Rect& cursor_rect) {
614 cursor_bound_.SetEdge(cursor_rect.origin(), cursor_rect.bottom_left());
615 cursor_bound_.set_type(ui::SelectionBound::Type::CENTER);
618 ~TestTouchEditable() override {}
620 private:
621 // Overridden from ui::TouchEditable.
622 void SelectRect(const gfx::Point& start, const gfx::Point& end) override {
623 NOTREACHED();
625 void MoveCaretTo(const gfx::Point& point) override { NOTREACHED(); }
626 void GetSelectionEndPoints(ui::SelectionBound* anchor,
627 ui::SelectionBound* focus) override {
628 *anchor = *focus = cursor_bound_;
630 gfx::Rect GetBounds() override { return gfx::Rect(bounds_.size()); }
631 gfx::NativeView GetNativeView() const override { return window_; }
632 void ConvertPointToScreen(gfx::Point* point) override {
633 aura::client::ScreenPositionClient* screen_position_client =
634 aura::client::GetScreenPositionClient(window_->GetRootWindow());
635 if (screen_position_client)
636 screen_position_client->ConvertPointToScreen(window_, point);
638 void ConvertPointFromScreen(gfx::Point* point) override {
639 aura::client::ScreenPositionClient* screen_position_client =
640 aura::client::GetScreenPositionClient(window_->GetRootWindow());
641 if (screen_position_client)
642 screen_position_client->ConvertPointFromScreen(window_, point);
644 bool DrawsHandles() override { return false; }
645 void OpenContextMenu(const gfx::Point& anchor) override { NOTREACHED(); }
646 void DestroyTouchSelection() override { NOTREACHED(); }
648 // Overridden from ui::SimpleMenuModel::Delegate.
649 bool IsCommandIdChecked(int command_id) const override {
650 NOTREACHED();
651 return false;
653 bool IsCommandIdEnabled(int command_id) const override {
654 NOTREACHED();
655 return false;
657 bool GetAcceleratorForCommandId(int command_id,
658 ui::Accelerator* accelerator) override {
659 NOTREACHED();
660 return false;
662 void ExecuteCommand(int command_id, int event_flags) override {
663 NOTREACHED();
666 aura::Window* window_;
668 // Boundaries of the client view.
669 gfx::Rect bounds_;
671 // Cursor position inside the client view.
672 //gfx::Rect cursor_rect_;
673 ui::SelectionBound cursor_bound_;
675 DISALLOW_COPY_AND_ASSIGN(TestTouchEditable);
678 // Tests if the touch editing handle is shown or hidden properly according to
679 // the cursor position relative to the client boundaries.
680 TEST_F(TouchSelectionControllerImplTest,
681 VisibilityOfHandleRegardingClientBounds) {
682 CreateWidget();
684 TestTouchEditable touch_editable(widget_->GetNativeView());
685 scoped_ptr<ui::TouchEditingControllerDeprecated> touch_selection_controller(
686 ui::TouchEditingControllerDeprecated::Create(&touch_editable));
688 touch_editable.set_bounds(gfx::Rect(0, 0, 100, 20));
690 // Put the cursor completely inside the client bounds. Handle should be
691 // visible.
692 touch_editable.set_cursor_rect(gfx::Rect(2, 0, 1, 20));
693 touch_selection_controller->SelectionChanged();
694 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
696 // Move the cursor up such that |kBarMinHeight| pixels are still in the client
697 // bounds. Handle should still be visible.
698 touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20, 1, 20));
699 touch_selection_controller->SelectionChanged();
700 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
702 // Move the cursor up such that less than |kBarMinHeight| pixels are in the
703 // client bounds. Handle should be hidden.
704 touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20 - 1, 1, 20));
705 touch_selection_controller->SelectionChanged();
706 EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
708 // Move the Cursor down such that |kBarBottomAllowance| pixels are out of the
709 // client bounds. Handle should be visible.
710 touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance, 1, 20));
711 touch_selection_controller->SelectionChanged();
712 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
714 // Move the cursor down such that more than |kBarBottomAllowance| pixels are
715 // out of the client bounds. Handle should be hidden.
716 touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance + 1, 1, 20));
717 touch_selection_controller->SelectionChanged();
718 EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
720 touch_selection_controller.reset();
723 TEST_F(TouchSelectionControllerImplTest, HandlesStackAboveParent) {
724 ui::EventTarget* root = GetContext();
725 ui::EventTargeter* targeter = root->GetEventTargeter();
727 // Create the first window containing a Views::Textfield.
728 CreateTextfield();
729 aura::Window* window1 = textfield_widget_->GetNativeView();
731 // Start touch editing, check that the handle is above the first window, and
732 // end touch editing.
733 StartTouchEditing();
734 gfx::Point test_point = GetCursorHandleDragPoint();
735 ui::MouseEvent test_event1(ui::ET_MOUSE_MOVED, test_point, test_point,
736 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
737 EXPECT_EQ(GetCursorHandleNativeView(),
738 targeter->FindTargetForEvent(root, &test_event1));
739 EndTouchEditing();
741 // Create the second (empty) window over the first one.
742 CreateWidget();
743 aura::Window* window2 = widget_->GetNativeView();
745 // Start touch editing (in the first window) and check that the handle is not
746 // above the second window.
747 StartTouchEditing();
748 ui::MouseEvent test_event2(ui::ET_MOUSE_MOVED, test_point, test_point,
749 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
750 EXPECT_EQ(window2, targeter->FindTargetForEvent(root, &test_event2));
752 // Move the first window to top and check that the handle is kept above the
753 // first window.
754 window1->GetRootWindow()->StackChildAtTop(window1);
755 ui::MouseEvent test_event3(ui::ET_MOUSE_MOVED, test_point, test_point,
756 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
757 EXPECT_EQ(GetCursorHandleNativeView(),
758 targeter->FindTargetForEvent(root, &test_event3));
761 TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) {
762 CreateTextfield();
763 EXPECT_FALSE(GetSelectionController());
765 ui::test::EventGenerator generator(
766 textfield_widget_->GetNativeView()->GetRootWindow());
768 generator.set_current_location(gfx::Point(5, 5));
769 RunPendingMessages();
771 // Start touch editing; then move mouse over the textfield and ensure it
772 // deactivates touch selection.
773 StartTouchEditing();
774 EXPECT_TRUE(GetSelectionController());
775 generator.MoveMouseTo(gfx::Point(5, 10));
776 RunPendingMessages();
777 EXPECT_FALSE(GetSelectionController());
779 generator.MoveMouseTo(gfx::Point(5, 50));
780 RunPendingMessages();
782 // Start touch editing; then move mouse out of the textfield, but inside the
783 // winow and ensure it deactivates touch selection.
784 StartTouchEditing();
785 EXPECT_TRUE(GetSelectionController());
786 generator.MoveMouseTo(gfx::Point(5, 55));
787 RunPendingMessages();
788 EXPECT_FALSE(GetSelectionController());
790 generator.MoveMouseTo(gfx::Point(5, 500));
791 RunPendingMessages();
793 // Start touch editing; then move mouse out of the textfield and window and
794 // ensure it deactivates touch selection.
795 StartTouchEditing();
796 EXPECT_TRUE(GetSelectionController());
797 generator.MoveMouseTo(5, 505);
798 RunPendingMessages();
799 EXPECT_FALSE(GetSelectionController());
802 TEST_F(TouchSelectionControllerImplTest, KeyEventDeactivatesTouchSelection) {
803 CreateTextfield();
804 EXPECT_FALSE(GetSelectionController());
806 ui::test::EventGenerator generator(
807 textfield_widget_->GetNativeView()->GetRootWindow());
809 RunPendingMessages();
811 // Start touch editing; then press a key and ensure it deactivates touch
812 // selection.
813 StartTouchEditing();
814 EXPECT_TRUE(GetSelectionController());
815 generator.PressKey(ui::VKEY_A, 0);
816 RunPendingMessages();
817 EXPECT_FALSE(GetSelectionController());
820 } // namespace views