| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/controls/menu/menu_controller.h" | 5 #include "ui/views/controls/menu/menu_controller.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "ui/aura/scoped_window_targeter.h" | 14 #include "ui/aura/scoped_window_targeter.h" |
| 15 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
| 16 #include "ui/events/event.h" | 16 #include "ui/events/event.h" |
| 17 #include "ui/events/event_constants.h" | 17 #include "ui/events/event_constants.h" |
| 18 #include "ui/events/event_handler.h" | 18 #include "ui/events/event_handler.h" |
| 19 #include "ui/events/event_utils.h" | 19 #include "ui/events/event_utils.h" |
| 20 #include "ui/events/null_event_targeter.h" | 20 #include "ui/events/null_event_targeter.h" |
| 21 #include "ui/events/test/event_generator.h" | 21 #include "ui/events/test/event_generator.h" |
| 22 #include "ui/gfx/geometry/point.h" | 22 #include "ui/gfx/geometry/point.h" |
| 23 #include "ui/gfx/geometry/rect.h" | 23 #include "ui/gfx/geometry/rect.h" |
| 24 #include "ui/views/controls/menu/menu_controller_delegate.h" | 24 #include "ui/views/controls/menu/menu_controller_delegate.h" |
| 25 #include "ui/views/controls/menu/menu_delegate.h" | 25 #include "ui/views/controls/menu/menu_delegate.h" |
| 26 #include "ui/views/controls/menu/menu_host.h" | 26 #include "ui/views/controls/menu/menu_host.h" |
| 27 #include "ui/views/controls/menu/menu_host_root_view.h" | |
| 28 #include "ui/views/controls/menu/menu_item_view.h" | 27 #include "ui/views/controls/menu/menu_item_view.h" |
| 29 #include "ui/views/controls/menu/menu_message_loop.h" | 28 #include "ui/views/controls/menu/menu_message_loop.h" |
| 30 #include "ui/views/controls/menu/menu_scroll_view_container.h" | 29 #include "ui/views/controls/menu/menu_scroll_view_container.h" |
| 31 #include "ui/views/controls/menu/submenu_view.h" | 30 #include "ui/views/controls/menu/submenu_view.h" |
| 32 #include "ui/views/test/menu_test_utils.h" | 31 #include "ui/views/test/menu_test_utils.h" |
| 33 #include "ui/views/test/test_views_delegate.h" | 32 #include "ui/views/test/test_views_delegate.h" |
| 34 #include "ui/views/test/views_test_base.h" | 33 #include "ui/views/test/views_test_base.h" |
| 35 #include "ui/views/widget/root_view.h" | |
| 36 | 34 |
| 37 #if defined(USE_AURA) | 35 #if defined(USE_AURA) |
| 38 #include "ui/aura/client/drag_drop_client.h" | 36 #include "ui/aura/client/drag_drop_client.h" |
| 39 #include "ui/aura/scoped_window_targeter.h" | 37 #include "ui/aura/scoped_window_targeter.h" |
| 40 #include "ui/aura/window.h" | 38 #include "ui/aura/window.h" |
| 41 #include "ui/views/controls/menu/menu_pre_target_handler.h" | 39 #include "ui/views/controls/menu/menu_pre_target_handler.h" |
| 42 #endif | 40 #endif |
| 43 | 41 |
| 44 #if defined(USE_X11) | 42 #if defined(USE_X11) |
| 45 #include <X11/Xlib.h> | 43 #include <X11/Xlib.h> |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 internal::MenuControllerDelegate* GetCurrentDelegate() { | 499 internal::MenuControllerDelegate* GetCurrentDelegate() { |
| 502 return menu_controller_->delegate_; | 500 return menu_controller_->delegate_; |
| 503 } | 501 } |
| 504 | 502 |
| 505 bool IsAsyncRun() { return menu_controller_->async_run_; } | 503 bool IsAsyncRun() { return menu_controller_->async_run_; } |
| 506 | 504 |
| 507 bool IsShowing() { return menu_controller_->showing_; } | 505 bool IsShowing() { return menu_controller_->showing_; } |
| 508 | 506 |
| 509 MenuHost* GetMenuHost(SubmenuView* submenu) { return submenu->host_; } | 507 MenuHost* GetMenuHost(SubmenuView* submenu) { return submenu->host_; } |
| 510 | 508 |
| 511 MenuHostRootView* CreateMenuHostRootView(MenuHost* host) { | |
| 512 return static_cast<MenuHostRootView*>(host->CreateRootView()); | |
| 513 } | |
| 514 | |
| 515 void MenuHostOnDragWillStart(MenuHost* host) { host->OnDragWillStart(); } | 509 void MenuHostOnDragWillStart(MenuHost* host) { host->OnDragWillStart(); } |
| 516 | 510 |
| 517 void MenuHostOnDragComplete(MenuHost* host) { host->OnDragComplete(); } | 511 void MenuHostOnDragComplete(MenuHost* host) { host->OnDragComplete(); } |
| 518 | 512 |
| 519 void SelectByChar(base::char16 character) { | 513 void SelectByChar(base::char16 character) { |
| 520 menu_controller_->SelectByChar(character); | 514 menu_controller_->SelectByChar(character); |
| 521 } | 515 } |
| 522 | 516 |
| 523 void SetDropMenuItem(MenuItemView* target, | 517 void SetDropMenuItem(MenuItemView* target, |
| 524 MenuDelegate::DropPosition position) { | 518 MenuDelegate::DropPosition position) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 | 600 |
| 607 void SetHotTrackedButton(CustomButton* hot_button) { | 601 void SetHotTrackedButton(CustomButton* hot_button) { |
| 608 menu_controller_->SetHotTrackedButton(hot_button); | 602 menu_controller_->SetHotTrackedButton(hot_button); |
| 609 } | 603 } |
| 610 | 604 |
| 611 void ExitMenuRun() { | 605 void ExitMenuRun() { |
| 612 menu_controller_->SetExitType(MenuController::ExitType::EXIT_OUTERMOST); | 606 menu_controller_->SetExitType(MenuController::ExitType::EXIT_OUTERMOST); |
| 613 menu_controller_->ExitMenuRun(); | 607 menu_controller_->ExitMenuRun(); |
| 614 } | 608 } |
| 615 | 609 |
| 610 private: |
| 616 void DestroyMenuController() { | 611 void DestroyMenuController() { |
| 617 if (!menu_controller_) | 612 if (!menu_controller_) |
| 618 return; | 613 return; |
| 619 | 614 |
| 620 if (!owner_->IsClosed()) | 615 if (!owner_->IsClosed()) |
| 621 owner_->RemoveObserver(menu_controller_); | 616 owner_->RemoveObserver(menu_controller_); |
| 622 | 617 |
| 623 menu_controller_->showing_ = false; | 618 menu_controller_->showing_ = false; |
| 624 menu_controller_->owner_ = nullptr; | 619 menu_controller_->owner_ = nullptr; |
| 625 delete menu_controller_; | 620 delete menu_controller_; |
| 626 menu_controller_ = nullptr; | 621 menu_controller_ = nullptr; |
| 627 } | 622 } |
| 628 | 623 |
| 629 private: | |
| 630 void Init() { | 624 void Init() { |
| 631 owner_.reset(new Widget); | 625 owner_.reset(new Widget); |
| 632 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 626 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 633 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 627 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 634 owner_->Init(params); | 628 owner_->Init(params); |
| 635 event_generator_.reset( | 629 event_generator_.reset( |
| 636 new ui::test::EventGenerator(owner_->GetNativeWindow())); | 630 new ui::test::EventGenerator(owner_->GetNativeWindow())); |
| 637 owner_->Show(); | 631 owner_->Show(); |
| 638 | 632 |
| 639 SetupMenuItem(); | 633 SetupMenuItem(); |
| (...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1191 | 1185 |
| 1192 SubmenuView* submenu = menu_item()->GetSubmenu(); | 1186 SubmenuView* submenu = menu_item()->GetSubmenu(); |
| 1193 submenu->ShowAt(owner(), menu_item()->bounds(), false); | 1187 submenu->ShowAt(owner(), menu_item()->bounds(), false); |
| 1194 MenuHost* host = GetMenuHost(submenu); | 1188 MenuHost* host = GetMenuHost(submenu); |
| 1195 MenuHostOnDragWillStart(host); | 1189 MenuHostOnDragWillStart(host); |
| 1196 submenu->Close(); | 1190 submenu->Close(); |
| 1197 DestroyMenuItem(); | 1191 DestroyMenuItem(); |
| 1198 MenuHostOnDragComplete(host); | 1192 MenuHostOnDragComplete(host); |
| 1199 } | 1193 } |
| 1200 | 1194 |
| 1201 // Widget destruction and cleanup occurs on the MessageLoop after the | |
| 1202 // MenuController has been destroyed. A MenuHostRootView should not attempt to | |
| 1203 // access a destroyed MenuController. This test should not cause a crash. | |
| 1204 TEST_F(MenuControllerTest, HostReceivesInputBeforeDestruction) { | |
| 1205 MenuController* controller = menu_controller(); | |
| 1206 controller->SetAsyncRun(true); | |
| 1207 | |
| 1208 SubmenuView* submenu = menu_item()->GetSubmenu(); | |
| 1209 submenu->ShowAt(owner(), menu_item()->bounds(), false); | |
| 1210 gfx::Point location(submenu->bounds().bottom_right()); | |
| 1211 location.Offset(1, 1); | |
| 1212 | |
| 1213 MenuHost* host = GetMenuHost(submenu); | |
| 1214 MenuHostRootView* root_view = CreateMenuHostRootView(host); | |
| 1215 DestroyMenuController(); | |
| 1216 | |
| 1217 ui::MouseEvent event(ui::ET_MOUSE_MOVED, location, location, | |
| 1218 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0); | |
| 1219 | |
| 1220 root_view->OnMouseMoved(event); | |
| 1221 } | |
| 1222 | |
| 1223 // Tets that an asynchronous menu nested within an asynchronous menu closes both | 1195 // Tets that an asynchronous menu nested within an asynchronous menu closes both |
| 1224 // menus, and notifies both delegates. | 1196 // menus, and notifies both delegates. |
| 1225 TEST_F(MenuControllerTest, DoubleAsynchronousNested) { | 1197 TEST_F(MenuControllerTest, DoubleAsynchronousNested) { |
| 1226 MenuController* controller = menu_controller(); | 1198 MenuController* controller = menu_controller(); |
| 1227 TestMenuControllerDelegate* delegate = menu_controller_delegate(); | 1199 TestMenuControllerDelegate* delegate = menu_controller_delegate(); |
| 1228 std::unique_ptr<TestMenuControllerDelegate> nested_delegate( | 1200 std::unique_ptr<TestMenuControllerDelegate> nested_delegate( |
| 1229 new TestMenuControllerDelegate()); | 1201 new TestMenuControllerDelegate()); |
| 1230 | 1202 |
| 1231 ASSERT_FALSE(IsAsyncRun()); | 1203 ASSERT_FALSE(IsAsyncRun()); |
| 1232 // Sets the run created in SetUp | 1204 // Sets the run created in SetUp |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1542 controller->Run(owner(), nullptr, menu_item(), gfx::Rect(), | 1514 controller->Run(owner(), nullptr, menu_item(), gfx::Rect(), |
| 1543 MENU_ANCHOR_TOPLEFT, false, false, &mouse_event_flags); | 1515 MENU_ANCHOR_TOPLEFT, false, false, &mouse_event_flags); |
| 1544 EXPECT_EQ(run_result, nullptr); | 1516 EXPECT_EQ(run_result, nullptr); |
| 1545 TestDestroyedDuringViewsRelease(); | 1517 TestDestroyedDuringViewsRelease(); |
| 1546 } | 1518 } |
| 1547 | 1519 |
| 1548 #endif // defined(USE_AURA) | 1520 #endif // defined(USE_AURA) |
| 1549 | 1521 |
| 1550 } // namespace test | 1522 } // namespace test |
| 1551 } // namespace views | 1523 } // namespace views |
| OLD | NEW |