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
);
57 // flaky on Windows - http://crbug.com/523255
58 #define MAYBE_SelectItem2 DISABLED_SelectItem2
59 #define MAYBE_SelectItem1 DISABLED_SelectItem1
60 #define MAYBE_SelectItem0 DISABLED_SelectItem0
62 #define MAYBE_SelectItem2 SelectItem2
63 #define MAYBE_SelectItem1 SelectItem1
64 #define MAYBE_SelectItem0 SelectItem0
67 // Click each item of a 3-item menu (with separator).
68 typedef MenuItemViewTestBasic
<0> MenuItemViewTestBasic0
;
69 typedef MenuItemViewTestBasic
<1> MenuItemViewTestBasic1
;
70 typedef MenuItemViewTestBasic
<2> MenuItemViewTestBasic2
;
71 VIEW_TEST(MenuItemViewTestBasic0
, MAYBE_SelectItem0
)
72 VIEW_TEST(MenuItemViewTestBasic1
, MAYBE_SelectItem1
)
74 VIEW_TEST(MenuItemViewTestBasic2
, MAYBE_SelectItem2
)
76 // Test class for inserting a menu item while the menu is open.
77 template<int INSERT_INDEX
, int SELECT_INDEX
>
78 class MenuItemViewTestInsert
: public MenuTestBase
{
80 MenuItemViewTestInsert() : inserted_item_(NULL
) {
83 ~MenuItemViewTestInsert() override
{
86 // MenuTestBase implementation
87 void BuildMenu(views::MenuItemView
* menu
) override
{
88 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
89 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
92 // Insert item at INSERT_INDEX and click item at SELECT_INDEX.
93 void DoTestWithMenuOpen() override
{
94 LOG(ERROR
) << "\nDoTestWithMenuOpen\n";
95 views::SubmenuView
* submenu
= menu()->GetSubmenu();
97 ASSERT_TRUE(submenu
->IsShowing());
98 ASSERT_EQ(2, submenu
->GetMenuItemCount());
100 inserted_item_
= menu()->AddMenuItemAt(INSERT_INDEX
,
102 ASCIIToUTF16("inserted item"),
106 views::MenuItemView::NORMAL
,
107 ui::NORMAL_SEPARATOR
);
108 ASSERT_TRUE(inserted_item_
);
109 menu()->ChildrenChanged();
111 // click an item and pass control to the next step
112 views::MenuItemView
* item
= submenu
->GetMenuItemAt(SELECT_INDEX
);
114 Click(item
, CreateEventTask(this, &MenuItemViewTestInsert::Step2
));
117 // Check clicked item and complete test.
119 LOG(ERROR
) << "\nStep2\n";
120 views::SubmenuView
* submenu
= menu()->GetSubmenu();
121 ASSERT_TRUE(submenu
);
122 ASSERT_FALSE(submenu
->IsShowing());
123 ASSERT_EQ(3, submenu
->GetMenuItemCount());
125 if (SELECT_INDEX
== INSERT_INDEX
)
126 ASSERT_EQ(1000, last_command());
127 else if (SELECT_INDEX
< INSERT_INDEX
)
128 ASSERT_EQ(SELECT_INDEX
+ 1, last_command());
130 ASSERT_EQ(SELECT_INDEX
, last_command());
132 LOG(ERROR
) << "\nDone\n";
137 views::MenuItemView
* inserted_item_
;
139 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestInsert
);
142 // MenuItemViewTestInsertXY inserts an item at index X and selects the
143 // item at index Y (after the insertion). The tests here cover
144 // inserting at the beginning, middle, and end, crossbarred with
145 // selecting the first and last item.
146 typedef MenuItemViewTestInsert
<0,0> MenuItemViewTestInsert00
;
147 typedef MenuItemViewTestInsert
<0,2> MenuItemViewTestInsert02
;
148 typedef MenuItemViewTestInsert
<1,0> MenuItemViewTestInsert10
;
149 typedef MenuItemViewTestInsert
<1,2> MenuItemViewTestInsert12
;
150 typedef MenuItemViewTestInsert
<2,0> MenuItemViewTestInsert20
;
151 typedef MenuItemViewTestInsert
<2,2> MenuItemViewTestInsert22
;
154 // flaky on Windows - http://crbug.com/523255
155 #define MAYBE_InsertItem00 DISABLED_InsertItem00
156 #define MAYBE_InsertItem02 DISABLED_InsertItem02
157 #define MAYBE_InsertItem10 DISABLED_InsertItem10
159 #define MAYBE_InsertItem00 InsertItem00
160 #define MAYBE_InsertItem02 InsertItem02
161 #define MAYBE_InsertItem10 InsertItem10
163 VIEW_TEST(MenuItemViewTestInsert00
, MAYBE_InsertItem00
)
165 VIEW_TEST(MenuItemViewTestInsert02
, MAYBE_InsertItem02
)
166 VIEW_TEST(MenuItemViewTestInsert10
, MAYBE_InsertItem10
)
169 // flaky on Windows - http://crbug.com/523255
170 #define MAYBE_InsertItem12 DISABLED_InsertItem12
171 #define MAYBE_InsertItem20 DISABLED_InsertItem20
173 #define MAYBE_InsertItem12 InsertItem12
174 #define MAYBE_InsertItem20 InsertItem20
176 VIEW_TEST(MenuItemViewTestInsert12
, MAYBE_InsertItem12
)
178 VIEW_TEST(MenuItemViewTestInsert20
, MAYBE_InsertItem20
)
181 // flaky on Windows - http://crbug.com/523255
182 #define MAYBE_InsertItem22 DISABLED_InsertItem22
184 #define MAYBE_InsertItem22 InsertItem22
186 VIEW_TEST(MenuItemViewTestInsert22
, MAYBE_InsertItem22
)
188 // Test class for inserting a menu item while a submenu is open.
189 template<int INSERT_INDEX
>
190 class MenuItemViewTestInsertWithSubmenu
: public MenuTestBase
{
192 MenuItemViewTestInsertWithSubmenu() :
194 inserted_item_(NULL
) {
197 ~MenuItemViewTestInsertWithSubmenu() override
{
200 // MenuTestBase implementation
201 void BuildMenu(views::MenuItemView
* menu
) override
{
202 submenu_
= menu
->AppendSubMenu(1, ASCIIToUTF16("My Submenu"));
203 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 1"));
204 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 2"));
205 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
209 void DoTestWithMenuOpen() override
{
211 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step2
));
214 // Insert item at INSERT_INDEX.
216 inserted_item_
= menu()->AddMenuItemAt(INSERT_INDEX
,
218 ASCIIToUTF16("inserted item"),
222 views::MenuItemView::NORMAL
,
223 ui::NORMAL_SEPARATOR
);
224 ASSERT_TRUE(inserted_item_
);
225 menu()->ChildrenChanged();
227 Click(inserted_item_
,
228 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step3
));
236 views::MenuItemView
* submenu_
;
237 views::MenuItemView
* inserted_item_
;
239 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestInsertWithSubmenu
);
242 // MenuItemViewTestInsertWithSubmenuX posts a menu and its submenu,
243 // then inserts an item in the top-level menu at X.
244 typedef MenuItemViewTestInsertWithSubmenu
<0> MenuItemViewTestInsertWithSubmenu0
;
245 typedef MenuItemViewTestInsertWithSubmenu
<1> MenuItemViewTestInsertWithSubmenu1
;
249 // flaky on Windows - http://crbug.com/523255
250 #define MAYBE_InsertItemWithSubmenu0 DISABLED_InsertItemWithSubmenu0
251 #define MAYBE_InsertItemWithSubmenu1 DISABLED_InsertItemWithSubmenu1
253 #define MAYBE_InsertItemWithSubmenu0 InsertItemWithSubmenu0
254 #define MAYBE_InsertItemWithSubmenu1 InsertItemWithSubmenu1
256 VIEW_TEST(MenuItemViewTestInsertWithSubmenu0
, MAYBE_InsertItemWithSubmenu0
)
257 VIEW_TEST(MenuItemViewTestInsertWithSubmenu1
, MAYBE_InsertItemWithSubmenu1
)
259 // Test class for removing a menu item while the menu is open.
260 template<int REMOVE_INDEX
, int SELECT_INDEX
>
261 class MenuItemViewTestRemove
: public MenuTestBase
{
263 MenuItemViewTestRemove() {
266 ~MenuItemViewTestRemove() override
{
269 // MenuTestBase implementation
270 void BuildMenu(views::MenuItemView
* menu
) override
{
271 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
272 menu
->AppendMenuItemWithLabel(2, ASCIIToUTF16("item 2"));
273 menu
->AppendMenuItemWithLabel(3, ASCIIToUTF16("item 3"));
276 // Remove item at REMOVE_INDEX and click item at SELECT_INDEX.
277 void DoTestWithMenuOpen() override
{
278 views::SubmenuView
* submenu
= menu()->GetSubmenu();
279 ASSERT_TRUE(submenu
);
280 ASSERT_TRUE(submenu
->IsShowing());
281 ASSERT_EQ(3, submenu
->GetMenuItemCount());
284 menu()->RemoveMenuItemAt(REMOVE_INDEX
);
285 menu()->ChildrenChanged();
288 views::MenuItemView
* item
= submenu
->GetMenuItemAt(SELECT_INDEX
);
290 Click(item
, CreateEventTask(this, &MenuItemViewTestRemove::Step2
));
293 // Check clicked item and complete test.
295 views::SubmenuView
* submenu
= menu()->GetSubmenu();
296 ASSERT_TRUE(submenu
);
297 ASSERT_FALSE(submenu
->IsShowing());
298 ASSERT_EQ(2, submenu
->GetMenuItemCount());
300 if (SELECT_INDEX
< REMOVE_INDEX
)
301 ASSERT_EQ(SELECT_INDEX
+ 1, last_command());
303 ASSERT_EQ(SELECT_INDEX
+ 2, last_command());
309 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestRemove
);
312 typedef MenuItemViewTestRemove
<0,0> MenuItemViewTestRemove00
;
313 typedef MenuItemViewTestRemove
<0,1> MenuItemViewTestRemove01
;
314 typedef MenuItemViewTestRemove
<1,0> MenuItemViewTestRemove10
;
315 typedef MenuItemViewTestRemove
<1,1> MenuItemViewTestRemove11
;
316 typedef MenuItemViewTestRemove
<2,0> MenuItemViewTestRemove20
;
317 typedef MenuItemViewTestRemove
<2,1> MenuItemViewTestRemove21
;
319 // flaky on Windows - http://crbug.com/523255
320 #define MAYBE_RemoveItem00 DISABLED_RemoveItem00
321 #define MAYBE_RemoveItem01 DISABLED_RemoveItem01
323 #define MAYBE_RemoveItem00 RemoveItem00
324 #define MAYBE_RemoveItem01 RemoveItem01
326 VIEW_TEST(MenuItemViewTestRemove00
, MAYBE_RemoveItem00
)
327 VIEW_TEST(MenuItemViewTestRemove01
, MAYBE_RemoveItem01
)
330 // flaky on Windows - http://crbug.com/523255
331 #define MAYBE_RemoveItem10 DISABLED_RemoveItem10
333 #define MAYBE_RemoveItem10 RemoveItem10
335 VIEW_TEST(MenuItemViewTestRemove10
, MAYBE_RemoveItem10
)
338 // flaky on Windows - http://crbug.com/523255
339 #define MAYBE_RemoveItem11 DISABLED_RemoveItem11
341 #define MAYBE_RemoveItem11 RemoveItem11
343 VIEW_TEST(MenuItemViewTestRemove11
, MAYBE_RemoveItem11
)
346 // flaky on Windows - http://crbug.com/523255
347 #define MAYBE_RemoveItem20 DISABLED_RemoveItem20
348 #define MAYBE_RemoveItem21 DISABLED_RemoveItem21
350 #define MAYBE_RemoveItem20 RemoveItem20
351 #define MAYBE_RemoveItem21 RemoveItem21
353 VIEW_TEST(MenuItemViewTestRemove20
, MAYBE_RemoveItem20
)
355 VIEW_TEST(MenuItemViewTestRemove21
, MAYBE_RemoveItem21
)
357 // Test class for removing a menu item while a submenu is open.
358 template<int REMOVE_INDEX
>
359 class MenuItemViewTestRemoveWithSubmenu
: public MenuTestBase
{
361 MenuItemViewTestRemoveWithSubmenu() : submenu_(NULL
) {
364 ~MenuItemViewTestRemoveWithSubmenu() override
{
367 // MenuTestBase implementation
368 void BuildMenu(views::MenuItemView
* menu
) override
{
369 menu
->AppendMenuItemWithLabel(1, ASCIIToUTF16("item 1"));
370 submenu_
= menu
->AppendSubMenu(2, ASCIIToUTF16("My Submenu"));
371 submenu_
->AppendMenuItemWithLabel(101, ASCIIToUTF16("submenu item 1"));
372 submenu_
->AppendMenuItemWithLabel(102, ASCIIToUTF16("submenu item 2"));
376 void DoTestWithMenuOpen() override
{
377 views::SubmenuView
* submenu
= menu()->GetSubmenu();
378 ASSERT_TRUE(submenu
);
379 ASSERT_TRUE(submenu
->IsShowing());
382 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step2
));
385 // Remove item at REMOVE_INDEX and press escape to exit the menu loop.
387 views::SubmenuView
* submenu
= menu()->GetSubmenu();
388 ASSERT_TRUE(submenu
);
389 ASSERT_TRUE(submenu
->IsShowing());
390 ASSERT_EQ(2, submenu
->GetMenuItemCount());
393 menu()->RemoveMenuItemAt(REMOVE_INDEX
);
394 menu()->ChildrenChanged();
397 KeyPress(ui::VKEY_ESCAPE
,
398 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step3
));
402 views::SubmenuView
* submenu
= menu()->GetSubmenu();
403 ASSERT_TRUE(submenu
);
404 ASSERT_FALSE(submenu
->IsShowing());
405 ASSERT_EQ(1, submenu
->GetMenuItemCount());
411 views::MenuItemView
* submenu_
;
413 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestRemoveWithSubmenu
);
416 typedef MenuItemViewTestRemoveWithSubmenu
<0> MenuItemViewTestRemoveWithSubmenu0
;
417 typedef MenuItemViewTestRemoveWithSubmenu
<1> MenuItemViewTestRemoveWithSubmenu1
;
419 #if defined(USE_OZONE) || defined(OS_WIN)
420 // ozone bringup - http://crbug.com/401304
421 // flaky on Windows - http://crbug.com/523255
422 #define MAYBE_RemoveItemWithSubmenu0 DISABLED_RemoveItemWithSubmenu0
423 #define MAYBE_RemoveItemWithSubmenu1 DISABLED_RemoveItemWithSubmenu1
425 #define MAYBE_RemoveItemWithSubmenu0 RemoveItemWithSubmenu0
426 #define MAYBE_RemoveItemWithSubmenu1 RemoveItemWithSubmenu1
428 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu0
, MAYBE_RemoveItemWithSubmenu0
)
429 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu1
, MAYBE_RemoveItemWithSubmenu1
)