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