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>
27 * gdk_test_render_sync:
28 * @window: a mapped #GdkWindow
30 * This function retrieves a pixel from @window to force the windowing
31 * system to carry out any pending rendering commands.
32 * This function is intended to be used to syncronize with rendering
33 * pipelines, to benchmark windowing system rendering operations.
38 gdk_test_render_sync (GdkWindow *window)
40 Display *display = gdk_x11_drawable_get_xdisplay (window);
43 /* syncronize to X drawing queue, see:
44 * http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html
46 ximage = XGetImage (display, DefaultRootWindow (display),
47 0, 0, 1, 1, AllPlanes, ZPixmap);
49 XDestroyImage (ximage);
53 * gdk_test_simulate_key
54 * @window: a #GdkWindow to simulate a key event for.
55 * @x: x coordinate within @window for the key event.
56 * @y: y coordinate within @window for the key event.
57 * @keyval: A GDK keyboard value.
58 * @modifiers: Keyboard modifiers the event is setup with.
59 * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
61 * This function is intended to be used in GTK+ test programs.
62 * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
63 * the given (@x,@y) corrdinates within @window and simulate a
64 * key press or release event.
66 * When the mouse pointer is warped to the target location, use
67 * of this function outside of test programs that run in their
68 * own virtual windowing system (e.g. Xvfb) is not recommended.
69 * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
70 * be warped and @window origin will be used as mouse pointer
71 * location for the event.
73 * Also, gtk_test_simulate_key() is a fairly low level function,
74 * for most testing purposes, gtk_test_widget_send_key() is the
75 * right function to call which will generate a key press event
76 * followed by its accompanying key release event.
78 * Returns: whether all actions neccessary for a key event simulation
79 * were carried out successfully.
84 gdk_test_simulate_key (GdkWindow *window,
88 GdkModifierType modifiers,
89 GdkEventType key_pressrelease)
92 GdkKeymapKey *keys = NULL;
93 GdkWindowObject *priv;
101 g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
102 g_return_val_if_fail (window != NULL, FALSE);
103 if (!GDK_WINDOW_IS_MAPPED (window))
105 screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
108 gdk_drawable_get_size (window, &x, &y);
113 priv = (GdkWindowObject *)window;
114 /* Convert to impl coordinates */
118 xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
119 xev.display = GDK_DRAWABLE_XDISPLAY (window);
120 xev.window = GDK_WINDOW_XID (window);
121 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
128 xev.state = modifiers;
130 success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_drawable_get_display (window)), keyval, &keys, &n_keys);
131 success &= n_keys > 0;
135 for (i = 0; i < n_keys; i++)
136 if (keys[i].group == 0 && keys[i].level == 0)
138 xev.keycode = keys[i].keycode;
141 if (i >= n_keys) /* no match for group==0 and level==0 */
142 xev.keycode = keys[0].keycode;
147 gdk_error_trap_push ();
148 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
149 xev.x, xev.y, &xev.x_root, &xev.y_root,
152 xev.subwindow = xev.window;
153 success &= xev.same_screen;
154 if (x >= 0 && y >= 0)
155 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
156 success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
157 XSync (xev.display, False);
158 success &= 0 == gdk_error_trap_pop();
163 * gdk_test_simulate_button
164 * @window: a #GdkWindow to simulate a button event for.
165 * @x: x coordinate within @window for the button event.
166 * @y: y coordinate within @window for the button event.
167 * @button: Number of the pointer button for the event, usually 1, 2 or 3.
168 * @modifiers: Keyboard modifiers the event is setup with.
169 * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
171 * This function is intended to be used in GTK+ test programs.
172 * It will warp the mouse pointer to the given (@x,@y) corrdinates
173 * within @window and simulate a button press or release event.
174 * Because the mouse pointer needs to be warped to the target
175 * location, use of this function outside of test programs that
176 * run in their own virtual windowing system (e.g. Xvfb) is not
179 * Also, gtk_test_simulate_button() is a fairly low level function,
180 * for most testing purposes, gtk_test_widget_click() is the right
181 * function to call which will generate a button press event followed
182 * by its accompanying button release event.
184 * Returns: whether all actions neccessary for a button event simulation
185 * were carried out successfully.
190 gdk_test_simulate_button (GdkWindow *window,
193 guint button, /*1..3*/
194 GdkModifierType modifiers,
195 GdkEventType button_pressrelease)
204 GdkWindowObject *priv;
206 g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
207 g_return_val_if_fail (window != NULL, FALSE);
209 if (!GDK_WINDOW_IS_MAPPED (window))
211 screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
214 gdk_drawable_get_size (window, &x, &y);
219 priv = (GdkWindowObject *)window;
220 /* Convert to impl coordinates */
224 xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
225 xev.display = GDK_DRAWABLE_XDISPLAY (window);
226 xev.window = GDK_WINDOW_XID (window);
227 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
234 xev.state = modifiers;
236 gdk_error_trap_push ();
237 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
238 xev.x, xev.y, &xev.x_root, &xev.y_root,
241 xev.subwindow = xev.window;
242 success = xev.same_screen;
243 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
244 success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
245 XSync (xev.display, False);
246 success &= 0 == gdk_error_trap_pop();