1 /* Gtk+ testing utilities
2 * Copyright (C) 2007 Imendio AB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gdktestutils.h"
25 #include "gdkkeysyms.h"
31 * gdk_test_render_sync:
32 * @window: a mapped #GdkWindow
34 * This function retrieves a pixel from @window to force the windowing
35 * system to carry out any pending rendering commands.
36 * This function is intended to be used to syncronize with rendering
37 * pipelines, to benchmark windowing system rendering operations.
42 gdk_test_render_sync (GdkWindow *window)
44 Display *display = GDK_WINDOW_XDISPLAY (window);
47 /* syncronize to X drawing queue, see:
48 * http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html
50 ximage = XGetImage (display, DefaultRootWindow (display),
51 0, 0, 1, 1, AllPlanes, ZPixmap);
53 XDestroyImage (ximage);
57 * gdk_test_simulate_key
58 * @window: a #GdkWindow to simulate a key event for.
59 * @x: x coordinate within @window for the key event.
60 * @y: y coordinate within @window for the key event.
61 * @keyval: A GDK keyboard value.
62 * @modifiers: Keyboard modifiers the event is setup with.
63 * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
65 * This function is intended to be used in GTK+ test programs.
66 * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
67 * the given (@x,@y) corrdinates within @window and simulate a
68 * key press or release event.
70 * When the mouse pointer is warped to the target location, use
71 * of this function outside of test programs that run in their
72 * own virtual windowing system (e.g. Xvfb) is not recommended.
73 * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
74 * be warped and @window origin will be used as mouse pointer
75 * location for the event.
77 * Also, gtk_test_simulate_key() is a fairly low level function,
78 * for most testing purposes, gtk_test_widget_send_key() is the
79 * right function to call which will generate a key press event
80 * followed by its accompanying key release event.
82 * Returns: whether all actions neccessary for a key event simulation
83 * were carried out successfully.
88 gdk_test_simulate_key (GdkWindow *window,
92 GdkModifierType modifiers,
93 GdkEventType key_pressrelease)
96 GdkKeymapKey *keys = NULL;
104 g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
105 g_return_val_if_fail (window != NULL, FALSE);
106 if (!GDK_WINDOW_IS_MAPPED (window))
109 screen = gdk_window_get_screen (window);
113 x = window->width / 2;
114 y = window->height / 2;
117 /* Convert to impl coordinates */
118 x = x + window->abs_x;
119 y = y + window->abs_y;
121 xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
122 xev.display = GDK_WINDOW_XDISPLAY (window);
123 xev.window = GDK_WINDOW_XID (window);
124 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
131 xev.state = modifiers;
133 success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_window_get_display (window)), keyval, &keys, &n_keys);
134 success &= n_keys > 0;
138 for (i = 0; i < n_keys; i++)
139 if (keys[i].group == 0 && keys[i].level == 0)
141 xev.keycode = keys[i].keycode;
144 if (i >= n_keys) /* no match for group==0 and level==0 */
145 xev.keycode = keys[0].keycode;
150 gdk_error_trap_push ();
151 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
152 xev.x, xev.y, &xev.x_root, &xev.y_root,
155 xev.subwindow = xev.window;
156 success &= xev.same_screen;
157 if (x >= 0 && y >= 0)
158 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
159 success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
160 XSync (xev.display, False);
161 success &= 0 == gdk_error_trap_pop();
166 * gdk_test_simulate_button
167 * @window: a #GdkWindow to simulate a button event for.
168 * @x: x coordinate within @window for the button event.
169 * @y: y coordinate within @window for the button event.
170 * @button: Number of the pointer button for the event, usually 1, 2 or 3.
171 * @modifiers: Keyboard modifiers the event is setup with.
172 * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
174 * This function is intended to be used in GTK+ test programs.
175 * It will warp the mouse pointer to the given (@x,@y) corrdinates
176 * within @window and simulate a button press or release event.
177 * Because the mouse pointer needs to be warped to the target
178 * location, use of this function outside of test programs that
179 * run in their own virtual windowing system (e.g. Xvfb) is not
182 * Also, gtk_test_simulate_button() is a fairly low level function,
183 * for most testing purposes, gtk_test_widget_click() is the right
184 * function to call which will generate a button press event followed
185 * by its accompanying button release event.
187 * Returns: whether all actions neccessary for a button event simulation
188 * were carried out successfully.
193 gdk_test_simulate_button (GdkWindow *window,
196 guint button, /*1..3*/
197 GdkModifierType modifiers,
198 GdkEventType button_pressrelease)
208 g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
209 g_return_val_if_fail (window != NULL, FALSE);
211 if (!GDK_WINDOW_IS_MAPPED (window))
214 screen = gdk_window_get_screen (window);
218 x = window->width / 2;
219 y = window->height / 2;
222 /* Convert to impl coordinates */
223 x = x + window->abs_x;
224 y = y + window->abs_y;
226 xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
227 xev.display = GDK_WINDOW_XDISPLAY (window);
228 xev.window = GDK_WINDOW_XID (window);
229 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
236 xev.state = modifiers;
238 gdk_error_trap_push ();
239 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
240 xev.x, xev.y, &xev.x_root, &xev.y_root,
243 xev.subwindow = xev.window;
244 success = xev.same_screen;
245 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
246 success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
247 XSync (xev.display, False);
248 success &= 0 == gdk_error_trap_pop();