| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/aura/window_event_dispatcher.h" | 5 #include "ui/aura/window_event_dispatcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "ui/aura/client/capture_client.h" | 19 #include "ui/aura/client/capture_client.h" |
| 20 #include "ui/aura/client/event_client.h" | 20 #include "ui/aura/client/event_client.h" |
| 21 #include "ui/aura/client/focus_client.h" | 21 #include "ui/aura/client/focus_client.h" |
| 22 #include "ui/aura/env.h" | 22 #include "ui/aura/env.h" |
| 23 #include "ui/aura/mus/window_tree_client.h" |
| 23 #include "ui/aura/test/aura_test_base.h" | 24 #include "ui/aura/test/aura_test_base.h" |
| 24 #include "ui/aura/test/env_test_helper.h" | 25 #include "ui/aura/test/env_test_helper.h" |
| 25 #include "ui/aura/test/test_cursor_client.h" | 26 #include "ui/aura/test/test_cursor_client.h" |
| 26 #include "ui/aura/test/test_screen.h" | 27 #include "ui/aura/test/test_screen.h" |
| 27 #include "ui/aura/test/test_window_delegate.h" | 28 #include "ui/aura/test/test_window_delegate.h" |
| 28 #include "ui/aura/test/test_windows.h" | 29 #include "ui/aura/test/test_windows.h" |
| 29 #include "ui/aura/window.h" | 30 #include "ui/aura/window.h" |
| 30 #include "ui/aura/window_tracker.h" | 31 #include "ui/aura/window_tracker.h" |
| 31 #include "ui/base/hit_test.h" | 32 #include "ui/base/hit_test.h" |
| 32 #include "ui/display/screen.h" | 33 #include "ui/display/screen.h" |
| (...skipping 2650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2683 INSTANTIATE_TEST_CASE_P(/* no prefix */, | 2684 INSTANTIATE_TEST_CASE_P(/* no prefix */, |
| 2684 WindowEventDispatcherTestWithMessageLoop, | 2685 WindowEventDispatcherTestWithMessageLoop, |
| 2685 ::testing::Values(test::BackendType::CLASSIC, | 2686 ::testing::Values(test::BackendType::CLASSIC, |
| 2686 test::BackendType::MUS)); | 2687 test::BackendType::MUS)); |
| 2687 | 2688 |
| 2688 INSTANTIATE_TEST_CASE_P(/* no prefix */, | 2689 INSTANTIATE_TEST_CASE_P(/* no prefix */, |
| 2689 WindowEventDispatcherTestInHighDPI, | 2690 WindowEventDispatcherTestInHighDPI, |
| 2690 ::testing::Values(test::BackendType::CLASSIC, | 2691 ::testing::Values(test::BackendType::CLASSIC, |
| 2691 test::BackendType::MUS)); | 2692 test::BackendType::MUS)); |
| 2692 | 2693 |
| 2694 using WindowEventDispatcherMusTest = test::AuraTestBaseMus; |
| 2695 |
| 2696 class LastEventLocationDelegate : public test::TestWindowDelegate { |
| 2697 public: |
| 2698 LastEventLocationDelegate() {} |
| 2699 ~LastEventLocationDelegate() override {} |
| 2700 |
| 2701 int mouse_event_count() const { return mouse_event_count_; } |
| 2702 const gfx::Point& last_mouse_location() const { return last_mouse_location_; } |
| 2703 |
| 2704 // TestWindowDelegate: |
| 2705 void OnMouseEvent(ui::MouseEvent* event) override { |
| 2706 ++mouse_event_count_; |
| 2707 last_mouse_location_ = event->root_location(); |
| 2708 EXPECT_EQ(last_mouse_location_, Env::GetInstance()->last_mouse_location()); |
| 2709 } |
| 2710 |
| 2711 private: |
| 2712 int mouse_event_count_ = 0; |
| 2713 gfx::Point last_mouse_location_; |
| 2714 |
| 2715 DISALLOW_COPY_AND_ASSIGN(LastEventLocationDelegate); |
| 2716 }; |
| 2717 |
| 2718 TEST_F(WindowEventDispatcherMusTest, LastEventLocation) { |
| 2719 LastEventLocationDelegate last_event_location_delegate; |
| 2720 std::unique_ptr<Window> window( |
| 2721 CreateTestWindowWithDelegate(&last_event_location_delegate, 123, |
| 2722 gfx::Rect(0, 0, 10, 20), root_window())); |
| 2723 |
| 2724 // Enable fetching mouse location from mouse. |
| 2725 test::EnvTestHelper().SetAlwaysUseLastMouseLocation(false); |
| 2726 EXPECT_EQ(gfx::Point(0, 0), |
| 2727 window_tree_client_impl()->GetCursorScreenPoint()); |
| 2728 EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location()); |
| 2729 |
| 2730 // Dispatch an event to |mouse_location|. While dispatching the event |
| 2731 // Env::last_mouse_location() should return |mouse_location|. |
| 2732 const gfx::Point mouse_location(1, 2); |
| 2733 ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, mouse_location, mouse_location, |
| 2734 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, |
| 2735 ui::EF_LEFT_MOUSE_BUTTON); |
| 2736 DispatchEventUsingWindowDispatcher(&mouse); |
| 2737 EXPECT_EQ(1, last_event_location_delegate.mouse_event_count()); |
| 2738 EXPECT_EQ(mouse_location, last_event_location_delegate.last_mouse_location()); |
| 2739 |
| 2740 // After dispatch the location should fallback to that of the |
| 2741 // WindowTreeClient, which defaults to 0,0. |
| 2742 EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location()); |
| 2743 } |
| 2744 |
| 2745 class NestedLocationDelegate : public test::TestWindowDelegate { |
| 2746 public: |
| 2747 NestedLocationDelegate() {} |
| 2748 ~NestedLocationDelegate() override {} |
| 2749 |
| 2750 int mouse_event_count() const { return mouse_event_count_; } |
| 2751 int nested_message_loop_count() const { return nested_message_loop_count_; } |
| 2752 const gfx::Point& last_mouse_location() const { return last_mouse_location_; } |
| 2753 |
| 2754 // TestWindowDelegate: |
| 2755 void OnMouseEvent(ui::MouseEvent* event) override { |
| 2756 ++mouse_event_count_; |
| 2757 last_mouse_location_ = event->root_location(); |
| 2758 EXPECT_EQ(last_mouse_location_, Env::GetInstance()->last_mouse_location()); |
| 2759 |
| 2760 // Start a RunLoop that in turn starts a RunLoop. We have to do this as the |
| 2761 // first RunLoop doesn't triggering nesting (the MessageLoop isn't running |
| 2762 // at this point). The second RunLoop (created in InInitialMessageLoop()) |
| 2763 // is considered the first nested loop. |
| 2764 base::RunLoop run_loop; |
| 2765 base::MessageLoop::current()->task_runner()->PostTask( |
| 2766 FROM_HERE, base::Bind(&NestedLocationDelegate::InInitialMessageLoop, |
| 2767 base::Unretained(this), &run_loop)); |
| 2768 run_loop.Run(); |
| 2769 } |
| 2770 |
| 2771 private: |
| 2772 void InInitialMessageLoop(base::RunLoop* initial_run_loop) { |
| 2773 // See comments in OnMouseEvent() for details on which this creates another |
| 2774 // RunLoop. |
| 2775 base::MessageLoop::ScopedNestableTaskAllower allow_nestable_tasks( |
| 2776 base::MessageLoop::current()); |
| 2777 base::RunLoop run_loop; |
| 2778 base::MessageLoop::current()->task_runner()->PostTask( |
| 2779 FROM_HERE, base::Bind(&NestedLocationDelegate::InRunMessageLoop, |
| 2780 base::Unretained(this), &run_loop)); |
| 2781 run_loop.Run(); |
| 2782 initial_run_loop->Quit(); |
| 2783 } |
| 2784 |
| 2785 void InRunMessageLoop(base::RunLoop* run_loop) { |
| 2786 ++nested_message_loop_count_; |
| 2787 // Nested message loops trigger falling back to using the location from |
| 2788 // WindowTreeClient, which is 0,0. |
| 2789 EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location()); |
| 2790 run_loop->Quit(); |
| 2791 } |
| 2792 |
| 2793 int mouse_event_count_ = 0; |
| 2794 // Incremented when the deepest message loop is encountered. |
| 2795 int nested_message_loop_count_ = 0; |
| 2796 gfx::Point last_mouse_location_; |
| 2797 |
| 2798 DISALLOW_COPY_AND_ASSIGN(NestedLocationDelegate); |
| 2799 }; |
| 2800 |
| 2801 TEST_F(WindowEventDispatcherMusTest, EventDispatchTriggersNestedMessageLoop) { |
| 2802 NestedLocationDelegate last_event_location_delegate; |
| 2803 std::unique_ptr<Window> window( |
| 2804 CreateTestWindowWithDelegate(&last_event_location_delegate, 123, |
| 2805 gfx::Rect(0, 0, 10, 20), root_window())); |
| 2806 |
| 2807 // Enable fetching mouse location from mouse. |
| 2808 test::EnvTestHelper().SetAlwaysUseLastMouseLocation(false); |
| 2809 EXPECT_EQ(gfx::Point(0, 0), |
| 2810 window_tree_client_impl()->GetCursorScreenPoint()); |
| 2811 EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location()); |
| 2812 |
| 2813 // Dispatch an event to |mouse_location|. While dispatching the event |
| 2814 // Env::last_mouse_location() should return |mouse_location|. |
| 2815 const gfx::Point mouse_location(1, 2); |
| 2816 ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, mouse_location, mouse_location, |
| 2817 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, |
| 2818 ui::EF_LEFT_MOUSE_BUTTON); |
| 2819 DispatchEventUsingWindowDispatcher(&mouse); |
| 2820 EXPECT_EQ(1, last_event_location_delegate.mouse_event_count()); |
| 2821 EXPECT_EQ(1, last_event_location_delegate.nested_message_loop_count()); |
| 2822 EXPECT_EQ(mouse_location, last_event_location_delegate.last_mouse_location()); |
| 2823 |
| 2824 // After dispatch the location should fallback to that of the |
| 2825 // WindowTreeClient, which defaults to 0,0. |
| 2826 EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location()); |
| 2827 } |
| 2828 |
| 2693 } // namespace aura | 2829 } // namespace aura |
| OLD | NEW |