OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/mus/ws/event_dispatcher.h" | 5 #include "components/mus/ws/event_dispatcher.h" |
6 | 6 |
7 #include "components/mus/public/cpp/event_matcher.h" | 7 #include "components/mus/public/cpp/event_matcher.h" |
8 #include "components/mus/ws/event_dispatcher_delegate.h" | 8 #include "components/mus/ws/event_dispatcher_delegate.h" |
9 #include "components/mus/ws/server_window.h" | 9 #include "components/mus/ws/server_window.h" |
10 #include "components/mus/ws/test_server_window_delegate.h" | 10 #include "components/mus/ws/test_server_window_delegate.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 void OnAccelerator(uint32_t accelerator, mojom::EventPtr event) override { | 60 void OnAccelerator(uint32_t accelerator, mojom::EventPtr event) override { |
61 EXPECT_EQ(0u, last_accelerator_); | 61 EXPECT_EQ(0u, last_accelerator_); |
62 last_accelerator_ = accelerator; | 62 last_accelerator_ = accelerator; |
63 } | 63 } |
64 void SetFocusedWindowFromEventDispatcher(ServerWindow* window) override { | 64 void SetFocusedWindowFromEventDispatcher(ServerWindow* window) override { |
65 focused_window_ = window; | 65 focused_window_ = window; |
66 } | 66 } |
67 ServerWindow* GetFocusedWindowForEventDispatcher() override { | 67 ServerWindow* GetFocusedWindowForEventDispatcher() override { |
68 return focused_window_; | 68 return focused_window_; |
69 } | 69 } |
| 70 void OnLostCapture(ServerWindow* window) override {} |
70 void DispatchInputEventToWindow(ServerWindow* target, | 71 void DispatchInputEventToWindow(ServerWindow* target, |
71 bool in_nonclient_area, | 72 bool in_nonclient_area, |
72 mojom::EventPtr event) override { | 73 mojom::EventPtr event) override { |
73 last_target_ = target; | 74 last_target_ = target; |
74 last_dispatched_event_ = event.Pass(); | 75 last_dispatched_event_ = event.Pass(); |
75 last_in_nonclient_area_ = in_nonclient_area; | 76 last_in_nonclient_area_ = in_nonclient_area; |
76 } | 77 } |
77 | 78 |
78 ServerWindow* root_; | 79 ServerWindow* root_; |
79 ServerWindow* focused_window_; | 80 ServerWindow* focused_window_; |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 | 494 |
494 const ui::TouchEvent drag_event1(ui::ET_TOUCH_MOVED, gfx::Point(53, 54), 1, | 495 const ui::TouchEvent drag_event1(ui::ET_TOUCH_MOVED, gfx::Point(53, 54), 1, |
495 base::TimeDelta()); | 496 base::TimeDelta()); |
496 dispatcher.OnEvent( | 497 dispatcher.OnEvent( |
497 mojom::Event::From(static_cast<const ui::Event&>(drag_event1))); | 498 mojom::Event::From(static_cast<const ui::Event&>(drag_event1))); |
498 EXPECT_EQ(nullptr, event_dispatcher_delegate.GetAndClearLastTarget()); | 499 EXPECT_EQ(nullptr, event_dispatcher_delegate.GetAndClearLastTarget()); |
499 EXPECT_EQ(nullptr, | 500 EXPECT_EQ(nullptr, |
500 event_dispatcher_delegate.GetAndClearLastDispatchedEvent().get()); | 501 event_dispatcher_delegate.GetAndClearLastDispatchedEvent().get()); |
501 } | 502 } |
502 | 503 |
| 504 TEST(EventDispatcherTest, SetExplictCapture) { |
| 505 TestServerWindowDelegate window_delegate; |
| 506 ServerWindow root(&window_delegate, WindowId(1, 2)); |
| 507 window_delegate.set_root_window(&root); |
| 508 root.SetVisible(true); |
| 509 |
| 510 ServerWindow child(&window_delegate, WindowId(1, 3)); |
| 511 root.Add(&child); |
| 512 child.SetVisible(true); |
| 513 |
| 514 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 515 child.SetBounds(gfx::Rect(10, 10, 20, 20)); |
| 516 |
| 517 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
| 518 EventDispatcher dispatcher(&event_dispatcher_delegate); |
| 519 dispatcher.set_root(&root); |
| 520 |
| 521 { |
| 522 // Send all pointer events to the child. |
| 523 dispatcher.SetCaptureWindow(&child); |
| 524 |
| 525 // The mouse press should go to the child even though its outside its |
| 526 // bounds. |
| 527 const ui::MouseEvent press_event( |
| 528 ui::ET_MOUSE_PRESSED, gfx::Point(5, 5), gfx::Point(5, 5), |
| 529 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 530 dispatcher.OnEvent( |
| 531 mojom::Event::From(static_cast<const ui::Event&>(press_event))); |
| 532 |
| 533 // Events should target child. |
| 534 ASSERT_EQ(&child, event_dispatcher_delegate.last_target()); |
| 535 } |
| 536 |
| 537 { |
| 538 // Releasing capture and sending the same event will go to the root. |
| 539 dispatcher.SetCaptureWindow(nullptr); |
| 540 const ui::MouseEvent press_event( |
| 541 ui::ET_MOUSE_PRESSED, gfx::Point(5, 5), gfx::Point(5, 5), |
| 542 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 543 dispatcher.OnEvent( |
| 544 mojom::Event::From(static_cast<const ui::Event&>(press_event))); |
| 545 |
| 546 // Events should target the root. |
| 547 ASSERT_EQ(&root, event_dispatcher_delegate.last_target()); |
| 548 } |
| 549 } |
| 550 |
| 551 // This test verifies that explicit capture overrides and resets implicit |
| 552 // capture. |
| 553 TEST(EventDispatcherTest, ExplicitCaptureOverridesImplicitCapture) { |
| 554 TestServerWindowDelegate window_delegate; |
| 555 ServerWindow root(&window_delegate, WindowId(1, 2)); |
| 556 window_delegate.set_root_window(&root); |
| 557 root.SetVisible(true); |
| 558 |
| 559 ServerWindow child(&window_delegate, WindowId(1, 3)); |
| 560 root.Add(&child); |
| 561 child.SetVisible(true); |
| 562 |
| 563 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 564 child.SetBounds(gfx::Rect(10, 10, 20, 20)); |
| 565 |
| 566 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
| 567 EventDispatcher dispatcher(&event_dispatcher_delegate); |
| 568 dispatcher.set_root(&root); |
| 569 |
| 570 // Run some implicit capture tests. |
| 571 MouseEventTest tests[] = { |
| 572 // Send a mouse down event over child with a left mouse button |
| 573 {ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(20, 25), |
| 574 gfx::Point(20, 25), base::TimeDelta(), |
| 575 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON), |
| 576 &child, gfx::Point(20, 25), gfx::Point(10, 15)}, |
| 577 // Capture should be activated. Let's send a mouse move outside the bounds |
| 578 // of the child and press the right mouse button too. |
| 579 {ui::MouseEvent(ui::ET_MOUSE_MOVED, gfx::Point(50, 50), |
| 580 gfx::Point(50, 50), base::TimeDelta(), |
| 581 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON, 0), |
| 582 &child, gfx::Point(50, 50), gfx::Point(40, 40)}, |
| 583 // Release the left mouse button and verify that the mouse up event goes |
| 584 // to the child. |
| 585 {ui::MouseEvent(ui::ET_MOUSE_RELEASED, gfx::Point(50, 50), |
| 586 gfx::Point(50, 50), base::TimeDelta(), |
| 587 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON, |
| 588 ui::EF_RIGHT_MOUSE_BUTTON), |
| 589 &child, gfx::Point(50, 50), gfx::Point(40, 40)}, |
| 590 // A mouse move at (50, 50) should still go to the child. |
| 591 {ui::MouseEvent(ui::ET_MOUSE_MOVED, gfx::Point(50, 50), |
| 592 gfx::Point(50, 50), base::TimeDelta(), |
| 593 ui::EF_LEFT_MOUSE_BUTTON, 0), |
| 594 &child, gfx::Point(50, 50), gfx::Point(40, 40)}, |
| 595 |
| 596 }; |
| 597 RunMouseEventTests(&dispatcher, &event_dispatcher_delegate, tests, |
| 598 arraysize(tests)); |
| 599 ASSERT_EQ(&child, event_dispatcher_delegate.last_target()); |
| 600 |
| 601 // Verify that no window has explicit capture and hence we did indeed do |
| 602 // implicit capture. |
| 603 ASSERT_EQ(nullptr, dispatcher.capture_window()); |
| 604 |
| 605 // Give the root window explicit capture and verify input evnets over the |
| 606 // child go to the root instead. |
| 607 dispatcher.SetCaptureWindow(&root); |
| 608 const ui::MouseEvent press_event( |
| 609 ui::ET_MOUSE_PRESSED, gfx::Point(15, 15), gfx::Point(15, 15), |
| 610 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 611 dispatcher.OnEvent( |
| 612 mojom::Event::From(static_cast<const ui::Event&>(press_event))); |
| 613 |
| 614 // Events should target the root. |
| 615 ASSERT_EQ(&root, event_dispatcher_delegate.last_target()); |
| 616 } |
| 617 |
503 } // namespace ws | 618 } // namespace ws |
504 } // namespace mus | 619 } // namespace mus |
OLD | NEW |