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 target_.reset(new MockSyntheticPointerTouchActionTarget()); |
| 92 synthetic_pointer_ = |
| 93 SyntheticPointer::Create(SyntheticGestureParams::TOUCH_INPUT); |
| 94 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 95 std::fill(index_map_.begin(), index_map_.end(), -1); |
| 96 num_success_ = 0; |
| 97 num_failure_ = 0; |
| 98 } |
| 99 ~SyntheticPointerActionTest() override {} |
| 100 |
| 101 protected: |
| 102 void ForwardSyntheticPointerAction() { |
| 103 pointer_action_.reset(new SyntheticPointerAction( |
| 104 std::move(action_param_list_), synthetic_pointer_.get(), &index_map_)); |
| 105 |
| 106 SyntheticGesture::Result result = pointer_action_->ForwardInputEvents( |
| 107 base::TimeTicks::Now(), target_.get()); |
| 108 |
| 109 if (result == SyntheticGesture::GESTURE_FINISHED) |
| 110 num_success_++; |
| 111 else |
| 112 num_failure_++; |
| 113 } |
| 114 |
| 115 int num_success_; |
| 116 int num_failure_; |
| 117 std::unique_ptr<MockSyntheticPointerActionTarget> target_; |
| 118 std::unique_ptr<SyntheticGesture> pointer_action_; |
| 119 std::unique_ptr<SyntheticPointer> synthetic_pointer_; |
| 120 std::unique_ptr<std::vector<SyntheticPointerActionParams>> action_param_list_; |
| 121 SyntheticPointerAction::IndexMap index_map_; |
| 122 }; |
| 123 |
| 124 TEST_F(SyntheticPointerActionTest, PointerTouchAction) { |
| 125 // Send a touch press for one finger. |
| 126 SyntheticPointerActionParams params0 = SyntheticPointerActionParams( |
| 127 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 128 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 129 params0.set_index(0); |
| 130 params0.set_position(gfx::PointF(54, 89)); |
| 131 action_param_list_->push_back(params0); |
| 132 ForwardSyntheticPointerAction(); |
| 133 |
| 134 MockSyntheticPointerTouchActionTarget* pointer_touch_target = |
| 135 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); |
| 136 EXPECT_EQ(1, num_success_); |
| 137 EXPECT_EQ(0, num_failure_); |
| 138 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); |
| 139 EXPECT_EQ(pointer_touch_target->indexes(0), 0); |
| 140 EXPECT_EQ(index_map_[0], 0); |
| 141 EXPECT_EQ(pointer_touch_target->positions(0), gfx::PointF(54, 89)); |
| 142 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); |
| 143 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); |
| 144 |
| 145 // Send a touch move for the first finger and a touch press for the second |
| 146 // finger. |
| 147 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 148 params0.set_pointer_action_type( |
| 149 SyntheticPointerActionParams::PointerActionType::MOVE); |
| 150 params0.set_position(gfx::PointF(133, 156)); |
| 151 SyntheticPointerActionParams params1 = SyntheticPointerActionParams( |
| 152 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 153 params1.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 154 params1.set_index(1); |
| 155 params1.set_position(gfx::PointF(79, 132)); |
| 156 action_param_list_->push_back(params0); |
| 157 action_param_list_->push_back(params1); |
| 158 ForwardSyntheticPointerAction(); |
| 159 |
| 160 pointer_touch_target = |
| 161 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); |
| 162 EXPECT_EQ(2, num_success_); |
| 163 EXPECT_EQ(0, num_failure_); |
| 164 // The type of the SyntheticWebTouchEvent is the action of the last finger. |
| 165 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); |
| 166 EXPECT_EQ(pointer_touch_target->indexes(0), 0); |
| 167 EXPECT_EQ(index_map_[0], 0); |
| 168 EXPECT_EQ(pointer_touch_target->positions(0), gfx::PointF(133, 156)); |
| 169 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateMoved); |
| 170 EXPECT_EQ(pointer_touch_target->indexes(1), 1); |
| 171 EXPECT_EQ(index_map_[1], 1); |
| 172 EXPECT_EQ(pointer_touch_target->positions(1), gfx::PointF(79, 132)); |
| 173 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StatePressed); |
| 174 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); |
| 175 |
| 176 // Send a touch move for the second finger. |
| 177 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 178 params1.set_pointer_action_type( |
| 179 SyntheticPointerActionParams::PointerActionType::MOVE); |
| 180 params1.set_position(gfx::PointF(87, 253)); |
| 181 action_param_list_->push_back(params1); |
| 182 ForwardSyntheticPointerAction(); |
| 183 |
| 184 pointer_touch_target = |
| 185 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); |
| 186 EXPECT_EQ(3, num_success_); |
| 187 EXPECT_EQ(0, num_failure_); |
| 188 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchMove); |
| 189 EXPECT_EQ(pointer_touch_target->indexes(1), 1); |
| 190 EXPECT_EQ(index_map_[1], 1); |
| 191 EXPECT_EQ(pointer_touch_target->positions(1), gfx::PointF(87, 253)); |
| 192 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateMoved); |
| 193 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); |
| 194 |
| 195 // Send touch releases for both fingers. |
| 196 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 197 params0.set_pointer_action_type( |
| 198 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 199 params1.set_pointer_action_type( |
| 200 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 201 action_param_list_->push_back(params0); |
| 202 action_param_list_->push_back(params1); |
| 203 ForwardSyntheticPointerAction(); |
| 204 |
| 205 EXPECT_EQ(4, num_success_); |
| 206 EXPECT_EQ(0, num_failure_); |
| 207 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchEnd); |
| 208 EXPECT_EQ(pointer_touch_target->indexes(0), 0); |
| 209 EXPECT_EQ(index_map_[0], -1); |
| 210 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StateReleased); |
| 211 EXPECT_EQ(pointer_touch_target->indexes(1), 1); |
| 212 EXPECT_EQ(index_map_[1], -1); |
| 213 EXPECT_EQ(pointer_touch_target->states(1), WebTouchPoint::StateReleased); |
| 214 ASSERT_EQ(pointer_touch_target->touch_length(), 2U); |
| 215 } |
| 216 |
| 217 TEST_F(SyntheticPointerActionTest, PointerTouchActionIndexInvalid) { |
| 218 // Users sent a wrong index for the touch action. |
| 219 SyntheticPointerActionParams params0 = SyntheticPointerActionParams( |
| 220 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 221 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 222 params0.set_index(-1); |
| 223 params0.set_position(gfx::PointF(54, 89)); |
| 224 action_param_list_->push_back(params0); |
| 225 ForwardSyntheticPointerAction(); |
| 226 |
| 227 EXPECT_EQ(0, num_success_); |
| 228 EXPECT_EQ(1, num_failure_); |
| 229 |
| 230 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 231 params0.set_index(0); |
| 232 action_param_list_->push_back(params0); |
| 233 ForwardSyntheticPointerAction(); |
| 234 |
| 235 MockSyntheticPointerTouchActionTarget* pointer_touch_target = |
| 236 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); |
| 237 EXPECT_EQ(1, num_success_); |
| 238 EXPECT_EQ(1, num_failure_); |
| 239 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); |
| 240 EXPECT_EQ(pointer_touch_target->indexes(0), 0); |
| 241 EXPECT_EQ(index_map_[0], 0); |
| 242 EXPECT_EQ(pointer_touch_target->positions(0), gfx::PointF(54, 89)); |
| 243 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); |
| 244 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); |
| 245 } |
| 246 |
| 247 TEST_F(SyntheticPointerActionTest, PointerTouchActionSourceTypeInvalid) { |
| 248 // Users' gesture source type does not match with the touch action. |
| 249 SyntheticPointerActionParams params0 = SyntheticPointerActionParams( |
| 250 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 251 params0.gesture_source_type = SyntheticGestureParams::MOUSE_INPUT; |
| 252 params0.set_index(0); |
| 253 params0.set_position(gfx::PointF(54, 89)); |
| 254 action_param_list_->push_back(params0); |
| 255 ForwardSyntheticPointerAction(); |
| 256 |
| 257 EXPECT_EQ(0, num_success_); |
| 258 EXPECT_EQ(1, num_failure_); |
| 259 |
| 260 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 261 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 262 action_param_list_->push_back(params0); |
| 263 ForwardSyntheticPointerAction(); |
| 264 |
| 265 MockSyntheticPointerTouchActionTarget* pointer_touch_target = |
| 266 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); |
| 267 EXPECT_EQ(1, num_success_); |
| 268 EXPECT_EQ(1, num_failure_); |
| 269 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); |
| 270 EXPECT_EQ(pointer_touch_target->indexes(0), 0); |
| 271 EXPECT_EQ(index_map_[0], 0); |
| 272 EXPECT_EQ(pointer_touch_target->positions(0), gfx::PointF(54, 89)); |
| 273 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); |
| 274 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); |
| 275 } |
| 276 |
| 277 TEST_F(SyntheticPointerActionTest, PointerTouchActionTypeInvalid) { |
| 278 // Cannot send a touch move or touch release without sending a touch press |
| 279 // first. |
| 280 SyntheticPointerActionParams params0 = SyntheticPointerActionParams( |
| 281 SyntheticPointerActionParams::PointerActionType::MOVE); |
| 282 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 283 params0.set_index(0); |
| 284 params0.set_position(gfx::PointF(54, 89)); |
| 285 action_param_list_->push_back(params0); |
| 286 ForwardSyntheticPointerAction(); |
| 287 |
| 288 EXPECT_EQ(0, num_success_); |
| 289 EXPECT_EQ(1, num_failure_); |
| 290 |
| 291 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 292 params0.set_pointer_action_type( |
| 293 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 294 action_param_list_->push_back(params0); |
| 295 ForwardSyntheticPointerAction(); |
| 296 |
| 297 EXPECT_EQ(0, num_success_); |
| 298 EXPECT_EQ(2, num_failure_); |
| 299 |
| 300 // Send a touch press for one finger. |
| 301 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 302 params0.set_pointer_action_type( |
| 303 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 304 action_param_list_->push_back(params0); |
| 305 ForwardSyntheticPointerAction(); |
| 306 |
| 307 MockSyntheticPointerTouchActionTarget* pointer_touch_target = |
| 308 static_cast<MockSyntheticPointerTouchActionTarget*>(target_.get()); |
| 309 EXPECT_EQ(1, num_success_); |
| 310 EXPECT_EQ(2, num_failure_); |
| 311 EXPECT_EQ(pointer_touch_target->type(), WebInputEvent::TouchStart); |
| 312 EXPECT_EQ(pointer_touch_target->indexes(0), 0); |
| 313 EXPECT_EQ(index_map_[0], 0); |
| 314 EXPECT_EQ(pointer_touch_target->positions(0), gfx::PointF(54, 89)); |
| 315 EXPECT_EQ(pointer_touch_target->states(0), WebTouchPoint::StatePressed); |
| 316 ASSERT_EQ(pointer_touch_target->touch_length(), 1U); |
| 317 |
| 318 // Cannot send a touch press again without releasing the finger. |
| 319 action_param_list_.reset(new std::vector<SyntheticPointerActionParams>()); |
| 320 params0.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 321 params0.set_index(0); |
| 322 params0.set_pointer_action_type( |
| 323 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 324 action_param_list_->push_back(params0); |
| 325 ForwardSyntheticPointerAction(); |
| 326 |
| 327 EXPECT_EQ(1, num_success_); |
| 328 EXPECT_EQ(3, num_failure_); |
| 329 } |
| 330 |
| 331 } // namespace |
| 332 |
| 333 } // namespace content |
OLD | NEW |