| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/views/mus/native_widget_mus.h" | 5 #include "ui/views/mus/native_widget_mus.h" |
| 6 | 6 |
| 7 #include "base/callback.h" |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 #include "components/mus/public/cpp/property_type_converters.h" | 9 #include "components/mus/public/cpp/property_type_converters.h" |
| 9 #include "components/mus/public/cpp/window.h" | 10 #include "components/mus/public/cpp/window.h" |
| 10 #include "components/mus/public/cpp/window_property.h" | 11 #include "components/mus/public/cpp/window_property.h" |
| 11 #include "components/mus/public/interfaces/window_manager.mojom.h" | 12 #include "components/mus/public/interfaces/window_manager.mojom.h" |
| 13 #include "components/mus/public/interfaces/window_tree.mojom.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "third_party/skia/include/core/SkColor.h" | 16 #include "third_party/skia/include/core/SkColor.h" |
| 15 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
| 18 #include "ui/events/event.h" |
| 16 #include "ui/gfx/geometry/rect.h" | 19 #include "ui/gfx/geometry/rect.h" |
| 17 #include "ui/gfx/image/image_skia.h" | 20 #include "ui/gfx/image/image_skia.h" |
| 18 #include "ui/gfx/skia_util.h" | 21 #include "ui/gfx/skia_util.h" |
| 19 #include "ui/views/test/focus_manager_test.h" | 22 #include "ui/views/test/focus_manager_test.h" |
| 20 #include "ui/views/test/views_test_base.h" | 23 #include "ui/views/test/views_test_base.h" |
| 21 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 22 #include "ui/views/widget/widget_delegate.h" | 25 #include "ui/views/widget/widget_delegate.h" |
| 23 #include "ui/views/widget/widget_observer.h" | 26 #include "ui/views/widget/widget_observer.h" |
| 24 | 27 |
| 28 using mus::mojom::EventResult; |
| 29 |
| 25 namespace views { | 30 namespace views { |
| 26 namespace { | 31 namespace { |
| 27 | 32 |
| 33 // A view that reports any mouse press as handled. |
| 34 class HandleMousePressView : public View { |
| 35 public: |
| 36 HandleMousePressView() {} |
| 37 ~HandleMousePressView() override {} |
| 38 |
| 39 // View: |
| 40 bool OnMousePressed(const ui::MouseEvent& event) override { return true; } |
| 41 |
| 42 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(HandleMousePressView); |
| 44 }; |
| 45 |
| 46 // A view that deletes a widget on mouse press. |
| 47 class DeleteWidgetView : public View { |
| 48 public: |
| 49 explicit DeleteWidgetView(std::unique_ptr<Widget>* widget_ptr) |
| 50 : widget_ptr_(widget_ptr) {} |
| 51 ~DeleteWidgetView() override {} |
| 52 |
| 53 // View: |
| 54 bool OnMousePressed(const ui::MouseEvent& event) override { |
| 55 widget_ptr_->reset(); |
| 56 return true; |
| 57 } |
| 58 |
| 59 private: |
| 60 std::unique_ptr<Widget>* widget_ptr_; |
| 61 DISALLOW_COPY_AND_ASSIGN(DeleteWidgetView); |
| 62 }; |
| 63 |
| 28 // Returns a small colored bitmap. | 64 // Returns a small colored bitmap. |
| 29 SkBitmap MakeBitmap(SkColor color) { | 65 SkBitmap MakeBitmap(SkColor color) { |
| 30 SkBitmap bitmap; | 66 SkBitmap bitmap; |
| 31 bitmap.allocN32Pixels(8, 8); | 67 bitmap.allocN32Pixels(8, 8); |
| 32 bitmap.eraseColor(color); | 68 bitmap.eraseColor(color); |
| 33 return bitmap; | 69 return bitmap; |
| 34 } | 70 } |
| 35 | 71 |
| 36 // An observer that tracks widget activation changes. | 72 // An observer that tracks widget activation changes. |
| 37 class WidgetActivationObserver : public WidgetObserver { | 73 class WidgetActivationObserver : public WidgetObserver { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 109 |
| 74 // views::WidgetDelegate: | 110 // views::WidgetDelegate: |
| 75 gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } | 111 gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } |
| 76 | 112 |
| 77 private: | 113 private: |
| 78 gfx::ImageSkia app_icon_; | 114 gfx::ImageSkia app_icon_; |
| 79 | 115 |
| 80 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); | 116 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); |
| 81 }; | 117 }; |
| 82 | 118 |
| 119 } // namespace |
| 120 |
| 83 class NativeWidgetMusTest : public ViewsTestBase { | 121 class NativeWidgetMusTest : public ViewsTestBase { |
| 84 public: | 122 public: |
| 85 NativeWidgetMusTest() {} | 123 NativeWidgetMusTest() {} |
| 86 ~NativeWidgetMusTest() override {} | 124 ~NativeWidgetMusTest() override {} |
| 87 | 125 |
| 88 // Creates a test widget. Takes ownership of |delegate|. | 126 // Creates a test widget. Takes ownership of |delegate|. |
| 89 std::unique_ptr<Widget> CreateWidget(TestWidgetDelegate* delegate) { | 127 std::unique_ptr<Widget> CreateWidget(TestWidgetDelegate* delegate) { |
| 90 std::unique_ptr<Widget> widget(new Widget()); | 128 std::unique_ptr<Widget> widget(new Widget()); |
| 91 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | 129 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 92 params.delegate = delegate; | 130 params.delegate = delegate; |
| 93 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 131 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 94 params.bounds = gfx::Rect(10, 20, 100, 200); | 132 params.bounds = gfx::Rect(10, 20, 100, 200); |
| 95 widget->Init(params); | 133 widget->Init(params); |
| 96 return widget; | 134 return widget; |
| 97 } | 135 } |
| 98 | 136 |
| 137 int ack_callback_count() { return ack_callback_count_; } |
| 138 |
| 139 void AckCallback(mus::mojom::EventResult result) { |
| 140 ack_callback_count_++; |
| 141 EXPECT_EQ(mus::mojom::EventResult::HANDLED, result); |
| 142 } |
| 143 |
| 144 // Returns a mouse pressed event inside the widget. Tests that place views |
| 145 // within the widget that respond to the event must be constructed within the |
| 146 // widget coordinate space such that they respond correctly. |
| 147 std::unique_ptr<ui::MouseEvent> CreateMouseEvent() { |
| 148 return base::WrapUnique(new ui::MouseEvent( |
| 149 ui::ET_MOUSE_PRESSED, gfx::Point(50, 50), gfx::Point(50, 50), |
| 150 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); |
| 151 } |
| 152 |
| 153 // Simulates an input event to the NativeWidget. |
| 154 void OnWindowInputEvent( |
| 155 NativeWidgetMus* native_widget, |
| 156 const ui::Event& event, |
| 157 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* |
| 158 ack_callback) { |
| 159 native_widget->OnWindowInputEvent(native_widget->window(), event, |
| 160 ack_callback); |
| 161 } |
| 162 |
| 99 private: | 163 private: |
| 164 int ack_callback_count_ = 0; |
| 165 |
| 100 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); | 166 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); |
| 101 }; | 167 }; |
| 102 | 168 |
| 103 // Tests communication of activation and focus between Widget and | 169 // Tests communication of activation and focus between Widget and |
| 104 // NativeWidgetMus. | 170 // NativeWidgetMus. |
| 105 TEST_F(NativeWidgetMusTest, OnActivationChanged) { | 171 TEST_F(NativeWidgetMusTest, OnActivationChanged) { |
| 106 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); | 172 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 107 widget->Show(); | 173 widget->Show(); |
| 108 | 174 |
| 109 // Track activation, focus and blur events. | 175 // Track activation, focus and blur events. |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 Widget widget; | 285 Widget widget; |
| 220 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | 286 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 221 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 287 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 222 params.name = "MyWidget"; | 288 params.name = "MyWidget"; |
| 223 widget.Init(params); | 289 widget.Init(params); |
| 224 mus::Window* window = | 290 mus::Window* window = |
| 225 static_cast<NativeWidgetMus*>(widget.native_widget_private())->window(); | 291 static_cast<NativeWidgetMus*>(widget.native_widget_private())->window(); |
| 226 EXPECT_EQ("MyWidget", window->GetName()); | 292 EXPECT_EQ("MyWidget", window->GetName()); |
| 227 } | 293 } |
| 228 | 294 |
| 229 } // namespace | 295 // Tests that an incoming UI event is acked with the handled status. |
| 296 TEST_F(NativeWidgetMusTest, EventAcked) { |
| 297 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 298 widget->Show(); |
| 299 |
| 300 View* content = new HandleMousePressView; |
| 301 content->SetBounds(10, 20, 90, 180); |
| 302 widget->GetContentsView()->AddChildView(content); |
| 303 |
| 304 // Dispatch an input event to the window and view. |
| 305 std::unique_ptr<ui::MouseEvent> event = CreateMouseEvent(); |
| 306 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback( |
| 307 new base::Callback<void(EventResult)>(base::Bind( |
| 308 &NativeWidgetMusTest::AckCallback, base::Unretained(this)))); |
| 309 OnWindowInputEvent( |
| 310 static_cast<NativeWidgetMus*>(widget->native_widget_private()), |
| 311 *event, |
| 312 &ack_callback); |
| 313 |
| 314 // The test took ownership of the callback and called it. |
| 315 EXPECT_FALSE(ack_callback); |
| 316 EXPECT_EQ(1, ack_callback_count()); |
| 317 } |
| 318 |
| 319 // Tests that a window that is deleted during event handling properly acks the |
| 320 // event. |
| 321 TEST_F(NativeWidgetMusTest, EventAckedWithWindowDestruction) { |
| 322 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 323 widget->Show(); |
| 324 |
| 325 View* content = new DeleteWidgetView(&widget); |
| 326 content->SetBounds(10, 20, 90, 180); |
| 327 widget->GetContentsView()->AddChildView(content); |
| 328 |
| 329 // Dispatch an input event to the window and view. |
| 330 std::unique_ptr<ui::MouseEvent> event = CreateMouseEvent(); |
| 331 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback( |
| 332 new base::Callback<void(EventResult)>(base::Bind( |
| 333 &NativeWidgetMusTest::AckCallback, base::Unretained(this)))); |
| 334 OnWindowInputEvent( |
| 335 static_cast<NativeWidgetMus*>(widget->native_widget_private()), |
| 336 *event, |
| 337 &ack_callback); |
| 338 |
| 339 // The widget was deleted. |
| 340 EXPECT_FALSE(widget.get()); |
| 341 |
| 342 // The test took ownership of the callback and called it. |
| 343 EXPECT_FALSE(ack_callback); |
| 344 EXPECT_EQ(1, ack_callback_count()); |
| 345 } |
| 346 |
| 230 } // namespace views | 347 } // namespace views |
| OLD | NEW |