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