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_x11_drawable_get_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;
97 GdkWindowObject *priv;
105 g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
106 g_return_val_if_fail (window != NULL, FALSE);
107 if (!GDK_WINDOW_IS_MAPPED (window))
110 screen = gdk_window_get_screen (window);
111 priv = (GdkWindowObject *)window;
116 y = priv->height / 2;
119 /* Convert to impl coordinates */
123 xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
124 xev.display = GDK_DRAWABLE_XDISPLAY (window);
125 xev.window = GDK_WINDOW_XID (window);
126 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
133 xev.state = modifiers;
135 success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_window_get_display (window)), keyval, &keys, &n_keys);
136 success &= n_keys > 0;
140 for (i = 0; i < n_keys; i++)
141 if (keys[i].group == 0 && keys[i].level == 0)
143 xev.keycode = keys[i].keycode;
146 if (i >= n_keys) /* no match for group==0 and level==0 */
147 xev.keycode = keys[0].keycode;
152 gdk_error_trap_push ();
153 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
154 xev.x, xev.y, &xev.x_root, &xev.y_root,
157 xev.subwindow = xev.window;
158 success &= xev.same_screen;
159 if (x >= 0 && y >= 0)
160 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
161 success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
162 XSync (xev.display, False);
163 success &= 0 == gdk_error_trap_pop();
168 * gdk_test_simulate_button
169 * @window: a #GdkWindow to simulate a button event for.
170 * @x: x coordinate within @window for the button event.
171 * @y: y coordinate within @window for the button event.
172 * @button: Number of the pointer button for the event, usually 1, 2 or 3.
173 * @modifiers: Keyboard modifiers the event is setup with.
174 * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
176 * This function is intended to be used in GTK+ test programs.
177 * It will warp the mouse pointer to the given (@x,@y) corrdinates
178 * within @window and simulate a button press or release event.
179 * Because the mouse pointer needs to be warped to the target
180 * location, use of this function outside of test programs that
181 * run in their own virtual windowing system (e.g. Xvfb) is not
184 * Also, gtk_test_simulate_button() is a fairly low level function,
185 * for most testing purposes, gtk_test_widget_click() is the right
186 * function to call which will generate a button press event followed
187 * by its accompanying button release event.
189 * Returns: whether all actions neccessary for a button event simulation
190 * were carried out successfully.
195 gdk_test_simulate_button (GdkWindow *window,
198 guint button, /*1..3*/
199 GdkModifierType modifiers,
200 GdkEventType button_pressrelease)
209 GdkWindowObject *priv;
211 g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
212 g_return_val_if_fail (window != NULL, FALSE);
214 if (!GDK_WINDOW_IS_MAPPED (window))
217 screen = gdk_window_get_screen (window);
218 priv = (GdkWindowObject *)window;
223 y = priv->height / 2;
226 /* Convert to impl coordinates */
230 xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
231 xev.display = GDK_DRAWABLE_XDISPLAY (window);
232 xev.window = GDK_WINDOW_XID (window);
233 xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
240 xev.state = modifiers;
242 gdk_error_trap_push ();
243 xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
244 xev.x, xev.y, &xev.x_root, &xev.y_root,
247 xev.subwindow = xev.window;
248 success = xev.same_screen;
249 success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
250 success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
251 XSync (xev.display, False);
252 success &= 0 == gdk_error_trap_pop();