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