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 void SetupMenu(views::Widget* owner, views::MenuItemView* item) { | |
171 ResetMenuController(); | |
172 controller_ = new MenuController(NULL, true, NULL); | |
173 controller_->owner_ = owner; | |
174 controller_->showing_ = true; | |
175 controller_->SetSelection(item, | |
176 MenuController::SELECTION_UPDATE_IMMEDIATELY); | |
177 } | |
178 | |
156 void RunMenu(views::Widget* owner) { | 179 void RunMenu(views::Widget* owner) { |
157 scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView); | 180 scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView); |
158 ResetMenuController(); | 181 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); | 182 controller_->RunMessageLoop(false); |
165 } | 183 } |
166 | 184 |
167 #if defined(USE_X11) | 185 #if defined(USE_X11) |
168 void DispatchEscapeAndExpect(MenuController::ExitType exit_type) { | 186 void DispatchEscapeAndExpect(MenuController::ExitType exit_type) { |
169 ui::ScopedXI2Event key_event; | 187 ui::ScopedXI2Event key_event; |
170 key_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0); | 188 key_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0); |
171 event_source_.Dispatch(key_event); | 189 event_source_.Dispatch(key_event); |
172 EXPECT_EQ(exit_type, controller_->exit_type()); | 190 EXPECT_EQ(exit_type, controller_->exit_type()); |
173 controller_->exit_type_ = MenuController::EXIT_ALL; | 191 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)); | 332 base::Unretained(this), MenuController::EXIT_OUTERMOST)); |
315 | 333 |
316 RunMenu(owner.get()); | 334 RunMenu(owner.get()); |
317 EXPECT_EQ(0, test_event_handler.outstanding_touches()); | 335 EXPECT_EQ(0, test_event_handler.outstanding_touches()); |
318 | 336 |
319 owner->GetNativeWindow()->GetRootWindow()->RemovePreTargetHandler( | 337 owner->GetNativeWindow()->GetRootWindow()->RemovePreTargetHandler( |
320 &test_event_handler); | 338 &test_event_handler); |
321 } | 339 } |
322 #endif // defined(USE_X11) | 340 #endif // defined(USE_X11) |
323 | 341 |
342 TEST_F(MenuControllerTest, NextSelectedItem) { | |
343 scoped_ptr<Widget> owner(CreateOwnerWidget()); | |
344 scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView); | |
345 menu_item->AppendMenuItemWithLabel(1, base::ASCIIToUTF16("One")); | |
346 menu_item->AppendMenuItemWithLabel(2, base::ASCIIToUTF16("Two")); | |
347 menu_item->AppendMenuItemWithLabel(3, base::ASCIIToUTF16("Three")); | |
348 menu_item->AppendMenuItemWithLabel(4, base::ASCIIToUTF16("Four")); | |
349 // Disabling the item "Two" gets it skipped when using keyboard to navigate. | |
sadrul
2015/05/21 13:44:28
Item "Three" is actually being disabled, right?
varkha
2015/05/21 16:33:12
Done. Yes, good eye! Forgot to fix this after copy
| |
350 menu_item->GetSubmenu()->GetMenuItemAt(2)->SetEnabled(false); | |
351 | |
352 SetupMenu(owner.get(), menu_item.get()); | |
353 | |
354 // Fake initial hot selection. | |
355 SetPendingStateItem(menu_item->GetSubmenu()->GetMenuItemAt(0)); | |
356 EXPECT_EQ(1, pending_state_item()->GetCommand()); | |
357 | |
358 // Move down in the menu. | |
359 // Select next item. | |
360 IncrementSelection(1); | |
361 EXPECT_EQ(2, pending_state_item()->GetCommand()); | |
362 | |
363 // Skip disabled item. | |
364 IncrementSelection(1); | |
365 EXPECT_EQ(4, pending_state_item()->GetCommand()); | |
366 | |
367 // Wrap around. | |
368 IncrementSelection(1); | |
369 EXPECT_EQ(1, pending_state_item()->GetCommand()); | |
370 | |
371 // Move up in the menu. | |
372 // Wrap around. | |
373 IncrementSelection(-1); | |
374 EXPECT_EQ(4, pending_state_item()->GetCommand()); | |
375 | |
376 // Skip disabled item. | |
377 IncrementSelection(-1); | |
378 EXPECT_EQ(2, pending_state_item()->GetCommand()); | |
379 | |
380 // Select previous item. | |
381 IncrementSelection(-1); | |
382 EXPECT_EQ(1, pending_state_item()->GetCommand()); | |
383 } | |
sadrul
2015/05/21 13:44:27
Would it be possible to add another test case wher
varkha
2015/05/21 16:33:12
Done.
| |
384 | |
324 } // namespace views | 385 } // namespace views |
OLD | NEW |