NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_bar_view_test.cc
blobd8887c24ed9b9d890d4dffa8d70174181658113f
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.
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_factory.h"
13 #include "chrome/browser/chrome_content_browser_client.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_tabstrip.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
22 #include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
23 #include "chrome/common/chrome_content_client.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/test/base/interactive_test_utils.h"
26 #include "chrome/test/base/scoped_testing_local_state.h"
27 #include "chrome/test/base/test_browser_window.h"
28 #include "chrome/test/base/testing_browser_process.h"
29 #include "chrome/test/base/testing_profile.h"
30 #include "chrome/test/base/ui_test_utils.h"
31 #include "chrome/test/base/view_event_test_base.h"
32 #include "components/bookmarks/browser/bookmark_model.h"
33 #include "components/bookmarks/test/bookmark_test_helpers.h"
34 #include "components/constrained_window/constrained_window_views.h"
35 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/page_navigator.h"
37 #include "content/public/test/test_browser_thread.h"
38 #include "ui/aura/env.h"
39 #include "ui/aura/env_observer.h"
40 #include "ui/aura/window.h"
41 #include "ui/base/clipboard/clipboard.h"
42 #include "ui/base/test/ui_controls.h"
43 #include "ui/events/keycodes/keyboard_codes.h"
44 #include "ui/views/controls/button/menu_button.h"
45 #include "ui/views/controls/menu/menu_controller.h"
46 #include "ui/views/controls/menu/menu_item_view.h"
47 #include "ui/views/controls/menu/submenu_view.h"
48 #include "ui/views/widget/widget.h"
50 using base::ASCIIToUTF16;
51 using bookmarks::BookmarkModel;
52 using bookmarks::BookmarkNode;
53 using content::BrowserThread;
54 using content::OpenURLParams;
55 using content::PageNavigator;
56 using content::WebContents;
58 namespace {
60 // Waits for a views::Widget dialog to show up.
61 class DialogWaiter : public aura::EnvObserver,
62 public views::WidgetObserver {
63 public:
64 DialogWaiter()
65 : dialog_created_(false),
66 dialog_(NULL) {
67 aura::Env::GetInstance()->AddObserver(this);
70 ~DialogWaiter() override { aura::Env::GetInstance()->RemoveObserver(this); }
72 views::Widget* WaitForDialog() {
73 if (dialog_created_)
74 return dialog_;
75 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
76 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
77 base::RunLoop run_loop;
78 quit_closure_ = run_loop.QuitClosure();
79 run_loop.Run();
80 return dialog_;
83 private:
84 // aura::EnvObserver:
85 void OnWindowInitialized(aura::Window* window) override {
86 if (dialog_)
87 return;
88 views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
89 if (!widget || !widget->IsDialogBox())
90 return;
91 dialog_ = widget;
92 dialog_->AddObserver(this);
95 // views::WidgetObserver:
96 void OnWidgetVisibilityChanged(views::Widget* widget, bool visible) override {
97 CHECK_EQ(dialog_, widget);
98 if (visible) {
99 dialog_created_ = true;
100 dialog_->RemoveObserver(this);
101 if (!quit_closure_.is_null())
102 quit_closure_.Run();
106 bool dialog_created_;
107 views::Widget* dialog_;
108 base::Closure quit_closure_;
110 DISALLOW_COPY_AND_ASSIGN(DialogWaiter);
113 // Waits for a dialog to terminate.
114 class DialogCloseWaiter : public views::WidgetObserver {
115 public:
116 explicit DialogCloseWaiter(views::Widget* dialog)
117 : dialog_closed_(false) {
118 dialog->AddObserver(this);
121 ~DialogCloseWaiter() override {
122 // It is not necessary to remove |this| from the dialog's observer, since
123 // the dialog is destroyed before this waiter.
126 void WaitForDialogClose() {
127 if (dialog_closed_)
128 return;
129 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
130 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
131 base::RunLoop run_loop;
132 quit_closure_ = run_loop.QuitClosure();
133 run_loop.Run();
136 private:
137 // views::WidgetObserver:
138 void OnWidgetDestroyed(views::Widget* widget) override {
139 dialog_closed_ = true;
140 if (!quit_closure_.is_null())
141 quit_closure_.Run();
144 bool dialog_closed_;
145 base::Closure quit_closure_;
147 DISALLOW_COPY_AND_ASSIGN(DialogCloseWaiter);
150 // Waits for a views::Widget to receive a Tab key.
151 class TabKeyWaiter : public ui::EventHandler {
152 public:
153 explicit TabKeyWaiter(views::Widget* widget)
154 : widget_(widget),
155 received_tab_(false) {
156 widget_->GetNativeView()->AddPreTargetHandler(this);
159 ~TabKeyWaiter() override {
160 widget_->GetNativeView()->RemovePreTargetHandler(this);
163 void WaitForTab() {
164 if (received_tab_)
165 return;
166 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
167 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
168 base::RunLoop run_loop;
169 quit_closure_ = run_loop.QuitClosure();
170 run_loop.Run();
173 private:
174 // ui::EventHandler:
175 void OnKeyEvent(ui::KeyEvent* event) override {
176 if (event->type() == ui::ET_KEY_RELEASED &&
177 event->key_code() == ui::VKEY_TAB) {
178 received_tab_ = true;
179 if (!quit_closure_.is_null())
180 quit_closure_.Run();
184 views::Widget* widget_;
185 bool received_tab_;
186 base::Closure quit_closure_;
188 DISALLOW_COPY_AND_ASSIGN(TabKeyWaiter);
191 void MoveMouseAndPress(const gfx::Point& screen_pos,
192 ui_controls::MouseButton button,
193 int state,
194 const base::Closure& closure) {
195 ui_controls::SendMouseMove(screen_pos.x(), screen_pos.y());
196 ui_controls::SendMouseEventsNotifyWhenDone(button, state, closure);
199 // PageNavigator implementation that records the URL.
200 class TestingPageNavigator : public PageNavigator {
201 public:
202 WebContents* OpenURL(const OpenURLParams& params) override {
203 url_ = params.url;
204 return NULL;
207 GURL url_;
210 // TODO(erg): Fix bookmark DND tests on linux_aura. crbug.com/163931
211 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
212 #define MAYBE(x) DISABLED_##x
213 #else
214 #define MAYBE(x) x
215 #endif
217 } // namespace
219 // Base class for event generating bookmark view tests. These test are intended
220 // to exercise View's menus, but that's easier done with BookmarkBarView rather
221 // than View's menu itself.
223 // SetUp creates a bookmark model with the following structure.
224 // All folders are in upper case, all URLs in lower case.
225 // F1
226 // f1a
227 // F11
228 // f11a
229 // *
230 // a
231 // b
232 // c
233 // d
234 // F2
235 // e
236 // OTHER
237 // oa
238 // OF
239 // ofa
240 // ofb
241 // OF2
242 // of2a
243 // of2b
245 // * if CreateBigMenu returns return true, 100 menu items are created here with
246 // the names f1-f100.
248 // Subclasses should be sure and invoke super's implementation of SetUp and
249 // TearDown.
250 class BookmarkBarViewEventTestBase : public ViewEventTestBase {
251 public:
252 BookmarkBarViewEventTestBase()
253 : ViewEventTestBase(),
254 model_(NULL) {}
256 void SetUp() override {
257 content_client_.reset(new ChromeContentClient);
258 content::SetContentClient(content_client_.get());
259 browser_content_client_.reset(new chrome::ChromeContentBrowserClient());
260 content::SetBrowserClientForTesting(browser_content_client_.get());
262 views::MenuController::TurnOffMenuSelectionHoldForTest();
263 BookmarkBarView::DisableAnimationsForTesting(true);
264 SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient());
266 profile_.reset(new TestingProfile());
267 profile_->CreateBookmarkModel(true);
268 model_ = BookmarkModelFactory::GetForProfile(profile_.get());
269 bookmarks::test::WaitForBookmarkModelToLoad(model_);
270 profile_->GetPrefs()->SetBoolean(bookmarks::prefs::kShowBookmarkBar, true);
272 Browser::CreateParams native_params(profile_.get(),
273 chrome::GetActiveDesktop());
274 browser_.reset(
275 chrome::CreateBrowserWithTestWindowForParams(&native_params));
277 local_state_.reset(new ScopedTestingLocalState(
278 TestingBrowserProcess::GetGlobal()));
279 model_->ClearStore();
281 bb_view_.reset(new BookmarkBarView(browser_.get(), NULL));
282 bb_view_->set_owned_by_client();
283 bb_view_->SetPageNavigator(&navigator_);
285 AddTestData(CreateBigMenu());
287 // Calculate the preferred size so that one button doesn't fit, which
288 // triggers the overflow button to appear. We have to do this incrementally
289 // as there isn't a good way to determine the point at which the overflow
290 // button is shown.
292 // This code looks a bit hacky, but I've written it so that it shouldn't
293 // be dependant upon any of the layout code in BookmarkBarView. Instead
294 // we brute force search for a size that triggers the overflow button.
295 bb_view_pref_ = bb_view_->GetPreferredSize();
296 bb_view_pref_.set_width(1000);
297 do {
298 bb_view_pref_.set_width(bb_view_pref_.width() - 25);
299 bb_view_->SetBounds(0, 0, bb_view_pref_.width(), bb_view_pref_.height());
300 bb_view_->Layout();
301 } while (GetBookmarkButton(6)->visible());
303 ViewEventTestBase::SetUp();
306 void TearDown() override {
307 // Destroy everything, then run the message loop to ensure we delete all
308 // Tasks and fully shut down.
309 browser_->tab_strip_model()->CloseAllTabs();
310 bb_view_.reset();
311 browser_.reset();
312 profile_.reset();
314 // Run the message loop to ensure we delete allTasks and fully shut down.
315 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
316 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
317 base::RunLoop run_loop;
318 loop->PostTask(FROM_HERE, run_loop.QuitClosure());
319 run_loop.Run();
321 ViewEventTestBase::TearDown();
322 BookmarkBarView::DisableAnimationsForTesting(false);
323 constrained_window::SetConstrainedWindowViewsClient(nullptr);
325 browser_content_client_.reset();
326 content_client_.reset();
327 content::SetContentClient(NULL);
330 protected:
331 views::View* CreateContentsView() override { return bb_view_.get(); }
333 gfx::Size GetPreferredSize() const override { return bb_view_pref_; }
335 views::LabelButton* GetBookmarkButton(int view_index) {
336 return bb_view_->GetBookmarkButton(view_index);
339 // See comment above class description for what this does.
340 virtual bool CreateBigMenu() { return false; }
342 BookmarkModel* model_;
343 scoped_ptr<BookmarkBarView> bb_view_;
344 TestingPageNavigator navigator_;
346 private:
347 void AddTestData(bool big_menu) {
348 const BookmarkNode* bb_node = model_->bookmark_bar_node();
349 std::string test_base = "file:///c:/tmp/";
350 const BookmarkNode* f1 = model_->AddFolder(bb_node, 0, ASCIIToUTF16("F1"));
351 model_->AddURL(f1, 0, ASCIIToUTF16("f1a"), GURL(test_base + "f1a"));
352 const BookmarkNode* f11 = model_->AddFolder(f1, 1, ASCIIToUTF16("F11"));
353 model_->AddURL(f11, 0, ASCIIToUTF16("f11a"), GURL(test_base + "f11a"));
354 if (big_menu) {
355 for (int i = 1; i <= 100; ++i) {
356 model_->AddURL(f1, i + 1, ASCIIToUTF16("f") + base::IntToString16(i),
357 GURL(test_base + "f" + base::IntToString(i)));
360 model_->AddURL(bb_node, 1, ASCIIToUTF16("a"), GURL(test_base + "a"));
361 model_->AddURL(bb_node, 2, ASCIIToUTF16("b"), GURL(test_base + "b"));
362 model_->AddURL(bb_node, 3, ASCIIToUTF16("c"), GURL(test_base + "c"));
363 model_->AddURL(bb_node, 4, ASCIIToUTF16("d"), GURL(test_base + "d"));
364 model_->AddFolder(bb_node, 5, ASCIIToUTF16("F2"));
365 model_->AddURL(bb_node, 6, ASCIIToUTF16("d"), GURL(test_base + "d"));
367 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("oa"),
368 GURL(test_base + "oa"));
369 const BookmarkNode* of = model_->AddFolder(model_->other_node(), 1,
370 ASCIIToUTF16("OF"));
371 model_->AddURL(of, 0, ASCIIToUTF16("ofa"), GURL(test_base + "ofa"));
372 model_->AddURL(of, 1, ASCIIToUTF16("ofb"), GURL(test_base + "ofb"));
373 const BookmarkNode* of2 = model_->AddFolder(model_->other_node(), 2,
374 ASCIIToUTF16("OF2"));
375 model_->AddURL(of2, 0, ASCIIToUTF16("of2a"), GURL(test_base + "of2a"));
376 model_->AddURL(of2, 1, ASCIIToUTF16("of2b"), GURL(test_base + "of2b"));
379 gfx::Size bb_view_pref_;
380 scoped_ptr<ChromeContentClient> content_client_;
381 scoped_ptr<chrome::ChromeContentBrowserClient> browser_content_client_;
382 scoped_ptr<TestingProfile> profile_;
383 scoped_ptr<Browser> browser_;
384 scoped_ptr<ScopedTestingLocalState> local_state_;
387 // Clicks on first menu, makes sure button is depressed. Moves mouse to first
388 // child, clicks it and makes sure a navigation occurs.
389 class BookmarkBarViewTest1 : public BookmarkBarViewEventTestBase {
390 protected:
391 void DoTestOnMessageLoop() override {
392 // Move the mouse to the first folder on the bookmark bar and press the
393 // mouse.
394 views::LabelButton* button = GetBookmarkButton(0);
395 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
396 ui_controls::DOWN | ui_controls::UP,
397 CreateEventTask(this, &BookmarkBarViewTest1::Step2));
400 private:
401 void Step2() {
402 // Menu should be showing.
403 views::MenuItemView* menu = bb_view_->GetMenu();
404 ASSERT_TRUE(menu != NULL);
405 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
407 // Button should be depressed.
408 views::LabelButton* button = GetBookmarkButton(0);
409 ASSERT_TRUE(button->state() == views::CustomButton::STATE_PRESSED);
411 // Click on the 2nd menu item (A URL).
412 ASSERT_TRUE(menu->GetSubmenu());
414 views::MenuItemView* menu_to_select =
415 menu->GetSubmenu()->GetMenuItemAt(0);
416 ui_test_utils::MoveMouseToCenterAndPress(menu_to_select, ui_controls::LEFT,
417 ui_controls::DOWN | ui_controls::UP,
418 CreateEventTask(this, &BookmarkBarViewTest1::Step3));
421 void Step3() {
422 // We should have navigated to URL f1a.
423 ASSERT_TRUE(navigator_.url_ ==
424 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url());
426 // Make sure button is no longer pushed.
427 views::LabelButton* button = GetBookmarkButton(0);
428 ASSERT_TRUE(button->state() == views::CustomButton::STATE_NORMAL);
430 views::MenuItemView* menu = bb_view_->GetMenu();
431 ASSERT_TRUE(menu == NULL || !menu->GetSubmenu()->IsShowing());
433 Done();
437 VIEW_TEST(BookmarkBarViewTest1, Basic)
439 // Brings up menu, clicks on empty space and make sure menu hides.
440 class BookmarkBarViewTest2 : public BookmarkBarViewEventTestBase {
441 protected:
442 void DoTestOnMessageLoop() override {
443 // Move the mouse to the first folder on the bookmark bar and press the
444 // mouse.
445 views::LabelButton* button = GetBookmarkButton(0);
446 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
447 ui_controls::DOWN | ui_controls::UP,
448 CreateEventTask(this, &BookmarkBarViewTest2::Step2));
451 private:
452 void Step2() {
453 // Menu should be showing.
454 views::MenuItemView* menu = bb_view_->GetMenu();
455 ASSERT_TRUE(menu != NULL && menu->GetSubmenu()->IsShowing());
457 // Click on 0x0, which should trigger closing menu.
458 // NOTE: this code assume there is a left margin, which is currently
459 // true. If that changes, this code will need to find another empty space
460 // to press the mouse on.
461 gfx::Point mouse_loc;
462 views::View::ConvertPointToScreen(bb_view_.get(), &mouse_loc);
463 ui_controls::SendMouseMoveNotifyWhenDone(0, 0,
464 CreateEventTask(this, &BookmarkBarViewTest2::Step3));
467 void Step3() {
468 // As the click is on the desktop the hook never sees the up, so we only
469 // wait on the down. We still send the up though else the system thinks
470 // the mouse is still down.
471 ui_controls::SendMouseEventsNotifyWhenDone(
472 ui_controls::LEFT, ui_controls::DOWN,
473 CreateEventTask(this, &BookmarkBarViewTest2::Step4));
474 ui_controls::SendMouseEvents(ui_controls::LEFT, ui_controls::UP);
477 void Step4() {
478 // The menu shouldn't be showing.
479 views::MenuItemView* menu = bb_view_->GetMenu();
480 ASSERT_TRUE(menu == NULL || !menu->GetSubmenu()->IsShowing());
482 // Make sure button is no longer pushed.
483 views::LabelButton* button = GetBookmarkButton(0);
484 ASSERT_TRUE(button->state() == views::CustomButton::STATE_NORMAL);
486 Done();
490 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
491 // TODO(erg): linux_aura bringup: http://crbug.com/163931
492 #define MAYBE_HideOnDesktopClick DISABLED_HideOnDesktopClick
493 #else
494 #define MAYBE_HideOnDesktopClick HideOnDesktopClick
495 #endif
497 VIEW_TEST(BookmarkBarViewTest2, MAYBE_HideOnDesktopClick)
499 // Brings up menu. Moves over child to make sure submenu appears, moves over
500 // another child and make sure next menu appears.
501 class BookmarkBarViewTest3 : public BookmarkBarViewEventTestBase {
502 protected:
503 void DoTestOnMessageLoop() override {
504 // Move the mouse to the first folder on the bookmark bar and press the
505 // mouse.
506 views::MenuButton* button = bb_view_->other_bookmarks_button();
507 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
508 ui_controls::DOWN | ui_controls::UP,
509 CreateEventTask(this, &BookmarkBarViewTest3::Step2));
512 private:
513 void Step2() {
514 // Menu should be showing.
515 views::MenuItemView* menu = bb_view_->GetMenu();
516 ASSERT_TRUE(menu != NULL);
517 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
519 views::MenuItemView* child_menu =
520 menu->GetSubmenu()->GetMenuItemAt(1);
521 ASSERT_TRUE(child_menu != NULL);
523 // Click on second child, which has a submenu.
524 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
525 ui_controls::DOWN | ui_controls::UP,
526 CreateEventTask(this, &BookmarkBarViewTest3::Step3));
529 void Step3() {
530 // Make sure sub menu is showing.
531 views::MenuItemView* menu = bb_view_->GetMenu();
532 ASSERT_TRUE(menu);
533 views::MenuItemView* child_menu =
534 menu->GetSubmenu()->GetMenuItemAt(1);
535 ASSERT_TRUE(child_menu->GetSubmenu() != NULL);
536 ASSERT_TRUE(child_menu->GetSubmenu()->IsShowing());
538 // Click on third child, which has a submenu too.
539 child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
540 ASSERT_TRUE(child_menu != NULL);
541 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
542 ui_controls::DOWN | ui_controls::UP,
543 CreateEventTask(this, &BookmarkBarViewTest3::Step4));
546 void Step4() {
547 // Make sure sub menu we first clicked isn't showing.
548 views::MenuItemView* menu = bb_view_->GetMenu();
549 views::MenuItemView* child_menu =
550 menu->GetSubmenu()->GetMenuItemAt(1);
551 ASSERT_TRUE(child_menu->GetSubmenu() != NULL);
552 ASSERT_FALSE(child_menu->GetSubmenu()->IsShowing());
554 // And submenu we last clicked is showing.
555 child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
556 ASSERT_TRUE(child_menu != NULL);
557 ASSERT_TRUE(child_menu->GetSubmenu()->IsShowing());
559 // Nothing should have been selected.
560 EXPECT_EQ(GURL(), navigator_.url_);
562 // Hide menu.
563 menu->GetMenuController()->CancelAll();
565 Done();
569 VIEW_TEST(BookmarkBarViewTest3, Submenus)
571 // Observer that posts task upon the context menu creation.
572 // This is necessary for Linux as the context menu has to check
573 // the clipboard, which invokes the event loop.
574 class BookmarkContextMenuNotificationObserver
575 : public content::NotificationObserver {
576 public:
577 explicit BookmarkContextMenuNotificationObserver(const base::Closure& task)
578 : task_(task) {
579 registrar_.Add(this,
580 chrome::NOTIFICATION_BOOKMARK_CONTEXT_MENU_SHOWN,
581 content::NotificationService::AllSources());
584 void Observe(int type,
585 const content::NotificationSource& source,
586 const content::NotificationDetails& details) override {
587 base::MessageLoop::current()->PostTask(FROM_HERE, task_);
590 // Sets the task that is posted when the context menu is shown.
591 void set_task(const base::Closure& task) { task_ = task; }
593 private:
594 content::NotificationRegistrar registrar_;
595 base::Closure task_;
597 DISALLOW_COPY_AND_ASSIGN(BookmarkContextMenuNotificationObserver);
600 // Tests context menus by way of opening a context menu for a bookmark,
601 // then right clicking to get context menu and selecting the first menu item
602 // (open).
603 class BookmarkBarViewTest4 : public BookmarkBarViewEventTestBase {
604 public:
605 BookmarkBarViewTest4()
606 : observer_(CreateEventTask(this, &BookmarkBarViewTest4::Step3)) {
609 protected:
610 void DoTestOnMessageLoop() override {
611 // Move the mouse to the first folder on the bookmark bar and press the
612 // mouse.
613 views::LabelButton* button = bb_view_->other_bookmarks_button();
614 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
615 ui_controls::DOWN | ui_controls::UP,
616 CreateEventTask(this, &BookmarkBarViewTest4::Step2));
619 private:
620 void Step2() {
621 // Menu should be showing.
622 views::MenuItemView* menu = bb_view_->GetMenu();
623 ASSERT_TRUE(menu != NULL);
624 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
626 views::MenuItemView* child_menu =
627 menu->GetSubmenu()->GetMenuItemAt(0);
628 ASSERT_TRUE(child_menu != NULL);
630 // Right click on the first child to get its context menu.
631 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
632 ui_controls::DOWN | ui_controls::UP, base::Closure());
633 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
636 void Step3() {
637 // Make sure the context menu is showing.
638 views::MenuItemView* menu = bb_view_->GetContextMenu();
639 ASSERT_TRUE(menu != NULL);
640 ASSERT_TRUE(menu->GetSubmenu());
641 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
643 // Select the first menu item (open).
644 ui_test_utils::MoveMouseToCenterAndPress(
645 menu->GetSubmenu()->GetMenuItemAt(0),
646 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
647 CreateEventTask(this, &BookmarkBarViewTest4::Step4));
650 void Step4() {
651 EXPECT_EQ(navigator_.url_, model_->other_node()->GetChild(0)->url());
652 Done();
655 BookmarkContextMenuNotificationObserver observer_;
658 VIEW_TEST(BookmarkBarViewTest4, ContextMenus)
660 // Tests drag and drop within the same menu.
661 class BookmarkBarViewTest5 : public BookmarkBarViewEventTestBase {
662 protected:
663 void DoTestOnMessageLoop() override {
664 url_dragging_ =
665 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
667 // Move the mouse to the first folder on the bookmark bar and press the
668 // mouse.
669 views::LabelButton* button = GetBookmarkButton(0);
670 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
671 ui_controls::DOWN | ui_controls::UP,
672 CreateEventTask(this, &BookmarkBarViewTest5::Step2));
675 private:
676 void Step2() {
677 // Menu should be showing.
678 views::MenuItemView* menu = bb_view_->GetMenu();
679 ASSERT_TRUE(menu != NULL);
680 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
682 views::MenuItemView* child_menu =
683 menu->GetSubmenu()->GetMenuItemAt(0);
684 ASSERT_TRUE(child_menu != NULL);
686 // Move mouse to center of menu and press button.
687 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
688 ui_controls::DOWN,
689 CreateEventTask(this, &BookmarkBarViewTest5::Step3));
692 void Step3() {
693 views::MenuItemView* target_menu =
694 bb_view_->GetMenu()->GetSubmenu()->GetMenuItemAt(1);
695 gfx::Point loc(1, target_menu->height() - 1);
696 views::View::ConvertPointToScreen(target_menu, &loc);
698 // Start a drag.
699 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
700 CreateEventTask(this, &BookmarkBarViewTest5::Step4));
702 // See comment above this method as to why we do this.
703 ScheduleMouseMoveInBackground(loc.x(), loc.y());
706 void Step4() {
707 // Drop the item so that it's now the second item.
708 views::MenuItemView* target_menu =
709 bb_view_->GetMenu()->GetSubmenu()->GetMenuItemAt(1);
710 gfx::Point loc(1, target_menu->height() - 2);
711 views::View::ConvertPointToScreen(target_menu, &loc);
712 ui_controls::SendMouseMove(loc.x(), loc.y());
714 ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT,
715 ui_controls::UP,
716 CreateEventTask(this, &BookmarkBarViewTest5::Step5));
719 void Step5() {
720 GURL url = model_->bookmark_bar_node()->GetChild(0)->GetChild(1)->url();
721 EXPECT_EQ(url_dragging_, url);
722 Done();
725 GURL url_dragging_;
728 #if !defined(OS_WIN) // flaky http://crbug.com/400578
729 VIEW_TEST(BookmarkBarViewTest5, DND)
730 #endif
732 // Tests holding mouse down on overflow button, dragging such that menu pops up
733 // then selecting an item.
734 class BookmarkBarViewTest6 : public BookmarkBarViewEventTestBase {
735 protected:
736 void DoTestOnMessageLoop() override {
737 // Press the mouse button on the overflow button. Don't release it though.
738 views::LabelButton* button = bb_view_->overflow_button();
739 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
740 ui_controls::DOWN, CreateEventTask(this, &BookmarkBarViewTest6::Step2));
743 private:
744 void Step2() {
745 // Menu should be showing.
746 views::MenuItemView* menu = bb_view_->GetMenu();
747 ASSERT_TRUE(menu != NULL);
748 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
750 views::MenuItemView* child_menu =
751 menu->GetSubmenu()->GetMenuItemAt(0);
752 ASSERT_TRUE(child_menu != NULL);
754 // Move mouse to center of menu and release mouse.
755 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
756 ui_controls::UP, CreateEventTask(this, &BookmarkBarViewTest6::Step3));
759 void Step3() {
760 ASSERT_TRUE(navigator_.url_ ==
761 model_->bookmark_bar_node()->GetChild(6)->url());
762 Done();
765 GURL url_dragging_;
768 VIEW_TEST(BookmarkBarViewTest6, OpenMenuOnClickAndHold)
770 // Tests drag and drop to different menu.
771 class BookmarkBarViewTest7 : public BookmarkBarViewEventTestBase {
772 protected:
773 void DoTestOnMessageLoop() override {
774 url_dragging_ =
775 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
777 // Move the mouse to the first folder on the bookmark bar and press the
778 // mouse.
779 views::LabelButton* button = GetBookmarkButton(0);
780 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
781 ui_controls::DOWN | ui_controls::UP,
782 CreateEventTask(this, &BookmarkBarViewTest7::Step2));
785 private:
786 void Step2() {
787 // Menu should be showing.
788 views::MenuItemView* menu = bb_view_->GetMenu();
789 ASSERT_TRUE(menu != NULL);
790 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
792 views::MenuItemView* child_menu =
793 menu->GetSubmenu()->GetMenuItemAt(0);
794 ASSERT_TRUE(child_menu != NULL);
796 // Move mouse to center of menu and press button.
797 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
798 ui_controls::DOWN,
799 CreateEventTask(this, &BookmarkBarViewTest7::Step3));
802 void Step3() {
803 // Drag over other button.
804 views::LabelButton* other_button = bb_view_->other_bookmarks_button();
805 gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
806 views::View::ConvertPointToScreen(other_button, &loc);
808 #if defined(USE_AURA)
809 // TODO: fix this. Aura requires an additional mouse event to trigger drag
810 // and drop checking state.
811 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
812 base::Bind(&BookmarkBarViewTest7::Step3A, this));
813 #else
814 // Start a drag.
815 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
816 base::Bind(&BookmarkBarViewTest7::Step4, this));
818 // See comment above this method as to why we do this.
819 ScheduleMouseMoveInBackground(loc.x(), loc.y());
820 #endif
823 void Step3A() {
824 // Drag over other button.
825 views::LabelButton* other_button = bb_view_->other_bookmarks_button();
826 gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
827 views::View::ConvertPointToScreen(other_button, &loc);
829 ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
830 base::Bind(&BookmarkBarViewTest7::Step4, this));
833 void Step4() {
834 views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
835 ASSERT_TRUE(drop_menu != NULL);
836 ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
838 views::MenuItemView* target_menu =
839 drop_menu->GetSubmenu()->GetMenuItemAt(0);
840 gfx::Point loc(1, 1);
841 views::View::ConvertPointToScreen(target_menu, &loc);
842 ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
843 CreateEventTask(this, &BookmarkBarViewTest7::Step5));
846 void Step5() {
847 ui_controls::SendMouseEventsNotifyWhenDone(
848 ui_controls::LEFT, ui_controls::UP,
849 CreateEventTask(this, &BookmarkBarViewTest7::Step6));
852 void Step6() {
853 ASSERT_TRUE(model_->other_node()->GetChild(0)->url() == url_dragging_);
854 Done();
857 GURL url_dragging_;
860 #if !defined(OS_WIN)
861 // This test passes locally (on aero and non-aero) but fails on the trybots and
862 // buildbot.
863 // http://crbug.com/154081
864 VIEW_TEST(BookmarkBarViewTest7, MAYBE(DNDToDifferentMenu))
865 #endif
867 // Drags from one menu to next so that original menu closes, then back to
868 // original menu.
869 class BookmarkBarViewTest8 : public BookmarkBarViewEventTestBase {
870 protected:
871 void DoTestOnMessageLoop() override {
872 url_dragging_ =
873 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
875 // Move the mouse to the first folder on the bookmark bar and press the
876 // mouse.
877 views::LabelButton* button = GetBookmarkButton(0);
878 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
879 ui_controls::DOWN | ui_controls::UP,
880 CreateEventTask(this, &BookmarkBarViewTest8::Step2));
883 private:
884 void Step2() {
885 // Menu should be showing.
886 views::MenuItemView* menu = bb_view_->GetMenu();
887 ASSERT_TRUE(menu != NULL);
888 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
890 views::MenuItemView* child_menu =
891 menu->GetSubmenu()->GetMenuItemAt(0);
892 ASSERT_TRUE(child_menu != NULL);
894 // Move mouse to center of menu and press button.
895 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
896 ui_controls::DOWN,
897 CreateEventTask(this, &BookmarkBarViewTest8::Step3));
900 void Step3() {
901 // Drag over other button.
902 views::LabelButton* other_button = bb_view_->other_bookmarks_button();
903 gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
904 views::View::ConvertPointToScreen(other_button, &loc);
906 // Start a drag.
907 #if defined(USE_AURA)
908 // TODO: fix this. Aura requires an additional mouse event to trigger drag
909 // and drop checking state.
910 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
911 base::Bind(&BookmarkBarViewTest8::Step3A, this));
912 #else
913 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
914 base::Bind(&BookmarkBarViewTest8::Step4, this));
915 // See comment above this method as to why we do this.
916 ScheduleMouseMoveInBackground(loc.x(), loc.y());
917 #endif
920 void Step3A() {
921 // Drag over other button.
922 views::LabelButton* other_button = bb_view_->other_bookmarks_button();
923 gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
924 views::View::ConvertPointToScreen(other_button, &loc);
926 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
927 base::Bind(&BookmarkBarViewTest8::Step4, this));
930 void Step4() {
931 views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
932 ASSERT_TRUE(drop_menu != NULL);
933 ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
935 // Now drag back over first menu.
936 views::LabelButton* button = GetBookmarkButton(0);
937 gfx::Point loc(button->width() / 2, button->height() / 2);
938 views::View::ConvertPointToScreen(button, &loc);
939 ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
940 base::Bind(&BookmarkBarViewTest8::Step5, this));
943 void Step5() {
944 // Drop on folder F11.
945 views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
946 ASSERT_TRUE(drop_menu != NULL);
947 ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
949 views::MenuItemView* target_menu =
950 drop_menu->GetSubmenu()->GetMenuItemAt(1);
951 ui_test_utils::MoveMouseToCenterAndPress(
952 target_menu, ui_controls::LEFT, ui_controls::UP,
953 CreateEventTask(this, &BookmarkBarViewTest8::Step6));
956 void Step6() {
957 // Make sure drop was processed.
958 GURL final_url = model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->
959 GetChild(1)->url();
960 ASSERT_TRUE(final_url == url_dragging_);
961 Done();
964 GURL url_dragging_;
967 #if !defined(OS_WIN)
968 // This test passes locally (on aero and non-aero) but fails on the trybots and
969 // buildbot.
970 // http://crbug.com/154081
971 VIEW_TEST(BookmarkBarViewTest8, MAYBE(DNDBackToOriginatingMenu))
972 #endif
974 // Moves the mouse over the scroll button and makes sure we get scrolling.
975 class BookmarkBarViewTest9 : public BookmarkBarViewEventTestBase {
976 protected:
977 bool CreateBigMenu() override { return true; }
979 void DoTestOnMessageLoop() override {
980 // Move the mouse to the first folder on the bookmark bar and press the
981 // mouse.
982 views::LabelButton* button = GetBookmarkButton(0);
983 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
984 ui_controls::DOWN | ui_controls::UP,
985 CreateEventTask(this, &BookmarkBarViewTest9::Step2));
988 private:
989 void Step2() {
990 // Menu should be showing.
991 views::MenuItemView* menu = bb_view_->GetMenu();
992 ASSERT_TRUE(menu != NULL);
993 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
995 first_menu_ = menu->GetSubmenu()->GetMenuItemAt(0);
996 gfx::Point menu_loc;
997 views::View::ConvertPointToScreen(first_menu_, &menu_loc);
998 start_y_ = menu_loc.y();
1000 // Move the mouse over the scroll button.
1001 views::View* scroll_container = menu->GetSubmenu()->parent();
1002 ASSERT_TRUE(scroll_container != NULL);
1003 scroll_container = scroll_container->parent();
1004 ASSERT_TRUE(scroll_container != NULL);
1005 views::View* scroll_down_button = scroll_container->child_at(1);
1006 ASSERT_TRUE(scroll_down_button);
1007 gfx::Point loc(scroll_down_button->width() / 2,
1008 scroll_down_button->height() / 2);
1009 views::View::ConvertPointToScreen(scroll_down_button, &loc);
1011 // On linux, the sending one location isn't enough.
1012 ui_controls::SendMouseMove(loc.x() - 1 , loc.y() - 1);
1013 ui_controls::SendMouseMoveNotifyWhenDone(
1014 loc.x(), loc.y(), CreateEventTask(this, &BookmarkBarViewTest9::Step3));
1017 void Step3() {
1018 base::MessageLoop::current()->PostDelayedTask(
1019 FROM_HERE,
1020 base::Bind(&BookmarkBarViewTest9::Step4, this),
1021 base::TimeDelta::FromMilliseconds(200));
1024 void Step4() {
1025 gfx::Point menu_loc;
1026 views::View::ConvertPointToScreen(first_menu_, &menu_loc);
1027 ASSERT_NE(start_y_, menu_loc.y());
1029 // Hide menu.
1030 bb_view_->GetMenu()->GetMenuController()->CancelAll();
1032 // On linux, Cancelling menu will call Quit on the message loop,
1033 // which can interfere with Done. We need to run Done in the
1034 // next execution loop.
1035 base::MessageLoop::current()->PostTask(
1036 FROM_HERE, base::Bind(&ViewEventTestBase::Done, this));
1039 int start_y_;
1040 views::MenuItemView* first_menu_;
1043 // Fails on official cros bot. crbug.com/431427.
1044 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
1045 #define MAYBE_ScrollButtonScrolls DISABLED_ScrollButtonScrolls
1046 #else
1047 #define MAYBE_ScrollButtonScrolls ScrollButtonScrolls
1048 #endif
1050 VIEW_TEST(BookmarkBarViewTest9, MAYBE_ScrollButtonScrolls)
1052 // Tests up/down/left/enter key messages.
1053 class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {
1054 protected:
1055 void DoTestOnMessageLoop() override {
1056 // Move the mouse to the first folder on the bookmark bar and press the
1057 // mouse.
1058 views::LabelButton* button = GetBookmarkButton(0);
1059 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1060 ui_controls::DOWN | ui_controls::UP,
1061 CreateEventTask(this, &BookmarkBarViewTest10::Step2));
1062 base::MessageLoop::current()->RunUntilIdle();
1065 private:
1066 void Step2() {
1067 // Menu should be showing.
1068 views::MenuItemView* menu = bb_view_->GetMenu();
1069 ASSERT_TRUE(menu != NULL);
1070 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1072 // Send a down event, which should select the first item.
1073 ui_controls::SendKeyPressNotifyWhenDone(
1074 window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
1075 CreateEventTask(this, &BookmarkBarViewTest10::Step3));
1078 void Step3() {
1079 // Make sure menu is showing and item is selected.
1080 views::MenuItemView* menu = bb_view_->GetMenu();
1081 ASSERT_TRUE(menu != NULL);
1082 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1083 ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
1085 // Send a key down event, which should select the next item.
1086 ui_controls::SendKeyPressNotifyWhenDone(
1087 window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
1088 CreateEventTask(this, &BookmarkBarViewTest10::Step4));
1091 void Step4() {
1092 views::MenuItemView* menu = bb_view_->GetMenu();
1093 ASSERT_TRUE(menu != NULL);
1094 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1095 ASSERT_FALSE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
1096 ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(1)->IsSelected());
1098 // Send a right arrow to force the menu to open.
1099 ui_controls::SendKeyPressNotifyWhenDone(
1100 window_->GetNativeWindow(), ui::VKEY_RIGHT, false, false, false, false,
1101 CreateEventTask(this, &BookmarkBarViewTest10::Step5));
1104 void Step5() {
1105 // Make sure the submenu is showing.
1106 views::MenuItemView* menu = bb_view_->GetMenu();
1107 ASSERT_TRUE(menu != NULL);
1108 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1109 views::MenuItemView* submenu = menu->GetSubmenu()->GetMenuItemAt(1);
1110 ASSERT_TRUE(submenu->IsSelected());
1111 ASSERT_TRUE(submenu->GetSubmenu());
1112 ASSERT_TRUE(submenu->GetSubmenu()->IsShowing());
1114 // Send a left arrow to close the submenu.
1115 ui_controls::SendKeyPressNotifyWhenDone(
1116 window_->GetNativeWindow(), ui::VKEY_LEFT, false, false, false, false,
1117 CreateEventTask(this, &BookmarkBarViewTest10::Step6));
1120 void Step6() {
1121 // Make sure the submenu is showing.
1122 views::MenuItemView* menu = bb_view_->GetMenu();
1123 ASSERT_TRUE(menu != NULL);
1124 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1125 views::MenuItemView* submenu = menu->GetSubmenu()->GetMenuItemAt(1);
1126 ASSERT_TRUE(submenu->IsSelected());
1127 ASSERT_TRUE(!submenu->GetSubmenu() || !submenu->GetSubmenu()->IsShowing());
1129 // Send a down arrow to wrap back to f1a
1130 ui_controls::SendKeyPressNotifyWhenDone(
1131 window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
1132 CreateEventTask(this, &BookmarkBarViewTest10::Step7));
1135 void Step7() {
1136 // Make sure menu is showing and item is selected.
1137 views::MenuItemView* menu = bb_view_->GetMenu();
1138 ASSERT_TRUE(menu != NULL);
1139 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1140 ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
1142 // Send enter, which should select the item.
1143 ui_controls::SendKeyPressNotifyWhenDone(
1144 window_->GetNativeWindow(), ui::VKEY_RETURN, false, false, false, false,
1145 CreateEventTask(this, &BookmarkBarViewTest10::Step8));
1148 void Step8() {
1149 ASSERT_TRUE(
1150 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url() ==
1151 navigator_.url_);
1152 Done();
1156 #if defined(USE_OZONE)
1157 // ozone bringup - http://crbug.com/401304
1158 #define MAYBE_KeyEvents DISABLED_KeyEvents
1159 #else
1160 #define MAYBE_KeyEvents KeyEvents
1161 #endif
1163 VIEW_TEST(BookmarkBarViewTest10, MAYBE_KeyEvents)
1165 // Make sure the menu closes with the following sequence: show menu, show
1166 // context menu, close context menu (via escape), then click else where. This
1167 // effectively verifies we maintain mouse capture after the context menu is
1168 // hidden.
1169 class BookmarkBarViewTest11 : public BookmarkBarViewEventTestBase {
1170 public:
1171 BookmarkBarViewTest11()
1172 : observer_(CreateEventTask(this, &BookmarkBarViewTest11::Step3)) {
1175 protected:
1176 void DoTestOnMessageLoop() override {
1177 // Move the mouse to the first folder on the bookmark bar and press the
1178 // mouse.
1179 views::LabelButton* button = bb_view_->other_bookmarks_button();
1180 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1181 ui_controls::DOWN | ui_controls::UP,
1182 CreateEventTask(this, &BookmarkBarViewTest11::Step2));
1185 private:
1186 void Step2() {
1187 // Menu should be showing.
1188 views::MenuItemView* menu = bb_view_->GetMenu();
1189 ASSERT_TRUE(menu != NULL);
1190 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1192 views::MenuItemView* child_menu =
1193 menu->GetSubmenu()->GetMenuItemAt(0);
1194 ASSERT_TRUE(child_menu != NULL);
1196 // Right click on the first child to get its context menu.
1197 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1198 ui_controls::DOWN | ui_controls::UP, base::Closure());
1199 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1202 void Step3() {
1203 // Send escape so that the context menu hides.
1204 ui_controls::SendKeyPressNotifyWhenDone(
1205 window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
1206 CreateEventTask(this, &BookmarkBarViewTest11::Step4));
1209 void Step4() {
1210 // Make sure the context menu is no longer showing.
1211 views::MenuItemView* menu = bb_view_->GetContextMenu();
1212 ASSERT_TRUE(!menu || !menu->GetSubmenu() ||
1213 !menu->GetSubmenu()->IsShowing());
1215 // But the menu should be showing.
1216 menu = bb_view_->GetMenu();
1217 ASSERT_TRUE(menu && menu->GetSubmenu() && menu->GetSubmenu()->IsShowing());
1219 // Now click on empty space.
1220 gfx::Point mouse_loc;
1221 views::View::ConvertPointToScreen(bb_view_.get(), &mouse_loc);
1222 ui_controls::SendMouseMove(mouse_loc.x(), mouse_loc.y());
1223 ui_controls::SendMouseEventsNotifyWhenDone(
1224 ui_controls::LEFT, ui_controls::UP | ui_controls::DOWN,
1225 CreateEventTask(this, &BookmarkBarViewTest11::Step5));
1228 void Step5() {
1229 // Make sure the menu is not showing.
1230 views::MenuItemView* menu = bb_view_->GetMenu();
1231 ASSERT_TRUE(!menu || !menu->GetSubmenu() ||
1232 !menu->GetSubmenu()->IsShowing());
1233 Done();
1236 BookmarkContextMenuNotificationObserver observer_;
1239 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1240 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1241 #define MAYBE_CloseMenuAfterClosingContextMenu \
1242 DISABLED_CloseMenuAfterClosingContextMenu
1243 #elif defined(USE_OZONE)
1244 // ozone bringup - http://crbug.com/401304
1245 #define MAYBE_CloseMenuAfterClosingContextMenu \
1246 DISABLED_CloseMenuAfterClosingContextMenu
1247 #else
1248 #define MAYBE_CloseMenuAfterClosingContextMenu CloseMenuAfterClosingContextMenu
1249 #endif
1251 VIEW_TEST(BookmarkBarViewTest11, MAYBE_CloseMenuAfterClosingContextMenu)
1253 // Tests showing a modal dialog from a context menu.
1254 class BookmarkBarViewTest12 : public BookmarkBarViewEventTestBase {
1255 protected:
1256 void DoTestOnMessageLoop() override {
1257 // Open up the other folder.
1258 views::LabelButton* button = bb_view_->other_bookmarks_button();
1259 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1260 ui_controls::DOWN | ui_controls::UP,
1261 CreateEventTask(this, &BookmarkBarViewTest12::Step2));
1262 chrome::num_bookmark_urls_before_prompting = 1;
1265 ~BookmarkBarViewTest12() override {
1266 chrome::num_bookmark_urls_before_prompting = 15;
1269 private:
1270 void Step2() {
1271 // Menu should be showing.
1272 views::MenuItemView* menu = bb_view_->GetMenu();
1273 ASSERT_TRUE(menu != NULL);
1274 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1276 views::MenuItemView* child_menu =
1277 menu->GetSubmenu()->GetMenuItemAt(1);
1278 ASSERT_TRUE(child_menu != NULL);
1280 // Right click on the second child (a folder) to get its context menu.
1281 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1282 ui_controls::DOWN | ui_controls::UP,
1283 CreateEventTask(this, &BookmarkBarViewTest12::Step3));
1286 void Step3() {
1287 // Make sure the context menu is showing.
1288 views::MenuItemView* menu = bb_view_->GetContextMenu();
1289 ASSERT_TRUE(menu && menu->GetSubmenu() && menu->GetSubmenu()->IsShowing());
1291 // Select the first item in the context menu (open all).
1292 views::MenuItemView* child_menu =
1293 menu->GetSubmenu()->GetMenuItemAt(0);
1294 ASSERT_TRUE(child_menu != NULL);
1296 // Click and wait until the dialog box appears.
1297 scoped_ptr<DialogWaiter> dialog_waiter(new DialogWaiter());
1298 ui_test_utils::MoveMouseToCenterAndPress(
1299 child_menu,
1300 ui_controls::LEFT,
1301 ui_controls::DOWN | ui_controls::UP,
1302 base::Bind(
1303 &BookmarkBarViewTest12::Step4, this, base::Passed(&dialog_waiter)));
1306 void Step4(scoped_ptr<DialogWaiter> waiter) {
1307 views::Widget* dialog = waiter->WaitForDialog();
1308 waiter.reset();
1310 // Press tab to give focus to the cancel button. Wait until the widget
1311 // receives the tab key.
1312 TabKeyWaiter tab_waiter(dialog);
1313 ui_controls::SendKeyPress(
1314 window_->GetNativeWindow(), ui::VKEY_TAB, false, false, false, false);
1315 tab_waiter.WaitForTab();
1317 // For some reason return isn't processed correctly unless we delay.
1318 base::MessageLoop::current()->PostDelayedTask(
1319 FROM_HERE,
1320 base::Bind(
1321 &BookmarkBarViewTest12::Step5, this, base::Unretained(dialog)),
1322 base::TimeDelta::FromSeconds(1));
1325 void Step5(views::Widget* dialog) {
1326 DialogCloseWaiter waiter(dialog);
1327 // And press enter so that the cancel button is selected.
1328 ui_controls::SendKeyPressNotifyWhenDone(window_->GetNativeWindow(),
1329 ui::VKEY_RETURN,
1330 false,
1331 false,
1332 false,
1333 false,
1334 base::Closure());
1335 waiter.WaitForDialogClose();
1336 Done();
1340 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1341 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1342 #define MAYBE_CloseWithModalDialog DISABLED_CloseWithModalDialog
1343 #else
1344 #define MAYBE_CloseWithModalDialog CloseWithModalDialog
1345 #endif
1347 VIEW_TEST(BookmarkBarViewTest12, MAYBE_CloseWithModalDialog)
1349 // Tests clicking on the separator of a context menu (this is for coverage of
1350 // bug 17862).
1351 class BookmarkBarViewTest13 : public BookmarkBarViewEventTestBase {
1352 public:
1353 BookmarkBarViewTest13()
1354 : observer_(CreateEventTask(this, &BookmarkBarViewTest13::Step3)) {
1357 protected:
1358 void DoTestOnMessageLoop() override {
1359 // Move the mouse to the first folder on the bookmark bar and press the
1360 // mouse.
1361 views::LabelButton* button = bb_view_->other_bookmarks_button();
1362 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1363 ui_controls::DOWN | ui_controls::UP,
1364 CreateEventTask(this, &BookmarkBarViewTest13::Step2));
1367 private:
1368 void Step2() {
1369 // Menu should be showing.
1370 views::MenuItemView* menu = bb_view_->GetMenu();
1371 ASSERT_TRUE(menu != NULL);
1372 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1374 views::MenuItemView* child_menu =
1375 menu->GetSubmenu()->GetMenuItemAt(0);
1376 ASSERT_TRUE(child_menu != NULL);
1378 // Right click on the first child to get its context menu.
1379 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1380 ui_controls::DOWN | ui_controls::UP, base::Closure());
1381 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1384 void Step3() {
1385 // Make sure the context menu is showing.
1386 views::MenuItemView* menu = bb_view_->GetContextMenu();
1387 ASSERT_TRUE(menu != NULL);
1388 ASSERT_TRUE(menu->GetSubmenu());
1389 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1391 // Find the first separator.
1392 views::SubmenuView* submenu = menu->GetSubmenu();
1393 views::View* separator_view = NULL;
1394 for (int i = 0; i < submenu->child_count(); ++i) {
1395 if (submenu->child_at(i)->id() != views::MenuItemView::kMenuItemViewID) {
1396 separator_view = submenu->child_at(i);
1397 break;
1400 ASSERT_TRUE(separator_view);
1402 // Click on the separator. Clicking on the separator shouldn't visually
1403 // change anything.
1404 ui_test_utils::MoveMouseToCenterAndPress(separator_view,
1405 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1406 CreateEventTask(this, &BookmarkBarViewTest13::Step4));
1409 void Step4() {
1410 // The context menu should still be showing.
1411 views::MenuItemView* menu = bb_view_->GetContextMenu();
1412 ASSERT_TRUE(menu != NULL);
1413 ASSERT_TRUE(menu->GetSubmenu());
1414 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1416 // Select the first context menu item.
1417 ui_test_utils::MoveMouseToCenterAndPress(
1418 menu->GetSubmenu()->GetMenuItemAt(0),
1419 ui_controls::LEFT,
1420 ui_controls::DOWN | ui_controls::UP,
1421 CreateEventTask(this, &BookmarkBarViewTest13::Step5));
1424 void Step5() {
1425 Done();
1428 BookmarkContextMenuNotificationObserver observer_;
1431 VIEW_TEST(BookmarkBarViewTest13, ClickOnContextMenuSeparator)
1433 // Makes sure right clicking on a folder on the bookmark bar doesn't result in
1434 // both a context menu and showing the menu.
1435 class BookmarkBarViewTest14 : public BookmarkBarViewEventTestBase {
1436 public:
1437 BookmarkBarViewTest14()
1438 : observer_(CreateEventTask(this, &BookmarkBarViewTest14::Step2)) {
1441 protected:
1442 void DoTestOnMessageLoop() override {
1443 // Move the mouse to the first folder on the bookmark bar and press the
1444 // right mouse button.
1445 views::LabelButton* button = GetBookmarkButton(0);
1446 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::RIGHT,
1447 ui_controls::DOWN | ui_controls::UP, base::Closure());
1448 // Step2 will be invoked by BookmarkContextMenuNotificationObserver.
1451 private:
1453 void Step2() {
1454 // Menu should NOT be showing.
1455 views::MenuItemView* menu = bb_view_->GetMenu();
1456 ASSERT_TRUE(menu == NULL);
1458 // Send escape so that the context menu hides.
1459 ui_controls::SendKeyPressNotifyWhenDone(
1460 window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
1461 CreateEventTask(this, &BookmarkBarViewTest14::Step3));
1464 void Step3() {
1465 Done();
1468 BookmarkContextMenuNotificationObserver observer_;
1471 #if defined(USE_OZONE)
1472 // ozone bringup - http://crbug.com/401304
1473 #define MAYBE_ContextMenus2 DISABLED_ContextMenus2
1474 #else
1475 #define MAYBE_ContextMenus2 ContextMenus2
1476 #endif
1478 VIEW_TEST(BookmarkBarViewTest14, MAYBE_ContextMenus2)
1480 // Makes sure deleting from the context menu keeps the bookmark menu showing.
1481 class BookmarkBarViewTest15 : public BookmarkBarViewEventTestBase {
1482 public:
1483 BookmarkBarViewTest15()
1484 : deleted_menu_id_(0),
1485 observer_(CreateEventTask(this, &BookmarkBarViewTest15::Step3)) {
1488 protected:
1489 void DoTestOnMessageLoop() override {
1490 // Show the other bookmarks.
1491 views::LabelButton* button = bb_view_->other_bookmarks_button();
1492 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1493 ui_controls::DOWN | ui_controls::UP,
1494 CreateEventTask(this, &BookmarkBarViewTest15::Step2));
1497 private:
1498 void Step2() {
1499 // Menu should be showing.
1500 views::MenuItemView* menu = bb_view_->GetMenu();
1501 ASSERT_TRUE(menu != NULL);
1502 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1504 views::MenuItemView* child_menu =
1505 menu->GetSubmenu()->GetMenuItemAt(1);
1506 ASSERT_TRUE(child_menu != NULL);
1508 deleted_menu_id_ = child_menu->GetCommand();
1510 // Right click on the second child to get its context menu.
1511 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1512 ui_controls::DOWN | ui_controls::UP, base::Closure());
1513 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1516 void Step3() {
1517 // Make sure the context menu is showing.
1518 views::MenuItemView* menu = bb_view_->GetContextMenu();
1519 ASSERT_TRUE(menu != NULL);
1520 ASSERT_TRUE(menu->GetSubmenu());
1521 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1523 views::MenuItemView* delete_menu =
1524 menu->GetMenuItemByID(IDC_BOOKMARK_BAR_REMOVE);
1525 ASSERT_TRUE(delete_menu);
1527 // Click on the delete button.
1528 ui_test_utils::MoveMouseToCenterAndPress(delete_menu,
1529 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1530 CreateEventTask(this, &BookmarkBarViewTest15::Step4));
1533 void Step4() {
1534 // The context menu should not be showing.
1535 views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1536 ASSERT_TRUE(context_menu == NULL);
1538 // But the menu should be showing.
1539 views::MenuItemView* menu = bb_view_->GetMenu();
1540 ASSERT_TRUE(menu != NULL);
1541 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1543 // And the deleted_menu_id_ should have been removed.
1544 ASSERT_TRUE(menu->GetMenuItemByID(deleted_menu_id_) == NULL);
1546 bb_view_->GetMenu()->GetMenuController()->CancelAll();
1548 Done();
1551 int deleted_menu_id_;
1552 BookmarkContextMenuNotificationObserver observer_;
1555 VIEW_TEST(BookmarkBarViewTest15, MenuStaysVisibleAfterDelete)
1557 // Tests that we don't crash or get stuck if the parent of a menu is closed.
1558 class BookmarkBarViewTest16 : public BookmarkBarViewEventTestBase {
1559 protected:
1560 void DoTestOnMessageLoop() override {
1561 // Move the mouse to the first folder on the bookmark bar and press the
1562 // mouse.
1563 views::LabelButton* button = GetBookmarkButton(0);
1564 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1565 ui_controls::DOWN | ui_controls::UP,
1566 CreateEventTask(this, &BookmarkBarViewTest16::Step2));
1569 private:
1570 void Step2() {
1571 // Menu should be showing.
1572 views::MenuItemView* menu = bb_view_->GetMenu();
1573 ASSERT_TRUE(menu != NULL);
1574 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1576 // Button should be depressed.
1577 views::LabelButton* button = GetBookmarkButton(0);
1578 ASSERT_TRUE(button->state() == views::CustomButton::STATE_PRESSED);
1580 // Close the window.
1581 window_->Close();
1582 window_ = NULL;
1584 base::MessageLoop::current()->PostTask(
1585 FROM_HERE, CreateEventTask(this, &BookmarkBarViewTest16::Done));
1589 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1590 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1591 #define MAYBE_DeleteMenu DISABLED_DeleteMenu
1592 #else
1593 #define MAYBE_DeleteMenu DeleteMenu
1594 #endif
1596 VIEW_TEST(BookmarkBarViewTest16, MAYBE_DeleteMenu)
1598 // Makes sure right clicking on an item while a context menu is already showing
1599 // doesn't crash and works.
1600 class BookmarkBarViewTest17 : public BookmarkBarViewEventTestBase {
1601 public:
1602 BookmarkBarViewTest17()
1603 : observer_(CreateEventTask(this, &BookmarkBarViewTest17::Step3)) {
1606 protected:
1607 void DoTestOnMessageLoop() override {
1608 // Move the mouse to the other folder on the bookmark bar and press the
1609 // left mouse button.
1610 views::LabelButton* button = bb_view_->other_bookmarks_button();
1611 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1612 ui_controls::DOWN | ui_controls::UP,
1613 CreateEventTask(this, &BookmarkBarViewTest17::Step2));
1616 private:
1617 void Step2() {
1618 // Menu should be showing.
1619 views::MenuItemView* menu = bb_view_->GetMenu();
1620 ASSERT_TRUE(menu != NULL);
1621 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1623 // Right click on the second item to show its context menu.
1624 views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
1625 ASSERT_TRUE(child_menu != NULL);
1626 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1627 ui_controls::DOWN | ui_controls::UP, base::Closure());
1628 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1631 void Step3() {
1632 // Make sure the context menu is showing.
1633 views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1634 ASSERT_TRUE(context_menu != NULL);
1635 ASSERT_TRUE(context_menu->GetSubmenu());
1636 ASSERT_TRUE(context_menu->GetSubmenu()->IsShowing());
1638 // Right click on the first menu item to trigger its context menu.
1639 views::MenuItemView* menu = bb_view_->GetMenu();
1640 ASSERT_TRUE(menu != NULL);
1641 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1642 views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1643 ASSERT_TRUE(child_menu != NULL);
1645 // The context menu and child_menu can be overlapped, calculate the
1646 // non-intersected Rect of the child menu and click on its center to make
1647 // sure the click is always on the child menu.
1648 gfx::Rect context_rect = context_menu->GetSubmenu()->GetBoundsInScreen();
1649 gfx::Rect child_menu_rect = child_menu->GetBoundsInScreen();
1650 gfx::Rect clickable_rect =
1651 gfx::SubtractRects(child_menu_rect, context_rect);
1652 ASSERT_FALSE(clickable_rect.IsEmpty());
1653 observer_.set_task(CreateEventTask(this, &BookmarkBarViewTest17::Step4));
1654 MoveMouseAndPress(clickable_rect.CenterPoint(), ui_controls::RIGHT,
1655 ui_controls::DOWN | ui_controls::UP, base::Closure());
1656 // Step4 will be invoked by BookmarkContextMenuNotificationObserver.
1659 void Step4() {
1660 // The context menu should still be showing.
1661 views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1662 ASSERT_TRUE(context_menu != NULL);
1664 // And the menu should be showing.
1665 views::MenuItemView* menu = bb_view_->GetMenu();
1666 ASSERT_TRUE(menu != NULL);
1667 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1669 bb_view_->GetMenu()->GetMenuController()->CancelAll();
1671 Done();
1674 BookmarkContextMenuNotificationObserver observer_;
1677 #if defined(OS_WIN)
1678 // Flaky on Win7. crbug/453796
1679 #define MAYBE_ContextMenus3 DISABLED_ContextMenus3
1680 #else
1681 #define MAYBE_ContextMenus3 ContextMenus3
1682 #endif
1684 VIEW_TEST(BookmarkBarViewTest17, MAYBE_ContextMenus3)
1686 // Verifies sibling menus works. Clicks on the 'other bookmarks' folder, then
1687 // moves the mouse over the first item on the bookmark bar and makes sure the
1688 // menu appears.
1689 class BookmarkBarViewTest18 : public BookmarkBarViewEventTestBase {
1690 protected:
1691 void DoTestOnMessageLoop() override {
1692 // Move the mouse to the other folder on the bookmark bar and press the
1693 // left mouse button.
1694 views::LabelButton* button = bb_view_->other_bookmarks_button();
1695 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1696 ui_controls::DOWN | ui_controls::UP,
1697 CreateEventTask(this, &BookmarkBarViewTest18::Step2));
1700 private:
1701 void Step2() {
1702 // Menu should be showing.
1703 views::MenuItemView* menu = bb_view_->GetMenu();
1704 ASSERT_TRUE(menu != NULL);
1705 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1707 // Move the mouse to the first folder on the bookmark bar
1708 views::LabelButton* button = GetBookmarkButton(0);
1709 gfx::Point button_center(button->width() / 2, button->height() / 2);
1710 views::View::ConvertPointToScreen(button, &button_center);
1711 ui_controls::SendMouseMoveNotifyWhenDone(
1712 button_center.x(), button_center.y(),
1713 CreateEventTask(this, &BookmarkBarViewTest18::Step3));
1716 void Step3() {
1717 // Make sure the menu is showing.
1718 views::MenuItemView* menu = bb_view_->GetMenu();
1719 ASSERT_TRUE(menu != NULL);
1720 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1722 // The menu for the first folder should be in the pressed state (since the
1723 // menu is showing for it).
1724 EXPECT_EQ(views::CustomButton::STATE_PRESSED,
1725 GetBookmarkButton(0)->state());
1727 menu->GetMenuController()->CancelAll();
1729 Done();
1733 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1734 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1735 #define MAYBE_BookmarkBarViewTest18_SiblingMenu \
1736 DISABLED_BookmarkBarViewTest18_SiblingMenu
1737 #else
1738 #define MAYBE_BookmarkBarViewTest18_SiblingMenu \
1739 BookmarkBarViewTest18_SiblingMenu
1740 #endif
1742 VIEW_TEST(BookmarkBarViewTest18, MAYBE_BookmarkBarViewTest18_SiblingMenu)
1744 // Verifies mousing over an already open sibling menu doesn't prematurely cancel
1745 // the menu.
1746 class BookmarkBarViewTest19 : public BookmarkBarViewEventTestBase {
1747 protected:
1748 void DoTestOnMessageLoop() override {
1749 // Move the mouse to the other folder on the bookmark bar and press the
1750 // left mouse button.
1751 views::LabelButton* button = bb_view_->other_bookmarks_button();
1752 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1753 ui_controls::DOWN | ui_controls::UP,
1754 CreateEventTask(this, &BookmarkBarViewTest19::Step2));
1757 private:
1758 void Step2() {
1759 // Menu should be showing.
1760 views::MenuItemView* menu = bb_view_->GetMenu();
1761 ASSERT_TRUE(menu != NULL);
1762 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1764 // Click on the first folder.
1765 views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1766 ASSERT_TRUE(child_menu != NULL);
1767 ui_test_utils::MoveMouseToCenterAndPress(
1768 child_menu, ui_controls::LEFT,
1769 ui_controls::DOWN | ui_controls::UP,
1770 CreateEventTask(this, &BookmarkBarViewTest19::Step3));
1773 void Step3() {
1774 // Make sure the menu is showing.
1775 views::MenuItemView* menu = bb_view_->GetMenu();
1776 ASSERT_TRUE(menu != NULL);
1777 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1779 // Move the mouse back to the "Other Bookmarks" button.
1780 views::LabelButton* button = bb_view_->other_bookmarks_button();
1781 gfx::Point button_center(button->width() / 2, button->height() / 2);
1782 views::View::ConvertPointToScreen(button, &button_center);
1783 ui_controls::SendMouseMoveNotifyWhenDone(
1784 button_center.x() + 1, button_center.y() + 1,
1785 CreateEventTask(this, &BookmarkBarViewTest19::Step4));
1788 void Step4() {
1789 // Menu should be showing.
1790 views::MenuItemView* menu = bb_view_->GetMenu();
1791 ASSERT_TRUE(menu != NULL);
1792 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1794 // Click on the first folder.
1795 views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1796 ASSERT_TRUE(child_menu != NULL);
1797 ui_test_utils::MoveMouseToCenterAndPress(
1798 child_menu,
1799 ui_controls::LEFT,
1800 ui_controls::DOWN | ui_controls::UP,
1801 CreateEventTask(this, &BookmarkBarViewTest19::Step5));
1804 void Step5() {
1805 // Make sure the menu is showing.
1806 views::MenuItemView* menu = bb_view_->GetMenu();
1807 ASSERT_TRUE(menu != NULL);
1808 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1810 menu->GetMenuController()->CancelAll();
1812 Done();
1816 VIEW_TEST(BookmarkBarViewTest19, BookmarkBarViewTest19_SiblingMenu)
1818 // Verify that when clicking a mouse button outside a context menu,
1819 // the context menu is dismissed *and* the underlying view receives
1820 // the the mouse event (due to event reposting).
1821 class BookmarkBarViewTest20 : public BookmarkBarViewEventTestBase {
1822 public:
1823 BookmarkBarViewTest20() : test_view_(new TestViewForMenuExit) {}
1825 protected:
1826 void DoTestOnMessageLoop() override {
1827 // Add |test_view_| next to |bb_view_|.
1828 views::View* parent = bb_view_->parent();
1829 views::View* container_view = new ContainerViewForMenuExit;
1830 container_view->AddChildView(bb_view_.get());
1831 container_view->AddChildView(test_view_);
1832 parent->AddChildView(container_view);
1833 parent->Layout();
1835 ASSERT_EQ(test_view_->press_count(), 0);
1837 // Move the mouse to the Test View and press the left mouse button.
1838 ui_test_utils::MoveMouseToCenterAndPress(
1839 test_view_, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1840 CreateEventTask(this, &BookmarkBarViewTest20::Step1));
1843 private:
1844 void Step1() {
1845 ASSERT_EQ(test_view_->press_count(), 1);
1846 ASSERT_TRUE(bb_view_->GetMenu() == NULL);
1848 // Move the mouse to the first folder on the bookmark bar and press the
1849 // left mouse button.
1850 views::LabelButton* button = GetBookmarkButton(0);
1851 ui_test_utils::MoveMouseToCenterAndPress(
1852 button, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1853 CreateEventTask(this, &BookmarkBarViewTest20::Step2));
1856 void Step2() {
1857 ASSERT_EQ(test_view_->press_count(), 1);
1858 views::MenuItemView* menu = bb_view_->GetMenu();
1859 ASSERT_TRUE(menu != NULL);
1860 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1862 // Move the mouse to the Test View and press the left mouse button.
1863 // The context menu will consume the event and exit. Thereafter,
1864 // the event is reposted and delivered to the Test View which
1865 // increases its press-count.
1866 ui_test_utils::MoveMouseToCenterAndPress(
1867 test_view_, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1868 CreateEventTask(this, &BookmarkBarViewTest20::Step3));
1871 void Step3() {
1872 ASSERT_EQ(test_view_->press_count(), 2);
1873 ASSERT_TRUE(bb_view_->GetMenu() == NULL);
1874 Done();
1877 class ContainerViewForMenuExit : public views::View {
1878 public:
1879 ContainerViewForMenuExit() {
1882 void Layout() override {
1883 DCHECK_EQ(2, child_count());
1884 views::View* bb_view = child_at(0);
1885 views::View* test_view = child_at(1);
1886 const int width = bb_view->width();
1887 const int height = bb_view->height();
1888 bb_view->SetBounds(0,0, width - 22, height);
1889 test_view->SetBounds(width - 20, 0, 20, height);
1892 private:
1894 DISALLOW_COPY_AND_ASSIGN(ContainerViewForMenuExit);
1897 class TestViewForMenuExit : public views::View {
1898 public:
1899 TestViewForMenuExit() : press_count_(0) {
1901 bool OnMousePressed(const ui::MouseEvent& event) override {
1902 ++press_count_;
1903 return true;
1905 int press_count() const { return press_count_; }
1907 private:
1908 int press_count_;
1910 DISALLOW_COPY_AND_ASSIGN(TestViewForMenuExit);
1913 TestViewForMenuExit* test_view_;
1916 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1917 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1918 #define MAYBE_ContextMenuExitTest DISABLED_ContextMenuExitTest
1919 #else
1920 #define MAYBE_ContextMenuExitTest ContextMenuExitTest
1921 #endif
1923 VIEW_TEST(BookmarkBarViewTest20, MAYBE_ContextMenuExitTest)
1925 // Tests context menu by way of opening a context menu for a empty folder menu.
1926 // The opened context menu should behave as it is from the folder button.
1927 class BookmarkBarViewTest21 : public BookmarkBarViewEventTestBase {
1928 public:
1929 BookmarkBarViewTest21()
1930 : observer_(CreateEventTask(this, &BookmarkBarViewTest21::Step3)) {
1933 protected:
1934 // Move the mouse to the empty folder on the bookmark bar and press the
1935 // left mouse button.
1936 void DoTestOnMessageLoop() override {
1937 views::LabelButton* button = GetBookmarkButton(5);
1938 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1939 ui_controls::DOWN | ui_controls::UP,
1940 CreateEventTask(this, &BookmarkBarViewTest21::Step2));
1943 private:
1944 // Confirm that a menu for empty folder shows and right click the menu.
1945 void Step2() {
1946 // Menu should be showing.
1947 views::MenuItemView* menu = bb_view_->GetMenu();
1948 ASSERT_TRUE(menu != NULL);
1950 views::SubmenuView* submenu = menu->GetSubmenu();
1951 ASSERT_TRUE(submenu->IsShowing());
1952 ASSERT_EQ(1, submenu->child_count());
1954 views::View* view = submenu->child_at(0);
1955 ASSERT_TRUE(view != NULL);
1956 EXPECT_EQ(views::MenuItemView::kEmptyMenuItemViewID, view->id());
1958 // Right click on the first child to get its context menu.
1959 ui_test_utils::MoveMouseToCenterAndPress(view, ui_controls::RIGHT,
1960 ui_controls::DOWN | ui_controls::UP, base::Closure());
1961 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1964 // Confirm that context menu shows and click REMOVE menu.
1965 void Step3() {
1966 // Make sure the context menu is showing.
1967 views::MenuItemView* menu = bb_view_->GetContextMenu();
1968 ASSERT_TRUE(menu != NULL);
1969 ASSERT_TRUE(menu->GetSubmenu());
1970 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1972 views::MenuItemView* delete_menu =
1973 menu->GetMenuItemByID(IDC_BOOKMARK_BAR_REMOVE);
1974 ASSERT_TRUE(delete_menu);
1976 // Click on the delete menu item.
1977 ui_test_utils::MoveMouseToCenterAndPress(delete_menu,
1978 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1979 CreateEventTask(this, &BookmarkBarViewTest21::Step4));
1982 // Confirm that the empty folder gets removed and menu doesn't show.
1983 void Step4() {
1984 views::LabelButton* button = GetBookmarkButton(5);
1985 ASSERT_TRUE(button);
1986 EXPECT_EQ(ASCIIToUTF16("d"), button->GetText());
1987 EXPECT_TRUE(bb_view_->GetContextMenu() == NULL);
1988 EXPECT_TRUE(bb_view_->GetMenu() == NULL);
1990 Done();
1993 BookmarkContextMenuNotificationObserver observer_;
1996 VIEW_TEST(BookmarkBarViewTest21, ContextMenusForEmptyFolder)
1998 // Test that closing the source browser window while dragging a bookmark does
1999 // not cause a crash.
2000 class BookmarkBarViewTest22 : public BookmarkBarViewEventTestBase {
2001 protected:
2002 void DoTestOnMessageLoop() override {
2003 // Move the mouse to the first folder on the bookmark bar and press the
2004 // mouse.
2005 views::LabelButton* button = GetBookmarkButton(0);
2006 ui_test_utils::MoveMouseToCenterAndPress(
2007 button, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
2008 CreateEventTask(this, &BookmarkBarViewTest22::Step2));
2011 private:
2012 void Step2() {
2013 // Menu should be showing.
2014 views::MenuItemView* menu = bb_view_->GetMenu();
2015 ASSERT_TRUE(menu);
2016 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2018 views::MenuItemView* child_menu =
2019 menu->GetSubmenu()->GetMenuItemAt(0);
2020 ASSERT_TRUE(child_menu != NULL);
2022 // Move mouse to center of menu and press button.
2023 ui_test_utils::MoveMouseToCenterAndPress(
2024 child_menu, ui_controls::LEFT, ui_controls::DOWN,
2025 CreateEventTask(this, &BookmarkBarViewTest22::Step3));
2028 void Step3() {
2029 views::MenuItemView* target_menu =
2030 bb_view_->GetMenu()->GetSubmenu()->GetMenuItemAt(1);
2031 gfx::Point loc(1, target_menu->height() - 1);
2032 views::View::ConvertPointToScreen(target_menu, &loc);
2034 // Start a drag.
2035 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
2036 CreateEventTask(this, &BookmarkBarViewTest22::Step4));
2037 ScheduleMouseMoveInBackground(loc.x(), loc.y());
2040 void Step4() {
2041 window_->Close();
2042 window_ = NULL;
2044 #if defined(OS_CHROMEOS)
2045 ui_controls::SendMouseEventsNotifyWhenDone(
2046 ui_controls::LEFT, ui_controls::UP,
2047 CreateEventTask(this, &BookmarkBarViewTest22::Done));
2048 #else
2049 // There are no widgets to send the mouse release to.
2050 Done();
2051 #endif
2055 #if defined(OS_WIN)
2056 // This test times out on Windows. TODO(pkotwicz): Find out why.
2057 #define MAYBE_CloseSourceBrowserDuringDrag DISABLED_CloseSourceBrowserDuringDrag
2058 #else
2059 #define MAYBE_CloseSourceBrowserDuringDrag CloseSourceBrowserDuringDrag
2060 #endif
2062 VIEW_TEST(BookmarkBarViewTest22, MAYBE_CloseSourceBrowserDuringDrag)