| Index: ui/aura/window_event_dispatcher_unittest.cc
|
| diff --git a/ui/aura/window_event_dispatcher_unittest.cc b/ui/aura/window_event_dispatcher_unittest.cc
|
| index ebec238f29dfcb9e996ae909f55013bc1288c2a0..ca48fb2d792765baa17ffcf7eea237184fa72651 100644
|
| --- a/ui/aura/window_event_dispatcher_unittest.cc
|
| +++ b/ui/aura/window_event_dispatcher_unittest.cc
|
| @@ -20,6 +20,7 @@
|
| #include "ui/aura/client/event_client.h"
|
| #include "ui/aura/client/focus_client.h"
|
| #include "ui/aura/env.h"
|
| +#include "ui/aura/mus/window_tree_client.h"
|
| #include "ui/aura/test/aura_test_base.h"
|
| #include "ui/aura/test/env_test_helper.h"
|
| #include "ui/aura/test/test_cursor_client.h"
|
| @@ -2690,4 +2691,139 @@ INSTANTIATE_TEST_CASE_P(/* no prefix */,
|
| ::testing::Values(test::BackendType::CLASSIC,
|
| test::BackendType::MUS));
|
|
|
| +using WindowEventDispatcherMusTest = test::AuraTestBaseMus;
|
| +
|
| +class LastEventLocationDelegate : public test::TestWindowDelegate {
|
| + public:
|
| + LastEventLocationDelegate() {}
|
| + ~LastEventLocationDelegate() override {}
|
| +
|
| + int mouse_event_count() const { return mouse_event_count_; }
|
| + const gfx::Point& last_mouse_location() const { return last_mouse_location_; }
|
| +
|
| + // TestWindowDelegate:
|
| + void OnMouseEvent(ui::MouseEvent* event) override {
|
| + ++mouse_event_count_;
|
| + last_mouse_location_ = event->root_location();
|
| + EXPECT_EQ(last_mouse_location_, Env::GetInstance()->last_mouse_location());
|
| + }
|
| +
|
| + private:
|
| + int mouse_event_count_ = 0;
|
| + gfx::Point last_mouse_location_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(LastEventLocationDelegate);
|
| +};
|
| +
|
| +TEST_F(WindowEventDispatcherMusTest, LastEventLocation) {
|
| + LastEventLocationDelegate last_event_location_delegate;
|
| + std::unique_ptr<Window> window(
|
| + CreateTestWindowWithDelegate(&last_event_location_delegate, 123,
|
| + gfx::Rect(0, 0, 10, 20), root_window()));
|
| +
|
| + // Enable fetching mouse location from mouse.
|
| + test::EnvTestHelper().SetAlwaysUseLastMouseLocation(false);
|
| + EXPECT_EQ(gfx::Point(0, 0),
|
| + window_tree_client_impl()->GetCursorScreenPoint());
|
| + EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location());
|
| +
|
| + // Dispatch an event to |mouse_location|. While dispatching the event
|
| + // Env::last_mouse_location() should return |mouse_location|.
|
| + const gfx::Point mouse_location(1, 2);
|
| + ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, mouse_location, mouse_location,
|
| + ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
|
| + ui::EF_LEFT_MOUSE_BUTTON);
|
| + DispatchEventUsingWindowDispatcher(&mouse);
|
| + EXPECT_EQ(1, last_event_location_delegate.mouse_event_count());
|
| + EXPECT_EQ(mouse_location, last_event_location_delegate.last_mouse_location());
|
| +
|
| + // After dispatch the location should fallback to that of the
|
| + // WindowTreeClient, which defaults to 0,0.
|
| + EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location());
|
| +}
|
| +
|
| +class NestedLocationDelegate : public test::TestWindowDelegate {
|
| + public:
|
| + NestedLocationDelegate() {}
|
| + ~NestedLocationDelegate() override {}
|
| +
|
| + int mouse_event_count() const { return mouse_event_count_; }
|
| + int nested_message_loop_count() const { return nested_message_loop_count_; }
|
| + const gfx::Point& last_mouse_location() const { return last_mouse_location_; }
|
| +
|
| + // TestWindowDelegate:
|
| + void OnMouseEvent(ui::MouseEvent* event) override {
|
| + ++mouse_event_count_;
|
| + last_mouse_location_ = event->root_location();
|
| + EXPECT_EQ(last_mouse_location_, Env::GetInstance()->last_mouse_location());
|
| +
|
| + // Start a RunLoop that in turn starts a RunLoop. We have to do this as the
|
| + // first RunLoop doesn't triggering nesting (the MessageLoop isn't running
|
| + // at this point). The second RunLoop (created in InInitialMessageLoop())
|
| + // is considered the first nested loop.
|
| + base::RunLoop run_loop;
|
| + base::MessageLoop::current()->task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&NestedLocationDelegate::InInitialMessageLoop,
|
| + base::Unretained(this), &run_loop));
|
| + run_loop.Run();
|
| + }
|
| +
|
| + private:
|
| + void InInitialMessageLoop(base::RunLoop* initial_run_loop) {
|
| + // See comments in OnMouseEvent() for details on which this creates another
|
| + // RunLoop.
|
| + base::MessageLoop::ScopedNestableTaskAllower allow_nestable_tasks(
|
| + base::MessageLoop::current());
|
| + base::RunLoop run_loop;
|
| + base::MessageLoop::current()->task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&NestedLocationDelegate::InRunMessageLoop,
|
| + base::Unretained(this), &run_loop));
|
| + run_loop.Run();
|
| + initial_run_loop->Quit();
|
| + }
|
| +
|
| + void InRunMessageLoop(base::RunLoop* run_loop) {
|
| + ++nested_message_loop_count_;
|
| + // Nested message loops trigger falling back to using the location from
|
| + // WindowTreeClient, which is 0,0.
|
| + EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location());
|
| + run_loop->Quit();
|
| + }
|
| +
|
| + int mouse_event_count_ = 0;
|
| + // Incremented when the deepest message loop is encountered.
|
| + int nested_message_loop_count_ = 0;
|
| + gfx::Point last_mouse_location_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(NestedLocationDelegate);
|
| +};
|
| +
|
| +TEST_F(WindowEventDispatcherMusTest, EventDispatchTriggersNestedMessageLoop) {
|
| + NestedLocationDelegate last_event_location_delegate;
|
| + std::unique_ptr<Window> window(
|
| + CreateTestWindowWithDelegate(&last_event_location_delegate, 123,
|
| + gfx::Rect(0, 0, 10, 20), root_window()));
|
| +
|
| + // Enable fetching mouse location from mouse.
|
| + test::EnvTestHelper().SetAlwaysUseLastMouseLocation(false);
|
| + EXPECT_EQ(gfx::Point(0, 0),
|
| + window_tree_client_impl()->GetCursorScreenPoint());
|
| + EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location());
|
| +
|
| + // Dispatch an event to |mouse_location|. While dispatching the event
|
| + // Env::last_mouse_location() should return |mouse_location|.
|
| + const gfx::Point mouse_location(1, 2);
|
| + ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, mouse_location, mouse_location,
|
| + ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
|
| + ui::EF_LEFT_MOUSE_BUTTON);
|
| + DispatchEventUsingWindowDispatcher(&mouse);
|
| + EXPECT_EQ(1, last_event_location_delegate.mouse_event_count());
|
| + EXPECT_EQ(1, last_event_location_delegate.nested_message_loop_count());
|
| + EXPECT_EQ(mouse_location, last_event_location_delegate.last_mouse_location());
|
| +
|
| + // After dispatch the location should fallback to that of the
|
| + // WindowTreeClient, which defaults to 0,0.
|
| + EXPECT_EQ(gfx::Point(0, 0), Env::GetInstance()->last_mouse_location());
|
| +}
|
| +
|
| } // namespace aura
|
|
|