| 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" |
| 27 #include "ui/views/controls/menu/menu_item_view.h" | 28 #include "ui/views/controls/menu/menu_item_view.h" |
| 28 #include "ui/views/controls/menu/menu_message_loop.h" | 29 #include "ui/views/controls/menu/menu_message_loop.h" |
| 29 #include "ui/views/controls/menu/menu_scroll_view_container.h" | 30 #include "ui/views/controls/menu/menu_scroll_view_container.h" |
| 30 #include "ui/views/controls/menu/submenu_view.h" | 31 #include "ui/views/controls/menu/submenu_view.h" |
| 31 #include "ui/views/test/menu_test_utils.h" | 32 #include "ui/views/test/menu_test_utils.h" |
| 32 #include "ui/views/test/test_views_delegate.h" | 33 #include "ui/views/test/test_views_delegate.h" |
| 33 #include "ui/views/test/views_test_base.h" | 34 #include "ui/views/test/views_test_base.h" |
| 35 #include "ui/views/widget/root_view.h" |
| 34 | 36 |
| 35 #if defined(USE_AURA) | 37 #if defined(USE_AURA) |
| 36 #include "ui/aura/client/drag_drop_client.h" | 38 #include "ui/aura/client/drag_drop_client.h" |
| 37 #include "ui/aura/scoped_window_targeter.h" | 39 #include "ui/aura/scoped_window_targeter.h" |
| 38 #include "ui/aura/window.h" | 40 #include "ui/aura/window.h" |
| 39 #include "ui/views/controls/menu/menu_pre_target_handler.h" | 41 #include "ui/views/controls/menu/menu_pre_target_handler.h" |
| 40 #endif | 42 #endif |
| 41 | 43 |
| 42 #if defined(USE_X11) | 44 #if defined(USE_X11) |
| 43 #include <X11/Xlib.h> | 45 #include <X11/Xlib.h> |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 internal::MenuControllerDelegate* GetCurrentDelegate() { | 501 internal::MenuControllerDelegate* GetCurrentDelegate() { |
| 500 return menu_controller_->delegate_; | 502 return menu_controller_->delegate_; |
| 501 } | 503 } |
| 502 | 504 |
| 503 bool IsAsyncRun() { return menu_controller_->async_run_; } | 505 bool IsAsyncRun() { return menu_controller_->async_run_; } |
| 504 | 506 |
| 505 bool IsShowing() { return menu_controller_->showing_; } | 507 bool IsShowing() { return menu_controller_->showing_; } |
| 506 | 508 |
| 507 MenuHost* GetMenuHost(SubmenuView* submenu) { return submenu->host_; } | 509 MenuHost* GetMenuHost(SubmenuView* submenu) { return submenu->host_; } |
| 508 | 510 |
| 511 MenuHostRootView* CreateMenuHostRootView(MenuHost* host) { |
| 512 return static_cast<MenuHostRootView*>(host->CreateRootView()); |
| 513 } |
| 514 |
| 509 void MenuHostOnDragWillStart(MenuHost* host) { host->OnDragWillStart(); } | 515 void MenuHostOnDragWillStart(MenuHost* host) { host->OnDragWillStart(); } |
| 510 | 516 |
| 511 void MenuHostOnDragComplete(MenuHost* host) { host->OnDragComplete(); } | 517 void MenuHostOnDragComplete(MenuHost* host) { host->OnDragComplete(); } |
| 512 | 518 |
| 513 void SelectByChar(base::char16 character) { | 519 void SelectByChar(base::char16 character) { |
| 514 menu_controller_->SelectByChar(character); | 520 menu_controller_->SelectByChar(character); |
| 515 } | 521 } |
| 516 | 522 |
| 517 void SetDropMenuItem(MenuItemView* target, | 523 void SetDropMenuItem(MenuItemView* target, |
| 518 MenuDelegate::DropPosition position) { | 524 MenuDelegate::DropPosition position) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 | 606 |
| 601 void SetHotTrackedButton(CustomButton* hot_button) { | 607 void SetHotTrackedButton(CustomButton* hot_button) { |
| 602 menu_controller_->SetHotTrackedButton(hot_button); | 608 menu_controller_->SetHotTrackedButton(hot_button); |
| 603 } | 609 } |
| 604 | 610 |
| 605 void ExitMenuRun() { | 611 void ExitMenuRun() { |
| 606 menu_controller_->SetExitType(MenuController::ExitType::EXIT_OUTERMOST); | 612 menu_controller_->SetExitType(MenuController::ExitType::EXIT_OUTERMOST); |
| 607 menu_controller_->ExitMenuRun(); | 613 menu_controller_->ExitMenuRun(); |
| 608 } | 614 } |
| 609 | 615 |
| 610 private: | |
| 611 void DestroyMenuController() { | 616 void DestroyMenuController() { |
| 612 if (!menu_controller_) | 617 if (!menu_controller_) |
| 613 return; | 618 return; |
| 614 | 619 |
| 615 if (!owner_->IsClosed()) | 620 if (!owner_->IsClosed()) |
| 616 owner_->RemoveObserver(menu_controller_); | 621 owner_->RemoveObserver(menu_controller_); |
| 617 | 622 |
| 618 menu_controller_->showing_ = false; | 623 menu_controller_->showing_ = false; |
| 619 menu_controller_->owner_ = nullptr; | 624 menu_controller_->owner_ = nullptr; |
| 620 delete menu_controller_; | 625 delete menu_controller_; |
| 621 menu_controller_ = nullptr; | 626 menu_controller_ = nullptr; |
| 622 } | 627 } |
| 623 | 628 |
| 629 private: |
| 624 void Init() { | 630 void Init() { |
| 625 owner_.reset(new Widget); | 631 owner_.reset(new Widget); |
| 626 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 632 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 627 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 633 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 628 owner_->Init(params); | 634 owner_->Init(params); |
| 629 event_generator_.reset( | 635 event_generator_.reset( |
| 630 new ui::test::EventGenerator(owner_->GetNativeWindow())); | 636 new ui::test::EventGenerator(owner_->GetNativeWindow())); |
| 631 owner_->Show(); | 637 owner_->Show(); |
| 632 | 638 |
| 633 SetupMenuItem(); | 639 SetupMenuItem(); |
| (...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1185 | 1191 |
| 1186 SubmenuView* submenu = menu_item()->GetSubmenu(); | 1192 SubmenuView* submenu = menu_item()->GetSubmenu(); |
| 1187 submenu->ShowAt(owner(), menu_item()->bounds(), false); | 1193 submenu->ShowAt(owner(), menu_item()->bounds(), false); |
| 1188 MenuHost* host = GetMenuHost(submenu); | 1194 MenuHost* host = GetMenuHost(submenu); |
| 1189 MenuHostOnDragWillStart(host); | 1195 MenuHostOnDragWillStart(host); |
| 1190 submenu->Close(); | 1196 submenu->Close(); |
| 1191 DestroyMenuItem(); | 1197 DestroyMenuItem(); |
| 1192 MenuHostOnDragComplete(host); | 1198 MenuHostOnDragComplete(host); |
| 1193 } | 1199 } |
| 1194 | 1200 |
| 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 |
| 1195 // Tets that an asynchronous menu nested within an asynchronous menu closes both | 1223 // Tets that an asynchronous menu nested within an asynchronous menu closes both |
| 1196 // menus, and notifies both delegates. | 1224 // menus, and notifies both delegates. |
| 1197 TEST_F(MenuControllerTest, DoubleAsynchronousNested) { | 1225 TEST_F(MenuControllerTest, DoubleAsynchronousNested) { |
| 1198 MenuController* controller = menu_controller(); | 1226 MenuController* controller = menu_controller(); |
| 1199 TestMenuControllerDelegate* delegate = menu_controller_delegate(); | 1227 TestMenuControllerDelegate* delegate = menu_controller_delegate(); |
| 1200 std::unique_ptr<TestMenuControllerDelegate> nested_delegate( | 1228 std::unique_ptr<TestMenuControllerDelegate> nested_delegate( |
| 1201 new TestMenuControllerDelegate()); | 1229 new TestMenuControllerDelegate()); |
| 1202 | 1230 |
| 1203 ASSERT_FALSE(IsAsyncRun()); | 1231 ASSERT_FALSE(IsAsyncRun()); |
| 1204 // Sets the run created in SetUp | 1232 // Sets the run created in SetUp |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1514 controller->Run(owner(), nullptr, menu_item(), gfx::Rect(), | 1542 controller->Run(owner(), nullptr, menu_item(), gfx::Rect(), |
| 1515 MENU_ANCHOR_TOPLEFT, false, false, &mouse_event_flags); | 1543 MENU_ANCHOR_TOPLEFT, false, false, &mouse_event_flags); |
| 1516 EXPECT_EQ(run_result, nullptr); | 1544 EXPECT_EQ(run_result, nullptr); |
| 1517 TestDestroyedDuringViewsRelease(); | 1545 TestDestroyedDuringViewsRelease(); |
| 1518 } | 1546 } |
| 1519 | 1547 |
| 1520 #endif // defined(USE_AURA) | 1548 #endif // defined(USE_AURA) |
| 1521 | 1549 |
| 1522 } // namespace test | 1550 } // namespace test |
| 1523 } // namespace views | 1551 } // namespace views |
| OLD | NEW |