OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/mus/platform_window_mus.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/events/event.h" |
| 15 #include "ui/gfx/geometry/rect.h" |
| 16 #include "ui/views/mus/native_widget_mus.h" |
| 17 #include "ui/views/mus/window_tree_host_mus.h" |
| 18 #include "ui/views/test/views_test_base.h" |
| 19 #include "ui/views/widget/widget.h" |
| 20 #include "ui/views/widget/widget_delegate.h" |
| 21 |
| 22 using mus::mojom::EventResult; |
| 23 |
| 24 namespace views { |
| 25 namespace { |
| 26 |
| 27 // A view that reports any mouse press as handled. |
| 28 class HandleMousePressView : public View { |
| 29 public: |
| 30 HandleMousePressView() {} |
| 31 ~HandleMousePressView() override {} |
| 32 |
| 33 // View: |
| 34 bool OnMousePressed(const ui::MouseEvent& event) override { return true; } |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(HandleMousePressView); |
| 38 }; |
| 39 |
| 40 // A view that deletes a widget on mouse press. |
| 41 class DeleteWidgetView : public View { |
| 42 public: |
| 43 explicit DeleteWidgetView(std::unique_ptr<Widget>* widget_ptr) |
| 44 : widget_ptr_(widget_ptr) {} |
| 45 ~DeleteWidgetView() override {} |
| 46 |
| 47 // View: |
| 48 bool OnMousePressed(const ui::MouseEvent& event) override { |
| 49 widget_ptr_->reset(); |
| 50 return true; |
| 51 } |
| 52 |
| 53 private: |
| 54 std::unique_ptr<Widget>* widget_ptr_; |
| 55 DISALLOW_COPY_AND_ASSIGN(DeleteWidgetView); |
| 56 }; |
| 57 |
| 58 } // namespace |
| 59 |
| 60 class PlatformWindowMusTest : public ViewsTestBase { |
| 61 public: |
| 62 PlatformWindowMusTest() {} |
| 63 ~PlatformWindowMusTest() override {} |
| 64 |
| 65 int ack_callback_count() { return ack_callback_count_; } |
| 66 |
| 67 void AckCallback(mus::mojom::EventResult result) { |
| 68 ack_callback_count_++; |
| 69 EXPECT_EQ(mus::mojom::EventResult::HANDLED, result); |
| 70 } |
| 71 |
| 72 // testing::Test: |
| 73 void SetUp() override { |
| 74 ViewsTestBase::SetUp(); |
| 75 widget_.reset(new Widget); |
| 76 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 77 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 78 params.bounds = gfx::Rect(0, 0, 100, 100); |
| 79 widget_->Init(params); |
| 80 widget_->Show(); |
| 81 native_widget_ = |
| 82 static_cast<NativeWidgetMus*>(widget_->native_widget_private()); |
| 83 platform_window_ = native_widget_->window_tree_host()->platform_window(); |
| 84 ASSERT_TRUE(platform_window_); |
| 85 } |
| 86 |
| 87 // Returns a mouse pressed event in the middle of the widget. |
| 88 std::unique_ptr<ui::MouseEvent> CreateMouseEvent() { |
| 89 return base::WrapUnique(new ui::MouseEvent( |
| 90 ui::ET_MOUSE_PRESSED, gfx::Point(50, 50), gfx::Point(50, 50), |
| 91 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); |
| 92 } |
| 93 |
| 94 // Simulates an input event to the PlatformWindow. |
| 95 void OnWindowInputEvent( |
| 96 const ui::Event& event, |
| 97 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* |
| 98 ack_callback) { |
| 99 platform_window_->OnWindowInputEvent(native_widget_->window(), event, |
| 100 ack_callback); |
| 101 } |
| 102 |
| 103 protected: |
| 104 std::unique_ptr<Widget> widget_; |
| 105 |
| 106 private: |
| 107 NativeWidgetMus* native_widget_ = nullptr; |
| 108 PlatformWindowMus* platform_window_ = nullptr; |
| 109 int ack_callback_count_ = 0; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(PlatformWindowMusTest); |
| 112 }; |
| 113 |
| 114 // Tests that an incoming UI event is acked with the handled status. |
| 115 TEST_F(PlatformWindowMusTest, EventAcked) { |
| 116 View* content = new HandleMousePressView; |
| 117 content->SetBounds(0, 0, 100, 100); |
| 118 widget_->GetContentsView()->AddChildView(content); |
| 119 |
| 120 // Dispatch an input event to the window and view. |
| 121 std::unique_ptr<ui::MouseEvent> event = CreateMouseEvent(); |
| 122 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback( |
| 123 new base::Callback<void(EventResult)>(base::Bind( |
| 124 &PlatformWindowMusTest::AckCallback, base::Unretained(this)))); |
| 125 OnWindowInputEvent(*event, &ack_callback); |
| 126 |
| 127 // The platform window took ownership of the callback and called it. |
| 128 EXPECT_FALSE(ack_callback); |
| 129 EXPECT_EQ(1, ack_callback_count()); |
| 130 } |
| 131 |
| 132 // Tests that a window that is deleted during event handling properly acks the |
| 133 // event. |
| 134 TEST_F(PlatformWindowMusTest, EventAckedWithWindowDestruction) { |
| 135 View* content = new DeleteWidgetView(&widget_); |
| 136 content->SetBounds(0, 0, 100, 100); |
| 137 widget_->GetContentsView()->AddChildView(content); |
| 138 |
| 139 // Dispatch an input event to the window and view. |
| 140 std::unique_ptr<ui::MouseEvent> event = CreateMouseEvent(); |
| 141 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback( |
| 142 new base::Callback<void(EventResult)>(base::Bind( |
| 143 &PlatformWindowMusTest::AckCallback, base::Unretained(this)))); |
| 144 OnWindowInputEvent(*event, &ack_callback); |
| 145 |
| 146 // The widget was deleted. |
| 147 EXPECT_FALSE(widget_); |
| 148 |
| 149 // The platform window took ownership of the callback and called it. |
| 150 EXPECT_FALSE(ack_callback); |
| 151 EXPECT_EQ(1, ack_callback_count()); |
| 152 } |
| 153 |
| 154 } // namespace views |
OLD | NEW |