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"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/browser/ui/gtk/view_id_util.h"
15 #include "ui/base/gtk/gtk_screen_util.h"
16 #include "ui/base/test/ui_controls.h"
18 namespace ui_test_utils
{
21 // Check if the focused widget for |root| is |target| or a child of |target|.
22 static bool IsWidgetInFocusChain(GtkWidget
* root
, GtkWidget
* target
) {
23 GtkWidget
* iter
= root
;
29 if (!GTK_IS_CONTAINER(iter
))
32 iter
= GTK_CONTAINER(iter
)->focus_child
;
39 bool IsViewFocused(const Browser
* browser
, ViewID vid
) {
40 BrowserWindow
* browser_window
= browser
->window();
41 DCHECK(browser_window
);
42 gfx::NativeWindow window
= browser_window
->GetNativeWindow();
44 GtkWidget
* widget
= ViewIDUtil::GetWidget(GTK_WIDGET(window
), vid
);
46 return IsWidgetInFocusChain(GTK_WIDGET(window
), widget
);
49 void ClickOnView(const Browser
* browser
, ViewID vid
) {
50 BrowserWindow
* browser_window
= browser
->window();
51 DCHECK(browser_window
);
52 gfx::NativeWindow window
= browser_window
->GetNativeWindow();
54 GtkWidget
* view
= ViewIDUtil::GetWidget(GTK_WIDGET(window
), vid
);
56 MoveMouseToCenterAndPress(view
,
58 ui_controls::DOWN
| ui_controls::UP
,
59 base::MessageLoop::QuitClosure());
60 content::RunMessageLoop();
63 void HideNativeWindow(gfx::NativeWindow window
) {
64 gtk_widget_hide(GTK_WIDGET(window
));
67 bool ShowAndFocusNativeWindow(gfx::NativeWindow window
) {
68 if (!gtk_window_has_toplevel_focus(GTK_WINDOW(window
)))
69 gtk_window_present(GTK_WINDOW(window
));
73 void MoveMouseToCenterAndPress(GtkWidget
* widget
,
74 ui_controls::MouseButton button
,
76 const base::Closure
& task
) {
77 gfx::Rect bounds
= ui::GetWidgetScreenBounds(widget
);
78 ui_controls::SendMouseMoveNotifyWhenDone(
79 bounds
.x() + bounds
.width() / 2,
80 bounds
.y() + bounds
.height() / 2,
81 base::Bind(&internal::ClickTask
, button
, state
, task
));
84 } // namespace ui_test_utils