]> Pileus Git - ~andy/gtk/blob - gtk/gtktestutils.c
b5daba6d28b7c6c6150445649017d9acac951305
[~andy/gtk] / gtk / gtktestutils.c
1 /* Gtk+ testing utilities
2  * Copyright (C) 2007 Tim Janik
3  *
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.
8  *
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.
13  *
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.
18  */
19 #include "gtktestutils.h"
20 #include <gtk/gtk.h>
21 #include <gdk/gdkkeysyms.h>
22 #include <x11/gdkx.h>
23 #include "gtkalias.h"
24
25 #include <X11/Xlib.h>
26 #include <locale.h>
27 #include <string.h>
28 #include <math.h>
29
30
31 /* --- test utilities --- */
32 /**
33  * gtk_test_init:
34  * @argc: Address of the <parameter>argc</parameter> parameter of the
35  *        main() function. Changed if any arguments were handled.
36  * @argv: Address of the <parameter>argv</parameter> parameter of main().
37  *        Any parameters understood by g_test_init() or gtk_init() are
38  *        stripped before return.
39  *
40  * This function is used to initialize a Gtk+ test program.
41  * It will in turn call g_test_init() and gtk_init() to properly
42  * initialize the testing framework and graphical toolkit.
43  * It'll also set the program's locale to "C" and prevent loading of
44  * rc files and Gtk+ modules. This is done to make tets program
45  * environments as deterministic as possible.
46  * Like gtk_init() and g_test_init(), any known arguments will be
47  * processed and stripped from @argc and @argv.
48  **/
49 void
50 gtk_test_init (int    *argcp,
51                char ***argvp,
52                ...)
53 {
54   /* - enter C locale
55    * - call g_test_init();
56    * - call gtk_init();
57    * - prevent RC files from loading;
58    * - prevent Gtk modules from loading;
59    * - supply mock object for GtkSettings
60    * FUTURE TODO:
61    * - this function could install a mock object around GtkSettings
62    */
63   // FIXME: g_test_init (argcp, argvp, NULL);
64   g_setenv ("GTK_MODULES", "", TRUE);
65   g_setenv ("GTK2_RC_FILES", "/dev/null", TRUE);
66   gtk_disable_setlocale();
67   setlocale (LC_ALL, "C");
68   gtk_init (argcp, argvp);
69 }
70
71 static GSList*
72 test_find_widget_input_windows (GtkWidget *widget,
73                                 gboolean   input_only)
74 {
75   GList *node, *children;
76   GSList *matches = NULL;
77   gpointer udata;
78   gdk_window_get_user_data (widget->window, &udata);
79   if (udata == widget && (!input_only || (GDK_IS_WINDOW (widget->window) && GDK_WINDOW_OBJECT (widget->window)->input_only)))
80     matches = g_slist_prepend (matches, widget->window);
81   children = gdk_window_get_children (gtk_widget_get_parent_window (widget));
82   for (node = children; node; node = node->next)
83     {
84       gdk_window_get_user_data (node->data, &udata);
85       if (udata == widget && (!input_only || (GDK_IS_WINDOW (node->data) && GDK_WINDOW_OBJECT (node->data)->input_only)))
86         matches = g_slist_prepend (matches, node->data);
87     }
88   return g_slist_reverse (matches);
89 }
90
91 /**
92  * gtk_test_simulate_key
93  * @window: Gdk window to simulate a key event for.
94  * @x:      x coordinate within @window for the key event.
95  * @y:      y coordinate within @window for the key event.
96  * @keyval: A Gdk keyboard value.
97  * @modifiers: Keyboard modifiers the event is setup with.
98  * @press_or_release: %TRUE to generate key press events, %FALSE to generate key release events.
99  *
100  * This function is intended to be used in Gtk+ test programs.
101  * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
102  * the given (@x,@y) corrdinates within @window and simulate a
103  * key press or release event.
104  * When the mouse pointer is warped to the target location, use
105  * of this function outside of test programs that run in their
106  * own virtual windowing system (e.g. Xvfb) is not recommended.
107  * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
108  * be warped and @window origin will be used as mouse pointer
109  * location for the event.
110  * Also, gtk_test_simulate_key() is a fairly low level function,
111  * for most testing purposes, gtk_test_widget_send_key() is the
112  * right function to call which will generate a key press event
113  * followed by its accompanying key release event.
114  *
115  * Returns: wether all actions neccessary for a key event simulation were carried out successfully.
116  **/
117 gboolean
118 gtk_test_simulate_key (GdkWindow      *window,
119                        gint            x,
120                        gint            y,
121                        guint           keyval,
122                        GdkModifierType modifiers,
123                        gboolean        press_or_release)
124 {
125   GdkScreen *screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
126   GdkKeymapKey *keys = NULL;
127   gboolean success;
128   gint n_keys = 0;
129   XKeyEvent xev = {
130     0,  /* type */
131     0,  /* serial */
132     1,  /* send_event */
133   };
134   if (x < 0 && y < 0)
135     {
136       gdk_drawable_get_size (window, &x, &y);
137       x /= 2;
138       y /= 2;
139     }
140   xev.type = press_or_release ? KeyPress : KeyRelease;
141   xev.display = GDK_DRAWABLE_XDISPLAY (window);
142   xev.window = GDK_WINDOW_XID (window);
143   xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
144   xev.subwindow = 0;
145   xev.time = 0;
146   xev.x = MAX (x, 0);
147   xev.y = MAX (y, 0);
148   xev.x_root = 0;
149   xev.y_root = 0;
150   xev.state = modifiers;
151   xev.keycode = 0;
152   success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_drawable_get_display (window)), keyval, &keys, &n_keys);
153   success &= n_keys > 0;
154   if (success)
155     {
156       gint i;
157       for (i = 0; i < n_keys; i++)
158         if (keys[i].group == 0 && keys[i].level == 0)
159           {
160             xev.keycode = keys[i].keycode;
161             break;
162           }
163       if (i >= n_keys) /* no match for group==0 and level==0 */
164         xev.keycode = keys[0].keycode;
165     }
166   g_free (keys);
167   if (!success)
168     return FALSE;
169   gdk_error_trap_push ();
170   xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
171                                            xev.x, xev.y, &xev.x_root, &xev.y_root,
172                                            &xev.subwindow);
173   if (!xev.subwindow)
174     xev.subwindow = xev.window;
175   success &= xev.same_screen;
176   if (x >= 0 && y >= 0)
177     success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
178   success &= 0 != XSendEvent (xev.display, xev.window, True, press_or_release ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
179   XSync (xev.display, False);
180   success &= 0 == gdk_error_trap_pop();
181   return success;
182 }
183
184 /**
185  * gtk_test_widget_send_key
186  * @widget: Widget to generate a key press and release on.
187  * @keyval: A Gdk keyboard value.
188  * @modifiers: Keyboard modifiers the event is setup with.
189  *
190  * This function will generate keyboard press and release events in
191  * the middle of the first GdkWindow found that belongs to @widget.
192  * For %GTK_NO_WINDOW widgets like GtkButton, this will often be an
193  * input-only event window. For other widgets, this is usually widget->window.
194  * Certain caveats should be considered when using this function, in
195  * particular because the mouse pointer is warped to the key press
196  * location, see gtk_test_simulate_key() for details.
197  *
198  * Returns: wether all actions neccessary for the key event simulation were carried out successfully.
199  **/
200 gboolean
201 gtk_test_widget_send_key (GtkWidget      *widget,
202                           guint           keyval,
203                           GdkModifierType modifiers)
204 {
205   gboolean k1res, k2res;
206   GSList *iwindows = test_find_widget_input_windows (widget, FALSE);
207   if (!iwindows)
208     iwindows = test_find_widget_input_windows (widget, TRUE);
209   if (!iwindows)
210     return FALSE;
211   k1res = gtk_test_simulate_key (iwindows->data, -1, -1, keyval, modifiers, TRUE);
212   k2res = gtk_test_simulate_key (iwindows->data, -1, -1, keyval, modifiers, FALSE);
213   g_slist_free (iwindows);
214   return k1res && k2res;
215 }
216
217 /**
218  * gtk_test_simulate_button
219  * @window: Gdk window to simulate a button event for.
220  * @x:      x coordinate within @window for the button event.
221  * @y:      y coordinate within @window for the button event.
222  * @button: Number of the pointer button for the event, usually 1, 2 or 3.
223  * @modifiers: Keyboard modifiers the event is setup with.
224  * @press_or_release: %TRUE to generate button press events, %FALSE to generate button release events.
225  *
226  * This function is intended to be used in Gtk+ test programs.
227  * It will warp the mouse pointer to the given (@x,@y) corrdinates
228  * within @window and simulate a button press or release event.
229  * Because the mouse pointer needs to be warped to the target
230  * location, use of this function outside of test programs that
231  * run in their own virtual windowing system (e.g. Xvfb) is not
232  * recommended.
233  * Also, gtk_test_simulate_button() is a fairly low level function,
234  * for most testing purposes, gtk_test_widget_click() is the right
235  * function to call which will generate a button press event followed
236  * by its accompanying button release event.
237  *
238  * Returns: wether all actions neccessary for a button event simulation were carried out successfully.
239  **/
240 gboolean
241 gtk_test_simulate_button (GdkWindow      *window,
242                           gint            x,
243                           gint            y,
244                           guint           button, /*1..3*/
245                           GdkModifierType modifiers,
246                           gboolean        press_or_release)
247 {
248   GdkScreen *screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
249   XButtonEvent xev = {
250     0,  /* type */
251     0,  /* serial */
252     1,  /* send_event */
253   };
254   if (x < 0 && y < 0)
255     {
256       gdk_drawable_get_size (window, &x, &y);
257       x /= 2;
258       y /= 2;
259     }
260   xev.type = press_or_release ? ButtonPress : ButtonRelease;
261   xev.display = GDK_DRAWABLE_XDISPLAY (window);
262   xev.window = GDK_WINDOW_XID (window);
263   xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
264   xev.subwindow = 0;
265   xev.time = 0;
266   xev.x = x;
267   xev.y = y;
268   xev.x_root = 0;
269   xev.y_root = 0;
270   xev.state = modifiers;
271   xev.button = button;
272   gdk_error_trap_push ();
273   xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
274                                            xev.x, xev.y, &xev.x_root, &xev.y_root,
275                                            &xev.subwindow);
276   if (!xev.subwindow)
277     xev.subwindow = xev.window;
278   gboolean success = xev.same_screen;
279   success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
280   success &= 0 != XSendEvent (xev.display, xev.window, True, press_or_release ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
281   XSync (xev.display, False);
282   success &= 0 == gdk_error_trap_pop();
283   return success;
284 }
285
286 /**
287  * gtk_test_widget_click
288  * @widget: Widget to generate a button click on.
289  * @button: Number of the pointer button for the event, usually 1, 2 or 3.
290  * @modifiers: Keyboard modifiers the event is setup with.
291  *
292  * This function will generate a @button click (button press and button
293  * release event) in the middle of the first GdkWindow found that belongs
294  * to @widget.
295  * For %GTK_NO_WINDOW widgets like GtkButton, this will often be an
296  * input-only event window. For other widgets, this is usually widget->window.
297  * Certain caveats should be considered when using this function, in
298  * particular because the mouse pointer is warped to the button click
299  * location, see gtk_test_simulate_button() for details.
300  *
301  * Returns: wether all actions neccessary for the button click simulation were carried out successfully.
302  **/
303 gboolean
304 gtk_test_widget_click (GtkWidget      *widget,
305                        guint           button,
306                        GdkModifierType modifiers)
307 {
308   gboolean b1res, b2res;
309   GSList *iwindows = test_find_widget_input_windows (widget, FALSE);
310   if (!iwindows)
311     iwindows = test_find_widget_input_windows (widget, TRUE);
312   if (!iwindows)
313     return FALSE;
314   b1res = gtk_test_simulate_button (iwindows->data, -1, -1, button, modifiers, TRUE);
315   b2res = gtk_test_simulate_button (iwindows->data, -1, -1, button, modifiers, FALSE);
316   g_slist_free (iwindows);
317   return b1res && b2res;
318 }
319
320 /**
321  * gtk_test_spin_button_click
322  * @spinner: valid GtkSpinButton widget.
323  * @button:  Number of the pointer button for the event, usually 1, 2 or 3.
324  * @upwards: %TRUE for upwards arrow click, %FALSE for downwards arrow click.
325  *
326  * This function will generate a @button click in the upwards or downwards
327  * spin button arrow areas, usually leading to an increase or decrease of
328  * spin button's value.
329  *
330  * Returns: wether all actions neccessary for the button click simulation were carried out successfully.
331  **/
332 gboolean
333 gtk_test_spin_button_click (GtkSpinButton  *spinner,
334                             guint           button,
335                             gboolean        upwards)
336 {
337   gboolean b1res = FALSE, b2res = FALSE;
338   if (spinner->panel)
339     {
340       gint width, height, pos;
341       gdk_drawable_get_size (spinner->panel, &width, &height);
342       pos = upwards ? 0 : height - 1;
343       b1res = gtk_test_simulate_button (spinner->panel, width - 1, pos, button, 0, TRUE);
344       b2res = gtk_test_simulate_button (spinner->panel, width - 1, pos, button, 0, FALSE);
345     }
346   return b1res && b2res;
347 }
348
349 /**
350  * gtk_test_find_label
351  * @widget:        Valid label or container widget.
352  * @label_pattern: Shell-glob pattern to match a label string.
353  *
354  * This function will search @widget and all its descendants for a GtkLabel
355  * widget with a text string matching @label_pattern.
356  * The @label_pattern may contain asterisks '*' and question marks '?' as
357  * placeholders, g_pattern_match() is used for the matching.
358  * Note that locales other than "C" tend to alter (translate" label strings,
359  * so this function is genrally only useful in test programs with
360  * predetermined locales, see gtk_test_init() for more details.
361  *
362  * Returns: a GtkLabel widget if any is found.
363  **/
364 GtkWidget*
365 gtk_test_find_label (GtkWidget    *widget,
366                      const gchar  *label_pattern)
367 {
368   if (GTK_IS_LABEL (widget))
369     {
370       const gchar *text = gtk_label_get_text (GTK_LABEL (widget));
371       if (g_pattern_match_simple (label_pattern, text))
372         return widget;
373     }
374   if (GTK_IS_CONTAINER (widget))
375     {
376       GList *node, *list = gtk_container_get_children (GTK_CONTAINER (widget));
377       for (node = list; node; node = node->next)
378         {
379           GtkWidget *label = gtk_test_find_label (node->data, label_pattern);
380           if (label)
381             return label;
382         }
383       g_list_free (list);
384     }
385   return NULL;
386 }
387
388 static GList*
389 test_list_descendants (GtkWidget *widget,
390                        GType      widget_type)
391 {
392   GList *results = NULL;
393   if (GTK_IS_CONTAINER (widget))
394     {
395       GList *node, *list = gtk_container_get_children (GTK_CONTAINER (widget));
396       for (node = list; node; node = node->next)
397         {
398           if (!widget_type || g_type_is_a (G_OBJECT_TYPE (node->data), widget_type))
399             results = g_list_prepend (results, node->data);
400           else
401             results = g_list_concat (results, test_list_descendants (node->data, widget_type));
402         }
403       g_list_free (list);
404     }
405   return results;
406 }
407
408 static int
409 widget_geo_dist (GtkWidget *a,
410                  GtkWidget *b,
411                  GtkWidget *base)
412 {
413   int ax0, ay0, ax1, ay1, bx0, by0, bx1, by1, xdist = 0, ydist = 0;
414   if (!gtk_widget_translate_coordinates (a, base, 0, 0, &ax0, &ay0) ||
415       !gtk_widget_translate_coordinates (a, base, a->allocation.width, a->allocation.height, &ax1, &ay1))
416     return -G_MAXINT;
417   if (!gtk_widget_translate_coordinates (b, base, 0, 0, &bx0, &by0) ||
418       !gtk_widget_translate_coordinates (b, base, b->allocation.width, b->allocation.height, &bx1, &by1))
419     return +G_MAXINT;
420   if (bx0 >= ax1)
421     xdist = bx0 - ax1;
422   else if (ax0 >= bx1)
423     xdist = ax0 - bx1;
424   if (by0 >= ay1)
425     ydist = by0 - ay1;
426   else if (ay0 >= by1)
427     ydist = ay0 - by1;
428   return xdist + ydist;
429 }
430
431 static int
432 widget_geo_cmp (gconstpointer a,
433                 gconstpointer b,
434                 gpointer      user_data)
435 {
436   gpointer *data = user_data;
437   GtkWidget *wa = (void*) a, *wb = (void*) b, *toplevel = data[0], *base_widget = data[1];
438   int adist = widget_geo_dist (wa, base_widget, toplevel);
439   int bdist = widget_geo_dist (wb, base_widget, toplevel);
440   return adist > bdist ? +1 : adist == bdist ? 0 : -1;
441 }
442
443 /**
444  * gtk_test_find_sibling
445  * @base_widget:        Valid widget, part of a widget hierarchy
446  * @widget_type:        Type of a aearched for sibling widget
447  *
448  * This function will search siblings of @base_widget and siblings of its
449  * ancestors for all widgets matching @widget_type.
450  * Of the matching widgets, the one that is geometrically closest to
451  * @base_widget will be returned.
452  * The general purpose of this function is to find the most likely "action"
453  * widget, relative to another labeling widget. Such as finding a
454  * button or text entry widget, given it's corresponding label widget.
455  *
456  * Returns: a widget of type @widget_type if any is found.
457  **/
458 GtkWidget*
459 gtk_test_find_sibling (GtkWidget *base_widget,
460                        GType      widget_type)
461 {
462   GList *siblings = NULL;
463   GtkWidget *tmpwidget = base_widget;
464   gpointer data[2];
465   /* find all sibling candidates */
466   while (tmpwidget)
467     {
468       tmpwidget = tmpwidget->parent;
469       siblings = g_list_concat (siblings, test_list_descendants (tmpwidget, widget_type));
470     }
471   /* sort them by distance to base_widget */
472   data[0] = gtk_widget_get_toplevel (base_widget);
473   data[1] = base_widget;
474   siblings = g_list_sort_with_data (siblings, widget_geo_cmp, data);
475   /* pick nearest != base_widget */
476   siblings = g_list_remove (siblings, base_widget);
477   tmpwidget = siblings ? siblings->data : NULL;
478   g_list_free (siblings);
479   return tmpwidget;
480 }
481
482 /**
483  * gtk_test_find_widget
484  * @widget:        Container widget, usually a GtkWindow.
485  * @label_pattern: Shell-glob pattern to match a label string.
486  * @widget_type:   Type of a aearched for label sibling widget.
487  *
488  * This function will search the descendants of @widget for a widget
489  * of type @widget_type that has a label matching @label_pattern next
490  * to it. This is most useful for automated GUI testing, e.g. to find
491  * the "OK" button in a dialog and synthesize clicks on it.
492  * However see gtk_test_find_label(), gtk_test_find_sibling() and
493  * gtk_test_widget_click() for possible caveats involving the search of
494  * such widgets and synthesizing widget events.
495  *
496  * Returns: a valid widget if any is found or %NULL.
497  **/
498 GtkWidget*
499 gtk_test_find_widget (GtkWidget    *widget,
500                       const gchar  *label_pattern,
501                       GType         widget_type)
502 {
503   GtkWidget *label = gtk_test_find_label (widget, label_pattern);
504   if (!label)
505     label = gtk_test_find_label (gtk_widget_get_toplevel (widget), label_pattern);
506   if (label)
507     return gtk_test_find_sibling (label, widget_type);
508   return NULL;
509 }
510
511 /**
512  * gtk_test_slider_set_perc
513  * @widget:     valid widget pointer.
514  * @percentage: value between 0 and 100.
515  *
516  * This function will adjust theslider position of all GtkRange
517  * based widgets, such as scrollbars or scales, it'll also adjust
518  * spin buttons. The adjustment value of tehse widgets is set to
519  * a value between the lower and upper limits, according to the
520  * @percentage argument.
521  **/
522 void
523 gtk_test_slider_set_perc (GtkWidget      *widget,
524                           double          percentage)
525 {
526   GtkAdjustment *adjustment = NULL;
527   if (GTK_IS_RANGE (widget))
528     adjustment = gtk_range_get_adjustment (GTK_RANGE (widget));
529   else if (GTK_IS_SPIN_BUTTON (widget))
530     adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
531   if (adjustment)
532     gtk_adjustment_set_value (adjustment, adjustment->lower + (adjustment->upper - adjustment->lower - adjustment->page_size) * percentage * 0.01);
533 }
534
535 /**
536  * gtk_test_slider_get_value
537  * @widget:     valid widget pointer.
538  *
539  * Retrive the literal adjustment value for GtkRange based
540  * widgets and spin buttons. Note that the value returned by
541  * this function is anything between the lower and upper bounds
542  * of the adjustment belonging to @widget, and is not a percentage
543  * as passed in to gtk_test_slider_set_perc().
544  *
545  * Returns: adjustment->value for an adjustment belonging to @widget.
546  **/
547 double
548 gtk_test_slider_get_value (GtkWidget *widget)
549 {
550   GtkAdjustment *adjustment = NULL;
551   if (GTK_IS_RANGE (widget))
552     adjustment = gtk_range_get_adjustment (GTK_RANGE (widget));
553   else if (GTK_IS_SPIN_BUTTON (widget))
554     adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
555   return adjustment ? adjustment->value : 0;
556 }
557
558 /**
559  * gtk_test_text_set
560  * @widget:     valid widget pointer.
561  * @string:     a 0-terminated C string
562  *
563  * Set the text string of @widget to @string if it is a GtkLabel,
564  * GtkEditable (entry and text widgets) or GtkTextView.
565  **/
566 void
567 gtk_test_text_set (GtkWidget   *widget,
568                    const gchar *string)
569 {
570   if (GTK_IS_LABEL (widget))
571     gtk_label_set_text (GTK_LABEL (widget), string);
572   else if (GTK_IS_EDITABLE (widget))
573     {
574       int pos = 0;
575       gtk_editable_delete_text (GTK_EDITABLE (widget), 0, -1);
576       gtk_editable_insert_text (GTK_EDITABLE (widget), string, -1, &pos);
577     }
578   else if (GTK_IS_TEXT_VIEW (widget))
579     {
580       GtkTextBuffer *tbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
581       gtk_text_buffer_set_text (tbuffer, string, -1);
582     }
583 }
584
585 /**
586  * gtk_test_text_get
587  * @widget:     valid widget pointer.
588  *
589  * Retrive the text string of @widget if it is a GtkLabel,
590  * GtkEditable (entry and text widgets) or GtkTextView.
591  *
592  * Returns: new 0-terminated C string, needs to be releaed with g_free().
593  **/
594 gchar*
595 gtk_test_text_get (GtkWidget *widget)
596 {
597   if (GTK_IS_LABEL (widget))
598     return g_strdup (gtk_label_get_text (GTK_LABEL (widget)));
599   else if (GTK_IS_EDITABLE (widget))
600     {
601       return g_strdup (gtk_editable_get_chars (GTK_EDITABLE (widget), 0, -1));
602     }
603   else if (GTK_IS_TEXT_VIEW (widget))
604     {
605       GtkTextBuffer *tbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
606       GtkTextIter start, end;
607       gtk_text_buffer_get_start_iter (tbuffer, &start);
608       gtk_text_buffer_get_end_iter (tbuffer, &end);
609       return gtk_text_buffer_get_text (tbuffer, &start, &end, FALSE);
610     }
611   return NULL;
612 }
613
614 /**
615  * gtk_test_create_widget
616  * @widget_type:        valid widget type.
617  * @first_property_name: Name of first property to set or %NULL
618  *
619  * This function wraps g_object_new() for widget types.
620  * It'll automatically show all created non window widgets, also
621  * g_object_ref_sink() them (to keep them alive across a running test)
622  * and set them up for destruction during test teardown phase.
623  *
624  * Returns: a newly created widget.
625  */
626 GtkWidget*
627 gtk_test_create_widget (GType        widget_type,
628                         const gchar *first_property_name,
629                         ...)
630 {
631   GtkWidget *widget;
632   va_list var_args;
633   g_return_val_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET), NULL);
634   va_start (var_args, first_property_name);
635   widget = (GtkWidget*) g_object_new_valist (widget_type, first_property_name, var_args);
636   va_end (var_args);
637   if (widget)
638     {
639       if (!GTK_IS_WINDOW (widget))
640         gtk_widget_show (widget);
641       g_object_ref_sink (widget);
642 #ifdef __G_TESTFRAMEWORK_H__ /* FIXME */
643       g_test_queue_unref (widget);
644       g_test_queue_destroy ((GDestroyNotify) gtk_widget_destroy, widget);
645 #endif
646     }
647   return widget;
648 }
649
650 static void
651 try_main_quit (void)
652 {
653   if (gtk_main_level())
654     gtk_main_quit();
655 }
656
657 static int
658 test_increment_intp (int *intp)
659 {
660   *intp += 1;
661   return 1; /* TRUE in case we're connected to event signals */
662 }
663
664 /**
665  * gtk_test_display_button_window
666  * @window_title:       Title of the window to be displayed.
667  * @dialog_text:        Text inside the window to be displayed.
668  * @...:                %NULL terminated list of (const char *label, int *nump) pairs.
669  *
670  * Create a window with window title @window_title, text contents @dialog_text,
671  * and a number of buttons, according to the paired argument list given
672  * as @... parameters.
673  * Each button is created with a @label and a ::clicked signal handler that
674  * incremrents the integer stored in @nump.
675  * The window will be automatically shown with gtk_widget_show_now() after
676  * creation, so when this function returns it has already been mapped,
677  * resized and positioned on screen.
678  * The window will quit any running gtk_main()-loop when destroyed, and it
679  * will automatically be destroyed upon test function teardown.
680  *
681  * Returns: a widget pointer to the newly created GtkWindow.
682  **/
683 GtkWidget*
684 gtk_test_display_button_window (const gchar *window_title,
685                                 const gchar *dialog_text,
686                                 ...) /* NULL terminated list of (label, &int) pairs */
687 {
688   va_list var_args;
689   GtkWidget *window = gtk_test_create_widget (GTK_TYPE_WINDOW, "title", window_title, NULL);
690   GtkWidget *vbox = gtk_test_create_widget (GTK_TYPE_VBOX, "parent", window, NULL);
691   const char *arg1;
692   gtk_test_create_widget (GTK_TYPE_LABEL, "label", dialog_text, "parent", vbox, NULL);
693   g_signal_connect (window, "destroy", G_CALLBACK (try_main_quit), NULL);
694   va_start (var_args, dialog_text);
695   arg1 = va_arg (var_args, const char*);
696   while (arg1)
697     {
698       int *arg2 = va_arg (var_args, int*);
699       GtkWidget *button = gtk_test_create_widget (GTK_TYPE_BUTTON, "label", arg1, "parent", vbox, NULL);
700       g_signal_connect_swapped (button, "clicked", G_CALLBACK (test_increment_intp), arg2);
701       arg1 = va_arg (var_args, const char*);
702     }
703   va_end (var_args);
704   gtk_widget_show_all (vbox);
705   gtk_widget_show_now (window);
706   while (gtk_events_pending ())
707     gtk_main_iteration ();
708   return window;
709 }
710
711 /**
712  * gtk_test_create_simple_window
713  * @window_title:       Title of the window to be displayed.
714  * @dialog_text:        Text inside the window to be displayed.
715  *
716  * Create a simple window with window title @window_title and
717  * text contents @dialog_text.
718  * The window will quit any running gtk_main()-loop when destroyed, and it
719  * will automatically be destroyed upon test function teardown.
720  *
721  * Returns: a widget pointer to the newly created GtkWindow.
722  **/
723 GtkWidget*
724 gtk_test_create_simple_window (const gchar *window_title,
725                                const gchar *dialog_text)
726 {
727   GtkWidget *window = gtk_test_create_widget (GTK_TYPE_WINDOW, "title", window_title, NULL);
728   GtkWidget *vbox = gtk_test_create_widget (GTK_TYPE_VBOX, "parent", window, NULL);
729   gtk_test_create_widget (GTK_TYPE_LABEL, "label", dialog_text, "parent", vbox, NULL);
730   g_signal_connect (window, "destroy", G_CALLBACK (try_main_quit), NULL);
731   gtk_widget_show_all (vbox);
732   return window;
733 }
734
735 /**
736  * gtk_test_xserver_render_sync
737  * @window: a mapped GdkWindow
738  *
739  * This function retrives a pixel from @window to force the windowing
740  * system to carry out any pending rendering commands.
741  * This function is intended to be used to syncronize with rendering
742  * pipelines, to benchmark windowing system rendering operations.
743  **/
744 void
745 gtk_test_xserver_render_sync (GdkWindow *window)
746 {
747   static GdkImage *p1image = NULL;
748   /* syncronize to X drawing queue, see:
749    * http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html
750    */
751   p1image = gdk_drawable_copy_to_image (window, p1image, 0, 0, 0, 0, 1, 1);
752 }
753
754 /**
755  * gtk_test_register_all_types
756  *
757  * Force registration of all core Gtk+ and Gdk object types.
758  * This allowes to refer to any of those object types via
759  * g_type_from_name() after calling this function.
760  **/
761 void
762 gtk_test_register_all_types (void)
763 {
764   volatile GType vgt;
765 #include "gtktypefuncs.c"
766 }
767
768 #define __GTK_TEST_UTILS_C__
769 #include "gtkaliasdef.c"