Chromium Code Reviews| 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" | 10 #include "content/browser/renderer_host/input/synthetic_touch_driver.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 case SyntheticPointerActionParams::PointerActionType::IDLE: | 54 case SyntheticPointerActionParams::PointerActionType::IDLE: |
| 55 case SyntheticPointerActionParams::PointerActionType::NOT_INITIALIZED: | 55 case SyntheticPointerActionParams::PointerActionType::NOT_INITIALIZED: |
| 56 NOTREACHED() | 56 NOTREACHED() |
| 57 << "Invalid SyntheticPointerActionParams::PointerActionType."; | 57 << "Invalid SyntheticPointerActionParams::PointerActionType."; |
| 58 return WebInputEvent::Undefined; | 58 return WebInputEvent::Undefined; |
| 59 } | 59 } |
| 60 NOTREACHED() << "Invalid SyntheticPointerActionParams::PointerActionType."; | 60 NOTREACHED() << "Invalid SyntheticPointerActionParams::PointerActionType."; |
| 61 return WebInputEvent::Undefined; | 61 return WebInputEvent::Undefined; |
| 62 } | 62 } |
| 63 | 63 |
| 64 blink::WebMouseEvent::Button GetWebMouseEventButton(int button) { | |
|
tdresser
2017/01/17 13:23:18
We shouldn't duplicate these.
Let's put them some
lanwei
2017/01/17 19:50:34
Done.
| |
| 65 switch (button) { | |
| 66 case 0: | |
| 67 return blink::WebMouseEvent::Button::Left; | |
| 68 case 1: | |
| 69 return blink::WebMouseEvent::Button::Middle; | |
| 70 case 2: | |
| 71 return blink::WebMouseEvent::Button::Right; | |
| 72 } | |
| 73 NOTREACHED(); | |
| 74 return blink::WebMouseEvent::Button::NoButton; | |
| 75 } | |
| 76 | |
| 77 unsigned GetWebMouseEventModifier(int button) { | |
| 78 switch (button) { | |
| 79 case 0: | |
| 80 return blink::WebMouseEvent::LeftButtonDown; | |
| 81 case 1: | |
| 82 return blink::WebMouseEvent::MiddleButtonDown; | |
| 83 case 2: | |
| 84 return blink::WebMouseEvent::RightButtonDown; | |
| 85 } | |
| 86 NOTREACHED(); | |
| 87 return blink::WebMouseEvent::NoModifiers; | |
| 88 } | |
| 89 | |
| 64 class MockSyntheticPointerActionTarget : public SyntheticGestureTarget { | 90 class MockSyntheticPointerActionTarget : public SyntheticGestureTarget { |
| 65 public: | 91 public: |
| 66 MockSyntheticPointerActionTarget() {} | 92 MockSyntheticPointerActionTarget() {} |
| 67 ~MockSyntheticPointerActionTarget() override {} | 93 ~MockSyntheticPointerActionTarget() override {} |
| 68 | 94 |
| 69 // SyntheticGestureTarget: | 95 // SyntheticGestureTarget: |
| 70 void SetNeedsFlush() override { NOTIMPLEMENTED(); } | 96 void SetNeedsFlush() override { NOTIMPLEMENTED(); } |
| 71 | 97 |
| 72 base::TimeDelta PointerAssumedStoppedTime() const override { | 98 base::TimeDelta PointerAssumedStoppedTime() const override { |
| 73 NOTIMPLEMENTED(); | 99 NOTIMPLEMENTED(); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 private: | 190 private: |
| 165 gfx::PointF positions_[WebTouchEvent::kTouchesLengthCap]; | 191 gfx::PointF positions_[WebTouchEvent::kTouchesLengthCap]; |
| 166 unsigned touch_length_; | 192 unsigned touch_length_; |
| 167 int indexes_[WebTouchEvent::kTouchesLengthCap]; | 193 int indexes_[WebTouchEvent::kTouchesLengthCap]; |
| 168 WebTouchPoint::State states_[WebTouchEvent::kTouchesLengthCap]; | 194 WebTouchPoint::State states_[WebTouchEvent::kTouchesLengthCap]; |
| 169 }; | 195 }; |
| 170 | 196 |
| 171 class MockSyntheticPointerMouseActionTarget | 197 class MockSyntheticPointerMouseActionTarget |
| 172 : public MockSyntheticPointerActionTarget { | 198 : public MockSyntheticPointerActionTarget { |
| 173 public: | 199 public: |
| 174 MockSyntheticPointerMouseActionTarget() {} | 200 MockSyntheticPointerMouseActionTarget() : click_count_(0), modifiers_(0) {} |
| 175 ~MockSyntheticPointerMouseActionTarget() override {} | 201 ~MockSyntheticPointerMouseActionTarget() override {} |
| 176 | 202 |
| 177 void DispatchInputEventToPlatform(const WebInputEvent& event) override { | 203 void DispatchInputEventToPlatform(const WebInputEvent& event) override { |
| 178 DCHECK(WebInputEvent::isMouseEventType(event.type())); | 204 DCHECK(WebInputEvent::isMouseEventType(event.type())); |
| 179 const WebMouseEvent& mouse_event = static_cast<const WebMouseEvent&>(event); | 205 const WebMouseEvent& mouse_event = static_cast<const WebMouseEvent&>(event); |
| 180 type_ = mouse_event.type(); | 206 type_ = mouse_event.type(); |
| 181 position_ = gfx::PointF(mouse_event.x, mouse_event.y); | 207 position_ = gfx::PointF(mouse_event.x, mouse_event.y); |
| 182 clickCount_ = mouse_event.clickCount; | 208 click_count_ = mouse_event.clickCount; |
| 209 modifiers_ = mouse_event.modifiers(); | |
| 183 button_ = mouse_event.button; | 210 button_ = mouse_event.button; |
| 184 } | 211 } |
| 185 | 212 |
| 186 testing::AssertionResult SyntheticMouseActionDispatchedCorrectly( | 213 testing::AssertionResult SyntheticMouseActionDispatchedCorrectly( |
| 187 const SyntheticPointerActionParams& param, | 214 const SyntheticPointerActionParams& param, |
| 188 int click_count) { | 215 int click_count, |
| 216 std::vector<int> buttons) { | |
| 189 if (type_ != ToWebMouseEventType(param.pointer_action_type())) { | 217 if (type_ != ToWebMouseEventType(param.pointer_action_type())) { |
| 190 return testing::AssertionFailure() | 218 return testing::AssertionFailure() |
| 191 << "Pointer type was " << WebInputEvent::GetName(type_) | 219 << "Pointer type was " << WebInputEvent::GetName(type_) |
| 192 << ", expected " << WebInputEvent::GetName(ToWebMouseEventType( | 220 << ", expected " << WebInputEvent::GetName(ToWebMouseEventType( |
| 193 param.pointer_action_type())) << "."; | 221 param.pointer_action_type())) << "."; |
| 194 } | 222 } |
| 195 | 223 |
| 196 if (clickCount_ != click_count) { | 224 if (click_count_ != click_count) { |
| 197 return testing::AssertionFailure() << "Pointer click count was " | 225 return testing::AssertionFailure() << "Pointer click count was " |
| 198 << clickCount_ << ", expected " | 226 << click_count_ << ", expected " |
| 199 << click_count << "."; | 227 << click_count << "."; |
| 200 } | 228 } |
| 201 | 229 |
| 202 if (clickCount_ == 1 && button_ != WebMouseEvent::Button::Left) { | 230 if (click_count_ == 1) { |
| 203 return testing::AssertionFailure() | 231 DCHECK_GT(buttons.size(), 0U); |
| 204 << "Pointer button was " << (int)button_ << ", expected " | 232 int button = buttons[buttons.size() - 1]; |
| 205 << (int)WebMouseEvent::Button::Left << "."; | 233 if (button_ != GetWebMouseEventButton(button)) { |
| 234 return testing::AssertionFailure() << "Pointer button was " | |
| 235 << (int)button_ << ", expected " | |
|
tdresser
2017/01/17 13:23:18
static_cast
lanwei
2017/01/17 19:50:34
Done.
| |
| 236 << button << "."; | |
| 237 } | |
| 206 } | 238 } |
| 207 | 239 |
| 208 if (clickCount_ == 0 && button_ != WebMouseEvent::Button::NoButton) { | 240 if (click_count_ == 0 && button_ != WebMouseEvent::Button::NoButton) { |
| 241 DCHECK_EQ(buttons.size(), 0U); | |
| 209 return testing::AssertionFailure() | 242 return testing::AssertionFailure() |
| 210 << "Pointer button was " << (int)button_ << ", expected " | 243 << "Pointer button was " << (int)button_ << ", expected " |
| 211 << (int)WebMouseEvent::Button::NoButton << "."; | 244 << (int)WebMouseEvent::Button::NoButton << "."; |
| 212 } | 245 } |
| 213 | 246 |
| 247 int modifiers = 0; | |
| 248 for (size_t index = 0; index < buttons.size(); ++index) | |
| 249 modifiers |= GetWebMouseEventModifier(buttons[index]); | |
| 250 if (modifiers_ != modifiers) { | |
| 251 return testing::AssertionFailure() << "Pointer modifiers was " | |
| 252 << modifiers_ << ", expected " | |
| 253 << modifiers << "."; | |
| 254 } | |
| 255 | |
| 214 if ((param.pointer_action_type() == | 256 if ((param.pointer_action_type() == |
| 215 SyntheticPointerActionParams::PointerActionType::PRESS || | 257 SyntheticPointerActionParams::PointerActionType::PRESS || |
| 216 param.pointer_action_type() == | 258 param.pointer_action_type() == |
| 217 SyntheticPointerActionParams::PointerActionType::MOVE) && | 259 SyntheticPointerActionParams::PointerActionType::MOVE) && |
| 218 position_ != param.position()) { | 260 position_ != param.position()) { |
| 219 return testing::AssertionFailure() | 261 return testing::AssertionFailure() |
| 220 << "Pointer position was " << position_.ToString() | 262 << "Pointer position was " << position_.ToString() |
| 221 << ", expected " << param.position().ToString() << "."; | 263 << ", expected " << param.position().ToString() << "."; |
| 222 } | 264 } |
| 223 return testing::AssertionSuccess(); | 265 return testing::AssertionSuccess(); |
| 224 } | 266 } |
| 225 | 267 |
| 226 SyntheticGestureParams::GestureSourceType | 268 SyntheticGestureParams::GestureSourceType |
| 227 GetDefaultSyntheticGestureSourceType() const override { | 269 GetDefaultSyntheticGestureSourceType() const override { |
| 228 return SyntheticGestureParams::MOUSE_INPUT; | 270 return SyntheticGestureParams::MOUSE_INPUT; |
| 229 } | 271 } |
| 230 | 272 |
| 231 private: | 273 private: |
| 232 gfx::PointF position_; | 274 gfx::PointF position_; |
| 233 int clickCount_; | 275 int click_count_; |
| 276 int modifiers_; | |
| 234 WebMouseEvent::Button button_; | 277 WebMouseEvent::Button button_; |
| 235 }; | 278 }; |
| 236 | 279 |
| 237 class SyntheticPointerActionTest : public testing::Test { | 280 class SyntheticPointerActionTest : public testing::Test { |
| 238 public: | 281 public: |
| 239 SyntheticPointerActionTest() { | 282 SyntheticPointerActionTest() { |
| 240 params_ = SyntheticPointerActionListParams(); | 283 params_ = SyntheticPointerActionListParams(); |
| 241 num_success_ = 0; | 284 num_success_ = 0; |
| 242 num_failure_ = 0; | 285 num_failure_ = 0; |
| 243 } | 286 } |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 491 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( | 534 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( |
| 492 SyntheticPointerActionParams::PointerActionType::RELEASE); | 535 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 493 params_.PushPointerActionParams(param4); | 536 params_.PushPointerActionParams(param4); |
| 494 pointer_action_.reset(new SyntheticPointerAction(params_)); | 537 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 495 | 538 |
| 496 ForwardSyntheticPointerAction(); | 539 ForwardSyntheticPointerAction(); |
| 497 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = | 540 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = |
| 498 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); | 541 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); |
| 499 EXPECT_EQ(1, num_success_); | 542 EXPECT_EQ(1, num_success_); |
| 500 EXPECT_EQ(0, num_failure_); | 543 EXPECT_EQ(0, num_failure_); |
| 501 EXPECT_TRUE( | 544 std::vector<int> buttons; |
| 502 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param1, 0)); | 545 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 546 param1, 0, buttons)); | |
| 503 | 547 |
| 504 ForwardSyntheticPointerAction(); | 548 ForwardSyntheticPointerAction(); |
| 505 EXPECT_EQ(2, num_success_); | 549 EXPECT_EQ(2, num_success_); |
| 506 EXPECT_EQ(0, num_failure_); | 550 EXPECT_EQ(0, num_failure_); |
| 507 EXPECT_TRUE( | 551 buttons.push_back(0); |
| 508 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param2, 1)); | 552 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 553 param2, 1, buttons)); | |
| 509 | 554 |
| 510 ForwardSyntheticPointerAction(); | 555 ForwardSyntheticPointerAction(); |
| 511 EXPECT_EQ(3, num_success_); | 556 EXPECT_EQ(3, num_success_); |
| 512 EXPECT_EQ(0, num_failure_); | 557 EXPECT_EQ(0, num_failure_); |
| 513 EXPECT_TRUE( | 558 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 514 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param3, 1)); | 559 param3, 1, buttons)); |
| 515 | 560 |
| 516 ForwardSyntheticPointerAction(); | 561 ForwardSyntheticPointerAction(); |
| 517 EXPECT_EQ(4, num_success_); | 562 EXPECT_EQ(4, num_success_); |
| 518 EXPECT_EQ(0, num_failure_); | 563 EXPECT_EQ(0, num_failure_); |
| 519 EXPECT_TRUE( | 564 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 520 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param4, 1)); | 565 param4, 1, buttons)); |
| 566 } | |
| 567 | |
| 568 TEST_F(SyntheticPointerActionTest, PointerMouseActionMultiPress) { | |
| 569 CreateSyntheticPointerActionTarget<MockSyntheticPointerMouseActionTarget>(); | |
| 570 | |
| 571 // Press a mouse's left button. | |
| 572 SyntheticPointerActionParams param1 = SyntheticPointerActionParams( | |
| 573 SyntheticPointerActionParams::PointerActionType::PRESS); | |
| 574 param1.set_position(gfx::PointF(189, 62)); | |
| 575 param1.set_button(0); | |
| 576 params_.PushPointerActionParams(param1); | |
| 577 | |
| 578 // Press a mouse's middle button. | |
| 579 SyntheticPointerActionParams param2 = SyntheticPointerActionParams( | |
| 580 SyntheticPointerActionParams::PointerActionType::PRESS); | |
| 581 param2.set_position(gfx::PointF(189, 62)); | |
| 582 param2.set_button(1); | |
| 583 params_.PushPointerActionParams(param2); | |
| 584 | |
| 585 // Release a mouse's middle button. | |
| 586 SyntheticPointerActionParams param3 = SyntheticPointerActionParams( | |
| 587 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
| 588 param3.set_button(1); | |
| 589 params_.PushPointerActionParams(param3); | |
| 590 pointer_action_.reset(new SyntheticPointerAction(params_)); | |
| 591 | |
| 592 // Release a mouse's middle button. | |
| 593 SyntheticPointerActionParams param4 = SyntheticPointerActionParams( | |
| 594 SyntheticPointerActionParams::PointerActionType::RELEASE); | |
| 595 param4.set_button(0); | |
| 596 params_.PushPointerActionParams(param4); | |
| 597 pointer_action_.reset(new SyntheticPointerAction(params_)); | |
| 598 | |
| 599 ForwardSyntheticPointerAction(); | |
| 600 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = | |
| 601 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); | |
| 602 EXPECT_EQ(1, num_success_); | |
| 603 EXPECT_EQ(0, num_failure_); | |
| 604 std::vector<int> buttons(1, 0); | |
| 605 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( | |
| 606 param1, 1, buttons)); | |
| 607 | |
| 608 ForwardSyntheticPointerAction(); | |
| 609 EXPECT_EQ(2, num_success_); | |
| 610 EXPECT_EQ(0, num_failure_); | |
| 611 buttons.push_back(1); | |
| 612 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( | |
| 613 param2, 1, buttons)); | |
| 614 | |
| 615 ForwardSyntheticPointerAction(); | |
| 616 EXPECT_EQ(3, num_success_); | |
| 617 EXPECT_EQ(0, num_failure_); | |
| 618 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( | |
| 619 param3, 1, buttons)); | |
| 620 | |
| 621 ForwardSyntheticPointerAction(); | |
| 622 EXPECT_EQ(4, num_success_); | |
| 623 EXPECT_EQ(0, num_failure_); | |
| 624 buttons.pop_back(); | |
| 625 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( | |
| 626 param4, 1, buttons)); | |
| 521 } | 627 } |
| 522 | 628 |
| 523 TEST_F(SyntheticPointerActionTest, PointerMouseActionTypeInvalid) { | 629 TEST_F(SyntheticPointerActionTest, PointerMouseActionTypeInvalid) { |
| 524 CreateSyntheticPointerActionTarget<MockSyntheticPointerMouseActionTarget>(); | 630 CreateSyntheticPointerActionTarget<MockSyntheticPointerMouseActionTarget>(); |
| 525 | 631 |
| 526 // Cannot send a mouse up without sending a mouse down first. | 632 // Cannot send a mouse up without sending a mouse down first. |
| 527 SyntheticPointerActionParams param = SyntheticPointerActionParams( | 633 SyntheticPointerActionParams param = SyntheticPointerActionParams( |
| 528 SyntheticPointerActionParams::PointerActionType::RELEASE); | 634 SyntheticPointerActionParams::PointerActionType::RELEASE); |
| 529 params_.PushPointerActionParams(param); | 635 params_.PushPointerActionParams(param); |
| 530 pointer_action_.reset(new SyntheticPointerAction(params_)); | 636 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 542 | 648 |
| 543 // Cannot send a mouse down again without releasing the mouse button. | 649 // Cannot send a mouse down again without releasing the mouse button. |
| 544 params_.PushPointerActionParams(param); | 650 params_.PushPointerActionParams(param); |
| 545 pointer_action_.reset(new SyntheticPointerAction(params_)); | 651 pointer_action_.reset(new SyntheticPointerAction(params_)); |
| 546 | 652 |
| 547 ForwardSyntheticPointerAction(); | 653 ForwardSyntheticPointerAction(); |
| 548 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = | 654 MockSyntheticPointerMouseActionTarget* pointer_mouse_target = |
| 549 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); | 655 static_cast<MockSyntheticPointerMouseActionTarget*>(target_.get()); |
| 550 EXPECT_EQ(1, num_success_); | 656 EXPECT_EQ(1, num_success_); |
| 551 EXPECT_EQ(1, num_failure_); | 657 EXPECT_EQ(1, num_failure_); |
| 552 EXPECT_TRUE( | 658 std::vector<int> buttons(1, 0); |
| 553 pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly(param, 1)); | 659 EXPECT_TRUE(pointer_mouse_target->SyntheticMouseActionDispatchedCorrectly( |
| 660 param, 1, buttons)); | |
| 554 | 661 |
| 555 ForwardSyntheticPointerAction(); | 662 ForwardSyntheticPointerAction(); |
| 556 EXPECT_EQ(1, num_success_); | 663 EXPECT_EQ(1, num_success_); |
| 557 EXPECT_EQ(2, num_failure_); | 664 EXPECT_EQ(2, num_failure_); |
| 558 } | 665 } |
| 559 | 666 |
| 560 } // namespace | 667 } // namespace |
| 561 | 668 |
| 562 } // namespace content | 669 } // namespace content |
| OLD | NEW |