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