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
3 // found in the LICENSE file.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/ui/views/menu_test_base.h"
7 #include "ui/views/controls/menu/menu_item_view.h"
8 #include "ui/views/controls/menu/submenu_view.h"
10 using base::ASCIIToUTF16
;
12 // Simple test for clicking a menu item. This template class clicks on an
13 // item and checks that the returned id matches. The index of the item
14 // is the template argument.
16 class MenuItemViewTestBasic
: public MenuTestBase
{
18 MenuItemViewTestBasic() {
21 virtual ~MenuItemViewTestBasic() {
24 // MenuTestBase implementation
25 virtual void BuildMenu(views::MenuItemView
* menu
) override
{
26 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
27 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
28 menu
->AppendSeparator();
29 menu
->AppendMenuItemWithLabel(3, ASCIIToUTF16("item 3"));
32 // Click on item INDEX.
33 virtual void DoTestWithMenuOpen() override
{
34 views::SubmenuView
* submenu
= menu()->GetSubmenu();
36 ASSERT_TRUE(submenu
->IsShowing());
37 ASSERT_EQ(3, submenu
->GetMenuItemCount());
39 // click an item and pass control to the next step
40 views::MenuItemView
* item
= submenu
->GetMenuItemAt(INDEX
);
42 Click(item
, CreateEventTask(this, &MenuItemViewTestBasic::Step2
));
45 // Check the clicked item and complete the test.
47 ASSERT_FALSE(menu()->GetSubmenu()->IsShowing());
48 ASSERT_EQ(INDEX
+ 1, last_command());
53 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestBasic
);
56 // Click each item of a 3-item menu (with separator).
57 typedef MenuItemViewTestBasic
<0> MenuItemViewTestBasic0
;
58 typedef MenuItemViewTestBasic
<1> MenuItemViewTestBasic1
;
59 typedef MenuItemViewTestBasic
<2> MenuItemViewTestBasic2
;
60 VIEW_TEST(MenuItemViewTestBasic0
, SelectItem0
)
61 VIEW_TEST(MenuItemViewTestBasic1
, SelectItem1
)
62 VIEW_TEST(MenuItemViewTestBasic2
, SelectItem2
)
64 // Test class for inserting a menu item while the menu is open.
65 template<int INSERT_INDEX
, int SELECT_INDEX
>
66 class MenuItemViewTestInsert
: public MenuTestBase
{
68 MenuItemViewTestInsert() : inserted_item_(NULL
) {
71 virtual ~MenuItemViewTestInsert() {
74 // MenuTestBase implementation
75 virtual void BuildMenu(views::MenuItemView
* menu
) override
{
76 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
77 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
80 // Insert item at INSERT_INDEX and click item at SELECT_INDEX.
81 virtual void DoTestWithMenuOpen() override
{
82 views::SubmenuView
* submenu
= menu()->GetSubmenu();
84 ASSERT_TRUE(submenu
->IsShowing());
85 ASSERT_EQ(2, submenu
->GetMenuItemCount());
87 inserted_item_
= menu()->AddMenuItemAt(INSERT_INDEX
,
89 ASCIIToUTF16("inserted item"),
93 views::MenuItemView::NORMAL
,
94 ui::NORMAL_SEPARATOR
);
95 ASSERT_TRUE(inserted_item_
);
96 menu()->ChildrenChanged();
98 // click an item and pass control to the next step
99 views::MenuItemView
* item
= submenu
->GetMenuItemAt(SELECT_INDEX
);
101 Click(item
, CreateEventTask(this, &MenuItemViewTestInsert::Step2
));
104 // Check clicked item and complete test.
106 views::SubmenuView
* submenu
= menu()->GetSubmenu();
107 ASSERT_TRUE(submenu
);
108 ASSERT_FALSE(submenu
->IsShowing());
109 ASSERT_EQ(3, submenu
->GetMenuItemCount());
111 if (SELECT_INDEX
== INSERT_INDEX
)
112 ASSERT_EQ(1000, last_command());
113 else if (SELECT_INDEX
< INSERT_INDEX
)
114 ASSERT_EQ(SELECT_INDEX
+ 1, last_command());
116 ASSERT_EQ(SELECT_INDEX
, last_command());
122 views::MenuItemView
* inserted_item_
;
124 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestInsert
);
127 // MenuItemViewTestInsertXY inserts an item at index X and selects the
128 // item at index Y (after the insertion). The tests here cover
129 // inserting at the beginning, middle, and end, crossbarred with
130 // selecting the first and last item.
131 typedef MenuItemViewTestInsert
<0,0> MenuItemViewTestInsert00
;
132 typedef MenuItemViewTestInsert
<0,2> MenuItemViewTestInsert02
;
133 typedef MenuItemViewTestInsert
<1,0> MenuItemViewTestInsert10
;
134 typedef MenuItemViewTestInsert
<1,2> MenuItemViewTestInsert12
;
135 typedef MenuItemViewTestInsert
<2,0> MenuItemViewTestInsert20
;
136 typedef MenuItemViewTestInsert
<2,2> MenuItemViewTestInsert22
;
137 VIEW_TEST(MenuItemViewTestInsert00
, InsertItem00
)
138 VIEW_TEST(MenuItemViewTestInsert02
, InsertItem02
)
139 VIEW_TEST(MenuItemViewTestInsert10
, InsertItem10
)
140 VIEW_TEST(MenuItemViewTestInsert12
, InsertItem12
)
141 VIEW_TEST(MenuItemViewTestInsert20
, InsertItem20
)
142 VIEW_TEST(MenuItemViewTestInsert22
, InsertItem22
)
144 // Test class for inserting a menu item while a submenu is open.
145 template<int INSERT_INDEX
>
146 class MenuItemViewTestInsertWithSubmenu
: public MenuTestBase
{
148 MenuItemViewTestInsertWithSubmenu() :
150 inserted_item_(NULL
) {
153 virtual ~MenuItemViewTestInsertWithSubmenu() {
156 // MenuTestBase implementation
157 virtual void BuildMenu(views::MenuItemView
* menu
) override
{
158 submenu_
= menu
->AppendSubMenu(1, ASCIIToUTF16("My Submenu"));
159 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 1"));
160 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 2"));
161 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
165 virtual void DoTestWithMenuOpen() override
{
167 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step2
));
170 // Insert item at INSERT_INDEX.
172 inserted_item_
= menu()->AddMenuItemAt(INSERT_INDEX
,
174 ASCIIToUTF16("inserted item"),
178 views::MenuItemView::NORMAL
,
179 ui::NORMAL_SEPARATOR
);
180 ASSERT_TRUE(inserted_item_
);
181 menu()->ChildrenChanged();
183 Click(inserted_item_
,
184 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step3
));
192 views::MenuItemView
* submenu_
;
193 views::MenuItemView
* inserted_item_
;
195 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestInsertWithSubmenu
);
198 // MenuItemViewTestInsertWithSubmenuX posts a menu and its submenu,
199 // then inserts an item in the top-level menu at X.
200 typedef MenuItemViewTestInsertWithSubmenu
<0> MenuItemViewTestInsertWithSubmenu0
;
201 typedef MenuItemViewTestInsertWithSubmenu
<1> MenuItemViewTestInsertWithSubmenu1
;
202 VIEW_TEST(MenuItemViewTestInsertWithSubmenu0
, InsertItemWithSubmenu0
)
203 VIEW_TEST(MenuItemViewTestInsertWithSubmenu1
, InsertItemWithSubmenu1
)
205 // Test class for removing a menu item while the menu is open.
206 template<int REMOVE_INDEX
, int SELECT_INDEX
>
207 class MenuItemViewTestRemove
: public MenuTestBase
{
209 MenuItemViewTestRemove() {
212 virtual ~MenuItemViewTestRemove() {
215 // MenuTestBase implementation
216 virtual void BuildMenu(views::MenuItemView
* menu
) override
{
217 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
218 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
219 menu
->AppendMenuItemWithLabel(3, ASCIIToUTF16("item 3"));
222 // Remove item at REMOVE_INDEX and click item at SELECT_INDEX.
223 virtual void DoTestWithMenuOpen() override
{
224 views::SubmenuView
* submenu
= menu()->GetSubmenu();
225 ASSERT_TRUE(submenu
);
226 ASSERT_TRUE(submenu
->IsShowing());
227 ASSERT_EQ(3, submenu
->GetMenuItemCount());
230 menu()->RemoveMenuItemAt(REMOVE_INDEX
);
231 menu()->ChildrenChanged();
234 views::MenuItemView
* item
= submenu
->GetMenuItemAt(SELECT_INDEX
);
236 Click(item
, CreateEventTask(this, &MenuItemViewTestRemove::Step2
));
239 // Check clicked item and complete test.
241 views::SubmenuView
* submenu
= menu()->GetSubmenu();
242 ASSERT_TRUE(submenu
);
243 ASSERT_FALSE(submenu
->IsShowing());
244 ASSERT_EQ(2, submenu
->GetMenuItemCount());
246 if (SELECT_INDEX
< REMOVE_INDEX
)
247 ASSERT_EQ(SELECT_INDEX
+ 1, last_command());
249 ASSERT_EQ(SELECT_INDEX
+ 2, last_command());
255 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestRemove
);
258 typedef MenuItemViewTestRemove
<0,0> MenuItemViewTestRemove00
;
259 typedef MenuItemViewTestRemove
<0,1> MenuItemViewTestRemove01
;
260 typedef MenuItemViewTestRemove
<1,0> MenuItemViewTestRemove10
;
261 typedef MenuItemViewTestRemove
<1,1> MenuItemViewTestRemove11
;
262 typedef MenuItemViewTestRemove
<2,0> MenuItemViewTestRemove20
;
263 typedef MenuItemViewTestRemove
<2,1> MenuItemViewTestRemove21
;
264 VIEW_TEST(MenuItemViewTestRemove00
, RemoveItem00
)
265 VIEW_TEST(MenuItemViewTestRemove01
, RemoveItem01
)
266 VIEW_TEST(MenuItemViewTestRemove10
, RemoveItem10
)
267 VIEW_TEST(MenuItemViewTestRemove11
, RemoveItem11
)
268 VIEW_TEST(MenuItemViewTestRemove20
, RemoveItem20
)
269 VIEW_TEST(MenuItemViewTestRemove21
, RemoveItem21
)
271 // Test class for removing a menu item while a submenu is open.
272 template<int REMOVE_INDEX
>
273 class MenuItemViewTestRemoveWithSubmenu
: public MenuTestBase
{
275 MenuItemViewTestRemoveWithSubmenu() : submenu_(NULL
) {
278 virtual ~MenuItemViewTestRemoveWithSubmenu() {
281 // MenuTestBase implementation
282 virtual void BuildMenu(views::MenuItemView
* menu
) override
{
283 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
284 submenu_
= menu
->AppendSubMenu(2, ASCIIToUTF16("My Submenu"));
285 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 1"));
286 submenu_
->AppendMenuItemWithLabel(102, ASCIIToUTF16("submenu item 2"));
290 virtual void DoTestWithMenuOpen() override
{
291 views::SubmenuView
* submenu
= menu()->GetSubmenu();
292 ASSERT_TRUE(submenu
);
293 ASSERT_TRUE(submenu
->IsShowing());
296 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step2
));
299 // Remove item at REMOVE_INDEX and press escape to exit the menu loop.
301 views::SubmenuView
* submenu
= menu()->GetSubmenu();
302 ASSERT_TRUE(submenu
);
303 ASSERT_TRUE(submenu
->IsShowing());
304 ASSERT_EQ(2, submenu
->GetMenuItemCount());
307 menu()->RemoveMenuItemAt(REMOVE_INDEX
);
308 menu()->ChildrenChanged();
311 KeyPress(ui::VKEY_ESCAPE
,
312 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step3
));
316 views::SubmenuView
* submenu
= menu()->GetSubmenu();
317 ASSERT_TRUE(submenu
);
318 ASSERT_FALSE(submenu
->IsShowing());
319 ASSERT_EQ(1, submenu
->GetMenuItemCount());
325 views::MenuItemView
* submenu_
;
327 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestRemoveWithSubmenu
);
330 typedef MenuItemViewTestRemoveWithSubmenu
<0> MenuItemViewTestRemoveWithSubmenu0
;
331 typedef MenuItemViewTestRemoveWithSubmenu
<1> MenuItemViewTestRemoveWithSubmenu1
;
333 #if defined(USE_OZONE)
334 // ozone bringup - http://crbug.com/401304
335 #define MAYBE_RemoveItemWithSubmenu0 DISABLED_RemoveItemWithSubmenu0
336 #define MAYBE_RemoveItemWithSubmenu1 DISABLED_RemoveItemWithSubmenu1
338 #define MAYBE_RemoveItemWithSubmenu0 RemoveItemWithSubmenu0
339 #define MAYBE_RemoveItemWithSubmenu1 RemoveItemWithSubmenu1
342 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu0
, MAYBE_RemoveItemWithSubmenu0
)
343 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu1
, MAYBE_RemoveItemWithSubmenu1
)