bookmark_bar_view_test.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2012 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.
4
5#include "base/bind.h"
6#include "base/callback.h"
7#include "base/compiler_specific.h"
8#include "base/prefs/pref_service.h"
9#include "base/strings/string_number_conversions.h"
10#include "base/strings/utf_string_conversions.h"
11#include "chrome/app/chrome_command_ids.h"
12#include "chrome/browser/bookmarks/bookmark_model.h"
13#include "chrome/browser/bookmarks/bookmark_model_factory.h"
14#include "chrome/browser/bookmarks/bookmark_test_helpers.h"
15#include "chrome/browser/chrome_notification_types.h"
16#include "chrome/browser/profiles/profile.h"
17#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
18#include "chrome/browser/ui/browser.h"
19#include "chrome/browser/ui/browser_tabstrip.h"
20#include "chrome/browser/ui/browser_window.h"
21#include "chrome/browser/ui/tabs/tab_strip_model.h"
22#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
23#include "chrome/common/pref_names.h"
24#include "chrome/test/base/interactive_test_utils.h"
25#include "chrome/test/base/scoped_testing_local_state.h"
26#include "chrome/test/base/test_browser_window.h"
27#include "chrome/test/base/testing_browser_process.h"
28#include "chrome/test/base/testing_profile.h"
29#include "chrome/test/base/ui_test_utils.h"
30#include "chrome/test/base/view_event_test_base.h"
31#include "content/public/browser/notification_service.h"
32#include "content/public/browser/page_navigator.h"
33#include "content/public/test/test_browser_thread.h"
34#include "grit/generated_resources.h"
35#include "ui/base/accessibility/accessibility_types.h"
36#include "ui/base/clipboard/clipboard.h"
37#include "ui/base/test/ui_controls.h"
38#include "ui/events/keycodes/keyboard_codes.h"
39#include "ui/views/controls/button/menu_button.h"
40#include "ui/views/controls/button/text_button.h"
41#include "ui/views/controls/menu/menu_controller.h"
42#include "ui/views/controls/menu/menu_item_view.h"
43#include "ui/views/controls/menu/submenu_view.h"
44#include "ui/views/widget/widget.h"
45
46using base::ASCIIToUTF16;
47using content::BrowserThread;
48using content::OpenURLParams;
49using content::PageNavigator;
50using content::WebContents;
51
52namespace {
53
54void MoveMouseAndPress(const gfx::Point& screen_pos,
55                       ui_controls::MouseButton button,
56                       int state,
57                       const base::Closure& closure) {
58  ui_controls::SendMouseMove(screen_pos.x(), screen_pos.y());
59  ui_controls::SendMouseEventsNotifyWhenDone(button, state, closure);
60}
61
62// PageNavigator implementation that records the URL.
63class TestingPageNavigator : public PageNavigator {
64 public:
65  virtual WebContents* OpenURL(const OpenURLParams& params) OVERRIDE {
66    url_ = params.url;
67    return NULL;
68  }
69
70  GURL url_;
71};
72
73// TODO(jschuh): Fix bookmark DND tests on Win64. crbug.com/244605
74// TODO(erg): Fix bookmark DBD tests on linux_aura. crbug.com/163931
75#if (defined(OS_WIN) && defined(ARCH_CPU_X86_64)) || \
76  (defined(OS_LINUX) && defined(USE_AURA))
77#define MAYBE(x) DISABLED_##x
78#else
79#define MAYBE(x) x
80#endif
81
82}  // namespace
83
84// Base class for event generating bookmark view tests. These test are intended
85// to exercise View's menus, but that's easier done with BookmarkBarView rather
86// than View's menu itself.
87//
88// SetUp creates a bookmark model with the following structure.
89// All folders are in upper case, all URLs in lower case.
90// F1
91//   f1a
92//   F11
93//     f11a
94//   *
95// a
96// b
97// c
98// d
99// F2
100// e
101// OTHER
102//   oa
103//   OF
104//     ofa
105//     ofb
106//   OF2
107//     of2a
108//     of2b
109//
110// * if CreateBigMenu returns return true, 100 menu items are created here with
111//   the names f1-f100.
112//
113// Subclasses should be sure and invoke super's implementation of SetUp and
114// TearDown.
115class BookmarkBarViewEventTestBase : public ViewEventTestBase {
116 public:
117  BookmarkBarViewEventTestBase()
118      : ViewEventTestBase(),
119        model_(NULL) {}
120
121  virtual void SetUp() OVERRIDE {
122    views::MenuController::TurnOffMenuSelectionHoldForTest();
123    BookmarkBarView::DisableAnimationsForTesting(true);
124
125    profile_.reset(new TestingProfile());
126    profile_->CreateBookmarkModel(true);
127    model_ = BookmarkModelFactory::GetForProfile(profile_.get());
128    test::WaitForBookmarkModelToLoad(model_);
129    profile_->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
130
131    Browser::CreateParams native_params(profile_.get(),
132                                        chrome::GetActiveDesktop());
133    browser_.reset(
134        chrome::CreateBrowserWithTestWindowForParams(&native_params));
135
136    local_state_.reset(new ScopedTestingLocalState(
137        TestingBrowserProcess::GetGlobal()));
138    model_->ClearStore();
139
140    bb_view_.reset(new BookmarkBarView(browser_.get(), NULL));
141    bb_view_->set_owned_by_client();
142    bb_view_->SetPageNavigator(&navigator_);
143
144    AddTestData(CreateBigMenu());
145
146    // Calculate the preferred size so that one button doesn't fit, which
147    // triggers the overflow button to appear.
148    //
149    // BookmarkBarView::Layout does nothing if the parent is NULL and
150    // GetPreferredSize hard codes a width of 1. For that reason we add the
151    // BookmarkBarView to a dumby view as the parent.
152    //
153    // This code looks a bit hacky, but I've written it so that it shouldn't
154    // be dependant upon any of the layout code in BookmarkBarView. Instead
155    // we brute force search for a size that triggers the overflow button.
156    views::View tmp_parent;
157
158    tmp_parent.AddChildView(bb_view_.get());
159
160    bb_view_pref_ = bb_view_->GetPreferredSize();
161    bb_view_pref_.set_width(1000);
162    views::TextButton* button = GetBookmarkButton(6);
163    while (button->visible()) {
164      bb_view_pref_.set_width(bb_view_pref_.width() - 25);
165      bb_view_->SetBounds(0, 0, bb_view_pref_.width(), bb_view_pref_.height());
166      bb_view_->Layout();
167    }
168
169    tmp_parent.RemoveChildView(bb_view_.get());
170
171    ViewEventTestBase::SetUp();
172  }
173
174  virtual void TearDown() {
175    // Destroy everything, then run the message loop to ensure we delete all
176    // Tasks and fully shut down.
177    browser_->tab_strip_model()->CloseAllTabs();
178    bb_view_.reset();
179    browser_.reset();
180    profile_.reset();
181
182    // Run the message loop to ensure we delete allTasks and fully shut down.
183    base::MessageLoop::current()->PostTask(FROM_HERE,
184                                           base::MessageLoop::QuitClosure());
185    base::MessageLoop::current()->Run();
186
187    ViewEventTestBase::TearDown();
188    BookmarkBarView::DisableAnimationsForTesting(false);
189  }
190
191 protected:
192  virtual views::View* CreateContentsView() OVERRIDE {
193    return bb_view_.get();
194  }
195
196  virtual gfx::Size GetPreferredSize() OVERRIDE { return bb_view_pref_; }
197
198  views::TextButton* GetBookmarkButton(int view_index) {
199    return bb_view_->GetBookmarkButton(view_index);
200  }
201
202  // See comment above class description for what this does.
203  virtual bool CreateBigMenu() { return false; }
204
205  BookmarkModel* model_;
206  scoped_ptr<BookmarkBarView> bb_view_;
207  TestingPageNavigator navigator_;
208
209 private:
210  void AddTestData(bool big_menu) {
211    const BookmarkNode* bb_node = model_->bookmark_bar_node();
212    std::string test_base = "file:///c:/tmp/";
213    const BookmarkNode* f1 = model_->AddFolder(bb_node, 0, ASCIIToUTF16("F1"));
214    model_->AddURL(f1, 0, ASCIIToUTF16("f1a"), GURL(test_base + "f1a"));
215    const BookmarkNode* f11 = model_->AddFolder(f1, 1, ASCIIToUTF16("F11"));
216    model_->AddURL(f11, 0, ASCIIToUTF16("f11a"), GURL(test_base + "f11a"));
217    if (big_menu) {
218      for (int i = 1; i <= 100; ++i) {
219        model_->AddURL(f1, i + 1, ASCIIToUTF16("f") + base::IntToString16(i),
220                       GURL(test_base + "f" + base::IntToString(i)));
221      }
222    }
223    model_->AddURL(bb_node, 1, ASCIIToUTF16("a"), GURL(test_base + "a"));
224    model_->AddURL(bb_node, 2, ASCIIToUTF16("b"), GURL(test_base + "b"));
225    model_->AddURL(bb_node, 3, ASCIIToUTF16("c"), GURL(test_base + "c"));
226    model_->AddURL(bb_node, 4, ASCIIToUTF16("d"), GURL(test_base + "d"));
227    model_->AddFolder(bb_node, 5, ASCIIToUTF16("F2"));
228    model_->AddURL(bb_node, 6, ASCIIToUTF16("d"), GURL(test_base + "d"));
229
230    model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("oa"),
231                   GURL(test_base + "oa"));
232    const BookmarkNode* of = model_->AddFolder(model_->other_node(), 1,
233                                               ASCIIToUTF16("OF"));
234    model_->AddURL(of, 0, ASCIIToUTF16("ofa"), GURL(test_base + "ofa"));
235    model_->AddURL(of, 1, ASCIIToUTF16("ofb"), GURL(test_base + "ofb"));
236    const BookmarkNode* of2 = model_->AddFolder(model_->other_node(), 2,
237                                                ASCIIToUTF16("OF2"));
238    model_->AddURL(of2, 0, ASCIIToUTF16("of2a"), GURL(test_base + "of2a"));
239    model_->AddURL(of2, 1, ASCIIToUTF16("of2b"), GURL(test_base + "of2b"));
240  }
241
242  gfx::Size bb_view_pref_;
243  scoped_ptr<TestingProfile> profile_;
244  scoped_ptr<Browser> browser_;
245  scoped_ptr<ScopedTestingLocalState> local_state_;
246};
247
248// Clicks on first menu, makes sure button is depressed. Moves mouse to first
249// child, clicks it and makes sure a navigation occurs.
250class BookmarkBarViewTest1 : public BookmarkBarViewEventTestBase {
251 protected:
252  virtual void DoTestOnMessageLoop() OVERRIDE {
253    // Move the mouse to the first folder on the bookmark bar and press the
254    // mouse.
255    views::TextButton* button = GetBookmarkButton(0);
256    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
257        ui_controls::DOWN | ui_controls::UP,
258        CreateEventTask(this, &BookmarkBarViewTest1::Step2));
259  }
260
261 private:
262  void Step2() {
263    // Menu should be showing.
264    views::MenuItemView* menu = bb_view_->GetMenu();
265    ASSERT_TRUE(menu != NULL);
266    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
267
268    // Button should be depressed.
269    views::TextButton* button = GetBookmarkButton(0);
270    ASSERT_TRUE(button->state() == views::CustomButton::STATE_PRESSED);
271
272    // Click on the 2nd menu item (A URL).
273    ASSERT_TRUE(menu->GetSubmenu());
274
275    views::MenuItemView* menu_to_select =
276        menu->GetSubmenu()->GetMenuItemAt(0);
277    ui_test_utils::MoveMouseToCenterAndPress(menu_to_select, ui_controls::LEFT,
278        ui_controls::DOWN | ui_controls::UP,
279        CreateEventTask(this, &BookmarkBarViewTest1::Step3));
280  }
281
282  void Step3() {
283    // We should have navigated to URL f1a.
284    ASSERT_TRUE(navigator_.url_ ==
285                model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url());
286
287    // Make sure button is no longer pushed.
288    views::TextButton* button = GetBookmarkButton(0);
289    ASSERT_TRUE(button->state() == views::CustomButton::STATE_NORMAL);
290
291    views::MenuItemView* menu = bb_view_->GetMenu();
292    ASSERT_TRUE(menu == NULL || !menu->GetSubmenu()->IsShowing());
293
294    Done();
295  }
296};
297
298VIEW_TEST(BookmarkBarViewTest1, Basic)
299
300// Brings up menu, clicks on empty space and make sure menu hides.
301class BookmarkBarViewTest2 : public BookmarkBarViewEventTestBase {
302 protected:
303  virtual void DoTestOnMessageLoop() OVERRIDE {
304    // Move the mouse to the first folder on the bookmark bar and press the
305    // mouse.
306    views::TextButton* button = GetBookmarkButton(0);
307    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
308        ui_controls::DOWN | ui_controls::UP,
309        CreateEventTask(this, &BookmarkBarViewTest2::Step2));
310  }
311
312 private:
313  void Step2() {
314    // Menu should be showing.
315    views::MenuItemView* menu = bb_view_->GetMenu();
316    ASSERT_TRUE(menu != NULL && menu->GetSubmenu()->IsShowing());
317
318    // Click on 0x0, which should trigger closing menu.
319    // NOTE: this code assume there is a left margin, which is currently
320    // true. If that changes, this code will need to find another empty space
321    // to press the mouse on.
322    gfx::Point mouse_loc;
323    views::View::ConvertPointToScreen(bb_view_.get(), &mouse_loc);
324    ui_controls::SendMouseMoveNotifyWhenDone(0, 0,
325        CreateEventTask(this, &BookmarkBarViewTest2::Step3));
326  }
327
328  void Step3() {
329    // As the click is on the desktop the hook never sees the up, so we only
330    // wait on the down. We still send the up though else the system thinks
331    // the mouse is still down.
332    ui_controls::SendMouseEventsNotifyWhenDone(
333        ui_controls::LEFT, ui_controls::DOWN,
334        CreateEventTask(this, &BookmarkBarViewTest2::Step4));
335    ui_controls::SendMouseEvents(ui_controls::LEFT, ui_controls::UP);
336  }
337
338  void Step4() {
339    // The menu shouldn't be showing.
340    views::MenuItemView* menu = bb_view_->GetMenu();
341    ASSERT_TRUE(menu == NULL || !menu->GetSubmenu()->IsShowing());
342
343    // Make sure button is no longer pushed.
344    views::TextButton* button = GetBookmarkButton(0);
345    ASSERT_TRUE(button->state() == views::CustomButton::STATE_NORMAL);
346
347    Done();
348  }
349};
350
351#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
352// TODO(erg): linux_aura bringup: http://crbug.com/163931
353#define MAYBE_HideOnDesktopClick DISABLED_HideOnDesktopClick
354#else
355#define MAYBE_HideOnDesktopClick HideOnDesktopClick
356#endif
357
358VIEW_TEST(BookmarkBarViewTest2, MAYBE_HideOnDesktopClick)
359
360// Brings up menu. Moves over child to make sure submenu appears, moves over
361// another child and make sure next menu appears.
362class BookmarkBarViewTest3 : public BookmarkBarViewEventTestBase {
363 protected:
364  virtual void DoTestOnMessageLoop() OVERRIDE {
365    // Move the mouse to the first folder on the bookmark bar and press the
366    // mouse.
367    views::MenuButton* button = bb_view_->other_bookmarked_button();
368    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
369        ui_controls::DOWN | ui_controls::UP,
370        CreateEventTask(this, &BookmarkBarViewTest3::Step2));
371  }
372
373 private:
374  void Step2() {
375    // Menu should be showing.
376    views::MenuItemView* menu = bb_view_->GetMenu();
377    ASSERT_TRUE(menu != NULL);
378    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
379
380    views::MenuItemView* child_menu =
381        menu->GetSubmenu()->GetMenuItemAt(1);
382    ASSERT_TRUE(child_menu != NULL);
383
384    // Click on second child, which has a submenu.
385    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
386        ui_controls::DOWN | ui_controls::UP,
387        CreateEventTask(this, &BookmarkBarViewTest3::Step3));
388  }
389
390  void Step3() {
391    // Make sure sub menu is showing.
392    views::MenuItemView* menu = bb_view_->GetMenu();
393    ASSERT_TRUE(menu);
394    views::MenuItemView* child_menu =
395        menu->GetSubmenu()->GetMenuItemAt(1);
396    ASSERT_TRUE(child_menu->GetSubmenu() != NULL);
397    ASSERT_TRUE(child_menu->GetSubmenu()->IsShowing());
398
399    // Click on third child, which has a submenu too.
400    child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
401    ASSERT_TRUE(child_menu != NULL);
402    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
403        ui_controls::DOWN | ui_controls::UP,
404        CreateEventTask(this, &BookmarkBarViewTest3::Step4));
405  }
406
407  void Step4() {
408    // Make sure sub menu we first clicked isn't showing.
409    views::MenuItemView* menu = bb_view_->GetMenu();
410    views::MenuItemView* child_menu =
411        menu->GetSubmenu()->GetMenuItemAt(1);
412    ASSERT_TRUE(child_menu->GetSubmenu() != NULL);
413    ASSERT_FALSE(child_menu->GetSubmenu()->IsShowing());
414
415    // And submenu we last clicked is showing.
416    child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
417    ASSERT_TRUE(child_menu != NULL);
418    ASSERT_TRUE(child_menu->GetSubmenu()->IsShowing());
419
420    // Nothing should have been selected.
421    EXPECT_EQ(GURL(), navigator_.url_);
422
423    // Hide menu.
424    menu->GetMenuController()->CancelAll();
425
426    Done();
427  }
428};
429
430VIEW_TEST(BookmarkBarViewTest3, Submenus)
431
432// Observer that posts task upon the context menu creation.
433// This is necessary for Linux as the context menu has to check
434// the clipboard, which invokes the event loop.
435class BookmarkContextMenuNotificationObserver
436    : public content::NotificationObserver {
437 public:
438  explicit BookmarkContextMenuNotificationObserver(const base::Closure& task)
439      : task_(task) {
440    registrar_.Add(this,
441                   chrome::NOTIFICATION_BOOKMARK_CONTEXT_MENU_SHOWN,
442                   content::NotificationService::AllSources());
443  }
444
445  virtual void Observe(int type,
446                       const content::NotificationSource& source,
447                       const content::NotificationDetails& details) OVERRIDE {
448    base::MessageLoop::current()->PostTask(FROM_HERE, task_);
449  }
450
451  // Sets the task that is posted when the context menu is shown.
452  void set_task(const base::Closure& task) { task_ = task; }
453
454 private:
455  content::NotificationRegistrar registrar_;
456  base::Closure task_;
457
458  DISALLOW_COPY_AND_ASSIGN(BookmarkContextMenuNotificationObserver);
459};
460
461// Tests context menus by way of opening a context menu for a bookmark,
462// then right clicking to get context menu and selecting the first menu item
463// (open).
464class BookmarkBarViewTest4 : public BookmarkBarViewEventTestBase {
465 public:
466  BookmarkBarViewTest4()
467      : observer_(CreateEventTask(this, &BookmarkBarViewTest4::Step3)) {
468  }
469
470 protected:
471  virtual void DoTestOnMessageLoop() OVERRIDE {
472    // Move the mouse to the first folder on the bookmark bar and press the
473    // mouse.
474    views::TextButton* button = bb_view_->other_bookmarked_button();
475    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
476        ui_controls::DOWN | ui_controls::UP,
477        CreateEventTask(this, &BookmarkBarViewTest4::Step2));
478  }
479
480 private:
481  void Step2() {
482    // Menu should be showing.
483    views::MenuItemView* menu = bb_view_->GetMenu();
484    ASSERT_TRUE(menu != NULL);
485    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
486
487    views::MenuItemView* child_menu =
488        menu->GetSubmenu()->GetMenuItemAt(0);
489    ASSERT_TRUE(child_menu != NULL);
490
491    // Right click on the first child to get its context menu.
492    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
493        ui_controls::DOWN | ui_controls::UP, base::Closure());
494    // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
495  }
496
497  void Step3() {
498    // Make sure the context menu is showing.
499    views::MenuItemView* menu = bb_view_->GetContextMenu();
500    ASSERT_TRUE(menu != NULL);
501    ASSERT_TRUE(menu->GetSubmenu());
502    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
503
504    // Select the first menu item (open).
505    ui_test_utils::MoveMouseToCenterAndPress(
506        menu->GetSubmenu()->GetMenuItemAt(0),
507        ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
508        CreateEventTask(this, &BookmarkBarViewTest4::Step4));
509  }
510
511  void Step4() {
512    EXPECT_EQ(navigator_.url_, model_->other_node()->GetChild(0)->url());
513    Done();
514  }
515
516  BookmarkContextMenuNotificationObserver observer_;
517};
518
519VIEW_TEST(BookmarkBarViewTest4, ContextMenus)
520
521// Tests drag and drop within the same menu.
522class BookmarkBarViewTest5 : public BookmarkBarViewEventTestBase {
523 protected:
524  virtual void DoTestOnMessageLoop() OVERRIDE {
525    url_dragging_ =
526        model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
527
528    // Move the mouse to the first folder on the bookmark bar and press the
529    // mouse.
530    views::TextButton* button = GetBookmarkButton(0);
531    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
532        ui_controls::DOWN | ui_controls::UP,
533        CreateEventTask(this, &BookmarkBarViewTest5::Step2));
534  }
535
536 private:
537  void Step2() {
538    // Menu should be showing.
539    views::MenuItemView* menu = bb_view_->GetMenu();
540    ASSERT_TRUE(menu != NULL);
541    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
542
543    views::MenuItemView* child_menu =
544        menu->GetSubmenu()->GetMenuItemAt(0);
545    ASSERT_TRUE(child_menu != NULL);
546
547    // Move mouse to center of menu and press button.
548    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
549        ui_controls::DOWN,
550        CreateEventTask(this, &BookmarkBarViewTest5::Step3));
551  }
552
553  void Step3() {
554    views::MenuItemView* target_menu =
555        bb_view_->GetMenu()->GetSubmenu()->GetMenuItemAt(1);
556    gfx::Point loc(1, target_menu->height() - 1);
557    views::View::ConvertPointToScreen(target_menu, &loc);
558
559    // Start a drag.
560    ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
561        CreateEventTask(this, &BookmarkBarViewTest5::Step4));
562
563    // See comment above this method as to why we do this.
564    ScheduleMouseMoveInBackground(loc.x(), loc.y());
565  }
566
567  void Step4() {
568    // Drop the item so that it's now the second item.
569    views::MenuItemView* target_menu =
570        bb_view_->GetMenu()->GetSubmenu()->GetMenuItemAt(1);
571    gfx::Point loc(1, target_menu->height() - 2);
572    views::View::ConvertPointToScreen(target_menu, &loc);
573    ui_controls::SendMouseMove(loc.x(), loc.y());
574
575    ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT,
576        ui_controls::UP,
577        CreateEventTask(this, &BookmarkBarViewTest5::Step5));
578  }
579
580  void Step5() {
581    GURL url = model_->bookmark_bar_node()->GetChild(0)->GetChild(1)->url();
582    EXPECT_EQ(url_dragging_, url);
583    Done();
584  }
585
586  GURL url_dragging_;
587};
588
589VIEW_TEST(BookmarkBarViewTest5, MAYBE(DND))
590
591// Tests holding mouse down on overflow button, dragging such that menu pops up
592// then selecting an item.
593class BookmarkBarViewTest6 : public BookmarkBarViewEventTestBase {
594 protected:
595  virtual void DoTestOnMessageLoop() OVERRIDE {
596    // Press the mouse button on the overflow button. Don't release it though.
597    views::TextButton* button = bb_view_->overflow_button();
598    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
599        ui_controls::DOWN, CreateEventTask(this, &BookmarkBarViewTest6::Step2));
600  }
601
602 private:
603  void Step2() {
604    // Menu should be showing.
605    views::MenuItemView* menu = bb_view_->GetMenu();
606    ASSERT_TRUE(menu != NULL);
607    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
608
609    views::MenuItemView* child_menu =
610        menu->GetSubmenu()->GetMenuItemAt(0);
611    ASSERT_TRUE(child_menu != NULL);
612
613    // Move mouse to center of menu and release mouse.
614    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
615        ui_controls::UP, CreateEventTask(this, &BookmarkBarViewTest6::Step3));
616  }
617
618  void Step3() {
619    ASSERT_TRUE(navigator_.url_ ==
620                model_->bookmark_bar_node()->GetChild(6)->url());
621    Done();
622  }
623
624  GURL url_dragging_;
625};
626
627VIEW_TEST(BookmarkBarViewTest6, OpenMenuOnClickAndHold)
628
629// Tests drag and drop to different menu.
630class BookmarkBarViewTest7 : public BookmarkBarViewEventTestBase {
631 protected:
632  virtual void DoTestOnMessageLoop() OVERRIDE {
633    url_dragging_ =
634        model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
635
636    // Move the mouse to the first folder on the bookmark bar and press the
637    // mouse.
638    views::TextButton* button = GetBookmarkButton(0);
639    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
640        ui_controls::DOWN | ui_controls::UP,
641        CreateEventTask(this, &BookmarkBarViewTest7::Step2));
642  }
643
644 private:
645  void Step2() {
646    // Menu should be showing.
647    views::MenuItemView* menu = bb_view_->GetMenu();
648    ASSERT_TRUE(menu != NULL);
649    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
650
651    views::MenuItemView* child_menu =
652        menu->GetSubmenu()->GetMenuItemAt(0);
653    ASSERT_TRUE(child_menu != NULL);
654
655    // Move mouse to center of menu and press button.
656    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
657        ui_controls::DOWN,
658        CreateEventTask(this, &BookmarkBarViewTest7::Step3));
659  }
660
661  void Step3() {
662    // Drag over other button.
663    views::TextButton* other_button =
664        bb_view_->other_bookmarked_button();
665    gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
666    views::View::ConvertPointToScreen(other_button, &loc);
667
668#if defined(USE_AURA)
669    // TODO: fix this. Aura requires an additional mouse event to trigger drag
670    // and drop checking state.
671    ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
672        base::Bind(&BookmarkBarViewTest7::Step3A, this));
673#else
674    // Start a drag.
675    ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
676        base::Bind(&BookmarkBarViewTest7::Step4, this));
677
678    // See comment above this method as to why we do this.
679    ScheduleMouseMoveInBackground(loc.x(), loc.y());
680#endif
681  }
682
683  void Step3A() {
684    // Drag over other button.
685    views::TextButton* other_button =
686        bb_view_->other_bookmarked_button();
687    gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
688    views::View::ConvertPointToScreen(other_button, &loc);
689
690    ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
691        base::Bind(&BookmarkBarViewTest7::Step4, this));
692  }
693
694  void Step4() {
695    views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
696    ASSERT_TRUE(drop_menu != NULL);
697    ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
698
699    views::MenuItemView* target_menu =
700        drop_menu->GetSubmenu()->GetMenuItemAt(0);
701    gfx::Point loc(1, 1);
702    views::View::ConvertPointToScreen(target_menu, &loc);
703    ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
704        CreateEventTask(this, &BookmarkBarViewTest7::Step5));
705  }
706
707  void Step5() {
708    ui_controls::SendMouseEventsNotifyWhenDone(
709        ui_controls::LEFT, ui_controls::UP,
710        CreateEventTask(this, &BookmarkBarViewTest7::Step6));
711  }
712
713  void Step6() {
714    ASSERT_TRUE(model_->other_node()->GetChild(0)->url() == url_dragging_);
715    Done();
716  }
717
718  GURL url_dragging_;
719};
720
721#if !defined(OS_WIN)
722// This test passes locally (on aero and non-aero) but fails on the trybots and
723// buildbot.
724// http://crbug.com/154081
725VIEW_TEST(BookmarkBarViewTest7, MAYBE(DNDToDifferentMenu))
726#endif
727
728// Drags from one menu to next so that original menu closes, then back to
729// original menu.
730class BookmarkBarViewTest8 : public BookmarkBarViewEventTestBase {
731 protected:
732  virtual void DoTestOnMessageLoop() OVERRIDE {
733    url_dragging_ =
734        model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
735
736    // Move the mouse to the first folder on the bookmark bar and press the
737    // mouse.
738    views::TextButton* button = GetBookmarkButton(0);
739    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
740        ui_controls::DOWN | ui_controls::UP,
741        CreateEventTask(this, &BookmarkBarViewTest8::Step2));
742  }
743
744 private:
745  void Step2() {
746    // Menu should be showing.
747    views::MenuItemView* menu = bb_view_->GetMenu();
748    ASSERT_TRUE(menu != NULL);
749    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
750
751    views::MenuItemView* child_menu =
752        menu->GetSubmenu()->GetMenuItemAt(0);
753    ASSERT_TRUE(child_menu != NULL);
754
755    // Move mouse to center of menu and press button.
756    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
757        ui_controls::DOWN,
758        CreateEventTask(this, &BookmarkBarViewTest8::Step3));
759  }
760
761  void Step3() {
762    // Drag over other button.
763    views::TextButton* other_button =
764        bb_view_->other_bookmarked_button();
765    gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
766    views::View::ConvertPointToScreen(other_button, &loc);
767
768    // Start a drag.
769#if defined(USE_AURA)
770    // TODO: fix this. Aura requires an additional mouse event to trigger drag
771    // and drop checking state.
772    ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
773        base::Bind(&BookmarkBarViewTest8::Step3A, this));
774#else
775    ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
776        base::Bind(&BookmarkBarViewTest8::Step4, this));
777    // See comment above this method as to why we do this.
778    ScheduleMouseMoveInBackground(loc.x(), loc.y());
779#endif
780  }
781
782  void Step3A() {
783    // Drag over other button.
784    views::TextButton* other_button =
785        bb_view_->other_bookmarked_button();
786    gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
787    views::View::ConvertPointToScreen(other_button, &loc);
788
789    ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
790        base::Bind(&BookmarkBarViewTest8::Step4, this));
791  }
792
793  void Step4() {
794    views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
795    ASSERT_TRUE(drop_menu != NULL);
796    ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
797
798    // Now drag back over first menu.
799    views::TextButton* button = GetBookmarkButton(0);
800    gfx::Point loc(button->width() / 2, button->height() / 2);
801    views::View::ConvertPointToScreen(button, &loc);
802    ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
803        base::Bind(&BookmarkBarViewTest8::Step5, this));
804  }
805
806  void Step5() {
807    // Drop on folder F11.
808    views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
809    ASSERT_TRUE(drop_menu != NULL);
810    ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
811
812    views::MenuItemView* target_menu =
813        drop_menu->GetSubmenu()->GetMenuItemAt(1);
814    ui_test_utils::MoveMouseToCenterAndPress(
815        target_menu, ui_controls::LEFT, ui_controls::UP,
816        CreateEventTask(this, &BookmarkBarViewTest8::Step6));
817  }
818
819  void Step6() {
820    // Make sure drop was processed.
821    GURL final_url = model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->
822        GetChild(1)->url();
823    ASSERT_TRUE(final_url == url_dragging_);
824    Done();
825  }
826
827  GURL url_dragging_;
828};
829
830#if !defined(OS_WIN)
831// This test passes locally (on aero and non-aero) but fails on the trybots and
832// buildbot.
833// http://crbug.com/154081
834VIEW_TEST(BookmarkBarViewTest8, MAYBE(DNDBackToOriginatingMenu))
835#endif
836
837// Moves the mouse over the scroll button and makes sure we get scrolling.
838class BookmarkBarViewTest9 : public BookmarkBarViewEventTestBase {
839 protected:
840  virtual bool CreateBigMenu() OVERRIDE { return true; }
841
842  virtual void DoTestOnMessageLoop() OVERRIDE {
843    // Move the mouse to the first folder on the bookmark bar and press the
844    // mouse.
845    views::TextButton* button = GetBookmarkButton(0);
846    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
847        ui_controls::DOWN | ui_controls::UP,
848        CreateEventTask(this, &BookmarkBarViewTest9::Step2));
849  }
850
851 private:
852  void Step2() {
853    // Menu should be showing.
854    views::MenuItemView* menu = bb_view_->GetMenu();
855    ASSERT_TRUE(menu != NULL);
856    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
857
858    first_menu_ = menu->GetSubmenu()->GetMenuItemAt(0);
859    gfx::Point menu_loc;
860    views::View::ConvertPointToScreen(first_menu_, &menu_loc);
861    start_y_ = menu_loc.y();
862
863    // Move the mouse over the scroll button.
864    views::View* scroll_container = menu->GetSubmenu()->parent();
865    ASSERT_TRUE(scroll_container != NULL);
866    scroll_container = scroll_container->parent();
867    ASSERT_TRUE(scroll_container != NULL);
868    views::View* scroll_down_button = scroll_container->child_at(1);
869    ASSERT_TRUE(scroll_down_button);
870    gfx::Point loc(scroll_down_button->width() / 2,
871                   scroll_down_button->height() / 2);
872    views::View::ConvertPointToScreen(scroll_down_button, &loc);
873
874    // On linux, the sending one location isn't enough.
875    ui_controls::SendMouseMove(loc.x() - 1 , loc.y() - 1);
876    ui_controls::SendMouseMoveNotifyWhenDone(
877        loc.x(), loc.y(), CreateEventTask(this, &BookmarkBarViewTest9::Step3));
878  }
879
880  void Step3() {
881    base::MessageLoop::current()->PostDelayedTask(
882        FROM_HERE,
883        base::Bind(&BookmarkBarViewTest9::Step4, this),
884        base::TimeDelta::FromMilliseconds(200));
885  }
886
887  void Step4() {
888    gfx::Point menu_loc;
889    views::View::ConvertPointToScreen(first_menu_, &menu_loc);
890    ASSERT_NE(start_y_, menu_loc.y());
891
892    // Hide menu.
893    bb_view_->GetMenu()->GetMenuController()->CancelAll();
894
895    // On linux, Cancelling menu will call Quit on the message loop,
896    // which can interfere with Done. We need to run Done in the
897    // next execution loop.
898    base::MessageLoop::current()->PostTask(
899        FROM_HERE, base::Bind(&ViewEventTestBase::Done, this));
900  }
901
902  int start_y_;
903  views::MenuItemView* first_menu_;
904};
905
906VIEW_TEST(BookmarkBarViewTest9, ScrollButtonScrolls)
907
908// Tests up/down/left/enter key messages.
909class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {
910 protected:
911  virtual void DoTestOnMessageLoop() OVERRIDE {
912    // Move the mouse to the first folder on the bookmark bar and press the
913    // mouse.
914    views::TextButton* button = GetBookmarkButton(0);
915    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
916        ui_controls::DOWN | ui_controls::UP,
917        CreateEventTask(this, &BookmarkBarViewTest10::Step2));
918    base::MessageLoop::current()->RunUntilIdle();
919  }
920
921 private:
922  void Step2() {
923    // Menu should be showing.
924    views::MenuItemView* menu = bb_view_->GetMenu();
925    ASSERT_TRUE(menu != NULL);
926    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
927
928    // Send a down event, which should select the first item.
929    ui_controls::SendKeyPressNotifyWhenDone(
930        window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
931        CreateEventTask(this, &BookmarkBarViewTest10::Step3));
932  }
933
934  void Step3() {
935    // Make sure menu is showing and item is selected.
936    views::MenuItemView* menu = bb_view_->GetMenu();
937    ASSERT_TRUE(menu != NULL);
938    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
939    ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
940
941    // Send a key down event, which should select the next item.
942    ui_controls::SendKeyPressNotifyWhenDone(
943        window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
944        CreateEventTask(this, &BookmarkBarViewTest10::Step4));
945  }
946
947  void Step4() {
948    views::MenuItemView* menu = bb_view_->GetMenu();
949    ASSERT_TRUE(menu != NULL);
950    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
951    ASSERT_FALSE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
952    ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(1)->IsSelected());
953
954    // Send a right arrow to force the menu to open.
955    ui_controls::SendKeyPressNotifyWhenDone(
956        window_->GetNativeWindow(), ui::VKEY_RIGHT, false, false, false, false,
957        CreateEventTask(this, &BookmarkBarViewTest10::Step5));
958  }
959
960  void Step5() {
961    // Make sure the submenu is showing.
962    views::MenuItemView* menu = bb_view_->GetMenu();
963    ASSERT_TRUE(menu != NULL);
964    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
965    views::MenuItemView* submenu = menu->GetSubmenu()->GetMenuItemAt(1);
966    ASSERT_TRUE(submenu->IsSelected());
967    ASSERT_TRUE(submenu->GetSubmenu());
968    ASSERT_TRUE(submenu->GetSubmenu()->IsShowing());
969
970    // Send a left arrow to close the submenu.
971    ui_controls::SendKeyPressNotifyWhenDone(
972        window_->GetNativeWindow(), ui::VKEY_LEFT, false, false, false, false,
973        CreateEventTask(this, &BookmarkBarViewTest10::Step6));
974  }
975
976  void Step6() {
977    // Make sure the submenu is showing.
978    views::MenuItemView* menu = bb_view_->GetMenu();
979    ASSERT_TRUE(menu != NULL);
980    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
981    views::MenuItemView* submenu = menu->GetSubmenu()->GetMenuItemAt(1);
982    ASSERT_TRUE(submenu->IsSelected());
983    ASSERT_TRUE(!submenu->GetSubmenu() || !submenu->GetSubmenu()->IsShowing());
984
985    // Send a down arrow to wrap back to f1a
986    ui_controls::SendKeyPressNotifyWhenDone(
987        window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
988        CreateEventTask(this, &BookmarkBarViewTest10::Step7));
989  }
990
991  void Step7() {
992    // Make sure menu is showing and item is selected.
993    views::MenuItemView* menu = bb_view_->GetMenu();
994    ASSERT_TRUE(menu != NULL);
995    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
996    ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
997
998    // Send enter, which should select the item.
999    ui_controls::SendKeyPressNotifyWhenDone(
1000        window_->GetNativeWindow(), ui::VKEY_RETURN, false, false, false, false,
1001        CreateEventTask(this, &BookmarkBarViewTest10::Step8));
1002  }
1003
1004  void Step8() {
1005    ASSERT_TRUE(
1006        model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url() ==
1007        navigator_.url_);
1008    Done();
1009  }
1010};
1011
1012VIEW_TEST(BookmarkBarViewTest10, KeyEvents)
1013
1014// Make sure the menu closes with the following sequence: show menu, show
1015// context menu, close context menu (via escape), then click else where. This
1016// effectively verifies we maintain mouse capture after the context menu is
1017// hidden.
1018class BookmarkBarViewTest11 : public BookmarkBarViewEventTestBase {
1019 public:
1020  BookmarkBarViewTest11()
1021      : observer_(CreateEventTask(this, &BookmarkBarViewTest11::Step3)) {
1022  }
1023
1024 protected:
1025  virtual void DoTestOnMessageLoop() OVERRIDE {
1026    // Move the mouse to the first folder on the bookmark bar and press the
1027    // mouse.
1028    views::TextButton* button = bb_view_->other_bookmarked_button();
1029    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1030        ui_controls::DOWN | ui_controls::UP,
1031        CreateEventTask(this, &BookmarkBarViewTest11::Step2));
1032  }
1033
1034 private:
1035  void Step2() {
1036    // Menu should be showing.
1037    views::MenuItemView* menu = bb_view_->GetMenu();
1038    ASSERT_TRUE(menu != NULL);
1039    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1040
1041    views::MenuItemView* child_menu =
1042        menu->GetSubmenu()->GetMenuItemAt(0);
1043    ASSERT_TRUE(child_menu != NULL);
1044
1045    // Right click on the first child to get its context menu.
1046    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1047        ui_controls::DOWN | ui_controls::UP, base::Closure());
1048    // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1049  }
1050
1051  void Step3() {
1052    // Send escape so that the context menu hides.
1053    ui_controls::SendKeyPressNotifyWhenDone(
1054        window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
1055        CreateEventTask(this, &BookmarkBarViewTest11::Step4));
1056  }
1057
1058  void Step4() {
1059    // Make sure the context menu is no longer showing.
1060    views::MenuItemView* menu = bb_view_->GetContextMenu();
1061    ASSERT_TRUE(!menu || !menu->GetSubmenu() ||
1062                !menu->GetSubmenu()->IsShowing());
1063
1064    // But the menu should be showing.
1065    menu = bb_view_->GetMenu();
1066    ASSERT_TRUE(menu && menu->GetSubmenu() && menu->GetSubmenu()->IsShowing());
1067
1068    // Now click on empty space.
1069    gfx::Point mouse_loc;
1070    views::View::ConvertPointToScreen(bb_view_.get(), &mouse_loc);
1071    ui_controls::SendMouseMove(mouse_loc.x(), mouse_loc.y());
1072    ui_controls::SendMouseEventsNotifyWhenDone(
1073        ui_controls::LEFT, ui_controls::UP | ui_controls::DOWN,
1074        CreateEventTask(this, &BookmarkBarViewTest11::Step5));
1075  }
1076
1077  void Step5() {
1078    // Make sure the menu is not showing.
1079    views::MenuItemView* menu = bb_view_->GetMenu();
1080    ASSERT_TRUE(!menu || !menu->GetSubmenu() ||
1081                !menu->GetSubmenu()->IsShowing());
1082    Done();
1083  }
1084
1085  BookmarkContextMenuNotificationObserver observer_;
1086};
1087
1088#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
1089// TODO(erg): linux_aura bringup: http://crbug.com/163931
1090#define MAYBE_CloseMenuAfterClosingContextMenu \
1091  DISABLED_CloseMenuAfterClosingContextMenu
1092#else
1093#define MAYBE_CloseMenuAfterClosingContextMenu CloseMenuAfterClosingContextMenu
1094#endif
1095
1096VIEW_TEST(BookmarkBarViewTest11, MAYBE_CloseMenuAfterClosingContextMenu)
1097
1098// Tests showing a modal dialog from a context menu.
1099class BookmarkBarViewTest12 : public BookmarkBarViewEventTestBase {
1100 protected:
1101  virtual void DoTestOnMessageLoop() OVERRIDE {
1102    // Open up the other folder.
1103    views::TextButton* button = bb_view_->other_bookmarked_button();
1104    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1105        ui_controls::DOWN | ui_controls::UP,
1106        CreateEventTask(this, &BookmarkBarViewTest12::Step2));
1107    chrome::num_bookmark_urls_before_prompting = 1;
1108  }
1109
1110  virtual ~BookmarkBarViewTest12() {
1111    chrome::num_bookmark_urls_before_prompting = 15;
1112  }
1113
1114 private:
1115  void Step2() {
1116    // Menu should be showing.
1117    views::MenuItemView* menu = bb_view_->GetMenu();
1118    ASSERT_TRUE(menu != NULL);
1119    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1120
1121    views::MenuItemView* child_menu =
1122        menu->GetSubmenu()->GetMenuItemAt(1);
1123    ASSERT_TRUE(child_menu != NULL);
1124
1125    // Right click on the second child (a folder) to get its context menu.
1126    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1127        ui_controls::DOWN | ui_controls::UP,
1128        CreateEventTask(this, &BookmarkBarViewTest12::Step3));
1129  }
1130
1131  void Step3() {
1132    // Make sure the context menu is showing.
1133    views::MenuItemView* menu = bb_view_->GetContextMenu();
1134    ASSERT_TRUE(menu && menu->GetSubmenu() && menu->GetSubmenu()->IsShowing());
1135
1136    // Select the first item in the context menu (open all).
1137    views::MenuItemView* child_menu =
1138        menu->GetSubmenu()->GetMenuItemAt(0);
1139    ASSERT_TRUE(child_menu != NULL);
1140    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
1141        ui_controls::DOWN | ui_controls::UP, base::Closure());
1142
1143    // Delay until we send tab, otherwise the message box doesn't appear
1144    // correctly.
1145    base::MessageLoop::current()->PostDelayedTask(
1146        FROM_HERE,
1147        CreateEventTask(this, &BookmarkBarViewTest12::Step4),
1148        base::TimeDelta::FromSeconds(1));
1149  }
1150
1151  void Step4() {
1152    // Press tab to give focus to the cancel button.
1153    ui_controls::SendKeyPress(
1154        window_->GetNativeWindow(), ui::VKEY_TAB, false, false, false, false);
1155
1156    // For some reason return isn't processed correctly unless we delay.
1157    base::MessageLoop::current()->PostDelayedTask(
1158        FROM_HERE,
1159        CreateEventTask(this, &BookmarkBarViewTest12::Step5),
1160        base::TimeDelta::FromSeconds(1));
1161  }
1162
1163  void Step5() {
1164    // And press enter so that the cancel button is selected.
1165    ui_controls::SendKeyPressNotifyWhenDone(
1166        window_->GetNativeWindow(), ui::VKEY_RETURN, false, false, false, false,
1167        CreateEventTask(this, &BookmarkBarViewTest12::Step6));
1168  }
1169
1170  void Step6() {
1171    // Do a delayed task to give the dialog time to exit.
1172    base::MessageLoop::current()->PostTask(
1173        FROM_HERE, CreateEventTask(this, &BookmarkBarViewTest12::Step7));
1174  }
1175
1176  void Step7() {
1177    Done();
1178  }
1179};
1180
1181#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
1182// TODO(erg): linux_aura bringup: http://crbug.com/163931
1183#define MAYBE_CloseWithModalDialog DISABLED_CloseWithModalDialog
1184#else
1185#define MAYBE_CloseWithModalDialog CloseWithModalDialog
1186#endif
1187
1188VIEW_TEST(BookmarkBarViewTest12, MAYBE_CloseWithModalDialog)
1189
1190// Tests clicking on the separator of a context menu (this is for coverage of
1191// bug 17862).
1192class BookmarkBarViewTest13 : public BookmarkBarViewEventTestBase {
1193 public:
1194  BookmarkBarViewTest13()
1195      : observer_(CreateEventTask(this, &BookmarkBarViewTest13::Step3)) {
1196  }
1197
1198 protected:
1199  virtual void DoTestOnMessageLoop() OVERRIDE {
1200    // Move the mouse to the first folder on the bookmark bar and press the
1201    // mouse.
1202    views::TextButton* button = bb_view_->other_bookmarked_button();
1203    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1204        ui_controls::DOWN | ui_controls::UP,
1205        CreateEventTask(this, &BookmarkBarViewTest13::Step2));
1206  }
1207
1208 private:
1209  void Step2() {
1210    // Menu should be showing.
1211    views::MenuItemView* menu = bb_view_->GetMenu();
1212    ASSERT_TRUE(menu != NULL);
1213    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1214
1215    views::MenuItemView* child_menu =
1216        menu->GetSubmenu()->GetMenuItemAt(0);
1217    ASSERT_TRUE(child_menu != NULL);
1218
1219    // Right click on the first child to get its context menu.
1220    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1221        ui_controls::DOWN | ui_controls::UP, base::Closure());
1222    // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1223  }
1224
1225  void Step3() {
1226    // Make sure the context menu is showing.
1227    views::MenuItemView* menu = bb_view_->GetContextMenu();
1228    ASSERT_TRUE(menu != NULL);
1229    ASSERT_TRUE(menu->GetSubmenu());
1230    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1231
1232    // Find the first separator.
1233    views::SubmenuView* submenu = menu->GetSubmenu();
1234    views::View* separator_view = NULL;
1235    for (int i = 0; i < submenu->child_count(); ++i) {
1236      if (submenu->child_at(i)->id() != views::MenuItemView::kMenuItemViewID) {
1237        separator_view = submenu->child_at(i);
1238        break;
1239      }
1240    }
1241    ASSERT_TRUE(separator_view);
1242
1243    // Click on the separator. Clicking on the separator shouldn't visually
1244    // change anything.
1245    ui_test_utils::MoveMouseToCenterAndPress(separator_view,
1246        ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1247        CreateEventTask(this, &BookmarkBarViewTest13::Step4));
1248  }
1249
1250  void Step4() {
1251    // The context menu should still be showing.
1252    views::MenuItemView* menu = bb_view_->GetContextMenu();
1253    ASSERT_TRUE(menu != NULL);
1254    ASSERT_TRUE(menu->GetSubmenu());
1255    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1256
1257    // Select the first context menu item.
1258    ui_test_utils::MoveMouseToCenterAndPress(
1259        menu->GetSubmenu()->GetMenuItemAt(0),
1260        ui_controls::LEFT,
1261        ui_controls::DOWN | ui_controls::UP,
1262        CreateEventTask(this, &BookmarkBarViewTest13::Step5));
1263  }
1264
1265  void Step5() {
1266    Done();
1267  }
1268
1269  BookmarkContextMenuNotificationObserver observer_;
1270};
1271
1272VIEW_TEST(BookmarkBarViewTest13, ClickOnContextMenuSeparator)
1273
1274// Makes sure right clicking on a folder on the bookmark bar doesn't result in
1275// both a context menu and showing the menu.
1276class BookmarkBarViewTest14 : public BookmarkBarViewEventTestBase {
1277 public:
1278  BookmarkBarViewTest14()
1279      : observer_(CreateEventTask(this, &BookmarkBarViewTest14::Step2)) {
1280  }
1281
1282 protected:
1283  virtual void DoTestOnMessageLoop() OVERRIDE {
1284    // Move the mouse to the first folder on the bookmark bar and press the
1285    // right mouse button.
1286    views::TextButton* button = GetBookmarkButton(0);
1287    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::RIGHT,
1288        ui_controls::DOWN | ui_controls::UP, base::Closure());
1289    // Step2 will be invoked by BookmarkContextMenuNotificationObserver.
1290  }
1291
1292 private:
1293
1294  void Step2() {
1295    // Menu should NOT be showing.
1296    views::MenuItemView* menu = bb_view_->GetMenu();
1297    ASSERT_TRUE(menu == NULL);
1298
1299    // Send escape so that the context menu hides.
1300    ui_controls::SendKeyPressNotifyWhenDone(
1301        window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
1302        CreateEventTask(this, &BookmarkBarViewTest14::Step3));
1303  }
1304
1305  void Step3() {
1306    Done();
1307  }
1308
1309  BookmarkContextMenuNotificationObserver observer_;
1310};
1311
1312VIEW_TEST(BookmarkBarViewTest14, ContextMenus2)
1313
1314// Makes sure deleting from the context menu keeps the bookmark menu showing.
1315class BookmarkBarViewTest15 : public BookmarkBarViewEventTestBase {
1316 public:
1317  BookmarkBarViewTest15()
1318      : deleted_menu_id_(0),
1319        observer_(CreateEventTask(this, &BookmarkBarViewTest15::Step3)) {
1320  }
1321
1322 protected:
1323  virtual void DoTestOnMessageLoop() OVERRIDE {
1324    // Show the other bookmarks.
1325    views::TextButton* button = bb_view_->other_bookmarked_button();
1326    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1327        ui_controls::DOWN | ui_controls::UP,
1328        CreateEventTask(this, &BookmarkBarViewTest15::Step2));
1329  }
1330
1331 private:
1332  void Step2() {
1333    // Menu should be showing.
1334    views::MenuItemView* menu = bb_view_->GetMenu();
1335    ASSERT_TRUE(menu != NULL);
1336    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1337
1338    views::MenuItemView* child_menu =
1339        menu->GetSubmenu()->GetMenuItemAt(1);
1340    ASSERT_TRUE(child_menu != NULL);
1341
1342    deleted_menu_id_ = child_menu->GetCommand();
1343
1344    // Right click on the second child to get its context menu.
1345    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1346        ui_controls::DOWN | ui_controls::UP, base::Closure());
1347    // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1348  }
1349
1350  void Step3() {
1351    // Make sure the context menu is showing.
1352    views::MenuItemView* menu = bb_view_->GetContextMenu();
1353    ASSERT_TRUE(menu != NULL);
1354    ASSERT_TRUE(menu->GetSubmenu());
1355    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1356
1357    views::MenuItemView* delete_menu =
1358        menu->GetMenuItemByID(IDC_BOOKMARK_BAR_REMOVE);
1359    ASSERT_TRUE(delete_menu);
1360
1361    // Click on the delete button.
1362    ui_test_utils::MoveMouseToCenterAndPress(delete_menu,
1363        ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1364        CreateEventTask(this, &BookmarkBarViewTest15::Step4));
1365  }
1366
1367  void Step4() {
1368    // The context menu should not be showing.
1369    views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1370    ASSERT_TRUE(context_menu == NULL);
1371
1372    // But the menu should be showing.
1373    views::MenuItemView* menu = bb_view_->GetMenu();
1374    ASSERT_TRUE(menu != NULL);
1375    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1376
1377    // And the deleted_menu_id_ should have been removed.
1378    ASSERT_TRUE(menu->GetMenuItemByID(deleted_menu_id_) == NULL);
1379
1380    bb_view_->GetMenu()->GetMenuController()->CancelAll();
1381
1382    Done();
1383  }
1384
1385  int deleted_menu_id_;
1386  BookmarkContextMenuNotificationObserver observer_;
1387};
1388
1389VIEW_TEST(BookmarkBarViewTest15, MenuStaysVisibleAfterDelete)
1390
1391// Tests that we don't crash or get stuck if the parent of a menu is closed.
1392class BookmarkBarViewTest16 : public BookmarkBarViewEventTestBase {
1393 protected:
1394  virtual void DoTestOnMessageLoop() OVERRIDE {
1395    // Move the mouse to the first folder on the bookmark bar and press the
1396    // mouse.
1397    views::TextButton* button = GetBookmarkButton(0);
1398    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1399        ui_controls::DOWN | ui_controls::UP,
1400        CreateEventTask(this, &BookmarkBarViewTest16::Step2));
1401  }
1402
1403 private:
1404  void Step2() {
1405    // Menu should be showing.
1406    views::MenuItemView* menu = bb_view_->GetMenu();
1407    ASSERT_TRUE(menu != NULL);
1408    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1409
1410    // Button should be depressed.
1411    views::TextButton* button = GetBookmarkButton(0);
1412    ASSERT_TRUE(button->state() == views::CustomButton::STATE_PRESSED);
1413
1414    // Close the window.
1415    window_->Close();
1416    window_ = NULL;
1417
1418    base::MessageLoop::current()->PostTask(
1419        FROM_HERE, CreateEventTask(this, &BookmarkBarViewTest16::Done));
1420  }
1421};
1422
1423#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1424// TODO(erg): linux_aura bringup: http://crbug.com/163931
1425#define MAYBE_DeleteMenu DISABLED_DeleteMenu
1426#else
1427#define MAYBE_DeleteMenu DeleteMenu
1428#endif
1429
1430VIEW_TEST(BookmarkBarViewTest16, MAYBE_DeleteMenu)
1431
1432// Makes sure right clicking on an item while a context menu is already showing
1433// doesn't crash and works.
1434class BookmarkBarViewTest17 : public BookmarkBarViewEventTestBase {
1435 public:
1436  BookmarkBarViewTest17()
1437      : observer_(CreateEventTask(this, &BookmarkBarViewTest17::Step3)) {
1438  }
1439
1440 protected:
1441  virtual void DoTestOnMessageLoop() OVERRIDE {
1442    // Move the mouse to the other folder on the bookmark bar and press the
1443    // left mouse button.
1444    views::TextButton* button = bb_view_->other_bookmarked_button();
1445    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1446        ui_controls::DOWN | ui_controls::UP,
1447        CreateEventTask(this, &BookmarkBarViewTest17::Step2));
1448  }
1449
1450 private:
1451  void Step2() {
1452    // Menu should be showing.
1453    views::MenuItemView* menu = bb_view_->GetMenu();
1454    ASSERT_TRUE(menu != NULL);
1455    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1456
1457    // Right click on the second item to show its context menu.
1458    views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
1459    ASSERT_TRUE(child_menu != NULL);
1460    ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1461        ui_controls::DOWN | ui_controls::UP, base::Closure());
1462    // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1463  }
1464
1465  void Step3() {
1466    // Make sure the context menu is showing.
1467    views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1468    ASSERT_TRUE(context_menu != NULL);
1469    ASSERT_TRUE(context_menu->GetSubmenu());
1470    ASSERT_TRUE(context_menu->GetSubmenu()->IsShowing());
1471
1472    // Right click on the first menu item to trigger its context menu.
1473    views::MenuItemView* menu = bb_view_->GetMenu();
1474    ASSERT_TRUE(menu != NULL);
1475    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1476    views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1477    ASSERT_TRUE(child_menu != NULL);
1478
1479    // The context menu and child_menu can be overlapped, calculate the
1480    // non-intersected Rect of the child menu and click on its center to make
1481    // sure the click is always on the child menu.
1482    gfx::Rect context_rect = context_menu->GetSubmenu()->GetBoundsInScreen();
1483    gfx::Rect child_menu_rect = child_menu->GetBoundsInScreen();
1484    gfx::Rect clickable_rect =
1485        gfx::SubtractRects(child_menu_rect, context_rect);
1486    ASSERT_FALSE(clickable_rect.IsEmpty());
1487    observer_.set_task(CreateEventTask(this, &BookmarkBarViewTest17::Step4));
1488    MoveMouseAndPress(clickable_rect.CenterPoint(), ui_controls::RIGHT,
1489        ui_controls::DOWN | ui_controls::UP, base::Closure());
1490    // Step4 will be invoked by BookmarkContextMenuNotificationObserver.
1491  }
1492
1493  void Step4() {
1494    // The context menu should still be showing.
1495    views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1496    ASSERT_TRUE(context_menu != NULL);
1497
1498    // And the menu should be showing.
1499    views::MenuItemView* menu = bb_view_->GetMenu();
1500    ASSERT_TRUE(menu != NULL);
1501    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1502
1503    bb_view_->GetMenu()->GetMenuController()->CancelAll();
1504
1505    Done();
1506  }
1507
1508  BookmarkContextMenuNotificationObserver observer_;
1509};
1510
1511#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
1512// TODO(erg): linux_aura bringup: http://crbug.com/163931
1513#define MAYBE_ContextMenus3 DISABLED_ContextMenus3
1514#else
1515#define MAYBE_ContextMenus3 ContextMenus3
1516#endif
1517
1518VIEW_TEST(BookmarkBarViewTest17, MAYBE_ContextMenus3)
1519
1520// Verifies sibling menus works. Clicks on the 'other bookmarks' folder, then
1521// moves the mouse over the first item on the bookmark bar and makes sure the
1522// menu appears.
1523class BookmarkBarViewTest18 : public BookmarkBarViewEventTestBase {
1524 protected:
1525  virtual void DoTestOnMessageLoop() OVERRIDE {
1526    // Move the mouse to the other folder on the bookmark bar and press the
1527    // left mouse button.
1528    views::TextButton* button = bb_view_->other_bookmarked_button();
1529    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1530        ui_controls::DOWN | ui_controls::UP,
1531        CreateEventTask(this, &BookmarkBarViewTest18::Step2));
1532  }
1533
1534 private:
1535  void Step2() {
1536    // Menu should be showing.
1537    views::MenuItemView* menu = bb_view_->GetMenu();
1538    ASSERT_TRUE(menu != NULL);
1539    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1540
1541    // Move the mouse to the first folder on the bookmark bar
1542    views::TextButton* button = GetBookmarkButton(0);
1543    gfx::Point button_center(button->width() / 2, button->height() / 2);
1544    views::View::ConvertPointToScreen(button, &button_center);
1545    ui_controls::SendMouseMoveNotifyWhenDone(
1546        button_center.x(), button_center.y(),
1547        CreateEventTask(this, &BookmarkBarViewTest18::Step3));
1548  }
1549
1550  void Step3() {
1551    // Make sure the menu is showing.
1552    views::MenuItemView* menu = bb_view_->GetMenu();
1553    ASSERT_TRUE(menu != NULL);
1554    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1555
1556    // The menu for the first folder should be in the pressed state (since the
1557    // menu is showing for it).
1558    EXPECT_EQ(views::CustomButton::STATE_PRESSED,
1559              GetBookmarkButton(0)->state());
1560
1561    menu->GetMenuController()->CancelAll();
1562
1563    Done();
1564  }
1565};
1566
1567#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1568// TODO(erg): linux_aura bringup: http://crbug.com/163931
1569#define MAYBE_BookmarkBarViewTest18_SiblingMenu \
1570  DISABLED_BookmarkBarViewTest18_SiblingMenu
1571#else
1572#define MAYBE_BookmarkBarViewTest18_SiblingMenu \
1573  BookmarkBarViewTest18_SiblingMenu
1574#endif
1575
1576VIEW_TEST(BookmarkBarViewTest18, MAYBE_BookmarkBarViewTest18_SiblingMenu)
1577
1578// Verifies mousing over an already open sibling menu doesn't prematurely cancel
1579// the menu.
1580class BookmarkBarViewTest19 : public BookmarkBarViewEventTestBase {
1581 protected:
1582  virtual void DoTestOnMessageLoop() OVERRIDE {
1583    // Move the mouse to the other folder on the bookmark bar and press the
1584    // left mouse button.
1585    views::TextButton* button = bb_view_->other_bookmarked_button();
1586    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1587        ui_controls::DOWN | ui_controls::UP,
1588        CreateEventTask(this, &BookmarkBarViewTest19::Step2));
1589  }
1590
1591 private:
1592  void Step2() {
1593    // Menu should be showing.
1594    views::MenuItemView* menu = bb_view_->GetMenu();
1595    ASSERT_TRUE(menu != NULL);
1596    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1597
1598    // Click on the first folder.
1599    views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1600    ASSERT_TRUE(child_menu != NULL);
1601    ui_test_utils::MoveMouseToCenterAndPress(
1602        child_menu, ui_controls::LEFT,
1603        ui_controls::DOWN | ui_controls::UP,
1604        CreateEventTask(this, &BookmarkBarViewTest19::Step3));
1605  }
1606
1607  void Step3() {
1608    // Make sure the menu is showing.
1609    views::MenuItemView* menu = bb_view_->GetMenu();
1610    ASSERT_TRUE(menu != NULL);
1611    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1612
1613    // Move the mouse back to the other bookmark button.
1614    views::TextButton* button = bb_view_->other_bookmarked_button();
1615    gfx::Point button_center(button->width() / 2, button->height() / 2);
1616    views::View::ConvertPointToScreen(button, &button_center);
1617    ui_controls::SendMouseMoveNotifyWhenDone(
1618        button_center.x() + 1, button_center.y() + 1,
1619        CreateEventTask(this, &BookmarkBarViewTest19::Step4));
1620  }
1621
1622  void Step4() {
1623    // Menu should be showing.
1624    views::MenuItemView* menu = bb_view_->GetMenu();
1625    ASSERT_TRUE(menu != NULL);
1626    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1627
1628    // Click on the first folder.
1629    views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1630    ASSERT_TRUE(child_menu != NULL);
1631    ui_test_utils::MoveMouseToCenterAndPress(
1632        child_menu,
1633        ui_controls::LEFT,
1634        ui_controls::DOWN | ui_controls::UP,
1635        CreateEventTask(this, &BookmarkBarViewTest19::Step5));
1636  }
1637
1638  void Step5() {
1639    // Make sure the menu is showing.
1640    views::MenuItemView* menu = bb_view_->GetMenu();
1641    ASSERT_TRUE(menu != NULL);
1642    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1643
1644    menu->GetMenuController()->CancelAll();
1645
1646    Done();
1647  }
1648};
1649
1650VIEW_TEST(BookmarkBarViewTest19, BookmarkBarViewTest19_SiblingMenu)
1651
1652// Verify that when clicking a mouse button outside a context menu,
1653// the context menu is dismissed *and* the underlying view receives
1654// the the mouse event (due to event reposting).
1655class BookmarkBarViewTest20 : public BookmarkBarViewEventTestBase {
1656 public:
1657  BookmarkBarViewTest20() : test_view_(new TestViewForMenuExit) {}
1658
1659 protected:
1660  virtual void DoTestOnMessageLoop() OVERRIDE {
1661    // Add |test_view_| next to |bb_view_|.
1662    views::View* parent = bb_view_->parent();
1663    views::View* container_view = new ContainerViewForMenuExit;
1664    container_view->AddChildView(bb_view_.get());
1665    container_view->AddChildView(test_view_);
1666    parent->AddChildView(container_view);
1667    parent->Layout();
1668
1669    ASSERT_EQ(test_view_->press_count(), 0);
1670
1671    // Move the mouse to the Test View and press the left mouse button.
1672    ui_test_utils::MoveMouseToCenterAndPress(
1673        test_view_, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1674        CreateEventTask(this, &BookmarkBarViewTest20::Step1));
1675  }
1676
1677 private:
1678  void Step1() {
1679    ASSERT_EQ(test_view_->press_count(), 1);
1680    ASSERT_TRUE(bb_view_->GetMenu() == NULL);
1681
1682    // Move the mouse to the first folder on the bookmark bar and press the
1683    // left mouse button.
1684    views::TextButton* button = GetBookmarkButton(0);
1685    ui_test_utils::MoveMouseToCenterAndPress(
1686        button, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1687        CreateEventTask(this, &BookmarkBarViewTest20::Step2));
1688  }
1689
1690  void Step2() {
1691    ASSERT_EQ(test_view_->press_count(), 1);
1692    views::MenuItemView* menu = bb_view_->GetMenu();
1693    ASSERT_TRUE(menu != NULL);
1694    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1695
1696    // Move the mouse to the Test View and press the left mouse button.
1697    // The context menu will consume the event and exit. Thereafter,
1698    // the event is reposted and delivered to the Test View which
1699    // increases its press-count.
1700    ui_test_utils::MoveMouseToCenterAndPress(
1701        test_view_, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1702        CreateEventTask(this, &BookmarkBarViewTest20::Step3));
1703  }
1704
1705  void Step3() {
1706    ASSERT_EQ(test_view_->press_count(), 2);
1707    ASSERT_TRUE(bb_view_->GetMenu() == NULL);
1708    Done();
1709  }
1710
1711  class ContainerViewForMenuExit : public views::View {
1712   public:
1713    ContainerViewForMenuExit() {
1714    }
1715
1716    virtual void Layout() OVERRIDE {
1717      DCHECK_EQ(2, child_count());
1718      views::View* bb_view = child_at(0);
1719      views::View* test_view = child_at(1);
1720      const int width = bb_view->width();
1721      const int height = bb_view->height();
1722      bb_view->SetBounds(0,0, width - 22, height);
1723      test_view->SetBounds(width - 20, 0, 20, height);
1724    }
1725
1726   private:
1727
1728    DISALLOW_COPY_AND_ASSIGN(ContainerViewForMenuExit);
1729  };
1730
1731  class TestViewForMenuExit : public views::View {
1732   public:
1733    TestViewForMenuExit() : press_count_(0) {
1734    }
1735    virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE {
1736      ++press_count_;
1737      return true;
1738    }
1739    int press_count() const { return press_count_; }
1740
1741   private:
1742    int press_count_;
1743
1744    DISALLOW_COPY_AND_ASSIGN(TestViewForMenuExit);
1745  };
1746
1747  TestViewForMenuExit* test_view_;
1748};
1749
1750#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1751// TODO(erg): linux_aura bringup: http://crbug.com/163931
1752#define MAYBE_ContextMenuExitTest DISABLED_ContextMenuExitTest
1753#else
1754#define MAYBE_ContextMenuExitTest ContextMenuExitTest
1755#endif
1756
1757VIEW_TEST(BookmarkBarViewTest20, MAYBE_ContextMenuExitTest)
1758
1759// Tests context menu by way of opening a context menu for a empty folder menu.
1760// The opened context menu should behave as it is from the folder button.
1761class BookmarkBarViewTest21 : public BookmarkBarViewEventTestBase {
1762 public:
1763  BookmarkBarViewTest21()
1764      : observer_(CreateEventTask(this, &BookmarkBarViewTest21::Step3)) {
1765  }
1766
1767 protected:
1768  // Move the mouse to the empty folder on the bookmark bar and press the
1769  // left mouse button.
1770  virtual void DoTestOnMessageLoop() OVERRIDE {
1771    views::TextButton* button = GetBookmarkButton(5);
1772    ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1773        ui_controls::DOWN | ui_controls::UP,
1774        CreateEventTask(this, &BookmarkBarViewTest21::Step2));
1775  }
1776
1777 private:
1778  // Confirm that a menu for empty folder shows and right click the menu.
1779  void Step2() {
1780    // Menu should be showing.
1781    views::MenuItemView* menu = bb_view_->GetMenu();
1782    ASSERT_TRUE(menu != NULL);
1783
1784    views::SubmenuView* submenu = menu->GetSubmenu();
1785    ASSERT_TRUE(submenu->IsShowing());
1786    ASSERT_EQ(1, submenu->child_count());
1787
1788    views::View* view = submenu->child_at(0);
1789    ASSERT_TRUE(view != NULL);
1790    EXPECT_EQ(views::MenuItemView::kEmptyMenuItemViewID, view->id());
1791
1792    // Right click on the first child to get its context menu.
1793    ui_test_utils::MoveMouseToCenterAndPress(view, ui_controls::RIGHT,
1794        ui_controls::DOWN | ui_controls::UP, base::Closure());
1795    // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1796  }
1797
1798  // Confirm that context menu shows and click REMOVE menu.
1799  void Step3() {
1800    // Make sure the context menu is showing.
1801    views::MenuItemView* menu = bb_view_->GetContextMenu();
1802    ASSERT_TRUE(menu != NULL);
1803    ASSERT_TRUE(menu->GetSubmenu());
1804    ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1805
1806    views::MenuItemView* delete_menu =
1807        menu->GetMenuItemByID(IDC_BOOKMARK_BAR_REMOVE);
1808    ASSERT_TRUE(delete_menu);
1809
1810    // Click on the delete menu item.
1811    ui_test_utils::MoveMouseToCenterAndPress(delete_menu,
1812        ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1813        CreateEventTask(this, &BookmarkBarViewTest21::Step4));
1814  }
1815
1816  // Confirm that the empty folder gets removed and menu doesn't show.
1817  void Step4() {
1818    views::TextButton* button = GetBookmarkButton(5);
1819    ASSERT_TRUE(button);
1820    EXPECT_EQ(ASCIIToUTF16("d"), button->text());
1821    EXPECT_TRUE(bb_view_->GetContextMenu() == NULL);
1822    EXPECT_TRUE(bb_view_->GetMenu() == NULL);
1823
1824    Done();
1825  }
1826
1827  BookmarkContextMenuNotificationObserver observer_;
1828};
1829
1830VIEW_TEST(BookmarkBarViewTest21, ContextMenusForEmptyFolder)
1831