Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / menu_item_view_interactive_uitest.cc
blob3509d17a4019db828c2c56f6ff33b955d9502f8b
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.
15 template<int INDEX>
16 class MenuItemViewTestBasic : public MenuTestBase {
17 public:
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();
35 ASSERT_TRUE(submenu);
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);
41 ASSERT_TRUE(item);
42 Click(item, CreateEventTask(this, &MenuItemViewTestBasic::Step2));
45 // Check the clicked item and complete the test.
46 void Step2() {
47 ASSERT_FALSE(menu()->GetSubmenu()->IsShowing());
48 ASSERT_EQ(INDEX + 1, last_command());
49 Done();
52 private:
53 DISALLOW_COPY_AND_ASSIGN(MenuItemViewTestBasic);
56 #if defined(OS_WIN)
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
61 #else
62 #define MAYBE_SelectItem2 SelectItem2
63 #define MAYBE_SelectItem1 SelectItem1
64 #define MAYBE_SelectItem0 SelectItem0
65 #endif
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 {
79 public:
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();
96 ASSERT_TRUE(submenu);
97 ASSERT_TRUE(submenu->IsShowing());
98 ASSERT_EQ(2, submenu->GetMenuItemCount());
100 inserted_item_ = menu()->AddMenuItemAt(INSERT_INDEX,
101 1000,
102 ASCIIToUTF16("inserted item"),
103 base::string16(),
104 base::string16(),
105 gfx::ImageSkia(),
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);
113 ASSERT_TRUE(item);
114 Click(item, CreateEventTask(this, &MenuItemViewTestInsert::Step2));
117 // Check clicked item and complete test.
118 void Step2() {
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());
129 else
130 ASSERT_EQ(SELECT_INDEX, last_command());
132 LOG(ERROR) << "\nDone\n";
133 Done();
136 private:
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;
153 #if defined(OS_WIN)
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
158 #else
159 #define MAYBE_InsertItem00 InsertItem00
160 #define MAYBE_InsertItem02 InsertItem02
161 #define MAYBE_InsertItem10 InsertItem10
162 #endif
163 VIEW_TEST(MenuItemViewTestInsert00, MAYBE_InsertItem00)
165 VIEW_TEST(MenuItemViewTestInsert02, MAYBE_InsertItem02)
166 VIEW_TEST(MenuItemViewTestInsert10, MAYBE_InsertItem10)
168 #if defined(OS_WIN)
169 // flaky on Windows - http://crbug.com/523255
170 #define MAYBE_InsertItem12 DISABLED_InsertItem12
171 #define MAYBE_InsertItem20 DISABLED_InsertItem20
172 #else
173 #define MAYBE_InsertItem12 InsertItem12
174 #define MAYBE_InsertItem20 InsertItem20
175 #endif
176 VIEW_TEST(MenuItemViewTestInsert12, MAYBE_InsertItem12)
178 VIEW_TEST(MenuItemViewTestInsert20, MAYBE_InsertItem20)
180 #if defined(OS_WIN)
181 // flaky on Windows - http://crbug.com/523255
182 #define MAYBE_InsertItem22 DISABLED_InsertItem22
183 #else
184 #define MAYBE_InsertItem22 InsertItem22
185 #endif
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 {
191 public:
192 MenuItemViewTestInsertWithSubmenu() :
193 submenu_(NULL),
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"));
208 // Post submenu.
209 void DoTestWithMenuOpen() override {
210 Click(submenu_,
211 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step2));
214 // Insert item at INSERT_INDEX.
215 void Step2() {
216 inserted_item_ = menu()->AddMenuItemAt(INSERT_INDEX,
217 1000,
218 ASCIIToUTF16("inserted item"),
219 base::string16(),
220 base::string16(),
221 gfx::ImageSkia(),
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));
231 void Step3() {
232 Done();
235 private:
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;
248 #if defined(OS_WIN)
249 // flaky on Windows - http://crbug.com/523255
250 #define MAYBE_InsertItemWithSubmenu0 DISABLED_InsertItemWithSubmenu0
251 #define MAYBE_InsertItemWithSubmenu1 DISABLED_InsertItemWithSubmenu1
252 #else
253 #define MAYBE_InsertItemWithSubmenu0 InsertItemWithSubmenu0
254 #define MAYBE_InsertItemWithSubmenu1 InsertItemWithSubmenu1
255 #endif
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 {
262 public:
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());
283 // remove
284 menu()->RemoveMenuItemAt(REMOVE_INDEX);
285 menu()->ChildrenChanged();
287 // click
288 views::MenuItemView* item = submenu->GetMenuItemAt(SELECT_INDEX);
289 ASSERT_TRUE(item);
290 Click(item, CreateEventTask(this, &MenuItemViewTestRemove::Step2));
293 // Check clicked item and complete test.
294 void Step2() {
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());
302 else
303 ASSERT_EQ(SELECT_INDEX + 2, last_command());
305 Done();
308 private:
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;
318 #if defined(OS_WIN)
319 // flaky on Windows - http://crbug.com/523255
320 #define MAYBE_RemoveItem00 DISABLED_RemoveItem00
321 #define MAYBE_RemoveItem01 DISABLED_RemoveItem01
322 #else
323 #define MAYBE_RemoveItem00 RemoveItem00
324 #define MAYBE_RemoveItem01 RemoveItem01
325 #endif
326 VIEW_TEST(MenuItemViewTestRemove00, MAYBE_RemoveItem00)
327 VIEW_TEST(MenuItemViewTestRemove01, MAYBE_RemoveItem01)
329 #if defined(OS_WIN)
330 // flaky on Windows - http://crbug.com/523255
331 #define MAYBE_RemoveItem10 DISABLED_RemoveItem10
332 #else
333 #define MAYBE_RemoveItem10 RemoveItem10
334 #endif
335 VIEW_TEST(MenuItemViewTestRemove10, MAYBE_RemoveItem10)
337 #if defined(OS_WIN)
338 // flaky on Windows - http://crbug.com/523255
339 #define MAYBE_RemoveItem11 DISABLED_RemoveItem11
340 #else
341 #define MAYBE_RemoveItem11 RemoveItem11
342 #endif
343 VIEW_TEST(MenuItemViewTestRemove11, MAYBE_RemoveItem11)
345 #if defined(OS_WIN)
346 // flaky on Windows - http://crbug.com/523255
347 #define MAYBE_RemoveItem20 DISABLED_RemoveItem20
348 #define MAYBE_RemoveItem21 DISABLED_RemoveItem21
349 #else
350 #define MAYBE_RemoveItem20 RemoveItem20
351 #define MAYBE_RemoveItem21 RemoveItem21
352 #endif
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 {
360 public:
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"));
375 // Post submenu.
376 void DoTestWithMenuOpen() override {
377 views::SubmenuView* submenu = menu()->GetSubmenu();
378 ASSERT_TRUE(submenu);
379 ASSERT_TRUE(submenu->IsShowing());
381 Click(submenu_,
382 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step2));
385 // Remove item at REMOVE_INDEX and press escape to exit the menu loop.
386 void Step2() {
387 views::SubmenuView* submenu = menu()->GetSubmenu();
388 ASSERT_TRUE(submenu);
389 ASSERT_TRUE(submenu->IsShowing());
390 ASSERT_EQ(2, submenu->GetMenuItemCount());
392 // remove
393 menu()->RemoveMenuItemAt(REMOVE_INDEX);
394 menu()->ChildrenChanged();
396 // click
397 KeyPress(ui::VKEY_ESCAPE,
398 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step3));
401 void Step3() {
402 views::SubmenuView* submenu = menu()->GetSubmenu();
403 ASSERT_TRUE(submenu);
404 ASSERT_FALSE(submenu->IsShowing());
405 ASSERT_EQ(1, submenu->GetMenuItemCount());
407 Done();
410 private:
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
424 #else
425 #define MAYBE_RemoveItemWithSubmenu0 RemoveItemWithSubmenu0
426 #define MAYBE_RemoveItemWithSubmenu1 RemoveItemWithSubmenu1
427 #endif
428 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu0, MAYBE_RemoveItemWithSubmenu0)
429 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu1, MAYBE_RemoveItemWithSubmenu1)