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.
20 #include <gdk/gdktestutils.h>
21 #include <gdk/gdkkeysyms.h>
28 * gdk_test_render_sync:
29 * @window: a mapped #GdkWindow
31 * This function retrieves a pixel from @window to force the windowing
32 * system to carry out any pending rendering commands.
33 * This function is intended to be used to syncronize with rendering
34 * pipelines, to benchmark windowing system rendering operations.
39 gdk_test_render_sync (GdkWindow *window)
41 static GdkImage *p1image = NULL;
42 /* syncronize to X drawing queue, see:
43 * http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html
45 p1image = gdk_drawable_copy_to_image (window, p1image, 0, 0, 0, 0, 1, 1);
49 * gdk_test_simulate_key
50 * @window: a #GdkWindow to simulate a key event for.
51 * @x: x coordinate within @window for the key event.
52 * @y: y coordinate within @window for the key event.
53 * @keyval: A GDK keyboard value.
54 * @modifiers: Keyboard modifiers the event is setup with.
55 * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
57 * This function is intended to be used in GTK+ test programs.
58 * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
59 * the given (@x,@y) corrdinates within @window and simulate a
60 * key press or release event.
62 * When the mouse pointer is warped to the target location, use
63 * of this function outside of test programs that run in their
64 * own virtual windowing system (e.g. Xvfb) is not recommended.
65 * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
66 * be warped and @window origin will be used as mouse pointer
67 * location for the event.
69 * Also, gtk_test_simulate_key() is a fairly low level function,
70 * for most testing purposes, gtk_test_widget_send_key() is the
71 * right function to call which will generate a key press event
72 * followed by its accompanying key release event.
74 * Returns: wether all actions neccessary for a key event simulation
75 * were carried out successfully.
80 gdk_test_simulate_key (GdkWindow *window,
84 GdkModifierType modifiers,
85 GdkEventType key_pressrelease)
88 GdkKeymapKey *keys = NULL;
96 g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
97 g_return_val_if_fail (window != NULL, FALSE);
98 if (!GDK_WINDOW_IS_MAPPED (window))
100 screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
103 gdk_drawable_get_size (window, &x, &y);
107 xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
108 xev.display = GDK_DRAWABLE_XDISPLAY (window);
109 xev.window = GDK_WINDOW_XID (window);
110 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
117 xev.state = modifiers;
119 success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_drawable_get_display (window)), keyval, &keys, &n_keys);
120 success &= n_keys > 0;
124 for (i = 0; i < n_keys; i++)
125 if (keys[i].group == 0 && keys[i].level == 0)
127 xev.keycode = keys[i].keycode;
130 if (i >= n_keys) /* no match for group==0 and level==0 */
131 xev.keycode = keys[0].keycode;
136 gdk_error_trap_push ();
137 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
138 xev.x, xev.y, &xev.x_root, &xev.y_root,
141 xev.subwindow = xev.window;
142 success &= xev.same_screen;
143 if (x >= 0 && y >= 0)
144 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
145 success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
146 XSync (xev.display, False);
147 success &= 0 == gdk_error_trap_pop();
152 * gdk_test_simulate_button
153 * @window: a #GdkWindow to simulate a button event for.
154 * @x: x coordinate within @window for the button event.
155 * @y: y coordinate within @window for the button event.
156 * @button: Number of the pointer button for the event, usually 1, 2 or 3.
157 * @modifiers: Keyboard modifiers the event is setup with.
158 * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
160 * This function is intended to be used in GTK+ test programs.
161 * It will warp the mouse pointer to the given (@x,@y) corrdinates
162 * within @window and simulate a button press or release event.
163 * Because the mouse pointer needs to be warped to the target
164 * location, use of this function outside of test programs that
165 * run in their own virtual windowing system (e.g. Xvfb) is not
168 * Also, gtk_test_simulate_button() is a fairly low level function,
169 * for most testing purposes, gtk_test_widget_click() is the right
170 * function to call which will generate a button press event followed
171 * by its accompanying button release event.
173 * Returns: wether all actions neccessary for a button event simulation
174 * were carried out successfully.
179 gdk_test_simulate_button (GdkWindow *window,
182 guint button, /*1..3*/
183 GdkModifierType modifiers,
184 GdkEventType button_pressrelease)
194 g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
195 g_return_val_if_fail (window != NULL, FALSE);
197 if (!GDK_WINDOW_IS_MAPPED (window))
199 screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
202 gdk_drawable_get_size (window, &x, &y);
206 xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
207 xev.display = GDK_DRAWABLE_XDISPLAY (window);
208 xev.window = GDK_WINDOW_XID (window);
209 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
216 xev.state = modifiers;
218 gdk_error_trap_push ();
219 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
220 xev.x, xev.y, &xev.x_root, &xev.y_root,
223 xev.subwindow = xev.window;
224 success = xev.same_screen;
225 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
226 success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
227 XSync (xev.display, False);
228 success &= 0 == gdk_error_trap_pop();
232 #define __GDK_TEST_UTILS_X11_C__
233 #include "gdkaliasdef.c"