1 /* Gtk+ object tests
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 #include <gtk/gtk.h>
21 #include <string.h>
22 
23 /* --- helper macros for property value generation --- */
24 /* dvalue=+0: generate minimum value
25  * dvalue=.x: generate value within value range proportional to x.
26  * dvalue=+1: generate maximum value
27  * dvalue=-1: generate random value within value range
28  * dvalue=+2: initialize value from default_value
29  */
30 #define ASSIGN_VALUE(__g_value_set_func, __value, PSPECTYPE, __pspec, __default_value, __minimum, __maximum, __dvalue) do { \
31   PSPECTYPE __p = (PSPECTYPE) __pspec; \
32   __g_value_set_func (__value, SELECT_VALUE (__dvalue, __p->__default_value, __p->__minimum, __p->__maximum)); \
33 } while (0)
34 #define SELECT_VALUE(__dvalue, __default_value, __minimum, __maximum) ( \
35   __dvalue >= 0 && __dvalue <= 1 ? __minimum * (1 - __dvalue) + __dvalue * __maximum : \
36     __dvalue <= -1 ? g_test_rand_double_range (__minimum, __maximum) : \
37       __default_value)
38 #define SELECT_NAME(__dvalue) ( \
39   __dvalue == 0 ? "minimum" : \
40     __dvalue == 1 ? "maximum" : \
41       __dvalue >= +2 ? "default" : \
42         __dvalue == 0.5 ? "medium" : \
43           __dvalue > 0 && __dvalue < 1 ? "fractional" : \
44             "random")
45 #define MATCH_ANY_VALUE         ((void*) 0xf1874c23)
46 
47 /* --- property blacklists --- */
48 typedef struct {
49   const char   *type_name;
50   const char   *name;
51   gconstpointer value;
52 } IgnoreProperty;
53 static const IgnoreProperty*
list_ignore_properties(gboolean buglist)54 list_ignore_properties (gboolean buglist)
55 {
56   /* currently untestable properties */
57   static const IgnoreProperty ignore_properties[] = {
58     { "GtkCurve",               "",                     NULL, },                        /* Just ignore it, not worth fixing */
59     { "GtkContainer",           "child",                NULL, },                        /* needs working child widget */
60     { "GtkRadioMenuItem",       "group",                NULL, },                        /* needs working sibling */
61     { "GtkWidget",              "parent",               NULL, },                        /* needs working parent widget */
62     { "GtkCList",               "selection-mode",       (void*) GTK_SELECTION_NONE, },
63     { "GtkWidget",              "has-default",          (void*) TRUE, },                /* conflicts with toplevel-less widgets */
64     { "GtkWidget",              "screen",               NULL, },
65     { "GtkWindow",              "type-hint",            (void*) GDK_WINDOW_TYPE_HINT_DND, }, /* conflicts with ::visible=TRUE */
66     { "GtkCellView",            "background",           (void*) "", },                  /* "" is not a valid background color */
67     { "GtkColorButton",         "color",                (void*) NULL, },                /* not a valid boxed color */
68     { "GtkInputDialog",         "has-separator",        (void*) MATCH_ANY_VALUE, },     /* property disabled */
69     { "GtkMessageDialog",       "has-separator",        (void*) MATCH_ANY_VALUE, },     /* property disabled */
70     { "GtkFontSelectionDialog", "has-separator",        (void*) MATCH_ANY_VALUE, },     /* property disabled */
71     { "GtkColorSelectionDialog","has-separator",        (void*) MATCH_ANY_VALUE, },     /* property disabled */
72     { "GtkColorSelection",      "child",                NULL, },
73     { "GtkColorSelection",      "current-color",        (void*) NULL, },                /* not a valid boxed color */
74     { "GtkComboBox",            "row-span-column",      (void*) MATCH_ANY_VALUE },      /* GtkComboBoxEntry needs a tree model for this */
75     { "GtkComboBox",            "column-span-column",   (void*) MATCH_ANY_VALUE },      /* GtkComboBoxEntry needs a tree model for this */
76     { "GtkFileChooserButton",   "select-multiple",      (void*) MATCH_ANY_VALUE },      /* property disabled */
77     { "GtkFileChooserButton",   "action",               (void*) GTK_FILE_CHOOSER_ACTION_SAVE },
78     { "GtkFileChooserButton",   "action",               (void*) GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER },
79     { "GtkFileChooserWidget",   "select-multiple",      (void*) 0x1 },                  /* property conflicts */
80     { "GtkFileChooserDialog",   "select-multiple",      (void*) MATCH_ANY_VALUE },      /* property disabled */
81     { "GtkMenu",                "accel-path",           (void*) MATCH_ANY_VALUE },      /* has odd restrictions in the setter */
82     { "GtkMenuItem",            "accel-path",           (void*) MATCH_ANY_VALUE },      /* has odd restrictions in the setter */
83     { "GtkRecentChooserMenu",   "select-multiple",      (void*) MATCH_ANY_VALUE },      /* property disabled */
84     { "GtkTextView",            "overwrite",            (void*) MATCH_ANY_VALUE },      /* needs text buffer */
85     { "GtkToolbar",             "icon-size",            (void*) GTK_ICON_SIZE_INVALID },
86     { NULL, NULL, NULL }
87   };
88   /* properties suspected to be Gdk/Gtk+ bugs */
89   static const IgnoreProperty bug_properties[] = {
90     { "GtkComboBox",            "active",               (void*) MATCH_ANY_VALUE },      /* FIXME: triggers NULL model bug */
91     { "GtkCTree",               "spacing",              (void*) MATCH_ANY_VALUE },      /* FIXME: triggers signedness bug */
92     { "GtkFileChooserButton",   "local-only",           (void*) MATCH_ANY_VALUE },      /* FIXME: triggers NULL path assertion */
93     { "GtkFileChooserDialog",   "local-only",           (void*) MATCH_ANY_VALUE },      /* FIXME: triggers NULL path assertion */
94     { "GtkFileChooserWidget",   "local-only",           (void*) MATCH_ANY_VALUE },      /* FIXME: triggers NULL path assertion */
95     { "GtkMenu",                "tearoff-state",        (void*) MATCH_ANY_VALUE },      /* FIXME: triggers NULL widget cast */
96     { "GtkText",                "text-position",        (void*) MATCH_ANY_VALUE },      /* FIXME: segfaults, fix property minimum/maximum */
97     { NULL, NULL, NULL }
98   };
99   if (buglist)
100     return bug_properties;
101   else
102     return ignore_properties;
103 }
104 
105 /* --- test functions --- */
106 static void
pspec_select_value(GParamSpec * pspec,GValue * value,double dvalue)107 pspec_select_value (GParamSpec *pspec,
108                     GValue     *value,
109                     double      dvalue)
110 {
111   /* generate a value suitable for pspec */
112   if (G_IS_PARAM_SPEC_CHAR (pspec))
113     ASSIGN_VALUE (g_value_set_char, value, GParamSpecChar*, pspec, default_value, minimum, maximum, dvalue);
114   else if (G_IS_PARAM_SPEC_UCHAR (pspec))
115     ASSIGN_VALUE (g_value_set_uchar, value, GParamSpecUChar*, pspec, default_value, minimum, maximum, dvalue);
116   else if (G_IS_PARAM_SPEC_INT (pspec))
117     ASSIGN_VALUE (g_value_set_int, value, GParamSpecInt*, pspec, default_value, minimum, maximum, dvalue);
118   else if (G_IS_PARAM_SPEC_UINT (pspec))
119     ASSIGN_VALUE (g_value_set_uint, value, GParamSpecUInt*, pspec, default_value, minimum, maximum, dvalue);
120   else if (G_IS_PARAM_SPEC_LONG (pspec))
121     ASSIGN_VALUE (g_value_set_long, value, GParamSpecLong*, pspec, default_value, minimum, maximum, dvalue);
122   else if (G_IS_PARAM_SPEC_ULONG (pspec))
123     ASSIGN_VALUE (g_value_set_ulong, value, GParamSpecULong*, pspec, default_value, minimum, maximum, dvalue);
124   else if (G_IS_PARAM_SPEC_INT64 (pspec))
125     ASSIGN_VALUE (g_value_set_int64, value, GParamSpecInt64*, pspec, default_value, minimum, maximum, dvalue);
126   else if (G_IS_PARAM_SPEC_UINT64 (pspec))
127     ASSIGN_VALUE (g_value_set_uint64, value, GParamSpecUInt64*, pspec, default_value, minimum, maximum, dvalue);
128   else if (G_IS_PARAM_SPEC_FLOAT (pspec))
129     ASSIGN_VALUE (g_value_set_float, value, GParamSpecFloat*, pspec, default_value, minimum, maximum, dvalue);
130   else if (G_IS_PARAM_SPEC_DOUBLE (pspec))
131     ASSIGN_VALUE (g_value_set_double, value, GParamSpecDouble*, pspec, default_value, minimum, maximum, dvalue);
132   else if (G_IS_PARAM_SPEC_BOOLEAN (pspec))
133     g_value_set_boolean (value, SELECT_VALUE (dvalue, ((GParamSpecBoolean*) pspec)->default_value, FALSE, TRUE));
134   else if (G_IS_PARAM_SPEC_UNICHAR (pspec))
135     g_value_set_uint (value, SELECT_VALUE (dvalue, ((GParamSpecUnichar*) pspec)->default_value, FALSE, TRUE));
136   else if (G_IS_PARAM_SPEC_GTYPE (pspec))
137     g_value_set_gtype (value, SELECT_VALUE ((int) dvalue, ((GParamSpecGType*) pspec)->is_a_type, 0, GTK_TYPE_WIDGET));
138   else if (G_IS_PARAM_SPEC_STRING (pspec))
139     {
140       GParamSpecString *sspec = (GParamSpecString*) pspec;
141       if (dvalue >= +2)
142         g_value_set_string (value, sspec->default_value);
143       if (dvalue > 0 && sspec->cset_first && sspec->cset_nth)
144         g_value_take_string (value, g_strdup_printf ("%c%c", sspec->cset_first[0], sspec->cset_nth[0]));
145       else /* if (sspec->ensure_non_null) */
146         g_value_set_string (value, "");
147     }
148   else if (G_IS_PARAM_SPEC_ENUM (pspec))
149     {
150       GParamSpecEnum *espec = (GParamSpecEnum*) pspec;
151       if (dvalue >= +2)
152         g_value_set_enum (value, espec->default_value);
153       if (dvalue >= 0 && dvalue <= 1)
154         g_value_set_enum (value, espec->enum_class->values[(int) ((espec->enum_class->n_values - 1) * dvalue)].value);
155       else if (dvalue <= -1)
156         g_value_set_enum (value, espec->enum_class->values[g_test_rand_int_range (0, espec->enum_class->n_values)].value);
157     }
158   else if (G_IS_PARAM_SPEC_FLAGS (pspec))
159     {
160       GParamSpecFlags *fspec = (GParamSpecFlags*) pspec;
161       if (dvalue >= +2)
162         g_value_set_flags (value, fspec->default_value);
163       if (dvalue >= 0 && dvalue <= 1)
164         g_value_set_flags (value, fspec->flags_class->values[(int) ((fspec->flags_class->n_values - 1) * dvalue)].value);
165       else if (dvalue <= -1)
166         g_value_set_flags (value, fspec->flags_class->values[g_test_rand_int_range (0, fspec->flags_class->n_values)].value);
167     }
168   /* unimplemented:
169    * G_IS_PARAM_SPEC_PARAM
170    * G_IS_PARAM_SPEC_BOXED
171    * G_IS_PARAM_SPEC_POINTER
172    * G_IS_PARAM_SPEC_VALUE_ARRAY
173    * G_IS_PARAM_SPEC_OBJECT
174    */
175 }
176 
177 static gpointer
value_as_pointer(GValue * value)178 value_as_pointer (GValue *value)
179 {
180   if (g_value_fits_pointer (value))
181     return g_value_peek_pointer (value);
182   if (G_VALUE_HOLDS_BOOLEAN (value))
183     return GINT_TO_POINTER(g_value_get_boolean (value));
184   if (G_VALUE_HOLDS_CHAR (value))
185     return (void*) (gssize) g_value_get_char (value);
186   if (G_VALUE_HOLDS_UCHAR (value))
187     return (void*) (gsize) g_value_get_uchar (value);
188   if (G_VALUE_HOLDS_INT (value))
189     return GINT_TO_POINTER(g_value_get_int (value));
190   if (G_VALUE_HOLDS_UINT (value))
191     return GUINT_TO_POINTER(g_value_get_uint (value));
192   if (G_VALUE_HOLDS_LONG (value))
193     return (void*) g_value_get_long (value);
194   if (G_VALUE_HOLDS_ULONG (value))
195     return (void*) g_value_get_ulong (value);
196   if (G_VALUE_HOLDS_FLOAT (value))
197     return (void*) (gssize) g_value_get_float (value);
198   if (G_VALUE_HOLDS_DOUBLE (value))
199     return (void*) (gssize) g_value_get_double (value);
200   if (G_VALUE_HOLDS_ENUM (value))
201     return (void*) (gssize) g_value_get_enum (value);
202   if (G_VALUE_HOLDS_FLAGS (value))
203     return (void*) (gsize) g_value_get_flags (value);
204   return (void*) 0x1373babe;
205 }
206 
207 static void
object_test_property(GObject * object,GParamSpec * pspec,double dvalue)208 object_test_property (GObject           *object,
209                       GParamSpec        *pspec,
210                       double             dvalue)
211 {
212   /* test setting of a normal writable property */
213   if (pspec->flags & G_PARAM_WRITABLE &&
214       !(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
215     {
216       GValue value = { 0, };
217       guint i;
218       const IgnoreProperty *ignore_properties;
219       /* select value to set */
220       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
221       pspec_select_value (pspec, &value, dvalue);
222       /* ignore untestable properties */
223       ignore_properties = list_ignore_properties (FALSE);
224       for (i = 0; ignore_properties[i].name; i++)
225         if (g_strcmp0 ("", ignore_properties[i].name) ||
226             (g_type_is_a (G_OBJECT_TYPE (object), g_type_from_name (ignore_properties[i].type_name)) &&
227              strcmp (pspec->name, ignore_properties[i].name) == 0 &&
228              (MATCH_ANY_VALUE == ignore_properties[i].value ||
229               value_as_pointer (&value) == ignore_properties[i].value ||
230               (G_VALUE_HOLDS_STRING (&value) &&
231                strcmp (g_value_get_string (&value), ignore_properties[i].value) == 0))))
232           break;
233       /* ignore known property bugs if not testing thoroughly */
234       if (ignore_properties[i].name == NULL && !g_test_thorough ())
235         {
236           ignore_properties = list_ignore_properties (TRUE);
237           for (i = 0; ignore_properties[i].name; i++)
238             if (g_type_is_a (G_OBJECT_TYPE (object), g_type_from_name (ignore_properties[i].type_name)) &&
239                 strcmp (pspec->name, ignore_properties[i].name) == 0 &&
240                 (MATCH_ANY_VALUE == ignore_properties[i].value ||
241                  value_as_pointer (&value) == ignore_properties[i].value ||
242                  (G_VALUE_HOLDS_STRING (&value) &&
243                   strcmp (g_value_get_string (&value), ignore_properties[i].value) == 0)))
244               break;
245         }
246       /* assign unignored properties */
247       if (ignore_properties[i].name == NULL)
248         {
249           if (g_test_verbose ())
250             g_print ("PropertyTest: %s::%s := (%s value (%s): %p)\n",
251                      g_type_name (G_OBJECT_TYPE (object)), pspec->name,
252                      SELECT_NAME (dvalue), g_type_name (G_VALUE_TYPE (&value)),
253                      value_as_pointer (&value));
254           g_object_set_property (object, pspec->name, &value);
255         }
256       /* cleanups */
257       g_value_unset (&value);
258     }
259 }
260 
261 static void
widget_test_properties(GtkWidget * widget,double dvalue)262 widget_test_properties (GtkWidget   *widget,
263                         double       dvalue)
264 {
265   /* try setting all possible properties, according to dvalue */
266   guint i, n_pspecs = 0;
267   GParamSpec **pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (widget), &n_pspecs);
268   for (i = 0; i < n_pspecs; i++)
269     {
270       GParamSpec *pspec = pspecs[i];
271       if (pspec->flags & G_PARAM_WRITABLE &&
272           !(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
273         object_test_property (G_OBJECT (widget), pspecs[i], dvalue);
274     }
275   g_free (pspecs);
276 }
277 
278 static void
widget_property_tests(gconstpointer test_data)279 widget_property_tests (gconstpointer test_data)
280 {
281   GType wtype = (GType) test_data;
282   /* create widget */
283   GtkWidget *widget = gtk_widget_new (wtype, NULL);
284   g_object_ref_sink (widget);
285   /* test property values */
286   widget_test_properties (widget,  +2); /* test default_value */
287   widget_test_properties (widget,   0); /* test minimum */
288   widget_test_properties (widget, 0.5); /* test medium */
289   widget_test_properties (widget,   1); /* test maximum */
290   widget_test_properties (widget,  -1); /* test random value */
291   /* cleanup */
292   gtk_widget_destroy (widget);
293   g_object_unref (widget);
294 }
295 
296 extern void pixbuf_init (void);
297 
298 /* --- main test program --- */
299 int
main(int argc,char * argv[])300 main (int   argc,
301       char *argv[])
302 {
303   const GType *otypes;
304   guint i;
305   /* initialize test program */
306   pixbuf_init ();
307   gtk_test_init (&argc, &argv);
308   gtk_test_register_all_types ();
309   /* install a property test for each widget type */
310   otypes = gtk_test_list_all_types (NULL);
311   for (i = 0; otypes[i]; i++)
312     if (g_type_is_a (otypes[i], GTK_TYPE_WIDGET) &&
313         G_TYPE_IS_OBJECT (otypes[i]) &&
314         !G_TYPE_IS_ABSTRACT (otypes[i]))
315       {
316         gchar *testpath = g_strdup_printf ("/properties/%s", g_type_name (otypes[i]));
317         g_test_add_data_func (testpath, (void*) otypes[i], widget_property_tests);
318         g_free (testpath);
319       }
320   return g_test_run ();
321 }
322