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 ~MenuItemViewTestBasic() override
{
24 // MenuTestBase implementation
25 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 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
)
65 // flaky on Windows - http://crbug.com/523255
66 #define MAYBE_SelectItem2 DISABLED_SelectItem2
68 #define MAYBE_SelectItem2 SelectItem2
70 VIEW_TEST(MenuItemViewTestBasic2
, MAYBE_SelectItem2
)
72 // Test class for inserting a menu item while the menu is open.
73 template<int INSERT_INDEX
, int SELECT_INDEX
>
74 class MenuItemViewTestInsert
: public MenuTestBase
{
76 MenuItemViewTestInsert() : inserted_item_(NULL
) {
79 ~MenuItemViewTestInsert() override
{
82 // MenuTestBase implementation
83 void BuildMenu(views::MenuItemView
* menu
) override
{
84 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
85 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
88 // Insert item at INSERT_INDEX and click item at SELECT_INDEX.
89 void DoTestWithMenuOpen() override
{
90 LOG(ERROR
) << "\nDoTestWithMenuOpen\n";
91 views::SubmenuView
* submenu
= menu()->GetSubmenu();
93 ASSERT_TRUE(submenu
->IsShowing());
94 ASSERT_EQ(2, submenu
->GetMenuItemCount());
96 inserted_item_
= menu()->AddMenuItemAt(INSERT_INDEX
,
98 ASCIIToUTF16("inserted item"),
102 views::MenuItemView::NORMAL
,
103 ui::NORMAL_SEPARATOR
);
104 ASSERT_TRUE(inserted_item_
);
105 menu()->ChildrenChanged();
107 // click an item and pass control to the next step
108 views::MenuItemView
* item
= submenu
->GetMenuItemAt(SELECT_INDEX
);
110 Click(item
, CreateEventTask(this, &MenuItemViewTestInsert::Step2
));
113 // Check clicked item and complete test.
115 LOG(ERROR
) << "\nStep2\n";
116 views::SubmenuView
* submenu
= menu()->GetSubmenu();
117 ASSERT_TRUE(submenu
);
118 ASSERT_FALSE(submenu
->IsShowing());
119 ASSERT_EQ(3, submenu
->GetMenuItemCount());
121 if (SELECT_INDEX
== INSERT_INDEX
)
122 ASSERT_EQ(1000, last_command());
123 else if (SELECT_INDEX
< INSERT_INDEX
)
124 ASSERT_EQ(SELECT_INDEX
+ 1, last_command());
126 ASSERT_EQ(SELECT_INDEX
, last_command());
128 LOG(ERROR
) << "\nDone\n";
133 views::MenuItemView
* inserted_item_
;
135 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestInsert
);
138 // MenuItemViewTestInsertXY inserts an item at index X and selects the
139 // item at index Y (after the insertion). The tests here cover
140 // inserting at the beginning, middle, and end, crossbarred with
141 // selecting the first and last item.
142 typedef MenuItemViewTestInsert
<0,0> MenuItemViewTestInsert00
;
143 typedef MenuItemViewTestInsert
<0,2> MenuItemViewTestInsert02
;
144 typedef MenuItemViewTestInsert
<1,0> MenuItemViewTestInsert10
;
145 typedef MenuItemViewTestInsert
<1,2> MenuItemViewTestInsert12
;
146 typedef MenuItemViewTestInsert
<2,0> MenuItemViewTestInsert20
;
147 typedef MenuItemViewTestInsert
<2,2> MenuItemViewTestInsert22
;
150 // flaky on Windows - http://crbug.com/523255
151 #define MAYBE_InsertItem00 DISABLED_InsertItem00
153 #define MAYBE_InsertItem00 InsertItem00
155 VIEW_TEST(MenuItemViewTestInsert00
, MAYBE_InsertItem00
)
157 VIEW_TEST(MenuItemViewTestInsert02
, InsertItem02
)
158 VIEW_TEST(MenuItemViewTestInsert10
, InsertItem10
)
161 // flaky on Windows - http://crbug.com/523255
162 #define MAYBE_InsertItem12 DISABLED_InsertItem12
164 #define MAYBE_InsertItem12 InsertItem12
166 VIEW_TEST(MenuItemViewTestInsert12
, MAYBE_InsertItem12
)
168 VIEW_TEST(MenuItemViewTestInsert20
, InsertItem20
)
171 // flaky on Windows - http://crbug.com/523255
172 #define MAYBE_InsertItem22 DISABLED_InsertItem22
174 #define MAYBE_InsertItem22 InsertItem22
176 VIEW_TEST(MenuItemViewTestInsert22
, MAYBE_InsertItem22
)
178 // Test class for inserting a menu item while a submenu is open.
179 template<int INSERT_INDEX
>
180 class MenuItemViewTestInsertWithSubmenu
: public MenuTestBase
{
182 MenuItemViewTestInsertWithSubmenu() :
184 inserted_item_(NULL
) {
187 ~MenuItemViewTestInsertWithSubmenu() override
{
190 // MenuTestBase implementation
191 void BuildMenu(views::MenuItemView
* menu
) override
{
192 submenu_
= menu
->AppendSubMenu(1, ASCIIToUTF16("My Submenu"));
193 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 1"));
194 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 2"));
195 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
199 void DoTestWithMenuOpen() override
{
201 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step2
));
204 // Insert item at INSERT_INDEX.
206 inserted_item_
= menu()->AddMenuItemAt(INSERT_INDEX
,
208 ASCIIToUTF16("inserted item"),
212 views::MenuItemView::NORMAL
,
213 ui::NORMAL_SEPARATOR
);
214 ASSERT_TRUE(inserted_item_
);
215 menu()->ChildrenChanged();
217 Click(inserted_item_
,
218 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step3
));
226 views::MenuItemView
* submenu_
;
227 views::MenuItemView
* inserted_item_
;
229 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestInsertWithSubmenu
);
232 // MenuItemViewTestInsertWithSubmenuX posts a menu and its submenu,
233 // then inserts an item in the top-level menu at X.
234 typedef MenuItemViewTestInsertWithSubmenu
<0> MenuItemViewTestInsertWithSubmenu0
;
235 typedef MenuItemViewTestInsertWithSubmenu
<1> MenuItemViewTestInsertWithSubmenu1
;
239 // flaky on Windows - http://crbug.com/523255
240 #define MAYBE_InsertItemWithSubmenu0 DISABLED_InsertItemWithSubmenu0
241 #define MAYBE_InsertItemWithSubmenu1 DISABLED_InsertItemWithSubmenu1
243 #define MAYBE_InsertItemWithSubmenu0 InsertItemWithSubmenu0
244 #define MAYBE_InsertItemWithSubmenu1 InsertItemWithSubmenu1
246 VIEW_TEST(MenuItemViewTestInsertWithSubmenu0
, MAYBE_InsertItemWithSubmenu0
)
247 VIEW_TEST(MenuItemViewTestInsertWithSubmenu1
, MAYBE_InsertItemWithSubmenu1
)
249 // Test class for removing a menu item while the menu is open.
250 template<int REMOVE_INDEX
, int SELECT_INDEX
>
251 class MenuItemViewTestRemove
: public MenuTestBase
{
253 MenuItemViewTestRemove() {
256 ~MenuItemViewTestRemove() override
{
259 // MenuTestBase implementation
260 void BuildMenu(views::MenuItemView
* menu
) override
{
261 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
262 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
263 menu
->AppendMenuItemWithLabel(3, ASCIIToUTF16("item 3"));
266 // Remove item at REMOVE_INDEX and click item at SELECT_INDEX.
267 void DoTestWithMenuOpen() override
{
268 views::SubmenuView
* submenu
= menu()->GetSubmenu();
269 ASSERT_TRUE(submenu
);
270 ASSERT_TRUE(submenu
->IsShowing());
271 ASSERT_EQ(3, submenu
->GetMenuItemCount());
274 menu()->RemoveMenuItemAt(REMOVE_INDEX
);
275 menu()->ChildrenChanged();
278 views::MenuItemView
* item
= submenu
->GetMenuItemAt(SELECT_INDEX
);
280 Click(item
, CreateEventTask(this, &MenuItemViewTestRemove::Step2
));
283 // Check clicked item and complete test.
285 views::SubmenuView
* submenu
= menu()->GetSubmenu();
286 ASSERT_TRUE(submenu
);
287 ASSERT_FALSE(submenu
->IsShowing());
288 ASSERT_EQ(2, submenu
->GetMenuItemCount());
290 if (SELECT_INDEX
< REMOVE_INDEX
)
291 ASSERT_EQ(SELECT_INDEX
+ 1, last_command());
293 ASSERT_EQ(SELECT_INDEX
+ 2, last_command());
299 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestRemove
);
302 typedef MenuItemViewTestRemove
<0,0> MenuItemViewTestRemove00
;
303 typedef MenuItemViewTestRemove
<0,1> MenuItemViewTestRemove01
;
304 typedef MenuItemViewTestRemove
<1,0> MenuItemViewTestRemove10
;
305 typedef MenuItemViewTestRemove
<1,1> MenuItemViewTestRemove11
;
306 typedef MenuItemViewTestRemove
<2,0> MenuItemViewTestRemove20
;
307 typedef MenuItemViewTestRemove
<2,1> MenuItemViewTestRemove21
;
308 VIEW_TEST(MenuItemViewTestRemove00
, RemoveItem00
)
309 VIEW_TEST(MenuItemViewTestRemove01
, RemoveItem01
)
312 // flaky on Windows - http://crbug.com/523255
313 #define MAYBE_RemoveItem10 DISABLED_RemoveItem10
315 #define MAYBE_RemoveItem10 RemoveItem10
317 VIEW_TEST(MenuItemViewTestRemove10
, MAYBE_RemoveItem10
)
319 VIEW_TEST(MenuItemViewTestRemove11
, RemoveItem11
)
322 // flaky on Windows - http://crbug.com/523255
323 #define MAYBE_RemoveItem20 DISABLED_RemoveItem20
325 #define MAYBE_RemoveItem20 RemoveItem20
327 VIEW_TEST(MenuItemViewTestRemove20
, MAYBE_RemoveItem20
)
329 VIEW_TEST(MenuItemViewTestRemove21
, RemoveItem21
)
331 // Test class for removing a menu item while a submenu is open.
332 template<int REMOVE_INDEX
>
333 class MenuItemViewTestRemoveWithSubmenu
: public MenuTestBase
{
335 MenuItemViewTestRemoveWithSubmenu() : submenu_(NULL
) {
338 ~MenuItemViewTestRemoveWithSubmenu() override
{
341 // MenuTestBase implementation
342 void BuildMenu(views::MenuItemView
* menu
) override
{
343 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
344 submenu_
= menu
->AppendSubMenu(2, ASCIIToUTF16("My Submenu"));
345 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 1"));
346 submenu_
->AppendMenuItemWithLabel(102, ASCIIToUTF16("submenu item 2"));
350 void DoTestWithMenuOpen() override
{
351 views::SubmenuView
* submenu
= menu()->GetSubmenu();
352 ASSERT_TRUE(submenu
);
353 ASSERT_TRUE(submenu
->IsShowing());
356 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step2
));
359 // Remove item at REMOVE_INDEX and press escape to exit the menu loop.
361 views::SubmenuView
* submenu
= menu()->GetSubmenu();
362 ASSERT_TRUE(submenu
);
363 ASSERT_TRUE(submenu
->IsShowing());
364 ASSERT_EQ(2, submenu
->GetMenuItemCount());
367 menu()->RemoveMenuItemAt(REMOVE_INDEX
);
368 menu()->ChildrenChanged();
371 KeyPress(ui::VKEY_ESCAPE
,
372 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step3
));
376 views::SubmenuView
* submenu
= menu()->GetSubmenu();
377 ASSERT_TRUE(submenu
);
378 ASSERT_FALSE(submenu
->IsShowing());
379 ASSERT_EQ(1, submenu
->GetMenuItemCount());
385 views::MenuItemView
* submenu_
;
387 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestRemoveWithSubmenu
);
390 typedef MenuItemViewTestRemoveWithSubmenu
<0> MenuItemViewTestRemoveWithSubmenu0
;
391 typedef MenuItemViewTestRemoveWithSubmenu
<1> MenuItemViewTestRemoveWithSubmenu1
;
393 #if defined(USE_OZONE) || defined(OS_WIN)
394 // ozone bringup - http://crbug.com/401304
395 // flaky on Windows - http://crbug.com/523255
396 #define MAYBE_RemoveItemWithSubmenu0 DISABLED_RemoveItemWithSubmenu0
397 #define MAYBE_RemoveItemWithSubmenu1 DISABLED_RemoveItemWithSubmenu1
399 #define MAYBE_RemoveItemWithSubmenu0 RemoveItemWithSubmenu0
400 #define MAYBE_RemoveItemWithSubmenu1 RemoveItemWithSubmenu1
402 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu0
, MAYBE_RemoveItemWithSubmenu0
)
403 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu1
, MAYBE_RemoveItemWithSubmenu1
)