1 /* Gtk+ testing utilities
2 * Copyright (C) 2007 Tim Janik
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 #include <gdk/gdktestutils.h>
20 #include <gdk/gdkkeysyms.h>
27 * gdk_test_render_sync
28 * @window: a mapped GdkWindow
30 * This function retrives 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.
36 gdk_test_render_sync (GdkWindow *window)
38 static GdkImage *p1image = NULL;
39 /* syncronize to X drawing queue, see:
40 * http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html
42 p1image = gdk_drawable_copy_to_image (window, p1image, 0, 0, 0, 0, 1, 1);
46 * gdk_test_simulate_key
47 * @window: Gdk window to simulate a key event for.
48 * @x: x coordinate within @window for the key event.
49 * @y: y coordinate within @window for the key event.
50 * @keyval: A Gdk keyboard value.
51 * @modifiers: Keyboard modifiers the event is setup with.
52 * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
54 * This function is intended to be used in Gtk+ test programs.
55 * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
56 * the given (@x,@y) corrdinates within @window and simulate a
57 * key press or release event.
58 * When the mouse pointer is warped to the target location, use
59 * of this function outside of test programs that run in their
60 * own virtual windowing system (e.g. Xvfb) is not recommended.
61 * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
62 * be warped and @window origin will be used as mouse pointer
63 * location for the event.
64 * Also, gtk_test_simulate_key() is a fairly low level function,
65 * for most testing purposes, gtk_test_widget_send_key() is the
66 * right function to call which will generate a key press event
67 * followed by its accompanying key release event.
69 * Returns: wether all actions neccessary for a key event simulation were carried out successfully.
72 gdk_test_simulate_key (GdkWindow *window,
76 GdkModifierType modifiers,
77 GdkEventType key_pressrelease)
80 GdkKeymapKey *keys = NULL;
88 g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
89 g_return_val_if_fail (window != NULL, FALSE);
90 if (!GDK_WINDOW_IS_MAPPED (window))
92 screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
95 gdk_drawable_get_size (window, &x, &y);
99 xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
100 xev.display = GDK_DRAWABLE_XDISPLAY (window);
101 xev.window = GDK_WINDOW_XID (window);
102 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
109 xev.state = modifiers;
111 success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_drawable_get_display (window)), keyval, &keys, &n_keys);
112 success &= n_keys > 0;
116 for (i = 0; i < n_keys; i++)
117 if (keys[i].group == 0 && keys[i].level == 0)
119 xev.keycode = keys[i].keycode;
122 if (i >= n_keys) /* no match for group==0 and level==0 */
123 xev.keycode = keys[0].keycode;
128 gdk_error_trap_push ();
129 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
130 xev.x, xev.y, &xev.x_root, &xev.y_root,
133 xev.subwindow = xev.window;
134 success &= xev.same_screen;
135 if (x >= 0 && y >= 0)
136 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
137 success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
138 XSync (xev.display, False);
139 success &= 0 == gdk_error_trap_pop();
144 * gdk_test_simulate_button
145 * @window: Gdk window to simulate a button event for.
146 * @x: x coordinate within @window for the button event.
147 * @y: y coordinate within @window for the button event.
148 * @button: Number of the pointer button for the event, usually 1, 2 or 3.
149 * @modifiers: Keyboard modifiers the event is setup with.
150 * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
152 * This function is intended to be used in Gtk+ test programs.
153 * It will warp the mouse pointer to the given (@x,@y) corrdinates
154 * within @window and simulate a button press or release event.
155 * Because the mouse pointer needs to be warped to the target
156 * location, use of this function outside of test programs that
157 * run in their own virtual windowing system (e.g. Xvfb) is not
159 * Also, gtk_test_simulate_button() is a fairly low level function,
160 * for most testing purposes, gtk_test_widget_click() is the right
161 * function to call which will generate a button press event followed
162 * by its accompanying button release event.
164 * Returns: wether all actions neccessary for a button event simulation were carried out successfully.
167 gdk_test_simulate_button (GdkWindow *window,
170 guint button, /*1..3*/
171 GdkModifierType modifiers,
172 GdkEventType button_pressrelease)
180 g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
181 g_return_val_if_fail (window != NULL, FALSE);
182 if (!GDK_WINDOW_IS_MAPPED (window))
184 screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
187 gdk_drawable_get_size (window, &x, &y);
191 xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
192 xev.display = GDK_DRAWABLE_XDISPLAY (window);
193 xev.window = GDK_WINDOW_XID (window);
194 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
201 xev.state = modifiers;
203 gdk_error_trap_push ();
204 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
205 xev.x, xev.y, &xev.x_root, &xev.y_root,
208 xev.subwindow = xev.window;
209 gboolean success = xev.same_screen;
210 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
211 success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
212 XSync (xev.display, False);
213 success &= 0 == gdk_error_trap_pop();
217 #define __GDK_TEST_UTILS_X11_C__
218 #include "gdkaliasdef.c"