Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / views / menu_item_view_interactive_uitest.cc
blob73d9f9f8dca3b5a82e0a897e64212b4169f35da2
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 // 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)
64 #if defined(OS_WIN)
65 // flaky on Windows - http://crbug.com/523255
66 #define MAYBE_SelectItem2 DISABLED_SelectItem2
67 #else
68 #define MAYBE_SelectItem2 SelectItem2
69 #endif
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 {
75 public:
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();
92 ASSERT_TRUE(submenu);
93 ASSERT_TRUE(submenu->IsShowing());
94 ASSERT_EQ(2, submenu->GetMenuItemCount());
96 inserted_item_ = menu()->AddMenuItemAt(INSERT_INDEX,
97 1000,
98 ASCIIToUTF16("inserted item"),
99 base::string16(),
100 base::string16(),
101 gfx::ImageSkia(),
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);
109 ASSERT_TRUE(item);
110 Click(item, CreateEventTask(this, &MenuItemViewTestInsert::Step2));
113 // Check clicked item and complete test.
114 void Step2() {
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());
125 else
126 ASSERT_EQ(SELECT_INDEX, last_command());
128 LOG(ERROR) << "\nDone\n";
129 Done();
132 private:
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;
149 #if defined(OS_WIN)
150 // flaky on Windows - http://crbug.com/523255
151 #define MAYBE_InsertItem00 DISABLED_InsertItem00
152 #else
153 #define MAYBE_InsertItem00 InsertItem00
154 #endif
155 VIEW_TEST(MenuItemViewTestInsert00, MAYBE_InsertItem00)
157 VIEW_TEST(MenuItemViewTestInsert02, InsertItem02)
158 VIEW_TEST(MenuItemViewTestInsert10, InsertItem10)
160 #if defined(OS_WIN)
161 // flaky on Windows - http://crbug.com/523255
162 #define MAYBE_InsertItem12 DISABLED_InsertItem12
163 #else
164 #define MAYBE_InsertItem12 InsertItem12
165 #endif
166 VIEW_TEST(MenuItemViewTestInsert12, MAYBE_InsertItem12)
168 VIEW_TEST(MenuItemViewTestInsert20, InsertItem20)
170 #if defined(OS_WIN)
171 // flaky on Windows - http://crbug.com/523255
172 #define MAYBE_InsertItem22 DISABLED_InsertItem22
173 #else
174 #define MAYBE_InsertItem22 InsertItem22
175 #endif
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 {
181 public:
182 MenuItemViewTestInsertWithSubmenu() :
183 submenu_(NULL),
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"));
198 // Post submenu.
199 void DoTestWithMenuOpen() override {
200 Click(submenu_,
201 CreateEventTask(this, &MenuItemViewTestInsertWithSubmenu::Step2));
204 // Insert item at INSERT_INDEX.
205 void Step2() {
206 inserted_item_ = menu()->AddMenuItemAt(INSERT_INDEX,
207 1000,
208 ASCIIToUTF16("inserted item"),
209 base::string16(),
210 base::string16(),
211 gfx::ImageSkia(),
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));
221 void Step3() {
222 Done();
225 private:
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;
238 #if defined(OS_WIN)
239 // flaky on Windows - http://crbug.com/523255
240 #define MAYBE_InsertItemWithSubmenu0 DISABLED_InsertItemWithSubmenu0
241 #define MAYBE_InsertItemWithSubmenu1 DISABLED_InsertItemWithSubmenu1
242 #else
243 #define MAYBE_InsertItemWithSubmenu0 InsertItemWithSubmenu0
244 #define MAYBE_InsertItemWithSubmenu1 InsertItemWithSubmenu1
245 #endif
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 {
252 public:
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());
273 // remove
274 menu()->RemoveMenuItemAt(REMOVE_INDEX);
275 menu()->ChildrenChanged();
277 // click
278 views::MenuItemView* item = submenu->GetMenuItemAt(SELECT_INDEX);
279 ASSERT_TRUE(item);
280 Click(item, CreateEventTask(this, &MenuItemViewTestRemove::Step2));
283 // Check clicked item and complete test.
284 void Step2() {
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());
292 else
293 ASSERT_EQ(SELECT_INDEX + 2, last_command());
295 Done();
298 private:
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)
311 #if defined(OS_WIN)
312 // flaky on Windows - http://crbug.com/523255
313 #define MAYBE_RemoveItem10 DISABLED_RemoveItem10
314 #else
315 #define MAYBE_RemoveItem10 RemoveItem10
316 #endif
317 VIEW_TEST(MenuItemViewTestRemove10, MAYBE_RemoveItem10)
319 VIEW_TEST(MenuItemViewTestRemove11, RemoveItem11)
321 #if defined(OS_WIN)
322 // flaky on Windows - http://crbug.com/523255
323 #define MAYBE_RemoveItem20 DISABLED_RemoveItem20
324 #else
325 #define MAYBE_RemoveItem20 RemoveItem20
326 #endif
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 {
334 public:
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"));
349 // Post submenu.
350 void DoTestWithMenuOpen() override {
351 views::SubmenuView* submenu = menu()->GetSubmenu();
352 ASSERT_TRUE(submenu);
353 ASSERT_TRUE(submenu->IsShowing());
355 Click(submenu_,
356 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step2));
359 // Remove item at REMOVE_INDEX and press escape to exit the menu loop.
360 void Step2() {
361 views::SubmenuView* submenu = menu()->GetSubmenu();
362 ASSERT_TRUE(submenu);
363 ASSERT_TRUE(submenu->IsShowing());
364 ASSERT_EQ(2, submenu->GetMenuItemCount());
366 // remove
367 menu()->RemoveMenuItemAt(REMOVE_INDEX);
368 menu()->ChildrenChanged();
370 // click
371 KeyPress(ui::VKEY_ESCAPE,
372 CreateEventTask(this, &MenuItemViewTestRemoveWithSubmenu::Step3));
375 void Step3() {
376 views::SubmenuView* submenu = menu()->GetSubmenu();
377 ASSERT_TRUE(submenu);
378 ASSERT_FALSE(submenu->IsShowing());
379 ASSERT_EQ(1, submenu->GetMenuItemCount());
381 Done();
384 private:
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
398 #else
399 #define MAYBE_RemoveItemWithSubmenu0 RemoveItemWithSubmenu0
400 #define MAYBE_RemoveItemWithSubmenu1 RemoveItemWithSubmenu1
401 #endif
402 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu0, MAYBE_RemoveItemWithSubmenu0)
403 VIEW_TEST(MenuItemViewTestRemoveWithSubmenu1, MAYBE_RemoveItemWithSubmenu1)