base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / views / touchui / touch_selection_controller_impl_unittest.cc
blob1e2254a0252bb40bc695b7be0097f4c8ef460e69
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/test/event_generator.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/geometry/point.h"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/render_text.h"
18 #include "ui/resources/grit/ui_resources.h"
19 #include "ui/views/controls/textfield/textfield.h"
20 #include "ui/views/controls/textfield/textfield_test_api.h"
21 #include "ui/views/test/views_test_base.h"
22 #include "ui/views/touchui/touch_selection_controller_impl.h"
23 #include "ui/views/views_touch_selection_controller_factory.h"
24 #include "ui/views/widget/widget.h"
26 using base::ASCIIToUTF16;
27 using base::UTF16ToUTF8;
28 using base::WideToUTF16;
30 namespace {
31 // Should match kSelectionHandleBarMinHeight in touch_selection_controller.
32 const int kBarMinHeight = 5;
34 // Should match kSelectionHandleBarBottomAllowance in
35 // touch_selection_controller.
36 const int kBarBottomAllowance = 3;
38 // Should match kMenuButtonWidth in touch_editing_menu.
39 const int kMenuButtonWidth = 63;
41 // Should match size of kMenuCommands array in touch_editing_menu.
42 const int kMenuCommandCount = 3;
44 // For selection bounds |b1| and |b2| in a paragraph of text, returns -1 if |b1|
45 // is physically before |b2|, +1 if |b2| is before |b1|, and 0 if they are at
46 // the same location.
47 int CompareTextSelectionBounds(const ui::SelectionBound& b1,
48 const ui::SelectionBound& b2) {
49 if (b1.edge_top().y() < b2.edge_top().y() ||
50 b1.edge_top().x() < b2.edge_top().x()) {
51 return -1;
53 if (b1 == b2)
54 return 0;
55 return 1;
58 } // namespace
60 namespace views {
62 class TouchSelectionControllerImplTest : public ViewsTestBase {
63 public:
64 TouchSelectionControllerImplTest()
65 : textfield_widget_(nullptr),
66 widget_(nullptr),
67 textfield_(nullptr),
68 views_tsc_factory_(new ViewsTouchEditingControllerFactory) {
69 base::CommandLine::ForCurrentProcess()->AppendSwitch(
70 switches::kEnableTouchEditing);
71 ui::TouchEditingControllerFactory::SetInstance(views_tsc_factory_.get());
74 ~TouchSelectionControllerImplTest() override {
75 ui::TouchEditingControllerFactory::SetInstance(nullptr);
78 void SetUp() override {
79 ViewsTestBase::SetUp();
80 test_cursor_client_.reset(new aura::test::TestCursorClient(GetContext()));
83 void TearDown() override {
84 test_cursor_client_.reset();
85 if (textfield_widget_ && !textfield_widget_->IsClosed())
86 textfield_widget_->Close();
87 if (widget_ && !widget_->IsClosed())
88 widget_->Close();
89 ViewsTestBase::TearDown();
92 void CreateTextfield() {
93 textfield_ = new Textfield();
94 textfield_widget_ = new Widget;
95 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
96 params.bounds = gfx::Rect(0, 0, 200, 200);
97 textfield_widget_->Init(params);
98 View* container = new View();
99 textfield_widget_->SetContentsView(container);
100 container->AddChildView(textfield_);
102 textfield_->SetBoundsRect(gfx::Rect(0, 0, 200, 20));
103 textfield_->set_id(1);
104 textfield_widget_->Show();
106 textfield_->RequestFocus();
107 textfield_test_api_.reset(new TextfieldTestApi(textfield_));
110 void CreateWidget() {
111 widget_ = new Widget;
112 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
113 params.bounds = gfx::Rect(0, 0, 200, 200);
114 widget_->Init(params);
115 widget_->Show();
118 protected:
119 static bool IsCursorHandleVisibleFor(
120 ui::TouchEditingControllerDeprecated* controller) {
121 TouchSelectionControllerImpl* impl =
122 static_cast<TouchSelectionControllerImpl*>(controller);
123 return impl->IsCursorHandleVisible();
126 gfx::Rect GetCursorRect(const gfx::SelectionModel& sel) {
127 return textfield_test_api_->GetRenderText()->GetCursorBounds(sel, true);
130 gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) {
131 gfx::Rect cursor_bounds = GetCursorRect(sel);
132 return gfx::Point(cursor_bounds.x(), cursor_bounds.y());
135 TouchSelectionControllerImpl* GetSelectionController() {
136 return static_cast<TouchSelectionControllerImpl*>(
137 textfield_test_api_->touch_selection_controller());
140 void StartTouchEditing() {
141 textfield_test_api_->CreateTouchSelectionControllerAndNotifyIt();
144 void EndTouchEditing() {
145 textfield_test_api_->ResetTouchSelectionController();
148 void SimulateSelectionHandleDrag(gfx::Vector2d v, int selection_handle) {
149 TouchSelectionControllerImpl* controller = GetSelectionController();
150 views::WidgetDelegateView* handle = nullptr;
151 if (selection_handle == 1)
152 handle = controller->GetHandle1View();
153 else
154 handle = controller->GetHandle2View();
156 gfx::Point grip_location = gfx::Point(handle->size().width() / 2,
157 handle->size().height() / 2);
158 base::TimeDelta time_stamp = base::TimeDelta();
160 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_BEGIN);
161 ui::GestureEvent scroll_begin(
162 grip_location.x(), grip_location.y(), 0, time_stamp, details);
163 handle->OnGestureEvent(&scroll_begin);
165 test_cursor_client_->DisableMouseEvents();
167 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_UPDATE);
168 gfx::Point update_location = grip_location + v;
169 ui::GestureEvent scroll_update(
170 update_location.x(), update_location.y(), 0, time_stamp, details);
171 handle->OnGestureEvent(&scroll_update);
174 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_END);
175 ui::GestureEvent scroll_end(
176 grip_location.x(), grip_location.y(), 0, time_stamp, details);
177 handle->OnGestureEvent(&scroll_end);
179 test_cursor_client_->EnableMouseEvents();
182 gfx::NativeView GetCursorHandleNativeView() {
183 return GetSelectionController()->GetCursorHandleNativeView();
186 gfx::Rect GetSelectionHandle1Bounds() {
187 return GetSelectionController()->GetSelectionHandle1Bounds();
190 gfx::Rect GetSelectionHandle2Bounds() {
191 return GetSelectionController()->GetSelectionHandle2Bounds();
194 gfx::Rect GetCursorHandleBounds() {
195 return GetSelectionController()->GetCursorHandleBounds();
198 gfx::Rect GetExpectedHandleBounds(const ui::SelectionBound& bound) {
199 return GetSelectionController()->GetExpectedHandleBounds(bound);
202 bool IsSelectionHandle1Visible() {
203 return GetSelectionController()->IsSelectionHandle1Visible();
206 bool IsSelectionHandle2Visible() {
207 return GetSelectionController()->IsSelectionHandle2Visible();
210 bool IsCursorHandleVisible() {
211 return GetSelectionController()->IsCursorHandleVisible();
214 gfx::RenderText* GetRenderText() {
215 return textfield_test_api_->GetRenderText();
218 gfx::Point GetCursorHandleDragPoint() {
219 gfx::Rect rect = GetCursorHandleBounds();
220 const gfx::SelectionModel& sel = textfield_->GetSelectionModel();
221 int cursor_height = GetCursorRect(sel).height();
222 gfx::Point point = rect.CenterPoint();
223 point.Offset(0, cursor_height);
224 return point;
227 // If textfield has selection, this verifies that the selection handles
228 // are visible, at the correct positions (at the end points of selection), and
229 // (if |check_direction| is set to true), that they have the correct
230 // directionality.
231 // |cursor_at_selection_handle_1| is used to decide whether selection
232 // handle 1's position is matched against the start of selection or the end.
233 void VerifyHandlePositions(bool cursor_at_selection_handle_1,
234 bool check_direction,
235 const tracked_objects::Location& from_here) {
236 ui::SelectionBound anchor, focus;
237 textfield_->GetSelectionEndPoints(&anchor, &focus);
238 std::string from_str = from_here.ToString();
239 if (textfield_->HasSelection()) {
240 EXPECT_TRUE(IsSelectionHandle1Visible()) << from_str;
241 EXPECT_TRUE(IsSelectionHandle2Visible()) << from_str;
242 EXPECT_FALSE(IsCursorHandleVisible());
243 gfx::Rect sh1_bounds = GetSelectionHandle1Bounds();
244 gfx::Rect sh2_bounds = GetSelectionHandle2Bounds();
245 if (cursor_at_selection_handle_1) {
246 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(focus)) << from_str;
247 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(anchor)) << from_str;
248 } else {
249 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(anchor)) << from_str;
250 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(focus)) << from_str;
252 } else {
253 EXPECT_FALSE(IsSelectionHandle1Visible()) << from_str;
254 EXPECT_FALSE(IsSelectionHandle2Visible()) << from_str;
255 EXPECT_TRUE(IsCursorHandleVisible());
256 gfx::Rect cursor_bounds = GetCursorHandleBounds();
257 DCHECK(anchor == focus);
258 EXPECT_EQ(cursor_bounds, GetExpectedHandleBounds(anchor)) << from_str;
260 if (check_direction) {
261 if (CompareTextSelectionBounds(anchor, focus) < 0) {
262 EXPECT_EQ(ui::SelectionBound::LEFT, anchor.type()) << from_str;
263 EXPECT_EQ(ui::SelectionBound::RIGHT, focus.type()) << from_str;
264 } else if (CompareTextSelectionBounds(anchor, focus) > 0) {
265 EXPECT_EQ(ui::SelectionBound::LEFT, focus.type()) << from_str;
266 EXPECT_EQ(ui::SelectionBound::RIGHT, anchor.type()) << from_str;
267 } else {
268 EXPECT_EQ(ui::SelectionBound::CENTER, focus.type()) << from_str;
269 EXPECT_EQ(ui::SelectionBound::CENTER, anchor.type()) << from_str;
274 Widget* textfield_widget_;
275 Widget* widget_;
277 Textfield* textfield_;
278 scoped_ptr<TextfieldTestApi> textfield_test_api_;
279 scoped_ptr<ViewsTouchEditingControllerFactory> views_tsc_factory_;
280 scoped_ptr<aura::test::TestCursorClient> test_cursor_client_;
282 private:
283 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest);
286 // Tests that the selection handles are placed appropriately when selection in
287 // a Textfield changes.
288 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) {
289 CreateTextfield();
290 textfield_->SetText(ASCIIToUTF16("some text"));
291 // Tap the textfield to invoke touch selection.
292 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
293 details.set_tap_count(1);
294 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
295 textfield_->OnGestureEvent(&tap);
297 // Test selecting a range.
298 textfield_->SelectRange(gfx::Range(3, 7));
299 VerifyHandlePositions(false, true, FROM_HERE);
301 // Test selecting everything.
302 textfield_->SelectAll(false);
303 VerifyHandlePositions(false, true, FROM_HERE);
305 // Test with no selection.
306 textfield_->ClearSelection();
307 VerifyHandlePositions(false, true, FROM_HERE);
309 // Test with lost focus.
310 textfield_widget_->GetFocusManager()->ClearFocus();
311 EXPECT_FALSE(GetSelectionController());
313 // Test with focus re-gained.
314 textfield_widget_->GetFocusManager()->SetFocusedView(textfield_);
315 EXPECT_FALSE(GetSelectionController());
316 textfield_->OnGestureEvent(&tap);
317 VerifyHandlePositions(false, true, FROM_HERE);
320 // Tests that the selection handles are placed appropriately in bidi text.
321 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) {
322 CreateTextfield();
323 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
324 // Tap the textfield to invoke touch selection.
325 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
326 details.set_tap_count(1);
327 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
328 textfield_->OnGestureEvent(&tap);
330 // Test cursor at run boundary and with empty selection.
331 textfield_->SelectSelectionModel(
332 gfx::SelectionModel(3, gfx::CURSOR_BACKWARD));
333 VerifyHandlePositions(false, true, FROM_HERE);
335 // Test selection range inside one run and starts or ends at run boundary.
336 textfield_->SelectRange(gfx::Range(2, 3));
337 VerifyHandlePositions(false, true, FROM_HERE);
339 textfield_->SelectRange(gfx::Range(3, 2));
340 VerifyHandlePositions(false, true, FROM_HERE);
342 // TODO(mfomitchev): crbug.com/429705
343 // The correct behavior for handles in mixed ltr/rtl text line is not known,
344 // so passing false for |check_direction| in some of these tests.
345 textfield_->SelectRange(gfx::Range(3, 4));
346 VerifyHandlePositions(false, false, FROM_HERE);
348 textfield_->SelectRange(gfx::Range(4, 3));
349 VerifyHandlePositions(false, false, FROM_HERE);
351 textfield_->SelectRange(gfx::Range(3, 6));
352 VerifyHandlePositions(false, false, FROM_HERE);
354 textfield_->SelectRange(gfx::Range(6, 3));
355 VerifyHandlePositions(false, false, FROM_HERE);
357 // Test selection range accross runs.
358 textfield_->SelectRange(gfx::Range(0, 6));
359 VerifyHandlePositions(false, true, FROM_HERE);
361 textfield_->SelectRange(gfx::Range(6, 0));
362 VerifyHandlePositions(false, true, FROM_HERE);
364 textfield_->SelectRange(gfx::Range(1, 4));
365 VerifyHandlePositions(false, true, FROM_HERE);
367 textfield_->SelectRange(gfx::Range(4, 1));
368 VerifyHandlePositions(false, true, FROM_HERE);
371 // Tests if the SelectRect callback is called appropriately when selection
372 // handles are moved.
373 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) {
374 CreateTextfield();
375 textfield_->SetText(ASCIIToUTF16("textfield with selected text"));
376 // Tap the textfield to invoke touch selection.
377 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
378 details.set_tap_count(1);
379 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
380 textfield_->OnGestureEvent(&tap);
381 textfield_->SelectRange(gfx::Range(3, 7));
383 gfx::Point textfield_origin;
384 textfield_->ConvertPointToScreen(&textfield_origin);
386 EXPECT_EQ("tfie", UTF16ToUTF8(textfield_->GetSelectedText()));
387 VerifyHandlePositions(false, true, FROM_HERE);
389 // Drag selection handle 2 to right by 3 chars.
390 const gfx::FontList& font_list = textfield_->GetFontList();
391 int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list);
392 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
393 EXPECT_EQ("tfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
394 VerifyHandlePositions(false, true, FROM_HERE);
396 // Drag selection handle 1 to the left by a large amount (selection should
397 // just stick to the beginning of the textfield).
398 SimulateSelectionHandleDrag(gfx::Vector2d(-50, 0), 1);
399 EXPECT_EQ("textfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
400 VerifyHandlePositions(true, true, FROM_HERE);
402 // Drag selection handle 1 across selection handle 2.
403 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list);
404 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
405 EXPECT_EQ("with ", UTF16ToUTF8(textfield_->GetSelectedText()));
406 VerifyHandlePositions(true, true, FROM_HERE);
408 // Drag selection handle 2 across selection handle 1.
409 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list);
410 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
411 EXPECT_EQ("selected ", UTF16ToUTF8(textfield_->GetSelectedText()));
412 VerifyHandlePositions(false, true, FROM_HERE);
415 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) {
416 CreateTextfield();
417 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def"));
418 // Tap the textfield to invoke touch selection.
419 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
420 details.set_tap_count(1);
421 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
422 textfield_->OnGestureEvent(&tap);
424 // Select [c] from left to right.
425 textfield_->SelectRange(gfx::Range(2, 3));
426 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
427 VerifyHandlePositions(false, true, FROM_HERE);
429 // Drag selection handle 2 to right by 1 char.
430 const gfx::FontList& font_list = textfield_->GetFontList();
431 int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
432 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
433 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
434 VerifyHandlePositions(false, true, FROM_HERE);
436 // Drag selection handle 1 to left by 1 char.
437 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
438 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
439 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
440 VerifyHandlePositions(true, true, FROM_HERE);
442 // Select [c] from right to left.
443 textfield_->SelectRange(gfx::Range(3, 2));
444 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
445 VerifyHandlePositions(false, true, FROM_HERE);
447 // Drag selection handle 1 to right by 1 char.
448 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
449 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
450 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
451 VerifyHandlePositions(true, true, FROM_HERE);
453 // Drag selection handle 2 to left by 1 char.
454 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
455 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
456 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
457 VerifyHandlePositions(false, true, FROM_HERE);
459 // Select [\x5e1] from right to left.
460 textfield_->SelectRange(gfx::Range(3, 4));
461 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
462 // TODO(mfomitchev): crbug.com/429705
463 // The correct behavior for handles in mixed ltr/rtl text line is not known,
464 // so passing false for |check_direction| in some of these tests.
465 VerifyHandlePositions(false, false, FROM_HERE);
467 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click
468 right of 'D' and select [D] then move the left selection handle to left
469 by one character, it should select [ED], instead it selects [F].
470 Reason: click right of 'D' and left of 'h' return the same x-axis position,
471 pass this position to FindCursorPosition() returns index of 'h'. which
472 means the selection start changed from 3 to 6.
473 Need further investigation on whether this is a bug in Pango and how to
474 work around it.
475 // Drag selection handle 2 to left by 1 char.
476 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
477 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
478 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
479 VERIFY_HANDLE_POSITIONS(false);
482 // Drag selection handle 1 to right by 1 char.
483 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
484 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
485 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
486 VerifyHandlePositions(true, true, FROM_HERE);
488 // Select [\x5e1] from left to right.
489 textfield_->SelectRange(gfx::Range(4, 3));
490 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
491 VerifyHandlePositions(false, false, FROM_HERE);
493 /* TODO(xji): see detail of above commented out test case.
494 // Drag selection handle 1 to left by 1 char.
495 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
496 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
497 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
498 VERIFY_HANDLE_POSITIONS(true);
501 // Drag selection handle 2 to right by 1 char.
502 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
503 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
504 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
505 VerifyHandlePositions(false, true, FROM_HERE);
507 // Select [\x05r3] from right to left.
508 textfield_->SelectRange(gfx::Range(5, 6));
509 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
510 VerifyHandlePositions(false, false, FROM_HERE);
512 // Drag selection handle 2 to left by 1 char.
513 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
514 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
515 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
516 VerifyHandlePositions(false, true, FROM_HERE);
518 // Drag selection handle 1 to right by 1 char.
519 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
520 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
521 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
522 VerifyHandlePositions(true, true, FROM_HERE);
524 // Select [\x05r3] from left to right.
525 textfield_->SelectRange(gfx::Range(6, 5));
526 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
527 VerifyHandlePositions(false, false, FROM_HERE);
529 // Drag selection handle 1 to left by 1 char.
530 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
531 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
532 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
533 VerifyHandlePositions(true, true, FROM_HERE);
535 // Drag selection handle 2 to right by 1 char.
536 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
537 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
538 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
539 VerifyHandlePositions(false, false, FROM_HERE);
542 TEST_F(TouchSelectionControllerImplTest,
543 HiddenSelectionHandleRetainsCursorPosition) {
544 // Create a textfield with lots of text in it.
545 CreateTextfield();
546 std::string textfield_text("some text");
547 for (int i = 0; i < 10; ++i)
548 textfield_text += textfield_text;
549 textfield_->SetText(ASCIIToUTF16(textfield_text));
551 // Tap the textfield to invoke selection.
552 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
553 details.set_tap_count(1);
554 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
555 textfield_->OnGestureEvent(&tap);
557 // Select some text such that one handle is hidden.
558 textfield_->SelectRange(gfx::Range(10, textfield_text.length()));
560 // Check that one selection handle is hidden.
561 EXPECT_FALSE(IsSelectionHandle1Visible());
562 EXPECT_TRUE(IsSelectionHandle2Visible());
563 EXPECT_EQ(gfx::Range(10, textfield_text.length()),
564 textfield_->GetSelectedRange());
566 // Drag the visible handle around and make sure the selection end point of the
567 // invisible handle does not change.
568 size_t visible_handle_position = textfield_->GetSelectedRange().end();
569 for (int i = 0; i < 10; ++i) {
570 static const int drag_diff = -10;
571 SimulateSelectionHandleDrag(gfx::Vector2d(drag_diff, 0), 2);
572 // Make sure that the visible handle is being dragged.
573 EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end());
574 visible_handle_position = textfield_->GetSelectedRange().end();
575 EXPECT_EQ((size_t) 10, textfield_->GetSelectedRange().start());
579 TEST_F(TouchSelectionControllerImplTest,
580 DoubleTapInTextfieldWithCursorHandleShouldSelectText) {
581 CreateTextfield();
582 textfield_->SetText(ASCIIToUTF16("some text"));
583 ui::test::EventGenerator generator(
584 textfield_->GetWidget()->GetNativeView()->GetRootWindow());
586 // Tap the textfield to invoke touch selection.
587 generator.GestureTapAt(gfx::Point(10, 10));
589 // Cursor handle should be visible.
590 EXPECT_FALSE(textfield_->HasSelection());
591 VerifyHandlePositions(false, true, FROM_HERE);
593 // Double tap on the cursor handle position. We want to check that the cursor
594 // handle is not eating the event and that the event is falling through to the
595 // textfield.
596 gfx::Point cursor_pos = GetCursorHandleBounds().origin();
597 cursor_pos.Offset(GetCursorHandleBounds().width() / 2, 0);
598 generator.GestureTapAt(cursor_pos);
599 generator.GestureTapAt(cursor_pos);
600 EXPECT_TRUE(textfield_->HasSelection());
603 // A simple implementation of TouchEditable that allows faking cursor position
604 // inside its boundaries.
605 class TestTouchEditable : public ui::TouchEditable {
606 public:
607 explicit TestTouchEditable(aura::Window* window)
608 : window_(window) {
609 DCHECK(window);
612 void set_bounds(const gfx::Rect& bounds) {
613 bounds_ = bounds;
616 void set_cursor_rect(const gfx::Rect& cursor_rect) {
617 cursor_bound_.SetEdge(cursor_rect.origin(), cursor_rect.bottom_left());
618 cursor_bound_.set_type(ui::SelectionBound::Type::CENTER);
621 ~TestTouchEditable() override {}
623 private:
624 // Overridden from ui::TouchEditable.
625 void SelectRect(const gfx::Point& start, const gfx::Point& end) override {
626 NOTREACHED();
628 void MoveCaretTo(const gfx::Point& point) override { NOTREACHED(); }
629 void GetSelectionEndPoints(ui::SelectionBound* anchor,
630 ui::SelectionBound* focus) override {
631 *anchor = *focus = cursor_bound_;
633 gfx::Rect GetBounds() override { return gfx::Rect(bounds_.size()); }
634 gfx::NativeView GetNativeView() const override { return window_; }
635 void ConvertPointToScreen(gfx::Point* point) override {
636 aura::client::ScreenPositionClient* screen_position_client =
637 aura::client::GetScreenPositionClient(window_->GetRootWindow());
638 if (screen_position_client)
639 screen_position_client->ConvertPointToScreen(window_, point);
641 void ConvertPointFromScreen(gfx::Point* point) override {
642 aura::client::ScreenPositionClient* screen_position_client =
643 aura::client::GetScreenPositionClient(window_->GetRootWindow());
644 if (screen_position_client)
645 screen_position_client->ConvertPointFromScreen(window_, point);
647 bool DrawsHandles() override { return false; }
648 void OpenContextMenu(const gfx::Point& anchor) override { NOTREACHED(); }
649 void DestroyTouchSelection() override { NOTREACHED(); }
651 // Overridden from ui::SimpleMenuModel::Delegate.
652 bool IsCommandIdChecked(int command_id) const override {
653 NOTREACHED();
654 return false;
656 bool IsCommandIdEnabled(int command_id) const override {
657 NOTREACHED();
658 return false;
660 bool GetAcceleratorForCommandId(int command_id,
661 ui::Accelerator* accelerator) override {
662 NOTREACHED();
663 return false;
665 void ExecuteCommand(int command_id, int event_flags) override {
666 NOTREACHED();
669 aura::Window* window_;
671 // Boundaries of the client view.
672 gfx::Rect bounds_;
674 // Cursor position inside the client view.
675 //gfx::Rect cursor_rect_;
676 ui::SelectionBound cursor_bound_;
678 DISALLOW_COPY_AND_ASSIGN(TestTouchEditable);
681 // Tests if the touch editing handle is shown or hidden properly according to
682 // the cursor position relative to the client boundaries.
683 TEST_F(TouchSelectionControllerImplTest,
684 VisibilityOfHandleRegardingClientBounds) {
685 CreateWidget();
687 TestTouchEditable touch_editable(widget_->GetNativeView());
688 scoped_ptr<ui::TouchEditingControllerDeprecated> touch_selection_controller(
689 ui::TouchEditingControllerDeprecated::Create(&touch_editable));
691 touch_editable.set_bounds(gfx::Rect(0, 0, 100, 20));
693 // Put the cursor completely inside the client bounds. Handle should be
694 // visible.
695 touch_editable.set_cursor_rect(gfx::Rect(2, 0, 1, 20));
696 touch_selection_controller->SelectionChanged();
697 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
699 // Move the cursor up such that |kBarMinHeight| pixels are still in the client
700 // bounds. Handle should still be visible.
701 touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20, 1, 20));
702 touch_selection_controller->SelectionChanged();
703 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
705 // Move the cursor up such that less than |kBarMinHeight| pixels are in the
706 // client bounds. Handle should be hidden.
707 touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20 - 1, 1, 20));
708 touch_selection_controller->SelectionChanged();
709 EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
711 // Move the Cursor down such that |kBarBottomAllowance| pixels are out of the
712 // client bounds. Handle should be visible.
713 touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance, 1, 20));
714 touch_selection_controller->SelectionChanged();
715 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
717 // Move the cursor down such that more than |kBarBottomAllowance| pixels are
718 // out of the client bounds. Handle should be hidden.
719 touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance + 1, 1, 20));
720 touch_selection_controller->SelectionChanged();
721 EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
723 touch_selection_controller.reset();
726 TEST_F(TouchSelectionControllerImplTest, HandlesStackAboveParent) {
727 ui::EventTarget* root = GetContext();
728 ui::EventTargeter* targeter = root->GetEventTargeter();
730 // Create the first window containing a Views::Textfield.
731 CreateTextfield();
732 aura::Window* window1 = textfield_widget_->GetNativeView();
734 // Start touch editing, check that the handle is above the first window, and
735 // end touch editing.
736 StartTouchEditing();
737 gfx::Point test_point = GetCursorHandleDragPoint();
738 ui::MouseEvent test_event1(ui::ET_MOUSE_MOVED, test_point, test_point,
739 ui::EF_NONE, ui::EF_NONE);
740 EXPECT_EQ(GetCursorHandleNativeView(),
741 targeter->FindTargetForEvent(root, &test_event1));
742 EndTouchEditing();
744 // Create the second (empty) window over the first one.
745 CreateWidget();
746 aura::Window* window2 = widget_->GetNativeView();
748 // Start touch editing (in the first window) and check that the handle is not
749 // above the second window.
750 StartTouchEditing();
751 ui::MouseEvent test_event2(ui::ET_MOUSE_MOVED, test_point, test_point,
752 ui::EF_NONE, ui::EF_NONE);
753 EXPECT_EQ(window2, targeter->FindTargetForEvent(root, &test_event2));
755 // Move the first window to top and check that the handle is kept above the
756 // first window.
757 window1->GetRootWindow()->StackChildAtTop(window1);
758 ui::MouseEvent test_event3(ui::ET_MOUSE_MOVED, test_point, test_point,
759 ui::EF_NONE, ui::EF_NONE);
760 EXPECT_EQ(GetCursorHandleNativeView(),
761 targeter->FindTargetForEvent(root, &test_event3));
764 // A simple implementation of TouchEditingMenuController that enables all
765 // available commands.
766 class TestTouchEditingMenuController : public TouchEditingMenuController {
767 public:
768 TestTouchEditingMenuController() {}
769 ~TestTouchEditingMenuController() override {}
771 // Overriden from TouchEditingMenuController.
772 bool IsCommandIdEnabled(int command_id) const override {
773 // Return true, since we want the menu to have all |kMenuCommandCount|
774 // available commands.
775 return true;
777 void ExecuteCommand(int command_id, int event_flags) override {
778 NOTREACHED();
780 void OpenContextMenu() override { NOTREACHED(); }
781 void OnMenuClosed(TouchEditingMenuView* menu) override {}
783 private:
784 DISALLOW_COPY_AND_ASSIGN(TestTouchEditingMenuController);
787 // Tests if anchor rect for touch editing quick menu is adjusted correctly based
788 // on the distance of handles.
789 TEST_F(TouchSelectionControllerImplTest, QuickMenuAdjustsAnchorRect) {
790 CreateWidget();
791 aura::Window* window = widget_->GetNativeView();
793 scoped_ptr<TestTouchEditingMenuController> quick_menu_controller(
794 new TestTouchEditingMenuController());
796 // Some arbitrary size for touch editing handle image.
797 gfx::Size handle_image_size(10, 10);
799 // Calculate the width of quick menu. In addition to |kMenuCommandCount|
800 // commands, there is an item for ellipsis.
801 int quick_menu_width = (kMenuCommandCount + 1) * kMenuButtonWidth +
802 kMenuCommandCount;
804 // Set anchor rect's width a bit smaller than the quick menu width plus handle
805 // image width and check that anchor rect's height is adjusted.
806 gfx::Rect anchor_rect(
807 0, 0, quick_menu_width + handle_image_size.width() - 10, 20);
808 TouchEditingMenuView* quick_menu(TouchEditingMenuView::Create(
809 quick_menu_controller.get(), anchor_rect, handle_image_size, window));
810 anchor_rect.Inset(0, 0, 0, -handle_image_size.height());
811 EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString());
813 // Set anchor rect's width a bit greater than the quick menu width plus handle
814 // image width and check that anchor rect's height is not adjusted.
815 anchor_rect =
816 gfx::Rect(0, 0, quick_menu_width + handle_image_size.width() + 10, 20);
817 quick_menu = TouchEditingMenuView::Create(
818 quick_menu_controller.get(), anchor_rect, handle_image_size, window);
819 EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString());
821 // Close the widget, hence quick menus, before quick menu controller goes out
822 // of scope.
823 widget_->CloseNow();
824 widget_ = nullptr;
827 TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) {
828 CreateTextfield();
829 EXPECT_FALSE(GetSelectionController());
831 ui::test::EventGenerator generator(
832 textfield_widget_->GetNativeView()->GetRootWindow());
834 generator.set_current_location(gfx::Point(5, 5));
835 RunPendingMessages();
837 // Start touch editing; then move mouse over the textfield and ensure it
838 // deactivates touch selection.
839 StartTouchEditing();
840 EXPECT_TRUE(GetSelectionController());
841 generator.MoveMouseTo(gfx::Point(5, 10));
842 RunPendingMessages();
843 EXPECT_FALSE(GetSelectionController());
845 generator.MoveMouseTo(gfx::Point(5, 50));
846 RunPendingMessages();
848 // Start touch editing; then move mouse out of the textfield, but inside the
849 // winow and ensure it deactivates touch selection.
850 StartTouchEditing();
851 EXPECT_TRUE(GetSelectionController());
852 generator.MoveMouseTo(gfx::Point(5, 55));
853 RunPendingMessages();
854 EXPECT_FALSE(GetSelectionController());
856 generator.MoveMouseTo(gfx::Point(5, 500));
857 RunPendingMessages();
859 // Start touch editing; then move mouse out of the textfield and window and
860 // ensure it deactivates touch selection.
861 StartTouchEditing();
862 EXPECT_TRUE(GetSelectionController());
863 generator.MoveMouseTo(5, 505);
864 RunPendingMessages();
865 EXPECT_FALSE(GetSelectionController());
868 TEST_F(TouchSelectionControllerImplTest, KeyEventDeactivatesTouchSelection) {
869 CreateTextfield();
870 EXPECT_FALSE(GetSelectionController());
872 ui::test::EventGenerator generator(
873 textfield_widget_->GetNativeView()->GetRootWindow());
875 RunPendingMessages();
877 // Start touch editing; then press a key and ensure it deactivates touch
878 // selection.
879 StartTouchEditing();
880 EXPECT_TRUE(GetSelectionController());
881 generator.PressKey(ui::VKEY_A, 0);
882 RunPendingMessages();
883 EXPECT_FALSE(GetSelectionController());
886 } // namespace views