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 "base/strings/utf_string_conversions.h" |
8 #include "ui/aura/scoped_window_targeter.h" | 9 #include "ui/aura/scoped_window_targeter.h" |
9 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
10 #include "ui/events/event_handler.h" | 11 #include "ui/events/event_handler.h" |
11 #include "ui/events/null_event_targeter.h" | 12 #include "ui/events/null_event_targeter.h" |
12 #include "ui/events/platform/platform_event_source.h" | 13 #include "ui/events/platform/platform_event_source.h" |
13 #include "ui/views/controls/menu/menu_item_view.h" | 14 #include "ui/views/controls/menu/menu_item_view.h" |
| 15 #include "ui/views/controls/menu/submenu_view.h" |
14 #include "ui/views/test/views_test_base.h" | 16 #include "ui/views/test/views_test_base.h" |
15 #include "ui/wm/public/dispatcher_client.h" | 17 #include "ui/wm/public/dispatcher_client.h" |
16 | 18 |
17 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
18 #include "base/message_loop/message_pump_dispatcher.h" | 20 #include "base/message_loop/message_pump_dispatcher.h" |
19 #elif defined(USE_X11) | 21 #elif defined(USE_X11) |
20 #include <X11/Xlib.h> | 22 #include <X11/Xlib.h> |
21 #undef Bool | 23 #undef Bool |
22 #undef None | 24 #undef None |
23 #include "ui/events/devices/x11/device_data_manager_x11.h" | 25 #include "ui/events/devices/x11/device_data_manager_x11.h" |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 148 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
147 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 149 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
148 widget->Init(params); | 150 widget->Init(params); |
149 widget->Show(); | 151 widget->Show(); |
150 | 152 |
151 aura::client::SetDispatcherClient( | 153 aura::client::SetDispatcherClient( |
152 widget->GetNativeWindow()->GetRootWindow(), &dispatcher_client_); | 154 widget->GetNativeWindow()->GetRootWindow(), &dispatcher_client_); |
153 return widget.Pass(); | 155 return widget.Pass(); |
154 } | 156 } |
155 | 157 |
| 158 const MenuItemView* pending_state_item() const { |
| 159 return controller_->pending_state_.item; |
| 160 } |
| 161 |
| 162 void SetPendingStateItem(MenuItemView* item) { |
| 163 controller_->pending_state_.item = item; |
| 164 } |
| 165 |
| 166 void IncrementSelection(int delta) { |
| 167 controller_->IncrementSelection(delta); |
| 168 } |
| 169 |
| 170 MenuItemView* FindFirstSelectableMenuItem(MenuItemView* parent) { |
| 171 return controller_->FindFirstSelectableMenuItem(parent); |
| 172 } |
| 173 |
| 174 MenuItemView* FindNextSelectableMenuItem(MenuItemView* parent, |
| 175 int index, |
| 176 int delta) { |
| 177 return controller_->FindNextSelectableMenuItem(parent, index, delta); |
| 178 } |
| 179 void SetupMenu(views::Widget* owner, views::MenuItemView* item) { |
| 180 ResetMenuController(); |
| 181 controller_ = new MenuController(NULL, true, NULL); |
| 182 controller_->owner_ = owner; |
| 183 controller_->showing_ = true; |
| 184 controller_->SetSelection(item, |
| 185 MenuController::SELECTION_UPDATE_IMMEDIATELY); |
| 186 } |
| 187 |
156 void RunMenu(views::Widget* owner) { | 188 void RunMenu(views::Widget* owner) { |
157 scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView); | 189 scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView); |
158 ResetMenuController(); | 190 SetupMenu(owner, menu_item.get()); |
159 controller_ = new MenuController(NULL, true, NULL); | |
160 controller_->owner_ = owner; | |
161 controller_->showing_ = true; | |
162 controller_->SetSelection(menu_item.get(), | |
163 MenuController::SELECTION_UPDATE_IMMEDIATELY); | |
164 controller_->RunMessageLoop(false); | 191 controller_->RunMessageLoop(false); |
165 } | 192 } |
166 | 193 |
167 #if defined(USE_X11) | 194 #if defined(USE_X11) |
168 void DispatchEscapeAndExpect(MenuController::ExitType exit_type) { | 195 void DispatchEscapeAndExpect(MenuController::ExitType exit_type) { |
169 ui::ScopedXI2Event key_event; | 196 ui::ScopedXI2Event key_event; |
170 key_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0); | 197 key_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0); |
171 event_source_.Dispatch(key_event); | 198 event_source_.Dispatch(key_event); |
172 EXPECT_EQ(exit_type, controller_->exit_type()); | 199 EXPECT_EQ(exit_type, controller_->exit_type()); |
173 controller_->exit_type_ = MenuController::EXIT_ALL; | 200 controller_->exit_type_ = MenuController::EXIT_ALL; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 base::Unretained(this), MenuController::EXIT_OUTERMOST)); | 341 base::Unretained(this), MenuController::EXIT_OUTERMOST)); |
315 | 342 |
316 RunMenu(owner.get()); | 343 RunMenu(owner.get()); |
317 EXPECT_EQ(0, test_event_handler.outstanding_touches()); | 344 EXPECT_EQ(0, test_event_handler.outstanding_touches()); |
318 | 345 |
319 owner->GetNativeWindow()->GetRootWindow()->RemovePreTargetHandler( | 346 owner->GetNativeWindow()->GetRootWindow()->RemovePreTargetHandler( |
320 &test_event_handler); | 347 &test_event_handler); |
321 } | 348 } |
322 #endif // defined(USE_X11) | 349 #endif // defined(USE_X11) |
323 | 350 |
| 351 TEST_F(MenuControllerTest, FirstSelectedItem) { |
| 352 scoped_ptr<Widget> owner(CreateOwnerWidget()); |
| 353 scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView); |
| 354 menu_item->AppendMenuItemWithLabel(1, base::ASCIIToUTF16("One")); |
| 355 menu_item->AppendMenuItemWithLabel(2, base::ASCIIToUTF16("Two")); |
| 356 // Disabling the item "One" gets it skipped when a menu is first opened. |
| 357 menu_item->GetSubmenu()->GetMenuItemAt(0)->SetEnabled(false); |
| 358 |
| 359 SetupMenu(owner.get(), menu_item.get()); |
| 360 |
| 361 // First selectable item should be item "Two". |
| 362 MenuItemView* first_selectable = FindFirstSelectableMenuItem(menu_item.get()); |
| 363 EXPECT_EQ(2, first_selectable->GetCommand()); |
| 364 |
| 365 // There should be no next or previous selectable item since there is only a |
| 366 // single enabled item in the menu. |
| 367 SetPendingStateItem(first_selectable); |
| 368 EXPECT_EQ(NULL, FindNextSelectableMenuItem(menu_item.get(), 1, 1)); |
| 369 EXPECT_EQ(NULL, FindNextSelectableMenuItem(menu_item.get(), 1, -1)); |
| 370 } |
| 371 |
| 372 TEST_F(MenuControllerTest, NextSelectedItem) { |
| 373 scoped_ptr<Widget> owner(CreateOwnerWidget()); |
| 374 scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView); |
| 375 menu_item->AppendMenuItemWithLabel(1, base::ASCIIToUTF16("One")); |
| 376 menu_item->AppendMenuItemWithLabel(2, base::ASCIIToUTF16("Two")); |
| 377 menu_item->AppendMenuItemWithLabel(3, base::ASCIIToUTF16("Three")); |
| 378 menu_item->AppendMenuItemWithLabel(4, base::ASCIIToUTF16("Four")); |
| 379 // Disabling the item "Three" gets it skipped when using keyboard to navigate. |
| 380 menu_item->GetSubmenu()->GetMenuItemAt(2)->SetEnabled(false); |
| 381 |
| 382 SetupMenu(owner.get(), menu_item.get()); |
| 383 |
| 384 // Fake initial hot selection. |
| 385 SetPendingStateItem(menu_item->GetSubmenu()->GetMenuItemAt(0)); |
| 386 EXPECT_EQ(1, pending_state_item()->GetCommand()); |
| 387 |
| 388 // Move down in the menu. |
| 389 // Select next item. |
| 390 IncrementSelection(1); |
| 391 EXPECT_EQ(2, pending_state_item()->GetCommand()); |
| 392 |
| 393 // Skip disabled item. |
| 394 IncrementSelection(1); |
| 395 EXPECT_EQ(4, pending_state_item()->GetCommand()); |
| 396 |
| 397 // Wrap around. |
| 398 IncrementSelection(1); |
| 399 EXPECT_EQ(1, pending_state_item()->GetCommand()); |
| 400 |
| 401 // Move up in the menu. |
| 402 // Wrap around. |
| 403 IncrementSelection(-1); |
| 404 EXPECT_EQ(4, pending_state_item()->GetCommand()); |
| 405 |
| 406 // Skip disabled item. |
| 407 IncrementSelection(-1); |
| 408 EXPECT_EQ(2, pending_state_item()->GetCommand()); |
| 409 |
| 410 // Select previous item. |
| 411 IncrementSelection(-1); |
| 412 EXPECT_EQ(1, pending_state_item()->GetCommand()); |
| 413 } |
| 414 |
324 } // namespace views | 415 } // namespace views |
OLD | NEW |