Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / ui / views / touchui / touch_selection_controller_impl_unittest.cc
blob78de052c60350176ea65273ec836bc77b09b729b
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"
26 #include "ui/views/widget/widget_delegate.h"
28 using base::ASCIIToUTF16;
29 using base::UTF16ToUTF8;
30 using base::WideToUTF16;
32 namespace {
33 // Should match kSelectionHandleBarMinHeight in touch_selection_controller.
34 const int kBarMinHeight = 5;
36 // Should match kSelectionHandleBarBottomAllowance in
37 // touch_selection_controller.
38 const int kBarBottomAllowance = 3;
40 // For selection bounds |b1| and |b2| in a paragraph of text, returns -1 if |b1|
41 // is physically before |b2|, +1 if |b2| is before |b1|, and 0 if they are at
42 // the same location.
43 int CompareTextSelectionBounds(const ui::SelectionBound& b1,
44 const ui::SelectionBound& b2) {
45 if (b1.edge_top().y() < b2.edge_top().y() ||
46 b1.edge_top().x() < b2.edge_top().x()) {
47 return -1;
49 if (b1 == b2)
50 return 0;
51 return 1;
54 } // namespace
56 namespace views {
58 class TouchSelectionControllerImplTest : public ViewsTestBase {
59 public:
60 TouchSelectionControllerImplTest()
61 : textfield_widget_(nullptr),
62 widget_(nullptr),
63 textfield_(nullptr),
64 views_tsc_factory_(new ViewsTouchEditingControllerFactory) {
65 base::CommandLine::ForCurrentProcess()->AppendSwitch(
66 switches::kEnableTouchEditing);
67 ui::TouchEditingControllerFactory::SetInstance(views_tsc_factory_.get());
70 ~TouchSelectionControllerImplTest() override {
71 ui::TouchEditingControllerFactory::SetInstance(nullptr);
74 void SetUp() override {
75 ViewsTestBase::SetUp();
76 test_cursor_client_.reset(new aura::test::TestCursorClient(GetContext()));
79 void TearDown() override {
80 test_cursor_client_.reset();
81 if (textfield_widget_ && !textfield_widget_->IsClosed())
82 textfield_widget_->Close();
83 if (widget_ && !widget_->IsClosed())
84 widget_->Close();
85 ViewsTestBase::TearDown();
88 void CreateTextfield() {
89 textfield_ = new Textfield();
90 textfield_widget_ = new Widget;
91 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
92 params.bounds = gfx::Rect(0, 0, 200, 200);
93 textfield_widget_->Init(params);
94 View* container = new View();
95 textfield_widget_->SetContentsView(container);
96 container->AddChildView(textfield_);
98 textfield_->SetBoundsRect(gfx::Rect(0, 0, 200, 20));
99 textfield_->set_id(1);
100 textfield_widget_->Show();
102 textfield_->RequestFocus();
103 textfield_test_api_.reset(new TextfieldTestApi(textfield_));
106 void CreateWidget() {
107 widget_ = new Widget;
108 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
109 params.bounds = gfx::Rect(0, 0, 200, 200);
110 widget_->Init(params);
111 widget_->Show();
114 protected:
115 static bool IsCursorHandleVisibleFor(
116 ui::TouchEditingControllerDeprecated* controller) {
117 TouchSelectionControllerImpl* impl =
118 static_cast<TouchSelectionControllerImpl*>(controller);
119 return impl->IsCursorHandleVisible();
122 gfx::Rect GetCursorRect(const gfx::SelectionModel& sel) {
123 return textfield_test_api_->GetRenderText()->GetCursorBounds(sel, true);
126 gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) {
127 gfx::Rect cursor_bounds = GetCursorRect(sel);
128 return gfx::Point(cursor_bounds.x(), cursor_bounds.y());
131 TouchSelectionControllerImpl* GetSelectionController() {
132 return static_cast<TouchSelectionControllerImpl*>(
133 textfield_test_api_->touch_selection_controller());
136 void StartTouchEditing() {
137 textfield_test_api_->CreateTouchSelectionControllerAndNotifyIt();
140 void EndTouchEditing() {
141 textfield_test_api_->ResetTouchSelectionController();
144 void SimulateSelectionHandleDrag(gfx::Vector2d v, int selection_handle) {
145 TouchSelectionControllerImpl* controller = GetSelectionController();
146 views::WidgetDelegateView* handle = nullptr;
147 if (selection_handle == 1)
148 handle = controller->GetHandle1View();
149 else
150 handle = controller->GetHandle2View();
152 gfx::Point grip_location = gfx::Point(handle->size().width() / 2,
153 handle->size().height() / 2);
154 base::TimeDelta time_stamp = base::TimeDelta();
156 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_BEGIN);
157 ui::GestureEvent scroll_begin(
158 grip_location.x(), grip_location.y(), 0, time_stamp, details);
159 handle->OnGestureEvent(&scroll_begin);
161 test_cursor_client_->DisableMouseEvents();
163 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_UPDATE);
164 gfx::Point update_location = grip_location + v;
165 ui::GestureEvent scroll_update(
166 update_location.x(), update_location.y(), 0, time_stamp, details);
167 handle->OnGestureEvent(&scroll_update);
170 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_END);
171 ui::GestureEvent scroll_end(
172 grip_location.x(), grip_location.y(), 0, time_stamp, details);
173 handle->OnGestureEvent(&scroll_end);
175 test_cursor_client_->EnableMouseEvents();
178 gfx::NativeView GetCursorHandleNativeView() {
179 return GetSelectionController()->GetCursorHandleNativeView();
182 gfx::Rect GetSelectionHandle1Bounds() {
183 return GetSelectionController()->GetSelectionHandle1Bounds();
186 gfx::Rect GetSelectionHandle2Bounds() {
187 return GetSelectionController()->GetSelectionHandle2Bounds();
190 gfx::Rect GetCursorHandleBounds() {
191 return GetSelectionController()->GetCursorHandleBounds();
194 gfx::Rect GetExpectedHandleBounds(const ui::SelectionBound& bound) {
195 return GetSelectionController()->GetExpectedHandleBounds(bound);
198 bool IsSelectionHandle1Visible() {
199 return GetSelectionController()->IsSelectionHandle1Visible();
202 bool IsSelectionHandle2Visible() {
203 return GetSelectionController()->IsSelectionHandle2Visible();
206 bool IsCursorHandleVisible() {
207 return GetSelectionController()->IsCursorHandleVisible();
210 gfx::RenderText* GetRenderText() {
211 return textfield_test_api_->GetRenderText();
214 gfx::Point GetCursorHandleDragPoint() {
215 gfx::Rect rect = GetCursorHandleBounds();
216 const gfx::SelectionModel& sel = textfield_->GetSelectionModel();
217 int cursor_height = GetCursorRect(sel).height();
218 gfx::Point point = rect.CenterPoint();
219 point.Offset(0, cursor_height);
220 return point;
223 // If textfield has selection, this verifies that the selection handles
224 // are visible, at the correct positions (at the end points of selection), and
225 // (if |check_direction| is set to true), that they have the correct
226 // directionality.
227 // |cursor_at_selection_handle_1| is used to decide whether selection
228 // handle 1's position is matched against the start of selection or the end.
229 void VerifyHandlePositions(bool cursor_at_selection_handle_1,
230 bool check_direction,
231 const tracked_objects::Location& from_here) {
232 ui::SelectionBound anchor, focus;
233 textfield_->GetSelectionEndPoints(&anchor, &focus);
234 std::string from_str = from_here.ToString();
235 if (textfield_->HasSelection()) {
236 EXPECT_TRUE(IsSelectionHandle1Visible()) << from_str;
237 EXPECT_TRUE(IsSelectionHandle2Visible()) << from_str;
238 EXPECT_FALSE(IsCursorHandleVisible());
239 gfx::Rect sh1_bounds = GetSelectionHandle1Bounds();
240 gfx::Rect sh2_bounds = GetSelectionHandle2Bounds();
241 if (cursor_at_selection_handle_1) {
242 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(focus)) << from_str;
243 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(anchor)) << from_str;
244 } else {
245 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(anchor)) << from_str;
246 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(focus)) << from_str;
248 } else {
249 EXPECT_FALSE(IsSelectionHandle1Visible()) << from_str;
250 EXPECT_FALSE(IsSelectionHandle2Visible()) << from_str;
251 EXPECT_TRUE(IsCursorHandleVisible());
252 gfx::Rect cursor_bounds = GetCursorHandleBounds();
253 DCHECK(anchor == focus);
254 EXPECT_EQ(cursor_bounds, GetExpectedHandleBounds(anchor)) << from_str;
256 if (check_direction) {
257 if (CompareTextSelectionBounds(anchor, focus) < 0) {
258 EXPECT_EQ(ui::SelectionBound::LEFT, anchor.type()) << from_str;
259 EXPECT_EQ(ui::SelectionBound::RIGHT, focus.type()) << from_str;
260 } else if (CompareTextSelectionBounds(anchor, focus) > 0) {
261 EXPECT_EQ(ui::SelectionBound::LEFT, focus.type()) << from_str;
262 EXPECT_EQ(ui::SelectionBound::RIGHT, anchor.type()) << from_str;
263 } else {
264 EXPECT_EQ(ui::SelectionBound::CENTER, focus.type()) << from_str;
265 EXPECT_EQ(ui::SelectionBound::CENTER, anchor.type()) << from_str;
270 Widget* textfield_widget_;
271 Widget* widget_;
273 Textfield* textfield_;
274 scoped_ptr<TextfieldTestApi> textfield_test_api_;
275 scoped_ptr<ViewsTouchEditingControllerFactory> views_tsc_factory_;
276 scoped_ptr<aura::test::TestCursorClient> test_cursor_client_;
278 private:
279 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest);
282 // Tests that the selection handles are placed appropriately when selection in
283 // a Textfield changes.
284 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) {
285 CreateTextfield();
286 textfield_->SetText(ASCIIToUTF16("some text"));
287 // Tap the textfield to invoke touch selection.
288 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
289 details.set_tap_count(1);
290 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
291 textfield_->OnGestureEvent(&tap);
293 // Test selecting a range.
294 textfield_->SelectRange(gfx::Range(3, 7));
295 VerifyHandlePositions(false, true, FROM_HERE);
297 // Test selecting everything.
298 textfield_->SelectAll(false);
299 VerifyHandlePositions(false, true, FROM_HERE);
301 // Test with no selection.
302 textfield_->ClearSelection();
303 VerifyHandlePositions(false, true, FROM_HERE);
305 // Test with lost focus.
306 textfield_widget_->GetFocusManager()->ClearFocus();
307 EXPECT_FALSE(GetSelectionController());
309 // Test with focus re-gained.
310 textfield_widget_->GetFocusManager()->SetFocusedView(textfield_);
311 EXPECT_FALSE(GetSelectionController());
312 textfield_->OnGestureEvent(&tap);
313 VerifyHandlePositions(false, true, FROM_HERE);
316 // Tests that the selection handles are placed appropriately in bidi text.
317 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) {
318 CreateTextfield();
319 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
320 // Tap the textfield to invoke touch selection.
321 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
322 details.set_tap_count(1);
323 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
324 textfield_->OnGestureEvent(&tap);
326 // Test cursor at run boundary and with empty selection.
327 textfield_->SelectSelectionModel(
328 gfx::SelectionModel(3, gfx::CURSOR_BACKWARD));
329 VerifyHandlePositions(false, true, FROM_HERE);
331 // Test selection range inside one run and starts or ends at run boundary.
332 textfield_->SelectRange(gfx::Range(2, 3));
333 VerifyHandlePositions(false, true, FROM_HERE);
335 textfield_->SelectRange(gfx::Range(3, 2));
336 VerifyHandlePositions(false, true, FROM_HERE);
338 // TODO(mfomitchev): crbug.com/429705
339 // The correct behavior for handles in mixed ltr/rtl text line is not known,
340 // so passing false for |check_direction| in some of these tests.
341 textfield_->SelectRange(gfx::Range(3, 4));
342 VerifyHandlePositions(false, false, FROM_HERE);
344 textfield_->SelectRange(gfx::Range(4, 3));
345 VerifyHandlePositions(false, false, FROM_HERE);
347 textfield_->SelectRange(gfx::Range(3, 6));
348 VerifyHandlePositions(false, false, FROM_HERE);
350 textfield_->SelectRange(gfx::Range(6, 3));
351 VerifyHandlePositions(false, false, FROM_HERE);
353 // Test selection range accross runs.
354 textfield_->SelectRange(gfx::Range(0, 6));
355 VerifyHandlePositions(false, true, FROM_HERE);
357 textfield_->SelectRange(gfx::Range(6, 0));
358 VerifyHandlePositions(false, true, FROM_HERE);
360 textfield_->SelectRange(gfx::Range(1, 4));
361 VerifyHandlePositions(false, true, FROM_HERE);
363 textfield_->SelectRange(gfx::Range(4, 1));
364 VerifyHandlePositions(false, true, FROM_HERE);
367 // Tests if the SelectRect callback is called appropriately when selection
368 // handles are moved.
369 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) {
370 CreateTextfield();
371 textfield_->SetText(ASCIIToUTF16("textfield with selected text"));
372 // Tap the textfield to invoke touch selection.
373 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
374 details.set_tap_count(1);
375 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
376 textfield_->OnGestureEvent(&tap);
377 textfield_->SelectRange(gfx::Range(3, 7));
379 gfx::Point textfield_origin;
380 textfield_->ConvertPointToScreen(&textfield_origin);
382 EXPECT_EQ("tfie", UTF16ToUTF8(textfield_->GetSelectedText()));
383 VerifyHandlePositions(false, true, FROM_HERE);
385 // Drag selection handle 2 to right by 3 chars.
386 const gfx::FontList& font_list = textfield_->GetFontList();
387 int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list);
388 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
389 EXPECT_EQ("tfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
390 VerifyHandlePositions(false, true, FROM_HERE);
392 // Drag selection handle 1 to the left by a large amount (selection should
393 // just stick to the beginning of the textfield).
394 SimulateSelectionHandleDrag(gfx::Vector2d(-50, 0), 1);
395 EXPECT_EQ("textfield ", UTF16ToUTF8(textfield_->GetSelectedText()));
396 VerifyHandlePositions(true, true, FROM_HERE);
398 // Drag selection handle 1 across selection handle 2.
399 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list);
400 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
401 EXPECT_EQ("with ", UTF16ToUTF8(textfield_->GetSelectedText()));
402 VerifyHandlePositions(true, true, FROM_HERE);
404 // Drag selection handle 2 across selection handle 1.
405 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list);
406 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
407 EXPECT_EQ("selected ", UTF16ToUTF8(textfield_->GetSelectedText()));
408 VerifyHandlePositions(false, true, FROM_HERE);
411 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) {
412 CreateTextfield();
413 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def"));
414 // Tap the textfield to invoke touch selection.
415 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
416 details.set_tap_count(1);
417 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
418 textfield_->OnGestureEvent(&tap);
420 // Select [c] from left to right.
421 textfield_->SelectRange(gfx::Range(2, 3));
422 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
423 VerifyHandlePositions(false, true, FROM_HERE);
425 // Drag selection handle 2 to right by 1 char.
426 const gfx::FontList& font_list = textfield_->GetFontList();
427 int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
428 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
429 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
430 VerifyHandlePositions(false, true, FROM_HERE);
432 // Drag selection handle 1 to left by 1 char.
433 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
434 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
435 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
436 VerifyHandlePositions(true, true, FROM_HERE);
438 // Select [c] from right to left.
439 textfield_->SelectRange(gfx::Range(3, 2));
440 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
441 VerifyHandlePositions(false, true, FROM_HERE);
443 // Drag selection handle 1 to right by 1 char.
444 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
445 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
446 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
447 VerifyHandlePositions(true, true, FROM_HERE);
449 // Drag selection handle 2 to left by 1 char.
450 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
451 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
452 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
453 VerifyHandlePositions(false, true, FROM_HERE);
455 // Select [\x5e1] from right to left.
456 textfield_->SelectRange(gfx::Range(3, 4));
457 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
458 // TODO(mfomitchev): crbug.com/429705
459 // The correct behavior for handles in mixed ltr/rtl text line is not known,
460 // so passing false for |check_direction| in some of these tests.
461 VerifyHandlePositions(false, false, FROM_HERE);
463 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click
464 right of 'D' and select [D] then move the left selection handle to left
465 by one character, it should select [ED], instead it selects [F].
466 Reason: click right of 'D' and left of 'h' return the same x-axis position,
467 pass this position to FindCursorPosition() returns index of 'h'. which
468 means the selection start changed from 3 to 6.
469 Need further investigation on whether this is a bug in Pango and how to
470 work around it.
471 // Drag selection handle 2 to left by 1 char.
472 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
473 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
474 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
475 VERIFY_HANDLE_POSITIONS(false);
478 // Drag selection handle 1 to right by 1 char.
479 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
480 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
481 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
482 VerifyHandlePositions(true, true, FROM_HERE);
484 // Select [\x5e1] from left to right.
485 textfield_->SelectRange(gfx::Range(4, 3));
486 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
487 VerifyHandlePositions(false, false, FROM_HERE);
489 /* TODO(xji): see detail of above commented out test case.
490 // Drag selection handle 1 to left by 1 char.
491 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
492 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
493 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
494 VERIFY_HANDLE_POSITIONS(true);
497 // Drag selection handle 2 to right by 1 char.
498 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
499 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
500 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
501 VerifyHandlePositions(false, true, FROM_HERE);
503 // Select [\x05r3] from right to left.
504 textfield_->SelectRange(gfx::Range(5, 6));
505 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
506 VerifyHandlePositions(false, false, FROM_HERE);
508 // Drag selection handle 2 to left by 1 char.
509 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
510 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2);
511 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
512 VerifyHandlePositions(false, true, FROM_HERE);
514 // Drag selection handle 1 to right by 1 char.
515 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
516 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1);
517 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
518 VerifyHandlePositions(true, true, FROM_HERE);
520 // Select [\x05r3] from left to right.
521 textfield_->SelectRange(gfx::Range(6, 5));
522 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
523 VerifyHandlePositions(false, false, FROM_HERE);
525 // Drag selection handle 1 to left by 1 char.
526 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
527 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1);
528 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
529 VerifyHandlePositions(true, true, FROM_HERE);
531 // Drag selection handle 2 to right by 1 char.
532 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
533 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2);
534 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
535 VerifyHandlePositions(false, false, FROM_HERE);
538 TEST_F(TouchSelectionControllerImplTest,
539 HiddenSelectionHandleRetainsCursorPosition) {
540 // Create a textfield with lots of text in it.
541 CreateTextfield();
542 std::string textfield_text("some text");
543 for (int i = 0; i < 10; ++i)
544 textfield_text += textfield_text;
545 textfield_->SetText(ASCIIToUTF16(textfield_text));
547 // Tap the textfield to invoke selection.
548 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
549 details.set_tap_count(1);
550 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
551 textfield_->OnGestureEvent(&tap);
553 // Select some text such that one handle is hidden.
554 textfield_->SelectRange(gfx::Range(10, textfield_text.length()));
556 // Check that one selection handle is hidden.
557 EXPECT_FALSE(IsSelectionHandle1Visible());
558 EXPECT_TRUE(IsSelectionHandle2Visible());
559 EXPECT_EQ(gfx::Range(10, textfield_text.length()),
560 textfield_->GetSelectedRange());
562 // Drag the visible handle around and make sure the selection end point of the
563 // invisible handle does not change.
564 size_t visible_handle_position = textfield_->GetSelectedRange().end();
565 for (int i = 0; i < 10; ++i) {
566 static const int drag_diff = -10;
567 SimulateSelectionHandleDrag(gfx::Vector2d(drag_diff, 0), 2);
568 // Make sure that the visible handle is being dragged.
569 EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end());
570 visible_handle_position = textfield_->GetSelectedRange().end();
571 EXPECT_EQ((size_t) 10, textfield_->GetSelectedRange().start());
575 TEST_F(TouchSelectionControllerImplTest,
576 DoubleTapInTextfieldWithCursorHandleShouldSelectText) {
577 CreateTextfield();
578 textfield_->SetText(ASCIIToUTF16("some text"));
579 ui::test::EventGenerator generator(
580 textfield_->GetWidget()->GetNativeView()->GetRootWindow());
582 // Tap the textfield to invoke touch selection.
583 generator.GestureTapAt(gfx::Point(10, 10));
585 // Cursor handle should be visible.
586 EXPECT_FALSE(textfield_->HasSelection());
587 VerifyHandlePositions(false, true, FROM_HERE);
589 // Double tap on the cursor handle position. We want to check that the cursor
590 // handle is not eating the event and that the event is falling through to the
591 // textfield.
592 gfx::Point cursor_pos = GetCursorHandleBounds().origin();
593 cursor_pos.Offset(GetCursorHandleBounds().width() / 2, 0);
594 generator.GestureTapAt(cursor_pos);
595 generator.GestureTapAt(cursor_pos);
596 EXPECT_TRUE(textfield_->HasSelection());
599 // A simple implementation of TouchEditable that allows faking cursor position
600 // inside its boundaries.
601 class TestTouchEditable : public ui::TouchEditable {
602 public:
603 explicit TestTouchEditable(aura::Window* window)
604 : window_(window) {
605 DCHECK(window);
608 void set_bounds(const gfx::Rect& bounds) {
609 bounds_ = bounds;
612 void set_cursor_rect(const gfx::Rect& cursor_rect) {
613 cursor_bound_.SetEdge(cursor_rect.origin(), cursor_rect.bottom_left());
614 cursor_bound_.set_type(ui::SelectionBound::Type::CENTER);
617 ~TestTouchEditable() override {}
619 private:
620 // Overridden from ui::TouchEditable.
621 void SelectRect(const gfx::Point& start, const gfx::Point& end) override {
622 NOTREACHED();
624 void MoveCaretTo(const gfx::Point& point) override { NOTREACHED(); }
625 void GetSelectionEndPoints(ui::SelectionBound* anchor,
626 ui::SelectionBound* focus) override {
627 *anchor = *focus = cursor_bound_;
629 gfx::Rect GetBounds() override { return gfx::Rect(bounds_.size()); }
630 gfx::NativeView GetNativeView() const override { return window_; }
631 void ConvertPointToScreen(gfx::Point* point) override {
632 aura::client::ScreenPositionClient* screen_position_client =
633 aura::client::GetScreenPositionClient(window_->GetRootWindow());
634 if (screen_position_client)
635 screen_position_client->ConvertPointToScreen(window_, point);
637 void ConvertPointFromScreen(gfx::Point* point) override {
638 aura::client::ScreenPositionClient* screen_position_client =
639 aura::client::GetScreenPositionClient(window_->GetRootWindow());
640 if (screen_position_client)
641 screen_position_client->ConvertPointFromScreen(window_, point);
643 bool DrawsHandles() override { return false; }
644 void OpenContextMenu(const gfx::Point& anchor) override { NOTREACHED(); }
645 void DestroyTouchSelection() override { NOTREACHED(); }
647 // Overridden from ui::SimpleMenuModel::Delegate.
648 bool IsCommandIdChecked(int command_id) const override {
649 NOTREACHED();
650 return false;
652 bool IsCommandIdEnabled(int command_id) const override {
653 NOTREACHED();
654 return false;
656 bool GetAcceleratorForCommandId(int command_id,
657 ui::Accelerator* accelerator) override {
658 NOTREACHED();
659 return false;
661 void ExecuteCommand(int command_id, int event_flags) override {
662 NOTREACHED();
665 aura::Window* window_;
667 // Boundaries of the client view.
668 gfx::Rect bounds_;
670 // Cursor position inside the client view.
671 //gfx::Rect cursor_rect_;
672 ui::SelectionBound cursor_bound_;
674 DISALLOW_COPY_AND_ASSIGN(TestTouchEditable);
677 // Tests if the touch editing handle is shown or hidden properly according to
678 // the cursor position relative to the client boundaries.
679 TEST_F(TouchSelectionControllerImplTest,
680 VisibilityOfHandleRegardingClientBounds) {
681 CreateWidget();
683 TestTouchEditable touch_editable(widget_->GetNativeView());
684 scoped_ptr<ui::TouchEditingControllerDeprecated> touch_selection_controller(
685 ui::TouchEditingControllerDeprecated::Create(&touch_editable));
687 touch_editable.set_bounds(gfx::Rect(0, 0, 100, 20));
689 // Put the cursor completely inside the client bounds. Handle should be
690 // visible.
691 touch_editable.set_cursor_rect(gfx::Rect(2, 0, 1, 20));
692 touch_selection_controller->SelectionChanged();
693 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
695 // Move the cursor up such that |kBarMinHeight| pixels are still in the client
696 // bounds. Handle should still be visible.
697 touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20, 1, 20));
698 touch_selection_controller->SelectionChanged();
699 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
701 // Move the cursor up such that less than |kBarMinHeight| pixels are in the
702 // client bounds. Handle should be hidden.
703 touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20 - 1, 1, 20));
704 touch_selection_controller->SelectionChanged();
705 EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
707 // Move the Cursor down such that |kBarBottomAllowance| pixels are out of the
708 // client bounds. Handle should be visible.
709 touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance, 1, 20));
710 touch_selection_controller->SelectionChanged();
711 EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
713 // Move the cursor down such that more than |kBarBottomAllowance| pixels are
714 // out of the client bounds. Handle should be hidden.
715 touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance + 1, 1, 20));
716 touch_selection_controller->SelectionChanged();
717 EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
719 touch_selection_controller.reset();
722 TEST_F(TouchSelectionControllerImplTest, HandlesStackAboveParent) {
723 ui::EventTarget* root = GetContext();
724 ui::EventTargeter* targeter = root->GetEventTargeter();
726 // Create the first window containing a Views::Textfield.
727 CreateTextfield();
728 aura::Window* window1 = textfield_widget_->GetNativeView();
730 // Start touch editing, check that the handle is above the first window, and
731 // end touch editing.
732 StartTouchEditing();
733 gfx::Point test_point = GetCursorHandleDragPoint();
734 ui::MouseEvent test_event1(ui::ET_MOUSE_MOVED, test_point, test_point,
735 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
736 EXPECT_EQ(GetCursorHandleNativeView(),
737 targeter->FindTargetForEvent(root, &test_event1));
738 EndTouchEditing();
740 // Create the second (empty) window over the first one.
741 CreateWidget();
742 aura::Window* window2 = widget_->GetNativeView();
744 // Start touch editing (in the first window) and check that the handle is not
745 // above the second window.
746 StartTouchEditing();
747 ui::MouseEvent test_event2(ui::ET_MOUSE_MOVED, test_point, test_point,
748 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
749 EXPECT_EQ(window2, targeter->FindTargetForEvent(root, &test_event2));
751 // Move the first window to top and check that the handle is kept above the
752 // first window.
753 window1->GetRootWindow()->StackChildAtTop(window1);
754 ui::MouseEvent test_event3(ui::ET_MOUSE_MOVED, test_point, test_point,
755 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
756 EXPECT_EQ(GetCursorHandleNativeView(),
757 targeter->FindTargetForEvent(root, &test_event3));
760 TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) {
761 CreateTextfield();
762 EXPECT_FALSE(GetSelectionController());
764 ui::test::EventGenerator generator(
765 textfield_widget_->GetNativeView()->GetRootWindow());
767 generator.set_current_location(gfx::Point(5, 5));
768 RunPendingMessages();
770 // Start touch editing; then move mouse over the textfield and ensure it
771 // deactivates touch selection.
772 StartTouchEditing();
773 EXPECT_TRUE(GetSelectionController());
774 generator.MoveMouseTo(gfx::Point(5, 10));
775 RunPendingMessages();
776 EXPECT_FALSE(GetSelectionController());
778 generator.MoveMouseTo(gfx::Point(5, 50));
779 RunPendingMessages();
781 // Start touch editing; then move mouse out of the textfield, but inside the
782 // winow and ensure it deactivates touch selection.
783 StartTouchEditing();
784 EXPECT_TRUE(GetSelectionController());
785 generator.MoveMouseTo(gfx::Point(5, 55));
786 RunPendingMessages();
787 EXPECT_FALSE(GetSelectionController());
789 generator.MoveMouseTo(gfx::Point(5, 500));
790 RunPendingMessages();
792 // Start touch editing; then move mouse out of the textfield and window and
793 // ensure it deactivates touch selection.
794 StartTouchEditing();
795 EXPECT_TRUE(GetSelectionController());
796 generator.MoveMouseTo(5, 505);
797 RunPendingMessages();
798 EXPECT_FALSE(GetSelectionController());
801 TEST_F(TouchSelectionControllerImplTest, KeyEventDeactivatesTouchSelection) {
802 CreateTextfield();
803 EXPECT_FALSE(GetSelectionController());
805 ui::test::EventGenerator generator(
806 textfield_widget_->GetNativeView()->GetRootWindow());
808 RunPendingMessages();
810 // Start touch editing; then press a key and ensure it deactivates touch
811 // selection.
812 StartTouchEditing();
813 EXPECT_TRUE(GetSelectionController());
814 generator.PressKey(ui::VKEY_A, 0);
815 RunPendingMessages();
816 EXPECT_FALSE(GetSelectionController());
819 } // namespace views