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