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 MockSyntheticPointerActionTarget : public SyntheticGestureTarget { | |
26 public: | |
27 MockSyntheticPointerActionTarget() {} | |
28 ~MockSyntheticPointerActionTarget() override {} | |
29 | |
30 // SyntheticGestureTarget: | |
31 void DispatchInputEventToPlatform(const WebInputEvent& event) override {} | |
32 | |
33 SyntheticGestureParams::GestureSourceType | |
34 GetDefaultSyntheticGestureSourceType() const override { | |
35 return SyntheticGestureParams::TOUCH_INPUT; | |
tdresser
2016/06/10 18:33:29
Should this be pure virtual?
lanwei
2016/06/16 07:54:24
Done.
| |
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 WebInputEvent::Type type() const { return type_; } | |
59 | |
60 protected: | |
61 WebInputEvent::Type type_; | |
62 }; | |
63 | |
64 class MockSyntheticPointerTouchActionTarget | |
65 : public MockSyntheticPointerActionTarget { | |
66 public: | |
67 MockSyntheticPointerTouchActionTarget() {} | |
68 ~MockSyntheticPointerTouchActionTarget() override {} | |
69 | |
70 void DispatchInputEventToPlatform(const WebInputEvent& event) override { | |
71 ASSERT_TRUE(WebInputEvent::isTouchEventType(event.type)); | |
72 const WebTouchEvent& touch_event = static_cast<const WebTouchEvent&>(event); | |
73 type_ = touch_event.type; | |
74 for (size_t i = 0; i < touch_event.touchesLength; ++i) { | |
75 indexes_[i] = touch_event.touches[i].id; | |
76 positions_[i] = gfx::PointF(touch_event.touches[i].position); | |
77 states_[i] = touch_event.touches[i].state; | |
78 } | |
79 touch_length_ = touch_event.touchesLength; | |
80 } | |
81 | |
82 SyntheticGestureParams::GestureSourceType PointerSourceType() const override { | |
83 return SyntheticGestureParams::TOUCH_INPUT; | |
84 } | |
85 | |
86 gfx::PointF positions(int index) const { return positions_[index]; } | |
87 int indexes(int index) const { return indexes_[index]; } | |
88 WebTouchPoint::State states(int index) { return states_[index]; } | |
89 unsigned touch_length() const { return touch_length_; } | |
90 | |
91 private: | |
92 gfx::PointF positions_[WebTouchEvent::touchesLengthCap]; | |
93 unsigned touch_length_; | |
94 int indexes_[WebTouchEvent::touchesLengthCap]; | |
95 WebTouchPoint::State states_[WebTouchEvent::touchesLengthCap]; | |
96 }; | |
97 | |
98 class SyntheticPointerActionTest : public testing::Test { | |
99 public: | |
100 SyntheticPointerActionTest() {} | |
101 ~SyntheticPointerActionTest() override {} | |
102 | |
103 protected: | |
104 template <typename MockGestureTarget> | |
105 void CreateControllerAndTarget() { | |
106 target_.reset(new MockGestureTarget()); | |
107 synthetic_pointer_ = SyntheticPointer::Create(target_->PointerSourceType()); | |
108 } | |
109 | |
110 void SetUp() override { | |
111 num_success_ = 0; | |
112 num_failure_ = 0; | |
113 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
114 std::fill(index_map_.begin(), index_map_.end(), -1); | |
115 } | |
116 | |
117 void TearDown() override { | |
118 target_.reset(); | |
119 pointer_action_.reset(); | |
120 synthetic_pointer_.reset(); | |
121 action_param_list_.reset(); | |
122 } | |
123 | |
124 void ForwardSyntheticPointerAction() { | |
125 pointer_action_.reset(new SyntheticPointerAction( | |
126 std::move(action_param_list_), synthetic_pointer_.get(), &index_map_)); | |
127 | |
128 SyntheticGesture::Result result = pointer_action_->ForwardInputEvents( | |
129 base::TimeTicks::Now(), target_.get()); | |
130 | |
131 if (result == SyntheticGesture::GESTURE_FINISHED) | |
132 num_success_++; | |
133 else | |
134 num_failure_++; | |
135 } | |
136 | |
137 int num_success_; | |
138 int num_failure_; | |
139 std::unique_ptr<MockSyntheticPointerActionTarget> target_; | |
140 std::unique_ptr<SyntheticGesture> pointer_action_; | |
141 std::unique_ptr<SyntheticPointer> synthetic_pointer_; | |
142 std::unique_ptr<std::vector<SyntheticPointerActionParams>> action_param_list_; | |
143 SyntheticPointerAction::IndexMap index_map_; | |
144 }; | |
145 | |
146 TEST_F(SyntheticPointerActionTest, PointerTouchAction) { | |
147 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>(); | |
148 | |
149 // Send a touch press for one finger. | |
150 SyntheticPointerActionParams params0 = SyntheticPointerActionParams( | |
151 SyntheticPointerActionParams::PointerActionType::PRESS); | |
152 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
153 params0.set_index(0); | |
154 params0.set_position(gfx::PointF(54, 89)); | |
155 action_param_list_->push_back(params0); | |
156 ForwardSyntheticPointerAction(); | |
157 | |
158 MockSyntheticPointerTouchActionTarget* pointer_touch_target = | |
159 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); | |
160 EXPECT_EQ(1, num_success_); | |
161 EXPECT_EQ(0, num_failure_); | |
162 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); | |
163 EXPECT_EQ(pointer_touch_target->indexes(0), index_map_[0]); | |
tdresser
2016/06/10 18:33:28
The value here is deterministic, right?
Can we ju
lanwei
2016/06/16 07:54:24
Done.
| |
164 EXPECT_EQ(pointer_touch_target->positions(0), params0.position()); | |
165 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); | |
166 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); | |
167 | |
168 // Send a touch move for the first finger and a touch press for the second | |
169 // finger. | |
170 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
171 params0.set_pointer_action_type( | |
172 SyntheticPointerActionParams::PointerActionType::MOVE); | |
173 params0.set_position(gfx::PointF(133, 156)); | |
174 SyntheticPointerActionParams params1 = SyntheticPointerActionParams( | |
175 SyntheticPointerActionParams::PointerActionType::PRESS); | |
176 params1.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
177 params1.set_index(1); | |
178 params1.set_position(gfx::PointF(79, 132)); | |
179 action_param_list_->push_back(params0); | |
180 action_param_list_->push_back(params1); | |
181 ForwardSyntheticPointerAction(); | |
182 | |
183 pointer_touch_target = | |
184 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); | |
185 EXPECT_EQ(2, num_success_); | |
186 EXPECT_EQ(0, num_failure_); | |
187 // The type of the SyntheticWebTouchEvent is the action of the last finger. | |
188 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); | |
189 EXPECT_EQ(pointer_touch_target->indexes(0), index_map_[0]); | |
190 EXPECT_EQ(pointer_touch_target->positions(0), params0.position()); | |
191 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateMoved); | |
192 EXPECT_EQ(pointer_touch_target->indexes(1), index_map_[1]); | |
193 EXPECT_EQ(pointer_touch_target->positions(1), params1.position()); | |
194 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed); | |
195 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); | |
196 | |
197 // Send a touch move for the second finger. | |
198 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
199 params1.set_pointer_action_type( | |
200 SyntheticPointerActionParams::PointerActionType::MOVE); | |
201 params1.set_position(gfx::PointF(87, 253)); | |
202 action_param_list_->push_back(params1); | |
203 ForwardSyntheticPointerAction(); | |
204 | |
205 pointer_touch_target = | |
206 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); | |
207 EXPECT_EQ(3, num_success_); | |
208 EXPECT_EQ(0, num_failure_); | |
209 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchMove); | |
210 EXPECT_EQ(pointer_touch_target->indexes(1), index_map_[1]); | |
211 EXPECT_EQ(pointer_touch_target->positions(1), params1.position()); | |
212 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateMoved); | |
213 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); | |
214 | |
215 // Send touch releases for both fingers. | |
216 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
217 params0.set_pointer_action_type( | |
218 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
219 params1.set_pointer_action_type( | |
220 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
221 action_param_list_->push_back(params0); | |
222 action_param_list_->push_back(params1); | |
223 int index0 = index_map_[0]; | |
224 int index1 = index_map_[1]; | |
225 ForwardSyntheticPointerAction(); | |
226 | |
227 EXPECT_EQ(4, num_success_); | |
228 EXPECT_EQ(0, num_failure_); | |
229 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchEnd); | |
230 EXPECT_EQ(pointer_touch_target->indexes(0), index0); | |
231 EXPECT_EQ(-1, index_map_[0]); | |
232 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateReleased); | |
233 EXPECT_EQ(pointer_touch_target->indexes(1), index1); | |
234 EXPECT_EQ(-1, index_map_[1]); | |
235 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased); | |
236 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); | |
237 } | |
238 | |
239 TEST_F(SyntheticPointerActionTest, PointerTouchActionInvalid) { | |
tdresser
2016/06/10 18:33:29
From this test, we can't tell if each individual w
lanwei
2016/06/16 07:54:24
Done.
| |
240 CreateControllerAndTarget<MockSyntheticPointerTouchActionTarget>(); | |
241 | |
242 // Users sent a wrong index for the touch action. | |
243 SyntheticPointerActionParams params0 = SyntheticPointerActionParams( | |
244 SyntheticPointerActionParams::PointerActionType::PRESS); | |
245 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
246 params0.set_index(-1); | |
247 params0.set_position(gfx::PointF(54, 89)); | |
248 action_param_list_->push_back(params0); | |
249 ForwardSyntheticPointerAction(); | |
250 | |
251 EXPECT_EQ(0, num_success_); | |
252 EXPECT_EQ(1, num_failure_); | |
253 | |
254 // Users' gesture source type does not match with the touch action. | |
255 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
256 params0.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT; | |
257 params0.set_index(0); | |
258 action_param_list_->push_back(params0); | |
259 ForwardSyntheticPointerAction(); | |
260 | |
261 EXPECT_EQ(0, num_success_); | |
262 EXPECT_EQ(2, num_failure_); | |
263 | |
264 // Cannot send a touch move or touch release without sending a touch press | |
265 // first. | |
266 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
267 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
268 params0.set_index(0); | |
269 params0.set_pointer_action_type( | |
270 SyntheticPointerActionParams::PointerActionType::MOVE); | |
271 action_param_list_->push_back(params0); | |
272 ForwardSyntheticPointerAction(); | |
273 | |
274 EXPECT_EQ(0, num_success_); | |
275 EXPECT_EQ(3, num_failure_); | |
276 | |
277 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
278 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
279 params0.set_index(1); | |
280 params0.set_pointer_action_type( | |
281 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
282 action_param_list_->push_back(params0); | |
283 ForwardSyntheticPointerAction(); | |
284 | |
285 EXPECT_EQ(0, num_success_); | |
286 EXPECT_EQ(4, num_failure_); | |
287 | |
288 // Send a touch press for one finger. | |
289 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
290 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
291 params0.set_index(0); | |
292 params0.set_pointer_action_type( | |
293 SyntheticPointerActionParams::PointerActionType::PRESS); | |
294 action_param_list_->push_back(params0); | |
295 ForwardSyntheticPointerAction(); | |
296 | |
297 EXPECT_EQ(1, num_success_); | |
298 EXPECT_EQ(4, num_failure_); | |
299 | |
300 // Cannot send a touch press again without releasing the finger. | |
301 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); | |
302 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
303 params0.set_index(0); | |
304 params0.set_pointer_action_type( | |
305 SyntheticPointerActionParams::PointerActionType::PRESS); | |
306 action_param_list_->push_back(params0); | |
307 ForwardSyntheticPointerAction(); | |
308 | |
309 EXPECT_EQ(1, num_success_); | |
310 EXPECT_EQ(5, num_failure_); | |
311 } | |
312 | |
313 } // namespace | |
314 | |
315 } // namespace content | |
OLD | NEW |