| 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/callback.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "components/mus/public/cpp/property_type_converters.h" | 9 #include "components/mus/public/cpp/property_type_converters.h" |
| 10 #include "components/mus/public/cpp/tests/window_tree_client_impl_private.h" | 10 #include "components/mus/public/cpp/tests/window_tree_client_impl_private.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 namespace views { | 36 namespace views { |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 // A view that reports any mouse press as handled. | 39 // A view that reports any mouse press as handled. |
| 40 class HandleMousePressView : public View { | 40 class HandleMousePressView : public View { |
| 41 public: | 41 public: |
| 42 HandleMousePressView() {} | 42 HandleMousePressView() {} |
| 43 ~HandleMousePressView() override {} | 43 ~HandleMousePressView() override {} |
| 44 | 44 |
| 45 // View: | 45 // View: |
| 46 bool OnMousePressed(const ui::MouseEvent& event) override { return true; } | 46 bool OnMousePressed(const ui::MouseEvent& event) override { |
| 47 latest_event_location_ = event.location(); |
| 48 return true; |
| 49 } |
| 50 |
| 51 gfx::Point latest_event_location() { return latest_event_location_; } |
| 47 | 52 |
| 48 private: | 53 private: |
| 54 gfx::Point latest_event_location_; |
| 55 |
| 49 DISALLOW_COPY_AND_ASSIGN(HandleMousePressView); | 56 DISALLOW_COPY_AND_ASSIGN(HandleMousePressView); |
| 50 }; | 57 }; |
| 51 | 58 |
| 52 // A view that deletes a widget on mouse press. | 59 // A view that deletes a widget on mouse press. |
| 53 class DeleteWidgetView : public View { | 60 class DeleteWidgetView : public View { |
| 54 public: | 61 public: |
| 55 explicit DeleteWidgetView(std::unique_ptr<Widget>* widget_ptr) | 62 explicit DeleteWidgetView(std::unique_ptr<Widget>* widget_ptr) |
| 56 : widget_ptr_(widget_ptr) {} | 63 : widget_ptr_(widget_ptr) {} |
| 57 ~DeleteWidgetView() override {} | 64 ~DeleteWidgetView() override {} |
| 58 | 65 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 void AckCallback(mus::mojom::EventResult result) { | 171 void AckCallback(mus::mojom::EventResult result) { |
| 165 ack_callback_count_++; | 172 ack_callback_count_++; |
| 166 EXPECT_EQ(mus::mojom::EventResult::HANDLED, result); | 173 EXPECT_EQ(mus::mojom::EventResult::HANDLED, result); |
| 167 } | 174 } |
| 168 | 175 |
| 169 // Returns a mouse pressed event inside the widget. Tests that place views | 176 // Returns a mouse pressed event inside the widget. Tests that place views |
| 170 // within the widget that respond to the event must be constructed within the | 177 // within the widget that respond to the event must be constructed within the |
| 171 // widget coordinate space such that they respond correctly. | 178 // widget coordinate space such that they respond correctly. |
| 172 std::unique_ptr<ui::MouseEvent> CreateMouseEvent() { | 179 std::unique_ptr<ui::MouseEvent> CreateMouseEvent() { |
| 173 return base::WrapUnique(new ui::MouseEvent( | 180 return base::WrapUnique(new ui::MouseEvent( |
| 174 ui::ET_MOUSE_PRESSED, gfx::Point(50, 50), gfx::Point(50, 50), | 181 ui::ET_MOUSE_PRESSED, mouse_location(), mouse_location(), |
| 175 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); | 182 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); |
| 176 } | 183 } |
| 177 | 184 |
| 178 // Simulates an input event to the NativeWidget. | 185 // Simulates an input event to the NativeWidget. |
| 179 void OnWindowInputEvent( | 186 void OnWindowInputEvent( |
| 180 NativeWidgetMus* native_widget, | 187 NativeWidgetMus* native_widget, |
| 181 const ui::Event& event, | 188 const ui::Event& event, |
| 182 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* | 189 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* |
| 183 ack_callback) { | 190 ack_callback) { |
| 184 native_widget->OnWindowInputEvent(native_widget->window(), event, | 191 native_widget->OnWindowInputEvent(native_widget->window(), event, |
| 185 ack_callback); | 192 ack_callback); |
| 186 } | 193 } |
| 187 | 194 |
| 188 protected: | 195 protected: |
| 189 gfx::Rect initial_bounds() { return gfx::Rect(10, 20, 100, 200); } | 196 gfx::Rect initial_bounds() { return gfx::Rect(10, 20, 100, 200); } |
| 190 | 197 |
| 198 gfx::Point mouse_location() { |
| 199 return gfx::Point(50, 50); |
| 200 } |
| 201 |
| 191 private: | 202 private: |
| 192 int ack_callback_count_ = 0; | 203 int ack_callback_count_ = 0; |
| 193 | 204 |
| 194 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); | 205 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); |
| 195 }; | 206 }; |
| 196 | 207 |
| 197 // Tests communication of activation and focus between Widget and | 208 // Tests communication of activation and focus between Widget and |
| 198 // NativeWidgetMus. | 209 // NativeWidgetMus. |
| 199 TEST_F(NativeWidgetMusTest, OnActivationChanged) { | 210 TEST_F(NativeWidgetMusTest, OnActivationChanged) { |
| 200 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); | 211 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 content->AddPreTargetHandler(&handler); | 425 content->AddPreTargetHandler(&handler); |
| 415 | 426 |
| 416 std::unique_ptr<ui::MouseEvent> mouse = CreateMouseEvent(); | 427 std::unique_ptr<ui::MouseEvent> mouse = CreateMouseEvent(); |
| 417 NativeWidgetMus* native_widget = | 428 NativeWidgetMus* native_widget = |
| 418 static_cast<NativeWidgetMus*>(widget->native_widget_private()); | 429 static_cast<NativeWidgetMus*>(widget->native_widget_private()); |
| 419 mus::WindowTreeClientImplPrivate test_api(native_widget->window()); | 430 mus::WindowTreeClientImplPrivate test_api(native_widget->window()); |
| 420 test_api.CallOnWindowInputEvent(native_widget->window(), *mouse); | 431 test_api.CallOnWindowInputEvent(native_widget->window(), *mouse); |
| 421 EXPECT_EQ(1, handler.num_mouse_events()); | 432 EXPECT_EQ(1, handler.num_mouse_events()); |
| 422 } | 433 } |
| 423 | 434 |
| 435 TEST_F(NativeWidgetMusTest, WidgetViewMouseEventAgreement) { |
| 436 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 437 widget->Show(); |
| 438 |
| 439 HandleMousePressView* content = new HandleMousePressView; |
| 440 int content_x = 10; |
| 441 int content_y = 20; |
| 442 content->SetBounds(content_x, content_y, 90, 180); |
| 443 widget->GetContentsView()->AddChildView(content); |
| 444 |
| 445 std::unique_ptr<ui::MouseEvent> mouse = CreateMouseEvent(); |
| 446 NativeWidgetMus* native_widget = |
| 447 static_cast<NativeWidgetMus*>(widget->native_widget_private()); |
| 448 mus::WindowTreeClientImplPrivate test_api(native_widget->window()); |
| 449 test_api.CallOnWindowInputEvent(native_widget->window(), *mouse); |
| 450 |
| 451 // Location delivered to view should be relative to view's origin. |
| 452 EXPECT_EQ(gfx::Point(mouse_location().x() - content_x, |
| 453 mouse_location().y() - content_y), |
| 454 content->latest_event_location()); |
| 455 } |
| 456 |
| 424 // Tests that an incoming UI event is acked with the handled status. | 457 // Tests that an incoming UI event is acked with the handled status. |
| 425 TEST_F(NativeWidgetMusTest, EventAcked) { | 458 TEST_F(NativeWidgetMusTest, EventAcked) { |
| 426 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); | 459 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 427 widget->Show(); | 460 widget->Show(); |
| 428 | 461 |
| 429 View* content = new HandleMousePressView; | 462 View* content = new HandleMousePressView; |
| 430 content->SetBounds(10, 20, 90, 180); | 463 content->SetBounds(10, 20, 90, 180); |
| 431 widget->GetContentsView()->AddChildView(content); | 464 widget->GetContentsView()->AddChildView(content); |
| 432 | 465 |
| 433 // Dispatch an input event to the window and view. | 466 // Dispatch an input event to the window and view. |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 mus_window->SetBounds(end_bounds); | 548 mus_window->SetBounds(end_bounds); |
| 516 | 549 |
| 517 EXPECT_EQ(end_bounds, mus_window->bounds()); | 550 EXPECT_EQ(end_bounds, mus_window->bounds()); |
| 518 | 551 |
| 519 // Main check for this test: Setting |mus_window| bounds while bypassing | 552 // Main check for this test: Setting |mus_window| bounds while bypassing |
| 520 // |native_widget| must update window_tree_host bounds. | 553 // |native_widget| must update window_tree_host bounds. |
| 521 EXPECT_EQ(end_bounds, native_widget->window_tree_host()->GetBounds()); | 554 EXPECT_EQ(end_bounds, native_widget->window_tree_host()->GetBounds()); |
| 522 } | 555 } |
| 523 | 556 |
| 524 } // namespace views | 557 } // namespace views |
| OLD | NEW |