Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: ui/touch_selection/touch_selection_controller_unittest.cc

Issue 1087893003: Support longpress drag selection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code review Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 25 matching lines...) Expand all
36 } 36 }
37 37
38 private: 38 private:
39 bool* intersects_rect_; 39 bool* intersects_rect_;
40 40
41 DISALLOW_COPY_AND_ASSIGN(MockTouchHandleDrawable); 41 DISALLOW_COPY_AND_ASSIGN(MockTouchHandleDrawable);
42 }; 42 };
43 43
44 } // namespace 44 } // namespace
45 45
46 class TouchSelectionControllerTestApi {
47 public:
48 explicit TouchSelectionControllerTestApi(TouchSelectionController* controller)
49 : controller_(controller) {}
50 ~TouchSelectionControllerTestApi() {}
51
52 bool GetStartVisible() const { return controller_->GetStartVisible(); }
53 bool GetEndVisible() const { return controller_->GetEndVisible(); }
54
55 private:
56 TouchSelectionController* controller_;
57
58 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerTestApi);
59 };
60
46 class TouchSelectionControllerTest : public testing::Test, 61 class TouchSelectionControllerTest : public testing::Test,
47 public TouchSelectionControllerClient { 62 public TouchSelectionControllerClient {
48 public: 63 public:
49 TouchSelectionControllerTest() 64 TouchSelectionControllerTest()
50 : caret_moved_(false), 65 : caret_moved_(false),
51 selection_moved_(false), 66 selection_moved_(false),
52 selection_points_swapped_(false), 67 selection_points_swapped_(false),
53 needs_animate_(false), 68 needs_animate_(false),
54 animation_enabled_(true), 69 animation_enabled_(true),
55 dragging_enabled_(false) {} 70 dragging_enabled_(false) {}
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 start_bound.set_type(SelectionBound::LEFT); 157 start_bound.set_type(SelectionBound::LEFT);
143 end_bound.set_type(SelectionBound::RIGHT); 158 end_bound.set_type(SelectionBound::RIGHT);
144 start_bound.SetEdge(start_rect.origin(), start_rect.bottom_left()); 159 start_bound.SetEdge(start_rect.origin(), start_rect.bottom_left());
145 end_bound.SetEdge(end_rect.origin(), end_rect.bottom_left()); 160 end_bound.SetEdge(end_rect.origin(), end_rect.bottom_left());
146 start_bound.set_visible(start_visible); 161 start_bound.set_visible(start_visible);
147 end_bound.set_visible(end_visible); 162 end_bound.set_visible(end_visible);
148 controller_->OnSelectionBoundsChanged(start_bound, end_bound); 163 controller_->OnSelectionBoundsChanged(start_bound, end_bound);
149 } 164 }
150 165
151 void OnLongPressEvent() { 166 void OnLongPressEvent() {
152 ASSERT_FALSE(controller().WillHandleLongPressEvent(kIgnoredPoint)); 167 ASSERT_FALSE(controller().WillHandleLongPressEvent(base::TimeTicks(),
168 kIgnoredPoint));
153 } 169 }
154 170
155 void OnTapEvent() { 171 void OnTapEvent() {
156 ASSERT_FALSE(controller().WillHandleTapEvent(kIgnoredPoint)); 172 ASSERT_FALSE(controller().WillHandleTapEvent(kIgnoredPoint));
157 } 173 }
158 174
159 void Animate() { 175 void Animate() {
160 base::TimeTicks now = base::TimeTicks::Now(); 176 base::TimeTicks now = base::TimeTicks::Now();
161 while (needs_animate_) { 177 while (needs_animate_) {
162 needs_animate_ = controller_->Animate(now); 178 needs_animate_ = controller_->Animate(now);
(...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 886
871 // If the client doesn't support animation, no animation should be triggered. 887 // If the client doesn't support animation, no animation should be triggered.
872 SetAnimationEnabled(false); 888 SetAnimationEnabled(false);
873 OnTapEvent(); 889 OnTapEvent();
874 visible = true; 890 visible = true;
875 ChangeInsertion(insertion_rect, visible); 891 ChangeInsertion(insertion_rect, visible);
876 EXPECT_FALSE(GetAndResetNeedsAnimate()); 892 EXPECT_FALSE(GetAndResetNeedsAnimate());
877 } 893 }
878 894
879 TEST_F(TouchSelectionControllerTest, TemporarilyHidden) { 895 TEST_F(TouchSelectionControllerTest, TemporarilyHidden) {
896 TouchSelectionControllerTestApi test_controller(&controller());
897
880 OnTapEvent(); 898 OnTapEvent();
881 controller().OnSelectionEditable(true); 899 controller().OnSelectionEditable(true);
882 900
883 gfx::RectF insertion_rect(5, 5, 0, 10); 901 gfx::RectF insertion_rect(5, 5, 0, 10);
884 902
885 bool visible = true; 903 bool visible = true;
886 ChangeInsertion(insertion_rect, visible); 904 ChangeInsertion(insertion_rect, visible);
887 EXPECT_FALSE(GetAndResetNeedsAnimate()); 905 EXPECT_FALSE(GetAndResetNeedsAnimate());
906 EXPECT_TRUE(test_controller.GetStartVisible());
907 EXPECT_TRUE(test_controller.GetEndVisible());
888 908
889 controller().SetTemporarilyHidden(true); 909 controller().SetTemporarilyHidden(true);
890 EXPECT_TRUE(GetAndResetNeedsAnimate()); 910 EXPECT_TRUE(GetAndResetNeedsAnimate());
911 EXPECT_FALSE(test_controller.GetStartVisible());
912 EXPECT_FALSE(test_controller.GetEndVisible());
891 913
892 visible = false; 914 visible = false;
893 ChangeInsertion(insertion_rect, visible); 915 ChangeInsertion(insertion_rect, visible);
894 EXPECT_FALSE(GetAndResetNeedsAnimate()); 916 EXPECT_FALSE(GetAndResetNeedsAnimate());
917 EXPECT_FALSE(test_controller.GetStartVisible());
895 918
896 visible = true; 919 visible = true;
897 ChangeInsertion(insertion_rect, visible); 920 ChangeInsertion(insertion_rect, visible);
898 EXPECT_FALSE(GetAndResetNeedsAnimate()); 921 EXPECT_FALSE(GetAndResetNeedsAnimate());
922 EXPECT_FALSE(test_controller.GetStartVisible());
899 923
900 controller().SetTemporarilyHidden(false); 924 controller().SetTemporarilyHidden(false);
901 EXPECT_TRUE(GetAndResetNeedsAnimate()); 925 EXPECT_TRUE(GetAndResetNeedsAnimate());
926 EXPECT_TRUE(test_controller.GetStartVisible());
902 } 927 }
903 928
904 TEST_F(TouchSelectionControllerTest, SelectionClearOnTap) { 929 TEST_F(TouchSelectionControllerTest, SelectionClearOnTap) {
905 gfx::RectF start_rect(5, 5, 0, 10); 930 gfx::RectF start_rect(5, 5, 0, 10);
906 gfx::RectF end_rect(50, 5, 0, 10); 931 gfx::RectF end_rect(50, 5, 0, 10);
907 bool visible = true; 932 bool visible = true;
908 933
909 OnLongPressEvent(); 934 OnLongPressEvent();
910 ChangeSelection(start_rect, visible, end_rect, visible); 935 ChangeSelection(start_rect, visible, end_rect, visible);
911 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 936 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 gfx::RectF start_rect(5, 5, 0, 10); 1035 gfx::RectF start_rect(5, 5, 0, 10);
1011 gfx::RectF end_rect(50, 5, 0, 10); 1036 gfx::RectF end_rect(50, 5, 0, 10);
1012 gfx::PointF inner_point(25, 10); 1037 gfx::PointF inner_point(25, 10);
1013 gfx::PointF outer_point(100, 100); 1038 gfx::PointF outer_point(100, 100);
1014 1039
1015 // Establish a selection without handles from 5 to 50 with height 10. 1040 // Establish a selection without handles from 5 to 50 with height 10.
1016 ChangeSelection(start_rect, visible, end_rect, visible); 1041 ChangeSelection(start_rect, visible, end_rect, visible);
1017 EXPECT_THAT(GetAndResetEvents(), IsEmpty()); 1042 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
1018 1043
1019 // A point outside the rect should not be handled. 1044 // A point outside the rect should not be handled.
1020 EXPECT_FALSE(controller().WillHandleLongPressEvent(outer_point)); 1045 EXPECT_FALSE(
1046 controller().WillHandleLongPressEvent(base::TimeTicks(), outer_point));
1021 EXPECT_THAT(GetAndResetEvents(), IsEmpty()); 1047 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
1022 1048
1023 // A point inside the rect should be handled. 1049 // A point inside the rect should be handled.
1024 EXPECT_TRUE(controller().WillHandleLongPressEvent(inner_point)); 1050 EXPECT_TRUE(
1051 controller().WillHandleLongPressEvent(base::TimeTicks(), inner_point));
1025 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 1052 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
1026 } 1053 }
1027 1054
1055 TEST_F(TouchSelectionControllerTest, LongPressDrag) {
1056 TouchSelectionControllerTestApi test_controller(&controller());
1057
1058 gfx::RectF start_rect(-50, 0, 0, 10);
1059 gfx::RectF end_rect(50, 0, 0, 10);
1060 bool visible = true;
1061
1062 // Start a touch sequence.
1063 MockMotionEvent event;
1064 EXPECT_FALSE(controller().WillHandleTouchEvent(event.PressPoint(0, 0)));
1065
1066 // Activate a longpress-triggered selection.
1067 OnLongPressEvent();
1068 ChangeSelection(start_rect, visible, end_rect, visible);
1069 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
1070 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
1071
1072 // The handles should remain invisible while the touch release and longpress
1073 // drag gesture are pending.
1074 EXPECT_FALSE(test_controller.GetStartVisible());
1075 EXPECT_FALSE(test_controller.GetEndVisible());
1076
1077 // The selection coordinates should reflect the drag movement.
1078 gfx::PointF fixed_offset = start_rect.CenterPoint();
1079 gfx::PointF end_offset = end_rect.CenterPoint();
1080 EXPECT_TRUE(controller().WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
1081 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
1082
1083 EXPECT_TRUE(
1084 controller().WillHandleTouchEvent(event.MovePoint(0, 0, kDefaulTapSlop)));
1085 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STARTED));
1086 EXPECT_EQ(fixed_offset, GetLastSelectionStart());
1087 EXPECT_EQ(end_offset, GetLastSelectionEnd());
1088
1089 // Movement after the start of drag will be relative to the moved endpoint.
1090 EXPECT_TRUE(controller().WillHandleTouchEvent(
1091 event.MovePoint(0, 0, 2 * kDefaulTapSlop)));
1092 EXPECT_TRUE(GetAndResetSelectionMoved());
1093 EXPECT_EQ(end_offset + gfx::Vector2dF(0, kDefaulTapSlop),
1094 GetLastSelectionEnd());
1095
1096 EXPECT_TRUE(controller().WillHandleTouchEvent(
1097 event.MovePoint(0, kDefaulTapSlop, 2 * kDefaulTapSlop)));
1098 EXPECT_TRUE(GetAndResetSelectionMoved());
1099 EXPECT_EQ(end_offset + gfx::Vector2dF(kDefaulTapSlop, kDefaulTapSlop),
1100 GetLastSelectionEnd());
1101
1102 EXPECT_TRUE(controller().WillHandleTouchEvent(
1103 event.MovePoint(0, 2 * kDefaulTapSlop, 2 * kDefaulTapSlop)));
1104 EXPECT_TRUE(GetAndResetSelectionMoved());
1105 EXPECT_EQ(end_offset + gfx::Vector2dF(2 * kDefaulTapSlop, kDefaulTapSlop),
1106 GetLastSelectionEnd());
1107
1108 // The handles should still be hidden.
1109 EXPECT_FALSE(test_controller.GetStartVisible());
1110 EXPECT_FALSE(test_controller.GetEndVisible());
1111
1112 // Releasing the touch sequence should end the drag and show the handles.
1113 EXPECT_FALSE(controller().WillHandleTouchEvent(event.ReleasePoint()));
1114 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STOPPED));
1115 EXPECT_TRUE(test_controller.GetStartVisible());
1116 EXPECT_TRUE(test_controller.GetEndVisible());
1117 }
1118
1119 TEST_F(TouchSelectionControllerTest, LongPressNoDrag) {
1120 TouchSelectionControllerTestApi test_controller(&controller());
1121
1122 gfx::RectF start_rect(-50, 0, 0, 10);
1123 gfx::RectF end_rect(50, 0, 0, 10);
1124 bool visible = true;
1125
1126 // Start a touch sequence.
1127 MockMotionEvent event;
1128 EXPECT_FALSE(controller().WillHandleTouchEvent(event.PressPoint(0, 0)));
1129
1130 // Activate a longpress-triggered selection.
1131 OnLongPressEvent();
1132 ChangeSelection(start_rect, visible, end_rect, visible);
1133 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
1134 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
1135
1136 // The handles should remain invisible while the touch release and longpress
1137 // drag gesture are pending.
1138 EXPECT_FALSE(test_controller.GetStartVisible());
1139 EXPECT_FALSE(test_controller.GetEndVisible());
1140
1141 // If no drag movement occurs, the handles should reappear after the touch
1142 // is released.
1143 EXPECT_FALSE(controller().WillHandleTouchEvent(event.ReleasePoint()));
1144 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
1145 EXPECT_TRUE(test_controller.GetStartVisible());
1146 EXPECT_TRUE(test_controller.GetEndVisible());
1147 }
1148
1028 } // namespace ui 1149 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698