Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/touch_selection/touch_selection_controller.h" | 5 #include "ui/touch_selection/touch_selection_controller.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 events_.push_back(event); | 99 events_.push_back(event); |
| 100 last_event_start_ = controller_->GetStartPosition(); | 100 last_event_start_ = controller_->GetStartPosition(); |
| 101 last_event_end_ = controller_->GetEndPosition(); | 101 last_event_end_ = controller_->GetEndPosition(); |
| 102 last_event_bounds_rect_ = controller_->GetRectBetweenBounds(); | 102 last_event_bounds_rect_ = controller_->GetRectBetweenBounds(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 std::unique_ptr<TouchHandleDrawable> CreateDrawable() override { | 105 std::unique_ptr<TouchHandleDrawable> CreateDrawable() override { |
| 106 return base::WrapUnique(new MockTouchHandleDrawable(&dragging_enabled_)); | 106 return base::WrapUnique(new MockTouchHandleDrawable(&dragging_enabled_)); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void AllowShowingOnTapForEmptyEditable() { | |
| 110 TouchSelectionController::Config config = DefaultConfig(); | |
| 111 config.show_on_tap_for_empty_editable = true; | |
| 112 controller_.reset(new TouchSelectionController(this, config)); | |
| 113 } | |
| 114 | |
| 115 void EnableLongPressDragSelection() { | 109 void EnableLongPressDragSelection() { |
| 116 TouchSelectionController::Config config = DefaultConfig(); | 110 TouchSelectionController::Config config = DefaultConfig(); |
| 117 config.enable_longpress_drag_selection = true; | 111 config.enable_longpress_drag_selection = true; |
| 118 controller_.reset(new TouchSelectionController(this, config)); | 112 controller_.reset(new TouchSelectionController(this, config)); |
| 119 } | 113 } |
| 120 | 114 |
| 121 void SetAnimationEnabled(bool enabled) { animation_enabled_ = enabled; } | 115 void SetAnimationEnabled(bool enabled) { animation_enabled_ = enabled; } |
| 122 void SetDraggingEnabled(bool enabled) { dragging_enabled_ = enabled; } | 116 void SetDraggingEnabled(bool enabled) { dragging_enabled_ = enabled; } |
| 123 | 117 |
| 124 void ClearSelection() { | 118 void ClearSelection() { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 std::vector<SelectionEventType> GetAndResetEvents() { | 201 std::vector<SelectionEventType> GetAndResetEvents() { |
| 208 std::vector<SelectionEventType> events; | 202 std::vector<SelectionEventType> events; |
| 209 events.swap(events_); | 203 events.swap(events_); |
| 210 return events; | 204 return events; |
| 211 } | 205 } |
| 212 | 206 |
| 213 TouchSelectionController& controller() { return *controller_; } | 207 TouchSelectionController& controller() { return *controller_; } |
| 214 | 208 |
| 215 private: | 209 private: |
| 216 TouchSelectionController::Config DefaultConfig() { | 210 TouchSelectionController::Config DefaultConfig() { |
| 217 // Both |show_on_tap_for_empty_editable| and | 211 // |enable_longpress_drag_selection| is set to false by default, and should |
| 218 // |enable_longpress_drag_selection| are set to false by default, and should | |
| 219 // be overriden for explicit testing. | 212 // be overriden for explicit testing. |
| 220 TouchSelectionController::Config config; | 213 TouchSelectionController::Config config; |
| 221 config.max_tap_duration = | 214 config.max_tap_duration = |
| 222 base::TimeDelta::FromMilliseconds(kDefaultTapTimeoutMs); | 215 base::TimeDelta::FromMilliseconds(kDefaultTapTimeoutMs); |
| 223 config.tap_slop = kDefaulTapSlop; | 216 config.tap_slop = kDefaulTapSlop; |
| 224 config.show_on_tap_for_empty_editable = false; | |
| 225 config.enable_longpress_drag_selection = false; | 217 config.enable_longpress_drag_selection = false; |
| 226 return config; | 218 return config; |
| 227 } | 219 } |
| 228 | 220 |
| 229 gfx::PointF last_event_start_; | 221 gfx::PointF last_event_start_; |
| 230 gfx::PointF last_event_end_; | 222 gfx::PointF last_event_end_; |
| 231 gfx::PointF caret_position_; | 223 gfx::PointF caret_position_; |
| 232 gfx::PointF selection_start_; | 224 gfx::PointF selection_start_; |
| 233 gfx::PointF selection_end_; | 225 gfx::PointF selection_end_; |
| 234 gfx::RectF last_event_bounds_rect_; | 226 gfx::RectF last_event_bounds_rect_; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 | 281 |
| 290 ChangeInsertion(insertion_rect, visible); | 282 ChangeInsertion(insertion_rect, visible); |
| 291 EXPECT_THAT(GetAndResetEvents(), | 283 EXPECT_THAT(GetAndResetEvents(), |
| 292 ElementsAre(SELECTION_ESTABLISHED, INSERTION_HANDLE_SHOWN)); | 284 ElementsAre(SELECTION_ESTABLISHED, INSERTION_HANDLE_SHOWN)); |
| 293 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); | 285 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); |
| 294 | 286 |
| 295 controller().OnSelectionEditable(false); | 287 controller().OnSelectionEditable(false); |
| 296 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_HANDLE_CLEARED)); | 288 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_HANDLE_CLEARED)); |
| 297 } | 289 } |
| 298 | 290 |
| 299 TEST_F(TouchSelectionControllerTest, InsertionWithNoShowOnTapForEmptyEditable) { | 291 TEST_F(TouchSelectionControllerTest, InsertionWithNoShowOnTapForEmptyEditable) { |
|
mohsen
2016/08/10 21:09:53
nit: Please remove the reference to the removed fl
| |
| 300 gfx::RectF insertion_rect(5, 5, 0, 10); | 292 gfx::RectF insertion_rect(5, 5, 0, 10); |
| 301 bool visible = true; | 293 bool visible = true; |
| 302 controller().OnSelectionEditable(true); | 294 controller().OnSelectionEditable(true); |
| 303 | 295 |
| 304 // Taps on an empty editable region should be ignored if the controller is | 296 // Taps on an empty editable region should be ignored |
| 305 // created with |show_on_tap_for_empty_editable| set to false. | |
| 306 OnTapEvent(); | 297 OnTapEvent(); |
| 307 controller().OnSelectionEmpty(true); | 298 controller().OnSelectionEmpty(true); |
| 308 ChangeInsertion(insertion_rect, visible); | 299 ChangeInsertion(insertion_rect, visible); |
| 309 EXPECT_EQ(gfx::PointF(), GetLastEventStart()); | 300 EXPECT_EQ(gfx::PointF(), GetLastEventStart()); |
| 310 | 301 |
| 311 // Once the region becomes non-empty, taps should show the insertion handle. | 302 // Once the region becomes non-empty, taps should show the insertion handle. |
| 312 OnTapEvent(); | 303 OnTapEvent(); |
| 313 controller().OnSelectionEmpty(false); | 304 controller().OnSelectionEmpty(false); |
| 314 ChangeInsertion(insertion_rect, visible); | 305 ChangeInsertion(insertion_rect, visible); |
| 315 EXPECT_THAT(GetAndResetEvents(), | 306 EXPECT_THAT(GetAndResetEvents(), |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 326 controller().OnSelectionEmpty(true); | 317 controller().OnSelectionEmpty(true); |
| 327 ChangeInsertion(insertion_rect, visible); | 318 ChangeInsertion(insertion_rect, visible); |
| 328 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_HANDLE_SHOWN)); | 319 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_HANDLE_SHOWN)); |
| 329 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); | 320 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); |
| 330 | 321 |
| 331 // Single Tap on an empty edit field should clear insertion handle. | 322 // Single Tap on an empty edit field should clear insertion handle. |
| 332 OnTapEvent(); | 323 OnTapEvent(); |
| 333 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_HANDLE_CLEARED)); | 324 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_HANDLE_CLEARED)); |
| 334 } | 325 } |
| 335 | 326 |
| 336 TEST_F(TouchSelectionControllerTest, InsertionWithShowOnTapForEmptyEditable) { | |
| 337 AllowShowingOnTapForEmptyEditable(); | |
| 338 | |
| 339 gfx::RectF insertion_rect(5, 5, 0, 10); | |
| 340 bool visible = true; | |
| 341 controller().OnSelectionEditable(true); | |
| 342 | |
| 343 // Taps on an empty editable region should show the insertion handle if the | |
| 344 // controller is created with |show_on_tap_for_empty_editable| set to true. | |
| 345 OnTapEvent(); | |
| 346 controller().OnSelectionEmpty(true); | |
| 347 ChangeInsertion(insertion_rect, visible); | |
| 348 EXPECT_THAT(GetAndResetEvents(), | |
| 349 ElementsAre(SELECTION_ESTABLISHED, INSERTION_HANDLE_SHOWN)); | |
| 350 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); | |
| 351 | |
| 352 // Additional taps should not hide the insertion handle in this case. | |
| 353 OnTapEvent(); | |
| 354 ChangeInsertion(insertion_rect, visible); | |
| 355 EXPECT_THAT(GetAndResetEvents(), IsEmpty()); | |
| 356 } | |
| 357 | |
| 358 TEST_F(TouchSelectionControllerTest, InsertionAppearsAfterTapFollowingTyping) { | 327 TEST_F(TouchSelectionControllerTest, InsertionAppearsAfterTapFollowingTyping) { |
| 359 gfx::RectF insertion_rect(5, 5, 0, 10); | 328 gfx::RectF insertion_rect(5, 5, 0, 10); |
| 360 bool visible = true; | 329 bool visible = true; |
| 361 | 330 |
| 362 // Simulate the user tapping an empty text field. | 331 // Simulate the user tapping an empty text field. |
| 363 OnTapEvent(); | 332 OnTapEvent(); |
| 364 controller().OnSelectionEditable(true); | 333 controller().OnSelectionEditable(true); |
| 365 controller().OnSelectionEmpty(true); | 334 controller().OnSelectionEmpty(true); |
| 366 ChangeInsertion(insertion_rect, visible); | 335 ChangeInsertion(insertion_rect, visible); |
| 367 EXPECT_EQ(gfx::PointF(), GetLastEventStart()); | 336 EXPECT_EQ(gfx::PointF(), GetLastEventStart()); |
| (...skipping 1107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1475 offset_rect.x(), offset_rect.bottom()); | 1444 offset_rect.x(), offset_rect.bottom()); |
| 1476 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); | 1445 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 1477 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_HANDLE_DRAG_STOPPED)); | 1446 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_HANDLE_DRAG_STOPPED)); |
| 1478 EXPECT_EQ(test_controller.GetStartHandleOrientation(), | 1447 EXPECT_EQ(test_controller.GetStartHandleOrientation(), |
| 1479 TouchHandleOrientation::LEFT); | 1448 TouchHandleOrientation::LEFT); |
| 1480 EXPECT_EQ(test_controller.GetEndHandleOrientation(), | 1449 EXPECT_EQ(test_controller.GetEndHandleOrientation(), |
| 1481 TouchHandleOrientation::RIGHT); | 1450 TouchHandleOrientation::RIGHT); |
| 1482 } | 1451 } |
| 1483 | 1452 |
| 1484 } // namespace ui | 1453 } // namespace ui |
| OLD | NEW |