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 "chrome/test/base/interactive_test_utils.h" 6 7 #include "base/logging.h" 8 #include "base/message_loop/message_loop.h" 9 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/views/frame/browser_view.h" 11 #include "ui/base/test/ui_controls.h" 12 #include "ui/views/focus/focus_manager.h" 13 14 namespace ui_test_utils { 15 16 bool IsViewFocused(const Browser* browser, ViewID vid) { 17 BrowserWindow* browser_window = browser->window(); 18 DCHECK(browser_window); 19 gfx::NativeWindow window = browser_window->GetNativeWindow(); 20 DCHECK(window); 21 const views::Widget* widget = 22 views::Widget::GetTopLevelWidgetForNativeView(window); 23 DCHECK(widget); 24 const views::FocusManager* focus_manager = widget->GetFocusManager(); 25 DCHECK(focus_manager); 26 DCHECK(focus_manager->GetFocusedView()); 27 return focus_manager->GetFocusedView()->id() == vid; 28 } 29 30 void ClickOnView(const Browser* browser, ViewID vid) { 31 views::View* view = 32 BrowserView::GetBrowserViewForBrowser(browser)->GetViewByID(vid); 33 DCHECK(view); 34 MoveMouseToCenterAndPress(view, 35 ui_controls::LEFT, 36 ui_controls::DOWN | ui_controls::UP, 37 base::MessageLoop::QuitClosure()); 38 content::RunMessageLoop(); 39 } 40 41 void MoveMouseToCenterAndPress(views::View* view, 42 ui_controls::MouseButton button, 43 int state, 44 const base::Closure& closure) { 45 DCHECK(view); 46 DCHECK(view->GetWidget()); 47 gfx::Point view_center(view->width() / 2, view->height() / 2); 48 views::View::ConvertPointToScreen(view, &view_center); 49 ui_controls::SendMouseMoveNotifyWhenDone( 50 view_center.x(), 51 view_center.y(), 52 base::Bind(&internal::ClickTask, button, state, closure)); 53 } 54 55 } // namespace ui_test_utils 56