OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/automation/ui_controls.h" | 5 #include "chrome/browser/automation/ui_controls.h" |
6 | 6 |
7 #include <gdk/gdkkeysyms.h> | 7 #include <gdk/gdkkeysyms.h> |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "chrome/browser/automation/ui_controls_internal.h" | 12 #include "chrome/browser/automation/ui_controls_internal.h" |
13 #include "chrome/browser/ui/gtk/gtk_util.h" | 13 #include "chrome/browser/ui/gtk/gtk_util.h" |
14 #include "chrome/common/automation_constants.h" | 14 #include "chrome/common/automation_constants.h" |
15 #include "ui/base/gtk/event_synthesis_gtk.h" | 15 #include "ui/base/gtk/event_synthesis_gtk.h" |
16 #include "ui/gfx/rect.h" | 16 #include "ui/gfx/rect.h" |
17 | 17 |
18 #if defined(TOOLKIT_VIEWS) | 18 #if defined(TOOLKIT_VIEWS) |
19 #include "views/view.h" | 19 #include "views/view.h" |
20 #include "views/widget/widget.h" | 20 #include "views/widget/widget.h" |
21 #endif | 21 #endif |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
| 25 // static |
| 26 guint32 XTimeNow() { |
| 27 struct timespec ts; |
| 28 clock_gettime(CLOCK_MONOTONIC, &ts); |
| 29 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000; |
| 30 } |
| 31 |
25 class EventWaiter : public MessageLoopForUI::Observer { | 32 class EventWaiter : public MessageLoopForUI::Observer { |
26 public: | 33 public: |
27 EventWaiter(Task* task, GdkEventType type, int count) | 34 EventWaiter(Task* task, GdkEventType type, int count) |
28 : task_(task), | 35 : task_(task), |
29 type_(type), | 36 type_(type), |
30 count_(count) { | 37 count_(count) { |
31 MessageLoopForUI::current()->AddObserver(this); | 38 MessageLoopForUI::current()->AddObserver(this); |
32 } | 39 } |
33 | 40 |
34 virtual ~EventWaiter() { | 41 virtual ~EventWaiter() { |
(...skipping 24 matching lines...) Expand all Loading... |
59 Task* task_; | 66 Task* task_; |
60 GdkEventType type_; | 67 GdkEventType type_; |
61 // The number of events of this type to wait for. | 68 // The number of events of this type to wait for. |
62 int count_; | 69 int count_; |
63 }; | 70 }; |
64 | 71 |
65 void FakeAMouseMotionEvent(gint x, gint y) { | 72 void FakeAMouseMotionEvent(gint x, gint y) { |
66 GdkEvent* event = gdk_event_new(GDK_MOTION_NOTIFY); | 73 GdkEvent* event = gdk_event_new(GDK_MOTION_NOTIFY); |
67 | 74 |
68 event->motion.send_event = false; | 75 event->motion.send_event = false; |
69 event->motion.time = gtk_util::XTimeNow(); | 76 event->motion.time = XTimeNow(); |
70 | 77 |
71 GtkWidget* grab_widget = gtk_grab_get_current(); | 78 GtkWidget* grab_widget = gtk_grab_get_current(); |
72 if (grab_widget) { | 79 if (grab_widget) { |
73 // If there is a grab, we need to target all events at it regardless of | 80 // If there is a grab, we need to target all events at it regardless of |
74 // what widget the mouse is over. | 81 // what widget the mouse is over. |
75 event->motion.window = grab_widget->window; | 82 event->motion.window = grab_widget->window; |
76 } else { | 83 } else { |
77 event->motion.window = gdk_window_at_pointer(&x, &y); | 84 event->motion.window = gdk_window_at_pointer(&x, &y); |
78 } | 85 } |
79 g_object_ref(event->motion.window); | 86 g_object_ref(event->motion.window); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 bool SendMouseMoveNotifyWhenDone(long x, long y, Task* task) { | 178 bool SendMouseMoveNotifyWhenDone(long x, long y, Task* task) { |
172 bool rv = SendMouseMove(x, y); | 179 bool rv = SendMouseMove(x, y); |
173 new EventWaiter(task, GDK_MOTION_NOTIFY, 1); | 180 new EventWaiter(task, GDK_MOTION_NOTIFY, 1); |
174 return rv; | 181 return rv; |
175 } | 182 } |
176 | 183 |
177 bool SendMouseEvents(MouseButton type, int state) { | 184 bool SendMouseEvents(MouseButton type, int state) { |
178 GdkEvent* event = gdk_event_new(GDK_BUTTON_PRESS); | 185 GdkEvent* event = gdk_event_new(GDK_BUTTON_PRESS); |
179 | 186 |
180 event->button.send_event = false; | 187 event->button.send_event = false; |
181 event->button.time = gtk_util::XTimeNow(); | 188 event->button.time = XTimeNow(); |
182 | 189 |
183 gint x, y; | 190 gint x, y; |
184 GtkWidget* grab_widget = gtk_grab_get_current(); | 191 GtkWidget* grab_widget = gtk_grab_get_current(); |
185 if (grab_widget) { | 192 if (grab_widget) { |
186 // If there is a grab, we need to target all events at it regardless of | 193 // If there is a grab, we need to target all events at it regardless of |
187 // what widget the mouse is over. | 194 // what widget the mouse is over. |
188 event->button.window = grab_widget->window; | 195 event->button.window = grab_widget->window; |
189 gdk_window_get_pointer(event->button.window, &x, &y, NULL); | 196 gdk_window_get_pointer(event->button.window, &x, &y, NULL); |
190 } else { | 197 } else { |
191 event->button.window = gdk_window_at_pointer(&x, &y); | 198 event->button.window = gdk_window_at_pointer(&x, &y); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 new EventWaiter(task, wait_type, 1); | 247 new EventWaiter(task, wait_type, 1); |
241 return rv; | 248 return rv; |
242 } | 249 } |
243 | 250 |
244 bool SendMouseClick(MouseButton type) { | 251 bool SendMouseClick(MouseButton type) { |
245 return SendMouseEvents(type, UP | DOWN); | 252 return SendMouseEvents(type, UP | DOWN); |
246 } | 253 } |
247 | 254 |
248 #if defined(TOOLKIT_VIEWS) | 255 #if defined(TOOLKIT_VIEWS) |
249 | 256 |
250 #if defined(OS_LINUX) | 257 #if defined(OS_LINUX) && !defined(USE_AURA) |
251 void OnConfigure(GtkWidget* gtk_widget, GdkEvent* event, gpointer data) { | 258 void OnConfigure(GtkWidget* gtk_widget, GdkEvent* event, gpointer data) { |
252 views::Widget* widget = static_cast<views::Widget*>(data); | 259 views::Widget* widget = static_cast<views::Widget*>(data); |
253 gfx::Rect actual = widget->GetWindowScreenBounds(); | 260 gfx::Rect actual = widget->GetWindowScreenBounds(); |
254 gfx::Rect desired = widget->GetRootView()->bounds(); | 261 gfx::Rect desired = widget->GetRootView()->bounds(); |
255 if (actual.size() == desired.size()) | 262 if (actual.size() == desired.size()) |
256 MessageLoop::current()->Quit(); | 263 MessageLoop::current()->Quit(); |
257 } | 264 } |
258 | 265 |
259 void SynchronizeWidgetSize(views::Widget* widget) { | 266 void SynchronizeWidgetSize(views::Widget* widget) { |
260 // If the actual window size and desired window size | 267 // If the actual window size and desired window size |
261 // are different, wait until the window is resized | 268 // are different, wait until the window is resized |
262 // to desired size. | 269 // to desired size. |
263 gfx::Rect actual = widget->GetWindowScreenBounds(); | 270 gfx::Rect actual = widget->GetWindowScreenBounds(); |
264 gfx::Rect desired = widget->GetRootView()->bounds(); | 271 gfx::Rect desired = widget->GetRootView()->bounds(); |
265 if (actual.size() != desired.size()) { | 272 if (actual.size() != desired.size()) { |
266 // Listen to configure-event that is emitted when an window gets | 273 // Listen to configure-event that is emitted when an window gets |
267 // resized. | 274 // resized. |
268 GtkWidget* gtk_widget = widget->GetNativeView(); | 275 GtkWidget* gtk_widget = widget->GetNativeView(); |
269 g_signal_connect(gtk_widget, "configure-event", | 276 g_signal_connect(gtk_widget, "configure-event", |
270 G_CALLBACK(&OnConfigure), widget); | 277 G_CALLBACK(&OnConfigure), widget); |
271 MessageLoop::current()->Run(); | 278 MessageLoop::current()->Run(); |
272 } | 279 } |
273 } | 280 } |
274 #endif | 281 #endif |
275 | 282 |
276 void MoveMouseToCenterAndPress(views::View* view, MouseButton button, | 283 void MoveMouseToCenterAndPress(views::View* view, MouseButton button, |
277 int state, Task* task) { | 284 int state, Task* task) { |
278 #if defined(OS_LINUX) | 285 #if defined(OS_LINUX) && !defined(USE_AURA) |
279 // X is asynchronous and we need to wait until the window gets | 286 // X is asynchronous and we need to wait until the window gets |
280 // resized to desired size. | 287 // resized to desired size. |
281 SynchronizeWidgetSize(view->GetWidget()); | 288 SynchronizeWidgetSize(view->GetWidget()); |
282 #endif | 289 #endif |
283 | 290 |
284 gfx::Point view_center(view->width() / 2, view->height() / 2); | 291 gfx::Point view_center(view->width() / 2, view->height() / 2); |
285 views::View::ConvertPointToScreen(view, &view_center); | 292 views::View::ConvertPointToScreen(view, &view_center); |
286 SendMouseMoveNotifyWhenDone(view_center.x(), view_center.y(), | 293 SendMouseMoveNotifyWhenDone(view_center.x(), view_center.y(), |
287 new ClickTask(button, state, task)); | 294 new ClickTask(button, state, task)); |
288 } | 295 } |
289 #else | 296 #else |
290 void MoveMouseToCenterAndPress(GtkWidget* widget, | 297 void MoveMouseToCenterAndPress(GtkWidget* widget, |
291 MouseButton button, | 298 MouseButton button, |
292 int state, | 299 int state, |
293 Task* task) { | 300 Task* task) { |
294 gfx::Rect bounds = gtk_util::GetWidgetScreenBounds(widget); | 301 gfx::Rect bounds = gtk_util::GetWidgetScreenBounds(widget); |
295 SendMouseMoveNotifyWhenDone(bounds.x() + bounds.width() / 2, | 302 SendMouseMoveNotifyWhenDone(bounds.x() + bounds.width() / 2, |
296 bounds.y() + bounds.height() / 2, | 303 bounds.y() + bounds.height() / 2, |
297 new ClickTask(button, state, task)); | 304 new ClickTask(button, state, task)); |
298 } | 305 } |
299 #endif | 306 #endif |
300 | 307 |
301 } // namespace ui_controls | 308 } // namespace ui_controls |
OLD | NEW |