OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/time/time.h" | |
7 #include "content/browser/renderer_host/input/synthetic_gesture.h" | |
8 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" | |
9 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" | |
10 #include "content/browser/renderer_host/input/synthetic_pointer_action.h" | |
11 #include "content/browser/renderer_host/input/synthetic_touch_pointer.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
14 #include "ui/gfx/geometry/point.h" | |
15 #include "ui/gfx/geometry/point_f.h" | |
16 | |
17 using blink::WebInputEvent; | |
18 using blink::WebTouchEvent; | |
19 using blink::WebTouchPoint; | |
20 | |
21 namespace content { | |
22 | |
23 namespace { | |
24 | |
25 class MockSyntheticGestureTarget : public SyntheticGestureTarget { | |
26 public: | |
27 MockSyntheticGestureTarget() {} | |
28 ~MockSyntheticGestureTarget() override {} | |
29 | |
30 // SyntheticGestureTarget: | |
31 void DispatchInputEventToPlatform(const WebInputEvent& event) override {} | |
32 | |
33 SyntheticGestureParams::GestureSourceType | |
34 GetDefaultSyntheticGestureSourceType() const override { | |
35 return SyntheticGestureParams::TOUCH_INPUT; | |
36 } | |
37 | |
38 void SetNeedsFlush() override { NOTIMPLEMENTED(); } | |
39 | |
40 base::TimeDelta PointerAssumedStoppedTime() const override { | |
41 NOTIMPLEMENTED(); | |
42 return base::TimeDelta(); | |
43 } | |
44 | |
45 float GetTouchSlopInDips() const override { | |
46 NOTIMPLEMENTED(); | |
47 return 0.0f; | |
48 } | |
49 | |
50 float GetMinScalingSpanInDips() const override { | |
51 NOTIMPLEMENTED(); | |
52 return 0.0f; | |
53 } | |
54 | |
55 virtual SyntheticGestureParams::GestureSourceType PointerSourceType() | |
56 const = 0; | |
57 }; | |
58 | |
59 class MockSyntheticPointerActionTarget : public MockSyntheticGestureTarget { | |
60 public: | |
61 MockSyntheticPointerActionTarget() {} | |
62 ~MockSyntheticPointerActionTarget() override {} | |
63 | |
64 gfx::PointF positions(int index) const { return positions_[index]; } | |
65 int indexes(int index) const { return indexes_[index]; } | |
66 WebTouchPoint::State states(int index) { return states_[index]; } | |
67 unsigned touch_length() const { return touch_length_; } | |
68 WebInputEvent::Type type() const { return type_; } | |
69 | |
70 protected: | |
71 gfx::PointF positions_[WebTouchEvent::touchesLengthCap]; | |
72 unsigned touch_length_; | |
tdresser
2016/06/09 16:50:22
This has touch specific information in it, but the
lanwei
2016/06/09 18:10:14
Sorry, I moved this into the MockSyntheticPointerT
| |
73 int indexes_[WebTouchEvent::touchesLengthCap]; | |
74 WebTouchPoint::State states_[WebTouchEvent::touchesLengthCap]; | |
75 WebInputEvent::Type type_; | |
76 }; | |
77 | |
78 class MockSyntheticPointerTouchActionTarget | |
79 : public MockSyntheticPointerActionTarget { | |
80 public: | |
81 MockSyntheticPointerTouchActionTarget() {} | |
82 ~MockSyntheticPointerTouchActionTarget() override {} | |
83 | |
84 void DispatchInputEventToPlatform(const WebInputEvent& event) override { | |
85 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); | |
86 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); | |
87 type_ = touch_event.type; | |
88 for (size_t i = 0; i < touch_event.touchesLength; ++i) { | |
89 indexes_[i] = touch_event.touches[i].id; | |
90 positions_[i] = gfx::PointF(touch_event.touches[i].position); | |
91 states_[i] = touch_event.touches[i].state; | |
92 } | |
93 touch_length_ = touch_event.touchesLength; | |
94 } | |
95 | |
96 SyntheticGestureParams::GestureSourceType PointerSourceType() const override { | |
97 return SyntheticGestureParams::TOUCH_INPUT; | |
98 } | |
99 }; | |
100 | |
101 class SyntheticPointerActionTest : public testing::Test { | |
102 public: | |
103 SyntheticPointerActionTest() {} | |
104 ~SyntheticPointerActionTest() override {} | |
105 | |
106 protected: | |
107 template <typename MockGestureTarget> | |
108 void CreateControllerAndTarget() { | |
109 target_.reset(new MockGestureTarget()); | |
110 synthetic_pointer_ = SyntheticPointer::Create(target_->PointerSourceType()); | |
111 } | |
112 | |
113 void SetUp() override { | |
114 num_success_ = 0; | |
115 num_failure_ = 0; | |
116 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
117 std::fill(index_map_.begin(), index_map_.end(), -1); | |
118 } | |
119 | |
120 void TearDown() override { | |
121 target_.reset(); | |
122 pointer_action_.reset(); | |
123 synthetic_pointer_.reset(); | |
124 action_param_list_.reset(); | |
125 } | |
126 | |
127 void ForwardSyntheticPointerAction() { | |
128 pointer_action_.reset(new SyntheticPointerAction( | |
129 std::move(action_param_list_), synthetic_pointer_.get(), &index_map_)); | |
130 | |
131 SyntheticGesture::Result result = pointer_action_->ForwardInputEvents( | |
132 base::TimeTicks::Now(), target_.get()); | |
133 | |
134 if (result == SyntheticGesture::GESTURE_FINISHED) | |
135 num_success_++; | |
136 else | |
137 num_failure_++; | |
138 } | |
139 | |
140 int num_success_; | |
141 int num_failure_; | |
142 std::unique_ptr<MockSyntheticGestureTarget> target_; | |
143 std::unique_ptr<SyntheticGesture> pointer_action_; | |
144 std::unique_ptr<SyntheticPointer> synthetic_pointer_; | |
145 std::unique_ptr<std::vector<SyntheticPointerActionParams>> action_param_list_; | |
146 SyntheticPointerAction::IndexMap index_map_; | |
147 }; | |
148 | |
149 TEST_F(SyntheticPointerActionTest, PointerTouchAction) { | |
150 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>(); | |
151 | |
152 // Send a touch press for one finger. | |
153 SyntheticPointerActionParams params0 = SyntheticPointerActionParams( | |
154 SyntheticPointerActionParams::PointerActionType::PRESS); | |
155 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
156 params0.set_index(0); | |
157 params0.set_position(gfx::PointF(54, 89)); | |
158 action_param_list_->push_back(params0); | |
159 ForwardSyntheticPointerAction(); | |
160 | |
161 MockSyntheticPointerTouchActionTarget* pointer_touch_target = | |
162 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); | |
163 EXPECT_EQ(1, num_success_); | |
164 EXPECT_EQ(0, num_failure_); | |
165 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); | |
166 EXPECT_EQ(pointer_touch_target->indexes(0), index_map_[0]); | |
167 EXPECT_EQ(pointer_touch_target->positions(0), params0.position()); | |
168 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); | |
169 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); | |
170 | |
171 // Send a touch move for the first finger and a touch press for the second | |
172 // finger. | |
173 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
174 params0.set_pointer_action_type( | |
175 SyntheticPointerActionParams::PointerActionType::MOVE); | |
176 params0.set_position(gfx::PointF(133, 156)); | |
177 SyntheticPointerActionParams params1 = SyntheticPointerActionParams( | |
178 SyntheticPointerActionParams::PointerActionType::PRESS); | |
179 params1.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
180 params1.set_index(1); | |
181 params1.set_position(gfx::PointF(79, 132)); | |
182 action_param_list_->push_back(params0); | |
183 action_param_list_->push_back(params1); | |
184 ForwardSyntheticPointerAction(); | |
185 | |
186 pointer_touch_target = | |
187 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); | |
188 EXPECT_EQ(2, num_success_); | |
189 EXPECT_EQ(0, num_failure_); | |
190 // The type of the SyntheticWebTouchEvent is the action of the last finger. | |
191 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); | |
192 EXPECT_EQ(pointer_touch_target->indexes(0), index_map_[0]); | |
193 EXPECT_EQ(pointer_touch_target->positions(0), params0.position()); | |
194 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateMoved); | |
195 EXPECT_EQ(pointer_touch_target->indexes(1), index_map_[1]); | |
196 EXPECT_EQ(pointer_touch_target->positions(1), params1.position()); | |
197 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed); | |
198 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); | |
199 | |
200 // Send a touch move for the second finger. | |
201 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
202 params1.set_pointer_action_type( | |
203 SyntheticPointerActionParams::PointerActionType::MOVE); | |
204 params1.set_position(gfx::PointF(87, 253)); | |
205 action_param_list_->push_back(params1); | |
206 ForwardSyntheticPointerAction(); | |
207 | |
208 pointer_touch_target = | |
209 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); | |
210 EXPECT_EQ(3, num_success_); | |
211 EXPECT_EQ(0, num_failure_); | |
212 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchMove); | |
213 EXPECT_EQ(pointer_touch_target->indexes(1), index_map_[1]); | |
214 EXPECT_EQ(pointer_touch_target->positions(1), params1.position()); | |
215 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateMoved); | |
216 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); | |
217 | |
218 // Send touch releases for both fingers. | |
219 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
220 params0.set_pointer_action_type( | |
221 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
222 params1.set_pointer_action_type( | |
223 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
224 action_param_list_->push_back(params0); | |
225 action_param_list_->push_back(params1); | |
226 int index0 = index_map_[0]; | |
227 int index1 = index_map_[1]; | |
228 ForwardSyntheticPointerAction(); | |
229 | |
230 EXPECT_EQ(4, num_success_); | |
231 EXPECT_EQ(0, num_failure_); | |
232 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchEnd); | |
233 EXPECT_EQ(pointer_touch_target->indexes(0), index0); | |
234 EXPECT_EQ(-1, index_map_[0]); | |
235 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateReleased); | |
236 EXPECT_EQ(pointer_touch_target->indexes(1), index1); | |
237 EXPECT_EQ(-1, index_map_[1]); | |
238 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased); | |
239 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); | |
240 } | |
241 | |
242 TEST_F(SyntheticPointerActionTest, PointerTouchActionInvalid) { | |
243 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>(); | |
244 | |
245 // Users sent a wrong index for the touch action. | |
246 SyntheticPointerActionParams params0 = SyntheticPointerActionParams( | |
247 SyntheticPointerActionParams::PointerActionType::PRESS); | |
248 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
249 params0.set_index(-1); | |
250 params0.set_position(gfx::PointF(54, 89)); | |
251 action_param_list_->push_back(params0); | |
252 ForwardSyntheticPointerAction(); | |
253 | |
254 EXPECT_EQ(0, num_success_); | |
255 EXPECT_EQ(1, num_failure_); | |
256 | |
257 // Users' gesture source type does not match with the touch action. | |
258 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
259 params0.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT; | |
260 params0.set_index(0); | |
261 action_param_list_->push_back(params0); | |
262 ForwardSyntheticPointerAction(); | |
263 | |
264 EXPECT_EQ(0, num_success_); | |
265 EXPECT_EQ(2, num_failure_); | |
266 | |
267 // Cannot send a touch move or touch release without sending a touch press | |
268 // first. | |
269 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
270 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
271 params0.set_index(0); | |
272 params0.set_pointer_action_type( | |
273 SyntheticPointerActionParams::PointerActionType::MOVE); | |
274 action_param_list_->push_back(params0); | |
275 ForwardSyntheticPointerAction(); | |
276 | |
277 EXPECT_EQ(0, num_success_); | |
278 EXPECT_EQ(3, num_failure_); | |
279 | |
280 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
281 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
282 params0.set_index(1); | |
283 params0.set_pointer_action_type( | |
284 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
285 action_param_list_->push_back(params0); | |
286 ForwardSyntheticPointerAction(); | |
287 | |
288 EXPECT_EQ(0, num_success_); | |
289 EXPECT_EQ(4, num_failure_); | |
290 | |
291 // Send a touch press for one finger. | |
292 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
293 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
294 params0.set_index(0); | |
295 params0.set_pointer_action_type( | |
296 SyntheticPointerActionParams::PointerActionType::PRESS); | |
297 action_param_list_->push_back(params0); | |
298 ForwardSyntheticPointerAction(); | |
299 | |
300 EXPECT_EQ(1, num_success_); | |
301 EXPECT_EQ(4, num_failure_); | |
302 | |
303 // Cannot send a touch press again without releasing the finger. | |
304 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
305 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
306 params0.set_index(0); | |
307 params0.set_pointer_action_type( | |
308 SyntheticPointerActionParams::PointerActionType::PRESS); | |
309 action_param_list_->push_back(params0); | |
310 ForwardSyntheticPointerAction(); | |
311 | |
312 EXPECT_EQ(1, num_success_); | |
313 EXPECT_EQ(5, num_failure_); | |
314 } | |
315 | |
316 } // namespace | |
317 | |
318 } // namespace content | |
OLD | NEW |