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