- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / base / interactive_test_utils_gtk.cc
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 <gtk/gtk.h>
8
9 #include "base/bind.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"
17
18 namespace ui_test_utils {
19
20 namespace {
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;
24
25   while (iter) {
26     if (iter == target)
27       return true;
28
29     if (!GTK_IS_CONTAINER(iter))
30       return false;
31
32     iter = GTK_CONTAINER(iter)->focus_child;
33   }
34
35   return false;
36 }
37 }  // namespace
38
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();
43   DCHECK(window);
44   GtkWidget* widget = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid);
45   DCHECK(widget);
46   return IsWidgetInFocusChain(GTK_WIDGET(window), widget);
47 }
48
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();
53   DCHECK(window);
54   GtkWidget* view = ViewIDUtil::GetWidget(GTK_WIDGET(window), vid);
55   DCHECK(view);
56   MoveMouseToCenterAndPress(view,
57                             ui_controls::LEFT,
58                             ui_controls::DOWN | ui_controls::UP,
59                             base::MessageLoop::QuitClosure());
60   content::RunMessageLoop();
61 }
62
63 void HideNativeWindow(gfx::NativeWindow window) {
64   gtk_widget_hide(GTK_WIDGET(window));
65 }
66
67 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
68   if (!gtk_window_has_toplevel_focus(GTK_WINDOW(window)))
69     gtk_window_present(GTK_WINDOW(window));
70   return true;
71 }
72
73 void MoveMouseToCenterAndPress(GtkWidget* widget,
74                                ui_controls::MouseButton button,
75                                int state,
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));
82 }
83
84 }  // namespace ui_test_utils