1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/test/base/interactive_test_utils.h"
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/compositor/layer.h"
13 #include "ui/compositor/layer_animator.h"
14 #include "ui/views/focus/focus_manager.h"
16 namespace ui_test_utils
{
18 // Until the whole browser UI is ported to tookit-views on Mac, these need to
19 // use the definitions in interactive_test_utils_mac.mm.
20 #if !defined(OS_MACOSX)
22 bool IsViewFocused(const Browser
* browser
, ViewID vid
) {
23 BrowserWindow
* browser_window
= browser
->window();
24 DCHECK(browser_window
);
25 gfx::NativeWindow window
= browser_window
->GetNativeWindow();
27 const views::Widget
* widget
= views::Widget::GetWidgetForNativeWindow(window
);
29 const views::FocusManager
* focus_manager
= widget
->GetFocusManager();
30 DCHECK(focus_manager
);
31 DCHECK(focus_manager
->GetFocusedView());
32 return focus_manager
->GetFocusedView()->id() == vid
;
35 void ClickOnView(const Browser
* browser
, ViewID vid
) {
37 BrowserView::GetBrowserViewForBrowser(browser
)->GetViewByID(vid
);
39 MoveMouseToCenterAndPress(view
,
41 ui_controls::DOWN
| ui_controls::UP
,
42 base::MessageLoop::QuitClosure());
43 content::RunMessageLoop();
46 void FocusView(const Browser
* browser
, ViewID vid
) {
48 BrowserView::GetBrowserViewForBrowser(browser
)->GetViewByID(vid
);
53 #endif // defined(OS_MACOSX)
55 void MoveMouseToCenterAndPress(views::View
* view
,
56 ui_controls::MouseButton button
,
58 const base::Closure
& closure
) {
60 DCHECK(view
->GetWidget());
61 // Complete any in-progress animation before sending the events so that the
62 // mouse-event targetting happens reliably, and does not flake because of
63 // unreliable animation state.
64 ui::Layer
* layer
= view
->GetWidget()->GetLayer();
66 ui::LayerAnimator
* animator
= layer
->GetAnimator();
67 if (animator
&& animator
->is_animating())
68 animator
->StopAnimating();
71 gfx::Point
view_center(view
->width() / 2, view
->height() / 2);
72 views::View::ConvertPointToScreen(view
, &view_center
);
73 ui_controls::SendMouseMoveNotifyWhenDone(
76 base::Bind(&internal::ClickTask
, button
, state
, closure
));
79 } // namespace ui_test_utils