Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_bar_view_test.cc
blobc659c617ba02fd198b26921eed59a8d38c9d023e
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/location.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/chrome_content_browser_client.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_tabstrip.h"
22 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
25 #include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
26 #include "chrome/common/chrome_content_client.h"
27 #include "chrome/common/pref_names.h"
28 #include "chrome/test/base/interactive_test_utils.h"
29 #include "chrome/test/base/scoped_testing_local_state.h"
30 #include "chrome/test/base/test_browser_window.h"
31 #include "chrome/test/base/testing_browser_process.h"
32 #include "chrome/test/base/testing_profile.h"
33 #include "chrome/test/base/ui_test_utils.h"
34 #include "chrome/test/base/view_event_test_base.h"
35 #include "components/bookmarks/browser/bookmark_model.h"
36 #include "components/bookmarks/test/bookmark_test_helpers.h"
37 #include "components/constrained_window/constrained_window_views.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/page_navigator.h"
40 #include "content/public/test/test_browser_thread.h"
41 #include "ui/aura/env.h"
42 #include "ui/aura/env_observer.h"
43 #include "ui/aura/window.h"
44 #include "ui/base/clipboard/clipboard.h"
45 #include "ui/base/test/ui_controls.h"
46 #include "ui/events/keycodes/keyboard_codes.h"
47 #include "ui/views/controls/button/menu_button.h"
48 #include "ui/views/controls/menu/menu_controller.h"
49 #include "ui/views/controls/menu/menu_item_view.h"
50 #include "ui/views/controls/menu/submenu_view.h"
51 #include "ui/views/widget/widget.h"
53 using base::ASCIIToUTF16;
54 using bookmarks::BookmarkModel;
55 using bookmarks::BookmarkNode;
56 using content::BrowserThread;
57 using content::OpenURLParams;
58 using content::PageNavigator;
59 using content::WebContents;
61 namespace {
63 // Waits for a views::Widget dialog to show up.
64 class DialogWaiter : public aura::EnvObserver,
65 public views::WidgetObserver {
66 public:
67 DialogWaiter()
68 : dialog_created_(false),
69 dialog_(NULL) {
70 aura::Env::GetInstance()->AddObserver(this);
73 ~DialogWaiter() override { aura::Env::GetInstance()->RemoveObserver(this); }
75 views::Widget* WaitForDialog() {
76 if (dialog_created_)
77 return dialog_;
78 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
79 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
80 base::RunLoop run_loop;
81 quit_closure_ = run_loop.QuitClosure();
82 run_loop.Run();
83 return dialog_;
86 private:
87 // aura::EnvObserver:
88 void OnWindowInitialized(aura::Window* window) override {
89 if (dialog_)
90 return;
91 views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
92 if (!widget || !widget->IsDialogBox())
93 return;
94 dialog_ = widget;
95 dialog_->AddObserver(this);
98 // views::WidgetObserver:
99 void OnWidgetVisibilityChanged(views::Widget* widget, bool visible) override {
100 CHECK_EQ(dialog_, widget);
101 if (visible) {
102 dialog_created_ = true;
103 dialog_->RemoveObserver(this);
104 if (!quit_closure_.is_null())
105 quit_closure_.Run();
109 bool dialog_created_;
110 views::Widget* dialog_;
111 base::Closure quit_closure_;
113 DISALLOW_COPY_AND_ASSIGN(DialogWaiter);
116 // Waits for a dialog to terminate.
117 class DialogCloseWaiter : public views::WidgetObserver {
118 public:
119 explicit DialogCloseWaiter(views::Widget* dialog)
120 : dialog_closed_(false) {
121 dialog->AddObserver(this);
124 ~DialogCloseWaiter() override {
125 // It is not necessary to remove |this| from the dialog's observer, since
126 // the dialog is destroyed before this waiter.
129 void WaitForDialogClose() {
130 if (dialog_closed_)
131 return;
132 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
133 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
134 base::RunLoop run_loop;
135 quit_closure_ = run_loop.QuitClosure();
136 run_loop.Run();
139 private:
140 // views::WidgetObserver:
141 void OnWidgetDestroyed(views::Widget* widget) override {
142 dialog_closed_ = true;
143 if (!quit_closure_.is_null())
144 quit_closure_.Run();
147 bool dialog_closed_;
148 base::Closure quit_closure_;
150 DISALLOW_COPY_AND_ASSIGN(DialogCloseWaiter);
153 // Waits for a views::Widget to receive a Tab key.
154 class TabKeyWaiter : public ui::EventHandler {
155 public:
156 explicit TabKeyWaiter(views::Widget* widget)
157 : widget_(widget),
158 received_tab_(false) {
159 widget_->GetNativeView()->AddPreTargetHandler(this);
162 ~TabKeyWaiter() override {
163 widget_->GetNativeView()->RemovePreTargetHandler(this);
166 void WaitForTab() {
167 if (received_tab_)
168 return;
169 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
170 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
171 base::RunLoop run_loop;
172 quit_closure_ = run_loop.QuitClosure();
173 run_loop.Run();
176 private:
177 // ui::EventHandler:
178 void OnKeyEvent(ui::KeyEvent* event) override {
179 if (event->type() == ui::ET_KEY_RELEASED &&
180 event->key_code() == ui::VKEY_TAB) {
181 received_tab_ = true;
182 if (!quit_closure_.is_null())
183 quit_closure_.Run();
187 views::Widget* widget_;
188 bool received_tab_;
189 base::Closure quit_closure_;
191 DISALLOW_COPY_AND_ASSIGN(TabKeyWaiter);
194 void MoveMouseAndPress(const gfx::Point& screen_pos,
195 ui_controls::MouseButton button,
196 int state,
197 const base::Closure& closure) {
198 ui_controls::SendMouseMove(screen_pos.x(), screen_pos.y());
199 ui_controls::SendMouseEventsNotifyWhenDone(button, state, closure);
202 // PageNavigator implementation that records the URL.
203 class TestingPageNavigator : public PageNavigator {
204 public:
205 WebContents* OpenURL(const OpenURLParams& params) override {
206 url_ = params.url;
207 return NULL;
210 GURL url_;
213 // TODO(erg): Fix bookmark DND tests on linux_aura. crbug.com/163931
214 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
215 #define MAYBE(x) DISABLED_##x
216 #else
217 #define MAYBE(x) x
218 #endif
220 } // namespace
222 // Base class for event generating bookmark view tests. These test are intended
223 // to exercise View's menus, but that's easier done with BookmarkBarView rather
224 // than View's menu itself.
226 // SetUp creates a bookmark model with the following structure.
227 // All folders are in upper case, all URLs in lower case.
228 // F1
229 // f1a
230 // F11
231 // f11a
232 // *
233 // a
234 // b
235 // c
236 // d
237 // F2
238 // e
239 // OTHER
240 // oa
241 // OF
242 // ofa
243 // ofb
244 // OF2
245 // of2a
246 // of2b
248 // * if CreateBigMenu returns return true, 100 menu items are created here with
249 // the names f1-f100.
251 // Subclasses should be sure and invoke super's implementation of SetUp and
252 // TearDown.
253 class BookmarkBarViewEventTestBase : public ViewEventTestBase {
254 public:
255 BookmarkBarViewEventTestBase()
256 : ViewEventTestBase(),
257 model_(NULL) {}
259 void SetUp() override {
260 content_client_.reset(new ChromeContentClient);
261 content::SetContentClient(content_client_.get());
262 browser_content_client_.reset(new ChromeContentBrowserClient());
263 content::SetBrowserClientForTesting(browser_content_client_.get());
265 views::MenuController::TurnOffMenuSelectionHoldForTest();
266 BookmarkBarView::DisableAnimationsForTesting(true);
267 SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient());
269 profile_.reset(new TestingProfile());
270 profile_->CreateBookmarkModel(true);
271 model_ = BookmarkModelFactory::GetForProfile(profile_.get());
272 bookmarks::test::WaitForBookmarkModelToLoad(model_);
273 profile_->GetPrefs()->SetBoolean(bookmarks::prefs::kShowBookmarkBar, true);
275 Browser::CreateParams native_params(profile_.get(),
276 chrome::GetActiveDesktop());
277 browser_ = chrome::CreateBrowserWithTestWindowForParams(&native_params);
279 local_state_.reset(new ScopedTestingLocalState(
280 TestingBrowserProcess::GetGlobal()));
281 model_->ClearStore();
283 bb_view_.reset(new BookmarkBarView(browser_.get(), NULL));
284 bb_view_->set_owned_by_client();
285 bb_view_->SetPageNavigator(&navigator_);
287 AddTestData(CreateBigMenu());
289 // Create the Widget. Note the initial size is given by GetPreferredSize()
290 // during initialization. This occurs after the WidgetDelegate provides
291 // |bb_view_| as the contents view and adds it to the hierarchy.
292 ViewEventTestBase::SetUp();
294 // Verify the layout triggered by the initial size preserves the overflow
295 // state calculated in GetPreferredSize().
296 EXPECT_TRUE(GetBookmarkButton(5)->visible());
297 EXPECT_FALSE(GetBookmarkButton(6)->visible());
300 void TearDown() override {
301 // Destroy everything, then run the message loop to ensure we delete all
302 // Tasks and fully shut down.
303 browser_->tab_strip_model()->CloseAllTabs();
304 bb_view_.reset();
305 browser_.reset();
306 profile_.reset();
308 // Run the message loop to ensure we delete allTasks and fully shut down.
309 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
310 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
311 base::RunLoop run_loop;
312 loop->task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
313 run_loop.Run();
315 ViewEventTestBase::TearDown();
316 BookmarkBarView::DisableAnimationsForTesting(false);
317 constrained_window::SetConstrainedWindowViewsClient(nullptr);
319 browser_content_client_.reset();
320 content_client_.reset();
321 content::SetContentClient(NULL);
324 protected:
325 views::View* CreateContentsView() override { return bb_view_.get(); }
327 gfx::Size GetPreferredSize() const override {
328 // Calculate the preferred size so that one button doesn't fit, which
329 // triggers the overflow button to appear. We have to do this incrementally
330 // as there isn't a good way to determine the point at which the overflow
331 // button is shown.
333 // This code looks a bit hacky, but it is written so that it shouldn't
334 // depend on any of the layout code in BookmarkBarView, or extra buttons
335 // added to the right of the bookmarks. Instead, brute force search for a
336 // size that triggers the overflow button.
337 gfx::Size size = bb_view_->GetPreferredSize();
338 size.set_width(1000);
339 do {
340 size.set_width(size.width() - 25);
341 bb_view_->SetBounds(0, 0, size.width(), size.height());
342 bb_view_->Layout();
343 } while (bb_view_->GetBookmarkButton(6)->visible());
344 return size;
347 views::LabelButton* GetBookmarkButton(int view_index) {
348 return bb_view_->GetBookmarkButton(view_index);
351 // See comment above class description for what this does.
352 virtual bool CreateBigMenu() { return false; }
354 BookmarkModel* model_;
355 scoped_ptr<BookmarkBarView> bb_view_;
356 TestingPageNavigator navigator_;
358 private:
359 void AddTestData(bool big_menu) {
360 const BookmarkNode* bb_node = model_->bookmark_bar_node();
361 std::string test_base = "file:///c:/tmp/";
362 const BookmarkNode* f1 = model_->AddFolder(bb_node, 0, ASCIIToUTF16("F1"));
363 model_->AddURL(f1, 0, ASCIIToUTF16("f1a"), GURL(test_base + "f1a"));
364 const BookmarkNode* f11 = model_->AddFolder(f1, 1, ASCIIToUTF16("F11"));
365 model_->AddURL(f11, 0, ASCIIToUTF16("f11a"), GURL(test_base + "f11a"));
366 if (big_menu) {
367 for (int i = 1; i <= 100; ++i) {
368 model_->AddURL(f1, i + 1, ASCIIToUTF16("f") + base::IntToString16(i),
369 GURL(test_base + "f" + base::IntToString(i)));
372 model_->AddURL(bb_node, 1, ASCIIToUTF16("a"), GURL(test_base + "a"));
373 model_->AddURL(bb_node, 2, ASCIIToUTF16("b"), GURL(test_base + "b"));
374 model_->AddURL(bb_node, 3, ASCIIToUTF16("c"), GURL(test_base + "c"));
375 model_->AddURL(bb_node, 4, ASCIIToUTF16("d"), GURL(test_base + "d"));
376 model_->AddFolder(bb_node, 5, ASCIIToUTF16("F2"));
377 model_->AddURL(bb_node, 6, ASCIIToUTF16("d"), GURL(test_base + "d"));
379 model_->AddURL(model_->other_node(), 0, ASCIIToUTF16("oa"),
380 GURL(test_base + "oa"));
381 const BookmarkNode* of = model_->AddFolder(model_->other_node(), 1,
382 ASCIIToUTF16("OF"));
383 model_->AddURL(of, 0, ASCIIToUTF16("ofa"), GURL(test_base + "ofa"));
384 model_->AddURL(of, 1, ASCIIToUTF16("ofb"), GURL(test_base + "ofb"));
385 const BookmarkNode* of2 = model_->AddFolder(model_->other_node(), 2,
386 ASCIIToUTF16("OF2"));
387 model_->AddURL(of2, 0, ASCIIToUTF16("of2a"), GURL(test_base + "of2a"));
388 model_->AddURL(of2, 1, ASCIIToUTF16("of2b"), GURL(test_base + "of2b"));
391 scoped_ptr<ChromeContentClient> content_client_;
392 scoped_ptr<ChromeContentBrowserClient> browser_content_client_;
393 scoped_ptr<TestingProfile> profile_;
394 scoped_ptr<Browser> browser_;
395 scoped_ptr<ScopedTestingLocalState> local_state_;
398 // Clicks on first menu, makes sure button is depressed. Moves mouse to first
399 // child, clicks it and makes sure a navigation occurs.
400 class BookmarkBarViewTest1 : public BookmarkBarViewEventTestBase {
401 protected:
402 void DoTestOnMessageLoop() override {
403 // Move the mouse to the first folder on the bookmark bar and press the
404 // mouse.
405 views::LabelButton* button = GetBookmarkButton(0);
406 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
407 ui_controls::DOWN | ui_controls::UP,
408 CreateEventTask(this, &BookmarkBarViewTest1::Step2));
411 private:
412 void Step2() {
413 // Menu should be showing.
414 views::MenuItemView* menu = bb_view_->GetMenu();
415 ASSERT_TRUE(menu != NULL);
416 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
418 // Button should be depressed.
419 views::LabelButton* button = GetBookmarkButton(0);
420 ASSERT_TRUE(button->state() == views::CustomButton::STATE_PRESSED);
422 // Click on the 2nd menu item (A URL).
423 ASSERT_TRUE(menu->GetSubmenu());
425 views::MenuItemView* menu_to_select =
426 menu->GetSubmenu()->GetMenuItemAt(0);
427 ui_test_utils::MoveMouseToCenterAndPress(menu_to_select, ui_controls::LEFT,
428 ui_controls::DOWN | ui_controls::UP,
429 CreateEventTask(this, &BookmarkBarViewTest1::Step3));
432 void Step3() {
433 // We should have navigated to URL f1a.
434 ASSERT_TRUE(navigator_.url_ ==
435 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url());
437 // Make sure button is no longer pushed.
438 views::LabelButton* button = GetBookmarkButton(0);
439 ASSERT_TRUE(button->state() == views::CustomButton::STATE_NORMAL);
441 views::MenuItemView* menu = bb_view_->GetMenu();
442 ASSERT_TRUE(menu == NULL || !menu->GetSubmenu()->IsShowing());
444 Done();
448 VIEW_TEST(BookmarkBarViewTest1, Basic)
450 // Brings up menu, clicks on empty space and make sure menu hides.
451 class BookmarkBarViewTest2 : public BookmarkBarViewEventTestBase {
452 protected:
453 void DoTestOnMessageLoop() override {
454 // Move the mouse to the first folder on the bookmark bar and press the
455 // mouse.
456 views::LabelButton* button = GetBookmarkButton(0);
457 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
458 ui_controls::DOWN | ui_controls::UP,
459 CreateEventTask(this, &BookmarkBarViewTest2::Step2));
462 private:
463 void Step2() {
464 // Menu should be showing.
465 views::MenuItemView* menu = bb_view_->GetMenu();
466 ASSERT_TRUE(menu != NULL && menu->GetSubmenu()->IsShowing());
468 // Click on 0x0, which should trigger closing menu.
469 // NOTE: this code assume there is a left margin, which is currently
470 // true. If that changes, this code will need to find another empty space
471 // to press the mouse on.
472 gfx::Point mouse_loc;
473 views::View::ConvertPointToScreen(bb_view_.get(), &mouse_loc);
474 ui_controls::SendMouseMoveNotifyWhenDone(0, 0,
475 CreateEventTask(this, &BookmarkBarViewTest2::Step3));
478 void Step3() {
479 // As the click is on the desktop the hook never sees the up, so we only
480 // wait on the down. We still send the up though else the system thinks
481 // the mouse is still down.
482 ui_controls::SendMouseEventsNotifyWhenDone(
483 ui_controls::LEFT, ui_controls::DOWN,
484 CreateEventTask(this, &BookmarkBarViewTest2::Step4));
485 ui_controls::SendMouseEvents(ui_controls::LEFT, ui_controls::UP);
488 void Step4() {
489 // The menu shouldn't be showing.
490 views::MenuItemView* menu = bb_view_->GetMenu();
491 ASSERT_TRUE(menu == NULL || !menu->GetSubmenu()->IsShowing());
493 // Make sure button is no longer pushed.
494 views::LabelButton* button = GetBookmarkButton(0);
495 ASSERT_TRUE(button->state() == views::CustomButton::STATE_NORMAL);
497 Done();
501 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
502 // TODO(erg): linux_aura bringup: http://crbug.com/163931
503 #define MAYBE_HideOnDesktopClick DISABLED_HideOnDesktopClick
504 #else
505 #define MAYBE_HideOnDesktopClick HideOnDesktopClick
506 #endif
508 VIEW_TEST(BookmarkBarViewTest2, MAYBE_HideOnDesktopClick)
510 // Brings up menu. Moves over child to make sure submenu appears, moves over
511 // another child and make sure next menu appears.
512 class BookmarkBarViewTest3 : public BookmarkBarViewEventTestBase {
513 protected:
514 void DoTestOnMessageLoop() override {
515 // Move the mouse to the first folder on the bookmark bar and press the
516 // mouse.
517 views::MenuButton* button = bb_view_->other_bookmarks_button();
518 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
519 ui_controls::DOWN | ui_controls::UP,
520 CreateEventTask(this, &BookmarkBarViewTest3::Step2));
523 private:
524 void Step2() {
525 // Menu should be showing.
526 views::MenuItemView* menu = bb_view_->GetMenu();
527 ASSERT_TRUE(menu != NULL);
528 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
530 views::MenuItemView* child_menu =
531 menu->GetSubmenu()->GetMenuItemAt(1);
532 ASSERT_TRUE(child_menu != NULL);
534 // Click on second child, which has a submenu.
535 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
536 ui_controls::DOWN | ui_controls::UP,
537 CreateEventTask(this, &BookmarkBarViewTest3::Step3));
540 void Step3() {
541 // Make sure sub menu is showing.
542 views::MenuItemView* menu = bb_view_->GetMenu();
543 ASSERT_TRUE(menu);
544 views::MenuItemView* child_menu =
545 menu->GetSubmenu()->GetMenuItemAt(1);
546 ASSERT_TRUE(child_menu->GetSubmenu() != NULL);
547 ASSERT_TRUE(child_menu->GetSubmenu()->IsShowing());
549 // Click on third child, which has a submenu too.
550 child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
551 ASSERT_TRUE(child_menu != NULL);
552 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
553 ui_controls::DOWN | ui_controls::UP,
554 CreateEventTask(this, &BookmarkBarViewTest3::Step4));
557 void Step4() {
558 // Make sure sub menu we first clicked isn't showing.
559 views::MenuItemView* menu = bb_view_->GetMenu();
560 views::MenuItemView* child_menu =
561 menu->GetSubmenu()->GetMenuItemAt(1);
562 ASSERT_TRUE(child_menu->GetSubmenu() != NULL);
563 ASSERT_FALSE(child_menu->GetSubmenu()->IsShowing());
565 // And submenu we last clicked is showing.
566 child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
567 ASSERT_TRUE(child_menu != NULL);
568 ASSERT_TRUE(child_menu->GetSubmenu()->IsShowing());
570 // Nothing should have been selected.
571 EXPECT_EQ(GURL(), navigator_.url_);
573 // Hide menu.
574 menu->GetMenuController()->CancelAll();
576 Done();
580 VIEW_TEST(BookmarkBarViewTest3, Submenus)
582 // Observer that posts task upon the context menu creation.
583 // This is necessary for Linux as the context menu has to check
584 // the clipboard, which invokes the event loop.
585 class BookmarkContextMenuNotificationObserver
586 : public content::NotificationObserver {
587 public:
588 explicit BookmarkContextMenuNotificationObserver(const base::Closure& task)
589 : task_(task) {
590 registrar_.Add(this,
591 chrome::NOTIFICATION_BOOKMARK_CONTEXT_MENU_SHOWN,
592 content::NotificationService::AllSources());
595 void Observe(int type,
596 const content::NotificationSource& source,
597 const content::NotificationDetails& details) override {
598 base::MessageLoop::current()->task_runner()->PostTask(FROM_HERE, task_);
601 // Sets the task that is posted when the context menu is shown.
602 void set_task(const base::Closure& task) { task_ = task; }
604 private:
605 content::NotificationRegistrar registrar_;
606 base::Closure task_;
608 DISALLOW_COPY_AND_ASSIGN(BookmarkContextMenuNotificationObserver);
611 // Tests context menus by way of opening a context menu for a bookmark,
612 // then right clicking to get context menu and selecting the first menu item
613 // (open).
614 class BookmarkBarViewTest4 : public BookmarkBarViewEventTestBase {
615 public:
616 BookmarkBarViewTest4()
617 : observer_(CreateEventTask(this, &BookmarkBarViewTest4::Step3)) {
620 protected:
621 void DoTestOnMessageLoop() override {
622 // Move the mouse to the first folder on the bookmark bar and press the
623 // mouse.
624 views::LabelButton* button = bb_view_->other_bookmarks_button();
625 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
626 ui_controls::DOWN | ui_controls::UP,
627 CreateEventTask(this, &BookmarkBarViewTest4::Step2));
630 private:
631 void Step2() {
632 // Menu should be showing.
633 views::MenuItemView* menu = bb_view_->GetMenu();
634 ASSERT_TRUE(menu != NULL);
635 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
637 views::MenuItemView* child_menu =
638 menu->GetSubmenu()->GetMenuItemAt(0);
639 ASSERT_TRUE(child_menu != NULL);
641 // Right click on the first child to get its context menu.
642 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
643 ui_controls::DOWN | ui_controls::UP, base::Closure());
644 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
647 void Step3() {
648 // Make sure the context menu is showing.
649 views::MenuItemView* menu = bb_view_->GetContextMenu();
650 ASSERT_TRUE(menu != NULL);
651 ASSERT_TRUE(menu->GetSubmenu());
652 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
654 // Select the first menu item (open).
655 ui_test_utils::MoveMouseToCenterAndPress(
656 menu->GetSubmenu()->GetMenuItemAt(0),
657 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
658 CreateEventTask(this, &BookmarkBarViewTest4::Step4));
661 void Step4() {
662 EXPECT_EQ(navigator_.url_, model_->other_node()->GetChild(0)->url());
663 Done();
666 BookmarkContextMenuNotificationObserver observer_;
669 VIEW_TEST(BookmarkBarViewTest4, ContextMenus)
671 // Tests drag and drop within the same menu.
672 class BookmarkBarViewTest5 : public BookmarkBarViewEventTestBase {
673 protected:
674 void DoTestOnMessageLoop() override {
675 url_dragging_ =
676 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
678 // Move the mouse to the first folder on the bookmark bar and press the
679 // mouse.
680 views::LabelButton* button = GetBookmarkButton(0);
681 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
682 ui_controls::DOWN | ui_controls::UP,
683 CreateEventTask(this, &BookmarkBarViewTest5::Step2));
686 private:
687 void Step2() {
688 // Menu should be showing.
689 views::MenuItemView* menu = bb_view_->GetMenu();
690 ASSERT_TRUE(menu != NULL);
691 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
693 views::MenuItemView* child_menu =
694 menu->GetSubmenu()->GetMenuItemAt(0);
695 ASSERT_TRUE(child_menu != NULL);
697 // Move mouse to center of menu and press button.
698 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
699 ui_controls::DOWN,
700 CreateEventTask(this, &BookmarkBarViewTest5::Step3));
703 void Step3() {
704 views::MenuItemView* target_menu =
705 bb_view_->GetMenu()->GetSubmenu()->GetMenuItemAt(1);
706 gfx::Point loc(1, target_menu->height() - 1);
707 views::View::ConvertPointToScreen(target_menu, &loc);
709 // Start a drag.
710 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
711 CreateEventTask(this, &BookmarkBarViewTest5::Step4));
713 // See comment above this method as to why we do this.
714 ScheduleMouseMoveInBackground(loc.x(), loc.y());
717 void Step4() {
718 // Drop the item so that it's now the second item.
719 views::MenuItemView* target_menu =
720 bb_view_->GetMenu()->GetSubmenu()->GetMenuItemAt(1);
721 gfx::Point loc(1, target_menu->height() - 2);
722 views::View::ConvertPointToScreen(target_menu, &loc);
723 ui_controls::SendMouseMove(loc.x(), loc.y());
725 ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT,
726 ui_controls::UP,
727 CreateEventTask(this, &BookmarkBarViewTest5::Step5));
730 void Step5() {
731 GURL url = model_->bookmark_bar_node()->GetChild(0)->GetChild(1)->url();
732 EXPECT_EQ(url_dragging_, url);
733 Done();
736 GURL url_dragging_;
739 #if !defined(OS_WIN) // flaky http://crbug.com/400578
740 VIEW_TEST(BookmarkBarViewTest5, DND)
741 #endif
743 // Tests holding mouse down on overflow button, dragging such that menu pops up
744 // then selecting an item.
745 class BookmarkBarViewTest6 : public BookmarkBarViewEventTestBase {
746 protected:
747 void DoTestOnMessageLoop() override {
748 // Press the mouse button on the overflow button. Don't release it though.
749 views::LabelButton* button = bb_view_->overflow_button();
750 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
751 ui_controls::DOWN, CreateEventTask(this, &BookmarkBarViewTest6::Step2));
754 private:
755 void Step2() {
756 // Menu should be showing.
757 views::MenuItemView* menu = bb_view_->GetMenu();
758 ASSERT_TRUE(menu != NULL);
759 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
761 views::MenuItemView* child_menu =
762 menu->GetSubmenu()->GetMenuItemAt(0);
763 ASSERT_TRUE(child_menu != NULL);
765 // Move mouse to center of menu and release mouse.
766 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
767 ui_controls::UP, CreateEventTask(this, &BookmarkBarViewTest6::Step3));
770 void Step3() {
771 ASSERT_TRUE(navigator_.url_ ==
772 model_->bookmark_bar_node()->GetChild(6)->url());
773 Done();
776 GURL url_dragging_;
779 #if defined(OS_WIN) // flaky http://crbug.com/523255
780 #define MAYBE_OpenMenuOnClickAndHold DISABLED_OpenMenuOnClickAndHold
781 #else
782 #define MAYBE_OpenMenuOnClickAndHold OpenMenuOnClickAndHold
783 #endif
784 VIEW_TEST(BookmarkBarViewTest6, MAYBE_OpenMenuOnClickAndHold)
786 // Tests drag and drop to different menu.
787 class BookmarkBarViewTest7 : public BookmarkBarViewEventTestBase {
788 protected:
789 void DoTestOnMessageLoop() override {
790 url_dragging_ =
791 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
793 // Move the mouse to the first folder on the bookmark bar and press the
794 // mouse.
795 views::LabelButton* button = GetBookmarkButton(0);
796 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
797 ui_controls::DOWN | ui_controls::UP,
798 CreateEventTask(this, &BookmarkBarViewTest7::Step2));
801 private:
802 void Step2() {
803 // Menu should be showing.
804 views::MenuItemView* menu = bb_view_->GetMenu();
805 ASSERT_TRUE(menu != NULL);
806 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
808 views::MenuItemView* child_menu =
809 menu->GetSubmenu()->GetMenuItemAt(0);
810 ASSERT_TRUE(child_menu != NULL);
812 // Move mouse to center of menu and press button.
813 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
814 ui_controls::DOWN,
815 CreateEventTask(this, &BookmarkBarViewTest7::Step3));
818 void Step3() {
819 // Drag over other button.
820 views::LabelButton* other_button = bb_view_->other_bookmarks_button();
821 gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
822 views::View::ConvertPointToScreen(other_button, &loc);
824 #if defined(USE_AURA)
825 // TODO: fix this. Aura requires an additional mouse event to trigger drag
826 // and drop checking state.
827 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
828 base::Bind(&BookmarkBarViewTest7::Step3A, this));
829 #else
830 // Start a drag.
831 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
832 base::Bind(&BookmarkBarViewTest7::Step4, this));
834 // See comment above this method as to why we do this.
835 ScheduleMouseMoveInBackground(loc.x(), loc.y());
836 #endif
839 void Step3A() {
840 // Drag over other button.
841 views::LabelButton* other_button = bb_view_->other_bookmarks_button();
842 gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
843 views::View::ConvertPointToScreen(other_button, &loc);
845 ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
846 base::Bind(&BookmarkBarViewTest7::Step4, this));
849 void Step4() {
850 views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
851 ASSERT_TRUE(drop_menu != NULL);
852 ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
854 views::MenuItemView* target_menu =
855 drop_menu->GetSubmenu()->GetMenuItemAt(0);
856 gfx::Point loc(1, 1);
857 views::View::ConvertPointToScreen(target_menu, &loc);
858 ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
859 CreateEventTask(this, &BookmarkBarViewTest7::Step5));
862 void Step5() {
863 ui_controls::SendMouseEventsNotifyWhenDone(
864 ui_controls::LEFT, ui_controls::UP,
865 CreateEventTask(this, &BookmarkBarViewTest7::Step6));
868 void Step6() {
869 ASSERT_TRUE(model_->other_node()->GetChild(0)->url() == url_dragging_);
870 Done();
873 GURL url_dragging_;
876 #if !defined(OS_WIN)
877 // This test passes locally (on aero and non-aero) but fails on the trybots and
878 // buildbot.
879 // http://crbug.com/154081
880 VIEW_TEST(BookmarkBarViewTest7, MAYBE(DNDToDifferentMenu))
881 #endif
883 // Drags from one menu to next so that original menu closes, then back to
884 // original menu.
885 class BookmarkBarViewTest8 : public BookmarkBarViewEventTestBase {
886 protected:
887 void DoTestOnMessageLoop() override {
888 url_dragging_ =
889 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url();
891 // Move the mouse to the first folder on the bookmark bar and press the
892 // mouse.
893 views::LabelButton* button = GetBookmarkButton(0);
894 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
895 ui_controls::DOWN | ui_controls::UP,
896 CreateEventTask(this, &BookmarkBarViewTest8::Step2));
899 private:
900 void Step2() {
901 // Menu should be showing.
902 views::MenuItemView* menu = bb_view_->GetMenu();
903 ASSERT_TRUE(menu != NULL);
904 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
906 views::MenuItemView* child_menu =
907 menu->GetSubmenu()->GetMenuItemAt(0);
908 ASSERT_TRUE(child_menu != NULL);
910 // Move mouse to center of menu and press button.
911 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::LEFT,
912 ui_controls::DOWN,
913 CreateEventTask(this, &BookmarkBarViewTest8::Step3));
916 void Step3() {
917 // Drag over other button.
918 views::LabelButton* other_button = bb_view_->other_bookmarks_button();
919 gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
920 views::View::ConvertPointToScreen(other_button, &loc);
922 // Start a drag.
923 #if defined(USE_AURA)
924 // TODO: fix this. Aura requires an additional mouse event to trigger drag
925 // and drop checking state.
926 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
927 base::Bind(&BookmarkBarViewTest8::Step3A, this));
928 #else
929 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
930 base::Bind(&BookmarkBarViewTest8::Step4, this));
931 // See comment above this method as to why we do this.
932 ScheduleMouseMoveInBackground(loc.x(), loc.y());
933 #endif
936 void Step3A() {
937 // Drag over other button.
938 views::LabelButton* other_button = bb_view_->other_bookmarks_button();
939 gfx::Point loc(other_button->width() / 2, other_button->height() / 2);
940 views::View::ConvertPointToScreen(other_button, &loc);
942 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
943 base::Bind(&BookmarkBarViewTest8::Step4, this));
946 void Step4() {
947 views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
948 ASSERT_TRUE(drop_menu != NULL);
949 ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
951 // Now drag back over first menu.
952 views::LabelButton* button = GetBookmarkButton(0);
953 gfx::Point loc(button->width() / 2, button->height() / 2);
954 views::View::ConvertPointToScreen(button, &loc);
955 ui_controls::SendMouseMoveNotifyWhenDone(loc.x(), loc.y(),
956 base::Bind(&BookmarkBarViewTest8::Step5, this));
959 void Step5() {
960 // Drop on folder F11.
961 views::MenuItemView* drop_menu = bb_view_->GetDropMenu();
962 ASSERT_TRUE(drop_menu != NULL);
963 ASSERT_TRUE(drop_menu->GetSubmenu()->IsShowing());
965 views::MenuItemView* target_menu =
966 drop_menu->GetSubmenu()->GetMenuItemAt(1);
967 ui_test_utils::MoveMouseToCenterAndPress(
968 target_menu, ui_controls::LEFT, ui_controls::UP,
969 CreateEventTask(this, &BookmarkBarViewTest8::Step6));
972 void Step6() {
973 // Make sure drop was processed.
974 GURL final_url = model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->
975 GetChild(1)->url();
976 ASSERT_TRUE(final_url == url_dragging_);
977 Done();
980 GURL url_dragging_;
983 #if !defined(OS_WIN)
984 // This test passes locally (on aero and non-aero) but fails on the trybots and
985 // buildbot.
986 // http://crbug.com/154081
987 VIEW_TEST(BookmarkBarViewTest8, MAYBE(DNDBackToOriginatingMenu))
988 #endif
990 // Moves the mouse over the scroll button and makes sure we get scrolling.
991 class BookmarkBarViewTest9 : public BookmarkBarViewEventTestBase {
992 protected:
993 bool CreateBigMenu() override { return true; }
995 void DoTestOnMessageLoop() override {
996 // Move the mouse to the first folder on the bookmark bar and press the
997 // mouse.
998 views::LabelButton* button = GetBookmarkButton(0);
999 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1000 ui_controls::DOWN | ui_controls::UP,
1001 CreateEventTask(this, &BookmarkBarViewTest9::Step2));
1004 private:
1005 void Step2() {
1006 // Menu should be showing.
1007 views::MenuItemView* menu = bb_view_->GetMenu();
1008 ASSERT_TRUE(menu != NULL);
1009 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1011 first_menu_ = menu->GetSubmenu()->GetMenuItemAt(0);
1012 gfx::Point menu_loc;
1013 views::View::ConvertPointToScreen(first_menu_, &menu_loc);
1014 start_y_ = menu_loc.y();
1016 // Move the mouse over the scroll button.
1017 views::View* scroll_container = menu->GetSubmenu()->parent();
1018 ASSERT_TRUE(scroll_container != NULL);
1019 scroll_container = scroll_container->parent();
1020 ASSERT_TRUE(scroll_container != NULL);
1021 views::View* scroll_down_button = scroll_container->child_at(1);
1022 ASSERT_TRUE(scroll_down_button);
1023 gfx::Point loc(scroll_down_button->width() / 2,
1024 scroll_down_button->height() / 2);
1025 views::View::ConvertPointToScreen(scroll_down_button, &loc);
1027 // On linux, the sending one location isn't enough.
1028 ui_controls::SendMouseMove(loc.x() - 1 , loc.y() - 1);
1029 ui_controls::SendMouseMoveNotifyWhenDone(
1030 loc.x(), loc.y(), CreateEventTask(this, &BookmarkBarViewTest9::Step3));
1033 void Step3() {
1034 base::MessageLoop::current()->task_runner()->PostDelayedTask(
1035 FROM_HERE, base::Bind(&BookmarkBarViewTest9::Step4, this),
1036 base::TimeDelta::FromMilliseconds(200));
1039 void Step4() {
1040 gfx::Point menu_loc;
1041 views::View::ConvertPointToScreen(first_menu_, &menu_loc);
1042 ASSERT_NE(start_y_, menu_loc.y());
1044 // Hide menu.
1045 bb_view_->GetMenu()->GetMenuController()->CancelAll();
1047 // On linux, Cancelling menu will call Quit on the message loop,
1048 // which can interfere with Done. We need to run Done in the
1049 // next execution loop.
1050 base::MessageLoop::current()->task_runner()->PostTask(
1051 FROM_HERE, base::Bind(&ViewEventTestBase::Done, this));
1054 int start_y_;
1055 views::MenuItemView* first_menu_;
1058 // Fails on official cros bot. crbug.com/431427.
1059 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
1060 #define MAYBE_ScrollButtonScrolls DISABLED_ScrollButtonScrolls
1061 #else
1062 #define MAYBE_ScrollButtonScrolls ScrollButtonScrolls
1063 #endif
1065 VIEW_TEST(BookmarkBarViewTest9, MAYBE_ScrollButtonScrolls)
1067 // Tests up/down/left/enter key messages.
1068 class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {
1069 protected:
1070 void DoTestOnMessageLoop() override {
1071 // Move the mouse to the first folder on the bookmark bar and press the
1072 // mouse.
1073 views::LabelButton* button = GetBookmarkButton(0);
1074 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1075 ui_controls::DOWN | ui_controls::UP,
1076 CreateEventTask(this, &BookmarkBarViewTest10::Step2));
1077 base::MessageLoop::current()->RunUntilIdle();
1080 private:
1081 void Step2() {
1082 // Menu should be showing.
1083 views::MenuItemView* menu = bb_view_->GetMenu();
1084 ASSERT_TRUE(menu != NULL);
1085 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1087 // Send a down event, which should select the first item.
1088 ui_controls::SendKeyPressNotifyWhenDone(
1089 window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
1090 CreateEventTask(this, &BookmarkBarViewTest10::Step3));
1093 void Step3() {
1094 // Make sure menu is showing and item is selected.
1095 views::MenuItemView* menu = bb_view_->GetMenu();
1096 ASSERT_TRUE(menu != NULL);
1097 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1098 ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
1100 // Send a key down event, which should select the next item.
1101 ui_controls::SendKeyPressNotifyWhenDone(
1102 window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
1103 CreateEventTask(this, &BookmarkBarViewTest10::Step4));
1106 void Step4() {
1107 views::MenuItemView* menu = bb_view_->GetMenu();
1108 ASSERT_TRUE(menu != NULL);
1109 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1110 ASSERT_FALSE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
1111 ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(1)->IsSelected());
1113 // Send a right arrow to force the menu to open.
1114 ui_controls::SendKeyPressNotifyWhenDone(
1115 window_->GetNativeWindow(), ui::VKEY_RIGHT, false, false, false, false,
1116 CreateEventTask(this, &BookmarkBarViewTest10::Step5));
1119 void Step5() {
1120 // Make sure the submenu is showing.
1121 views::MenuItemView* menu = bb_view_->GetMenu();
1122 ASSERT_TRUE(menu != NULL);
1123 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1124 views::MenuItemView* submenu = menu->GetSubmenu()->GetMenuItemAt(1);
1125 ASSERT_TRUE(submenu->IsSelected());
1126 ASSERT_TRUE(submenu->GetSubmenu());
1127 ASSERT_TRUE(submenu->GetSubmenu()->IsShowing());
1129 // Send a left arrow to close the submenu.
1130 ui_controls::SendKeyPressNotifyWhenDone(
1131 window_->GetNativeWindow(), ui::VKEY_LEFT, false, false, false, false,
1132 CreateEventTask(this, &BookmarkBarViewTest10::Step6));
1135 void Step6() {
1136 // Make sure the submenu is showing.
1137 views::MenuItemView* menu = bb_view_->GetMenu();
1138 ASSERT_TRUE(menu != NULL);
1139 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1140 views::MenuItemView* submenu = menu->GetSubmenu()->GetMenuItemAt(1);
1141 ASSERT_TRUE(submenu->IsSelected());
1142 ASSERT_TRUE(!submenu->GetSubmenu() || !submenu->GetSubmenu()->IsShowing());
1144 // Send a down arrow to wrap back to f1a
1145 ui_controls::SendKeyPressNotifyWhenDone(
1146 window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
1147 CreateEventTask(this, &BookmarkBarViewTest10::Step7));
1150 void Step7() {
1151 // Make sure menu is showing and item is selected.
1152 views::MenuItemView* menu = bb_view_->GetMenu();
1153 ASSERT_TRUE(menu != NULL);
1154 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1155 ASSERT_TRUE(menu->GetSubmenu()->GetMenuItemAt(0)->IsSelected());
1157 // Send enter, which should select the item.
1158 ui_controls::SendKeyPressNotifyWhenDone(
1159 window_->GetNativeWindow(), ui::VKEY_RETURN, false, false, false, false,
1160 CreateEventTask(this, &BookmarkBarViewTest10::Step8));
1163 void Step8() {
1164 ASSERT_TRUE(
1165 model_->bookmark_bar_node()->GetChild(0)->GetChild(0)->url() ==
1166 navigator_.url_);
1167 Done();
1171 #if defined(USE_OZONE)
1172 // ozone bringup - http://crbug.com/401304
1173 #define MAYBE_KeyEvents DISABLED_KeyEvents
1174 #else
1175 #define MAYBE_KeyEvents KeyEvents
1176 #endif
1178 VIEW_TEST(BookmarkBarViewTest10, MAYBE_KeyEvents)
1180 // Make sure the menu closes with the following sequence: show menu, show
1181 // context menu, close context menu (via escape), then click else where. This
1182 // effectively verifies we maintain mouse capture after the context menu is
1183 // hidden.
1184 class BookmarkBarViewTest11 : public BookmarkBarViewEventTestBase {
1185 public:
1186 BookmarkBarViewTest11()
1187 : observer_(CreateEventTask(this, &BookmarkBarViewTest11::Step3)) {
1190 protected:
1191 void DoTestOnMessageLoop() override {
1192 // Move the mouse to the first folder on the bookmark bar and press the
1193 // mouse.
1194 views::LabelButton* button = bb_view_->other_bookmarks_button();
1195 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1196 ui_controls::DOWN | ui_controls::UP,
1197 CreateEventTask(this, &BookmarkBarViewTest11::Step2));
1200 private:
1201 void Step2() {
1202 // Menu should be showing.
1203 views::MenuItemView* menu = bb_view_->GetMenu();
1204 ASSERT_TRUE(menu != NULL);
1205 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1207 views::MenuItemView* child_menu =
1208 menu->GetSubmenu()->GetMenuItemAt(0);
1209 ASSERT_TRUE(child_menu != NULL);
1211 // Right click on the first child to get its context menu.
1212 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1213 ui_controls::DOWN | ui_controls::UP, base::Closure());
1214 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1217 void Step3() {
1218 // Send escape so that the context menu hides.
1219 ui_controls::SendKeyPressNotifyWhenDone(
1220 window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
1221 CreateEventTask(this, &BookmarkBarViewTest11::Step4));
1224 void Step4() {
1225 // Make sure the context menu is no longer showing.
1226 views::MenuItemView* menu = bb_view_->GetContextMenu();
1227 ASSERT_TRUE(!menu || !menu->GetSubmenu() ||
1228 !menu->GetSubmenu()->IsShowing());
1230 // But the menu should be showing.
1231 menu = bb_view_->GetMenu();
1232 ASSERT_TRUE(menu && menu->GetSubmenu() && menu->GetSubmenu()->IsShowing());
1234 // Now click on empty space.
1235 gfx::Point mouse_loc;
1236 views::View::ConvertPointToScreen(bb_view_.get(), &mouse_loc);
1237 ui_controls::SendMouseMove(mouse_loc.x(), mouse_loc.y());
1238 ui_controls::SendMouseEventsNotifyWhenDone(
1239 ui_controls::LEFT, ui_controls::UP | ui_controls::DOWN,
1240 CreateEventTask(this, &BookmarkBarViewTest11::Step5));
1243 void Step5() {
1244 // Make sure the menu is not showing.
1245 views::MenuItemView* menu = bb_view_->GetMenu();
1246 ASSERT_TRUE(!menu || !menu->GetSubmenu() ||
1247 !menu->GetSubmenu()->IsShowing());
1248 Done();
1251 BookmarkContextMenuNotificationObserver observer_;
1254 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1255 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1256 #define MAYBE_CloseMenuAfterClosingContextMenu \
1257 DISABLED_CloseMenuAfterClosingContextMenu
1258 #elif defined(USE_OZONE)
1259 // ozone bringup - http://crbug.com/401304
1260 #define MAYBE_CloseMenuAfterClosingContextMenu \
1261 DISABLED_CloseMenuAfterClosingContextMenu
1262 #else
1263 #define MAYBE_CloseMenuAfterClosingContextMenu CloseMenuAfterClosingContextMenu
1264 #endif
1266 VIEW_TEST(BookmarkBarViewTest11, MAYBE_CloseMenuAfterClosingContextMenu)
1268 // Tests showing a modal dialog from a context menu.
1269 class BookmarkBarViewTest12 : public BookmarkBarViewEventTestBase {
1270 protected:
1271 void DoTestOnMessageLoop() override {
1272 // Open up the other folder.
1273 views::LabelButton* button = bb_view_->other_bookmarks_button();
1274 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1275 ui_controls::DOWN | ui_controls::UP,
1276 CreateEventTask(this, &BookmarkBarViewTest12::Step2));
1277 chrome::num_bookmark_urls_before_prompting = 1;
1280 ~BookmarkBarViewTest12() override {
1281 chrome::num_bookmark_urls_before_prompting = 15;
1284 private:
1285 void Step2() {
1286 // Menu should be showing.
1287 views::MenuItemView* menu = bb_view_->GetMenu();
1288 ASSERT_TRUE(menu != NULL);
1289 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1291 views::MenuItemView* child_menu =
1292 menu->GetSubmenu()->GetMenuItemAt(1);
1293 ASSERT_TRUE(child_menu != NULL);
1295 // Right click on the second child (a folder) to get its context menu.
1296 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1297 ui_controls::DOWN | ui_controls::UP,
1298 CreateEventTask(this, &BookmarkBarViewTest12::Step3));
1301 void Step3() {
1302 // Make sure the context menu is showing.
1303 views::MenuItemView* menu = bb_view_->GetContextMenu();
1304 ASSERT_TRUE(menu && menu->GetSubmenu() && menu->GetSubmenu()->IsShowing());
1306 // Select the first item in the context menu (open all).
1307 views::MenuItemView* child_menu =
1308 menu->GetSubmenu()->GetMenuItemAt(0);
1309 ASSERT_TRUE(child_menu != NULL);
1311 // Click and wait until the dialog box appears.
1312 scoped_ptr<DialogWaiter> dialog_waiter(new DialogWaiter());
1313 ui_test_utils::MoveMouseToCenterAndPress(
1314 child_menu,
1315 ui_controls::LEFT,
1316 ui_controls::DOWN | ui_controls::UP,
1317 base::Bind(
1318 &BookmarkBarViewTest12::Step4, this, base::Passed(&dialog_waiter)));
1321 void Step4(scoped_ptr<DialogWaiter> waiter) {
1322 views::Widget* dialog = waiter->WaitForDialog();
1323 waiter.reset();
1325 // Press tab to give focus to the cancel button. Wait until the widget
1326 // receives the tab key.
1327 TabKeyWaiter tab_waiter(dialog);
1328 ui_controls::SendKeyPress(
1329 window_->GetNativeWindow(), ui::VKEY_TAB, false, false, false, false);
1330 tab_waiter.WaitForTab();
1332 // For some reason return isn't processed correctly unless we delay.
1333 base::MessageLoop::current()->task_runner()->PostDelayedTask(
1334 FROM_HERE, base::Bind(&BookmarkBarViewTest12::Step5, this,
1335 base::Unretained(dialog)),
1336 base::TimeDelta::FromSeconds(1));
1339 void Step5(views::Widget* dialog) {
1340 DialogCloseWaiter waiter(dialog);
1341 // And press enter so that the cancel button is selected.
1342 ui_controls::SendKeyPressNotifyWhenDone(window_->GetNativeWindow(),
1343 ui::VKEY_RETURN,
1344 false,
1345 false,
1346 false,
1347 false,
1348 base::Closure());
1349 waiter.WaitForDialogClose();
1350 Done();
1354 // Times out on Win. http://crbug.com/499858
1355 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
1356 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1357 #define MAYBE_CloseWithModalDialog DISABLED_CloseWithModalDialog
1358 #else
1359 #define MAYBE_CloseWithModalDialog CloseWithModalDialog
1360 #endif
1362 VIEW_TEST(BookmarkBarViewTest12, MAYBE_CloseWithModalDialog)
1364 // Tests clicking on the separator of a context menu (this is for coverage of
1365 // bug 17862).
1366 class BookmarkBarViewTest13 : public BookmarkBarViewEventTestBase {
1367 public:
1368 BookmarkBarViewTest13()
1369 : observer_(CreateEventTask(this, &BookmarkBarViewTest13::Step3)) {
1372 protected:
1373 void DoTestOnMessageLoop() override {
1374 // Move the mouse to the first folder on the bookmark bar and press the
1375 // mouse.
1376 views::LabelButton* button = bb_view_->other_bookmarks_button();
1377 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1378 ui_controls::DOWN | ui_controls::UP,
1379 CreateEventTask(this, &BookmarkBarViewTest13::Step2));
1382 private:
1383 void Step2() {
1384 // Menu should be showing.
1385 views::MenuItemView* menu = bb_view_->GetMenu();
1386 ASSERT_TRUE(menu != NULL);
1387 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1389 views::MenuItemView* child_menu =
1390 menu->GetSubmenu()->GetMenuItemAt(0);
1391 ASSERT_TRUE(child_menu != NULL);
1393 // Right click on the first child to get its context menu.
1394 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1395 ui_controls::DOWN | ui_controls::UP, base::Closure());
1396 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1399 void Step3() {
1400 // Make sure the context menu is showing.
1401 views::MenuItemView* menu = bb_view_->GetContextMenu();
1402 ASSERT_TRUE(menu != NULL);
1403 ASSERT_TRUE(menu->GetSubmenu());
1404 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1406 // Find the first separator.
1407 views::SubmenuView* submenu = menu->GetSubmenu();
1408 views::View* separator_view = NULL;
1409 for (int i = 0; i < submenu->child_count(); ++i) {
1410 if (submenu->child_at(i)->id() != views::MenuItemView::kMenuItemViewID) {
1411 separator_view = submenu->child_at(i);
1412 break;
1415 ASSERT_TRUE(separator_view);
1417 // Click on the separator. Clicking on the separator shouldn't visually
1418 // change anything.
1419 ui_test_utils::MoveMouseToCenterAndPress(separator_view,
1420 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1421 CreateEventTask(this, &BookmarkBarViewTest13::Step4));
1424 void Step4() {
1425 // The context menu should still be showing.
1426 views::MenuItemView* menu = bb_view_->GetContextMenu();
1427 ASSERT_TRUE(menu != NULL);
1428 ASSERT_TRUE(menu->GetSubmenu());
1429 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1431 // Select the first context menu item.
1432 ui_test_utils::MoveMouseToCenterAndPress(
1433 menu->GetSubmenu()->GetMenuItemAt(0),
1434 ui_controls::LEFT,
1435 ui_controls::DOWN | ui_controls::UP,
1436 CreateEventTask(this, &BookmarkBarViewTest13::Step5));
1439 void Step5() {
1440 Done();
1443 BookmarkContextMenuNotificationObserver observer_;
1446 VIEW_TEST(BookmarkBarViewTest13, ClickOnContextMenuSeparator)
1448 // Makes sure right clicking on a folder on the bookmark bar doesn't result in
1449 // both a context menu and showing the menu.
1450 class BookmarkBarViewTest14 : public BookmarkBarViewEventTestBase {
1451 public:
1452 BookmarkBarViewTest14()
1453 : observer_(CreateEventTask(this, &BookmarkBarViewTest14::Step2)) {
1456 protected:
1457 void DoTestOnMessageLoop() override {
1458 // Move the mouse to the first folder on the bookmark bar and press the
1459 // right mouse button.
1460 views::LabelButton* button = GetBookmarkButton(0);
1461 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::RIGHT,
1462 ui_controls::DOWN | ui_controls::UP, base::Closure());
1463 // Step2 will be invoked by BookmarkContextMenuNotificationObserver.
1466 private:
1468 void Step2() {
1469 // Menu should NOT be showing.
1470 views::MenuItemView* menu = bb_view_->GetMenu();
1471 ASSERT_TRUE(menu == NULL);
1473 // Send escape so that the context menu hides.
1474 ui_controls::SendKeyPressNotifyWhenDone(
1475 window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
1476 CreateEventTask(this, &BookmarkBarViewTest14::Step3));
1479 void Step3() {
1480 Done();
1483 BookmarkContextMenuNotificationObserver observer_;
1486 #if defined(USE_OZONE)
1487 // ozone bringup - http://crbug.com/401304
1488 #define MAYBE_ContextMenus2 DISABLED_ContextMenus2
1489 #else
1490 #define MAYBE_ContextMenus2 ContextMenus2
1491 #endif
1493 VIEW_TEST(BookmarkBarViewTest14, MAYBE_ContextMenus2)
1495 // Makes sure deleting from the context menu keeps the bookmark menu showing.
1496 class BookmarkBarViewTest15 : public BookmarkBarViewEventTestBase {
1497 public:
1498 BookmarkBarViewTest15()
1499 : deleted_menu_id_(0),
1500 observer_(CreateEventTask(this, &BookmarkBarViewTest15::Step3)) {
1503 protected:
1504 void DoTestOnMessageLoop() override {
1505 // Show the other bookmarks.
1506 views::LabelButton* button = bb_view_->other_bookmarks_button();
1507 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1508 ui_controls::DOWN | ui_controls::UP,
1509 CreateEventTask(this, &BookmarkBarViewTest15::Step2));
1512 private:
1513 void Step2() {
1514 // Menu should be showing.
1515 views::MenuItemView* menu = bb_view_->GetMenu();
1516 ASSERT_TRUE(menu != NULL);
1517 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1519 views::MenuItemView* child_menu =
1520 menu->GetSubmenu()->GetMenuItemAt(1);
1521 ASSERT_TRUE(child_menu != NULL);
1523 deleted_menu_id_ = child_menu->GetCommand();
1525 // Right click on the second child to get its context menu.
1526 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1527 ui_controls::DOWN | ui_controls::UP, base::Closure());
1528 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1531 void Step3() {
1532 // Make sure the context menu is showing.
1533 views::MenuItemView* menu = bb_view_->GetContextMenu();
1534 ASSERT_TRUE(menu != NULL);
1535 ASSERT_TRUE(menu->GetSubmenu());
1536 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1538 views::MenuItemView* delete_menu =
1539 menu->GetMenuItemByID(IDC_BOOKMARK_BAR_REMOVE);
1540 ASSERT_TRUE(delete_menu);
1542 // Click on the delete button.
1543 ui_test_utils::MoveMouseToCenterAndPress(delete_menu,
1544 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1545 CreateEventTask(this, &BookmarkBarViewTest15::Step4));
1548 void Step4() {
1549 // The context menu should not be showing.
1550 views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1551 ASSERT_TRUE(context_menu == NULL);
1553 // But the menu should be showing.
1554 views::MenuItemView* menu = bb_view_->GetMenu();
1555 ASSERT_TRUE(menu != NULL);
1556 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1558 // And the deleted_menu_id_ should have been removed.
1559 ASSERT_TRUE(menu->GetMenuItemByID(deleted_menu_id_) == NULL);
1561 bb_view_->GetMenu()->GetMenuController()->CancelAll();
1563 Done();
1566 int deleted_menu_id_;
1567 BookmarkContextMenuNotificationObserver observer_;
1570 VIEW_TEST(BookmarkBarViewTest15, MenuStaysVisibleAfterDelete)
1572 // Tests that we don't crash or get stuck if the parent of a menu is closed.
1573 class BookmarkBarViewTest16 : public BookmarkBarViewEventTestBase {
1574 protected:
1575 void DoTestOnMessageLoop() override {
1576 // Move the mouse to the first folder on the bookmark bar and press the
1577 // mouse.
1578 views::LabelButton* button = GetBookmarkButton(0);
1579 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1580 ui_controls::DOWN | ui_controls::UP,
1581 CreateEventTask(this, &BookmarkBarViewTest16::Step2));
1584 private:
1585 void Step2() {
1586 // Menu should be showing.
1587 views::MenuItemView* menu = bb_view_->GetMenu();
1588 ASSERT_TRUE(menu != NULL);
1589 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1591 // Button should be depressed.
1592 views::LabelButton* button = GetBookmarkButton(0);
1593 ASSERT_TRUE(button->state() == views::CustomButton::STATE_PRESSED);
1595 // Close the window.
1596 window_->Close();
1597 window_ = NULL;
1599 base::MessageLoop::current()->task_runner()->PostTask(
1600 FROM_HERE, CreateEventTask(this, &BookmarkBarViewTest16::Done));
1604 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1605 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1606 #define MAYBE_DeleteMenu DISABLED_DeleteMenu
1607 #else
1608 #define MAYBE_DeleteMenu DeleteMenu
1609 #endif
1611 VIEW_TEST(BookmarkBarViewTest16, MAYBE_DeleteMenu)
1613 // Makes sure right clicking on an item while a context menu is already showing
1614 // doesn't crash and works.
1615 class BookmarkBarViewTest17 : public BookmarkBarViewEventTestBase {
1616 public:
1617 BookmarkBarViewTest17()
1618 : observer_(CreateEventTask(this, &BookmarkBarViewTest17::Step3)) {
1621 protected:
1622 void DoTestOnMessageLoop() override {
1623 // Move the mouse to the other folder on the bookmark bar and press the
1624 // left mouse button.
1625 views::LabelButton* button = bb_view_->other_bookmarks_button();
1626 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1627 ui_controls::DOWN | ui_controls::UP,
1628 CreateEventTask(this, &BookmarkBarViewTest17::Step2));
1631 private:
1632 void Step2() {
1633 // Menu should be showing.
1634 views::MenuItemView* menu = bb_view_->GetMenu();
1635 ASSERT_TRUE(menu != NULL);
1636 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1638 // Right click on the second item to show its context menu.
1639 views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(2);
1640 ASSERT_TRUE(child_menu != NULL);
1641 ui_test_utils::MoveMouseToCenterAndPress(child_menu, ui_controls::RIGHT,
1642 ui_controls::DOWN | ui_controls::UP, base::Closure());
1643 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1646 void Step3() {
1647 // Make sure the context menu is showing.
1648 views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1649 ASSERT_TRUE(context_menu != NULL);
1650 ASSERT_TRUE(context_menu->GetSubmenu());
1651 ASSERT_TRUE(context_menu->GetSubmenu()->IsShowing());
1653 // Right click on the first menu item to trigger its context menu.
1654 views::MenuItemView* menu = bb_view_->GetMenu();
1655 ASSERT_TRUE(menu != NULL);
1656 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1657 views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1658 ASSERT_TRUE(child_menu != NULL);
1660 // The context menu and child_menu can be overlapped, calculate the
1661 // non-intersected Rect of the child menu and click on its center to make
1662 // sure the click is always on the child menu.
1663 gfx::Rect context_rect = context_menu->GetSubmenu()->GetBoundsInScreen();
1664 gfx::Rect child_menu_rect = child_menu->GetBoundsInScreen();
1665 gfx::Rect clickable_rect =
1666 gfx::SubtractRects(child_menu_rect, context_rect);
1667 ASSERT_FALSE(clickable_rect.IsEmpty());
1668 observer_.set_task(CreateEventTask(this, &BookmarkBarViewTest17::Step4));
1669 MoveMouseAndPress(clickable_rect.CenterPoint(), ui_controls::RIGHT,
1670 ui_controls::DOWN | ui_controls::UP, base::Closure());
1671 // Step4 will be invoked by BookmarkContextMenuNotificationObserver.
1674 void Step4() {
1675 // The context menu should still be showing.
1676 views::MenuItemView* context_menu = bb_view_->GetContextMenu();
1677 ASSERT_TRUE(context_menu != NULL);
1679 // And the menu should be showing.
1680 views::MenuItemView* menu = bb_view_->GetMenu();
1681 ASSERT_TRUE(menu != NULL);
1682 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1684 bb_view_->GetMenu()->GetMenuController()->CancelAll();
1686 Done();
1689 BookmarkContextMenuNotificationObserver observer_;
1692 #if defined(OS_WIN)
1693 // Flaky on Win7. crbug/453796
1694 #define MAYBE_ContextMenus3 DISABLED_ContextMenus3
1695 #else
1696 #define MAYBE_ContextMenus3 ContextMenus3
1697 #endif
1699 VIEW_TEST(BookmarkBarViewTest17, MAYBE_ContextMenus3)
1701 // Verifies sibling menus works. Clicks on the 'other bookmarks' folder, then
1702 // moves the mouse over the first item on the bookmark bar and makes sure the
1703 // menu appears.
1704 class BookmarkBarViewTest18 : public BookmarkBarViewEventTestBase {
1705 protected:
1706 void DoTestOnMessageLoop() override {
1707 // Move the mouse to the other folder on the bookmark bar and press the
1708 // left mouse button.
1709 views::LabelButton* button = bb_view_->other_bookmarks_button();
1710 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1711 ui_controls::DOWN | ui_controls::UP,
1712 CreateEventTask(this, &BookmarkBarViewTest18::Step2));
1715 private:
1716 void Step2() {
1717 // Menu should be showing.
1718 views::MenuItemView* menu = bb_view_->GetMenu();
1719 ASSERT_TRUE(menu != NULL);
1720 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1722 // Move the mouse to the first folder on the bookmark bar
1723 views::LabelButton* button = GetBookmarkButton(0);
1724 gfx::Point button_center(button->width() / 2, button->height() / 2);
1725 views::View::ConvertPointToScreen(button, &button_center);
1726 ui_controls::SendMouseMoveNotifyWhenDone(
1727 button_center.x(), button_center.y(),
1728 CreateEventTask(this, &BookmarkBarViewTest18::Step3));
1731 void Step3() {
1732 // Make sure the menu is showing.
1733 views::MenuItemView* menu = bb_view_->GetMenu();
1734 ASSERT_TRUE(menu != NULL);
1735 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1737 // The menu for the first folder should be in the pressed state (since the
1738 // menu is showing for it).
1739 EXPECT_EQ(views::CustomButton::STATE_PRESSED,
1740 GetBookmarkButton(0)->state());
1742 menu->GetMenuController()->CancelAll();
1744 Done();
1748 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1749 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1750 #define MAYBE_BookmarkBarViewTest18_SiblingMenu \
1751 DISABLED_BookmarkBarViewTest18_SiblingMenu
1752 #else
1753 #define MAYBE_BookmarkBarViewTest18_SiblingMenu \
1754 BookmarkBarViewTest18_SiblingMenu
1755 #endif
1757 VIEW_TEST(BookmarkBarViewTest18, MAYBE_BookmarkBarViewTest18_SiblingMenu)
1759 // Verifies mousing over an already open sibling menu doesn't prematurely cancel
1760 // the menu.
1761 class BookmarkBarViewTest19 : public BookmarkBarViewEventTestBase {
1762 protected:
1763 void DoTestOnMessageLoop() override {
1764 // Move the mouse to the other folder on the bookmark bar and press the
1765 // left mouse button.
1766 views::LabelButton* button = bb_view_->other_bookmarks_button();
1767 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1768 ui_controls::DOWN | ui_controls::UP,
1769 CreateEventTask(this, &BookmarkBarViewTest19::Step2));
1772 private:
1773 void Step2() {
1774 // Menu should be showing.
1775 views::MenuItemView* menu = bb_view_->GetMenu();
1776 ASSERT_TRUE(menu != NULL);
1777 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1779 // Click on the first folder.
1780 views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1781 ASSERT_TRUE(child_menu != NULL);
1782 ui_test_utils::MoveMouseToCenterAndPress(
1783 child_menu, ui_controls::LEFT,
1784 ui_controls::DOWN | ui_controls::UP,
1785 CreateEventTask(this, &BookmarkBarViewTest19::Step3));
1788 void Step3() {
1789 // Make sure the menu is showing.
1790 views::MenuItemView* menu = bb_view_->GetMenu();
1791 ASSERT_TRUE(menu != NULL);
1792 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1794 // Move the mouse back to the "Other Bookmarks" button.
1795 views::LabelButton* button = bb_view_->other_bookmarks_button();
1796 gfx::Point button_center(button->width() / 2, button->height() / 2);
1797 views::View::ConvertPointToScreen(button, &button_center);
1798 ui_controls::SendMouseMoveNotifyWhenDone(
1799 button_center.x() + 1, button_center.y() + 1,
1800 CreateEventTask(this, &BookmarkBarViewTest19::Step4));
1803 void Step4() {
1804 // Menu should be showing.
1805 views::MenuItemView* menu = bb_view_->GetMenu();
1806 ASSERT_TRUE(menu != NULL);
1807 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1809 // Click on the first folder.
1810 views::MenuItemView* child_menu = menu->GetSubmenu()->GetMenuItemAt(1);
1811 ASSERT_TRUE(child_menu != NULL);
1812 ui_test_utils::MoveMouseToCenterAndPress(
1813 child_menu,
1814 ui_controls::LEFT,
1815 ui_controls::DOWN | ui_controls::UP,
1816 CreateEventTask(this, &BookmarkBarViewTest19::Step5));
1819 void Step5() {
1820 // Make sure the menu is showing.
1821 views::MenuItemView* menu = bb_view_->GetMenu();
1822 ASSERT_TRUE(menu != NULL);
1823 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1825 menu->GetMenuController()->CancelAll();
1827 Done();
1831 VIEW_TEST(BookmarkBarViewTest19, BookmarkBarViewTest19_SiblingMenu)
1833 // Verify that when clicking a mouse button outside a context menu,
1834 // the context menu is dismissed *and* the underlying view receives
1835 // the the mouse event (due to event reposting).
1836 class BookmarkBarViewTest20 : public BookmarkBarViewEventTestBase {
1837 public:
1838 BookmarkBarViewTest20() : test_view_(new TestViewForMenuExit) {}
1840 protected:
1841 void DoTestOnMessageLoop() override {
1842 // Add |test_view_| next to |bb_view_|.
1843 views::View* parent = bb_view_->parent();
1844 views::View* container_view = new ContainerViewForMenuExit;
1845 container_view->AddChildView(bb_view_.get());
1846 container_view->AddChildView(test_view_);
1847 parent->AddChildView(container_view);
1848 parent->Layout();
1850 ASSERT_EQ(test_view_->press_count(), 0);
1852 // Move the mouse to the Test View and press the left mouse button.
1853 ui_test_utils::MoveMouseToCenterAndPress(
1854 test_view_, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1855 CreateEventTask(this, &BookmarkBarViewTest20::Step1));
1858 private:
1859 void Step1() {
1860 ASSERT_EQ(test_view_->press_count(), 1);
1861 ASSERT_TRUE(bb_view_->GetMenu() == NULL);
1863 // Move the mouse to the first folder on the bookmark bar and press the
1864 // left mouse button.
1865 views::LabelButton* button = GetBookmarkButton(0);
1866 ui_test_utils::MoveMouseToCenterAndPress(
1867 button, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1868 CreateEventTask(this, &BookmarkBarViewTest20::Step2));
1871 void Step2() {
1872 ASSERT_EQ(test_view_->press_count(), 1);
1873 views::MenuItemView* menu = bb_view_->GetMenu();
1874 ASSERT_TRUE(menu != NULL);
1875 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1877 // Move the mouse to the Test View and press the left mouse button.
1878 // The context menu will consume the event and exit. Thereafter,
1879 // the event is reposted and delivered to the Test View which
1880 // increases its press-count.
1881 ui_test_utils::MoveMouseToCenterAndPress(
1882 test_view_, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1883 CreateEventTask(this, &BookmarkBarViewTest20::Step3));
1886 void Step3() {
1887 ASSERT_EQ(test_view_->press_count(), 2);
1888 ASSERT_TRUE(bb_view_->GetMenu() == NULL);
1889 Done();
1892 class ContainerViewForMenuExit : public views::View {
1893 public:
1894 ContainerViewForMenuExit() {
1897 void Layout() override {
1898 DCHECK_EQ(2, child_count());
1899 views::View* bb_view = child_at(0);
1900 views::View* test_view = child_at(1);
1901 const int width = bb_view->width();
1902 const int height = bb_view->height();
1903 bb_view->SetBounds(0,0, width - 22, height);
1904 test_view->SetBounds(width - 20, 0, 20, height);
1907 private:
1909 DISALLOW_COPY_AND_ASSIGN(ContainerViewForMenuExit);
1912 class TestViewForMenuExit : public views::View {
1913 public:
1914 TestViewForMenuExit() : press_count_(0) {
1916 bool OnMousePressed(const ui::MouseEvent& event) override {
1917 ++press_count_;
1918 return true;
1920 int press_count() const { return press_count_; }
1922 private:
1923 int press_count_;
1925 DISALLOW_COPY_AND_ASSIGN(TestViewForMenuExit);
1928 TestViewForMenuExit* test_view_;
1931 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
1932 // TODO(erg): linux_aura bringup: http://crbug.com/163931
1933 #define MAYBE_ContextMenuExitTest DISABLED_ContextMenuExitTest
1934 #else
1935 #define MAYBE_ContextMenuExitTest ContextMenuExitTest
1936 #endif
1938 VIEW_TEST(BookmarkBarViewTest20, MAYBE_ContextMenuExitTest)
1940 // Tests context menu by way of opening a context menu for a empty folder menu.
1941 // The opened context menu should behave as it is from the folder button.
1942 class BookmarkBarViewTest21 : public BookmarkBarViewEventTestBase {
1943 public:
1944 BookmarkBarViewTest21()
1945 : observer_(CreateEventTask(this, &BookmarkBarViewTest21::Step3)) {
1948 protected:
1949 // Move the mouse to the empty folder on the bookmark bar and press the
1950 // left mouse button.
1951 void DoTestOnMessageLoop() override {
1952 views::LabelButton* button = GetBookmarkButton(5);
1953 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
1954 ui_controls::DOWN | ui_controls::UP,
1955 CreateEventTask(this, &BookmarkBarViewTest21::Step2));
1958 private:
1959 // Confirm that a menu for empty folder shows and right click the menu.
1960 void Step2() {
1961 // Menu should be showing.
1962 views::MenuItemView* menu = bb_view_->GetMenu();
1963 ASSERT_TRUE(menu != NULL);
1965 views::SubmenuView* submenu = menu->GetSubmenu();
1966 ASSERT_TRUE(submenu->IsShowing());
1967 ASSERT_EQ(1, submenu->child_count());
1969 views::View* view = submenu->child_at(0);
1970 ASSERT_TRUE(view != NULL);
1971 EXPECT_EQ(views::MenuItemView::kEmptyMenuItemViewID, view->id());
1973 // Right click on the first child to get its context menu.
1974 ui_test_utils::MoveMouseToCenterAndPress(view, ui_controls::RIGHT,
1975 ui_controls::DOWN | ui_controls::UP, base::Closure());
1976 // Step3 will be invoked by BookmarkContextMenuNotificationObserver.
1979 // Confirm that context menu shows and click REMOVE menu.
1980 void Step3() {
1981 // Make sure the context menu is showing.
1982 views::MenuItemView* menu = bb_view_->GetContextMenu();
1983 ASSERT_TRUE(menu != NULL);
1984 ASSERT_TRUE(menu->GetSubmenu());
1985 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
1987 views::MenuItemView* delete_menu =
1988 menu->GetMenuItemByID(IDC_BOOKMARK_BAR_REMOVE);
1989 ASSERT_TRUE(delete_menu);
1991 // Click on the delete menu item.
1992 ui_test_utils::MoveMouseToCenterAndPress(delete_menu,
1993 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
1994 CreateEventTask(this, &BookmarkBarViewTest21::Step4));
1997 // Confirm that the empty folder gets removed and menu doesn't show.
1998 void Step4() {
1999 views::LabelButton* button = GetBookmarkButton(5);
2000 ASSERT_TRUE(button);
2001 EXPECT_EQ(ASCIIToUTF16("d"), button->GetText());
2002 EXPECT_TRUE(bb_view_->GetContextMenu() == NULL);
2003 EXPECT_TRUE(bb_view_->GetMenu() == NULL);
2005 Done();
2008 BookmarkContextMenuNotificationObserver observer_;
2011 #if defined(OS_WIN) // flaky http://crbug.com/523255
2012 #define MAYBE_ContextMenusForEmptyFolder DISABLED_ContextMenusForEmptyFolder
2013 #else
2014 #define MAYBE_ContextMenusForEmptyFolder ContextMenusForEmptyFolder
2015 #endif
2016 VIEW_TEST(BookmarkBarViewTest21, MAYBE_ContextMenusForEmptyFolder)
2018 // Test that closing the source browser window while dragging a bookmark does
2019 // not cause a crash.
2020 class BookmarkBarViewTest22 : public BookmarkBarViewEventTestBase {
2021 protected:
2022 void DoTestOnMessageLoop() override {
2023 // Move the mouse to the first folder on the bookmark bar and press the
2024 // mouse.
2025 views::LabelButton* button = GetBookmarkButton(0);
2026 ui_test_utils::MoveMouseToCenterAndPress(
2027 button, ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
2028 CreateEventTask(this, &BookmarkBarViewTest22::Step2));
2031 private:
2032 void Step2() {
2033 // Menu should be showing.
2034 views::MenuItemView* menu = bb_view_->GetMenu();
2035 ASSERT_TRUE(menu);
2036 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2038 views::MenuItemView* child_menu =
2039 menu->GetSubmenu()->GetMenuItemAt(0);
2040 ASSERT_TRUE(child_menu != NULL);
2042 // Move mouse to center of menu and press button.
2043 ui_test_utils::MoveMouseToCenterAndPress(
2044 child_menu, ui_controls::LEFT, ui_controls::DOWN,
2045 CreateEventTask(this, &BookmarkBarViewTest22::Step3));
2048 void Step3() {
2049 views::MenuItemView* target_menu =
2050 bb_view_->GetMenu()->GetSubmenu()->GetMenuItemAt(1);
2051 gfx::Point loc(1, target_menu->height() - 1);
2052 views::View::ConvertPointToScreen(target_menu, &loc);
2054 // Start a drag.
2055 ui_controls::SendMouseMoveNotifyWhenDone(loc.x() + 10, loc.y(),
2056 CreateEventTask(this, &BookmarkBarViewTest22::Step4));
2057 ScheduleMouseMoveInBackground(loc.x(), loc.y());
2060 void Step4() {
2061 window_->Close();
2062 window_ = NULL;
2064 #if defined(OS_CHROMEOS)
2065 ui_controls::SendMouseEventsNotifyWhenDone(
2066 ui_controls::LEFT, ui_controls::UP,
2067 CreateEventTask(this, &BookmarkBarViewTest22::Done));
2068 #else
2069 // There are no widgets to send the mouse release to.
2070 Done();
2071 #endif
2075 #if defined(OS_WIN)
2076 // This test times out on Windows. TODO(pkotwicz): Find out why.
2077 #define MAYBE_CloseSourceBrowserDuringDrag DISABLED_CloseSourceBrowserDuringDrag
2078 #else
2079 #define MAYBE_CloseSourceBrowserDuringDrag CloseSourceBrowserDuringDrag
2080 #endif
2082 VIEW_TEST(BookmarkBarViewTest22, MAYBE_CloseSourceBrowserDuringDrag)
2084 // Tests opening a context menu for a bookmark node from the keyboard.
2085 class BookmarkBarViewTest23 : public BookmarkBarViewEventTestBase {
2086 public:
2087 BookmarkBarViewTest23()
2088 : observer_(CreateEventTask(this, &BookmarkBarViewTest23::Step4)) {
2091 protected:
2092 void DoTestOnMessageLoop() override {
2093 // Move the mouse to the first folder on the bookmark bar and press the
2094 // mouse.
2095 views::LabelButton* button = bb_view_->other_bookmarks_button();
2096 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
2097 ui_controls::DOWN | ui_controls::UP,
2098 CreateEventTask(this, &BookmarkBarViewTest23::Step2));
2101 private:
2102 void Step2() {
2103 // Menu should be showing.
2104 views::MenuItemView* menu = bb_view_->GetMenu();
2105 ASSERT_TRUE(menu);
2106 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2108 // Navigate down to highlight the first menu item.
2109 ui_controls::SendKeyPressNotifyWhenDone(
2110 GetWidget()->GetNativeWindow(), ui::VKEY_DOWN,
2111 false, false, false, false, // No modifer keys
2112 CreateEventTask(this, &BookmarkBarViewTest23::Step3));
2115 void Step3() {
2116 // Menu should be showing.
2117 views::MenuItemView* menu = bb_view_->GetMenu();
2118 ASSERT_TRUE(menu);
2119 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2121 // Open the context menu via the keyboard.
2122 ui_controls::SendKeyPress(
2123 GetWidget()->GetNativeWindow(), ui::VKEY_APPS,
2124 false, false, false, false); // No modifer keys
2125 // The BookmarkContextMenuNotificationObserver triggers Step4.
2128 void Step4() {
2129 // Make sure the context menu is showing.
2130 views::MenuItemView* menu = bb_view_->GetContextMenu();
2131 ASSERT_TRUE(menu);
2132 ASSERT_TRUE(menu->GetSubmenu());
2133 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2135 // Select the first menu item (open).
2136 ui_test_utils::MoveMouseToCenterAndPress(
2137 menu->GetSubmenu()->GetMenuItemAt(0),
2138 ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP,
2139 CreateEventTask(this, &BookmarkBarViewTest23::Step5));
2142 void Step5() {
2143 EXPECT_EQ(navigator_.url_, model_->other_node()->GetChild(0)->url());
2144 Done();
2147 BookmarkContextMenuNotificationObserver observer_;
2150 #if defined(USE_OZONE)
2151 // ozone bringup - http://crbug.com/401304
2152 #define MAYBE_ContextMenusKeyboard DISABLED_ContextMenusKeyboard
2153 #else
2154 #define MAYBE_ContextMenusKeyboard ContextMenusKeyboard
2155 #endif
2156 VIEW_TEST(BookmarkBarViewTest23, MAYBE_ContextMenusKeyboard)
2158 // Test that pressing escape on a menu opened via the keyboard dismisses the
2159 // context menu but not the parent menu.
2160 class BookmarkBarViewTest24 : public BookmarkBarViewEventTestBase {
2161 public:
2162 BookmarkBarViewTest24()
2163 : observer_(CreateEventTask(this, &BookmarkBarViewTest24::Step4)) {}
2165 protected:
2166 void DoTestOnMessageLoop() override {
2167 // Move the mouse to the first folder on the bookmark bar and press the
2168 // mouse.
2169 views::LabelButton* button = bb_view_->other_bookmarks_button();
2170 ui_test_utils::MoveMouseToCenterAndPress(button, ui_controls::LEFT,
2171 ui_controls::DOWN | ui_controls::UP,
2172 CreateEventTask(this, &BookmarkBarViewTest24::Step2));
2175 private:
2176 void Step2() {
2177 // Menu should be showing.
2178 views::MenuItemView* menu = bb_view_->GetMenu();
2179 ASSERT_TRUE(menu);
2180 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2182 // Navigate down to highlight the first menu item.
2183 ui_controls::SendKeyPressNotifyWhenDone(
2184 GetWidget()->GetNativeWindow(), ui::VKEY_DOWN,
2185 false, false, false, false, // No modifer keys
2186 CreateEventTask(this, &BookmarkBarViewTest24::Step3));
2189 void Step3() {
2190 // Menu should be showing.
2191 views::MenuItemView* menu = bb_view_->GetMenu();
2192 ASSERT_TRUE(menu);
2193 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2195 // Open the context menu via the keyboard.
2196 ui_controls::SendKeyPress(
2197 GetWidget()->GetNativeWindow(), ui::VKEY_APPS,
2198 false, false, false, false); // No modifer keys
2199 // The BookmarkContextMenuNotificationObserver triggers Step4.
2202 void Step4() {
2203 // Make sure the context menu is showing.
2204 views::MenuItemView* menu = bb_view_->GetContextMenu();
2205 ASSERT_TRUE(menu);
2206 ASSERT_TRUE(menu->GetSubmenu());
2207 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2209 // Send escape to close the context menu.
2210 ui_controls::SendKeyPressNotifyWhenDone(
2211 window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
2212 CreateEventTask(this, &BookmarkBarViewTest24::Step5));
2215 void Step5() {
2216 // The context menu should be closed but the parent menu should still be
2217 // showing.
2218 ASSERT_FALSE(bb_view_->GetContextMenu());
2220 views::MenuItemView* menu = bb_view_->GetMenu();
2221 ASSERT_TRUE(menu);
2222 ASSERT_TRUE(menu->GetSubmenu()->IsShowing());
2224 // Send escape to close the main menu.
2225 ui_controls::SendKeyPressNotifyWhenDone(
2226 window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
2227 CreateEventTask(this, &BookmarkBarViewTest24::Done));
2230 BookmarkContextMenuNotificationObserver observer_;
2233 #if defined(USE_OZONE)
2234 // ozone bringup - http://crbug.com/401304
2235 #define MAYBE_ContextMenusKeyboardEscape DISABLED_ContextMenusKeyboardEscape
2236 #else
2237 #define MAYBE_ContextMenusKeyboardEscape ContextMenusKeyboardEscape
2238 #endif
2239 VIEW_TEST(BookmarkBarViewTest24, MAYBE_ContextMenusKeyboardEscape)