| 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_mouse_driver.h" |
| 9 #include "content/browser/renderer_host/input/synthetic_pointer_action.h" | 10 #include "content/browser/renderer_host/input/synthetic_pointer_action.h" |
| 10 #include "content/browser/renderer_host/input/synthetic_touch_driver.h" | 11 #include "content/browser/renderer_host/input/synthetic_touch_driver.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 13 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 13 #include "ui/gfx/geometry/point.h" | 14 #include "ui/gfx/geometry/point.h" |
| 14 #include "ui/gfx/geometry/point_f.h" | 15 #include "ui/gfx/geometry/point_f.h" |
| 15 | 16 |
| 16 using blink::WebInputEvent; | 17 using blink::WebInputEvent; |
| 17 using blink::WebTouchEvent; | 18 using blink::WebTouchEvent; |
| 18 using blink::WebMouseEvent; | 19 using blink::WebMouseEvent; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 private: | 165 private: |
| 165 gfx::PointF positions_[WebTouchEvent::kTouchesLengthCap]; | 166 gfx::PointF positions_[WebTouchEvent::kTouchesLengthCap]; |
| 166 unsigned touch_length_; | 167 unsigned touch_length_; |
| 167 int indexes_[WebTouchEvent::kTouchesLengthCap]; | 168 int indexes_[WebTouchEvent::kTouchesLengthCap]; |
| 168 WebTouchPoint::State states_[WebTouchEvent::kTouchesLengthCap]; | 169 WebTouchPoint::State states_[WebTouchEvent::kTouchesLengthCap]; |
| 169 }; | 170 }; |
| 170 | 171 |
| 171 class MockSyntheticPointerMouseActionTarget | 172 class MockSyntheticPointerMouseActionTarget |
| 172 : public MockSyntheticPointerActionTarget { | 173 : public MockSyntheticPointerActionTarget { |
| 173 public: | 174 public: |
| 174 MockSyntheticPointerMouseActionTarget() {} | 175 MockSyntheticPointerMouseActionTarget() : click_count_(0), modifiers_(0) {} |
| 175 ~MockSyntheticPointerMouseActionTarget() override {} | 176 ~MockSyntheticPointerMouseActionTarget() override {} |
| 176 | 177 |
| 177 void DispatchInputEventToPlatform(const WebInputEvent& event) override { | 178 void DispatchInputEventToPlatform(const WebInputEvent& event) override { |
| 178 DCHECK(WebInputEvent::isMouseEventType(event.type())); | 179 DCHECK(WebInputEvent::isMouseEventType(event.type())); |
| 179 const WebMouseEvent& mouse_event = static_cast<const WebMouseEvent&>(event); | 180 const WebMouseEvent& mouse_event = static_cast<const WebMouseEvent&>(event); |
| 180 type_ = mouse_event.type(); | 181 type_ = mouse_event.type(); |
| 181 position_ = gfx::PointF(mouse_event.x, mouse_event.y); | 182 position_ = gfx::PointF(mouse_event.x, mouse_event.y); |
| 182 clickCount_ = mouse_event.clickCount; | 183 click_count_ = mouse_event.clickCount; |
| 184 modifiers_ = mouse_event.modifiers(); |
| 183 button_ = mouse_event.button; | 185 button_ = mouse_event.button; |
| 184 } | 186 } |
| 185 | 187 |
| 186 testing::AssertionResult SyntheticMouseActionDispatchedCorrectly( | 188 testing::AssertionResult SyntheticMouseActionDispatchedCorrectly( |
| 187 const SyntheticPointerActionParams& param, | 189 const SyntheticPointerActionParams& param, |
| 188 int click_count) { | 190 int click_count, |
| 191 std::vector<int> buttons) { |
| 189 if (type_ != ToWebMouseEventType(param.pointer_action_type())) { | 192 if (type_ != ToWebMouseEventType(param.pointer_action_type())) { |
| 190 return testing::AssertionFailure() | 193 return testing::AssertionFailure() |
| 191 << "Pointer type was " << WebInputEvent::GetName(type_) | 194 << "Pointer type was " << WebInputEvent::GetName(type_) |
| 192 << ", expected " << WebInputEvent::GetName(ToWebMouseEventType( | 195 << ", expected " << WebInputEvent::GetName(ToWebMouseEventType( |
| 193 param.pointer_action_type())) << "."; | 196 param.pointer_action_type())) << "."; |
| 194 } | 197 } |
| 195 | 198 |
| 196 if (clickCount_ != click_count) { | 199 if (click_count_ != click_count) { |
| 197 return testing::AssertionFailure() << "Pointer click count was " | 200 return testing::AssertionFailure() << "Pointer click count was " |
| 198 << clickCount_ << ", expected " | 201 << click_count_ << ", expected " |
| 199 << click_count << "."; | 202 << click_count << "."; |
| 200 } | 203 } |
| 201 | 204 |
| 202 if (clickCount_ == 1 && button_ != WebMouseEvent::Button::Left) { | 205 if (click_count_ == 1) { |
| 203 return testing::AssertionFailure() | 206 DCHECK_GT(buttons.size(), 0U); |
| 204 << "Pointer button was " << (int)button_ << ", expected " | 207 int button = buttons[buttons.size() - 1]; |
| 205 << (int)WebMouseEvent::Button::Left << "."; | 208 if (button_ != SyntheticMouseDriver::GetWebMouseEventButton(button)) { |
| 209 return testing::AssertionFailure() << "Pointer button was " |
| 210 << static_cast<int>(button_) |
| 211 << ", expected " << button << "."; |
| 212 } |
| 206 } | 213 } |
| 207 | 214 |
| 208 if (clickCount_ == 0 && button_ != WebMouseEvent::Button::NoButton) { | 215 if (click_count_ == 0 && button_ != WebMouseEvent::Button::NoButton) { |
| 216 DCHECK_EQ(buttons.size(), 0U); |
| 209 return testing::AssertionFailure() | 217 return testing::AssertionFailure() |
| 210 << "Pointer button was " << (int)button_ << ", expected " | 218 << "Pointer button was " << static_cast<int>(button_) |
| 211 << (int)WebMouseEvent::Button::NoButton << "."; | 219 << ", expected " << (int)WebMouseEvent::Button::NoButton << "."; |
| 220 } |
| 221 |
| 222 int modifiers = 0; |
| 223 for (size_t index = 0; index < buttons.size(); ++index) |
| 224 modifiers |= |
| 225 SyntheticMouseDriver::GetWebMouseEventModifier(buttons[index]); |
| 226 if (modifiers_ != modifiers) { |
| 227 return testing::AssertionFailure() << "Pointer modifiers was " |
| 228 << modifiers_ << ", expected " |
| 229 << modifiers << "."; |
| 212 } | 230 } |
| 213 | 231 |
| 214 if ((param.pointer_action_type() == | 232 if ((param.pointer_action_type() == |
| 215 SyntheticPointerActionParams::PointerActionType::PRESS || | 233 SyntheticPointerActionParams::PointerActionType::PRESS || |
| 216 param.pointer_action_type() == | 234 param.pointer_action_type() == |
| 217 SyntheticPointerActionParams::PointerActionType::MOVE) && | 235 SyntheticPointerActionParams::PointerActionType::MOVE) && |
| 218 position_ != param.position()) { | 236 position_ != param.position()) { |
| 219 return testing::AssertionFailure() | 237 return testing::AssertionFailure() |
| 220 << "Pointer position was " << position_.ToString() | 238 << "Pointer position was " << position_.ToString() |
| 221 << ", expected " << param.position().ToString() << "."; | 239 << ", expected " << param.position().ToString() << "."; |
| 222 } | 240 } |
| 223 return testing::AssertionSuccess(); | 241 return testing::AssertionSuccess(); |
| 224 } | 242 } |
| 225 | 243 |
| 226 SyntheticGestureParams::GestureSourceType | 244 SyntheticGestureParams::GestureSourceType |
| 227 GetDefaultSyntheticGestureSourceType() const override { | 245 GetDefaultSyntheticGestureSourceType() const override { |
| 228 return SyntheticGestureParams::MOUSE_INPUT; | 246 return SyntheticGestureParams::MOUSE_INPUT; |
| 229 } | 247 } |
| 230 | 248 |
| 231 private: | 249 private: |
| 232 gfx::PointF position_; | 250 gfx::PointF position_; |
| 233 int clickCount_; | 251 int click_count_; |
| 252 int modifiers_; |
| 234 WebMouseEvent::Button button_; | 253 WebMouseEvent::Button button_; |
| 235 }; | 254 }; |
| 236 | 255 |
| 237 class SyntheticPointerActionTest : public testing::Test { | 256 class SyntheticPointerActionTest : public testing::Test { |
| 238 public: | 257 public: |
| 239 SyntheticPointerActionTest() { | 258 SyntheticPointerActionTest() { |
| 240 params_ = SyntheticPointerActionListParams(); | 259 params_ = SyntheticPointerActionListParams(); |
| 241 num_success_ = 0; | 260 num_success_ = 0; |
| 242 num_failure_ = 0; | 261 num_failure_ = 0; |
| 243 } | 262 } |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( | 510 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( |
| 492 SyntheticPointerActionParams::PointerActionType::RELEASE); | 511 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 493 params_.PushPointerActionParams(param4); | 512 params_.PushPointerActionParams(param4); |
| 494 pointer_action_.reset(new SyntheticPointerAction(params_)); | 513 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 495 | 514 |
| 496 ForwardSyntheticPointerAction(); | 515 ForwardSyntheticPointerAction(); |
| 497 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = | 516 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = |
| 498 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); | 517 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); |
| 499 EXPECT_EQ(1, num_success_); | 518 EXPECT_EQ(1, num_success_); |
| 500 EXPECT_EQ(0, num_failure_); | 519 EXPECT_EQ(0, num_failure_); |
| 501 EXPECT_TRUE( | 520 std::vector<int> buttons; |
| 502 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param1, 0)); | 521 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 522 param1, 0, buttons)); |
| 503 | 523 |
| 504 ForwardSyntheticPointerAction(); | 524 ForwardSyntheticPointerAction(); |
| 505 EXPECT_EQ(2, num_success_); | 525 EXPECT_EQ(2, num_success_); |
| 506 EXPECT_EQ(0, num_failure_); | 526 EXPECT_EQ(0, num_failure_); |
| 507 EXPECT_TRUE( | 527 buttons.push_back(0); |
| 508 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param2, 1)); | 528 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 529 param2, 1, buttons)); |
| 509 | 530 |
| 510 ForwardSyntheticPointerAction(); | 531 ForwardSyntheticPointerAction(); |
| 511 EXPECT_EQ(3, num_success_); | 532 EXPECT_EQ(3, num_success_); |
| 512 EXPECT_EQ(0, num_failure_); | 533 EXPECT_EQ(0, num_failure_); |
| 513 EXPECT_TRUE( | 534 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 514 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param3, 1)); | 535 param3, 1, buttons)); |
| 515 | 536 |
| 516 ForwardSyntheticPointerAction(); | 537 ForwardSyntheticPointerAction(); |
| 517 EXPECT_EQ(4, num_success_); | 538 EXPECT_EQ(4, num_success_); |
| 518 EXPECT_EQ(0, num_failure_); | 539 EXPECT_EQ(0, num_failure_); |
| 519 EXPECT_TRUE( | 540 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 520 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param4, 1)); | 541 param4, 1, buttons)); |
| 542 } |
| 543 |
| 544 TEST_F(SyntheticPointerActionTest, PointerMouseActionMultiPress) { |
| 545 CreateSyntheticPointerActionTarget<MockSyntheticPointerMouseActionTarget>(); |
| 546 |
| 547 // Press a mouse's left button. |
| 548 SyntheticPointerActionParams param1 = SyntheticPointerActionParams( |
| 549 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 550 param1.set_position(gfx::PointF(189, 62)); |
| 551 param1.set_button(0); |
| 552 params_.PushPointerActionParams(param1); |
| 553 |
| 554 // Press a mouse's middle button. |
| 555 SyntheticPointerActionParams param2 = SyntheticPointerActionParams( |
| 556 SyntheticPointerActionParams::PointerActionType::PRESS); |
| 557 param2.set_position(gfx::PointF(189, 62)); |
| 558 param2.set_button(1); |
| 559 params_.PushPointerActionParams(param2); |
| 560 |
| 561 // Release a mouse's middle button. |
| 562 SyntheticPointerActionParams param3 = SyntheticPointerActionParams( |
| 563 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 564 param3.set_button(1); |
| 565 params_.PushPointerActionParams(param3); |
| 566 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 567 |
| 568 // Release a mouse's middle button. |
| 569 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( |
| 570 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 571 param4.set_button(0); |
| 572 params_.PushPointerActionParams(param4); |
| 573 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 574 |
| 575 ForwardSyntheticPointerAction(); |
| 576 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = |
| 577 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); |
| 578 EXPECT_EQ(1, num_success_); |
| 579 EXPECT_EQ(0, num_failure_); |
| 580 std::vector<int> buttons(1, 0); |
| 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(1); |
| 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<int> buttons(1, 0); |
| 553 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param, 1)); | 635 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 636 param, 1, buttons)); |
| 554 | 637 |
| 555 ForwardSyntheticPointerAction(); | 638 ForwardSyntheticPointerAction(); |
| 556 EXPECT_EQ(1, num_success_); | 639 EXPECT_EQ(1, num_success_); |
| 557 EXPECT_EQ(2, num_failure_); | 640 EXPECT_EQ(2, num_failure_); |
| 558 } | 641 } |
| 559 | 642 |
| 560 } // namespace | 643 } // namespace |
| 561 | 644 |
| 562 } // namespace content | 645 } // namespace content |
| OLD | NEW |