| 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/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "ui/aura/scoped_window_targeter.h" | |
| 9 #include "ui/aura/window.h" | 8 #include "ui/aura/window.h" |
| 10 #include "ui/events/event_targeter.h" | |
| 11 #include "ui/events/platform/platform_event_source.h" | 9 #include "ui/events/platform/platform_event_source.h" |
| 12 #include "ui/views/controls/menu/menu_item_view.h" | |
| 13 #include "ui/views/test/views_test_base.h" | 10 #include "ui/views/test/views_test_base.h" |
| 14 #include "ui/wm/public/dispatcher_client.h" | 11 #include "ui/wm/public/dispatcher_client.h" |
| 15 | 12 |
| 16 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
| 17 #include "base/message_loop/message_pump_dispatcher.h" | 14 #include "base/message_loop/message_pump_dispatcher.h" |
| 18 #elif defined(USE_X11) | 15 #elif defined(USE_X11) |
| 19 #include <X11/Xlib.h> | 16 #include <X11/Xlib.h> |
| 20 #undef Bool | 17 #undef Bool |
| 21 #undef None | 18 #undef None |
| 22 #include "ui/events/test/events_test_utils_x11.h" | |
| 23 #endif | 19 #endif |
| 24 | 20 |
| 25 namespace views { | 21 namespace views { |
| 26 | 22 |
| 27 namespace { | 23 namespace { |
| 28 | 24 |
| 29 class TestMenuItemView : public MenuItemView { | |
| 30 public: | |
| 31 TestMenuItemView() : MenuItemView(NULL) {} | |
| 32 virtual ~TestMenuItemView() {} | |
| 33 | |
| 34 private: | |
| 35 DISALLOW_COPY_AND_ASSIGN(TestMenuItemView); | |
| 36 }; | |
| 37 | |
| 38 class TestPlatformEventSource : public ui::PlatformEventSource { | 25 class TestPlatformEventSource : public ui::PlatformEventSource { |
| 39 public: | 26 public: |
| 40 TestPlatformEventSource() {} | 27 TestPlatformEventSource() {} |
| 41 virtual ~TestPlatformEventSource() {} | 28 virtual ~TestPlatformEventSource() {} |
| 42 | 29 |
| 43 uint32_t Dispatch(const ui::PlatformEvent& event) { | 30 uint32_t Dispatch(const ui::PlatformEvent& event) { |
| 44 return DispatchEvent(event); | 31 return DispatchEvent(event); |
| 45 } | 32 } |
| 46 | 33 |
| 47 private: | 34 private: |
| 48 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); | 35 DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource); |
| 49 }; | 36 }; |
| 50 | 37 |
| 51 class TestNullTargeter : public ui::EventTargeter { | |
| 52 public: | |
| 53 TestNullTargeter() {} | |
| 54 virtual ~TestNullTargeter() {} | |
| 55 | |
| 56 virtual ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, | |
| 57 ui::Event* event) OVERRIDE { | |
| 58 return NULL; | |
| 59 } | |
| 60 | |
| 61 private: | |
| 62 DISALLOW_COPY_AND_ASSIGN(TestNullTargeter); | |
| 63 }; | |
| 64 | |
| 65 class TestDispatcherClient : public aura::client::DispatcherClient { | 38 class TestDispatcherClient : public aura::client::DispatcherClient { |
| 66 public: | 39 public: |
| 67 TestDispatcherClient() : dispatcher_(NULL) {} | 40 TestDispatcherClient() : dispatcher_(NULL) {} |
| 68 virtual ~TestDispatcherClient() {} | 41 virtual ~TestDispatcherClient() {} |
| 69 | 42 |
| 70 base::MessagePumpDispatcher* dispatcher() { | 43 base::MessagePumpDispatcher* dispatcher() { |
| 71 return dispatcher_; | 44 return dispatcher_; |
| 72 } | 45 } |
| 73 | 46 |
| 74 // aura::client::DispatcherClient: | 47 // aura::client::DispatcherClient: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 99 MenuControllerTest() : controller_(NULL) {} | 72 MenuControllerTest() : controller_(NULL) {} |
| 100 virtual ~MenuControllerTest() {} | 73 virtual ~MenuControllerTest() {} |
| 101 | 74 |
| 102 // Dispatches |count| number of items, each in a separate iteration of the | 75 // Dispatches |count| number of items, each in a separate iteration of the |
| 103 // message-loop, by posting a task. | 76 // message-loop, by posting a task. |
| 104 void Step3_DispatchEvents(int count) { | 77 void Step3_DispatchEvents(int count) { |
| 105 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 78 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 106 base::MessageLoop::ScopedNestableTaskAllower allow(loop); | 79 base::MessageLoop::ScopedNestableTaskAllower allow(loop); |
| 107 controller_->exit_type_ = MenuController::EXIT_ALL; | 80 controller_->exit_type_ = MenuController::EXIT_ALL; |
| 108 | 81 |
| 109 DispatchEvent(); | 82 #if defined(USE_X11) |
| 83 XEvent xevent; |
| 84 memset(&xevent, 0, sizeof(xevent)); |
| 85 event_source_.Dispatch(&xevent); |
| 86 #else |
| 87 MSG msg; |
| 88 memset(&msg, 0, sizeof(MSG)); |
| 89 dispatcher_client_.dispatcher()->Dispatch(msg); |
| 90 #endif |
| 91 |
| 110 if (count) { | 92 if (count) { |
| 111 base::MessageLoop::current()->PostTask( | 93 base::MessageLoop::current()->PostTask( |
| 112 FROM_HERE, | 94 FROM_HERE, |
| 113 base::Bind(&MenuControllerTest::Step3_DispatchEvents, | 95 base::Bind(&MenuControllerTest::Step3_DispatchEvents, |
| 114 base::Unretained(this), | 96 base::Unretained(this), |
| 115 count - 1)); | 97 count - 1)); |
| 116 } else { | 98 } else { |
| 117 EXPECT_TRUE(run_loop_->running()); | 99 EXPECT_TRUE(run_loop_->running()); |
| 118 run_loop_->Quit(); | 100 run_loop_->Quit(); |
| 119 } | 101 } |
| 120 } | 102 } |
| 121 | 103 |
| 122 // Runs a nested message-loop that does not involve the menu itself. | 104 // Runs a nested message-loop that does not involve the menu itself. |
| 123 void Step2_RunNestedLoop() { | 105 void Step2_RunNestedLoop() { |
| 124 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 106 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 125 base::MessageLoop::ScopedNestableTaskAllower allow(loop); | 107 base::MessageLoop::ScopedNestableTaskAllower allow(loop); |
| 126 base::MessageLoop::current()->PostTask( | 108 base::MessageLoop::current()->PostTask( |
| 127 FROM_HERE, | 109 FROM_HERE, |
| 128 base::Bind(&MenuControllerTest::Step3_DispatchEvents, | 110 base::Bind(&MenuControllerTest::Step3_DispatchEvents, |
| 129 base::Unretained(this), | 111 base::Unretained(this), |
| 130 3)); | 112 3)); |
| 131 run_loop_.reset(new base::RunLoop()); | 113 run_loop_.reset(new base::RunLoop()); |
| 132 run_loop_->Run(); | 114 run_loop_->Run(); |
| 133 } | 115 } |
| 134 | 116 |
| 135 void Step1_RunMenu() { | 117 void Step1_RunMenu() { |
| 118 Widget widget; |
| 119 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 120 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 121 widget.Init(params); |
| 122 widget.Show(); |
| 123 |
| 124 aura::client::SetDispatcherClient(widget.GetNativeWindow()->GetRootWindow(), |
| 125 &dispatcher_client_); |
| 126 |
| 127 controller_ = new MenuController(NULL, true, NULL); |
| 128 controller_->owner_ = &widget; |
| 136 base::MessageLoop::current()->PostTask( | 129 base::MessageLoop::current()->PostTask( |
| 137 FROM_HERE, | 130 FROM_HERE, |
| 138 base::Bind(&MenuControllerTest::Step2_RunNestedLoop, | 131 base::Bind(&MenuControllerTest::Step2_RunNestedLoop, |
| 139 base::Unretained(this))); | 132 base::Unretained(this))); |
| 140 scoped_ptr<Widget> owner(CreateOwnerWidget()); | |
| 141 RunMenu(owner.get()); | |
| 142 } | |
| 143 | |
| 144 scoped_ptr<Widget> CreateOwnerWidget() { | |
| 145 scoped_ptr<Widget> widget(new Widget); | |
| 146 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | |
| 147 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 148 widget->Init(params); | |
| 149 widget->Show(); | |
| 150 | |
| 151 aura::client::SetDispatcherClient( | |
| 152 widget->GetNativeWindow()->GetRootWindow(), &dispatcher_client_); | |
| 153 return widget.Pass(); | |
| 154 } | |
| 155 | |
| 156 void RunMenu(views::Widget* owner) { | |
| 157 scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView); | |
| 158 controller_ = new MenuController(NULL, true, NULL); | |
| 159 controller_->owner_ = owner; | |
| 160 controller_->showing_ = true; | |
| 161 controller_->SetSelection(menu_item.get(), | |
| 162 MenuController::SELECTION_UPDATE_IMMEDIATELY); | |
| 163 controller_->RunMessageLoop(false); | 133 controller_->RunMessageLoop(false); |
| 164 } | 134 } |
| 165 | 135 |
| 166 #if defined(USE_X11) | |
| 167 void DispatchEscapeAndExpect(MenuController::ExitType exit_type) { | |
| 168 ui::ScopedXI2Event key_event; | |
| 169 key_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0); | |
| 170 event_source_.Dispatch(key_event); | |
| 171 EXPECT_EQ(exit_type, controller_->exit_type()); | |
| 172 controller_->exit_type_ = MenuController::EXIT_ALL; | |
| 173 DispatchEvent(); | |
| 174 } | |
| 175 #endif | |
| 176 | |
| 177 void DispatchEvent() { | |
| 178 #if defined(USE_X11) | |
| 179 XEvent xevent; | |
| 180 memset(&xevent, 0, sizeof(xevent)); | |
| 181 event_source_.Dispatch(&xevent); | |
| 182 #else | |
| 183 MSG msg; | |
| 184 memset(&msg, 0, sizeof(MSG)); | |
| 185 dispatcher_client_.dispatcher()->Dispatch(msg); | |
| 186 #endif | |
| 187 } | |
| 188 | |
| 189 private: | 136 private: |
| 190 MenuController* controller_; | 137 MenuController* controller_; |
| 191 scoped_ptr<base::RunLoop> run_loop_; | 138 scoped_ptr<base::RunLoop> run_loop_; |
| 192 TestPlatformEventSource event_source_; | 139 TestPlatformEventSource event_source_; |
| 193 TestDispatcherClient dispatcher_client_; | 140 TestDispatcherClient dispatcher_client_; |
| 194 | 141 |
| 195 DISALLOW_COPY_AND_ASSIGN(MenuControllerTest); | 142 DISALLOW_COPY_AND_ASSIGN(MenuControllerTest); |
| 196 }; | 143 }; |
| 197 | 144 |
| 198 TEST_F(MenuControllerTest, Basic) { | 145 TEST_F(MenuControllerTest, Basic) { |
| 199 base::MessageLoop::ScopedNestableTaskAllower allow_nested( | 146 base::MessageLoop::ScopedNestableTaskAllower allow_nested( |
| 200 base::MessageLoop::current()); | 147 base::MessageLoop::current()); |
| 201 message_loop()->PostTask( | 148 message_loop()->PostTask( |
| 202 FROM_HERE, | 149 FROM_HERE, |
| 203 base::Bind(&MenuControllerTest::Step1_RunMenu, base::Unretained(this))); | 150 base::Bind(&MenuControllerTest::Step1_RunMenu, base::Unretained(this))); |
| 204 } | 151 } |
| 205 | 152 |
| 206 #if defined(OS_LINUX) && defined(USE_X11) | |
| 207 // Tests that an event targeter which blocks events will be honored by the menu | |
| 208 // event dispatcher. | |
| 209 TEST_F(MenuControllerTest, EventTargeter) { | |
| 210 { | |
| 211 // Verify that the menu handles the escape key under normal circumstances. | |
| 212 scoped_ptr<Widget> owner(CreateOwnerWidget()); | |
| 213 message_loop()->PostTask( | |
| 214 FROM_HERE, | |
| 215 base::Bind(&MenuControllerTest::DispatchEscapeAndExpect, | |
| 216 base::Unretained(this), | |
| 217 MenuController::EXIT_OUTERMOST)); | |
| 218 RunMenu(owner.get()); | |
| 219 } | |
| 220 | |
| 221 { | |
| 222 // With the NULL targeter instantiated and assigned we expect the menu to | |
| 223 // not handle the key event. | |
| 224 scoped_ptr<Widget> owner(CreateOwnerWidget()); | |
| 225 aura::ScopedWindowTargeter scoped_targeter( | |
| 226 owner->GetNativeWindow()->GetRootWindow(), | |
| 227 scoped_ptr<ui::EventTargeter>(new TestNullTargeter)); | |
| 228 message_loop()->PostTask( | |
| 229 FROM_HERE, | |
| 230 base::Bind(&MenuControllerTest::DispatchEscapeAndExpect, | |
| 231 base::Unretained(this), | |
| 232 MenuController::EXIT_NONE)); | |
| 233 RunMenu(owner.get()); | |
| 234 } | |
| 235 } | |
| 236 #endif | |
| 237 | |
| 238 } // namespace views | 153 } // namespace views |
| OLD | NEW |