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 "content/browser/renderer_host/input/touch_selection_controller.h" | 5 #include "content/browser/renderer_host/input/touch_selection_controller.h" |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/events/test/motion_event_test_utils.h" | 8 #include "ui/events/test/motion_event_test_utils.h" |
9 | 9 |
10 using ui::test::MockMotionEvent; | 10 using ui::test::MockMotionEvent; |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 class TouchSelectionControllerTest : public testing::Test, | 38 class TouchSelectionControllerTest : public testing::Test, |
39 public TouchSelectionControllerClient { | 39 public TouchSelectionControllerClient { |
40 public: | 40 public: |
41 TouchSelectionControllerTest() | 41 TouchSelectionControllerTest() |
42 : last_event_(SELECTION_CLEARED), | 42 : last_event_(SELECTION_CLEARED), |
43 caret_moved_(false), | 43 caret_moved_(false), |
44 selection_moved_(false), | 44 selection_moved_(false), |
| 45 selection_points_swapped_(false), |
45 needs_animate_(false), | 46 needs_animate_(false), |
46 animation_enabled_(true), | 47 animation_enabled_(true), |
47 dragging_enabled_(false) {} | 48 dragging_enabled_(false) {} |
48 | 49 |
49 ~TouchSelectionControllerTest() override {} | 50 ~TouchSelectionControllerTest() override {} |
50 | 51 |
51 // testing::Test implementation. | 52 // testing::Test implementation. |
52 void SetUp() override { | 53 void SetUp() override { |
53 controller_.reset(new TouchSelectionController( | 54 controller_.reset(new TouchSelectionController( |
54 this, | 55 this, |
55 base::TimeDelta::FromMilliseconds(kDefaultTapTimeoutMs), | 56 base::TimeDelta::FromMilliseconds(kDefaultTapTimeoutMs), |
56 kDefaulTapSlop)); | 57 kDefaulTapSlop)); |
57 } | 58 } |
58 | 59 |
59 void TearDown() override { controller_.reset(); } | 60 void TearDown() override { controller_.reset(); } |
60 | 61 |
61 // TouchSelectionControllerClient implementation. | 62 // TouchSelectionControllerClient implementation. |
62 | 63 |
63 bool SupportsAnimation() const override { return animation_enabled_; } | 64 bool SupportsAnimation() const override { return animation_enabled_; } |
64 | 65 |
65 void SetNeedsAnimate() override { needs_animate_ = true; } | 66 void SetNeedsAnimate() override { needs_animate_ = true; } |
66 | 67 |
67 void MoveCaret(const gfx::PointF& position) override { | 68 void MoveCaret(const gfx::PointF& position) override { |
68 caret_moved_ = true; | 69 caret_moved_ = true; |
69 caret_position_ = position; | 70 caret_position_ = position; |
70 } | 71 } |
71 | 72 |
72 void SelectBetweenCoordinates(const gfx::PointF& start, | 73 void SelectBetweenCoordinates(const gfx::PointF& base, |
73 const gfx::PointF& end) override { | 74 const gfx::PointF& extent) override { |
| 75 if (base == selection_end_ && extent == selection_start_) |
| 76 selection_points_swapped_ = true; |
| 77 |
| 78 selection_start_ = base; |
| 79 selection_end_ = extent; |
| 80 } |
| 81 |
| 82 virtual void MoveRangeSelectionExtent(const gfx::PointF& extent) override { |
74 selection_moved_ = true; | 83 selection_moved_ = true; |
75 selection_start_ = start; | 84 selection_end_ = extent; |
76 selection_end_ = end; | |
77 } | 85 } |
78 | 86 |
79 void OnSelectionEvent(SelectionEventType event, | 87 void OnSelectionEvent(SelectionEventType event, |
80 const gfx::PointF& end_position) override { | 88 const gfx::PointF& end_position) override { |
81 last_event_ = event; | 89 last_event_ = event; |
82 last_event_start_ = end_position; | 90 last_event_start_ = end_position; |
83 } | 91 } |
84 | 92 |
85 scoped_ptr<TouchHandleDrawable> CreateDrawable() override { | 93 scoped_ptr<TouchHandleDrawable> CreateDrawable() override { |
86 return scoped_ptr<TouchHandleDrawable>( | 94 return scoped_ptr<TouchHandleDrawable>( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 caret_moved_ = false; | 149 caret_moved_ = false; |
142 return moved; | 150 return moved; |
143 } | 151 } |
144 | 152 |
145 bool GetAndResetSelectionMoved() { | 153 bool GetAndResetSelectionMoved() { |
146 bool moved = selection_moved_; | 154 bool moved = selection_moved_; |
147 selection_moved_ = false; | 155 selection_moved_ = false; |
148 return moved; | 156 return moved; |
149 } | 157 } |
150 | 158 |
| 159 bool GetAndResetSelectionPointsSwapped() { |
| 160 bool swapped = selection_points_swapped_; |
| 161 selection_points_swapped_ = false; |
| 162 return swapped; |
| 163 } |
| 164 |
151 const gfx::PointF& GetLastCaretPosition() const { return caret_position_; } | 165 const gfx::PointF& GetLastCaretPosition() const { return caret_position_; } |
152 const gfx::PointF& GetLastSelectionStart() const { return selection_start_; } | 166 const gfx::PointF& GetLastSelectionStart() const { return selection_start_; } |
153 const gfx::PointF& GetLastSelectionEnd() const { return selection_end_; } | 167 const gfx::PointF& GetLastSelectionEnd() const { return selection_end_; } |
154 SelectionEventType GetLastEventType() const { return last_event_; } | 168 SelectionEventType GetLastEventType() const { return last_event_; } |
155 const gfx::PointF& GetLastEventAnchor() const { return last_event_start_; } | 169 const gfx::PointF& GetLastEventAnchor() const { return last_event_start_; } |
156 | 170 |
157 TouchSelectionController& controller() { return *controller_; } | 171 TouchSelectionController& controller() { return *controller_; } |
158 | 172 |
159 private: | 173 private: |
160 gfx::PointF last_event_start_; | 174 gfx::PointF last_event_start_; |
161 gfx::PointF caret_position_; | 175 gfx::PointF caret_position_; |
162 gfx::PointF selection_start_; | 176 gfx::PointF selection_start_; |
163 gfx::PointF selection_end_; | 177 gfx::PointF selection_end_; |
164 SelectionEventType last_event_; | 178 SelectionEventType last_event_; |
165 bool caret_moved_; | 179 bool caret_moved_; |
166 bool selection_moved_; | 180 bool selection_moved_; |
| 181 bool selection_points_swapped_; |
167 bool needs_animate_; | 182 bool needs_animate_; |
168 bool animation_enabled_; | 183 bool animation_enabled_; |
169 bool dragging_enabled_; | 184 bool dragging_enabled_; |
170 scoped_ptr<TouchSelectionController> controller_; | 185 scoped_ptr<TouchSelectionController> controller_; |
171 }; | 186 }; |
172 | 187 |
173 TEST_F(TouchSelectionControllerTest, InsertionBasic) { | 188 TEST_F(TouchSelectionControllerTest, InsertionBasic) { |
174 gfx::RectF insertion_rect(5, 5, 0, 10); | 189 gfx::RectF insertion_rect(5, 5, 0, 10); |
175 bool visible = true; | 190 bool visible = true; |
176 | 191 |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 EXPECT_EQ(fixed_offset, GetLastSelectionStart()); | 610 EXPECT_EQ(fixed_offset, GetLastSelectionStart()); |
596 EXPECT_EQ(end_offset - gfx::Vector2dF(touch_down_x, 0), | 611 EXPECT_EQ(end_offset - gfx::Vector2dF(touch_down_x, 0), |
597 GetLastSelectionEnd()); | 612 GetLastSelectionEnd()); |
598 | 613 |
599 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0); | 614 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0); |
600 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); | 615 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
601 EXPECT_EQ(SELECTION_DRAG_STOPPED, GetLastEventType()); | 616 EXPECT_EQ(SELECTION_DRAG_STOPPED, GetLastEventType()); |
602 EXPECT_FALSE(GetAndResetSelectionMoved()); | 617 EXPECT_FALSE(GetAndResetSelectionMoved()); |
603 } | 618 } |
604 | 619 |
| 620 TEST_F(TouchSelectionControllerTest, SelectionDraggedToSwitchBaseAndExtent) { |
| 621 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 622 controller().OnLongPressEvent(); |
| 623 |
| 624 float line_height = 10.f; |
| 625 gfx::RectF start_rect(50, line_height, 0, line_height); |
| 626 gfx::RectF end_rect(100, line_height, 0, line_height); |
| 627 bool visible = true; |
| 628 ChangeSelection(start_rect, visible, end_rect, visible); |
| 629 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType()); |
| 630 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor()); |
| 631 |
| 632 SetDraggingEnabled(true); |
| 633 |
| 634 // Move the extent, not triggering a swap of points. |
| 635 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, |
| 636 end_rect.x(), end_rect.bottom()); |
| 637 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 638 EXPECT_FALSE(GetAndResetSelectionMoved()); |
| 639 EXPECT_FALSE(GetAndResetSelectionPointsSwapped()); |
| 640 |
| 641 gfx::PointF base_offset = start_rect.CenterPoint(); |
| 642 gfx::PointF extent_offset = end_rect.CenterPoint(); |
| 643 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, |
| 644 end_rect.x(), end_rect.bottom() + 5); |
| 645 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 646 EXPECT_EQ(SELECTION_DRAG_STARTED, GetLastEventType()); |
| 647 EXPECT_TRUE(GetAndResetSelectionMoved()); |
| 648 EXPECT_FALSE(GetAndResetSelectionPointsSwapped()); |
| 649 EXPECT_EQ(base_offset, GetLastSelectionStart()); |
| 650 EXPECT_EQ(extent_offset + gfx::Vector2dF(0, 5), GetLastSelectionEnd()); |
| 651 |
| 652 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 10, 5); |
| 653 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 654 EXPECT_EQ(SELECTION_DRAG_STOPPED, GetLastEventType()); |
| 655 EXPECT_FALSE(GetAndResetSelectionMoved()); |
| 656 |
| 657 end_rect += gfx::Vector2dF(0, 5); |
| 658 ChangeSelection(start_rect, visible, end_rect, visible); |
| 659 |
| 660 // Move the base, triggering a swap of points. |
| 661 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, |
| 662 start_rect.x(), start_rect.bottom()); |
| 663 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 664 EXPECT_FALSE(GetAndResetSelectionMoved()); |
| 665 EXPECT_TRUE(GetAndResetSelectionPointsSwapped()); |
| 666 |
| 667 base_offset = end_rect.CenterPoint(); |
| 668 extent_offset = start_rect.CenterPoint(); |
| 669 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, |
| 670 start_rect.x(), start_rect.bottom() + 5); |
| 671 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 672 EXPECT_EQ(SELECTION_DRAG_STARTED, GetLastEventType()); |
| 673 EXPECT_TRUE(GetAndResetSelectionMoved()); |
| 674 EXPECT_FALSE(GetAndResetSelectionPointsSwapped()); |
| 675 EXPECT_EQ(base_offset, GetLastSelectionStart()); |
| 676 EXPECT_EQ(extent_offset + gfx::Vector2dF(0, 5), GetLastSelectionEnd()); |
| 677 |
| 678 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 10, 5); |
| 679 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 680 EXPECT_EQ(SELECTION_DRAG_STOPPED, GetLastEventType()); |
| 681 EXPECT_FALSE(GetAndResetSelectionMoved()); |
| 682 |
| 683 start_rect += gfx::Vector2dF(0, 5); |
| 684 ChangeSelection(start_rect, visible, end_rect, visible); |
| 685 |
| 686 // Move the same point again, not triggering a swap of points. |
| 687 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, |
| 688 start_rect.x(), start_rect.bottom()); |
| 689 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 690 EXPECT_FALSE(GetAndResetSelectionMoved()); |
| 691 EXPECT_FALSE(GetAndResetSelectionPointsSwapped()); |
| 692 |
| 693 base_offset = end_rect.CenterPoint(); |
| 694 extent_offset = start_rect.CenterPoint(); |
| 695 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, |
| 696 start_rect.x(), start_rect.bottom() + 5); |
| 697 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 698 EXPECT_EQ(SELECTION_DRAG_STARTED, GetLastEventType()); |
| 699 EXPECT_TRUE(GetAndResetSelectionMoved()); |
| 700 EXPECT_FALSE(GetAndResetSelectionPointsSwapped()); |
| 701 EXPECT_EQ(base_offset, GetLastSelectionStart()); |
| 702 EXPECT_EQ(extent_offset + gfx::Vector2dF(0, 5), GetLastSelectionEnd()); |
| 703 |
| 704 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 10, 5); |
| 705 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 706 EXPECT_EQ(SELECTION_DRAG_STOPPED, GetLastEventType()); |
| 707 EXPECT_FALSE(GetAndResetSelectionMoved()); |
| 708 |
| 709 start_rect += gfx::Vector2dF(0, 5); |
| 710 ChangeSelection(start_rect, visible, end_rect, visible); |
| 711 |
| 712 // Move the base, triggering a swap of points. |
| 713 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, |
| 714 end_rect.x(), end_rect.bottom()); |
| 715 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 716 EXPECT_FALSE(GetAndResetSelectionMoved()); |
| 717 EXPECT_TRUE(GetAndResetSelectionPointsSwapped()); |
| 718 |
| 719 base_offset = start_rect.CenterPoint(); |
| 720 extent_offset = end_rect.CenterPoint(); |
| 721 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, |
| 722 end_rect.x(), end_rect.bottom() + 5); |
| 723 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 724 EXPECT_EQ(SELECTION_DRAG_STARTED, GetLastEventType()); |
| 725 EXPECT_TRUE(GetAndResetSelectionMoved()); |
| 726 EXPECT_FALSE(GetAndResetSelectionPointsSwapped()); |
| 727 EXPECT_EQ(base_offset, GetLastSelectionStart()); |
| 728 EXPECT_EQ(extent_offset + gfx::Vector2dF(0, 5), GetLastSelectionEnd()); |
| 729 |
| 730 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 10, 5); |
| 731 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); |
| 732 EXPECT_EQ(SELECTION_DRAG_STOPPED, GetLastEventType()); |
| 733 EXPECT_FALSE(GetAndResetSelectionMoved()); |
| 734 } |
| 735 |
605 TEST_F(TouchSelectionControllerTest, Animation) { | 736 TEST_F(TouchSelectionControllerTest, Animation) { |
606 controller().OnTapEvent(); | 737 controller().OnTapEvent(); |
607 controller().OnSelectionEditable(true); | 738 controller().OnSelectionEditable(true); |
608 | 739 |
609 gfx::RectF insertion_rect(5, 5, 0, 10); | 740 gfx::RectF insertion_rect(5, 5, 0, 10); |
610 | 741 |
611 bool visible = true; | 742 bool visible = true; |
612 ChangeInsertion(insertion_rect, visible); | 743 ChangeInsertion(insertion_rect, visible); |
613 EXPECT_FALSE(GetAndResetNeedsAnimate()); | 744 EXPECT_FALSE(GetAndResetNeedsAnimate()); |
614 | 745 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType()); | 801 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType()); |
671 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor()); | 802 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor()); |
672 | 803 |
673 controller().OnTapEvent(); | 804 controller().OnTapEvent(); |
674 ClearSelection(); | 805 ClearSelection(); |
675 EXPECT_EQ(SELECTION_CLEARED, GetLastEventType()); | 806 EXPECT_EQ(SELECTION_CLEARED, GetLastEventType()); |
676 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor()); | 807 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor()); |
677 } | 808 } |
678 | 809 |
679 } // namespace content | 810 } // namespace content |
OLD | NEW |