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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "content/browser/renderer_host/input/gesture_text_selector.h" | 10 #include "content/browser/renderer_host/input/gesture_text_selector.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "ui/events/event_constants.h" | 12 #include "ui/events/event_constants.h" |
13 #include "ui/events/gesture_detection/gesture_event_data.h" | |
14 #include "ui/events/gesture_detection/motion_event.h" | 13 #include "ui/events/gesture_detection/motion_event.h" |
15 #include "ui/events/test/mock_motion_event.h" | 14 #include "ui/events/test/mock_motion_event.h" |
16 #include "ui/gfx/geometry/rect_f.h" | 15 #include "ui/gfx/geometry/rect_f.h" |
17 | 16 |
18 using ui::GestureEventData; | |
19 using ui::GestureEventDetails; | |
20 using ui::MotionEvent; | 17 using ui::MotionEvent; |
21 using ui::test::MockMotionEvent; | 18 using ui::test::MockMotionEvent; |
22 | 19 |
23 namespace content { | 20 namespace content { |
24 | 21 |
25 class GestureTextSelectorTest : public testing::Test, | 22 class GestureTextSelectorTest : public testing::Test, |
26 public GestureTextSelectorClient { | 23 public GestureTextSelectorClient { |
27 public: | 24 public: |
28 GestureTextSelectorTest() {} | 25 GestureTextSelectorTest() {} |
29 virtual ~GestureTextSelectorTest() {} | 26 virtual ~GestureTextSelectorTest() {} |
(...skipping 11 matching lines...) Expand all Loading... |
41 | 38 |
42 // GestureTextSelectorClient implementation. | 39 // GestureTextSelectorClient implementation. |
43 virtual void ShowSelectionHandlesAutomatically() OVERRIDE { | 40 virtual void ShowSelectionHandlesAutomatically() OVERRIDE { |
44 event_log_.push_back("Show"); | 41 event_log_.push_back("Show"); |
45 } | 42 } |
46 | 43 |
47 virtual void SelectRange(float x1, float y1, float x2, float y2) OVERRIDE { | 44 virtual void SelectRange(float x1, float y1, float x2, float y2) OVERRIDE { |
48 event_log_.push_back("SelectRange"); | 45 event_log_.push_back("SelectRange"); |
49 } | 46 } |
50 | 47 |
51 virtual void Unselect() OVERRIDE { | |
52 event_log_.push_back("Unselect"); | |
53 } | |
54 | |
55 virtual void LongPress(base::TimeTicks time, float x, float y) OVERRIDE { | 48 virtual void LongPress(base::TimeTicks time, float x, float y) OVERRIDE { |
56 event_log_.push_back("LongPress"); | 49 event_log_.push_back("LongPress"); |
57 } | 50 } |
58 | 51 |
59 protected: | 52 protected: |
60 static GestureEventData CreateGesture(ui::EventType type, | |
61 base::TimeTicks event_time, | |
62 float x, | |
63 float y) { | |
64 return GestureEventData(GestureEventDetails(type), | |
65 0, | |
66 MotionEvent::TOOL_TYPE_FINGER, | |
67 event_time, | |
68 x, | |
69 y, | |
70 x, | |
71 y, | |
72 1, | |
73 gfx::RectF(0, 0, 0, 0), | |
74 0); | |
75 } | |
76 | |
77 scoped_ptr<GestureTextSelector> selector_; | 53 scoped_ptr<GestureTextSelector> selector_; |
78 std::vector<std::string> event_log_; | 54 std::vector<std::string> event_log_; |
79 }; | 55 }; |
80 | 56 |
81 TEST_F(GestureTextSelectorTest, ShouldStartTextSelection) { | 57 TEST_F(GestureTextSelectorTest, ShouldStartTextSelection) { |
82 base::TimeTicks event_time = base::TimeTicks::Now(); | 58 base::TimeTicks event_time = base::TimeTicks::Now(); |
83 { // Touched with a finger. | 59 { // Touched with a finger. |
84 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | 60 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); |
85 e.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER); | 61 e.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER); |
86 e.set_button_state(0); | 62 e.set_button_state(0); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 action_down.set_button_state(MotionEvent::BUTTON_SECONDARY); | 111 action_down.set_button_state(MotionEvent::BUTTON_SECONDARY); |
136 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); | 112 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); |
137 EXPECT_TRUE(event_log_.empty()); | 113 EXPECT_TRUE(event_log_.empty()); |
138 | 114 |
139 // 2. ACTION_MOVE | 115 // 2. ACTION_MOVE |
140 event_time += base::TimeDelta::FromMilliseconds(10); | 116 event_time += base::TimeDelta::FromMilliseconds(10); |
141 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); | 117 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); |
142 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 118 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
143 action_move.set_button_state(MotionEvent::BUTTON_SECONDARY); | 119 action_move.set_button_state(MotionEvent::BUTTON_SECONDARY); |
144 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); | 120 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
145 EXPECT_TRUE(event_log_.empty()); | 121 ASSERT_EQ(2u, event_log_.size()); |
| 122 EXPECT_STREQ("Show", event_log_[0].c_str()); |
| 123 EXPECT_STREQ("SelectRange", event_log_[1].c_str()); |
146 | 124 |
147 // 3. DOUBLE TAP | 125 // 3. ACTION_UP |
148 // Suppress most gesture events when in text selection mode. | |
149 event_time += base::TimeDelta::FromMilliseconds(10); | |
150 const GestureEventData double_tap = | |
151 CreateGesture(ui::ET_GESTURE_DOUBLE_TAP, event_time, x2, y2); | |
152 EXPECT_TRUE(selector_->OnGestureEvent(double_tap)); | |
153 EXPECT_TRUE(event_log_.empty()); | |
154 | |
155 // 4. ET_GESTURE_SCROLL_BEGIN | |
156 event_time += base::TimeDelta::FromMilliseconds(10); | |
157 const GestureEventData scroll_begin = | |
158 CreateGesture(ui::ET_GESTURE_SCROLL_BEGIN, event_time, x1, y1); | |
159 EXPECT_TRUE(selector_->OnGestureEvent(scroll_begin)); | |
160 EXPECT_EQ(1u, event_log_.size()); // Unselect | |
161 | |
162 // 5. ET_GESTURE_SCROLL_UPDATE | |
163 event_time += base::TimeDelta::FromMilliseconds(10); | |
164 const GestureEventData scroll_update = | |
165 CreateGesture(ui::ET_GESTURE_SCROLL_UPDATE, event_time, x2, y2); | |
166 EXPECT_TRUE(selector_->OnGestureEvent(scroll_update)); | |
167 EXPECT_EQ(3u, event_log_.size()); // Unselect, Show, SelectRange | |
168 EXPECT_STREQ("SelectRange", event_log_.back().c_str()); | |
169 | |
170 // 6. ACTION_UP | |
171 event_time += base::TimeDelta::FromMilliseconds(10); | 126 event_time += base::TimeDelta::FromMilliseconds(10); |
172 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); | 127 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); |
173 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 128 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
174 action_up.set_button_state(0); | 129 action_up.set_button_state(0); |
175 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); | 130 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
176 EXPECT_EQ(3u, event_log_.size()); // NO CHANGE | 131 ASSERT_EQ(2u, event_log_.size()); // NO CHANGE |
177 | |
178 // 7. ET_GESTURE_SCROLL_END | |
179 event_time += base::TimeDelta::FromMilliseconds(10); | |
180 const GestureEventData scroll_end = | |
181 CreateGesture(ui::ET_GESTURE_SCROLL_END, event_time, x2, y2); | |
182 EXPECT_TRUE(selector_->OnGestureEvent(scroll_end)); | |
183 EXPECT_EQ(3u, event_log_.size()); // NO CHANGE | |
184 } | 132 } |
185 | 133 |
186 TEST_F(GestureTextSelectorTest, TapToSelectWord) { | 134 TEST_F(GestureTextSelectorTest, TapTriggersLongPressSelection) { |
187 base::TimeTicks event_time = base::TimeTicks::Now(); | 135 base::TimeTicks event_time = base::TimeTicks::Now(); |
188 const float x1 = 50.0f; | 136 const float x1 = 50.0f; |
189 const float y1 = 30.0f; | 137 const float y1 = 30.0f; |
190 const float x2 = 51.0f; | 138 const float x2 = 51.0f; |
191 const float y2 = 31.0f; | 139 const float y2 = 31.0f; |
192 // 1. ACTION_DOWN with stylus + button | 140 // 1. ACTION_DOWN with stylus + button |
193 event_time += base::TimeDelta::FromMilliseconds(10); | 141 event_time += base::TimeDelta::FromMilliseconds(1); |
194 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); | 142 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); |
195 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 143 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
196 action_down.set_button_state(MotionEvent::BUTTON_SECONDARY); | 144 action_down.set_button_state(MotionEvent::BUTTON_SECONDARY); |
197 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); | 145 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); |
198 EXPECT_TRUE(event_log_.empty()); | 146 EXPECT_TRUE(event_log_.empty()); |
199 | 147 |
200 // 5. TAP_DOWN | |
201 event_time += base::TimeDelta::FromMilliseconds(10); | |
202 const GestureEventData tap_down = | |
203 CreateGesture(ui::ET_GESTURE_TAP_DOWN, event_time, x2, y2); | |
204 EXPECT_TRUE(selector_->OnGestureEvent(tap_down)); | |
205 EXPECT_TRUE(event_log_.empty()); | |
206 | |
207 // 2. ACTION_MOVE | 148 // 2. ACTION_MOVE |
208 event_time += base::TimeDelta::FromMilliseconds(10); | 149 event_time += base::TimeDelta::FromMilliseconds(1); |
209 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); | 150 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); |
210 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 151 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
211 action_move.set_button_state(MotionEvent::BUTTON_SECONDARY); | 152 action_move.set_button_state(MotionEvent::BUTTON_SECONDARY); |
212 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); | 153 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
213 EXPECT_TRUE(event_log_.empty()); | 154 EXPECT_TRUE(event_log_.empty()); |
214 | 155 |
215 // 3. ACTION_UP | 156 // 3. ACTION_UP |
216 event_time += base::TimeDelta::FromMilliseconds(10); | 157 event_time += base::TimeDelta::FromMilliseconds(1); |
217 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); | 158 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); |
218 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 159 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
219 action_up.set_button_state(0); | 160 action_up.set_button_state(0); |
220 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); | 161 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
221 EXPECT_TRUE(event_log_.empty()); | 162 ASSERT_EQ(1u, event_log_.size()); |
222 | |
223 // 4. TAP | |
224 event_time += base::TimeDelta::FromMilliseconds(10); | |
225 const GestureEventData tap = | |
226 CreateGesture(ui::ET_GESTURE_TAP, event_time, x1, y1); | |
227 EXPECT_TRUE(selector_->OnGestureEvent(tap)); | |
228 EXPECT_EQ(1u, event_log_.size()); // LongPress | |
229 EXPECT_STREQ("LongPress", event_log_.back().c_str()); | 163 EXPECT_STREQ("LongPress", event_log_.back().c_str()); |
230 } | 164 } |
231 | 165 |
232 } // namespace content | 166 } // namespace content |
OLD | NEW |