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.
25 #include "gtkspinbutton.h"
34 * @Short_description: Utilities for testing GTK+ applications
40 * @argcp: Address of the <parameter>argc</parameter> parameter of the
41 * main() function. Changed if any arguments were handled.
42 * @argvp: Address of the <parameter>argv</parameter> parameter of main().
43 * Any parameters understood by g_test_init() or gtk_init() are
44 * stripped before return.
45 * @Varargs: currently unused
47 * This function is used to initialize a GTK+ test program.
49 * It will in turn call g_test_init() and gtk_init() to properly
50 * initialize the testing framework and graphical toolkit. It'll
51 * also set the program's locale to "C" and prevent loading of rc
52 * files and Gtk+ modules. This is done to make tets program
53 * environments as deterministic as possible.
55 * Like gtk_init() and g_test_init(), any known arguments will be
56 * processed and stripped from @argc and @argv.
61 gtk_test_init (int *argcp,
65 g_test_init (argcp, argvp, NULL);
67 * - call g_test_init();
69 * - prevent RC files from loading;
70 * - prevent Gtk modules from loading;
71 * - supply mock object for GtkSettings
73 * - this function could install a mock object around GtkSettings
75 g_setenv ("GTK_MODULES", "", TRUE);
76 g_setenv ("GTK2_RC_FILES", "/dev/null", TRUE);
77 gtk_disable_setlocale();
78 setlocale (LC_ALL, "C");
79 g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=%s");
80 gtk_init (argcp, argvp);
84 test_find_widget_input_windows (GtkWidget *widget,
87 GList *node, *children;
88 GSList *matches = NULL;
90 gdk_window_get_user_data (widget->window, &udata);
91 if (udata == widget && (!input_only || (GDK_IS_WINDOW (widget->window) && gdk_window_is_input_only (GDK_WINDOW (widget->window)))))
92 matches = g_slist_prepend (matches, widget->window);
93 children = gdk_window_get_children (gtk_widget_get_parent_window (widget));
94 for (node = children; node; node = node->next)
96 gdk_window_get_user_data (node->data, &udata);
97 if (udata == widget && (!input_only || (GDK_IS_WINDOW (node->data) && gdk_window_is_input_only (GDK_WINDOW (node->data)))))
98 matches = g_slist_prepend (matches, node->data);
100 return g_slist_reverse (matches);
104 * gtk_test_widget_send_key
105 * @widget: Widget to generate a key press and release on.
106 * @keyval: A Gdk keyboard value.
107 * @modifiers: Keyboard modifiers the event is setup with.
109 * This function will generate keyboard press and release events in
110 * the middle of the first GdkWindow found that belongs to @widget.
111 * For %GTK_NO_WINDOW widgets like GtkButton, this will often be an
112 * input-only event window. For other widgets, this is usually widget->window.
113 * Certain caveats should be considered when using this function, in
114 * particular because the mouse pointer is warped to the key press
115 * location, see gdk_test_simulate_key() for details.
117 * Returns: wether all actions neccessary for the key event simulation were carried out successfully.
122 gtk_test_widget_send_key (GtkWidget *widget,
124 GdkModifierType modifiers)
126 gboolean k1res, k2res;
127 GSList *iwindows = test_find_widget_input_windows (widget, FALSE);
129 iwindows = test_find_widget_input_windows (widget, TRUE);
132 k1res = gdk_test_simulate_key (iwindows->data, -1, -1, keyval, modifiers, GDK_KEY_PRESS);
133 k2res = gdk_test_simulate_key (iwindows->data, -1, -1, keyval, modifiers, GDK_KEY_RELEASE);
134 g_slist_free (iwindows);
135 return k1res && k2res;
139 * gtk_test_widget_click
140 * @widget: Widget to generate a button click on.
141 * @button: Number of the pointer button for the event, usually 1, 2 or 3.
142 * @modifiers: Keyboard modifiers the event is setup with.
144 * This function will generate a @button click (button press and button
145 * release event) in the middle of the first GdkWindow found that belongs
147 * For %GTK_NO_WINDOW widgets like GtkButton, this will often be an
148 * input-only event window. For other widgets, this is usually widget->window.
149 * Certain caveats should be considered when using this function, in
150 * particular because the mouse pointer is warped to the button click
151 * location, see gdk_test_simulate_button() for details.
153 * Returns: wether all actions neccessary for the button click simulation were carried out successfully.
158 gtk_test_widget_click (GtkWidget *widget,
160 GdkModifierType modifiers)
162 gboolean b1res, b2res;
163 GSList *iwindows = test_find_widget_input_windows (widget, FALSE);
165 iwindows = test_find_widget_input_windows (widget, TRUE);
168 b1res = gdk_test_simulate_button (iwindows->data, -1, -1, button, modifiers, GDK_BUTTON_PRESS);
169 b2res = gdk_test_simulate_button (iwindows->data, -1, -1, button, modifiers, GDK_BUTTON_RELEASE);
170 g_slist_free (iwindows);
171 return b1res && b2res;
175 * gtk_test_spin_button_click
176 * @spinner: valid GtkSpinButton widget.
177 * @button: Number of the pointer button for the event, usually 1, 2 or 3.
178 * @upwards: %TRUE for upwards arrow click, %FALSE for downwards arrow click.
180 * This function will generate a @button click in the upwards or downwards
181 * spin button arrow areas, usually leading to an increase or decrease of
182 * spin button's value.
184 * Returns: wether all actions neccessary for the button click simulation were carried out successfully.
189 gtk_test_spin_button_click (GtkSpinButton *spinner,
194 gboolean b1res = FALSE, b2res = FALSE;
196 panel = _gtk_spin_button_get_panel (spinner);
200 gint width, height, pos;
201 gdk_drawable_get_size (panel, &width, &height);
202 pos = upwards ? 0 : height - 1;
203 b1res = gdk_test_simulate_button (panel, width - 1, pos, button, 0, GDK_BUTTON_PRESS);
204 b2res = gdk_test_simulate_button (panel, width - 1, pos, button, 0, GDK_BUTTON_RELEASE);
206 return b1res && b2res;
210 * gtk_test_find_label
211 * @widget: Valid label or container widget.
212 * @label_pattern: Shell-glob pattern to match a label string.
214 * This function will search @widget and all its descendants for a GtkLabel
215 * widget with a text string matching @label_pattern.
216 * The @label_pattern may contain asterisks '*' and question marks '?' as
217 * placeholders, g_pattern_match() is used for the matching.
218 * Note that locales other than "C" tend to alter (translate" label strings,
219 * so this function is genrally only useful in test programs with
220 * predetermined locales, see gtk_test_init() for more details.
222 * Returns: a GtkLabel widget if any is found.
227 gtk_test_find_label (GtkWidget *widget,
228 const gchar *label_pattern)
230 if (GTK_IS_LABEL (widget))
232 const gchar *text = gtk_label_get_text (GTK_LABEL (widget));
233 if (g_pattern_match_simple (label_pattern, text))
236 if (GTK_IS_CONTAINER (widget))
238 GList *node, *list = gtk_container_get_children (GTK_CONTAINER (widget));
239 for (node = list; node; node = node->next)
241 GtkWidget *label = gtk_test_find_label (node->data, label_pattern);
251 test_list_descendants (GtkWidget *widget,
254 GList *results = NULL;
255 if (GTK_IS_CONTAINER (widget))
257 GList *node, *list = gtk_container_get_children (GTK_CONTAINER (widget));
258 for (node = list; node; node = node->next)
260 if (!widget_type || g_type_is_a (G_OBJECT_TYPE (node->data), widget_type))
261 results = g_list_prepend (results, node->data);
263 results = g_list_concat (results, test_list_descendants (node->data, widget_type));
271 widget_geo_dist (GtkWidget *a,
275 int ax0, ay0, ax1, ay1, bx0, by0, bx1, by1, xdist = 0, ydist = 0;
276 if (!gtk_widget_translate_coordinates (a, base, 0, 0, &ax0, &ay0) ||
277 !gtk_widget_translate_coordinates (a, base, a->allocation.width, a->allocation.height, &ax1, &ay1))
279 if (!gtk_widget_translate_coordinates (b, base, 0, 0, &bx0, &by0) ||
280 !gtk_widget_translate_coordinates (b, base, b->allocation.width, b->allocation.height, &bx1, &by1))
290 return xdist + ydist;
294 widget_geo_cmp (gconstpointer a,
298 gpointer *data = user_data;
299 GtkWidget *wa = (void*) a, *wb = (void*) b, *toplevel = data[0], *base_widget = data[1];
300 int adist = widget_geo_dist (wa, base_widget, toplevel);
301 int bdist = widget_geo_dist (wb, base_widget, toplevel);
302 return adist > bdist ? +1 : adist == bdist ? 0 : -1;
306 * gtk_test_find_sibling
307 * @base_widget: Valid widget, part of a widget hierarchy
308 * @widget_type: Type of a aearched for sibling widget
310 * This function will search siblings of @base_widget and siblings of its
311 * ancestors for all widgets matching @widget_type.
312 * Of the matching widgets, the one that is geometrically closest to
313 * @base_widget will be returned.
314 * The general purpose of this function is to find the most likely "action"
315 * widget, relative to another labeling widget. Such as finding a
316 * button or text entry widget, given it's corresponding label widget.
318 * Returns: a widget of type @widget_type if any is found.
323 gtk_test_find_sibling (GtkWidget *base_widget,
326 GList *siblings = NULL;
327 GtkWidget *tmpwidget = base_widget;
329 /* find all sibling candidates */
332 tmpwidget = tmpwidget->parent;
333 siblings = g_list_concat (siblings, test_list_descendants (tmpwidget, widget_type));
335 /* sort them by distance to base_widget */
336 data[0] = gtk_widget_get_toplevel (base_widget);
337 data[1] = base_widget;
338 siblings = g_list_sort_with_data (siblings, widget_geo_cmp, data);
339 /* pick nearest != base_widget */
340 siblings = g_list_remove (siblings, base_widget);
341 tmpwidget = siblings ? siblings->data : NULL;
342 g_list_free (siblings);
347 * gtk_test_find_widget
348 * @widget: Container widget, usually a GtkWindow.
349 * @label_pattern: Shell-glob pattern to match a label string.
350 * @widget_type: Type of a aearched for label sibling widget.
352 * This function will search the descendants of @widget for a widget
353 * of type @widget_type that has a label matching @label_pattern next
354 * to it. This is most useful for automated GUI testing, e.g. to find
355 * the "OK" button in a dialog and synthesize clicks on it.
356 * However see gtk_test_find_label(), gtk_test_find_sibling() and
357 * gtk_test_widget_click() for possible caveats involving the search of
358 * such widgets and synthesizing widget events.
360 * Returns: a valid widget if any is found or %NULL.
365 gtk_test_find_widget (GtkWidget *widget,
366 const gchar *label_pattern,
369 GtkWidget *label = gtk_test_find_label (widget, label_pattern);
371 label = gtk_test_find_label (gtk_widget_get_toplevel (widget), label_pattern);
373 return gtk_test_find_sibling (label, widget_type);
378 * gtk_test_slider_set_perc
379 * @widget: valid widget pointer.
380 * @percentage: value between 0 and 100.
382 * This function will adjust the slider position of all GtkRange
383 * based widgets, such as scrollbars or scales, it'll also adjust
384 * spin buttons. The adjustment value of these widgets is set to
385 * a value between the lower and upper limits, according to the
386 * @percentage argument.
391 gtk_test_slider_set_perc (GtkWidget *widget,
394 GtkAdjustment *adjustment = NULL;
395 if (GTK_IS_RANGE (widget))
396 adjustment = gtk_range_get_adjustment (GTK_RANGE (widget));
397 else if (GTK_IS_SPIN_BUTTON (widget))
398 adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
400 gtk_adjustment_set_value (adjustment, adjustment->lower + (adjustment->upper - adjustment->lower - adjustment->page_size) * percentage * 0.01);
404 * gtk_test_slider_get_value
405 * @widget: valid widget pointer.
407 * Retrive the literal adjustment value for GtkRange based
408 * widgets and spin buttons. Note that the value returned by
409 * this function is anything between the lower and upper bounds
410 * of the adjustment belonging to @widget, and is not a percentage
411 * as passed in to gtk_test_slider_set_perc().
413 * Returns: adjustment->value for an adjustment belonging to @widget.
418 gtk_test_slider_get_value (GtkWidget *widget)
420 GtkAdjustment *adjustment = NULL;
421 if (GTK_IS_RANGE (widget))
422 adjustment = gtk_range_get_adjustment (GTK_RANGE (widget));
423 else if (GTK_IS_SPIN_BUTTON (widget))
424 adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
425 return adjustment ? adjustment->value : 0;
430 * @widget: valid widget pointer.
431 * @string: a 0-terminated C string
433 * Set the text string of @widget to @string if it is a GtkLabel,
434 * GtkEditable (entry and text widgets) or GtkTextView.
439 gtk_test_text_set (GtkWidget *widget,
442 if (GTK_IS_LABEL (widget))
443 gtk_label_set_text (GTK_LABEL (widget), string);
444 else if (GTK_IS_EDITABLE (widget))
447 gtk_editable_delete_text (GTK_EDITABLE (widget), 0, -1);
448 gtk_editable_insert_text (GTK_EDITABLE (widget), string, -1, &pos);
450 else if (GTK_IS_TEXT_VIEW (widget))
452 GtkTextBuffer *tbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
453 gtk_text_buffer_set_text (tbuffer, string, -1);
459 * @widget: valid widget pointer.
461 * Retrive the text string of @widget if it is a GtkLabel,
462 * GtkEditable (entry and text widgets) or GtkTextView.
464 * Returns: new 0-terminated C string, needs to be released with g_free().
469 gtk_test_text_get (GtkWidget *widget)
471 if (GTK_IS_LABEL (widget))
472 return g_strdup (gtk_label_get_text (GTK_LABEL (widget)));
473 else if (GTK_IS_EDITABLE (widget))
475 return g_strdup (gtk_editable_get_chars (GTK_EDITABLE (widget), 0, -1));
477 else if (GTK_IS_TEXT_VIEW (widget))
479 GtkTextBuffer *tbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
480 GtkTextIter start, end;
481 gtk_text_buffer_get_start_iter (tbuffer, &start);
482 gtk_text_buffer_get_end_iter (tbuffer, &end);
483 return gtk_text_buffer_get_text (tbuffer, &start, &end, FALSE);
489 * gtk_test_create_widget
490 * @widget_type: a valid widget type.
491 * @first_property_name: (allow-none): Name of first property to set or %NULL
492 * @Varargs: value to set the first property to, followed by more
493 * name-value pairs, terminated by %NULL
495 * This function wraps g_object_new() for widget types.
496 * It'll automatically show all created non window widgets, also
497 * g_object_ref_sink() them (to keep them alive across a running test)
498 * and set them up for destruction during the next test teardown phase.
500 * Returns: a newly created widget.
505 gtk_test_create_widget (GType widget_type,
506 const gchar *first_property_name,
511 g_return_val_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET), NULL);
512 va_start (var_args, first_property_name);
513 widget = (GtkWidget*) g_object_new_valist (widget_type, first_property_name, var_args);
517 if (!GTK_IS_WINDOW (widget))
518 gtk_widget_show (widget);
519 g_object_ref_sink (widget);
520 g_test_queue_unref (widget);
521 g_test_queue_destroy ((GDestroyNotify) gtk_widget_destroy, widget);
529 if (gtk_main_level())
534 test_increment_intp (int *intp)
537 return 1; /* TRUE in case we're connected to event signals */
541 * gtk_test_display_button_window
542 * @window_title: Title of the window to be displayed.
543 * @dialog_text: Text inside the window to be displayed.
544 * @...: %NULL terminated list of (const char *label, int *nump) pairs.
546 * Create a window with window title @window_title, text contents @dialog_text,
547 * and a number of buttons, according to the paired argument list given
548 * as @... parameters.
549 * Each button is created with a @label and a ::clicked signal handler that
550 * incremrents the integer stored in @nump.
551 * The window will be automatically shown with gtk_widget_show_now() after
552 * creation, so when this function returns it has already been mapped,
553 * resized and positioned on screen.
554 * The window will quit any running gtk_main()-loop when destroyed, and it
555 * will automatically be destroyed upon test function teardown.
557 * Returns: a widget pointer to the newly created GtkWindow.
562 gtk_test_display_button_window (const gchar *window_title,
563 const gchar *dialog_text,
564 ...) /* NULL terminated list of (label, &int) pairs */
567 GtkWidget *window = gtk_test_create_widget (GTK_TYPE_WINDOW, "title", window_title, NULL);
568 GtkWidget *vbox = gtk_test_create_widget (GTK_TYPE_VBOX, "parent", window, NULL);
570 gtk_test_create_widget (GTK_TYPE_LABEL, "label", dialog_text, "parent", vbox, NULL);
571 g_signal_connect (window, "destroy", G_CALLBACK (try_main_quit), NULL);
572 va_start (var_args, dialog_text);
573 arg1 = va_arg (var_args, const char*);
576 int *arg2 = va_arg (var_args, int*);
577 GtkWidget *button = gtk_test_create_widget (GTK_TYPE_BUTTON, "label", arg1, "parent", vbox, NULL);
578 g_signal_connect_swapped (button, "clicked", G_CALLBACK (test_increment_intp), arg2);
579 arg1 = va_arg (var_args, const char*);
582 gtk_widget_show_all (vbox);
583 gtk_widget_show_now (window);
584 while (gtk_events_pending ())
585 gtk_main_iteration ();
590 * gtk_test_create_simple_window
591 * @window_title: Title of the window to be displayed.
592 * @dialog_text: Text inside the window to be displayed.
594 * Create a simple window with window title @window_title and
595 * text contents @dialog_text.
596 * The window will quit any running gtk_main()-loop when destroyed, and it
597 * will automatically be destroyed upon test function teardown.
599 * Returns: a widget pointer to the newly created GtkWindow.
604 gtk_test_create_simple_window (const gchar *window_title,
605 const gchar *dialog_text)
607 GtkWidget *window = gtk_test_create_widget (GTK_TYPE_WINDOW, "title", window_title, NULL);
608 GtkWidget *vbox = gtk_test_create_widget (GTK_TYPE_VBOX, "parent", window, NULL);
609 gtk_test_create_widget (GTK_TYPE_LABEL, "label", dialog_text, "parent", vbox, NULL);
610 g_signal_connect (window, "destroy", G_CALLBACK (try_main_quit), NULL);
611 gtk_widget_show_all (vbox);
615 static GType *all_registered_types = NULL;
616 static guint n_all_registered_types = 0;
619 * gtk_test_list_all_types
620 * @n_types: location to store number of types
621 * @returns: 0-terminated array of type ids
623 * Return the type ids that have been registered after
624 * calling gtk_test_register_all_types().
629 gtk_test_list_all_types (guint *n_types)
632 *n_types = n_all_registered_types;
633 return all_registered_types;
637 * gtk_test_register_all_types
639 * Force registration of all core Gtk+ and Gdk object types.
640 * This allowes to refer to any of those object types via
641 * g_type_from_name() after calling this function.
646 gtk_test_register_all_types (void)
648 if (!all_registered_types)
650 const guint max_gtk_types = 999;
652 all_registered_types = g_new0 (GType, max_gtk_types);
653 tp = all_registered_types;
654 #include "gtktypefuncs.c"
655 n_all_registered_types = tp - all_registered_types;
656 g_assert (n_all_registered_types + 1 < max_gtk_types);