MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / chrome / browser / ui / views / menu_item_view_interactive_uitest.cc
blob00160be5904cf9f4b6abb26bf8dcf89db1cb128e
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 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();
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 // 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 {
67 public:
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();
83 ASSERT_TRUE(submenu);
84 ASSERT_TRUE(submenu->IsShowing());
85 ASSERT_EQ(2, submenu->GetMenuItemCount());
87 inserted_item_ = menu()->AddMenuItemAt(INSERT_INDEX,
88 1000,
89 ASCIIToUTF16("inserted item"),
90 base::string16(),
91 base::string16(),
92 gfx::ImageSkia(),
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);
100 ASSERT_TRUE(item);
101 Click(item, CreateEventTask(this, &MenuItemViewTestInsert::Step2));
104 // Check clicked item and complete test.
105 void Step2() {
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());
115 else
116 ASSERT_EQ(SELECT_INDEX, last_command());
118 Done();
121 private:
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 {
147 public:
148 MenuItemViewTestInsertWithSubmenu() :
149 submenu_(NULL),
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"));
164 // Post submenu.
165 virtual void DoTestWithMenuOpen() override {
166 Click(submenu_,
167 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step2));
170 // Insert item at INSERT_INDEX.
171 void Step2() {
172 inserted_item_ = menu()->AddMenuItemAt(INSERT_INDEX,
173 1000,
174 ASCIIToUTF16("inserted item"),
175 base::string16(),
176 base::string16(),
177 gfx::ImageSkia(),
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));
187 void Step3() {
188 Done();
191 private:
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 {
208 public:
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());
229 // remove
230 menu()->RemoveMenuItemAt(REMOVE_INDEX);
231 menu()->ChildrenChanged();
233 // click
234 views::MenuItemView* item = submenu->GetMenuItemAt(SELECT_INDEX);
235 ASSERT_TRUE(item);
236 Click(item, CreateEventTask(this, &MenuItemViewTestRemove::Step2));
239 // Check clicked item and complete test.
240 void Step2() {
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());
248 else
249 ASSERT_EQ(SELECT_INDEX + 2, last_command());
251 Done();
254 private:
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 {
274 public:
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"));
289 // Post submenu.
290 virtual void DoTestWithMenuOpen() override {
291 views::SubmenuView* submenu = menu()->GetSubmenu();
292 ASSERT_TRUE(submenu);
293 ASSERT_TRUE(submenu->IsShowing());
295 Click(submenu_,
296 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step2));
299 // Remove item at REMOVE_INDEX and press escape to exit the menu loop.
300 void Step2() {
301 views::SubmenuView* submenu = menu()->GetSubmenu();
302 ASSERT_TRUE(submenu);
303 ASSERT_TRUE(submenu->IsShowing());
304 ASSERT_EQ(2, submenu->GetMenuItemCount());
306 // remove
307 menu()->RemoveMenuItemAt(REMOVE_INDEX);
308 menu()->ChildrenChanged();
310 // click
311 KeyPress(ui::VKEY_ESCAPE,
312 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step3));
315 void Step3() {
316 views::SubmenuView* submenu = menu()->GetSubmenu();
317 ASSERT_TRUE(submenu);
318 ASSERT_FALSE(submenu->IsShowing());
319 ASSERT_EQ(1, submenu->GetMenuItemCount());
321 Done();
324 private:
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
337 #else
338 #define MAYBE_RemoveItemWithSubmenu0 RemoveItemWithSubmenu0
339 #define MAYBE_RemoveItemWithSubmenu1 RemoveItemWithSubmenu1
340 #endif
342 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu0, MAYBE_RemoveItemWithSubmenu0)
343 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu1, MAYBE_RemoveItemWithSubmenu1)