| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "ui/aura/client/screen_position_client.h" | 7 #include "ui/aura/client/screen_position_client.h" |
| 8 #include "ui/aura/test/test_cursor_client.h" |
| 8 #include "ui/aura/window.h" | 9 #include "ui/aura/window.h" |
| 9 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
| 10 #include "ui/base/touch/touch_editing_controller.h" | 11 #include "ui/base/touch/touch_editing_controller.h" |
| 11 #include "ui/base/ui_base_switches.h" | 12 #include "ui/base/ui_base_switches.h" |
| 12 #include "ui/events/test/event_generator.h" | 13 #include "ui/events/test/event_generator.h" |
| 13 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/point.h" | 15 #include "ui/gfx/point.h" |
| 15 #include "ui/gfx/rect.h" | 16 #include "ui/gfx/rect.h" |
| 16 #include "ui/gfx/render_text.h" | 17 #include "ui/gfx/render_text.h" |
| 17 #include "ui/resources/grit/ui_resources.h" | 18 #include "ui/resources/grit/ui_resources.h" |
| 18 #include "ui/views/controls/textfield/textfield.h" | 19 #include "ui/views/controls/textfield/textfield.h" |
| 19 #include "ui/views/controls/textfield/textfield_test_api.h" | 20 #include "ui/views/controls/textfield/textfield_test_api.h" |
| 20 #include "ui/views/test/views_test_base.h" | 21 #include "ui/views/test/views_test_base.h" |
| 21 #include "ui/views/touchui/touch_selection_controller_impl.h" | 22 #include "ui/views/touchui/touch_selection_controller_impl.h" |
| 22 #include "ui/views/views_touch_selection_controller_factory.h" | 23 #include "ui/views/views_touch_selection_controller_factory.h" |
| 23 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 25 #include "ui/wm/core/default_screen_position_client.h" |
| 24 | 26 |
| 25 using base::ASCIIToUTF16; | 27 using base::ASCIIToUTF16; |
| 26 using base::UTF16ToUTF8; | 28 using base::UTF16ToUTF8; |
| 27 using base::WideToUTF16; | 29 using base::WideToUTF16; |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 // Should match kSelectionHandlePadding in touch_selection_controller. | |
| 31 const int kPadding = 10; | |
| 32 | |
| 33 // Should match kSelectionHandleBarMinHeight in touch_selection_controller. | 32 // Should match kSelectionHandleBarMinHeight in touch_selection_controller. |
| 34 const int kBarMinHeight = 5; | 33 const int kBarMinHeight = 5; |
| 35 | 34 |
| 36 // Should match kSelectionHandleBarBottomAllowance in | 35 // Should match kSelectionHandleBarBottomAllowance in |
| 37 // touch_selection_controller. | 36 // touch_selection_controller. |
| 38 const int kBarBottomAllowance = 3; | 37 const int kBarBottomAllowance = 3; |
| 39 | 38 |
| 40 // Should match kMenuButtonWidth in touch_editing_menu. | 39 // Should match kMenuButtonWidth in touch_editing_menu. |
| 41 const int kMenuButtonWidth = 63; | 40 const int kMenuButtonWidth = 63; |
| 42 | 41 |
| 43 // Should match size of kMenuCommands array in touch_editing_menu. | 42 // Should match size of kMenuCommands array in touch_editing_menu. |
| 44 const int kMenuCommandCount = 3; | 43 const int kMenuCommandCount = 3; |
| 45 | 44 |
| 46 gfx::Image* GetHandleImage() { | 45 // For selection bounds |b1| and |b2| in a paragraph of text, returns -1 if |b1| |
| 47 static gfx::Image* handle_image = NULL; | 46 // is physically before |b2|, +1 if |b2| is before |b1|, and 0 if they are at |
| 48 if (!handle_image) { | 47 // the same location. |
| 49 handle_image = &ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 48 int CompareTextSelectionBounds(const ui::SelectionBound& b1, |
| 50 IDR_TEXT_SELECTION_HANDLE); | 49 const ui::SelectionBound& b2) { |
| 51 } | 50 if ((b1.edge_top.y() < b2.edge_top.y()) || b1.edge_top.x() < b2.edge_top.x()) |
| 52 return handle_image; | 51 return -1; |
| 52 else if (b1 == b2) |
| 53 return 0; |
| 54 else |
| 55 return 1; |
| 53 } | 56 } |
| 54 | 57 |
| 55 gfx::Size GetHandleImageSize() { | |
| 56 return GetHandleImage()->Size(); | |
| 57 } | |
| 58 } // namespace | 58 } // namespace |
| 59 | 59 |
| 60 namespace views { | 60 namespace views { |
| 61 | 61 |
| 62 class TouchSelectionControllerImplTest : public ViewsTestBase { | 62 class TouchSelectionControllerImplTest : public ViewsTestBase { |
| 63 public: | 63 public: |
| 64 TouchSelectionControllerImplTest() | 64 TouchSelectionControllerImplTest() |
| 65 : textfield_widget_(NULL), | 65 : textfield_widget_(nullptr), |
| 66 widget_(NULL), | 66 widget_(nullptr), |
| 67 textfield_(NULL), | 67 textfield_(nullptr), |
| 68 views_tsc_factory_(new ViewsTouchSelectionControllerFactory) { | 68 views_tsc_factory_(new ViewsTouchSelectionControllerFactory) { |
| 69 CommandLine::ForCurrentProcess()->AppendSwitch( | 69 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 70 switches::kEnableTouchEditing); | 70 switches::kEnableTouchEditing); |
| 71 ui::TouchSelectionControllerFactory::SetInstance(views_tsc_factory_.get()); | 71 ui::TouchSelectionControllerFactory::SetInstance(views_tsc_factory_.get()); |
| 72 } | 72 } |
| 73 | 73 |
| 74 ~TouchSelectionControllerImplTest() override { | 74 ~TouchSelectionControllerImplTest() override { |
| 75 ui::TouchSelectionControllerFactory::SetInstance(NULL); | 75 ui::TouchSelectionControllerFactory::SetInstance(nullptr); |
| 76 } |
| 77 |
| 78 void SetUp() override { |
| 79 ViewsTestBase::SetUp(); |
| 80 SetUpRootWindowClients(GetContext()); |
| 76 } | 81 } |
| 77 | 82 |
| 78 void TearDown() override { | 83 void TearDown() override { |
| 84 test_cursor_client_.reset(); |
| 79 if (textfield_widget_ && !textfield_widget_->IsClosed()) | 85 if (textfield_widget_ && !textfield_widget_->IsClosed()) |
| 80 textfield_widget_->Close(); | 86 textfield_widget_->Close(); |
| 81 if (widget_ && !widget_->IsClosed()) | 87 if (widget_ && !widget_->IsClosed()) |
| 82 widget_->Close(); | 88 widget_->Close(); |
| 83 ViewsTestBase::TearDown(); | 89 ViewsTestBase::TearDown(); |
| 84 } | 90 } |
| 85 | 91 |
| 86 void CreateTextfield() { | 92 void CreateTextfield() { |
| 87 textfield_ = new Textfield(); | 93 textfield_ = new Textfield(); |
| 88 textfield_widget_ = new Widget; | 94 textfield_widget_ = new Widget; |
| 89 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 95 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 90 params.bounds = gfx::Rect(0, 0, 200, 200); | 96 params.bounds = gfx::Rect(0, 0, 200, 200); |
| 91 textfield_widget_->Init(params); | 97 textfield_widget_->Init(params); |
| 92 View* container = new View(); | 98 View* container = new View(); |
| 93 textfield_widget_->SetContentsView(container); | 99 textfield_widget_->SetContentsView(container); |
| 94 container->AddChildView(textfield_); | 100 container->AddChildView(textfield_); |
| 95 | 101 |
| 96 textfield_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); | 102 textfield_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); |
| 97 textfield_->set_id(1); | 103 textfield_->set_id(1); |
| 98 textfield_widget_->Show(); | 104 textfield_widget_->Show(); |
| 99 | 105 |
| 100 textfield_->RequestFocus(); | 106 textfield_->RequestFocus(); |
| 101 | |
| 102 textfield_test_api_.reset(new TextfieldTestApi(textfield_)); | 107 textfield_test_api_.reset(new TextfieldTestApi(textfield_)); |
| 103 } | 108 } |
| 104 | 109 |
| 105 void CreateWidget() { | 110 void CreateWidget() { |
| 106 widget_ = new Widget; | 111 widget_ = new Widget; |
| 107 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 112 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 108 params.bounds = gfx::Rect(0, 0, 200, 200); | 113 params.bounds = gfx::Rect(0, 0, 200, 200); |
| 109 widget_->Init(params); | 114 widget_->Init(params); |
| 110 widget_->Show(); | 115 widget_->Show(); |
| 111 } | 116 } |
| 112 | 117 |
| 113 protected: | 118 protected: |
| 114 static bool IsCursorHandleVisibleFor( | 119 static bool IsCursorHandleVisibleFor( |
| 115 ui::TouchSelectionController* controller) { | 120 ui::TouchSelectionController* controller) { |
| 116 TouchSelectionControllerImpl* impl = | 121 TouchSelectionControllerImpl* impl = |
| 117 static_cast<TouchSelectionControllerImpl*>(controller); | 122 static_cast<TouchSelectionControllerImpl*>(controller); |
| 118 return impl->IsCursorHandleVisible(); | 123 return impl->IsCursorHandleVisible(); |
| 119 } | 124 } |
| 120 | 125 |
| 121 gfx::Rect GetCursorRect(const gfx::SelectionModel& sel) { | 126 gfx::Rect GetCursorRect(const gfx::SelectionModel& sel) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 133 } | 138 } |
| 134 | 139 |
| 135 void StartTouchEditing() { | 140 void StartTouchEditing() { |
| 136 textfield_test_api_->CreateTouchSelectionControllerAndNotifyIt(); | 141 textfield_test_api_->CreateTouchSelectionControllerAndNotifyIt(); |
| 137 } | 142 } |
| 138 | 143 |
| 139 void EndTouchEditing() { | 144 void EndTouchEditing() { |
| 140 textfield_test_api_->ResetTouchSelectionController(); | 145 textfield_test_api_->ResetTouchSelectionController(); |
| 141 } | 146 } |
| 142 | 147 |
| 143 void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { | 148 void SimulateSelectionHandleDrag(gfx::Vector2d v, int selection_handle) { |
| 144 TouchSelectionControllerImpl* controller = GetSelectionController(); | 149 TouchSelectionControllerImpl* controller = GetSelectionController(); |
| 145 // Do the work of OnMousePressed(). | 150 views::WidgetDelegateView* handle = nullptr; |
| 146 if (selection_handle == 1) | 151 if (selection_handle == 1) |
| 147 controller->SetDraggingHandle(controller->selection_handle_1_.get()); | 152 handle = controller->GetHandle1View(); |
| 148 else | 153 else |
| 149 controller->SetDraggingHandle(controller->selection_handle_2_.get()); | 154 handle = controller->GetHandle2View(); |
| 150 | 155 |
| 151 // Offset the drag position by the selection handle radius since it is | 156 gfx::Point grip_location = gfx::Point(handle->size().width() / 2, |
| 152 // supposed to be in the coordinate system of the handle. | 157 handle->size().height() / 2); |
| 153 p.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); | 158 base::TimeDelta time_stamp = base::TimeDelta(); |
| 154 controller->SelectionHandleDragged(p); | 159 { |
| 155 | 160 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_BEGIN); |
| 156 // Do the work of OnMouseReleased(). | 161 ui::GestureEvent scroll_begin( |
| 157 controller->dragging_handle_ = NULL; | 162 grip_location.x(), grip_location.y(), 0, time_stamp, details); |
| 163 handle->OnGestureEvent(&scroll_begin); |
| 164 } |
| 165 test_cursor_client_->DisableMouseEvents(); |
| 166 { |
| 167 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_UPDATE); |
| 168 gfx::Point update_location = grip_location + v; |
| 169 ui::GestureEvent scroll_update( |
| 170 update_location.x(), update_location.y(), 0, time_stamp, details); |
| 171 handle->OnGestureEvent(&scroll_update); |
| 172 } |
| 173 { |
| 174 ui::GestureEventDetails details(ui::ET_GESTURE_SCROLL_END); |
| 175 ui::GestureEvent scroll_end( |
| 176 grip_location.x(), grip_location.y(), 0, time_stamp, details); |
| 177 handle->OnGestureEvent(&scroll_end); |
| 178 } |
| 179 test_cursor_client_->EnableMouseEvents(); |
| 158 } | 180 } |
| 159 | 181 |
| 160 gfx::NativeView GetCursorHandleNativeView() { | 182 gfx::NativeView GetCursorHandleNativeView() { |
| 161 return GetSelectionController()->GetCursorHandleNativeView(); | 183 return GetSelectionController()->GetCursorHandleNativeView(); |
| 162 } | 184 } |
| 163 | 185 |
| 164 gfx::Point GetSelectionHandle1Position() { | 186 gfx::Rect GetSelectionHandle1Bounds() { |
| 165 return GetSelectionController()->GetSelectionHandle1Position(); | 187 return GetSelectionController()->GetSelectionHandle1Bounds(); |
| 166 } | 188 } |
| 167 | 189 |
| 168 gfx::Point GetSelectionHandle2Position() { | 190 gfx::Rect GetSelectionHandle2Bounds() { |
| 169 return GetSelectionController()->GetSelectionHandle2Position(); | 191 return GetSelectionController()->GetSelectionHandle2Bounds(); |
| 170 } | 192 } |
| 171 | 193 |
| 172 gfx::Point GetCursorHandlePosition() { | 194 gfx::Rect GetCursorHandleBounds() { |
| 173 return GetSelectionController()->GetCursorHandlePosition(); | 195 return GetSelectionController()->GetCursorHandleBounds(); |
| 196 } |
| 197 |
| 198 gfx::Rect GetExpectedHandleBounds(const ui::SelectionBound& bound) { |
| 199 return GetSelectionController()->GetExpectedHandleBounds(bound); |
| 174 } | 200 } |
| 175 | 201 |
| 176 bool IsSelectionHandle1Visible() { | 202 bool IsSelectionHandle1Visible() { |
| 177 return GetSelectionController()->IsSelectionHandle1Visible(); | 203 return GetSelectionController()->IsSelectionHandle1Visible(); |
| 178 } | 204 } |
| 179 | 205 |
| 180 bool IsSelectionHandle2Visible() { | 206 bool IsSelectionHandle2Visible() { |
| 181 return GetSelectionController()->IsSelectionHandle2Visible(); | 207 return GetSelectionController()->IsSelectionHandle2Visible(); |
| 182 } | 208 } |
| 183 | 209 |
| 184 bool IsCursorHandleVisible() { | 210 bool IsCursorHandleVisible() { |
| 185 return GetSelectionController()->IsCursorHandleVisible(); | 211 return GetSelectionController()->IsCursorHandleVisible(); |
| 186 } | 212 } |
| 187 | 213 |
| 188 gfx::RenderText* GetRenderText() { | 214 gfx::RenderText* GetRenderText() { |
| 189 return textfield_test_api_->GetRenderText(); | 215 return textfield_test_api_->GetRenderText(); |
| 190 } | 216 } |
| 191 | 217 |
| 192 gfx::Point GetCursorHandleDragPoint() { | 218 gfx::Point GetCursorHandleDragPoint() { |
| 193 gfx::Point point = GetCursorHandlePosition(); | 219 gfx::Rect rect = GetCursorHandleBounds(); |
| 194 const gfx::SelectionModel& sel = textfield_->GetSelectionModel(); | 220 const gfx::SelectionModel& sel = textfield_->GetSelectionModel(); |
| 195 int cursor_height = GetCursorRect(sel).height(); | 221 int cursor_height = GetCursorRect(sel).height(); |
| 196 point.Offset(GetHandleImageSize().width() / 2 + kPadding, | 222 gfx::Point point = rect.CenterPoint(); |
| 197 GetHandleImageSize().height() / 2 + cursor_height); | 223 point.Offset(0, cursor_height); |
| 198 return point; | 224 return point; |
| 199 } | 225 } |
| 200 | 226 |
| 227 // If textfield has selection, this verifies that the selection handles |
| 228 // are visible, at the correct positions (at the end points of selection), and |
| 229 // (if |check_direction| is set to true), that they have the correct |
| 230 // directionality. |
| 231 // |cursor_at_selection_handle_1| is used to decide whether selection |
| 232 // handle 1's position is matched against the start of selection or the end. |
| 233 void VerifyHandlePositions( |
| 234 bool cursor_at_selection_handle_1, |
| 235 bool check_direction, |
| 236 const tracked_objects::Location& from_here) { |
| 237 ui::SelectionBound anchor, focus; |
| 238 textfield_->GetSelectionEndPoints(&anchor, &focus); |
| 239 std::string from_str = from_here.ToString(); |
| 240 if (textfield_->HasSelection()) { |
| 241 EXPECT_TRUE(IsSelectionHandle1Visible()) << from_str; |
| 242 EXPECT_TRUE(IsSelectionHandle2Visible()) << from_str; |
| 243 EXPECT_FALSE(IsCursorHandleVisible()); |
| 244 gfx::Rect sh1_bounds = GetSelectionHandle1Bounds(); |
| 245 gfx::Rect sh2_bounds = GetSelectionHandle2Bounds(); |
| 246 if (cursor_at_selection_handle_1) { |
| 247 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(focus)) << from_str; |
| 248 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(anchor)) << from_str; |
| 249 } else { |
| 250 EXPECT_EQ(sh1_bounds, GetExpectedHandleBounds(anchor)) << from_str; |
| 251 EXPECT_EQ(sh2_bounds, GetExpectedHandleBounds(focus)) << from_str; |
| 252 } |
| 253 } else { |
| 254 EXPECT_FALSE(IsSelectionHandle1Visible()) << from_str; |
| 255 EXPECT_FALSE(IsSelectionHandle2Visible()) << from_str; |
| 256 EXPECT_TRUE(IsCursorHandleVisible()); |
| 257 gfx::Rect cursor_bounds = GetCursorHandleBounds(); |
| 258 DCHECK(anchor == focus); |
| 259 EXPECT_EQ(cursor_bounds, GetExpectedHandleBounds(anchor)) << from_str; |
| 260 } |
| 261 if (check_direction) { |
| 262 if (CompareTextSelectionBounds(anchor, focus) < 0) { |
| 263 EXPECT_EQ(ui::SelectionBound::LEFT, anchor.type) << from_str; |
| 264 EXPECT_EQ(ui::SelectionBound::RIGHT, focus.type) << from_str; |
| 265 } else if (CompareTextSelectionBounds(anchor, focus) > 0) { |
| 266 EXPECT_EQ(ui::SelectionBound::LEFT, focus.type) << from_str; |
| 267 EXPECT_EQ(ui::SelectionBound::RIGHT, anchor.type) << from_str; |
| 268 } else { |
| 269 EXPECT_EQ(ui::SelectionBound::CENTER, focus.type) << from_str; |
| 270 EXPECT_EQ(ui::SelectionBound::CENTER, anchor.type) << from_str; |
| 271 } |
| 272 } |
| 273 } |
| 274 |
| 201 Widget* textfield_widget_; | 275 Widget* textfield_widget_; |
| 202 Widget* widget_; | 276 Widget* widget_; |
| 203 | 277 |
| 204 Textfield* textfield_; | 278 Textfield* textfield_; |
| 205 scoped_ptr<TextfieldTestApi> textfield_test_api_; | 279 scoped_ptr<TextfieldTestApi> textfield_test_api_; |
| 206 scoped_ptr<ViewsTouchSelectionControllerFactory> views_tsc_factory_; | 280 scoped_ptr<ViewsTouchSelectionControllerFactory> views_tsc_factory_; |
| 281 scoped_ptr<wm::DefaultScreenPositionClient> screen_position_client_; |
| 282 scoped_ptr<aura::test::TestCursorClient> test_cursor_client_; |
| 207 | 283 |
| 208 private: | 284 private: |
| 285 void SetUpRootWindowClients(aura::Window* root_window) { |
| 286 if (!screen_position_client_) { |
| 287 screen_position_client_.reset(new wm::DefaultScreenPositionClient()); |
| 288 aura::client::SetScreenPositionClient(root_window, |
| 289 screen_position_client_.get()); |
| 290 test_cursor_client_.reset(new aura::test::TestCursorClient(root_window)); |
| 291 } |
| 292 } |
| 293 |
| 209 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest); | 294 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerImplTest); |
| 210 }; | 295 }; |
| 211 | 296 |
| 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 | 297 // Tests that the selection handles are placed appropriately when selection in |
| 250 // a Textfield changes. | 298 // a Textfield changes. |
| 251 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { | 299 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { |
| 252 CreateTextfield(); | 300 CreateTextfield(); |
| 253 textfield_->SetText(ASCIIToUTF16("some text")); | 301 textfield_->SetText(ASCIIToUTF16("some text")); |
| 254 // Tap the textfield to invoke touch selection. | 302 // Tap the textfield to invoke touch selection. |
| 255 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); | 303 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); |
| 256 details.set_tap_count(1); | 304 details.set_tap_count(1); |
| 257 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details); | 305 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details); |
| 258 textfield_->OnGestureEvent(&tap); | 306 textfield_->OnGestureEvent(&tap); |
| 259 | 307 |
| 260 // Test selecting a range. | 308 // Test selecting a range. |
| 261 textfield_->SelectRange(gfx::Range(3, 7)); | 309 textfield_->SelectRange(gfx::Range(3, 7)); |
| 262 VERIFY_HANDLE_POSITIONS(false); | 310 VerifyHandlePositions(false, true, FROM_HERE); |
| 263 | 311 |
| 264 // Test selecting everything. | 312 // Test selecting everything. |
| 265 textfield_->SelectAll(false); | 313 textfield_->SelectAll(false); |
| 266 VERIFY_HANDLE_POSITIONS(false); | 314 VerifyHandlePositions(false, true, FROM_HERE); |
| 267 | 315 |
| 268 // Test with no selection. | 316 // Test with no selection. |
| 269 textfield_->ClearSelection(); | 317 textfield_->ClearSelection(); |
| 270 VERIFY_HANDLE_POSITIONS(false); | 318 VerifyHandlePositions(false, true, FROM_HERE); |
| 271 | 319 |
| 272 // Test with lost focus. | 320 // Test with lost focus. |
| 273 textfield_widget_->GetFocusManager()->ClearFocus(); | 321 textfield_widget_->GetFocusManager()->ClearFocus(); |
| 274 EXPECT_FALSE(GetSelectionController()); | 322 EXPECT_FALSE(GetSelectionController()); |
| 275 | 323 |
| 276 // Test with focus re-gained. | 324 // Test with focus re-gained. |
| 277 textfield_widget_->GetFocusManager()->SetFocusedView(textfield_); | 325 textfield_widget_->GetFocusManager()->SetFocusedView(textfield_); |
| 278 EXPECT_FALSE(GetSelectionController()); | 326 EXPECT_FALSE(GetSelectionController()); |
| 279 textfield_->OnGestureEvent(&tap); | 327 textfield_->OnGestureEvent(&tap); |
| 280 VERIFY_HANDLE_POSITIONS(false); | 328 VerifyHandlePositions(false, true, FROM_HERE); |
| 281 } | 329 } |
| 282 | 330 |
| 283 // Tests that the selection handles are placed appropriately in bidi text. | 331 // Tests that the selection handles are placed appropriately in bidi text. |
| 284 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { | 332 TEST_F(TouchSelectionControllerImplTest, SelectionInBidiTextfieldTest) { |
| 285 CreateTextfield(); | 333 CreateTextfield(); |
| 286 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); | 334 textfield_->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); |
| 287 // Tap the textfield to invoke touch selection. | 335 // Tap the textfield to invoke touch selection. |
| 288 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); | 336 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); |
| 289 details.set_tap_count(1); | 337 details.set_tap_count(1); |
| 290 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details); | 338 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details); |
| 291 textfield_->OnGestureEvent(&tap); | 339 textfield_->OnGestureEvent(&tap); |
| 292 | 340 |
| 293 // Test cursor at run boundary and with empty selection. | 341 // Test cursor at run boundary and with empty selection. |
| 294 textfield_->SelectSelectionModel( | 342 textfield_->SelectSelectionModel( |
| 295 gfx::SelectionModel(3, gfx::CURSOR_BACKWARD)); | 343 gfx::SelectionModel(3, gfx::CURSOR_BACKWARD)); |
| 296 VERIFY_HANDLE_POSITIONS(false); | 344 VerifyHandlePositions(false, true, FROM_HERE); |
| 297 | 345 |
| 298 // Test selection range inside one run and starts or ends at run boundary. | 346 // Test selection range inside one run and starts or ends at run boundary. |
| 299 textfield_->SelectRange(gfx::Range(2, 3)); | 347 textfield_->SelectRange(gfx::Range(2, 3)); |
| 300 VERIFY_HANDLE_POSITIONS(false); | 348 VerifyHandlePositions(false, true, FROM_HERE); |
| 301 | 349 |
| 302 textfield_->SelectRange(gfx::Range(3, 2)); | 350 textfield_->SelectRange(gfx::Range(3, 2)); |
| 303 VERIFY_HANDLE_POSITIONS(false); | 351 VerifyHandlePositions(false, true, FROM_HERE); |
| 304 | 352 |
| 353 // TODO(mfomitchev): crbug.com/429705 |
| 354 // The correct behavior for handles in mixed ltr/rtl text line is not known, |
| 355 // so passing false for |check_direction| in some of these tests. |
| 305 textfield_->SelectRange(gfx::Range(3, 4)); | 356 textfield_->SelectRange(gfx::Range(3, 4)); |
| 306 VERIFY_HANDLE_POSITIONS(false); | 357 VerifyHandlePositions(false, false, FROM_HERE); |
| 307 | 358 |
| 308 textfield_->SelectRange(gfx::Range(4, 3)); | 359 textfield_->SelectRange(gfx::Range(4, 3)); |
| 309 VERIFY_HANDLE_POSITIONS(false); | 360 VerifyHandlePositions(false, false, FROM_HERE); |
| 310 | 361 |
| 311 textfield_->SelectRange(gfx::Range(3, 6)); | 362 textfield_->SelectRange(gfx::Range(3, 6)); |
| 312 VERIFY_HANDLE_POSITIONS(false); | 363 VerifyHandlePositions(false, false, FROM_HERE); |
| 313 | 364 |
| 314 textfield_->SelectRange(gfx::Range(6, 3)); | 365 textfield_->SelectRange(gfx::Range(6, 3)); |
| 315 VERIFY_HANDLE_POSITIONS(false); | 366 VerifyHandlePositions(false, false, FROM_HERE); |
| 316 | 367 |
| 317 // Test selection range accross runs. | 368 // Test selection range accross runs. |
| 318 textfield_->SelectRange(gfx::Range(0, 6)); | 369 textfield_->SelectRange(gfx::Range(0, 6)); |
| 319 VERIFY_HANDLE_POSITIONS(false); | 370 VerifyHandlePositions(false, true, FROM_HERE); |
| 320 | 371 |
| 321 textfield_->SelectRange(gfx::Range(6, 0)); | 372 textfield_->SelectRange(gfx::Range(6, 0)); |
| 322 VERIFY_HANDLE_POSITIONS(false); | 373 VerifyHandlePositions(false, true, FROM_HERE); |
| 323 | 374 |
| 324 textfield_->SelectRange(gfx::Range(1, 4)); | 375 textfield_->SelectRange(gfx::Range(1, 4)); |
| 325 VERIFY_HANDLE_POSITIONS(false); | 376 VerifyHandlePositions(false, true, FROM_HERE); |
| 326 | 377 |
| 327 textfield_->SelectRange(gfx::Range(4, 1)); | 378 textfield_->SelectRange(gfx::Range(4, 1)); |
| 328 VERIFY_HANDLE_POSITIONS(false); | 379 VerifyHandlePositions(false, true, FROM_HERE); |
| 329 } | 380 } |
| 330 | 381 |
| 331 // Tests if the SelectRect callback is called appropriately when selection | 382 // Tests if the SelectRect callback is called appropriately when selection |
| 332 // handles are moved. | 383 // handles are moved. |
| 333 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { | 384 TEST_F(TouchSelectionControllerImplTest, SelectRectCallbackTest) { |
| 334 CreateTextfield(); | 385 CreateTextfield(); |
| 335 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); | 386 textfield_->SetText(ASCIIToUTF16("textfield with selected text")); |
| 336 // Tap the textfield to invoke touch selection. | 387 // Tap the textfield to invoke touch selection. |
| 337 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); | 388 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); |
| 338 details.set_tap_count(1); | 389 details.set_tap_count(1); |
| 339 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details); | 390 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details); |
| 340 textfield_->OnGestureEvent(&tap); | 391 textfield_->OnGestureEvent(&tap); |
| 341 textfield_->SelectRange(gfx::Range(3, 7)); | 392 textfield_->SelectRange(gfx::Range(3, 7)); |
| 342 | 393 |
| 343 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfie"); | 394 gfx::Point textfield_origin; |
| 344 VERIFY_HANDLE_POSITIONS(false); | 395 textfield_->ConvertPointToScreen(&textfield_origin); |
| 396 |
| 397 EXPECT_EQ("tfie", UTF16ToUTF8(textfield_->GetSelectedText())); |
| 398 VerifyHandlePositions(false, true, FROM_HERE); |
| 345 | 399 |
| 346 // Drag selection handle 2 to right by 3 chars. | 400 // Drag selection handle 2 to right by 3 chars. |
| 347 const gfx::FontList& font_list = textfield_->GetFontList(); | 401 const gfx::FontList& font_list = textfield_->GetFontList(); |
| 348 int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list); | 402 int x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("ld "), font_list); |
| 349 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 403 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2); |
| 350 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "tfield "); | 404 EXPECT_EQ("tfield ", UTF16ToUTF8(textfield_->GetSelectedText())); |
| 351 VERIFY_HANDLE_POSITIONS(false); | 405 VerifyHandlePositions(false, true, FROM_HERE); |
| 352 | 406 |
| 353 // Drag selection handle 1 to the left by a large amount (selection should | 407 // Drag selection handle 1 to the left by a large amount (selection should |
| 354 // just stick to the beginning of the textfield). | 408 // just stick to the beginning of the textfield). |
| 355 SimulateSelectionHandleDrag(gfx::Point(-50, 0), 1); | 409 SimulateSelectionHandleDrag(gfx::Vector2d(-50, 0), 1); |
| 356 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "textfield "); | 410 EXPECT_EQ("textfield ", UTF16ToUTF8(textfield_->GetSelectedText())); |
| 357 VERIFY_HANDLE_POSITIONS(true); | 411 VerifyHandlePositions(true, true, FROM_HERE); |
| 358 | 412 |
| 359 // Drag selection handle 1 across selection handle 2. | 413 // Drag selection handle 1 across selection handle 2. |
| 360 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list); | 414 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("textfield with "), font_list); |
| 361 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 415 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1); |
| 362 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "with "); | 416 EXPECT_EQ("with ", UTF16ToUTF8(textfield_->GetSelectedText())); |
| 363 VERIFY_HANDLE_POSITIONS(true); | 417 VerifyHandlePositions(true, true, FROM_HERE); |
| 364 | 418 |
| 365 // Drag selection handle 2 across selection handle 1. | 419 // Drag selection handle 2 across selection handle 1. |
| 366 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list); | 420 x = gfx::Canvas::GetStringWidth(ASCIIToUTF16("with selected "), font_list); |
| 367 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 421 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2); |
| 368 EXPECT_EQ(UTF16ToUTF8(textfield_->GetSelectedText()), "selected "); | 422 EXPECT_EQ("selected ", UTF16ToUTF8(textfield_->GetSelectedText())); |
| 369 VERIFY_HANDLE_POSITIONS(false); | 423 VerifyHandlePositions(false, true, FROM_HERE); |
| 370 } | 424 } |
| 371 | 425 |
| 372 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) { | 426 TEST_F(TouchSelectionControllerImplTest, SelectRectInBidiCallbackTest) { |
| 373 CreateTextfield(); | 427 CreateTextfield(); |
| 374 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def")); | 428 textfield_->SetText(WideToUTF16(L"abc\x05e1\x05e2\x05e3" L"def")); |
| 375 // Tap the textfield to invoke touch selection. | 429 // Tap the textfield to invoke touch selection. |
| 376 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); | 430 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); |
| 377 details.set_tap_count(1); | 431 details.set_tap_count(1); |
| 378 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details); | 432 ui::GestureEvent tap(0, 0, 0, base::TimeDelta(), details); |
| 379 textfield_->OnGestureEvent(&tap); | 433 textfield_->OnGestureEvent(&tap); |
| 380 | 434 |
| 381 // Select [c] from left to right. | 435 // Select [c] from left to right. |
| 382 textfield_->SelectRange(gfx::Range(2, 3)); | 436 textfield_->SelectRange(gfx::Range(2, 3)); |
| 383 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); | 437 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); |
| 384 VERIFY_HANDLE_POSITIONS(false); | 438 VerifyHandlePositions(false, true, FROM_HERE); |
| 385 | 439 |
| 386 // Drag selection handle 2 to right by 1 char. | 440 // Drag selection handle 2 to right by 1 char. |
| 387 const gfx::FontList& font_list = textfield_->GetFontList(); | 441 const gfx::FontList& font_list = textfield_->GetFontList(); |
| 388 int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list); | 442 int x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list); |
| 389 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 443 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2); |
| 390 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | 444 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 391 VERIFY_HANDLE_POSITIONS(false); | 445 VerifyHandlePositions(false, true, FROM_HERE); |
| 392 | 446 |
| 393 // Drag selection handle 1 to left by 1 char. | 447 // Drag selection handle 1 to left by 1 char. |
| 394 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list); | 448 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list); |
| 395 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | 449 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1); |
| 396 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); | 450 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 397 VERIFY_HANDLE_POSITIONS(true); | 451 VerifyHandlePositions(true, true, FROM_HERE); |
| 398 | 452 |
| 399 // Select [c] from right to left. | 453 // Select [c] from right to left. |
| 400 textfield_->SelectRange(gfx::Range(3, 2)); | 454 textfield_->SelectRange(gfx::Range(3, 2)); |
| 401 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); | 455 EXPECT_EQ(WideToUTF16(L"c"), textfield_->GetSelectedText()); |
| 402 VERIFY_HANDLE_POSITIONS(false); | 456 VerifyHandlePositions(false, true, FROM_HERE); |
| 403 | 457 |
| 404 // Drag selection handle 1 to right by 1 char. | 458 // Drag selection handle 1 to right by 1 char. |
| 405 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list); | 459 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e3"), font_list); |
| 406 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 460 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1); |
| 407 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | 461 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 408 VERIFY_HANDLE_POSITIONS(true); | 462 VerifyHandlePositions(true, true, FROM_HERE); |
| 409 | 463 |
| 410 // Drag selection handle 2 to left by 1 char. | 464 // Drag selection handle 2 to left by 1 char. |
| 411 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list); | 465 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"b"), font_list); |
| 412 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | 466 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2); |
| 413 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); | 467 EXPECT_EQ(WideToUTF16(L"bc\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 414 VERIFY_HANDLE_POSITIONS(false); | 468 VerifyHandlePositions(false, true, FROM_HERE); |
| 415 | 469 |
| 416 // Select [\x5e1] from right to left. | 470 // Select [\x5e1] from right to left. |
| 417 textfield_->SelectRange(gfx::Range(3, 4)); | 471 textfield_->SelectRange(gfx::Range(3, 4)); |
| 418 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); | 472 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); |
| 419 VERIFY_HANDLE_POSITIONS(false); | 473 // TODO(mfomitchev): crbug.com/429705 |
| 474 // The correct behavior for handles in mixed ltr/rtl text line is not known, |
| 475 // so passing false for |check_direction| in some of these tests. |
| 476 VerifyHandlePositions(false, false, FROM_HERE); |
| 420 | 477 |
| 421 /* TODO(xji): for bidi text "abcDEF" whose display is "abcFEDhij", when click | 478 /* 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 | 479 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]. | 480 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, | 481 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 | 482 pass this position to FindCursorPosition() returns index of 'h'. which |
| 426 means the selection start changed from 3 to 6. | 483 means the selection start changed from 3 to 6. |
| 427 Need further investigation on whether this is a bug in Pango and how to | 484 Need further investigation on whether this is a bug in Pango and how to |
| 428 work around it. | 485 work around it. |
| 429 // Drag selection handle 2 to left by 1 char. | 486 // Drag selection handle 2 to left by 1 char. |
| 430 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); | 487 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); |
| 431 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | 488 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2); |
| 432 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); | 489 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 433 VERIFY_HANDLE_POSITIONS(false); | 490 VERIFY_HANDLE_POSITIONS(false); |
| 434 */ | 491 */ |
| 435 | 492 |
| 436 // Drag selection handle 1 to right by 1 char. | 493 // Drag selection handle 1 to right by 1 char. |
| 437 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list); | 494 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list); |
| 438 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 495 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1); |
| 439 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText()); | 496 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText()); |
| 440 VERIFY_HANDLE_POSITIONS(true); | 497 VerifyHandlePositions(true, true, FROM_HERE); |
| 441 | 498 |
| 442 // Select [\x5e1] from left to right. | 499 // Select [\x5e1] from left to right. |
| 443 textfield_->SelectRange(gfx::Range(4, 3)); | 500 textfield_->SelectRange(gfx::Range(4, 3)); |
| 444 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); | 501 EXPECT_EQ(WideToUTF16(L"\x05e1"), textfield_->GetSelectedText()); |
| 445 VERIFY_HANDLE_POSITIONS(false); | 502 VerifyHandlePositions(false, false, FROM_HERE); |
| 446 | 503 |
| 447 /* TODO(xji): see detail of above commented out test case. | 504 /* TODO(xji): see detail of above commented out test case. |
| 448 // Drag selection handle 1 to left by 1 char. | 505 // Drag selection handle 1 to left by 1 char. |
| 449 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); | 506 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); |
| 450 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | 507 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1); |
| 451 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); | 508 EXPECT_EQ(WideToUTF16(L"\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 452 VERIFY_HANDLE_POSITIONS(true); | 509 VERIFY_HANDLE_POSITIONS(true); |
| 453 */ | 510 */ |
| 454 | 511 |
| 455 // Drag selection handle 2 to right by 1 char. | 512 // Drag selection handle 2 to right by 1 char. |
| 456 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list); | 513 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"d"), font_list); |
| 457 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 514 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2); |
| 458 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText()); | 515 EXPECT_EQ(WideToUTF16(L"\x05e2\x05e3" L"d"), textfield_->GetSelectedText()); |
| 459 VERIFY_HANDLE_POSITIONS(false); | 516 VerifyHandlePositions(false, true, FROM_HERE); |
| 460 | 517 |
| 461 // Select [\x05r3] from right to left. | 518 // Select [\x05r3] from right to left. |
| 462 textfield_->SelectRange(gfx::Range(5, 6)); | 519 textfield_->SelectRange(gfx::Range(5, 6)); |
| 463 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); | 520 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); |
| 464 VERIFY_HANDLE_POSITIONS(false); | 521 VerifyHandlePositions(false, false, FROM_HERE); |
| 465 | 522 |
| 466 // Drag selection handle 2 to left by 1 char. | 523 // Drag selection handle 2 to left by 1 char. |
| 467 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list); | 524 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list); |
| 468 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 2); | 525 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 2); |
| 469 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | 526 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 470 VERIFY_HANDLE_POSITIONS(false); | 527 VerifyHandlePositions(false, true, FROM_HERE); |
| 471 | 528 |
| 472 // Drag selection handle 1 to right by 1 char. | 529 // Drag selection handle 1 to right by 1 char. |
| 473 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); | 530 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); |
| 474 SimulateSelectionHandleDrag(gfx::Point(x, 0), 1); | 531 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 1); |
| 475 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); | 532 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); |
| 476 VERIFY_HANDLE_POSITIONS(true); | 533 VerifyHandlePositions(true, true, FROM_HERE); |
| 477 | 534 |
| 478 // Select [\x05r3] from left to right. | 535 // Select [\x05r3] from left to right. |
| 479 textfield_->SelectRange(gfx::Range(6, 5)); | 536 textfield_->SelectRange(gfx::Range(6, 5)); |
| 480 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); | 537 EXPECT_EQ(WideToUTF16(L"\x05e3"), textfield_->GetSelectedText()); |
| 481 VERIFY_HANDLE_POSITIONS(false); | 538 VerifyHandlePositions(false, false, FROM_HERE); |
| 482 | 539 |
| 483 // Drag selection handle 1 to left by 1 char. | 540 // Drag selection handle 1 to left by 1 char. |
| 484 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list); | 541 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"c"), font_list); |
| 485 SimulateSelectionHandleDrag(gfx::Point(-x, 0), 1); | 542 SimulateSelectionHandleDrag(gfx::Vector2d(-x, 0), 1); |
| 486 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); | 543 EXPECT_EQ(WideToUTF16(L"c\x05e1\x05e2"), textfield_->GetSelectedText()); |
| 487 VERIFY_HANDLE_POSITIONS(true); | 544 VerifyHandlePositions(true, true, FROM_HERE); |
| 488 | 545 |
| 489 // Drag selection handle 2 to right by 1 char. | 546 // Drag selection handle 2 to right by 1 char. |
| 490 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); | 547 x = gfx::Canvas::GetStringWidth(WideToUTF16(L"\x05e2"), font_list); |
| 491 SimulateSelectionHandleDrag(gfx::Point(x, 0), 2); | 548 SimulateSelectionHandleDrag(gfx::Vector2d(x, 0), 2); |
| 492 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); | 549 EXPECT_EQ(WideToUTF16(L"c\x05e1"), textfield_->GetSelectedText()); |
| 493 VERIFY_HANDLE_POSITIONS(false); | 550 VerifyHandlePositions(false, false, FROM_HERE); |
| 494 } | 551 } |
| 495 | 552 |
| 496 TEST_F(TouchSelectionControllerImplTest, | 553 TEST_F(TouchSelectionControllerImplTest, |
| 497 HiddenSelectionHandleRetainsCursorPosition) { | 554 HiddenSelectionHandleRetainsCursorPosition) { |
| 498 // Create a textfield with lots of text in it. | 555 // Create a textfield with lots of text in it. |
| 499 CreateTextfield(); | 556 CreateTextfield(); |
| 500 std::string textfield_text("some text"); | 557 std::string textfield_text("some text"); |
| 501 for (int i = 0; i < 10; ++i) | 558 for (int i = 0; i < 10; ++i) |
| 502 textfield_text += textfield_text; | 559 textfield_text += textfield_text; |
| 503 textfield_->SetText(ASCIIToUTF16(textfield_text)); | 560 textfield_->SetText(ASCIIToUTF16(textfield_text)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 514 // Check that one selection handle is hidden. | 571 // Check that one selection handle is hidden. |
| 515 EXPECT_FALSE(IsSelectionHandle1Visible()); | 572 EXPECT_FALSE(IsSelectionHandle1Visible()); |
| 516 EXPECT_TRUE(IsSelectionHandle2Visible()); | 573 EXPECT_TRUE(IsSelectionHandle2Visible()); |
| 517 EXPECT_EQ(gfx::Range(10, textfield_text.length()), | 574 EXPECT_EQ(gfx::Range(10, textfield_text.length()), |
| 518 textfield_->GetSelectedRange()); | 575 textfield_->GetSelectedRange()); |
| 519 | 576 |
| 520 // Drag the visible handle around and make sure the selection end point of the | 577 // Drag the visible handle around and make sure the selection end point of the |
| 521 // invisible handle does not change. | 578 // invisible handle does not change. |
| 522 size_t visible_handle_position = textfield_->GetSelectedRange().end(); | 579 size_t visible_handle_position = textfield_->GetSelectedRange().end(); |
| 523 for (int i = 0; i < 10; ++i) { | 580 for (int i = 0; i < 10; ++i) { |
| 524 SimulateSelectionHandleDrag(gfx::Point(-10, 0), 2); | 581 static const int drag_diff = -10; |
| 582 SimulateSelectionHandleDrag(gfx::Vector2d(drag_diff, 0), 2); |
| 525 // Make sure that the visible handle is being dragged. | 583 // Make sure that the visible handle is being dragged. |
| 526 EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end()); | 584 EXPECT_NE(visible_handle_position, textfield_->GetSelectedRange().end()); |
| 527 visible_handle_position = textfield_->GetSelectedRange().end(); | 585 visible_handle_position = textfield_->GetSelectedRange().end(); |
| 528 EXPECT_EQ((size_t) 10, textfield_->GetSelectedRange().start()); | 586 EXPECT_EQ((size_t) 10, textfield_->GetSelectedRange().start()); |
| 529 } | 587 } |
| 530 } | 588 } |
| 531 | 589 |
| 532 TEST_F(TouchSelectionControllerImplTest, | 590 TEST_F(TouchSelectionControllerImplTest, |
| 533 DoubleTapInTextfieldWithCursorHandleShouldSelectText) { | 591 DoubleTapInTextfieldWithCursorHandleShouldSelectText) { |
| 534 CreateTextfield(); | 592 CreateTextfield(); |
| 535 textfield_->SetText(ASCIIToUTF16("some text")); | 593 textfield_->SetText(ASCIIToUTF16("some text")); |
| 536 ui::test::EventGenerator generator( | 594 ui::test::EventGenerator generator( |
| 537 textfield_->GetWidget()->GetNativeView()->GetRootWindow()); | 595 textfield_->GetWidget()->GetNativeView()->GetRootWindow()); |
| 538 | 596 |
| 539 // Tap the textfield to invoke touch selection. | 597 // Tap the textfield to invoke touch selection. |
| 540 generator.GestureTapAt(gfx::Point(10, 10)); | 598 generator.GestureTapAt(gfx::Point(10, 10)); |
| 541 | 599 |
| 542 // Cursor handle should be visible. | 600 // Cursor handle should be visible. |
| 543 EXPECT_FALSE(textfield_->HasSelection()); | 601 EXPECT_FALSE(textfield_->HasSelection()); |
| 544 VERIFY_HANDLE_POSITIONS(false); | 602 VerifyHandlePositions(false, true, FROM_HERE); |
| 545 | 603 |
| 546 // Double tap on the cursor handle position. We want to check that the cursor | 604 // 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 | 605 // handle is not eating the event and that the event is falling through to the |
| 548 // textfield. | 606 // textfield. |
| 549 gfx::Point cursor_pos = GetCursorHandlePosition(); | 607 gfx::Point cursor_pos = GetCursorHandleBounds().origin(); |
| 550 cursor_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); | 608 cursor_pos.Offset(GetCursorHandleBounds().width() / 2, 0); |
| 551 generator.GestureTapAt(cursor_pos); | 609 generator.GestureTapAt(cursor_pos); |
| 552 generator.GestureTapAt(cursor_pos); | 610 generator.GestureTapAt(cursor_pos); |
| 553 EXPECT_TRUE(textfield_->HasSelection()); | 611 EXPECT_TRUE(textfield_->HasSelection()); |
| 554 } | 612 } |
| 555 | 613 |
| 556 // A simple implementation of TouchEditable that allows faking cursor position | 614 // A simple implementation of TouchEditable that allows faking cursor position |
| 557 // inside its boundaries. | 615 // inside its boundaries. |
| 558 class TestTouchEditable : public ui::TouchEditable { | 616 class TestTouchEditable : public ui::TouchEditable { |
| 559 public: | 617 public: |
| 560 explicit TestTouchEditable(aura::Window* window) | 618 explicit TestTouchEditable(aura::Window* window) |
| 561 : window_(window) { | 619 : window_(window) { |
| 562 DCHECK(window); | 620 DCHECK(window); |
| 563 } | 621 } |
| 564 | 622 |
| 565 void set_bounds(const gfx::Rect& bounds) { | 623 void set_bounds(const gfx::Rect& bounds) { |
| 566 bounds_ = bounds; | 624 bounds_ = bounds; |
| 567 } | 625 } |
| 568 | 626 |
| 569 void set_cursor_rect(const gfx::Rect& cursor_rect) { | 627 void set_cursor_rect(const gfx::Rect& cursor_rect) { |
| 570 cursor_rect_ = cursor_rect; | 628 cursor_bound_.edge_top = cursor_rect.origin(); |
| 629 cursor_bound_.edge_bottom = cursor_rect.bottom_left(); |
| 630 cursor_bound_.type = ui::SelectionBound::Type::CENTER; |
| 571 } | 631 } |
| 572 | 632 |
| 573 ~TestTouchEditable() override {} | 633 ~TestTouchEditable() override {} |
| 574 | 634 |
| 575 private: | 635 private: |
| 576 // Overridden from ui::TouchEditable. | 636 // Overridden from ui::TouchEditable. |
| 577 void SelectRect(const gfx::Point& start, const gfx::Point& end) override { | 637 void SelectRect(const gfx::Point& start, const gfx::Point& end) override { |
| 578 NOTREACHED(); | 638 NOTREACHED(); |
| 579 } | 639 } |
| 580 void MoveCaretTo(const gfx::Point& point) override { NOTREACHED(); } | 640 void MoveCaretTo(const gfx::Point& point) override { NOTREACHED(); } |
| 581 void GetSelectionEndPoints(gfx::Rect* p1, gfx::Rect* p2) override { | 641 void GetSelectionEndPoints(ui::SelectionBound* anchor, |
| 582 *p1 = *p2 = cursor_rect_; | 642 ui::SelectionBound* focus) override { |
| 643 *anchor = *focus = cursor_bound_; |
| 583 } | 644 } |
| 584 gfx::Rect GetBounds() override { return gfx::Rect(bounds_.size()); } | 645 gfx::Rect GetBounds() override { return gfx::Rect(bounds_.size()); } |
| 585 gfx::NativeView GetNativeView() const override { return window_; } | 646 gfx::NativeView GetNativeView() const override { return window_; } |
| 586 void ConvertPointToScreen(gfx::Point* point) override { | 647 void ConvertPointToScreen(gfx::Point* point) override { |
| 587 aura::client::ScreenPositionClient* screen_position_client = | 648 aura::client::ScreenPositionClient* screen_position_client = |
| 588 aura::client::GetScreenPositionClient(window_->GetRootWindow()); | 649 aura::client::GetScreenPositionClient(window_->GetRootWindow()); |
| 589 if (screen_position_client) | 650 if (screen_position_client) |
| 590 screen_position_client->ConvertPointToScreen(window_, point); | 651 screen_position_client->ConvertPointToScreen(window_, point); |
| 591 } | 652 } |
| 592 void ConvertPointFromScreen(gfx::Point* point) override { | 653 void ConvertPointFromScreen(gfx::Point* point) override { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 616 void ExecuteCommand(int command_id, int event_flags) override { | 677 void ExecuteCommand(int command_id, int event_flags) override { |
| 617 NOTREACHED(); | 678 NOTREACHED(); |
| 618 } | 679 } |
| 619 | 680 |
| 620 aura::Window* window_; | 681 aura::Window* window_; |
| 621 | 682 |
| 622 // Boundaries of the client view. | 683 // Boundaries of the client view. |
| 623 gfx::Rect bounds_; | 684 gfx::Rect bounds_; |
| 624 | 685 |
| 625 // Cursor position inside the client view. | 686 // Cursor position inside the client view. |
| 626 gfx::Rect cursor_rect_; | 687 //gfx::Rect cursor_rect_; |
| 688 ui::SelectionBound cursor_bound_; |
| 627 | 689 |
| 628 DISALLOW_COPY_AND_ASSIGN(TestTouchEditable); | 690 DISALLOW_COPY_AND_ASSIGN(TestTouchEditable); |
| 629 }; | 691 }; |
| 630 | 692 |
| 631 // Tests if the touch editing handle is shown or hidden properly according to | 693 // Tests if the touch editing handle is shown or hidden properly according to |
| 632 // the cursor position relative to the client boundaries. | 694 // the cursor position relative to the client boundaries. |
| 633 TEST_F(TouchSelectionControllerImplTest, | 695 TEST_F(TouchSelectionControllerImplTest, |
| 634 VisibilityOfHandleRegardingClientBounds) { | 696 VisibilityOfHandleRegardingClientBounds) { |
| 635 CreateWidget(); | 697 CreateWidget(); |
| 636 | 698 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 // image width and check that anchor rect's height is not adjusted. | 826 // image width and check that anchor rect's height is not adjusted. |
| 765 anchor_rect = | 827 anchor_rect = |
| 766 gfx::Rect(0, 0, quick_menu_width + handle_image_size.width() + 10, 20); | 828 gfx::Rect(0, 0, quick_menu_width + handle_image_size.width() + 10, 20); |
| 767 quick_menu = TouchEditingMenuView::Create( | 829 quick_menu = TouchEditingMenuView::Create( |
| 768 quick_menu_controller.get(), anchor_rect, handle_image_size, window); | 830 quick_menu_controller.get(), anchor_rect, handle_image_size, window); |
| 769 EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString()); | 831 EXPECT_EQ(anchor_rect.ToString(), quick_menu->GetAnchorRect().ToString()); |
| 770 | 832 |
| 771 // Close the widget, hence quick menus, before quick menu controller goes out | 833 // Close the widget, hence quick menus, before quick menu controller goes out |
| 772 // of scope. | 834 // of scope. |
| 773 widget_->CloseNow(); | 835 widget_->CloseNow(); |
| 774 widget_ = NULL; | 836 widget_ = nullptr; |
| 775 } | 837 } |
| 776 | 838 |
| 777 TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) { | 839 TEST_F(TouchSelectionControllerImplTest, MouseEventDeactivatesTouchSelection) { |
| 778 CreateTextfield(); | 840 CreateTextfield(); |
| 779 EXPECT_FALSE(GetSelectionController()); | 841 EXPECT_FALSE(GetSelectionController()); |
| 780 | 842 |
| 781 ui::test::EventGenerator generator( | 843 ui::test::EventGenerator generator( |
| 782 textfield_widget_->GetNativeView()->GetRootWindow()); | 844 textfield_widget_->GetNativeView()->GetRootWindow()); |
| 783 | 845 |
| 784 generator.set_current_location(gfx::Point(5, 5)); | 846 generator.set_current_location(gfx::Point(5, 5)); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 827 // Start touch editing; then press a key and ensure it deactivates touch | 889 // Start touch editing; then press a key and ensure it deactivates touch |
| 828 // selection. | 890 // selection. |
| 829 StartTouchEditing(); | 891 StartTouchEditing(); |
| 830 EXPECT_TRUE(GetSelectionController()); | 892 EXPECT_TRUE(GetSelectionController()); |
| 831 generator.PressKey(ui::VKEY_A, 0); | 893 generator.PressKey(ui::VKEY_A, 0); |
| 832 RunPendingMessages(); | 894 RunPendingMessages(); |
| 833 EXPECT_FALSE(GetSelectionController()); | 895 EXPECT_FALSE(GetSelectionController()); |
| 834 } | 896 } |
| 835 | 897 |
| 836 } // namespace views | 898 } // namespace views |
| OLD | NEW |