| 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/controls/native/native_view_host.h" | 22 #include "ui/views/controls/native/native_view_host.h" |
| 20 #include "ui/views/test/focus_manager_test.h" | 23 #include "ui/views/test/focus_manager_test.h" |
| 21 #include "ui/views/test/views_test_base.h" | 24 #include "ui/views/test/views_test_base.h" |
| 22 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
| 23 #include "ui/views/widget/widget_delegate.h" | 26 #include "ui/views/widget/widget_delegate.h" |
| 24 #include "ui/views/widget/widget_observer.h" | 27 #include "ui/views/widget/widget_observer.h" |
| 25 #include "ui/wm/public/activation_client.h" | 28 #include "ui/wm/public/activation_client.h" |
| 26 | 29 |
| 30 using mus::mojom::EventResult; |
| 31 |
| 27 namespace views { | 32 namespace views { |
| 28 namespace { | 33 namespace { |
| 29 | 34 |
| 35 // A view that reports any mouse press as handled. |
| 36 class HandleMousePressView : public View { |
| 37 public: |
| 38 HandleMousePressView() {} |
| 39 ~HandleMousePressView() override {} |
| 40 |
| 41 // View: |
| 42 bool OnMousePressed(const ui::MouseEvent& event) override { return true; } |
| 43 |
| 44 private: |
| 45 DISALLOW_COPY_AND_ASSIGN(HandleMousePressView); |
| 46 }; |
| 47 |
| 48 // A view that deletes a widget on mouse press. |
| 49 class DeleteWidgetView : public View { |
| 50 public: |
| 51 explicit DeleteWidgetView(std::unique_ptr<Widget>* widget_ptr) |
| 52 : widget_ptr_(widget_ptr) {} |
| 53 ~DeleteWidgetView() override {} |
| 54 |
| 55 // View: |
| 56 bool OnMousePressed(const ui::MouseEvent& event) override { |
| 57 widget_ptr_->reset(); |
| 58 return true; |
| 59 } |
| 60 |
| 61 private: |
| 62 std::unique_ptr<Widget>* widget_ptr_; |
| 63 DISALLOW_COPY_AND_ASSIGN(DeleteWidgetView); |
| 64 }; |
| 65 |
| 30 // Returns a small colored bitmap. | 66 // Returns a small colored bitmap. |
| 31 SkBitmap MakeBitmap(SkColor color) { | 67 SkBitmap MakeBitmap(SkColor color) { |
| 32 SkBitmap bitmap; | 68 SkBitmap bitmap; |
| 33 bitmap.allocN32Pixels(8, 8); | 69 bitmap.allocN32Pixels(8, 8); |
| 34 bitmap.eraseColor(color); | 70 bitmap.eraseColor(color); |
| 35 return bitmap; | 71 return bitmap; |
| 36 } | 72 } |
| 37 | 73 |
| 38 // An observer that tracks widget activation changes. | 74 // An observer that tracks widget activation changes. |
| 39 class WidgetActivationObserver : public WidgetObserver { | 75 class WidgetActivationObserver : public WidgetObserver { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 111 |
| 76 // views::WidgetDelegate: | 112 // views::WidgetDelegate: |
| 77 gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } | 113 gfx::ImageSkia GetWindowAppIcon() override { return app_icon_; } |
| 78 | 114 |
| 79 private: | 115 private: |
| 80 gfx::ImageSkia app_icon_; | 116 gfx::ImageSkia app_icon_; |
| 81 | 117 |
| 82 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); | 118 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); |
| 83 }; | 119 }; |
| 84 | 120 |
| 121 } // namespace |
| 122 |
| 85 class NativeWidgetMusTest : public ViewsTestBase { | 123 class NativeWidgetMusTest : public ViewsTestBase { |
| 86 public: | 124 public: |
| 87 NativeWidgetMusTest() {} | 125 NativeWidgetMusTest() {} |
| 88 ~NativeWidgetMusTest() override {} | 126 ~NativeWidgetMusTest() override {} |
| 89 | 127 |
| 90 // Creates a test widget. Takes ownership of |delegate|. | 128 // Creates a test widget. Takes ownership of |delegate|. |
| 91 std::unique_ptr<Widget> CreateWidget(TestWidgetDelegate* delegate) { | 129 std::unique_ptr<Widget> CreateWidget(TestWidgetDelegate* delegate) { |
| 92 std::unique_ptr<Widget> widget(new Widget()); | 130 std::unique_ptr<Widget> widget(new Widget()); |
| 93 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | 131 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 94 params.delegate = delegate; | 132 params.delegate = delegate; |
| 95 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 133 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 96 params.bounds = gfx::Rect(10, 20, 100, 200); | 134 params.bounds = gfx::Rect(10, 20, 100, 200); |
| 97 widget->Init(params); | 135 widget->Init(params); |
| 98 return widget; | 136 return widget; |
| 99 } | 137 } |
| 100 | 138 |
| 139 int ack_callback_count() { return ack_callback_count_; } |
| 140 |
| 141 void AckCallback(mus::mojom::EventResult result) { |
| 142 ack_callback_count_++; |
| 143 EXPECT_EQ(mus::mojom::EventResult::HANDLED, result); |
| 144 } |
| 145 |
| 146 // Returns a mouse pressed event inside the widget. Tests that place views |
| 147 // within the widget that respond to the event must be constructed within the |
| 148 // widget coordinate space such that they respond correctly. |
| 149 std::unique_ptr<ui::MouseEvent> CreateMouseEvent() { |
| 150 return base::WrapUnique(new ui::MouseEvent( |
| 151 ui::ET_MOUSE_PRESSED, gfx::Point(50, 50), gfx::Point(50, 50), |
| 152 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); |
| 153 } |
| 154 |
| 155 // Simulates an input event to the NativeWidget. |
| 156 void OnWindowInputEvent( |
| 157 NativeWidgetMus* native_widget, |
| 158 const ui::Event& event, |
| 159 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* |
| 160 ack_callback) { |
| 161 native_widget->OnWindowInputEvent(native_widget->window(), event, |
| 162 ack_callback); |
| 163 } |
| 164 |
| 101 private: | 165 private: |
| 166 int ack_callback_count_ = 0; |
| 167 |
| 102 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); | 168 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); |
| 103 }; | 169 }; |
| 104 | 170 |
| 105 // Tests communication of activation and focus between Widget and | 171 // Tests communication of activation and focus between Widget and |
| 106 // NativeWidgetMus. | 172 // NativeWidgetMus. |
| 107 TEST_F(NativeWidgetMusTest, OnActivationChanged) { | 173 TEST_F(NativeWidgetMusTest, OnActivationChanged) { |
| 108 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); | 174 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 109 widget->Show(); | 175 widget->Show(); |
| 110 | 176 |
| 111 // Track activation, focus and blur events. | 177 // Track activation, focus and blur events. |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 // the Widget's window instead. | 332 // the Widget's window instead. |
| 267 focusable->RequestFocus(); | 333 focusable->RequestFocus(); |
| 268 EXPECT_FALSE(window->HasFocus()); | 334 EXPECT_FALSE(window->HasFocus()); |
| 269 EXPECT_TRUE(widget.GetNativeView()->HasFocus()); | 335 EXPECT_TRUE(widget.GetNativeView()->HasFocus()); |
| 270 active_window = | 336 active_window = |
| 271 aura::client::GetActivationClient(window.get()->GetRootWindow()) | 337 aura::client::GetActivationClient(window.get()->GetRootWindow()) |
| 272 ->GetActiveWindow(); | 338 ->GetActiveWindow(); |
| 273 EXPECT_EQ(widget.GetNativeView(), active_window); | 339 EXPECT_EQ(widget.GetNativeView(), active_window); |
| 274 } | 340 } |
| 275 | 341 |
| 276 } // namespace | 342 // Tests that an incoming UI event is acked with the handled status. |
| 343 TEST_F(NativeWidgetMusTest, EventAcked) { |
| 344 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 345 widget->Show(); |
| 346 |
| 347 View* content = new HandleMousePressView; |
| 348 content->SetBounds(10, 20, 90, 180); |
| 349 widget->GetContentsView()->AddChildView(content); |
| 350 |
| 351 // Dispatch an input event to the window and view. |
| 352 std::unique_ptr<ui::MouseEvent> event = CreateMouseEvent(); |
| 353 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback( |
| 354 new base::Callback<void(EventResult)>(base::Bind( |
| 355 &NativeWidgetMusTest::AckCallback, base::Unretained(this)))); |
| 356 OnWindowInputEvent( |
| 357 static_cast<NativeWidgetMus*>(widget->native_widget_private()), |
| 358 *event, |
| 359 &ack_callback); |
| 360 |
| 361 // The test took ownership of the callback and called it. |
| 362 EXPECT_FALSE(ack_callback); |
| 363 EXPECT_EQ(1, ack_callback_count()); |
| 364 } |
| 365 |
| 366 // Tests that a window that is deleted during event handling properly acks the |
| 367 // event. |
| 368 TEST_F(NativeWidgetMusTest, EventAckedWithWindowDestruction) { |
| 369 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 370 widget->Show(); |
| 371 |
| 372 View* content = new DeleteWidgetView(&widget); |
| 373 content->SetBounds(10, 20, 90, 180); |
| 374 widget->GetContentsView()->AddChildView(content); |
| 375 |
| 376 // Dispatch an input event to the window and view. |
| 377 std::unique_ptr<ui::MouseEvent> event = CreateMouseEvent(); |
| 378 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback( |
| 379 new base::Callback<void(EventResult)>(base::Bind( |
| 380 &NativeWidgetMusTest::AckCallback, base::Unretained(this)))); |
| 381 OnWindowInputEvent( |
| 382 static_cast<NativeWidgetMus*>(widget->native_widget_private()), |
| 383 *event, |
| 384 &ack_callback); |
| 385 |
| 386 // The widget was deleted. |
| 387 EXPECT_FALSE(widget.get()); |
| 388 |
| 389 // The test took ownership of the callback and called it. |
| 390 EXPECT_FALSE(ack_callback); |
| 391 EXPECT_EQ(1, ack_callback_count()); |
| 392 } |
| 393 |
| 277 } // namespace views | 394 } // namespace views |
| OLD | NEW |