| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/time/time.h" | 6 #include "base/time/time.h" |
| 7 #include "content/browser/renderer_host/input/synthetic_gesture.h" | 7 #include "content/browser/renderer_host/input/synthetic_gesture.h" |
| 8 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" | 8 #include "content/browser/renderer_host/input/synthetic_gesture_target.h" |
| 9 #include "content/browser/renderer_host/input/synthetic_pointer_action.h" | 9 #include "content/browser/renderer_host/input/synthetic_pointer_action.h" |
| 10 #include "content/browser/renderer_host/input/synthetic_touch_driver.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 11 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 13 #include "ui/gfx/geometry/point.h" | 12 #include "ui/gfx/geometry/point.h" |
| 14 #include "ui/gfx/geometry/point_f.h" | 13 #include "ui/gfx/geometry/point_f.h" |
| 15 | 14 |
| 16 using blink::WebInputEvent; | 15 using blink::WebInputEvent; |
| 17 using blink::WebTouchEvent; | 16 using blink::WebTouchEvent; |
| 18 using blink::WebMouseEvent; | 17 using blink::WebMouseEvent; |
| 19 using blink::WebTouchPoint; | 18 using blink::WebTouchPoint; |
| 20 | 19 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 private: | 163 private: |
| 165 gfx::PointF positions_[WebTouchEvent::kTouchesLengthCap]; | 164 gfx::PointF positions_[WebTouchEvent::kTouchesLengthCap]; |
| 166 unsigned touch_length_; | 165 unsigned touch_length_; |
| 167 int indexes_[WebTouchEvent::kTouchesLengthCap]; | 166 int indexes_[WebTouchEvent::kTouchesLengthCap]; |
| 168 WebTouchPoint::State states_[WebTouchEvent::kTouchesLengthCap]; | 167 WebTouchPoint::State states_[WebTouchEvent::kTouchesLengthCap]; |
| 169 }; | 168 }; |
| 170 | 169 |
| 171 class MockSyntheticPointerMouseActionTarget | 170 class MockSyntheticPointerMouseActionTarget |
| 172 : public MockSyntheticPointerActionTarget { | 171 : public MockSyntheticPointerActionTarget { |
| 173 public: | 172 public: |
| 174 MockSyntheticPointerMouseActionTarget() {} | 173 MockSyntheticPointerMouseActionTarget() : click_count_(0), modifiers_(0) {} |
| 175 ~MockSyntheticPointerMouseActionTarget() override {} | 174 ~MockSyntheticPointerMouseActionTarget() override {} |
| 176 | 175 |
| 177 void DispatchInputEventToPlatform(const WebInputEvent& event) override { | 176 void DispatchInputEventToPlatform(const WebInputEvent& event) override { |
| 178 DCHECK(WebInputEvent::isMouseEventType(event.type())); | 177 DCHECK(WebInputEvent::isMouseEventType(event.type())); |
| 179 const WebMouseEvent& mouse_event = static_cast<const WebMouseEvent&>(event); | 178 const WebMouseEvent& mouse_event = static_cast<const WebMouseEvent&>(event); |
| 180 type_ = mouse_event.type(); | 179 type_ = mouse_event.type(); |
| 181 position_ = gfx::PointF(mouse_event.x, mouse_event.y); | 180 position_ = gfx::PointF(mouse_event.x, mouse_event.y); |
| 182 clickCount_ = mouse_event.clickCount; | 181 click_count_ = mouse_event.clickCount; |
| 182 modifiers_ = mouse_event.modifiers(); |
| 183 button_ = mouse_event.button; | 183 button_ = mouse_event.button; |
| 184 } | 184 } |
| 185 | 185 |
| 186 testing::AssertionResult SyntheticMouseActionDispatchedCorrectly( | 186 testing::AssertionResult SyntheticMouseActionDispatchedCorrectly( |
| 187 const SyntheticPointerActionParams& param, | 187 const SyntheticPointerActionParams& param, |
| 188 int click_count) { | 188 int click_count, |
| 189 std::vector<SyntheticPointerActionParams::Button> buttons) { |
| 189 if (type_ != ToWebMouseEventType(param.pointer_action_type())) { | 190 if (type_ != ToWebMouseEventType(param.pointer_action_type())) { |
| 190 return testing::AssertionFailure() | 191 return testing::AssertionFailure() |
| 191 << "Pointer type was " << WebInputEvent::GetName(type_) | 192 << "Pointer type was " << WebInputEvent::GetName(type_) |
| 192 << ", expected " << WebInputEvent::GetName(ToWebMouseEventType( | 193 << ", expected " << WebInputEvent::GetName(ToWebMouseEventType( |
| 193 param.pointer_action_type())) << "."; | 194 param.pointer_action_type())) << "."; |
| 194 } | 195 } |
| 195 | 196 |
| 196 if (clickCount_ != click_count) { | 197 if (click_count_ != click_count) { |
| 197 return testing::AssertionFailure() << "Pointer click count was " | 198 return testing::AssertionFailure() << "Pointer click count was " |
| 198 << clickCount_ << ", expected " | 199 << click_count_ << ", expected " |
| 199 << click_count << "."; | 200 << click_count << "."; |
| 200 } | 201 } |
| 201 | 202 |
| 202 if (clickCount_ == 1 && button_ != WebMouseEvent::Button::Left) { | 203 if (click_count_ == 1) { |
| 203 return testing::AssertionFailure() | 204 DCHECK_GT(buttons.size(), 0U); |
| 204 << "Pointer button was " << (int)button_ << ", expected " | 205 SyntheticPointerActionParams::Button button = buttons[buttons.size() - 1]; |
| 205 << (int)WebMouseEvent::Button::Left << "."; | 206 if (button_ != |
| 207 SyntheticPointerActionParams::GetWebMouseEventButton(button)) { |
| 208 return testing::AssertionFailure() |
| 209 << "Pointer button was " << static_cast<int>(button_) |
| 210 << ", expected " << static_cast<int>(button) << "."; |
| 211 } |
| 206 } | 212 } |
| 207 | 213 |
| 208 if (clickCount_ == 0 && button_ != WebMouseEvent::Button::NoButton) { | 214 if (click_count_ == 0 && button_ != WebMouseEvent::Button::NoButton) { |
| 215 DCHECK_EQ(buttons.size(), 0U); |
| 209 return testing::AssertionFailure() | 216 return testing::AssertionFailure() |
| 210 << "Pointer button was " << (int)button_ << ", expected " | 217 << "Pointer button was " << static_cast<int>(button_) |
| 211 << (int)WebMouseEvent::Button::NoButton << "."; | 218 << ", expected " << (int)WebMouseEvent::Button::NoButton << "."; |
| 219 } |
| 220 |
| 221 int modifiers = 0; |
| 222 for (size_t index = 0; index < buttons.size(); ++index) |
| 223 modifiers |= SyntheticPointerActionParams::GetWebMouseEventModifier( |
| 224 buttons[index]); |
| 225 if (modifiers_ != modifiers) { |
| 226 return testing::AssertionFailure() << "Pointer modifiers was " |
| 227 << modifiers_ << ", expected " |
| 228 << modifiers << "."; |
| 212 } | 229 } |
| 213 | 230 |
| 214 if ((param.pointer_action_type() == | 231 if ((param.pointer_action_type() == |
| 215 SyntheticPointerActionParams::PointerActionType::PRESS || | 232 SyntheticPointerActionParams::PointerActionType::PRESS || |
| 216 param.pointer_action_type() == | 233 param.pointer_action_type() == |
| 217 SyntheticPointerActionParams::PointerActionType::MOVE) && | 234 SyntheticPointerActionParams::PointerActionType::MOVE) && |
| 218 position_ != param.position()) { | 235 position_ != param.position()) { |
| 219 return testing::AssertionFailure() | 236 return testing::AssertionFailure() |
| 220 << "Pointer position was " << position_.ToString() | 237 << "Pointer position was " << position_.ToString() |
| 221 << ", expected " << param.position().ToString() << "."; | 238 << ", expected " << param.position().ToString() << "."; |
| 222 } | 239 } |
| 223 return testing::AssertionSuccess(); | 240 return testing::AssertionSuccess(); |
| 224 } | 241 } |
| 225 | 242 |
| 226 SyntheticGestureParams::GestureSourceType | 243 SyntheticGestureParams::GestureSourceType |
| 227 GetDefaultSyntheticGestureSourceType() const override { | 244 GetDefaultSyntheticGestureSourceType() const override { |
| 228 return SyntheticGestureParams::MOUSE_INPUT; | 245 return SyntheticGestureParams::MOUSE_INPUT; |
| 229 } | 246 } |
| 230 | 247 |
| 231 private: | 248 private: |
| 232 gfx::PointF position_; | 249 gfx::PointF position_; |
| 233 int clickCount_; | 250 int click_count_; |
| 251 int modifiers_; |
| 234 WebMouseEvent::Button button_; | 252 WebMouseEvent::Button button_; |
| 235 }; | 253 }; |
| 236 | 254 |
| 237 class SyntheticPointerActionTest : public testing::Test { | 255 class SyntheticPointerActionTest : public testing::Test { |
| 238 public: | 256 public: |
| 239 SyntheticPointerActionTest() { | 257 SyntheticPointerActionTest() { |
| 240 params_ = SyntheticPointerActionListParams(); | 258 params_ = SyntheticPointerActionListParams(); |
| 241 num_success_ = 0; | 259 num_success_ = 0; |
| 242 num_failure_ = 0; | 260 num_failure_ = 0; |
| 243 } | 261 } |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( | 509 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( |
| 492 SyntheticPointerActionParams::PointerActionType::RELEASE); | 510 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 493 params_.PushPointerActionParams(param4); | 511 params_.PushPointerActionParams(param4); |
| 494 pointer_action_.reset(new SyntheticPointerAction(params_)); | 512 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 495 | 513 |
| 496 ForwardSyntheticPointerAction(); | 514 ForwardSyntheticPointerAction(); |
| 497 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = | 515 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = |
| 498 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); | 516 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); |
| 499 EXPECT_EQ(1, num_success_); | 517 EXPECT_EQ(1, num_success_); |
| 500 EXPECT_EQ(0, num_failure_); | 518 EXPECT_EQ(0, num_failure_); |
| 501 EXPECT_TRUE( | 519 std::vector<SyntheticPointerActionParams::Button> buttons; |
| 502 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param1, 0)); | 520 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 521 param1, 0, buttons)); |
| 503 | 522 |
| 504 ForwardSyntheticPointerAction(); | 523 ForwardSyntheticPointerAction(); |
| 505 EXPECT_EQ(2, num_success_); | 524 EXPECT_EQ(2, num_success_); |
| 506 EXPECT_EQ(0, num_failure_); | 525 EXPECT_EQ(0, num_failure_); |
| 507 EXPECT_TRUE( | 526 buttons.push_back(SyntheticPointerActionParams::Button::LEFT); |
| 508 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param2, 1)); | 527 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 528 param2, 1, buttons)); |
| 509 | 529 |
| 510 ForwardSyntheticPointerAction(); | 530 ForwardSyntheticPointerAction(); |
| 511 EXPECT_EQ(3, num_success_); | 531 EXPECT_EQ(3, num_success_); |
| 512 EXPECT_EQ(0, num_failure_); | 532 EXPECT_EQ(0, num_failure_); |
| 513 EXPECT_TRUE( | 533 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 514 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param3, 1)); | 534 param3, 1, buttons)); |
| 515 | 535 |
| 516 ForwardSyntheticPointerAction(); | 536 ForwardSyntheticPointerAction(); |
| 517 EXPECT_EQ(4, num_success_); | 537 EXPECT_EQ(4, num_success_); |
| 518 EXPECT_EQ(0, num_failure_); | 538 EXPECT_EQ(0, num_failure_); |
| 519 EXPECT_TRUE( | 539 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 520 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param4, 1)); | 540 param4, 1, buttons)); |
| 541 } |
| 542 |
| 543 TEST_F(SyntheticPointerActionTest, PointerMouseActionMultiPress) { |
| 544 CreateSyntheticPointerActionTarget<MockSyntheticPointerMouseActionTarget>(); |
| 545 |
| 546 // Press a mouse's left button. |
| 547 SyntheticPointerActionParams param1 = SyntheticPointerActionParams( |
| 548 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 549 param1.set_position(gfx::PointF(189, 62)); |
| 550 param1.set_button(SyntheticPointerActionParams::Button::LEFT); |
| 551 params_.PushPointerActionParams(param1); |
| 552 |
| 553 // Press a mouse's middle button. |
| 554 SyntheticPointerActionParams param2 = SyntheticPointerActionParams( |
| 555 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 556 param2.set_position(gfx::PointF(189, 62)); |
| 557 param2.set_button(SyntheticPointerActionParams::Button::MIDDLE); |
| 558 params_.PushPointerActionParams(param2); |
| 559 |
| 560 // Release a mouse's middle button. |
| 561 SyntheticPointerActionParams param3 = SyntheticPointerActionParams( |
| 562 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 563 param3.set_button(SyntheticPointerActionParams::Button::MIDDLE); |
| 564 params_.PushPointerActionParams(param3); |
| 565 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 566 |
| 567 // Release a mouse's middle button. |
| 568 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( |
| 569 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 570 param4.set_button(SyntheticPointerActionParams::Button::LEFT); |
| 571 params_.PushPointerActionParams(param4); |
| 572 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 573 |
| 574 ForwardSyntheticPointerAction(); |
| 575 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = |
| 576 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); |
| 577 EXPECT_EQ(1, num_success_); |
| 578 EXPECT_EQ(0, num_failure_); |
| 579 std::vector<SyntheticPointerActionParams::Button> buttons( |
| 580 1, SyntheticPointerActionParams::Button::LEFT); |
| 581 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 582 param1, 1, buttons)); |
| 583 |
| 584 ForwardSyntheticPointerAction(); |
| 585 EXPECT_EQ(2, num_success_); |
| 586 EXPECT_EQ(0, num_failure_); |
| 587 buttons.push_back(SyntheticPointerActionParams::Button::MIDDLE); |
| 588 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 589 param2, 1, buttons)); |
| 590 |
| 591 ForwardSyntheticPointerAction(); |
| 592 EXPECT_EQ(3, num_success_); |
| 593 EXPECT_EQ(0, num_failure_); |
| 594 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 595 param3, 1, buttons)); |
| 596 |
| 597 ForwardSyntheticPointerAction(); |
| 598 EXPECT_EQ(4, num_success_); |
| 599 EXPECT_EQ(0, num_failure_); |
| 600 buttons.pop_back(); |
| 601 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 602 param4, 1, buttons)); |
| 521 } | 603 } |
| 522 | 604 |
| 523 TEST_F(SyntheticPointerActionTest, PointerMouseActionTypeInvalid) { | 605 TEST_F(SyntheticPointerActionTest, PointerMouseActionTypeInvalid) { |
| 524 CreateSyntheticPointerActionTarget<MockSyntheticPointerMouseActionTarget>(); | 606 CreateSyntheticPointerActionTarget<MockSyntheticPointerMouseActionTarget>(); |
| 525 | 607 |
| 526 // Cannot send a mouse up without sending a mouse down first. | 608 // Cannot send a mouse up without sending a mouse down first. |
| 527 SyntheticPointerActionParams param = SyntheticPointerActionParams( | 609 SyntheticPointerActionParams param = SyntheticPointerActionParams( |
| 528 SyntheticPointerActionParams::PointerActionType::RELEASE); | 610 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 529 params_.PushPointerActionParams(param); | 611 params_.PushPointerActionParams(param); |
| 530 pointer_action_.reset(new SyntheticPointerAction(params_)); | 612 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 542 | 624 |
| 543 // Cannot send a mouse down again without releasing the mouse button. | 625 // Cannot send a mouse down again without releasing the mouse button. |
| 544 params_.PushPointerActionParams(param); | 626 params_.PushPointerActionParams(param); |
| 545 pointer_action_.reset(new SyntheticPointerAction(params_)); | 627 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 546 | 628 |
| 547 ForwardSyntheticPointerAction(); | 629 ForwardSyntheticPointerAction(); |
| 548 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = | 630 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = |
| 549 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); | 631 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); |
| 550 EXPECT_EQ(1, num_success_); | 632 EXPECT_EQ(1, num_success_); |
| 551 EXPECT_EQ(1, num_failure_); | 633 EXPECT_EQ(1, num_failure_); |
| 552 EXPECT_TRUE( | 634 std::vector<SyntheticPointerActionParams::Button> buttons( |
| 553 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param, 1)); | 635 1, SyntheticPointerActionParams::Button::LEFT); |
| 636 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 637 param, 1, buttons)); |
| 554 | 638 |
| 555 ForwardSyntheticPointerAction(); | 639 ForwardSyntheticPointerAction(); |
| 556 EXPECT_EQ(1, num_success_); | 640 EXPECT_EQ(1, num_success_); |
| 557 EXPECT_EQ(2, num_failure_); | 641 EXPECT_EQ(2, num_failure_); |
| 558 } | 642 } |
| 559 | 643 |
| 560 } // namespace | 644 } // namespace |
| 561 | 645 |
| 562 } // namespace content | 646 } // namespace content |
| OLD | NEW |