Home | History | Annotate | Download | only in touchui
      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   virtual ~TouchSelectionControllerImplTest() {
     75     ui::TouchSelectionControllerFactory::SetInstance(NULL);
     76   }
     77 
     78   virtual void TearDown() {
     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   virtual ~TestTouchEditable() {}
    574 
    575  private:
    576   // Overridden from ui::TouchEditable.
    577   virtual void SelectRect(
    578       const gfx::Point& start, const gfx::Point& end) OVERRIDE {
    579     NOTREACHED();
    580   }
    581   virtual void MoveCaretTo(const gfx::Point& point) OVERRIDE {
    582     NOTREACHED();
    583   }
    584   virtual void GetSelectionEndPoints(gfx::Rect* p1, gfx::Rect* p2) OVERRIDE {
    585     *p1 = *p2 = cursor_rect_;
    586   }
    587   virtual gfx::Rect GetBounds() OVERRIDE {
    588     return gfx::Rect(bounds_.size());
    589   }
    590   virtual gfx::NativeView GetNativeView() const OVERRIDE {
    591     return window_;
    592   }
    593   virtual void ConvertPointToScreen(gfx::Point* point) OVERRIDE {
    594     aura::client::ScreenPositionClient* screen_position_client =
    595         aura::client::GetScreenPositionClient(window_->GetRootWindow());
    596     if (screen_position_client)
    597       screen_position_client->ConvertPointToScreen(window_, point);
    598   }
    599   virtual void ConvertPointFromScreen(gfx::Point* point) OVERRIDE {
    600     aura::client::ScreenPositionClient* screen_position_client =
    601         aura::client::GetScreenPositionClient(window_->GetRootWindow());
    602     if (screen_position_client)
    603       screen_position_client->ConvertPointFromScreen(window_, point);
    604   }
    605   virtual bool DrawsHandles() OVERRIDE {
    606     return false;
    607   }
    608   virtual void OpenContextMenu(const gfx::Point& anchor) OVERRIDE {
    609     NOTREACHED();
    610   }
    611   virtual void DestroyTouchSelection() OVERRIDE {
    612     NOTREACHED();
    613   }
    614 
    615   // Overridden from ui::SimpleMenuModel::Delegate.
    616   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE {
    617     NOTREACHED();
    618     return false;
    619   }
    620   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
    621     NOTREACHED();
    622     return false;
    623   }
    624   virtual bool GetAcceleratorForCommandId(
    625       int command_id,
    626       ui::Accelerator* accelerator) OVERRIDE {
    627     NOTREACHED();
    628     return false;
    629   }
    630   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {
    631     NOTREACHED();
    632   }
    633 
    634   aura::Window* window_;
    635 
    636   // Boundaries of the client view.
    637   gfx::Rect bounds_;
    638 
    639   // Cursor position inside the client view.
    640   gfx::Rect cursor_rect_;
    641 
    642   DISALLOW_COPY_AND_ASSIGN(TestTouchEditable);
    643 };
    644 
    645 // Tests if the touch editing handle is shown or hidden properly according to
    646 // the cursor position relative to the client boundaries.
    647 TEST_F(TouchSelectionControllerImplTest,
    648        VisibilityOfHandleRegardingClientBounds) {
    649   CreateWidget();
    650 
    651   TestTouchEditable touch_editable(widget_->GetNativeView());
    652   scoped_ptr<ui::TouchSelectionController> touch_selection_controller(
    653       ui::TouchSelectionController::create(&touch_editable));
    654 
    655   touch_editable.set_bounds(gfx::Rect(0, 0, 100, 20));
    656 
    657   // Put the cursor completely inside the client bounds. Handle should be
    658   // visible.
    659   touch_editable.set_cursor_rect(gfx::Rect(2, 0, 1, 20));
    660   touch_selection_controller->SelectionChanged();
    661   EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
    662 
    663   // Move the cursor up such that |kBarMinHeight| pixels are still in the client
    664   // bounds. Handle should still be visible.
    665   touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20, 1, 20));
    666   touch_selection_controller->SelectionChanged();
    667   EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
    668 
    669   // Move the cursor up such that less than |kBarMinHeight| pixels are in the
    670   // client bounds. Handle should be hidden.
    671   touch_editable.set_cursor_rect(gfx::Rect(2, kBarMinHeight - 20 - 1, 1, 20));
    672   touch_selection_controller->SelectionChanged();
    673   EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
    674 
    675   // Move the Cursor down such that |kBarBottomAllowance| pixels are out of the
    676   // client bounds. Handle should be visible.
    677   touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance, 1, 20));
    678   touch_selection_controller->SelectionChanged();
    679   EXPECT_TRUE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
    680 
    681   // Move the cursor down such that more than |kBarBottomAllowance| pixels are
    682   // out of the client bounds. Handle should be hidden.
    683   touch_editable.set_cursor_rect(gfx::Rect(2, kBarBottomAllowance + 1, 1, 20));
    684   touch_selection_controller->SelectionChanged();
    685   EXPECT_FALSE(IsCursorHandleVisibleFor(touch_selection_controller.get()));
    686 
    687   touch_selection_controller.reset();
    688 }
    689 
    690 TEST_F(TouchSelectionControllerImplTest, HandlesStackAboveParent) {
    691   ui::EventTarget* root = GetContext();
    692   ui::EventTargeter* targeter = root->GetEventTargeter();
    693 
    694   // Create the first window containing a Views::Textfield.
    695   CreateTextfield();
    696   aura::Window* window1 = textfield_widget_->GetNativeView();
    697 
    698   // Start touch editing, check that the handle is above the first window, and
    699   // end touch editing.
    700   StartTouchEditing();
    701   gfx::Point test_point = GetCursorHandleDragPoint();
    702   ui::MouseEvent test_event1(ui::ET_MOUSE_MOVED, test_point, test_point,
    703                              ui::EF_NONE, ui::EF_NONE);
    704   EXPECT_EQ(GetCursorHandleNativeView(),
    705             targeter->FindTargetForEvent(root, &test_event1));
    706   EndTouchEditing();
    707 
    708   // Create the second (empty) window over the first one.
    709   CreateWidget();
    710   aura::Window* window2 = widget_->GetNativeView();
    711 
    712   // Start touch editing (in the first window) and check that the handle is not
    713   // above the second window.
    714   StartTouchEditing();
    715   ui::MouseEvent test_event2(ui::ET_MOUSE_MOVED, test_point, test_point,
    716                              ui::EF_NONE, ui::EF_NONE);
    717   EXPECT_EQ(window2, targeter->FindTargetForEvent(root, &test_event2));
    718 
    719   // Move the first window to top and check that the handle is kept above the
    720   // first window.
    721   window1->GetRootWindow()->StackChildAtTop(window1);
    722   ui::MouseEvent test_event3(ui::ET_MOUSE_MOVED, test_point, test_point,
    723                              ui::EF_NONE, ui::EF_NONE);
    724   EXPECT_EQ(GetCursorHandleNativeView(),
    725             targeter->FindTargetForEvent(root, &test_event3));
    726 }
    727 
    728 // A simple implementation of TouchEditingMenuController that enables all
    729 // available commands.
    730 class TestTouchEditingMenuController : public TouchEditingMenuController {
    731  public:
    732   TestTouchEditingMenuController() {}
    733   virtual ~TestTouchEditingMenuController() {}
    734 
    735   // Overriden from TouchEditingMenuController.
    736   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
    737     // Return true, since we want the menu to have all |kMenuCommandCount|
    738     // available commands.
    739     return true;
    740   }
    741   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE {
    742     NOTREACHED();
    743   }
    744   virtual void OpenContextMenu() OVERRIDE {
    745     NOTREACHED();
    746   }
    747   virtual void OnMenuClosed(TouchEditingMenuView* menu) OVERRIDE {}
    748 
    749  private:
    750   DISALLOW_COPY_AND_ASSIGN(TestTouchEditingMenuController);
    751 };
    752 
    753 // Tests if anchor rect for touch editing quick menu is adjusted correctly based
    754 // on the distance of handles.
    755 TEST_F(TouchSelectionControllerImplTest, QuickMenuAdjustsAnchorRect) {
    756   CreateWidget();
    757   aura::Window* window = widget_->GetNativeView();
    758 
    759   scoped_ptr<TestTouchEditingMenuController> quick_menu_controller(
    760       new TestTouchEditingMenuController());
    761 
    762   // Some arbitrary size for touch editing handle image.
    763   gfx::Size handle_image_size(10, 10);
    764 
    765   // Calculate the width of quick menu. In addition to |kMenuCommandCount|
    766   // commands, there is an item for ellipsis.
    767   int quick_menu_width = (kMenuCommandCount + 1) * kMenuButtonWidth +
    768                          kMenuCommandCount;
    769 
    770   // Set anchor rect's width a bit smaller than the quick menu width plus handle
    771   // image width and check that anchor rect's height is adjusted.
    772   gfx::Rect anchor_rect(
    773       0, 0, quick_menu_width + handle_image_size.width() - 10, 20);
    774   TouchEditingMenuView* quick_menu(TouchEditingMenuView::Create(
    775       quick_menu_controller.get(), anchor_rect, handle_image_size, window));
    776   anchor_rect.Inset(0, 0, 0, -handle_image_size.height());
    777   EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString());
    778 
    779   // Set anchor rect's width a bit greater than the quick menu width plus handle
    780   // image width and check that anchor rect's height is not adjusted.
    781   anchor_rect =
    782       gfx::Rect(0, 0, quick_menu_width + handle_image_size.width() + 10, 20);
    783   quick_menu = TouchEditingMenuView::Create(
    784       quick_menu_controller.get(), anchor_rect, handle_image_size, window);
    785   EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString());
    786 
    787   // Close the widget, hence quick menus, before quick menu controller goes out
    788   // of scope.
    789   widget_->CloseNow();
    790   widget_ = NULL;
    791 }
    792 
    793 TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) {
    794   CreateTextfield();
    795   EXPECT_FALSE(GetSelectionController());
    796 
    797   ui::test::EventGenerator generator(
    798       textfield_widget_->GetNativeView()->GetRootWindow());
    799 
    800   generator.set_current_location(gfx::Point(5, 5));
    801   RunPendingMessages();
    802 
    803   // Start touch editing; then move mouse over the textfield and ensure it
    804   // deactivates touch selection.
    805   StartTouchEditing();
    806   EXPECT_TRUE(GetSelectionController());
    807   generator.MoveMouseTo(gfx::Point(5, 10));
    808   RunPendingMessages();
    809   EXPECT_FALSE(GetSelectionController());
    810 
    811   generator.MoveMouseTo(gfx::Point(5, 50));
    812   RunPendingMessages();
    813 
    814   // Start touch editing; then move mouse out of the textfield, but inside the
    815   // winow and ensure it deactivates touch selection.
    816   StartTouchEditing();
    817   EXPECT_TRUE(GetSelectionController());
    818   generator.MoveMouseTo(gfx::Point(5, 55));
    819   RunPendingMessages();
    820   EXPECT_FALSE(GetSelectionController());
    821 
    822   generator.MoveMouseTo(gfx::Point(5, 500));
    823   RunPendingMessages();
    824 
    825   // Start touch editing; then move mouse out of the textfield and window and
    826   // ensure it deactivates touch selection.
    827   StartTouchEditing();
    828   EXPECT_TRUE(GetSelectionController());
    829   generator.MoveMouseTo(5, 505);
    830   RunPendingMessages();
    831   EXPECT_FALSE(GetSelectionController());
    832 }
    833 
    834 TEST_F(TouchSelectionControllerImplTest, KeyEventDeactivatesTouchSelection) {
    835   CreateTextfield();
    836   EXPECT_FALSE(GetSelectionController());
    837 
    838   ui::test::EventGenerator generator(
    839       textfield_widget_->GetNativeView()->GetRootWindow());
    840 
    841   RunPendingMessages();
    842 
    843   // Start touch editing; then press a key and ensure it deactivates touch
    844   // selection.
    845   StartTouchEditing();
    846   EXPECT_TRUE(GetSelectionController());
    847   generator.PressKey(ui::VKEY_A, 0);
    848   RunPendingMessages();
    849   EXPECT_FALSE(GetSelectionController());
    850 }
    851 
    852 }  // namespace views
    853