| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "app/gfx/canvas.h" | 5 #include "app/gfx/canvas.h" |
| 6 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
| 7 #include "base/keyboard_codes.h" | 7 #include "base/keyboard_codes.h" |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "base/shared_memory.h" | 9 #include "base/shared_memory.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 gfx::Rect bounds_; | 117 gfx::Rect bounds_; |
| 118 DISALLOW_COPY_AND_ASSIGN(TestView); | 118 DISALLOW_COPY_AND_ASSIGN(TestView); |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 // MockRenderWidgetHostTest ---------------------------------------------------- | 121 // MockRenderWidgetHostTest ---------------------------------------------------- |
| 122 | 122 |
| 123 class MockRenderWidgetHost : public RenderWidgetHost { | 123 class MockRenderWidgetHost : public RenderWidgetHost { |
| 124 public: | 124 public: |
| 125 MockRenderWidgetHost(RenderProcessHost* process, int routing_id) | 125 MockRenderWidgetHost(RenderProcessHost* process, int routing_id) |
| 126 : RenderWidgetHost(process, routing_id), | 126 : RenderWidgetHost(process, routing_id), |
| 127 prehandle_keyboard_event_(false), |
| 128 prehandle_keyboard_event_called_(false), |
| 129 prehandle_keyboard_event_type_(WebInputEvent::Undefined), |
| 127 unhandled_keyboard_event_called_(false), | 130 unhandled_keyboard_event_called_(false), |
| 128 handle_unhandled_keyboard_event_(false), | |
| 129 unhandled_keyboard_event_type_(WebInputEvent::Undefined) { | 131 unhandled_keyboard_event_type_(WebInputEvent::Undefined) { |
| 130 } | 132 } |
| 131 | 133 |
| 132 // Tests that make sure we ignore keyboard event acknowledgments to events we | 134 // Tests that make sure we ignore keyboard event acknowledgments to events we |
| 133 // didn't send work by making sure we didn't call UnhandledKeyboardEvent(). | 135 // didn't send work by making sure we didn't call UnhandledKeyboardEvent(). |
| 134 bool unhandled_keyboard_event_called() const { | 136 bool unhandled_keyboard_event_called() const { |
| 135 return unhandled_keyboard_event_called_; | 137 return unhandled_keyboard_event_called_; |
| 136 } | 138 } |
| 137 | 139 |
| 138 WebInputEvent::Type unhandled_keyboard_event_type() const { | 140 WebInputEvent::Type unhandled_keyboard_event_type() const { |
| 139 return unhandled_keyboard_event_type_; | 141 return unhandled_keyboard_event_type_; |
| 140 } | 142 } |
| 141 | 143 |
| 142 void set_handle_unhandled_keyboard_event(bool handle) { | 144 bool prehandle_keyboard_event_called() const { |
| 143 handle_unhandled_keyboard_event_ = handle; | 145 return prehandle_keyboard_event_called_; |
| 146 } |
| 147 |
| 148 WebInputEvent::Type prehandle_keyboard_event_type() const { |
| 149 return prehandle_keyboard_event_type_; |
| 150 } |
| 151 |
| 152 void set_prehandle_keyboard_event(bool handle) { |
| 153 prehandle_keyboard_event_ = handle; |
| 144 } | 154 } |
| 145 | 155 |
| 146 protected: | 156 protected: |
| 147 virtual bool UnhandledKeyboardEvent(const NativeWebKeyboardEvent& event) { | 157 virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event, |
| 158 bool* is_keyboard_shortcut) { |
| 159 prehandle_keyboard_event_type_ = event.type; |
| 160 prehandle_keyboard_event_called_ = true; |
| 161 return prehandle_keyboard_event_; |
| 162 } |
| 163 |
| 164 virtual void UnhandledKeyboardEvent(const NativeWebKeyboardEvent& event) { |
| 148 unhandled_keyboard_event_type_ = event.type; | 165 unhandled_keyboard_event_type_ = event.type; |
| 149 unhandled_keyboard_event_called_ = true; | 166 unhandled_keyboard_event_called_ = true; |
| 150 return handle_unhandled_keyboard_event_; | |
| 151 } | 167 } |
| 152 | 168 |
| 153 private: | 169 private: |
| 170 bool prehandle_keyboard_event_; |
| 171 bool prehandle_keyboard_event_called_; |
| 172 WebInputEvent::Type prehandle_keyboard_event_type_; |
| 173 |
| 154 bool unhandled_keyboard_event_called_; | 174 bool unhandled_keyboard_event_called_; |
| 155 bool handle_unhandled_keyboard_event_; | |
| 156 WebInputEvent::Type unhandled_keyboard_event_type_; | 175 WebInputEvent::Type unhandled_keyboard_event_type_; |
| 157 }; | 176 }; |
| 158 | 177 |
| 159 // RenderWidgetHostTest -------------------------------------------------------- | 178 // RenderWidgetHostTest -------------------------------------------------------- |
| 160 | 179 |
| 161 class RenderWidgetHostTest : public testing::Test { | 180 class RenderWidgetHostTest : public testing::Test { |
| 162 public: | 181 public: |
| 163 RenderWidgetHostTest() : process_(NULL) { | 182 RenderWidgetHostTest() : process_(NULL) { |
| 164 } | 183 } |
| 165 ~RenderWidgetHostTest() { | 184 ~RenderWidgetHostTest() { |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 // Make sure we sent the input event to the renderer. | 508 // Make sure we sent the input event to the renderer. |
| 490 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | 509 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 491 ViewMsg_HandleInputEvent::ID)); | 510 ViewMsg_HandleInputEvent::ID)); |
| 492 process_->sink().ClearMessages(); | 511 process_->sink().ClearMessages(); |
| 493 | 512 |
| 494 // Send the simulated response from the renderer back. | 513 // Send the simulated response from the renderer back. |
| 495 SendInputEventACK(WebInputEvent::RawKeyDown, true); | 514 SendInputEventACK(WebInputEvent::RawKeyDown, true); |
| 496 EXPECT_FALSE(host_->unhandled_keyboard_event_called()); | 515 EXPECT_FALSE(host_->unhandled_keyboard_event_called()); |
| 497 } | 516 } |
| 498 | 517 |
| 499 TEST_F(RenderWidgetHostTest, DontSuppressNextCharEventsNoPending) { | 518 TEST_F(RenderWidgetHostTest, PreHandleRawKeyDownEvent) { |
| 519 // Simluate the situation that the browser handled the key down event during |
| 520 // pre-handle phrase. |
| 521 host_->set_prehandle_keyboard_event(true); |
| 522 process_->sink().ClearMessages(); |
| 523 |
| 500 // Simulate a keyboard event. | 524 // Simulate a keyboard event. |
| 501 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); | 525 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); |
| 502 | 526 |
| 503 // Make sure we sent the input event to the renderer. | 527 EXPECT_TRUE(host_->prehandle_keyboard_event_called()); |
| 504 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | 528 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->prehandle_keyboard_event_type()); |
| 505 ViewMsg_HandleInputEvent::ID)); | |
| 506 process_->sink().ClearMessages(); | |
| 507 | 529 |
| 508 // Send the simulated response from the renderer back. | 530 // Make sure the RawKeyDown event is not sent to the renderer. |
| 509 SendInputEventACK(WebInputEvent::RawKeyDown, false); | 531 EXPECT_EQ(0U, process_->sink().message_count()); |
| 510 | 532 |
| 511 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | 533 // The browser won't pre-handle a Char event. |
| 512 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); | 534 host_->set_prehandle_keyboard_event(false); |
| 513 | 535 |
| 514 // Forward the Char event. | 536 // Forward the Char event. |
| 515 SimulateKeyboardEvent(WebInputEvent::Char); | 537 SimulateKeyboardEvent(WebInputEvent::Char); |
| 516 | 538 |
| 517 // Make sure we sent the input event to the renderer. | |
| 518 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | |
| 519 ViewMsg_HandleInputEvent::ID)); | |
| 520 process_->sink().ClearMessages(); | |
| 521 | |
| 522 // Send the simulated response from the renderer back. | |
| 523 SendInputEventACK(WebInputEvent::Char, false); | |
| 524 | |
| 525 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 526 EXPECT_EQ(WebInputEvent::Char, host_->unhandled_keyboard_event_type()); | |
| 527 | |
| 528 // Forward the KeyUp event. | |
| 529 SimulateKeyboardEvent(WebInputEvent::KeyUp); | |
| 530 | |
| 531 // Make sure we sent the input event to the renderer. | |
| 532 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | |
| 533 ViewMsg_HandleInputEvent::ID)); | |
| 534 process_->sink().ClearMessages(); | |
| 535 | |
| 536 // Send the simulated response from the renderer back. | |
| 537 SendInputEventACK(WebInputEvent::KeyUp, false); | |
| 538 | |
| 539 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 540 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); | |
| 541 } | |
| 542 | |
| 543 TEST_F(RenderWidgetHostTest, SuppressNextCharEventsNoPending) { | |
| 544 // Simulate a keyboard event. | |
| 545 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); | |
| 546 | |
| 547 // Make sure we sent the input event to the renderer. | |
| 548 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | |
| 549 ViewMsg_HandleInputEvent::ID)); | |
| 550 process_->sink().ClearMessages(); | |
| 551 | |
| 552 // Simluate the situation that the browser handled the key down event. | |
| 553 host_->set_handle_unhandled_keyboard_event(true); | |
| 554 | |
| 555 // Send the simulated response from the renderer back. | |
| 556 SendInputEventACK(WebInputEvent::RawKeyDown, false); | |
| 557 | |
| 558 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 559 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); | |
| 560 | |
| 561 // Forward the Char event. | |
| 562 SimulateKeyboardEvent(WebInputEvent::Char); | |
| 563 | |
| 564 // Make sure the Char event is suppressed. | |
| 565 EXPECT_EQ(0U, process_->sink().message_count()); | |
| 566 | |
| 567 // Forward another Char event. | |
| 568 SimulateKeyboardEvent(WebInputEvent::Char); | |
| 569 | |
| 570 // Make sure the Char event is suppressed. | 539 // Make sure the Char event is suppressed. |
| 571 EXPECT_EQ(0U, process_->sink().message_count()); | 540 EXPECT_EQ(0U, process_->sink().message_count()); |
| 572 | 541 |
| 573 // Forward the KeyUp event. | 542 // Forward the KeyUp event. |
| 574 SimulateKeyboardEvent(WebInputEvent::KeyUp); | 543 SimulateKeyboardEvent(WebInputEvent::KeyUp); |
| 575 | 544 |
| 576 // Make sure we sent the input event to the renderer. | 545 // Make sure only KeyUp was sent to the renderer. |
| 577 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | |
| 578 ViewMsg_HandleInputEvent::ID)); | |
| 579 process_->sink().ClearMessages(); | |
| 580 | |
| 581 // The browser does not handle KeyUp events. | |
| 582 host_->set_handle_unhandled_keyboard_event(false); | |
| 583 | |
| 584 // Send the simulated response from the renderer back. | |
| 585 SendInputEventACK(WebInputEvent::KeyUp, false); | |
| 586 | |
| 587 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 588 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); | |
| 589 } | |
| 590 | |
| 591 TEST_F(RenderWidgetHostTest, DontSuppressNextCharEventsPending) { | |
| 592 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); | |
| 593 | |
| 594 // Make sure we sent the input event to the renderer. | |
| 595 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | |
| 596 ViewMsg_HandleInputEvent::ID)); | |
| 597 process_->sink().ClearMessages(); | |
| 598 | |
| 599 // Forward the Char event before receiving the ACK of previous KeyDown event. | |
| 600 SimulateKeyboardEvent(WebInputEvent::Char); | |
| 601 | |
| 602 // Make sure the Char event is pending. | |
| 603 EXPECT_EQ(0U, process_->sink().message_count()); | |
| 604 | |
| 605 // Forward the KeyUp event before receiving the ACK of previous KeyDown event. | |
| 606 SimulateKeyboardEvent(WebInputEvent::KeyUp); | |
| 607 | |
| 608 // Make sure the KeyUp event is pending. | |
| 609 EXPECT_EQ(0U, process_->sink().message_count()); | |
| 610 | |
| 611 // Send the simulated response of the KeyDown event from the renderer back. | |
| 612 SendInputEventACK(WebInputEvent::RawKeyDown, false); | |
| 613 | |
| 614 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 615 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); | |
| 616 | |
| 617 // Make sure both pending Char and KeyUp were sent to the renderer. | |
| 618 EXPECT_EQ(2U, process_->sink().message_count()); | |
| 619 EXPECT_EQ(ViewMsg_HandleInputEvent::ID, | |
| 620 process_->sink().GetMessageAt(0)->type()); | |
| 621 EXPECT_EQ(ViewMsg_HandleInputEvent::ID, | |
| 622 process_->sink().GetMessageAt(1)->type()); | |
| 623 process_->sink().ClearMessages(); | |
| 624 | |
| 625 // Send the simulated response from the renderer back. | |
| 626 SendInputEventACK(WebInputEvent::Char, false); | |
| 627 | |
| 628 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 629 EXPECT_EQ(WebInputEvent::Char, host_->unhandled_keyboard_event_type()); | |
| 630 | |
| 631 // Send the simulated response from the renderer back. | |
| 632 SendInputEventACK(WebInputEvent::KeyUp, false); | |
| 633 | |
| 634 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 635 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); | |
| 636 } | |
| 637 | |
| 638 TEST_F(RenderWidgetHostTest, SuppressNextCharEventsPending) { | |
| 639 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); | |
| 640 | |
| 641 // Make sure we sent the KeyDown event to the renderer. | |
| 642 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | |
| 643 ViewMsg_HandleInputEvent::ID)); | |
| 644 process_->sink().ClearMessages(); | |
| 645 | |
| 646 // Forward the Char event before receiving the ACK of previous KeyDown event. | |
| 647 SimulateKeyboardEvent(WebInputEvent::Char); | |
| 648 | |
| 649 // Make sure the Char event is pending. | |
| 650 EXPECT_EQ(0U, process_->sink().message_count()); | |
| 651 | |
| 652 // Forward another Char event before receiving the ACK of previous KeyDown | |
| 653 // event. | |
| 654 SimulateKeyboardEvent(WebInputEvent::Char); | |
| 655 | |
| 656 // Make sure the Char event is pending. | |
| 657 EXPECT_EQ(0U, process_->sink().message_count()); | |
| 658 | |
| 659 // Forward the KeyUp event before receiving the ACK of previous KeyDown event. | |
| 660 SimulateKeyboardEvent(WebInputEvent::KeyUp); | |
| 661 | |
| 662 // Make sure the KeyUp event is pending. | |
| 663 EXPECT_EQ(0U, process_->sink().message_count()); | |
| 664 | |
| 665 // Simluate the situation that the browser handled the key down event. | |
| 666 host_->set_handle_unhandled_keyboard_event(true); | |
| 667 | |
| 668 // Send the simulated response of the KeyDown event from the renderer back. | |
| 669 SendInputEventACK(WebInputEvent::RawKeyDown, false); | |
| 670 | |
| 671 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 672 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); | |
| 673 | |
| 674 // Make sure only pending KeyUp was sent to the renderer. | |
| 675 EXPECT_EQ(1U, process_->sink().message_count()); | 546 EXPECT_EQ(1U, process_->sink().message_count()); |
| 676 EXPECT_EQ(ViewMsg_HandleInputEvent::ID, | 547 EXPECT_EQ(ViewMsg_HandleInputEvent::ID, |
| 677 process_->sink().GetMessageAt(0)->type()); | 548 process_->sink().GetMessageAt(0)->type()); |
| 678 process_->sink().ClearMessages(); | 549 process_->sink().ClearMessages(); |
| 679 | 550 |
| 680 // Send the simulated response from the renderer back. | 551 // Send the simulated response from the renderer back. |
| 681 SendInputEventACK(WebInputEvent::KeyUp, false); | 552 SendInputEventACK(WebInputEvent::KeyUp, false); |
| 682 | 553 |
| 683 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | 554 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 684 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); | 555 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); |
| 685 } | 556 } |
| 686 | |
| 687 TEST_F(RenderWidgetHostTest, ManyKeyEventsPending) { | |
| 688 process_->sink().ClearMessages(); | |
| 689 | |
| 690 for (int i = 0; i < 10; ++i) { | |
| 691 // Forward a KeyDown event. | |
| 692 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); | |
| 693 | |
| 694 // Forward a Char event before receiving the ACK of previous KeyDown event. | |
| 695 SimulateKeyboardEvent(WebInputEvent::Char); | |
| 696 | |
| 697 // Forward a KeyUp event before receiving the ACK of previous KeyDown event. | |
| 698 SimulateKeyboardEvent(WebInputEvent::KeyUp); | |
| 699 } | |
| 700 | |
| 701 // Make sure only the first KeyDown event was sent to the renderer. All others | |
| 702 // are pending. | |
| 703 EXPECT_EQ(1U, process_->sink().message_count()); | |
| 704 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | |
| 705 ViewMsg_HandleInputEvent::ID)); | |
| 706 process_->sink().ClearMessages(); | |
| 707 | |
| 708 for (int i = 0; i < 10; ++i) { | |
| 709 // Send the simulated response of the KeyDown event from the renderer back. | |
| 710 SendInputEventACK(WebInputEvent::RawKeyDown, false); | |
| 711 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 712 EXPECT_EQ(WebInputEvent::RawKeyDown, | |
| 713 host_->unhandled_keyboard_event_type()); | |
| 714 | |
| 715 // Make sure the following pending Char, KeyUp and KeyDown event were sent | |
| 716 // to the renderer. | |
| 717 if (i < 9) | |
| 718 EXPECT_EQ(3U, process_->sink().message_count()); | |
| 719 else | |
| 720 EXPECT_EQ(2U, process_->sink().message_count()); | |
| 721 process_->sink().ClearMessages(); | |
| 722 | |
| 723 // Send the simulated response of the Char event from the renderer back. | |
| 724 SendInputEventACK(WebInputEvent::Char, false); | |
| 725 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 726 EXPECT_EQ(WebInputEvent::Char, host_->unhandled_keyboard_event_type()); | |
| 727 | |
| 728 // Send the simulated response of the KeyUp event from the renderer back. | |
| 729 SendInputEventACK(WebInputEvent::KeyUp, false); | |
| 730 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
| 731 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); | |
| 732 } | |
| 733 } | |
| OLD | NEW |