| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/widget/root_view.h" | 5 #include "ui/views/widget/root_view.h" |
| 6 | 6 |
| 7 #include "ui/events/event_utils.h" | 7 #include "ui/events/event_utils.h" |
| 8 #include "ui/views/context_menu_controller.h" | 8 #include "ui/views/context_menu_controller.h" |
| 9 #include "ui/views/test/views_test_base.h" | 9 #include "ui/views/test/views_test_base.h" |
| 10 #include "ui/views/view_targeter.h" | 10 #include "ui/views/view_targeter.h" |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 bool view_destroyed = false; | 384 bool view_destroyed = false; |
| 385 View* child = new DeleteViewOnEvent(ui::ET_MOUSE_EXITED, &view_destroyed); | 385 View* child = new DeleteViewOnEvent(ui::ET_MOUSE_EXITED, &view_destroyed); |
| 386 content->AddChildView(child); | 386 content->AddChildView(child); |
| 387 child->SetBounds(10, 10, 500, 500); | 387 child->SetBounds(10, 10, 500, 500); |
| 388 | 388 |
| 389 internal::RootView* root_view = | 389 internal::RootView* root_view = |
| 390 static_cast<internal::RootView*>(widget.GetRootView()); | 390 static_cast<internal::RootView*>(widget.GetRootView()); |
| 391 | 391 |
| 392 // Generate a mouse move event which ensures that the mouse_moved_handler_ | 392 // Generate a mouse move event which ensures that the mouse_moved_handler_ |
| 393 // member is set in the RootView class. | 393 // member is set in the RootView class. |
| 394 ui::MouseEvent moved_event(ui::ET_MOUSE_MOVED, gfx::Point(15, 15), | 394 ui::MouseEvent moved_event( |
| 395 gfx::Point(15, 15), ui::EventTimeForNow(), 0, | 395 ui::ET_MOUSE_MOVED, gfx::Point(15, 15), gfx::Point(15, 15), |
| 396 0); | 396 ui::EventTimeForNow(), 0, 0, |
| 397 ui::PointerEventDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 397 root_view->OnMouseMoved(moved_event); | 398 root_view->OnMouseMoved(moved_event); |
| 398 ASSERT_FALSE(view_destroyed); | 399 ASSERT_FALSE(view_destroyed); |
| 399 | 400 |
| 400 // Generate a mouse exit event which in turn will delete the child view which | 401 // Generate a mouse exit event which in turn will delete the child view which |
| 401 // was the target of the mouse move event above. This should not crash when | 402 // was the target of the mouse move event above. This should not crash when |
| 402 // the mouse exit handler returns from the child. | 403 // the mouse exit handler returns from the child. |
| 403 ui::MouseEvent exit_event(ui::ET_MOUSE_EXITED, gfx::Point(), gfx::Point(), | 404 ui::MouseEvent exit_event( |
| 404 ui::EventTimeForNow(), 0, 0); | 405 ui::ET_MOUSE_EXITED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), 0, |
| 406 0, ui::PointerEventDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 405 root_view->OnMouseExited(exit_event); | 407 root_view->OnMouseExited(exit_event); |
| 406 | 408 |
| 407 EXPECT_TRUE(view_destroyed); | 409 EXPECT_TRUE(view_destroyed); |
| 408 EXPECT_FALSE(content->has_children()); | 410 EXPECT_FALSE(content->has_children()); |
| 409 } | 411 } |
| 410 | 412 |
| 411 // Verifies deleting a View in OnMouseEntered() doesn't crash. | 413 // Verifies deleting a View in OnMouseEntered() doesn't crash. |
| 412 TEST_F(RootViewTest, DeleteViewOnMouseEnterDispatch) { | 414 TEST_F(RootViewTest, DeleteViewOnMouseEnterDispatch) { |
| 413 Widget widget; | 415 Widget widget; |
| 414 Widget::InitParams init_params = | 416 Widget::InitParams init_params = |
| 415 CreateParams(Widget::InitParams::TYPE_POPUP); | 417 CreateParams(Widget::InitParams::TYPE_POPUP); |
| 416 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 418 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 417 widget.Init(init_params); | 419 widget.Init(init_params); |
| 418 widget.SetBounds(gfx::Rect(10, 10, 500, 500)); | 420 widget.SetBounds(gfx::Rect(10, 10, 500, 500)); |
| 419 | 421 |
| 420 View* content = new View; | 422 View* content = new View; |
| 421 widget.SetContentsView(content); | 423 widget.SetContentsView(content); |
| 422 | 424 |
| 423 bool view_destroyed = false; | 425 bool view_destroyed = false; |
| 424 View* child = new DeleteViewOnEvent(ui::ET_MOUSE_ENTERED, &view_destroyed); | 426 View* child = new DeleteViewOnEvent(ui::ET_MOUSE_ENTERED, &view_destroyed); |
| 425 content->AddChildView(child); | 427 content->AddChildView(child); |
| 426 | 428 |
| 427 // Make |child| smaller than the containing Widget and RootView. | 429 // Make |child| smaller than the containing Widget and RootView. |
| 428 child->SetBounds(100, 100, 100, 100); | 430 child->SetBounds(100, 100, 100, 100); |
| 429 | 431 |
| 430 internal::RootView* root_view = | 432 internal::RootView* root_view = |
| 431 static_cast<internal::RootView*>(widget.GetRootView()); | 433 static_cast<internal::RootView*>(widget.GetRootView()); |
| 432 | 434 |
| 433 // Move the mouse within |widget| but outside of |child|. | 435 // Move the mouse within |widget| but outside of |child|. |
| 434 ui::MouseEvent moved_event(ui::ET_MOUSE_MOVED, gfx::Point(15, 15), | 436 ui::MouseEvent moved_event( |
| 435 gfx::Point(15, 15), ui::EventTimeForNow(), 0, | 437 ui::ET_MOUSE_MOVED, gfx::Point(15, 15), gfx::Point(15, 15), |
| 436 0); | 438 ui::EventTimeForNow(), 0, 0, |
| 439 ui::PointerEventDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 437 root_view->OnMouseMoved(moved_event); | 440 root_view->OnMouseMoved(moved_event); |
| 438 ASSERT_FALSE(view_destroyed); | 441 ASSERT_FALSE(view_destroyed); |
| 439 | 442 |
| 440 // Move the mouse within |child|, which should dispatch a mouse enter event to | 443 // Move the mouse within |child|, which should dispatch a mouse enter event to |
| 441 // |child| and destroy the view. This should not crash when the mouse enter | 444 // |child| and destroy the view. This should not crash when the mouse enter |
| 442 // handler returns from the child. | 445 // handler returns from the child. |
| 443 ui::MouseEvent moved_event2(ui::ET_MOUSE_MOVED, gfx::Point(115, 115), | 446 ui::MouseEvent moved_event2( |
| 444 gfx::Point(115, 115), ui::EventTimeForNow(), 0, | 447 ui::ET_MOUSE_MOVED, gfx::Point(115, 115), gfx::Point(115, 115), |
| 445 0); | 448 ui::EventTimeForNow(), 0, 0, |
| 449 ui::PointerEventDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 446 root_view->OnMouseMoved(moved_event2); | 450 root_view->OnMouseMoved(moved_event2); |
| 447 | 451 |
| 448 EXPECT_TRUE(view_destroyed); | 452 EXPECT_TRUE(view_destroyed); |
| 449 EXPECT_FALSE(content->has_children()); | 453 EXPECT_FALSE(content->has_children()); |
| 450 } | 454 } |
| 451 | 455 |
| 452 namespace { | 456 namespace { |
| 453 | 457 |
| 454 // View class which deletes its owning Widget when it gets a mouse exit event. | 458 // View class which deletes its owning Widget when it gets a mouse exit event. |
| 455 class DeleteWidgetOnMouseExit : public View { | 459 class DeleteWidgetOnMouseExit : public View { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 content->AddChildView(child); | 492 content->AddChildView(child); |
| 489 widget->SetContentsView(content); | 493 widget->SetContentsView(content); |
| 490 | 494 |
| 491 // Make |child| smaller than the containing Widget and RootView. | 495 // Make |child| smaller than the containing Widget and RootView. |
| 492 child->SetBounds(100, 100, 100, 100); | 496 child->SetBounds(100, 100, 100, 100); |
| 493 | 497 |
| 494 internal::RootView* root_view = | 498 internal::RootView* root_view = |
| 495 static_cast<internal::RootView*>(widget->GetRootView()); | 499 static_cast<internal::RootView*>(widget->GetRootView()); |
| 496 | 500 |
| 497 // Move the mouse within |child|. | 501 // Move the mouse within |child|. |
| 498 ui::MouseEvent moved_event(ui::ET_MOUSE_MOVED, gfx::Point(115, 115), | 502 ui::MouseEvent moved_event( |
| 499 gfx::Point(115, 115), ui::EventTimeForNow(), 0, | 503 ui::ET_MOUSE_MOVED, gfx::Point(115, 115), gfx::Point(115, 115), |
| 500 0); | 504 ui::EventTimeForNow(), 0, 0, |
| 505 ui::PointerEventDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 501 root_view->OnMouseMoved(moved_event); | 506 root_view->OnMouseMoved(moved_event); |
| 502 ASSERT_TRUE(widget_deletion_observer.IsWidgetAlive()); | 507 ASSERT_TRUE(widget_deletion_observer.IsWidgetAlive()); |
| 503 | 508 |
| 504 // Move the mouse outside of |child| which should dispatch a mouse exit event | 509 // Move the mouse outside of |child| which should dispatch a mouse exit event |
| 505 // to |child| and destroy the widget. This should not crash when the mouse | 510 // to |child| and destroy the widget. This should not crash when the mouse |
| 506 // exit handler returns from the child. | 511 // exit handler returns from the child. |
| 507 ui::MouseEvent move_event2(ui::ET_MOUSE_MOVED, gfx::Point(15, 15), | 512 ui::MouseEvent move_event2( |
| 508 gfx::Point(15, 15), ui::EventTimeForNow(), 0, 0); | 513 ui::ET_MOUSE_MOVED, gfx::Point(15, 15), gfx::Point(15, 15), |
| 514 ui::EventTimeForNow(), 0, 0, |
| 515 ui::PointerEventDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 509 root_view->OnMouseMoved(move_event2); | 516 root_view->OnMouseMoved(move_event2); |
| 510 EXPECT_FALSE(widget_deletion_observer.IsWidgetAlive()); | 517 EXPECT_FALSE(widget_deletion_observer.IsWidgetAlive()); |
| 511 } | 518 } |
| 512 | 519 |
| 513 // Test that there is no crash if a View deletes its parent widget as a result | 520 // Test that there is no crash if a View deletes its parent widget as a result |
| 514 // of a mouse exited event which was propagated from one of its children. | 521 // of a mouse exited event which was propagated from one of its children. |
| 515 TEST_F(RootViewTest, DeleteWidgetOnMouseExitDispatchFromChild) { | 522 TEST_F(RootViewTest, DeleteWidgetOnMouseExitDispatchFromChild) { |
| 516 Widget* widget = new Widget; | 523 Widget* widget = new Widget; |
| 517 Widget::InitParams init_params = | 524 Widget::InitParams init_params = |
| 518 CreateParams(Widget::InitParams::TYPE_POPUP); | 525 CreateParams(Widget::InitParams::TYPE_POPUP); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 533 child->SetBounds(100, 100, 100, 100); | 540 child->SetBounds(100, 100, 100, 100); |
| 534 subchild->SetBounds(0, 0, 100, 100); | 541 subchild->SetBounds(0, 0, 100, 100); |
| 535 | 542 |
| 536 // Make mouse enter and exit events get propagated from |subchild| to |child|. | 543 // Make mouse enter and exit events get propagated from |subchild| to |child|. |
| 537 child->set_notify_enter_exit_on_child(true); | 544 child->set_notify_enter_exit_on_child(true); |
| 538 | 545 |
| 539 internal::RootView* root_view = | 546 internal::RootView* root_view = |
| 540 static_cast<internal::RootView*>(widget->GetRootView()); | 547 static_cast<internal::RootView*>(widget->GetRootView()); |
| 541 | 548 |
| 542 // Move the mouse within |subchild| and |child|. | 549 // Move the mouse within |subchild| and |child|. |
| 543 ui::MouseEvent moved_event(ui::ET_MOUSE_MOVED, gfx::Point(115, 115), | 550 ui::MouseEvent moved_event( |
| 544 gfx::Point(115, 115), ui::EventTimeForNow(), 0, 0); | 551 ui::ET_MOUSE_MOVED, gfx::Point(115, 115), gfx::Point(115, 115), |
| 552 ui::EventTimeForNow(), 0, 0, |
| 553 ui::PointerEventDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 545 root_view->OnMouseMoved(moved_event); | 554 root_view->OnMouseMoved(moved_event); |
| 546 ASSERT_TRUE(widget_deletion_observer.IsWidgetAlive()); | 555 ASSERT_TRUE(widget_deletion_observer.IsWidgetAlive()); |
| 547 | 556 |
| 548 // Move the mouse outside of |subchild| and |child| which should dispatch a | 557 // Move the mouse outside of |subchild| and |child| which should dispatch a |
| 549 // mouse exit event to |subchild| and destroy the widget. This should not | 558 // mouse exit event to |subchild| and destroy the widget. This should not |
| 550 // crash when the mouse exit handler returns from |subchild|. | 559 // crash when the mouse exit handler returns from |subchild|. |
| 551 ui::MouseEvent move_event2(ui::ET_MOUSE_MOVED, gfx::Point(15, 15), | 560 ui::MouseEvent move_event2( |
| 552 gfx::Point(15, 15), ui::EventTimeForNow(), 0, 0); | 561 ui::ET_MOUSE_MOVED, gfx::Point(15, 15), gfx::Point(15, 15), |
| 562 ui::EventTimeForNow(), 0, 0, |
| 563 ui::PointerEventDetails(ui::EventPointerType::POINTER_TYPE_MOUSE)); |
| 553 root_view->OnMouseMoved(move_event2); | 564 root_view->OnMouseMoved(move_event2); |
| 554 EXPECT_FALSE(widget_deletion_observer.IsWidgetAlive()); | 565 EXPECT_FALSE(widget_deletion_observer.IsWidgetAlive()); |
| 555 } | 566 } |
| 556 | 567 |
| 557 } // namespace test | 568 } // namespace test |
| 558 } // namespace views | 569 } // namespace views |
| OLD | NEW |