Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / views / touchui / touch_selection_controller_impl_unittest.cc
blob3c9a9cc9929d09f71b34b94b35becb916bdb2521
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/test/views_test_base.h"
23 #include "ui/views/touchui/touch_selection_controller_impl.h"
24 #include "ui/views/views_touch_selection_controller_factory.h"
25 #include "ui/views/widget/widget.h"
27 using base::ASCIIToUTF16;
28 using base::UTF16ToUTF8;
29 using base::WideToUTF16;
31 namespace {
32 // Should match kSelectionHandleBarMinHeight in touch_selection_controller.
33 const int kBarMinHeight = 5;
35 // Should match kSelectionHandleBarBottomAllowance in
36 // touch_selection_controller.
37 const int kBarBottomAllowance = 3;
39 // Should match kMenuButtonWidth in touch_editing_menu.
40 const int kMenuButtonWidth = 63;
42 // Should match size of kMenuCommands array in touch_editing_menu.
43 const int kMenuCommandCount = 3;
45 // For selection bounds |b1| and |b2| in a paragraph of text, returns -1 if |b1|
46 // is physically before |b2|, +1 if |b2| is before |b1|, and 0 if they are at
47 // the same location.
48 int CompareTextSelectionBounds(const ui::SelectionBound& b1,
49 const ui::SelectionBound& b2) {
50 if (b1.edge_top().y() < b2.edge_top().y() ||
51 b1.edge_top().x() < b2.edge_top().x()) {
52 return -1;
54 if (b1 == b2)
55 return 0;
56 return 1;
59 } // namespace
61 namespace views {
63 class TouchSelectionControllerImplTest : public ViewsTestBase {
64 public:
65 TouchSelectionControllerImplTest()
66 : textfield_widget_(nullptr),
67 widget_(nullptr),
68 textfield_(nullptr),
69 views_tsc_factory_(new ViewsTouchEditingControllerFactory) {
70 base::CommandLine::ForCurrentProcess()->AppendSwitch(
71 switches::kEnableTouchEditing);
72 ui::TouchEditingControllerFactory::SetInstance(views_tsc_factory_.get());
75 ~TouchSelectionControllerImplTest() override {
76 ui::TouchEditingControllerFactory::SetInstance(nullptr);
79 void SetUp() override {
80 ViewsTestBase::SetUp();
81 test_cursor_client_.reset(new aura::test::TestCursorClient(GetContext()));
84 void TearDown() override {
85 test_cursor_client_.reset();
86 if (textfield_widget_ && !textfield_widget_->IsClosed())
87 textfield_widget_->Close();
88 if (widget_ && !widget_->IsClosed())
89 widget_->Close();
90 ViewsTestBase::TearDown();
93 void CreateTextfield() {
94 textfield_ = new Textfield();
95 textfield_widget_ = new Widget;
96 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
97 params.bounds = gfx::Rect(0, 0, 200, 200);
98 textfield_widget_->Init(params);
99 View* container = new View();
100 textfield_widget_->SetContentsView(container);
101 container->AddChildView(textfield_);
103 textfield_->SetBoundsRect(gfx::Rect(0, 0, 200, 20));
104 textfield_->set_id(1);
105 textfield_widget_->Show();
107 textfield_->RequestFocus();
108 textfield_test_api_.reset(new TextfieldTestApi(textfield_));
111 void CreateWidget() {
112 widget_ = new Widget;
113 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
114 params.bounds = gfx::Rect(0, 0, 200, 200);
115 widget_->Init(params);
116 widget_->Show();
119 protected:
120 static bool IsCursorHandleVisibleFor(
121 ui::TouchEditingControllerDeprecated* controller) {
122 TouchSelectionControllerImpl* impl =
123 static_cast<TouchSelectionControllerImpl*>(controller);
124 return impl->IsCursorHandleVisible();
127 gfx::Rect GetCursorRect(const gfx::SelectionModel& sel) {
128 return textfield_test_api_->GetRenderText()->GetCursorBounds(sel, true);
131 gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) {
132 gfx::Rect cursor_bounds = GetCursorRect(sel);
133 return gfx::Point(cursor_bounds.x(), cursor_bounds.y());
136 TouchSelectionControllerImpl* GetSelectionController() {
137 return static_cast<TouchSelectionControllerImpl*>(
138 textfield_test_api_->touch_selection_controller());
141 void StartTouchEditing() {
142 textfield_test_api_->CreateTouchSelectionControllerAndNotifyIt();
145 void EndTouchEditing() {
146 textfield_test_api_->ResetTouchSelectionController();
149 void SimulateSelectionHandleDrag(gfx::Vector2d v, int selection_handle) {
150 TouchSelectionControllerImpl* controller = GetSelectionController();
151 views::WidgetDelegateView* handle = nullptr;
152 if (selection_handle == 1)
153 handle = controller->GetHandle1View();
154 else
155 handle = controller->GetHandle2View();
157 gfx::Point grip_location = gfx::Point(handle->size().width() / 2,
158 handle->size().height() / 2);
159 base::TimeDelta time_stamp = base::TimeDelta();
161 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_BEGIN);
162 ui::GestureEvent scroll_begin(
163 grip_location.x(), grip_location.y(), 0, time_stamp, details);
164 handle->OnGestureEvent(&scroll_begin);
166 test_cursor_client_->DisableMouseEvents();
168 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_UPDATE);
169 gfx::Point update_location = grip_location + v;
170 ui::GestureEvent scroll_update(
171 update_location.x(), update_location.y(), 0, time_stamp, details);
172 handle->OnGestureEvent(&scroll_update);
175 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_END);
176 ui::GestureEvent scroll_end(
177 grip_location.x(), grip_location.y(), 0, time_stamp, details);
178 handle->OnGestureEvent(&scroll_end);
180 test_cursor_client_->EnableMouseEvents();
183 gfx::NativeView GetCursorHandleNativeView() {
184 return GetSelectionController()->GetCursorHandleNativeView();
187 gfx::Rect GetSelectionHandle1Bounds() {
188 return GetSelectionController()->GetSelectionHandle1Bounds();
191 gfx::Rect GetSelectionHandle2Bounds() {
192 return GetSelectionController()->GetSelectionHandle2Bounds();
195 gfx::Rect GetCursorHandleBounds() {
196 return GetSelectionController()->GetCursorHandleBounds();
199 gfx::Rect GetExpectedHandleBounds(const ui::SelectionBound& bound) {
200 return GetSelectionController()->GetExpectedHandleBounds(bound);
203 bool IsSelectionHandle1Visible() {
204 return GetSelectionController()->IsSelectionHandle1Visible();
207 bool IsSelectionHandle2Visible() {
208 return GetSelectionController()->IsSelectionHandle2Visible();
211 bool IsCursorHandleVisible() {
212 return GetSelectionController()->IsCursorHandleVisible();
215 gfx::RenderText* GetRenderText() {
216 return textfield_test_api_->GetRenderText();
219 gfx::Point GetCursorHandleDragPoint() {
220 gfx::Rect rect = GetCursorHandleBounds();
221 const gfx::SelectionModel& sel = textfield_->GetSelectionModel();
222 int cursor_height = GetCursorRect(sel).height();
223 gfx::Point point = rect.CenterPoint();
224 point.Offset(0, cursor_height);
225 return point;
228 // If textfield has selection, this verifies that the selection handles
229 // are visible, at the correct positions (at the end points of selection), and
230 // (if |check_direction| is set to true), that they have the correct
231 // directionality.
232 // |cursor_at_selection_handle_1| is used to decide whether selection
233 // handle 1's position is matched against the start of selection or the end.
234 void VerifyHandlePositions(bool cursor_at_selection_handle_1,
235 bool check_direction,
236 const tracked_objects::Location& from_here) {
237 ui::SelectionBound anchor, focus;
238 textfield_->GetSelectionEndPoints(&anchor, &focus);
239 std::string from_str = from_here.ToString();
240 if (textfield_->HasSelection()) {
241 EXPECT_TRUE(IsSelectionHandle1Visible()) << from_str;
242 EXPECT_TRUE(IsSelectionHandle2Visible()) << from_str;
243 EXPECT_FALSE(IsCursorHandleVisible());
244 gfx::Rect sh1_bounds = GetSelectionHandle1Bounds();
245 gfx::Rect sh2_bounds = GetSelectionHandle2Bounds();
246 if (cursor_at_selection_handle_1) {
247 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(focus)) << from_str;
248 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(anchor)) << from_str;
249 } else {
250 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(anchor)) << from_str;
251 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(focus)) << from_str;
253 } else {
254 EXPECT_FALSE(IsSelectionHandle1Visible()) << from_str;
255 EXPECT_FALSE(IsSelectionHandle2Visible()) << from_str;
256 EXPECT_TRUE(IsCursorHandleVisible());
257 gfx::Rect cursor_bounds = GetCursorHandleBounds();
258 DCHECK(anchor == focus);
259 EXPECT_EQ(cursor_bounds, GetExpectedHandleBounds(anchor)) << from_str;
261 if (check_direction) {
262 if (CompareTextSelectionBounds(anchor, focus) < 0) {
263 EXPECT_EQ(ui::SelectionBound::LEFT, anchor.type()) << from_str;
264 EXPECT_EQ(ui::SelectionBound::RIGHT, focus.type()) << from_str;
265 } else if (CompareTextSelectionBounds(anchor, focus) > 0) {
266 EXPECT_EQ(ui::SelectionBound::LEFT, focus.type()) << from_str;
267 EXPECT_EQ(ui::SelectionBound::RIGHT, anchor.type()) << from_str;
268 } else {
269 EXPECT_EQ(ui::SelectionBound::CENTER, focus.type()) << from_str;
270 EXPECT_EQ(ui::SelectionBound::CENTER, anchor.type()) << from_str;
275 Widget* textfield_widget_;
276 Widget* widget_;
278 Textfield* textfield_;
279 scoped_ptr<TextfieldTestApi> textfield_test_api_;
280 scoped_ptr<ViewsTouchEditingControllerFactory> views_tsc_factory_;
281 scoped_ptr<aura::test::TestCursorClient> test_cursor_client_;
283 private:
284 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest);
287 // Tests that the selection handles are placed appropriately when selection in
288 // a Textfield changes.
289 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) {
290 CreateTextfield();
291 textfield_->SetText(ASCIIToUTF16("some text"));
292 // Tap the textfield to invoke touch selection.
293 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
294 details.set_tap_count(1);
295 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
296 textfield_->OnGestureEvent(&tap);
298 // Test selecting a range.
299 textfield_->SelectRange(gfx::Range(3, 7));
300 VerifyHandlePositions(false, true, FROM_HERE);
302 // Test selecting everything.
303 textfield_->SelectAll(false);
304 VerifyHandlePositions(false, true, FROM_HERE);
306 // Test with no selection.
307 textfield_->ClearSelection();
308 VerifyHandlePositions(false, true, FROM_HERE);
310 // Test with lost focus.
311 textfield_widget_->GetFocusManager()->ClearFocus();
312 EXPECT_FALSE(GetSelectionController());
314 // Test with focus re-gained.
315 textfield_widget_->GetFocusManager()->SetFocusedView(textfield_);
316 EXPECT_FALSE(GetSelectionController());
317 textfield_->OnGestureEvent(&tap);
318 VerifyHandlePositions(false, true, FROM_HERE);
321 // Tests that the selection handles are placed appropriately in bidi text.
322 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) {
323 CreateTextfield();
324 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
325 // Tap the textfield to invoke touch selection.
326 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
327 details.set_tap_count(1);
328 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
329 textfield_->OnGestureEvent(&tap);
331 // Test cursor at run boundary and with empty selection.
332 textfield_->SelectSelectionModel(
333 gfx::SelectionModel(3, gfx::CURSOR_BACKWARD));
334 VerifyHandlePositions(false, true, FROM_HERE);
336 // Test selection range inside one run and starts or ends at run boundary.
337 textfield_->SelectRange(gfx::Range(2, 3));
338 VerifyHandlePositions(false, true, FROM_HERE);
340 textfield_->SelectRange(gfx::Range(3, 2));
341 VerifyHandlePositions(false, true, FROM_HERE);
343 // TODO(mfomitchev): crbug.com/429705
344 // The correct behavior for handles in mixed ltr/rtl text line is not known,
345 // so passing false for |check_direction| in some of these tests.
346 textfield_->SelectRange(gfx::Range(3, 4));
347 VerifyHandlePositions(false, false, FROM_HERE);
349 textfield_->SelectRange(gfx::Range(4, 3));
350 VerifyHandlePositions(false, false, FROM_HERE);
352 textfield_->SelectRange(gfx::Range(3, 6));
353 VerifyHandlePositions(false, false, FROM_HERE);
355 textfield_->SelectRange(gfx::Range(6, 3));
356 VerifyHandlePositions(false, false, FROM_HERE);
358 // Test selection range accross runs.
359 textfield_->SelectRange(gfx::Range(0, 6));
360 VerifyHandlePositions(false, true, FROM_HERE);
362 textfield_->SelectRange(gfx::Range(6, 0));
363 VerifyHandlePositions(false, true, FROM_HERE);
365 textfield_->SelectRange(gfx::Range(1, 4));
366 VerifyHandlePositions(false, true, FROM_HERE);
368 textfield_->SelectRange(gfx::Range(4, 1));
369 VerifyHandlePositions(false, true, FROM_HERE);
372 // Tests if the SelectRect callback is called appropriately when selection
373 // handles are moved.
374 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) {
375 CreateTextfield();
376 textfield_->SetText(ASCIIToUTF16("textfield with selected text"));
377 // Tap the textfield to invoke touch selection.
378 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
379 details.set_tap_count(1);
380 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
381 textfield_->OnGestureEvent(&tap);
382 textfield_->SelectRange(gfx::Range(3, 7));
384 gfx::Point textfield_origin;
385 textfield_->ConvertPointToScreen(&textfield_origin);
387 EXPECT_EQ("tfie", UTF16ToUTF8(textfield_->GetSelectedText()));
388 VerifyHandlePositions(false, true, FROM_HERE);
390 // Drag selection handle 2 to right by 3 chars.
391 const gfx::FontList& font_list = textfield_->GetFontList();
392 int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list);
393 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
394 EXPECT_EQ("tfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
395 VerifyHandlePositions(false, true, FROM_HERE);
397 // Drag selection handle 1 to the left by a large amount (selection should
398 // just stick to the beginning of the textfield).
399 SimulateSelectionHandleDrag(gfx::Vector2d(-50, 0), 1);
400 EXPECT_EQ("textfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
401 VerifyHandlePositions(true, true, FROM_HERE);
403 // Drag selection handle 1 across selection handle 2.
404 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list);
405 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
406 EXPECT_EQ("with ", UTF16ToUTF8(textfield_->GetSelectedText()));
407 VerifyHandlePositions(true, true, FROM_HERE);
409 // Drag selection handle 2 across selection handle 1.
410 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list);
411 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
412 EXPECT_EQ("selected ", UTF16ToUTF8(textfield_->GetSelectedText()));
413 VerifyHandlePositions(false, true, FROM_HERE);
416 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) {
417 CreateTextfield();
418 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def"));
419 // Tap the textfield to invoke touch selection.
420 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
421 details.set_tap_count(1);
422 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
423 textfield_->OnGestureEvent(&tap);
425 // Select [c] from left to right.
426 textfield_->SelectRange(gfx::Range(2, 3));
427 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
428 VerifyHandlePositions(false, true, FROM_HERE);
430 // Drag selection handle 2 to right by 1 char.
431 const gfx::FontList& font_list = textfield_->GetFontList();
432 int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
433 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
434 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
435 VerifyHandlePositions(false, true, FROM_HERE);
437 // Drag selection handle 1 to left by 1 char.
438 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
439 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
440 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
441 VerifyHandlePositions(true, true, FROM_HERE);
443 // Select [c] from right to left.
444 textfield_->SelectRange(gfx::Range(3, 2));
445 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
446 VerifyHandlePositions(false, true, FROM_HERE);
448 // Drag selection handle 1 to right by 1 char.
449 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
450 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
451 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
452 VerifyHandlePositions(true, true, FROM_HERE);
454 // Drag selection handle 2 to left by 1 char.
455 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
456 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
457 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
458 VerifyHandlePositions(false, true, FROM_HERE);
460 // Select [\x5e1] from right to left.
461 textfield_->SelectRange(gfx::Range(3, 4));
462 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
463 // TODO(mfomitchev): crbug.com/429705
464 // The correct behavior for handles in mixed ltr/rtl text line is not known,
465 // so passing false for |check_direction| in some of these tests.
466 VerifyHandlePositions(false, false, FROM_HERE);
468 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click
469 right of 'D' and select [D] then move the left selection handle to left
470 by one character, it should select [ED], instead it selects [F].
471 Reason: click right of 'D' and left of 'h' return the same x-axis position,
472 pass this position to FindCursorPosition() returns index of 'h'. which
473 means the selection start changed from 3 to 6.
474 Need further investigation on whether this is a bug in Pango and how to
475 work around it.
476 // Drag selection handle 2 to left by 1 char.
477 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
478 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
479 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
480 VERIFY_HANDLE_POSITIONS(false);
483 // Drag selection handle 1 to right by 1 char.
484 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
485 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
486 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
487 VerifyHandlePositions(true, true, FROM_HERE);
489 // Select [\x5e1] from left to right.
490 textfield_->SelectRange(gfx::Range(4, 3));
491 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
492 VerifyHandlePositions(false, false, FROM_HERE);
494 /* TODO(xji): see detail of above commented out test case.
495 // Drag selection handle 1 to left by 1 char.
496 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
497 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
498 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
499 VERIFY_HANDLE_POSITIONS(true);
502 // Drag selection handle 2 to right by 1 char.
503 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
504 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
505 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
506 VerifyHandlePositions(false, true, FROM_HERE);
508 // Select [\x05r3] from right to left.
509 textfield_->SelectRange(gfx::Range(5, 6));
510 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
511 VerifyHandlePositions(false, false, FROM_HERE);
513 // Drag selection handle 2 to left by 1 char.
514 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
515 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
516 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
517 VerifyHandlePositions(false, true, FROM_HERE);
519 // Drag selection handle 1 to right by 1 char.
520 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
521 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
522 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
523 VerifyHandlePositions(true, true, FROM_HERE);
525 // Select [\x05r3] from left to right.
526 textfield_->SelectRange(gfx::Range(6, 5));
527 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
528 VerifyHandlePositions(false, false, FROM_HERE);
530 // Drag selection handle 1 to left by 1 char.
531 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
532 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
533 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
534 VerifyHandlePositions(true, true, FROM_HERE);
536 // Drag selection handle 2 to right by 1 char.
537 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
538 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
539 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
540 VerifyHandlePositions(false, false, FROM_HERE);
543 TEST_F(TouchSelectionControllerImplTest,
544 HiddenSelectionHandleRetainsCursorPosition) {
545 // Create a textfield with lots of text in it.
546 CreateTextfield();
547 std::string textfield_text("some text");
548 for (int i = 0; i < 10; ++i)
549 textfield_text += textfield_text;
550 textfield_->SetText(ASCIIToUTF16(textfield_text));
552 // Tap the textfield to invoke selection.
553 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
554 details.set_tap_count(1);
555 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
556 textfield_->OnGestureEvent(&tap);
558 // Select some text such that one handle is hidden.
559 textfield_->SelectRange(gfx::Range(10, textfield_text.length()));
561 // Check that one selection handle is hidden.
562 EXPECT_FALSE(IsSelectionHandle1Visible());
563 EXPECT_TRUE(IsSelectionHandle2Visible());
564 EXPECT_EQ(gfx::Range(10, textfield_text.length()),
565 textfield_->GetSelectedRange());
567 // Drag the visible handle around and make sure the selection end point of the
568 // invisible handle does not change.
569 size_t visible_handle_position = textfield_->GetSelectedRange().end();
570 for (int i = 0; i < 10; ++i) {
571 static const int drag_diff = -10;
572 SimulateSelectionHandleDrag(gfx::Vector2d(drag_diff, 0), 2);
573 // Make sure that the visible handle is being dragged.
574 EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end());
575 visible_handle_position = textfield_->GetSelectedRange().end();
576 EXPECT_EQ((size_t) 10, textfield_->GetSelectedRange().start());
580 TEST_F(TouchSelectionControllerImplTest,
581 DoubleTapInTextfieldWithCursorHandleShouldSelectText) {
582 CreateTextfield();
583 textfield_->SetText(ASCIIToUTF16("some text"));
584 ui::test::EventGenerator generator(
585 textfield_->GetWidget()->GetNativeView()->GetRootWindow());
587 // Tap the textfield to invoke touch selection.
588 generator.GestureTapAt(gfx::Point(10, 10));
590 // Cursor handle should be visible.
591 EXPECT_FALSE(textfield_->HasSelection());
592 VerifyHandlePositions(false, true, FROM_HERE);
594 // Double tap on the cursor handle position. We want to check that the cursor
595 // handle is not eating the event and that the event is falling through to the
596 // textfield.
597 gfx::Point cursor_pos = GetCursorHandleBounds().origin();
598 cursor_pos.Offset(GetCursorHandleBounds().width() / 2, 0);
599 generator.GestureTapAt(cursor_pos);
600 generator.GestureTapAt(cursor_pos);
601 EXPECT_TRUE(textfield_->HasSelection());
604 // A simple implementation of TouchEditable that allows faking cursor position
605 // inside its boundaries.
606 class TestTouchEditable : public ui::TouchEditable {
607 public:
608 explicit TestTouchEditable(aura::Window* window)
609 : window_(window) {
610 DCHECK(window);
613 void set_bounds(const gfx::Rect& bounds) {
614 bounds_ = bounds;
617 void set_cursor_rect(const gfx::Rect& cursor_rect) {
618 cursor_bound_.SetEdge(cursor_rect.origin(), cursor_rect.bottom_left());
619 cursor_bound_.set_type(ui::SelectionBound::Type::CENTER);
622 ~TestTouchEditable() override {}
624 private:
625 // Overridden from ui::TouchEditable.
626 void SelectRect(const gfx::Point& start, const gfx::Point& end) override {
627 NOTREACHED();
629 void MoveCaretTo(const gfx::Point& point) override { NOTREACHED(); }
630 void GetSelectionEndPoints(ui::SelectionBound* anchor,
631 ui::SelectionBound* focus) override {
632 *anchor = *focus = cursor_bound_;
634 gfx::Rect GetBounds() override { return gfx::Rect(bounds_.size()); }
635 gfx::NativeView GetNativeView() const override { return window_; }
636 void ConvertPointToScreen(gfx::Point* point) override {
637 aura::client::ScreenPositionClient* screen_position_client =
638 aura::client::GetScreenPositionClient(window_->GetRootWindow());
639 if (screen_position_client)
640 screen_position_client->ConvertPointToScreen(window_, point);
642 void ConvertPointFromScreen(gfx::Point* point) override {
643 aura::client::ScreenPositionClient* screen_position_client =
644 aura::client::GetScreenPositionClient(window_->GetRootWindow());
645 if (screen_position_client)
646 screen_position_client->ConvertPointFromScreen(window_, point);
648 bool DrawsHandles() override { return false; }
649 void OpenContextMenu(const gfx::Point& anchor) override { NOTREACHED(); }
650 void DestroyTouchSelection() override { NOTREACHED(); }
652 // Overridden from ui::SimpleMenuModel::Delegate.
653 bool IsCommandIdChecked(int command_id) const override {
654 NOTREACHED();
655 return false;
657 bool IsCommandIdEnabled(int command_id) const override {
658 NOTREACHED();
659 return false;
661 bool GetAcceleratorForCommandId(int command_id,
662 ui::Accelerator* accelerator) override {
663 NOTREACHED();
664 return false;
666 void ExecuteCommand(int command_id, int event_flags) override {
667 NOTREACHED();
670 aura::Window* window_;
672 // Boundaries of the client view.
673 gfx::Rect bounds_;
675 // Cursor position inside the client view.
676 //gfx::Rect cursor_rect_;
677 ui::SelectionBound cursor_bound_;
679 DISALLOW_COPY_AND_ASSIGN(TestTouchEditable);
682 // Tests if the touch editing handle is shown or hidden properly according to
683 // the cursor position relative to the client boundaries.
684 TEST_F(TouchSelectionControllerImplTest,
685 VisibilityOfHandleRegardingClientBounds) {
686 CreateWidget();
688 TestTouchEditable touch_editable(widget_->GetNativeView());
689 scoped_ptr<ui::TouchEditingControllerDeprecated> touch_selection_controller(
690 ui::TouchEditingControllerDeprecated::Create(&touch_editable));
692 touch_editable.set_bounds(gfx::Rect(0, 0, 100, 20));
694 // Put the cursor completely inside the client bounds. Handle should be
695 // visible.
696 touch_editable.set_cursor_rect(gfx::Rect(2, 0, 1, 20));
697 touch_selection_controller->SelectionChanged();
698 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
700 // Move the cursor up such that |kBarMinHeight| pixels are still in the client
701 // bounds. Handle should still be visible.
702 touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20, 1, 20));
703 touch_selection_controller->SelectionChanged();
704 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
706 // Move the cursor up such that less than |kBarMinHeight| pixels are in the
707 // client bounds. Handle should be hidden.
708 touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20 - 1, 1, 20));
709 touch_selection_controller->SelectionChanged();
710 EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
712 // Move the Cursor down such that |kBarBottomAllowance| pixels are out of the
713 // client bounds. Handle should be visible.
714 touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance, 1, 20));
715 touch_selection_controller->SelectionChanged();
716 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
718 // Move the cursor down such that more than |kBarBottomAllowance| pixels are
719 // out of the client bounds. Handle should be hidden.
720 touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance + 1, 1, 20));
721 touch_selection_controller->SelectionChanged();
722 EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
724 touch_selection_controller.reset();
727 TEST_F(TouchSelectionControllerImplTest, HandlesStackAboveParent) {
728 ui::EventTarget* root = GetContext();
729 ui::EventTargeter* targeter = root->GetEventTargeter();
731 // Create the first window containing a Views::Textfield.
732 CreateTextfield();
733 aura::Window* window1 = textfield_widget_->GetNativeView();
735 // Start touch editing, check that the handle is above the first window, and
736 // end touch editing.
737 StartTouchEditing();
738 gfx::Point test_point = GetCursorHandleDragPoint();
739 ui::MouseEvent test_event1(ui::ET_MOUSE_MOVED, test_point, test_point,
740 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
741 EXPECT_EQ(GetCursorHandleNativeView(),
742 targeter->FindTargetForEvent(root, &test_event1));
743 EndTouchEditing();
745 // Create the second (empty) window over the first one.
746 CreateWidget();
747 aura::Window* window2 = widget_->GetNativeView();
749 // Start touch editing (in the first window) and check that the handle is not
750 // above the second window.
751 StartTouchEditing();
752 ui::MouseEvent test_event2(ui::ET_MOUSE_MOVED, test_point, test_point,
753 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
754 EXPECT_EQ(window2, targeter->FindTargetForEvent(root, &test_event2));
756 // Move the first window to top and check that the handle is kept above the
757 // first window.
758 window1->GetRootWindow()->StackChildAtTop(window1);
759 ui::MouseEvent test_event3(ui::ET_MOUSE_MOVED, test_point, test_point,
760 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
761 EXPECT_EQ(GetCursorHandleNativeView(),
762 targeter->FindTargetForEvent(root, &test_event3));
765 // A simple implementation of TouchEditingMenuController that enables all
766 // available commands.
767 class TestTouchEditingMenuController : public TouchEditingMenuController {
768 public:
769 TestTouchEditingMenuController() {}
770 ~TestTouchEditingMenuController() override {}
772 // Overriden from TouchEditingMenuController.
773 bool IsCommandIdEnabled(int command_id) const override {
774 // Return true, since we want the menu to have all |kMenuCommandCount|
775 // available commands.
776 return true;
778 void ExecuteCommand(int command_id, int event_flags) override {
779 NOTREACHED();
781 void OpenContextMenu() override { NOTREACHED(); }
782 void OnMenuClosed(TouchEditingMenuView* menu) override {}
784 private:
785 DISALLOW_COPY_AND_ASSIGN(TestTouchEditingMenuController);
788 // Tests if anchor rect for touch editing quick menu is adjusted correctly based
789 // on the distance of handles.
790 TEST_F(TouchSelectionControllerImplTest, QuickMenuAdjustsAnchorRect) {
791 CreateWidget();
792 aura::Window* window = widget_->GetNativeView();
794 scoped_ptr<TestTouchEditingMenuController> quick_menu_controller(
795 new TestTouchEditingMenuController());
797 // Some arbitrary size for touch editing handle image.
798 gfx::Size handle_image_size(10, 10);
800 // Calculate the width of quick menu. In addition to |kMenuCommandCount|
801 // commands, there is an item for ellipsis.
802 int quick_menu_width = (kMenuCommandCount + 1) * kMenuButtonWidth +
803 kMenuCommandCount;
805 // Set anchor rect's width a bit smaller than the quick menu width plus handle
806 // image width and check that anchor rect's height is adjusted.
807 gfx::Rect anchor_rect(
808 0, 0, quick_menu_width + handle_image_size.width() - 10, 20);
809 TouchEditingMenuView* quick_menu(TouchEditingMenuView::Create(
810 quick_menu_controller.get(), anchor_rect, handle_image_size, window));
811 anchor_rect.Inset(0, 0, 0, -handle_image_size.height());
812 EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString());
814 // Set anchor rect's width a bit greater than the quick menu width plus handle
815 // image width and check that anchor rect's height is not adjusted.
816 anchor_rect =
817 gfx::Rect(0, 0, quick_menu_width + handle_image_size.width() + 10, 20);
818 quick_menu = TouchEditingMenuView::Create(
819 quick_menu_controller.get(), anchor_rect, handle_image_size, window);
820 EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString());
822 // Close the widget, hence quick menus, before quick menu controller goes out
823 // of scope.
824 widget_->CloseNow();
825 widget_ = nullptr;
828 TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) {
829 CreateTextfield();
830 EXPECT_FALSE(GetSelectionController());
832 ui::test::EventGenerator generator(
833 textfield_widget_->GetNativeView()->GetRootWindow());
835 generator.set_current_location(gfx::Point(5, 5));
836 RunPendingMessages();
838 // Start touch editing; then move mouse over the textfield and ensure it
839 // deactivates touch selection.
840 StartTouchEditing();
841 EXPECT_TRUE(GetSelectionController());
842 generator.MoveMouseTo(gfx::Point(5, 10));
843 RunPendingMessages();
844 EXPECT_FALSE(GetSelectionController());
846 generator.MoveMouseTo(gfx::Point(5, 50));
847 RunPendingMessages();
849 // Start touch editing; then move mouse out of the textfield, but inside the
850 // winow and ensure it deactivates touch selection.
851 StartTouchEditing();
852 EXPECT_TRUE(GetSelectionController());
853 generator.MoveMouseTo(gfx::Point(5, 55));
854 RunPendingMessages();
855 EXPECT_FALSE(GetSelectionController());
857 generator.MoveMouseTo(gfx::Point(5, 500));
858 RunPendingMessages();
860 // Start touch editing; then move mouse out of the textfield and window and
861 // ensure it deactivates touch selection.
862 StartTouchEditing();
863 EXPECT_TRUE(GetSelectionController());
864 generator.MoveMouseTo(5, 505);
865 RunPendingMessages();
866 EXPECT_FALSE(GetSelectionController());
869 TEST_F(TouchSelectionControllerImplTest, KeyEventDeactivatesTouchSelection) {
870 CreateTextfield();
871 EXPECT_FALSE(GetSelectionController());
873 ui::test::EventGenerator generator(
874 textfield_widget_->GetNativeView()->GetRootWindow());
876 RunPendingMessages();
878 // Start touch editing; then press a key and ensure it deactivates touch
879 // selection.
880 StartTouchEditing();
881 EXPECT_TRUE(GetSelectionController());
882 generator.PressKey(ui::VKEY_A, 0);
883 RunPendingMessages();
884 EXPECT_FALSE(GetSelectionController());
887 } // namespace views