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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 gfx::Rect bounds_; | 116 gfx::Rect bounds_; |
117 DISALLOW_COPY_AND_ASSIGN(TestView); | 117 DISALLOW_COPY_AND_ASSIGN(TestView); |
118 }; | 118 }; |
119 | 119 |
120 // MockRenderWidgetHostTest ---------------------------------------------------- | 120 // MockRenderWidgetHostTest ---------------------------------------------------- |
121 | 121 |
122 class MockRenderWidgetHost : public RenderWidgetHost { | 122 class MockRenderWidgetHost : public RenderWidgetHost { |
123 public: | 123 public: |
124 MockRenderWidgetHost(RenderProcessHost* process, int routing_id) | 124 MockRenderWidgetHost(RenderProcessHost* process, int routing_id) |
125 : RenderWidgetHost(process, routing_id), | 125 : RenderWidgetHost(process, routing_id), |
126 unhandled_keyboard_event_called_(false) { | 126 unhandled_keyboard_event_called_(false), |
| 127 handle_unhandled_keyboard_event_(false), |
| 128 unhandled_keyboard_event_type_(WebInputEvent::Undefined) { |
127 } | 129 } |
128 | 130 |
129 // Tests that make sure we ignore keyboard event acknowledgments to events we | 131 // Tests that make sure we ignore keyboard event acknowledgments to events we |
130 // didn't send work by making sure we didn't call UnhandledKeyboardEvent(). | 132 // didn't send work by making sure we didn't call UnhandledKeyboardEvent(). |
131 bool unhandled_keyboard_event_called() const { | 133 bool unhandled_keyboard_event_called() const { |
132 return unhandled_keyboard_event_called_; | 134 return unhandled_keyboard_event_called_; |
133 } | 135 } |
134 | 136 |
| 137 WebInputEvent::Type unhandled_keyboard_event_type() const { |
| 138 return unhandled_keyboard_event_type_; |
| 139 } |
| 140 |
| 141 void set_handle_unhandled_keyboard_event(bool handle) { |
| 142 handle_unhandled_keyboard_event_ = handle; |
| 143 } |
| 144 |
135 protected: | 145 protected: |
136 virtual void UnhandledKeyboardEvent(const NativeWebKeyboardEvent& event) { | 146 virtual bool UnhandledKeyboardEvent(const NativeWebKeyboardEvent& event) { |
| 147 unhandled_keyboard_event_type_ = event.type; |
137 unhandled_keyboard_event_called_ = true; | 148 unhandled_keyboard_event_called_ = true; |
| 149 return handle_unhandled_keyboard_event_; |
138 } | 150 } |
139 | 151 |
140 private: | 152 private: |
141 bool unhandled_keyboard_event_called_; | 153 bool unhandled_keyboard_event_called_; |
| 154 bool handle_unhandled_keyboard_event_; |
| 155 WebInputEvent::Type unhandled_keyboard_event_type_; |
142 }; | 156 }; |
143 | 157 |
144 // RenderWidgetHostTest -------------------------------------------------------- | 158 // RenderWidgetHostTest -------------------------------------------------------- |
145 | 159 |
146 class RenderWidgetHostTest : public testing::Test { | 160 class RenderWidgetHostTest : public testing::Test { |
147 public: | 161 public: |
148 RenderWidgetHostTest() : process_(NULL) { | 162 RenderWidgetHostTest() : process_(NULL) { |
149 } | 163 } |
150 ~RenderWidgetHostTest() { | 164 ~RenderWidgetHostTest() { |
151 } | 165 } |
(...skipping 11 matching lines...) Expand all Loading... |
163 void TearDown() { | 177 void TearDown() { |
164 view_.reset(); | 178 view_.reset(); |
165 host_.reset(); | 179 host_.reset(); |
166 process_ = NULL; | 180 process_ = NULL; |
167 profile_.reset(); | 181 profile_.reset(); |
168 | 182 |
169 // Process all pending tasks to avoid leaks. | 183 // Process all pending tasks to avoid leaks. |
170 MessageLoop::current()->RunAllPending(); | 184 MessageLoop::current()->RunAllPending(); |
171 } | 185 } |
172 | 186 |
| 187 void SendInputEventACK(WebInputEvent::Type type, bool processed) { |
| 188 scoped_ptr<IPC::Message> response( |
| 189 new ViewHostMsg_HandleInputEvent_ACK(0)); |
| 190 response->WriteInt(type); |
| 191 response->WriteBool(processed); |
| 192 host_->OnMessageReceived(*response); |
| 193 } |
| 194 |
| 195 void SimulateKeyboardEvent(WebInputEvent::Type type) { |
| 196 NativeWebKeyboardEvent key_event; |
| 197 key_event.type = type; |
| 198 key_event.windowsKeyCode = base::VKEY_L; // non-null made up value. |
| 199 host_->ForwardKeyboardEvent(key_event); |
| 200 } |
| 201 |
173 MessageLoopForUI message_loop_; | 202 MessageLoopForUI message_loop_; |
174 | 203 |
175 scoped_ptr<TestingProfile> profile_; | 204 scoped_ptr<TestingProfile> profile_; |
176 RenderWidgetHostProcess* process_; // Deleted automatically by the widget. | 205 RenderWidgetHostProcess* process_; // Deleted automatically by the widget. |
177 scoped_ptr<MockRenderWidgetHost> host_; | 206 scoped_ptr<MockRenderWidgetHost> host_; |
178 scoped_ptr<TestView> view_; | 207 scoped_ptr<TestView> view_; |
179 | 208 |
180 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostTest); | 209 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostTest); |
181 }; | 210 }; |
182 | 211 |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 // It should have sent out a restored message with a request to paint. | 420 // It should have sent out a restored message with a request to paint. |
392 const IPC::Message* restored = process_->sink().GetUniqueMessageMatching( | 421 const IPC::Message* restored = process_->sink().GetUniqueMessageMatching( |
393 ViewMsg_WasRestored::ID); | 422 ViewMsg_WasRestored::ID); |
394 ASSERT_TRUE(restored); | 423 ASSERT_TRUE(restored); |
395 Tuple1<bool> needs_repaint; | 424 Tuple1<bool> needs_repaint; |
396 ViewMsg_WasRestored::Read(restored, &needs_repaint); | 425 ViewMsg_WasRestored::Read(restored, &needs_repaint); |
397 EXPECT_TRUE(needs_repaint.a); | 426 EXPECT_TRUE(needs_repaint.a); |
398 } | 427 } |
399 | 428 |
400 TEST_F(RenderWidgetHostTest, HandleKeyEventsWeSent) { | 429 TEST_F(RenderWidgetHostTest, HandleKeyEventsWeSent) { |
401 NativeWebKeyboardEvent key_event; | 430 // Simulate a keyboard event. |
402 key_event.type = WebInputEvent::KeyDown; | 431 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); |
403 key_event.modifiers = WebInputEvent::ControlKey; | 432 |
404 key_event.windowsKeyCode = base::VKEY_L; // non-null made up value. | 433 // Make sure we sent the input event to the renderer. |
405 | 434 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
406 host_->ForwardKeyboardEvent(key_event); | 435 ViewMsg_HandleInputEvent::ID)); |
407 | 436 process_->sink().ClearMessages(); |
408 // Make sure we sent the input event to the renderer. | 437 |
409 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( | 438 // Send the simulated response from the renderer back. |
410 ViewMsg_HandleInputEvent::ID)); | 439 SendInputEventACK(WebInputEvent::RawKeyDown, false); |
411 process_->sink().ClearMessages(); | 440 |
412 | 441 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
413 // Send the simulated response from the renderer back. | 442 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); |
414 scoped_ptr<IPC::Message> response( | |
415 new ViewHostMsg_HandleInputEvent_ACK(0)); | |
416 response->WriteInt(key_event.type); | |
417 response->WriteBool(false); | |
418 host_->OnMessageReceived(*response); | |
419 | |
420 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); | |
421 } | 443 } |
422 | 444 |
423 TEST_F(RenderWidgetHostTest, IgnoreKeyEventsWeDidntSend) { | 445 TEST_F(RenderWidgetHostTest, IgnoreKeyEventsWeDidntSend) { |
424 // Send a simulated, unrequested key response. We should ignore this. | 446 // Send a simulated, unrequested key response. We should ignore this. |
425 scoped_ptr<IPC::Message> response( | 447 SendInputEventACK(WebInputEvent::RawKeyDown, false); |
426 new ViewHostMsg_HandleInputEvent_ACK(0)); | |
427 response->WriteInt(WebInputEvent::KeyDown); | |
428 response->WriteBool(false); | |
429 host_->OnMessageReceived(*response); | |
430 | 448 |
431 EXPECT_FALSE(host_->unhandled_keyboard_event_called()); | 449 EXPECT_FALSE(host_->unhandled_keyboard_event_called()); |
432 } | 450 } |
| 451 |
| 452 TEST_F(RenderWidgetHostTest, IgnoreKeyEventsHandledByRenderer) { |
| 453 // Simulate a keyboard event. |
| 454 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); |
| 455 |
| 456 // Make sure we sent the input event to the renderer. |
| 457 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 458 ViewMsg_HandleInputEvent::ID)); |
| 459 process_->sink().ClearMessages(); |
| 460 |
| 461 // Send the simulated response from the renderer back. |
| 462 SendInputEventACK(WebInputEvent::RawKeyDown, true); |
| 463 EXPECT_FALSE(host_->unhandled_keyboard_event_called()); |
| 464 } |
| 465 |
| 466 TEST_F(RenderWidgetHostTest, DontSuppressNextCharEventsNoPending) { |
| 467 // Simulate a keyboard event. |
| 468 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); |
| 469 |
| 470 // Make sure we sent the input event to the renderer. |
| 471 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 472 ViewMsg_HandleInputEvent::ID)); |
| 473 process_->sink().ClearMessages(); |
| 474 |
| 475 // Send the simulated response from the renderer back. |
| 476 SendInputEventACK(WebInputEvent::RawKeyDown, false); |
| 477 |
| 478 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 479 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); |
| 480 |
| 481 // Forward the Char event. |
| 482 SimulateKeyboardEvent(WebInputEvent::Char); |
| 483 |
| 484 // Make sure we sent the input event to the renderer. |
| 485 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 486 ViewMsg_HandleInputEvent::ID)); |
| 487 process_->sink().ClearMessages(); |
| 488 |
| 489 // Send the simulated response from the renderer back. |
| 490 SendInputEventACK(WebInputEvent::Char, false); |
| 491 |
| 492 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 493 EXPECT_EQ(WebInputEvent::Char, host_->unhandled_keyboard_event_type()); |
| 494 |
| 495 // Forward the KeyUp event. |
| 496 SimulateKeyboardEvent(WebInputEvent::KeyUp); |
| 497 |
| 498 // Make sure we sent the input event to the renderer. |
| 499 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 500 ViewMsg_HandleInputEvent::ID)); |
| 501 process_->sink().ClearMessages(); |
| 502 |
| 503 // Send the simulated response from the renderer back. |
| 504 SendInputEventACK(WebInputEvent::KeyUp, false); |
| 505 |
| 506 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 507 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); |
| 508 } |
| 509 |
| 510 TEST_F(RenderWidgetHostTest, SuppressNextCharEventsNoPending) { |
| 511 // Simulate a keyboard event. |
| 512 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); |
| 513 |
| 514 // Make sure we sent the input event to the renderer. |
| 515 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 516 ViewMsg_HandleInputEvent::ID)); |
| 517 process_->sink().ClearMessages(); |
| 518 |
| 519 // Simluate the situation that the browser handled the key down event. |
| 520 host_->set_handle_unhandled_keyboard_event(true); |
| 521 |
| 522 // Send the simulated response from the renderer back. |
| 523 SendInputEventACK(WebInputEvent::RawKeyDown, false); |
| 524 |
| 525 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 526 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); |
| 527 |
| 528 // Forward the Char event. |
| 529 SimulateKeyboardEvent(WebInputEvent::Char); |
| 530 |
| 531 // Make sure the Char event is suppressed. |
| 532 EXPECT_EQ(0U, process_->sink().message_count()); |
| 533 |
| 534 // Forward another Char event. |
| 535 SimulateKeyboardEvent(WebInputEvent::Char); |
| 536 |
| 537 // Make sure the Char event is suppressed. |
| 538 EXPECT_EQ(0U, process_->sink().message_count()); |
| 539 |
| 540 // Forward the KeyUp event. |
| 541 SimulateKeyboardEvent(WebInputEvent::KeyUp); |
| 542 |
| 543 // Make sure we sent the input event to the renderer. |
| 544 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 545 ViewMsg_HandleInputEvent::ID)); |
| 546 process_->sink().ClearMessages(); |
| 547 |
| 548 // The browser does not handle KeyUp events. |
| 549 host_->set_handle_unhandled_keyboard_event(false); |
| 550 |
| 551 // Send the simulated response from the renderer back. |
| 552 SendInputEventACK(WebInputEvent::KeyUp, false); |
| 553 |
| 554 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 555 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); |
| 556 } |
| 557 |
| 558 TEST_F(RenderWidgetHostTest, DontSuppressNextCharEventsPending) { |
| 559 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); |
| 560 |
| 561 // Make sure we sent the input event to the renderer. |
| 562 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 563 ViewMsg_HandleInputEvent::ID)); |
| 564 process_->sink().ClearMessages(); |
| 565 |
| 566 // Forward the Char event before receiving the ACK of previous KeyDown event. |
| 567 SimulateKeyboardEvent(WebInputEvent::Char); |
| 568 |
| 569 // Make sure the Char event is pending. |
| 570 EXPECT_EQ(0U, process_->sink().message_count()); |
| 571 |
| 572 // Forward the KeyUp event before receiving the ACK of previous KeyDown event. |
| 573 SimulateKeyboardEvent(WebInputEvent::KeyUp); |
| 574 |
| 575 // Make sure the KeyUp event is pending. |
| 576 EXPECT_EQ(0U, process_->sink().message_count()); |
| 577 |
| 578 // Send the simulated response of the KeyDown event from the renderer back. |
| 579 SendInputEventACK(WebInputEvent::RawKeyDown, false); |
| 580 |
| 581 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 582 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); |
| 583 |
| 584 // Make sure both pending Char and KeyUp were sent to the renderer. |
| 585 EXPECT_EQ(2U, process_->sink().message_count()); |
| 586 EXPECT_EQ(ViewMsg_HandleInputEvent::ID, |
| 587 process_->sink().GetMessageAt(0)->type()); |
| 588 EXPECT_EQ(ViewMsg_HandleInputEvent::ID, |
| 589 process_->sink().GetMessageAt(1)->type()); |
| 590 process_->sink().ClearMessages(); |
| 591 |
| 592 // Send the simulated response from the renderer back. |
| 593 SendInputEventACK(WebInputEvent::Char, false); |
| 594 |
| 595 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 596 EXPECT_EQ(WebInputEvent::Char, host_->unhandled_keyboard_event_type()); |
| 597 |
| 598 // Send the simulated response from the renderer back. |
| 599 SendInputEventACK(WebInputEvent::KeyUp, false); |
| 600 |
| 601 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 602 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); |
| 603 } |
| 604 |
| 605 TEST_F(RenderWidgetHostTest, SuppressNextCharEventsPending) { |
| 606 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); |
| 607 |
| 608 // Make sure we sent the KeyDown event to the renderer. |
| 609 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 610 ViewMsg_HandleInputEvent::ID)); |
| 611 process_->sink().ClearMessages(); |
| 612 |
| 613 // Forward the Char event before receiving the ACK of previous KeyDown event. |
| 614 SimulateKeyboardEvent(WebInputEvent::Char); |
| 615 |
| 616 // Make sure the Char event is pending. |
| 617 EXPECT_EQ(0U, process_->sink().message_count()); |
| 618 |
| 619 // Forward another Char event before receiving the ACK of previous KeyDown |
| 620 // event. |
| 621 SimulateKeyboardEvent(WebInputEvent::Char); |
| 622 |
| 623 // Make sure the Char event is pending. |
| 624 EXPECT_EQ(0U, process_->sink().message_count()); |
| 625 |
| 626 // Forward the KeyUp event before receiving the ACK of previous KeyDown event. |
| 627 SimulateKeyboardEvent(WebInputEvent::KeyUp); |
| 628 |
| 629 // Make sure the KeyUp event is pending. |
| 630 EXPECT_EQ(0U, process_->sink().message_count()); |
| 631 |
| 632 // Simluate the situation that the browser handled the key down event. |
| 633 host_->set_handle_unhandled_keyboard_event(true); |
| 634 |
| 635 // Send the simulated response of the KeyDown event from the renderer back. |
| 636 SendInputEventACK(WebInputEvent::RawKeyDown, false); |
| 637 |
| 638 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 639 EXPECT_EQ(WebInputEvent::RawKeyDown, host_->unhandled_keyboard_event_type()); |
| 640 |
| 641 // Make sure only pending KeyUp was sent to the renderer. |
| 642 EXPECT_EQ(1U, process_->sink().message_count()); |
| 643 EXPECT_EQ(ViewMsg_HandleInputEvent::ID, |
| 644 process_->sink().GetMessageAt(0)->type()); |
| 645 process_->sink().ClearMessages(); |
| 646 |
| 647 // Send the simulated response from the renderer back. |
| 648 SendInputEventACK(WebInputEvent::KeyUp, false); |
| 649 |
| 650 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 651 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); |
| 652 } |
| 653 |
| 654 TEST_F(RenderWidgetHostTest, ManyKeyEventsPending) { |
| 655 process_->sink().ClearMessages(); |
| 656 |
| 657 for (int i = 0; i < 10; ++i) { |
| 658 // Forward a KeyDown event. |
| 659 SimulateKeyboardEvent(WebInputEvent::RawKeyDown); |
| 660 |
| 661 // Forward a Char event before receiving the ACK of previous KeyDown event. |
| 662 SimulateKeyboardEvent(WebInputEvent::Char); |
| 663 |
| 664 // Forward a KeyUp event before receiving the ACK of previous KeyDown event. |
| 665 SimulateKeyboardEvent(WebInputEvent::KeyUp); |
| 666 } |
| 667 |
| 668 // Make sure only the first KeyDown event was sent to the renderer. All others |
| 669 // are pending. |
| 670 EXPECT_EQ(1U, process_->sink().message_count()); |
| 671 EXPECT_TRUE(process_->sink().GetUniqueMessageMatching( |
| 672 ViewMsg_HandleInputEvent::ID)); |
| 673 process_->sink().ClearMessages(); |
| 674 |
| 675 for (int i = 0; i < 10; ++i) { |
| 676 // Send the simulated response of the KeyDown event from the renderer back. |
| 677 SendInputEventACK(WebInputEvent::RawKeyDown, false); |
| 678 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 679 EXPECT_EQ(WebInputEvent::RawKeyDown, |
| 680 host_->unhandled_keyboard_event_type()); |
| 681 |
| 682 // Make sure the following pending Char, KeyUp and KeyDown event were sent |
| 683 // to the renderer. |
| 684 if (i < 9) |
| 685 EXPECT_EQ(3U, process_->sink().message_count()); |
| 686 else |
| 687 EXPECT_EQ(2U, process_->sink().message_count()); |
| 688 process_->sink().ClearMessages(); |
| 689 |
| 690 // Send the simulated response of the Char event from the renderer back. |
| 691 SendInputEventACK(WebInputEvent::Char, false); |
| 692 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 693 EXPECT_EQ(WebInputEvent::Char, host_->unhandled_keyboard_event_type()); |
| 694 |
| 695 // Send the simulated response of the KeyUp event from the renderer back. |
| 696 SendInputEventACK(WebInputEvent::KeyUp, false); |
| 697 EXPECT_TRUE(host_->unhandled_keyboard_event_called()); |
| 698 EXPECT_EQ(WebInputEvent::KeyUp, host_->unhandled_keyboard_event_type()); |
| 699 } |
| 700 } |
OLD | NEW |