Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / views / touchui / touch_selection_controller_impl_unittest.cc
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.
4
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/window.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/base/touch/touch_editing_controller.h"
11 #include "ui/base/ui_base_switches.h"
12 #include "ui/events/test/event_generator.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/point.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/render_text.h"
17 #include "ui/resources/grit/ui_resources.h"
18 #include "ui/views/controls/textfield/textfield.h"
19 #include "ui/views/controls/textfield/textfield_test_api.h"
20 #include "ui/views/test/views_test_base.h"
21 #include "ui/views/touchui/touch_selection_controller_impl.h"
22 #include "ui/views/views_touch_selection_controller_factory.h"
23 #include "ui/views/widget/widget.h"
24
25 using base::ASCIIToUTF16;
26 using base::UTF16ToUTF8;
27 using base::WideToUTF16;
28
29 namespace {
30 // Should match kSelectionHandlePadding in touch_selection_controller.
31 const int kPadding = 10;
32
33 // Should match kSelectionHandleBarMinHeight in touch_selection_controller.
34 const int kBarMinHeight = 5;
35
36 // Should match kSelectionHandleBarBottomAllowance in
37 // touch_selection_controller.
38 const int kBarBottomAllowance = 3;
39
40 // Should match kMenuButtonWidth in touch_editing_menu.
41 const int kMenuButtonWidth = 63;
42
43 // Should match size of kMenuCommands array in touch_editing_menu.
44 const int kMenuCommandCount = 3;
45
46 gfx::Image* GetHandleImage() {
47   static gfx::Image* handle_image = NULL;
48   if (!handle_image) {
49     handle_image = &ui::ResourceBundle::GetSharedInstance().GetImageNamed(
50         IDR_TEXT_SELECTION_HANDLE);
51   }
52   return handle_image;
53 }
54
55 gfx::Size GetHandleImageSize() {
56   return GetHandleImage()->Size();
57 }
58 }  // namespace
59
60 namespace views {
61
62 class TouchSelectionControllerImplTest : public ViewsTestBase {
63  public:
64   TouchSelectionControllerImplTest()
65       : textfield_widget_(NULL),
66         widget_(NULL),
67         textfield_(NULL),
68         views_tsc_factory_(new ViewsTouchSelectionControllerFactory) {
69     CommandLine::ForCurrentProcess()->AppendSwitch(
70         switches::kEnableTouchEditing);
71     ui::TouchSelectionControllerFactory::SetInstance(views_tsc_factory_.get());
72   }
73
74   ~TouchSelectionControllerImplTest() override {
75     ui::TouchSelectionControllerFactory::SetInstance(NULL);
76   }
77
78   void TearDown() override {
79     if (textfield_widget_ && !textfield_widget_->IsClosed())
80       textfield_widget_->Close();
81     if (widget_ && !widget_->IsClosed())
82       widget_->Close();
83     ViewsTestBase::TearDown();
84   }
85
86   void CreateTextfield() {
87     textfield_ = new Textfield();
88     textfield_widget_ = new Widget;
89     Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
90     params.bounds = gfx::Rect(0, 0, 200, 200);
91     textfield_widget_->Init(params);
92     View* container = new View();
93     textfield_widget_->SetContentsView(container);
94     container->AddChildView(textfield_);
95
96     textfield_->SetBoundsRect(gfx::Rect(0, 0, 200, 20));
97     textfield_->set_id(1);
98     textfield_widget_->Show();
99
100     textfield_->RequestFocus();
101
102     textfield_test_api_.reset(new TextfieldTestApi(textfield_));
103   }
104
105   void CreateWidget() {
106     widget_ = new Widget;
107     Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
108     params.bounds = gfx::Rect(0, 0, 200, 200);
109     widget_->Init(params);
110     widget_->Show();
111   }
112
113  protected:
114   static bool IsCursorHandleVisibleFor(
115       ui::TouchSelectionController* controller) {
116     TouchSelectionControllerImpl* impl =
117         static_cast<TouchSelectionControllerImpl*>(controller);
118     return impl->IsCursorHandleVisible();
119   }
120
121   gfx::Rect GetCursorRect(const gfx::SelectionModel& sel) {
122     return textfield_test_api_->GetRenderText()->GetCursorBounds(sel, true);
123   }
124
125   gfx::Point GetCursorPosition(const gfx::SelectionModel& sel) {
126     gfx::Rect cursor_bounds = GetCursorRect(sel);
127     return gfx::Point(cursor_bounds.x(), cursor_bounds.y());
128   }
129
130   TouchSelectionControllerImpl* GetSelectionController() {
131     return static_cast<TouchSelectionControllerImpl*>(
132         textfield_test_api_->touch_selection_controller());
133   }
134
135   void StartTouchEditing() {
136     textfield_test_api_->CreateTouchSelectionControllerAndNotifyIt();
137   }
138
139   void EndTouchEditing() {
140     textfield_test_api_->ResetTouchSelectionController();
141   }
142
143   void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) {
144     TouchSelectionControllerImpl* controller = GetSelectionController();
145     // Do the work of OnMousePressed().
146     if (selection_handle == 1)
147       controller->SetDraggingHandle(controller->selection_handle_1_.get());
148     else
149       controller->SetDraggingHandle(controller->selection_handle_2_.get());
150
151     // Offset the drag position by the selection handle radius since it is
152     // supposed to be in the coordinate system of the handle.
153     p.Offset(GetHandleImageSize().width() / 2 + kPadding, 0);
154     controller->SelectionHandleDragged(p);
155
156     // Do the work of OnMouseReleased().
157     controller->dragging_handle_ = NULL;
158   }
159
160   gfx::NativeView GetCursorHandleNativeView() {
161     return GetSelectionController()->GetCursorHandleNativeView();
162   }
163
164   gfx::Point GetSelectionHandle1Position() {
165     return GetSelectionController()->GetSelectionHandle1Position();
166   }
167
168   gfx::Point GetSelectionHandle2Position() {
169     return GetSelectionController()->GetSelectionHandle2Position();
170   }
171
172   gfx::Point GetCursorHandlePosition() {
173     return GetSelectionController()->GetCursorHandlePosition();
174   }
175
176   bool IsSelectionHandle1Visible() {
177     return GetSelectionController()->IsSelectionHandle1Visible();
178   }
179
180   bool IsSelectionHandle2Visible() {
181     return GetSelectionController()->IsSelectionHandle2Visible();
182   }
183
184   bool IsCursorHandleVisible() {
185     return GetSelectionController()->IsCursorHandleVisible();
186   }
187
188   gfx::RenderText* GetRenderText() {
189     return textfield_test_api_->GetRenderText();
190   }
191
192   gfx::Point GetCursorHandleDragPoint() {
193     gfx::Point point = GetCursorHandlePosition();
194     const gfx::SelectionModel& sel = textfield_->GetSelectionModel();
195     int cursor_height = GetCursorRect(sel).height();
196     point.Offset(GetHandleImageSize().width() / 2 + kPadding,
197                  GetHandleImageSize().height() / 2 + cursor_height);
198     return point;
199   }
200
201   Widget* textfield_widget_;
202   Widget* widget_;
203
204   Textfield* textfield_;
205   scoped_ptr<TextfieldTestApi> textfield_test_api_;
206   scoped_ptr<ViewsTouchSelectionControllerFactory> views_tsc_factory_;
207
208  private:
209   DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest);
210 };
211
212 // If textfield has selection, this macro verifies that the selection handles
213 // are visible and at the correct positions (at the end points of selection).
214 // |cursor_at_selection_handle_1| is used to decide whether selection
215 // handle 1's position is matched against the start of selection or the end.
216 #define VERIFY_HANDLE_POSITIONS(cursor_at_selection_handle_1)                  \
217 {                                                                              \
218     gfx::SelectionModel sel = textfield_->GetSelectionModel();                 \
219     if (textfield_->HasSelection()) {                                          \
220       EXPECT_TRUE(IsSelectionHandle1Visible());                                \
221       EXPECT_TRUE(IsSelectionHandle2Visible());                                \
222       EXPECT_FALSE(IsCursorHandleVisible());                                   \
223       gfx::SelectionModel sel_start = GetRenderText()->                        \
224                                       GetSelectionModelForSelectionStart();    \
225       gfx::Point selection_start = GetCursorPosition(sel_start);               \
226       gfx::Point selection_end = GetCursorPosition(sel);                       \
227       gfx::Point sh1 = GetSelectionHandle1Position();                          \
228       gfx::Point sh2 = GetSelectionHandle2Position();                          \
229       sh1.Offset(GetHandleImageSize().width() / 2 + kPadding, 0);              \
230       sh2.Offset(GetHandleImageSize().width() / 2 + kPadding, 0);              \
231       if (cursor_at_selection_handle_1) {                                      \
232         EXPECT_EQ(sh1, selection_end);                                         \
233         EXPECT_EQ(sh2, selection_start);                                       \
234       } else {                                                                 \
235         EXPECT_EQ(sh1, selection_start);                                       \
236         EXPECT_EQ(sh2, selection_end);                                         \
237       }                                                                        \
238     } else {                                                                   \
239       EXPECT_FALSE(IsSelectionHandle1Visible());                               \
240       EXPECT_FALSE(IsSelectionHandle2Visible());                               \
241       EXPECT_TRUE(IsCursorHandleVisible());                                    \
242       gfx::Point cursor_pos = GetCursorPosition(sel);                          \
243       gfx::Point ch_pos = GetCursorHandlePosition();                           \
244       ch_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0);           \
245       EXPECT_EQ(ch_pos, cursor_pos);                                           \
246     }                                                                          \
247 }
248
249 // Tests that the selection handles are placed appropriately when selection in
250 // a Textfield changes.
251 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) {
252   CreateTextfield();
253   textfield_->SetText(ASCIIToUTF16("some text"));
254   // Tap the textfield to invoke touch selection.
255   ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
256   details.set_tap_count(1);
257   ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
258   textfield_->OnGestureEvent(&tap);
259
260   // Test selecting a range.
261   textfield_->SelectRange(gfx::Range(3, 7));
262   VERIFY_HANDLE_POSITIONS(false);
263
264   // Test selecting everything.
265   textfield_->SelectAll(false);
266   VERIFY_HANDLE_POSITIONS(false);
267
268   // Test with no selection.
269   textfield_->ClearSelection();
270   VERIFY_HANDLE_POSITIONS(false);
271
272   // Test with lost focus.
273   textfield_widget_->GetFocusManager()->ClearFocus();
274   EXPECT_FALSE(GetSelectionController());
275
276   // Test with focus re-gained.
277   textfield_widget_->GetFocusManager()->SetFocusedView(textfield_);
278   EXPECT_FALSE(GetSelectionController());
279   textfield_->OnGestureEvent(&tap);
280   VERIFY_HANDLE_POSITIONS(false);
281 }
282
283 // Tests that the selection handles are placed appropriately in bidi text.
284 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) {
285   CreateTextfield();
286   textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
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);
292
293   // Test cursor at run boundary and with empty selection.
294   textfield_->SelectSelectionModel(
295       gfx::SelectionModel(3, gfx::CURSOR_BACKWARD));
296   VERIFY_HANDLE_POSITIONS(false);
297
298   // Test selection range inside one run and starts or ends at run boundary.
299   textfield_->SelectRange(gfx::Range(2, 3));
300   VERIFY_HANDLE_POSITIONS(false);
301
302   textfield_->SelectRange(gfx::Range(3, 2));
303   VERIFY_HANDLE_POSITIONS(false);
304
305   textfield_->SelectRange(gfx::Range(3, 4));
306   VERIFY_HANDLE_POSITIONS(false);
307
308   textfield_->SelectRange(gfx::Range(4, 3));
309   VERIFY_HANDLE_POSITIONS(false);
310
311   textfield_->SelectRange(gfx::Range(3, 6));
312   VERIFY_HANDLE_POSITIONS(false);
313
314   textfield_->SelectRange(gfx::Range(6, 3));
315   VERIFY_HANDLE_POSITIONS(false);
316
317   // Test selection range accross runs.
318   textfield_->SelectRange(gfx::Range(0, 6));
319   VERIFY_HANDLE_POSITIONS(false);
320
321   textfield_->SelectRange(gfx::Range(6, 0));
322   VERIFY_HANDLE_POSITIONS(false);
323
324   textfield_->SelectRange(gfx::Range(1, 4));
325   VERIFY_HANDLE_POSITIONS(false);
326
327   textfield_->SelectRange(gfx::Range(4, 1));
328   VERIFY_HANDLE_POSITIONS(false);
329 }
330
331 // Tests if the SelectRect callback is called appropriately when selection
332 // handles are moved.
333 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) {
334   CreateTextfield();
335   textfield_->SetText(ASCIIToUTF16("textfield with selected text"));
336   // Tap the textfield to invoke touch selection.
337   ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
338   details.set_tap_count(1);
339   ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
340   textfield_->OnGestureEvent(&tap);
341   textfield_->SelectRange(gfx::Range(3, 7));
342
343   EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie");
344   VERIFY_HANDLE_POSITIONS(false);
345
346   // Drag selection handle 2 to right by 3 chars.
347   const gfx::FontList& font_list = textfield_->GetFontList();
348   int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list);
349   SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
350   EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield ");
351   VERIFY_HANDLE_POSITIONS(false);
352
353   // Drag selection handle 1 to the left by a large amount (selection should
354   // just stick to the beginning of the textfield).
355   SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1);
356   EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield ");
357   VERIFY_HANDLE_POSITIONS(true);
358
359   // Drag selection handle 1 across selection handle 2.
360   x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list);
361   SimulateSelectionHandleDrag(gfx::Point(x, 0), 1);
362   EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with ");
363   VERIFY_HANDLE_POSITIONS(true);
364
365   // Drag selection handle 2 across selection handle 1.
366   x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list);
367   SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
368   EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected ");
369   VERIFY_HANDLE_POSITIONS(false);
370 }
371
372 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) {
373   CreateTextfield();
374   textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def"));
375   // Tap the textfield to invoke touch selection.
376   ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
377   details.set_tap_count(1);
378   ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
379   textfield_->OnGestureEvent(&tap);
380
381   // Select [c] from left to right.
382   textfield_->SelectRange(gfx::Range(2, 3));
383   EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
384   VERIFY_HANDLE_POSITIONS(false);
385
386   // Drag selection handle 2 to right by 1 char.
387   const gfx::FontList& font_list = textfield_->GetFontList();
388   int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
389   SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
390   EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
391   VERIFY_HANDLE_POSITIONS(false);
392
393   // Drag selection handle 1 to left by 1 char.
394   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
395   SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1);
396   EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
397   VERIFY_HANDLE_POSITIONS(true);
398
399   // Select [c] from right to left.
400   textfield_->SelectRange(gfx::Range(3, 2));
401   EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText());
402   VERIFY_HANDLE_POSITIONS(false);
403
404   // Drag selection handle 1 to right by 1 char.
405   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list);
406   SimulateSelectionHandleDrag(gfx::Point(x, 0), 1);
407   EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
408   VERIFY_HANDLE_POSITIONS(true);
409
410   // Drag selection handle 2 to left by 1 char.
411   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list);
412   SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2);
413   EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText());
414   VERIFY_HANDLE_POSITIONS(false);
415
416   // Select [\x5e1] from right to left.
417   textfield_->SelectRange(gfx::Range(3, 4));
418   EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
419   VERIFY_HANDLE_POSITIONS(false);
420
421   /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click
422      right of 'D' and select [D] then move the left selection handle to left
423      by one character, it should select [ED], instead it selects [F].
424      Reason: click right of 'D' and left of 'h' return the same x-axis position,
425      pass this position to FindCursorPosition() returns index of 'h'. which
426      means the selection start changed from 3 to 6.
427      Need further investigation on whether this is a bug in Pango and how to
428      work around it.
429   // Drag selection handle 2 to left by 1 char.
430   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
431   SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2);
432   EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
433   VERIFY_HANDLE_POSITIONS(false);
434   */
435
436   // Drag selection handle 1 to right by 1 char.
437   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
438   SimulateSelectionHandleDrag(gfx::Point(x, 0), 1);
439   EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
440   VERIFY_HANDLE_POSITIONS(true);
441
442   // Select [\x5e1] from left to right.
443   textfield_->SelectRange(gfx::Range(4, 3));
444   EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText());
445   VERIFY_HANDLE_POSITIONS(false);
446
447   /* TODO(xji): see detail of above commented out test case.
448   // Drag selection handle 1 to left by 1 char.
449   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
450   SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1);
451   EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText());
452   VERIFY_HANDLE_POSITIONS(true);
453   */
454
455   // Drag selection handle 2 to right by 1 char.
456   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list);
457   SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
458   EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText());
459   VERIFY_HANDLE_POSITIONS(false);
460
461   // Select [\x05r3] from right to left.
462   textfield_->SelectRange(gfx::Range(5, 6));
463   EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
464   VERIFY_HANDLE_POSITIONS(false);
465
466   // Drag selection handle 2 to left by 1 char.
467   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
468   SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2);
469   EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
470   VERIFY_HANDLE_POSITIONS(false);
471
472   // Drag selection handle 1 to right by 1 char.
473   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
474   SimulateSelectionHandleDrag(gfx::Point(x, 0), 1);
475   EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
476   VERIFY_HANDLE_POSITIONS(true);
477
478   // Select [\x05r3] from left to right.
479   textfield_->SelectRange(gfx::Range(6, 5));
480   EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText());
481   VERIFY_HANDLE_POSITIONS(false);
482
483   // Drag selection handle 1 to left by 1 char.
484   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list);
485   SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1);
486   EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText());
487   VERIFY_HANDLE_POSITIONS(true);
488
489   // Drag selection handle 2 to right by 1 char.
490   x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list);
491   SimulateSelectionHandleDrag(gfx::Point(x, 0), 2);
492   EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText());
493   VERIFY_HANDLE_POSITIONS(false);
494 }
495
496 TEST_F(TouchSelectionControllerImplTest,
497        HiddenSelectionHandleRetainsCursorPosition) {
498   // Create a textfield with lots of text in it.
499   CreateTextfield();
500   std::string textfield_text("some text");
501   for (int i = 0; i < 10; ++i)
502     textfield_text += textfield_text;
503   textfield_->SetText(ASCIIToUTF16(textfield_text));
504
505   // Tap the textfield to invoke selection.
506   ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
507   details.set_tap_count(1);
508   ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details);
509   textfield_->OnGestureEvent(&tap);
510
511   // Select some text such that one handle is hidden.
512   textfield_->SelectRange(gfx::Range(10, textfield_text.length()));
513
514   // Check that one selection handle is hidden.
515   EXPECT_FALSE(IsSelectionHandle1Visible());
516   EXPECT_TRUE(IsSelectionHandle2Visible());
517   EXPECT_EQ(gfx::Range(10, textfield_text.length()),
518             textfield_->GetSelectedRange());
519
520   // Drag the visible handle around and make sure the selection end point of the
521   // invisible handle does not change.
522   size_t visible_handle_position = textfield_->GetSelectedRange().end();
523   for (int i = 0; i < 10; ++i) {
524     SimulateSelectionHandleDrag(gfx::Point(-10, 0), 2);
525     // Make sure that the visible handle is being dragged.
526     EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end());
527     visible_handle_position = textfield_->GetSelectedRange().end();
528     EXPECT_EQ((size_t) 10, textfield_->GetSelectedRange().start());
529   }
530 }
531
532 TEST_F(TouchSelectionControllerImplTest,
533        DoubleTapInTextfieldWithCursorHandleShouldSelectText) {
534   CreateTextfield();
535   textfield_->SetText(ASCIIToUTF16("some text"));
536   ui::test::EventGenerator generator(
537       textfield_->GetWidget()->GetNativeView()->GetRootWindow());
538
539   // Tap the textfield to invoke touch selection.
540   generator.GestureTapAt(gfx::Point(10, 10));
541
542   // Cursor handle should be visible.
543   EXPECT_FALSE(textfield_->HasSelection());
544   VERIFY_HANDLE_POSITIONS(false);
545
546   // Double tap on the cursor handle position. We want to check that the cursor
547   // handle is not eating the event and that the event is falling through to the
548   // textfield.
549   gfx::Point cursor_pos = GetCursorHandlePosition();
550   cursor_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0);
551   generator.GestureTapAt(cursor_pos);
552   generator.GestureTapAt(cursor_pos);
553   EXPECT_TRUE(textfield_->HasSelection());
554 }
555
556 // A simple implementation of TouchEditable that allows faking cursor position
557 // inside its boundaries.
558 class TestTouchEditable : public ui::TouchEditable {
559  public:
560   explicit TestTouchEditable(aura::Window* window)
561       : window_(window) {
562     DCHECK(window);
563   }
564
565   void set_bounds(const gfx::Rect& bounds) {
566     bounds_ = bounds;
567   }
568
569   void set_cursor_rect(const gfx::Rect& cursor_rect) {
570     cursor_rect_ = cursor_rect;
571   }
572
573   ~TestTouchEditable() override {}
574
575  private:
576   // Overridden from ui::TouchEditable.
577   void SelectRect(const gfx::Point& start, const gfx::Point& end) override {
578     NOTREACHED();
579   }
580   void MoveCaretTo(const gfx::Point& point) override { NOTREACHED(); }
581   void GetSelectionEndPoints(gfx::Rect* p1, gfx::Rect* p2) override {
582     *p1 = *p2 = cursor_rect_;
583   }
584   gfx::Rect GetBounds() override { return gfx::Rect(bounds_.size()); }
585   gfx::NativeView GetNativeView() const override { return window_; }
586   void ConvertPointToScreen(gfx::Point* point) override {
587     aura::client::ScreenPositionClient* screen_position_client =
588         aura::client::GetScreenPositionClient(window_->GetRootWindow());
589     if (screen_position_client)
590       screen_position_client->ConvertPointToScreen(window_, point);
591   }
592   void ConvertPointFromScreen(gfx::Point* point) override {
593     aura::client::ScreenPositionClient* screen_position_client =
594         aura::client::GetScreenPositionClient(window_->GetRootWindow());
595     if (screen_position_client)
596       screen_position_client->ConvertPointFromScreen(window_, point);
597   }
598   bool DrawsHandles() override { return false; }
599   void OpenContextMenu(const gfx::Point& anchor) override { NOTREACHED(); }
600   void DestroyTouchSelection() override { NOTREACHED(); }
601
602   // Overridden from ui::SimpleMenuModel::Delegate.
603   bool IsCommandIdChecked(int command_id) const override {
604     NOTREACHED();
605     return false;
606   }
607   bool IsCommandIdEnabled(int command_id) const override {
608     NOTREACHED();
609     return false;
610   }
611   bool GetAcceleratorForCommandId(int command_id,
612                                   ui::Accelerator* accelerator) override {
613     NOTREACHED();
614     return false;
615   }
616   void ExecuteCommand(int command_id, int event_flags) override {
617     NOTREACHED();
618   }
619
620   aura::Window* window_;
621
622   // Boundaries of the client view.
623   gfx::Rect bounds_;
624
625   // Cursor position inside the client view.
626   gfx::Rect cursor_rect_;
627
628   DISALLOW_COPY_AND_ASSIGN(TestTouchEditable);
629 };
630
631 // Tests if the touch editing handle is shown or hidden properly according to
632 // the cursor position relative to the client boundaries.
633 TEST_F(TouchSelectionControllerImplTest,
634        VisibilityOfHandleRegardingClientBounds) {
635   CreateWidget();
636
637   TestTouchEditable touch_editable(widget_->GetNativeView());
638   scoped_ptr<ui::TouchSelectionController> touch_selection_controller(
639       ui::TouchSelectionController::create(&touch_editable));
640
641   touch_editable.set_bounds(gfx::Rect(0, 0, 100, 20));
642
643   // Put the cursor completely inside the client bounds. Handle should be
644   // visible.
645   touch_editable.set_cursor_rect(gfx::Rect(2, 0, 1, 20));
646   touch_selection_controller->SelectionChanged();
647   EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
648
649   // Move the cursor up such that |kBarMinHeight| pixels are still in the client
650   // bounds. Handle should still be visible.
651   touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20, 1, 20));
652   touch_selection_controller->SelectionChanged();
653   EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
654
655   // Move the cursor up such that less than |kBarMinHeight| pixels are in the
656   // client bounds. Handle should be hidden.
657   touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20 - 1, 1, 20));
658   touch_selection_controller->SelectionChanged();
659   EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
660
661   // Move the Cursor down such that |kBarBottomAllowance| pixels are out of the
662   // client bounds. Handle should be visible.
663   touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance, 1, 20));
664   touch_selection_controller->SelectionChanged();
665   EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
666
667   // Move the cursor down such that more than |kBarBottomAllowance| pixels are
668   // out of the client bounds. Handle should be hidden.
669   touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance + 1, 1, 20));
670   touch_selection_controller->SelectionChanged();
671   EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
672
673   touch_selection_controller.reset();
674 }
675
676 TEST_F(TouchSelectionControllerImplTest, HandlesStackAboveParent) {
677   ui::EventTarget* root = GetContext();
678   ui::EventTargeter* targeter = root->GetEventTargeter();
679
680   // Create the first window containing a Views::Textfield.
681   CreateTextfield();
682   aura::Window* window1 = textfield_widget_->GetNativeView();
683
684   // Start touch editing, check that the handle is above the first window, and
685   // end touch editing.
686   StartTouchEditing();
687   gfx::Point test_point = GetCursorHandleDragPoint();
688   ui::MouseEvent test_event1(ui::ET_MOUSE_MOVED, test_point, test_point,
689                              ui::EF_NONE, ui::EF_NONE);
690   EXPECT_EQ(GetCursorHandleNativeView(),
691             targeter->FindTargetForEvent(root, &test_event1));
692   EndTouchEditing();
693
694   // Create the second (empty) window over the first one.
695   CreateWidget();
696   aura::Window* window2 = widget_->GetNativeView();
697
698   // Start touch editing (in the first window) and check that the handle is not
699   // above the second window.
700   StartTouchEditing();
701   ui::MouseEvent test_event2(ui::ET_MOUSE_MOVED, test_point, test_point,
702                              ui::EF_NONE, ui::EF_NONE);
703   EXPECT_EQ(window2, targeter->FindTargetForEvent(root, &test_event2));
704
705   // Move the first window to top and check that the handle is kept above the
706   // first window.
707   window1->GetRootWindow()->StackChildAtTop(window1);
708   ui::MouseEvent test_event3(ui::ET_MOUSE_MOVED, test_point, test_point,
709                              ui::EF_NONE, ui::EF_NONE);
710   EXPECT_EQ(GetCursorHandleNativeView(),
711             targeter->FindTargetForEvent(root, &test_event3));
712 }
713
714 // A simple implementation of TouchEditingMenuController that enables all
715 // available commands.
716 class TestTouchEditingMenuController : public TouchEditingMenuController {
717  public:
718   TestTouchEditingMenuController() {}
719   ~TestTouchEditingMenuController() override {}
720
721   // Overriden from TouchEditingMenuController.
722   bool IsCommandIdEnabled(int command_id) const override {
723     // Return true, since we want the menu to have all |kMenuCommandCount|
724     // available commands.
725     return true;
726   }
727   void ExecuteCommand(int command_id, int event_flags) override {
728     NOTREACHED();
729   }
730   void OpenContextMenu() override { NOTREACHED(); }
731   void OnMenuClosed(TouchEditingMenuView* menu) override {}
732
733  private:
734   DISALLOW_COPY_AND_ASSIGN(TestTouchEditingMenuController);
735 };
736
737 // Tests if anchor rect for touch editing quick menu is adjusted correctly based
738 // on the distance of handles.
739 TEST_F(TouchSelectionControllerImplTest, QuickMenuAdjustsAnchorRect) {
740   CreateWidget();
741   aura::Window* window = widget_->GetNativeView();
742
743   scoped_ptr<TestTouchEditingMenuController> quick_menu_controller(
744       new TestTouchEditingMenuController());
745
746   // Some arbitrary size for touch editing handle image.
747   gfx::Size handle_image_size(10, 10);
748
749   // Calculate the width of quick menu. In addition to |kMenuCommandCount|
750   // commands, there is an item for ellipsis.
751   int quick_menu_width = (kMenuCommandCount + 1) * kMenuButtonWidth +
752                          kMenuCommandCount;
753
754   // Set anchor rect's width a bit smaller than the quick menu width plus handle
755   // image width and check that anchor rect's height is adjusted.
756   gfx::Rect anchor_rect(
757       0, 0, quick_menu_width + handle_image_size.width() - 10, 20);
758   TouchEditingMenuView* quick_menu(TouchEditingMenuView::Create(
759       quick_menu_controller.get(), anchor_rect, handle_image_size, window));
760   anchor_rect.Inset(0, 0, 0, -handle_image_size.height());
761   EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString());
762
763   // Set anchor rect's width a bit greater than the quick menu width plus handle
764   // image width and check that anchor rect's height is not adjusted.
765   anchor_rect =
766       gfx::Rect(0, 0, quick_menu_width + handle_image_size.width() + 10, 20);
767   quick_menu = TouchEditingMenuView::Create(
768       quick_menu_controller.get(), anchor_rect, handle_image_size, window);
769   EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString());
770
771   // Close the widget, hence quick menus, before quick menu controller goes out
772   // of scope.
773   widget_->CloseNow();
774   widget_ = NULL;
775 }
776
777 TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) {
778   CreateTextfield();
779   EXPECT_FALSE(GetSelectionController());
780
781   ui::test::EventGenerator generator(
782       textfield_widget_->GetNativeView()->GetRootWindow());
783
784   generator.set_current_location(gfx::Point(5, 5));
785   RunPendingMessages();
786
787   // Start touch editing; then move mouse over the textfield and ensure it
788   // deactivates touch selection.
789   StartTouchEditing();
790   EXPECT_TRUE(GetSelectionController());
791   generator.MoveMouseTo(gfx::Point(5, 10));
792   RunPendingMessages();
793   EXPECT_FALSE(GetSelectionController());
794
795   generator.MoveMouseTo(gfx::Point(5, 50));
796   RunPendingMessages();
797
798   // Start touch editing; then move mouse out of the textfield, but inside the
799   // winow and ensure it deactivates touch selection.
800   StartTouchEditing();
801   EXPECT_TRUE(GetSelectionController());
802   generator.MoveMouseTo(gfx::Point(5, 55));
803   RunPendingMessages();
804   EXPECT_FALSE(GetSelectionController());
805
806   generator.MoveMouseTo(gfx::Point(5, 500));
807   RunPendingMessages();
808
809   // Start touch editing; then move mouse out of the textfield and window and
810   // ensure it deactivates touch selection.
811   StartTouchEditing();
812   EXPECT_TRUE(GetSelectionController());
813   generator.MoveMouseTo(5, 505);
814   RunPendingMessages();
815   EXPECT_FALSE(GetSelectionController());
816 }
817
818 TEST_F(TouchSelectionControllerImplTest, KeyEventDeactivatesTouchSelection) {
819   CreateTextfield();
820   EXPECT_FALSE(GetSelectionController());
821
822   ui::test::EventGenerator generator(
823       textfield_widget_->GetNativeView()->GetRootWindow());
824
825   RunPendingMessages();
826
827   // Start touch editing; then press a key and ensure it deactivates touch
828   // selection.
829   StartTouchEditing();
830   EXPECT_TRUE(GetSelectionController());
831   generator.PressKey(ui::VKEY_A, 0);
832   RunPendingMessages();
833   EXPECT_FALSE(GetSelectionController());
834 }
835
836 }  // namespace views