Chromium Code Reviews| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 void GetWidgetHitTestMask(gfx::Path* mask) const override { | 134 void GetWidgetHitTestMask(gfx::Path* mask) const override { |
| 135 mask->addRect(gfx::RectToSkRect(mask_rect_)); | 135 mask->addRect(gfx::RectToSkRect(mask_rect_)); |
| 136 } | 136 } |
| 137 | 137 |
| 138 private: | 138 private: |
| 139 gfx::Rect mask_rect_; | 139 gfx::Rect mask_rect_; |
| 140 | 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(WidgetDelegateWithHitTestMask); | 141 DISALLOW_COPY_AND_ASSIGN(WidgetDelegateWithHitTestMask); |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 class UIMouseEventHandler : public ui::test::TestEventHandler { | |
| 145 public: | |
| 146 explicit UIMouseEventHandler(gfx::Point* event_location) | |
| 147 : event_location_(event_location), event_received_(false) {} | |
| 148 ~UIMouseEventHandler() override {} | |
| 149 | |
| 150 void OnMouseEvent(ui::MouseEvent* event) override { | |
| 151 *event_location_ = event->location(); | |
| 152 set_event_received(); | |
| 153 } | |
| 154 | |
| 155 bool event_received() { return event_received_; } | |
| 156 | |
| 157 private: | |
| 158 void set_event_received() { event_received_ = true; } | |
| 159 | |
| 160 gfx::Point* event_location_; | |
| 161 bool event_received_; | |
| 162 | |
| 163 DISALLOW_COPY_AND_ASSIGN(UIMouseEventHandler); | |
| 164 }; | |
| 165 | |
| 166 class MusMouseEventHandler : public mus::InputEventHandler { | |
| 167 public: | |
| 168 MusMouseEventHandler(gfx::Point* event_location, mus::Window* window) | |
| 169 : event_location_(event_location), window_(window), | |
| 170 event_received_(false) { | |
| 171 delegate_ = window->input_event_handler(); | |
| 172 window->set_input_event_handler(this); | |
| 173 } | |
| 174 ~MusMouseEventHandler() override { | |
| 175 window_->set_input_event_handler(delegate_); | |
| 176 } | |
| 177 | |
| 178 bool event_received() { return event_received_; } | |
| 179 | |
| 180 private: | |
| 181 void set_event_received() { event_received_ = true; } | |
| 182 | |
| 183 // InputEventHandler: | |
| 184 void OnWindowInputEvent( | |
| 185 mus::Window* target, | |
| 186 const ui::Event& event, | |
| 187 std::unique_ptr<base::Callback<void(EventResult)>>* ack_callback) | |
| 188 override { | |
| 189 DCHECK(event.IsMouseEvent()); | |
| 190 *event_location_ = event.AsMouseEvent()->location(); | |
| 191 set_event_received(); | |
| 192 if (delegate_) | |
| 193 delegate_->OnWindowInputEvent(target, event, ack_callback); | |
| 194 } | |
| 195 | |
| 196 gfx::Point* event_location_; | |
| 197 mus::Window* window_; | |
| 198 bool event_received_; | |
| 199 mus::InputEventHandler* delegate_; | |
| 200 | |
| 201 DISALLOW_COPY_AND_ASSIGN(MusMouseEventHandler); | |
| 202 }; | |
| 203 | |
| 144 } // namespace | 204 } // namespace |
| 145 | 205 |
| 146 class NativeWidgetMusTest : public ViewsTestBase { | 206 class NativeWidgetMusTest : public ViewsTestBase { |
| 147 public: | 207 public: |
| 148 NativeWidgetMusTest() {} | 208 NativeWidgetMusTest() {} |
| 149 ~NativeWidgetMusTest() override {} | 209 ~NativeWidgetMusTest() override {} |
| 150 | 210 |
| 151 // Creates a test widget. Takes ownership of |delegate|. | 211 // Creates a test widget. Takes ownership of |delegate|. |
| 152 std::unique_ptr<Widget> CreateWidget(WidgetDelegate* delegate) { | 212 std::unique_ptr<Widget> CreateWidget(WidgetDelegate* delegate) { |
| 153 std::unique_ptr<Widget> widget(new Widget()); | 213 std::unique_ptr<Widget> widget(new Widget()); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 164 void AckCallback(mus::mojom::EventResult result) { | 224 void AckCallback(mus::mojom::EventResult result) { |
| 165 ack_callback_count_++; | 225 ack_callback_count_++; |
| 166 EXPECT_EQ(mus::mojom::EventResult::HANDLED, result); | 226 EXPECT_EQ(mus::mojom::EventResult::HANDLED, result); |
| 167 } | 227 } |
| 168 | 228 |
| 169 // Returns a mouse pressed event inside the widget. Tests that place views | 229 // 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 | 230 // within the widget that respond to the event must be constructed within the |
| 171 // widget coordinate space such that they respond correctly. | 231 // widget coordinate space such that they respond correctly. |
| 172 std::unique_ptr<ui::MouseEvent> CreateMouseEvent() { | 232 std::unique_ptr<ui::MouseEvent> CreateMouseEvent() { |
| 173 return base::WrapUnique(new ui::MouseEvent( | 233 return base::WrapUnique(new ui::MouseEvent( |
| 174 ui::ET_MOUSE_PRESSED, gfx::Point(50, 50), gfx::Point(50, 50), | 234 ui::ET_MOUSE_PRESSED, mouse_location(), mouse_location(), |
| 175 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); | 235 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); |
| 176 } | 236 } |
| 177 | 237 |
| 178 // Simulates an input event to the NativeWidget. | 238 // Simulates an input event to the NativeWidget. |
| 179 void OnWindowInputEvent( | 239 void OnWindowInputEvent( |
| 180 NativeWidgetMus* native_widget, | 240 NativeWidgetMus* native_widget, |
| 181 const ui::Event& event, | 241 const ui::Event& event, |
| 182 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* | 242 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* |
| 183 ack_callback) { | 243 ack_callback) { |
| 184 native_widget->OnWindowInputEvent(native_widget->window(), event, | 244 native_widget->OnWindowInputEvent(native_widget->window(), event, |
| 185 ack_callback); | 245 ack_callback); |
| 186 } | 246 } |
| 187 | 247 |
| 188 protected: | 248 protected: |
| 189 gfx::Rect initial_bounds() { | 249 gfx::Rect initial_bounds() { |
| 190 return gfx::Rect(10, 20, 100, 200); | 250 return gfx::Rect(10, 20, 100, 200); |
| 191 } | 251 } |
| 192 | 252 |
| 253 gfx::Point mouse_location() { | |
| 254 return gfx::Point(50, 50); | |
| 255 } | |
| 256 | |
| 193 private: | 257 private: |
| 194 int ack_callback_count_ = 0; | 258 int ack_callback_count_ = 0; |
| 195 | 259 |
| 196 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); | 260 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMusTest); |
| 197 }; | 261 }; |
| 198 | 262 |
| 199 // Tests communication of activation and focus between Widget and | 263 // Tests communication of activation and focus between Widget and |
| 200 // NativeWidgetMus. | 264 // NativeWidgetMus. |
| 201 TEST_F(NativeWidgetMusTest, OnActivationChanged) { | 265 TEST_F(NativeWidgetMusTest, OnActivationChanged) { |
| 202 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); | 266 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 416 content->AddPreTargetHandler(&handler); | 480 content->AddPreTargetHandler(&handler); |
| 417 | 481 |
| 418 std::unique_ptr<ui::MouseEvent> mouse = CreateMouseEvent(); | 482 std::unique_ptr<ui::MouseEvent> mouse = CreateMouseEvent(); |
| 419 NativeWidgetMus* native_widget = | 483 NativeWidgetMus* native_widget = |
| 420 static_cast<NativeWidgetMus*>(widget->native_widget_private()); | 484 static_cast<NativeWidgetMus*>(widget->native_widget_private()); |
| 421 mus::WindowTreeClientImplPrivate test_api(native_widget->window()); | 485 mus::WindowTreeClientImplPrivate test_api(native_widget->window()); |
| 422 test_api.CallOnWindowInputEvent(native_widget->window(), *mouse); | 486 test_api.CallOnWindowInputEvent(native_widget->window(), *mouse); |
| 423 EXPECT_EQ(1, handler.num_mouse_events()); | 487 EXPECT_EQ(1, handler.num_mouse_events()); |
| 424 } | 488 } |
| 425 | 489 |
| 490 TEST_F(NativeWidgetMusTest, WidgetViewMouseEventAgreement) { | |
| 491 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); | |
| 492 widget->Show(); | |
| 493 | |
| 494 View* content = new HandleMousePressView; | |
| 495 int content_x = 10; | |
| 496 int content_y = 20; | |
| 497 content->SetBounds(content_x, content_y, 90, 180); | |
| 498 widget->GetContentsView()->AddChildView(content); | |
| 499 | |
| 500 gfx::Point ui_event_location; | |
| 501 UIMouseEventHandler ui_handler(&ui_event_location); | |
| 502 content->AddPreTargetHandler(&ui_handler); | |
| 503 | |
| 504 gfx::Point mus_event_location; | |
| 505 NativeWidgetMus* native_widget = | |
| 506 static_cast<NativeWidgetMus*>(widget->native_widget_private()); | |
| 507 MusMouseEventHandler mus_handler(&mus_event_location, | |
| 508 native_widget->window()); // Self-installed. | |
| 509 | |
| 510 std::unique_ptr<ui::MouseEvent> mouse = CreateMouseEvent(); | |
| 511 mus::WindowTreeClientImplPrivate test_api(native_widget->window()); | |
| 512 test_api.CallOnWindowInputEvent(native_widget->window(), *mouse); | |
| 513 | |
| 514 EXPECT_TRUE(ui_handler.event_received()); | |
| 515 EXPECT_TRUE(mus_handler.event_received()); | |
| 516 // Location delivered to view should be relative to view's origin. | |
| 517 EXPECT_EQ(gfx::Point(mouse_location().x() - content_x, | |
| 518 mouse_location().y() - content_y), | |
| 519 ui_event_location); | |
| 520 EXPECT_EQ(gfx::Point(mouse_location()), mus_event_location); | |
|
sadrul
2016/05/27 18:11:16
I think this test could be simpler:
. Define an
Mark Dittmer
2016/05/30 17:03:59
Done.
| |
| 521 } | |
| 522 | |
| 426 // Tests that an incoming UI event is acked with the handled status. | 523 // Tests that an incoming UI event is acked with the handled status. |
| 427 TEST_F(NativeWidgetMusTest, EventAcked) { | 524 TEST_F(NativeWidgetMusTest, EventAcked) { |
| 428 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); | 525 std::unique_ptr<Widget> widget(CreateWidget(nullptr)); |
| 429 widget->Show(); | 526 widget->Show(); |
| 430 | 527 |
| 431 View* content = new HandleMousePressView; | 528 View* content = new HandleMousePressView; |
| 432 content->SetBounds(10, 20, 90, 180); | 529 content->SetBounds(10, 20, 90, 180); |
| 433 widget->GetContentsView()->AddChildView(content); | 530 widget->GetContentsView()->AddChildView(content); |
| 434 | 531 |
| 435 // Dispatch an input event to the window and view. | 532 // Dispatch an input event to the window and view. |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 mus_window->SetBounds(end_bounds); | 614 mus_window->SetBounds(end_bounds); |
| 518 | 615 |
| 519 EXPECT_EQ(end_bounds, mus_window->bounds()); | 616 EXPECT_EQ(end_bounds, mus_window->bounds()); |
| 520 | 617 |
| 521 // Main check for this test: Setting |mus_window| bounds while bypassing | 618 // Main check for this test: Setting |mus_window| bounds while bypassing |
| 522 // |native_widget| must update window_tree_host bounds. | 619 // |native_widget| must update window_tree_host bounds. |
| 523 EXPECT_EQ(end_bounds, native_widget->window_tree_host()->GetBounds()); | 620 EXPECT_EQ(end_bounds, native_widget->window_tree_host()->GetBounds()); |
| 524 } | 621 } |
| 525 | 622 |
| 526 } // namespace views | 623 } // namespace views |
| OLD | NEW |