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/aura/window.h"
12 #include "ui/base/test/ui_controls.h"
13 #include "ui/compositor/layer.h"
14 #include "ui/views/focus/focus_manager.h"
16 namespace ui_test_utils
{
18 bool IsViewFocused(const Browser
* browser
, ViewID vid
) {
19 BrowserWindow
* browser_window
= browser
->window();
20 DCHECK(browser_window
);
21 gfx::NativeWindow window
= browser_window
->GetNativeWindow();
23 const views::Widget
* widget
=
24 views::Widget::GetTopLevelWidgetForNativeView(window
);
26 const views::FocusManager
* focus_manager
= widget
->GetFocusManager();
27 DCHECK(focus_manager
);
28 DCHECK(focus_manager
->GetFocusedView());
29 return focus_manager
->GetFocusedView()->id() == vid
;
32 void ClickOnView(const Browser
* browser
, ViewID vid
) {
34 BrowserView::GetBrowserViewForBrowser(browser
)->GetViewByID(vid
);
36 MoveMouseToCenterAndPress(view
,
38 ui_controls::DOWN
| ui_controls::UP
,
39 base::MessageLoop::QuitClosure());
40 content::RunMessageLoop();
43 void MoveMouseToCenterAndPress(views::View
* view
,
44 ui_controls::MouseButton button
,
46 const base::Closure
& closure
) {
48 DCHECK(view
->GetWidget());
49 // Complete any in-progress animation before sending the events so that the
50 // mouse-event targetting happens reliably, and does not flake because of
51 // unreliable animation state.
52 aura::Window
* window
= view
->GetWidget()->GetNativeView();
53 if (window
&& window
->layer()) {
54 ui::LayerAnimator
* animator
= window
->layer()->GetAnimator();
55 if (animator
&& animator
->is_animating())
56 animator
->StopAnimating();
59 gfx::Point
view_center(view
->width() / 2, view
->height() / 2);
60 views::View::ConvertPointToScreen(view
, &view_center
);
61 ui_controls::SendMouseMoveNotifyWhenDone(
64 base::Bind(&internal::ClickTask
, button
, state
, closure
));
67 } // namespace ui_test_utils