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