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.
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
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)); \
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) : \
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" : \
45 #define MATCH_ANY_VALUE ((void*) 0xf1874c23)
47 /* --- property blacklists --- */
49 const char *type_name;
53 static const IgnoreProperty*
54 list_ignore_properties (gboolean buglist)
56 /* currently untestable properties */
57 static const IgnoreProperty ignore_properties[] = {
58 { "GtkContainer", "child", NULL, }, /* needs working child widget */
59 { "GtkRadioMenuItem", "group", NULL, }, /* needs working sibling */
60 { "GtkWidget", "parent", NULL, }, /* needs working parent widget */
61 { "GtkCList", "selection-mode", (void*) GTK_SELECTION_NONE, },
62 { "GtkWidget", "has-default", (void*) TRUE, }, /* conflicts with toplevel-less widgets */
63 { "GtkWidget", "screen", NULL, },
64 { "GtkWindow", "type-hint", (void*) GDK_WINDOW_TYPE_HINT_DND, }, /* conflicts with ::visible=TRUE */
65 { "GtkCellView", "background", (void*) "", }, /* "" is not a valid background color */
66 { "GtkColorButton", "color", (void*) NULL, }, /* not a valid boxed color */
67 { "GtkInputDialog", "has-separator", (void*) MATCH_ANY_VALUE, }, /* property disabled */
68 { "GtkMessageDialog", "has-separator", (void*) MATCH_ANY_VALUE, }, /* property disabled */
69 { "GtkFontSelectionDialog", "has-separator", (void*) MATCH_ANY_VALUE, }, /* property disabled */
70 { "GtkColorSelectionDialog","has-separator", (void*) MATCH_ANY_VALUE, }, /* property disabled */
71 { "GtkColorSelection", "child", NULL, },
72 { "GtkColorSelection", "current-color", (void*) NULL, }, /* not a valid boxed color */
73 { "GtkComboBox", "row-span-column", (void*) MATCH_ANY_VALUE }, /* GtkComboBoxEntry needs a tree model for this */
74 { "GtkComboBox", "column-span-column", (void*) MATCH_ANY_VALUE }, /* GtkComboBoxEntry needs a tree model for this */
75 { "GtkComboBoxEntry", "text-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 { "GtkRecentChooserMenu", "select-multiple", (void*) MATCH_ANY_VALUE }, /* property disabled */
82 { "GtkTextView", "overwrite", (void*) MATCH_ANY_VALUE }, /* needs text buffer */
83 { "GtkToolbar", "icon-size", (void*) GTK_ICON_SIZE_INVALID },
86 /* properties suspected to be Gdk/Gtk+ bugs */
87 static const IgnoreProperty bug_properties[] = {
88 { "GtkMessageDialog", "image", NULL, }, /* FIXME: should accept NULL images */
89 { "GtkOptionMenu", "menu", NULL, }, /* FIXME: should accept NULL menus */
90 { "GtkComboBox", "active", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers NULL model bug */
91 { "GtkComboBoxEntry", "text-column", (void*) 0xffffffff }, /* FIXME: triggers signedness bug */
92 { "GtkCTree", "indent", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers signedness bug */
93 { "GtkCTree", "spacing", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers signedness bug */
94 { "GtkCurve", "curve-type", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers OOM */
95 { "GtkCurve", "min-x", (void*) 0x80000000 }, /* FIXME: triggers coordinate OOB */
96 { "GtkCurve", "min-y", (void*) 0x80000000 }, /* FIXME: triggers coordinate OOB */
97 { "GtkCurve", "max-x", (void*) 0x80000000 }, /* FIXME: triggers coordinate OOB */
98 { "GtkCurve", "max-y", (void*) 0x80000000 }, /* FIXME: triggers coordinate OOB */
99 { "GtkFileChooserButton", "local-only", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers NULL path assertion */
100 { "GtkFileChooserDialog", "local-only", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers NULL path assertion */
101 { "GtkFileChooserDialog", "action", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers closure->ref_count assertion */
102 { "GtkFileChooserDialog", "visible", (void*) TRUE }, /* FIXME: triggers gtk_window_resize assertion */
103 { "GtkFileChooserWidget", "local-only", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers NULL path assertion */
104 { "GtkFontSelection", "font-name", (void*) MATCH_ANY_VALUE }, /* FIXME: requires non-NULL GdkScreen */
105 { "GtkInvisible", "has-focus", (void*) TRUE }, /* FIXME: triggers invalid window cast */
106 { "GtkInvisible", "is-focus", (void*) TRUE }, /* FIXME: triggers invalid window cast */
107 { "GtkMenu", "tearoff-state", (void*) MATCH_ANY_VALUE }, /* FIXME: triggers NULL widget cast */
108 { "GtkProgress", "activity-mode", (void*) TRUE }, /* FIXME: segfaults */
109 { "GtkScaleButton", "adjustment", NULL, }, /* FIXME: should accept NULL adjustments */
110 { "GtkStatusbar", "sensitive", (void*) FALSE }, /* FIXME: check if widget is realize */
111 { "GtkTable", "n-rows", (void*) MATCH_ANY_VALUE }, /* FIXME: fix property minimum/maximum */
112 { "GtkTable", "n-columns", (void*) MATCH_ANY_VALUE }, /* FIXME: fix property minimum/maximum */
113 { "GtkText", "text-position", (void*) MATCH_ANY_VALUE }, /* FIXME: segfaults, fix property minimum/maximum */
117 return bug_properties;
119 return ignore_properties;
122 /* --- test functions --- */
124 pspec_select_value (GParamSpec *pspec,
128 /* generate a value suitable for pspec */
129 if (G_IS_PARAM_SPEC_CHAR (pspec))
130 ASSIGN_VALUE (g_value_set_char, value, GParamSpecChar*, pspec, default_value, minimum, maximum, dvalue);
131 else if (G_IS_PARAM_SPEC_UCHAR (pspec))
132 ASSIGN_VALUE (g_value_set_uchar, value, GParamSpecUChar*, pspec, default_value, minimum, maximum, dvalue);
133 else if (G_IS_PARAM_SPEC_INT (pspec))
134 ASSIGN_VALUE (g_value_set_int, value, GParamSpecInt*, pspec, default_value, minimum, maximum, dvalue);
135 else if (G_IS_PARAM_SPEC_UINT (pspec))
136 ASSIGN_VALUE (g_value_set_uint, value, GParamSpecUInt*, pspec, default_value, minimum, maximum, dvalue);
137 else if (G_IS_PARAM_SPEC_LONG (pspec))
138 ASSIGN_VALUE (g_value_set_long, value, GParamSpecLong*, pspec, default_value, minimum, maximum, dvalue);
139 else if (G_IS_PARAM_SPEC_ULONG (pspec))
140 ASSIGN_VALUE (g_value_set_ulong, value, GParamSpecULong*, pspec, default_value, minimum, maximum, dvalue);
141 else if (G_IS_PARAM_SPEC_INT64 (pspec))
142 ASSIGN_VALUE (g_value_set_int64, value, GParamSpecInt64*, pspec, default_value, minimum, maximum, dvalue);
143 else if (G_IS_PARAM_SPEC_UINT64 (pspec))
144 ASSIGN_VALUE (g_value_set_uint64, value, GParamSpecUInt64*, pspec, default_value, minimum, maximum, dvalue);
145 else if (G_IS_PARAM_SPEC_FLOAT (pspec))
146 ASSIGN_VALUE (g_value_set_float, value, GParamSpecFloat*, pspec, default_value, minimum, maximum, dvalue);
147 else if (G_IS_PARAM_SPEC_DOUBLE (pspec))
148 ASSIGN_VALUE (g_value_set_double, value, GParamSpecDouble*, pspec, default_value, minimum, maximum, dvalue);
149 else if (G_IS_PARAM_SPEC_BOOLEAN (pspec))
150 g_value_set_boolean (value, SELECT_VALUE (dvalue, ((GParamSpecBoolean*) pspec)->default_value, FALSE, TRUE));
151 else if (G_IS_PARAM_SPEC_UNICHAR (pspec))
152 g_value_set_uint (value, SELECT_VALUE (dvalue, ((GParamSpecUnichar*) pspec)->default_value, FALSE, TRUE));
153 else if (G_IS_PARAM_SPEC_GTYPE (pspec))
154 g_value_set_gtype (value, SELECT_VALUE ((int) dvalue, ((GParamSpecGType*) pspec)->is_a_type, 0, GTK_TYPE_WIDGET));
155 else if (G_IS_PARAM_SPEC_STRING (pspec))
157 GParamSpecString *sspec = (GParamSpecString*) pspec;
159 g_value_set_string (value, sspec->default_value);
160 if (dvalue > 0 && sspec->cset_first && sspec->cset_nth)
161 g_value_take_string (value, g_strdup_printf ("%c%c", sspec->cset_first[0], sspec->cset_nth[0]));
162 else /* if (sspec->ensure_non_null) */
163 g_value_set_string (value, "");
165 else if (G_IS_PARAM_SPEC_ENUM (pspec))
167 GParamSpecEnum *espec = (GParamSpecEnum*) pspec;
169 g_value_set_enum (value, espec->default_value);
170 if (dvalue >= 0 && dvalue <= 1)
171 g_value_set_enum (value, espec->enum_class->values[(int) ((espec->enum_class->n_values - 1) * dvalue)].value);
172 else if (dvalue <= -1)
173 g_value_set_enum (value, espec->enum_class->values[g_test_rand_int_range (0, espec->enum_class->n_values)].value);
175 else if (G_IS_PARAM_SPEC_FLAGS (pspec))
177 GParamSpecFlags *fspec = (GParamSpecFlags*) pspec;
179 g_value_set_flags (value, fspec->default_value);
180 if (dvalue >= 0 && dvalue <= 1)
181 g_value_set_flags (value, fspec->flags_class->values[(int) ((fspec->flags_class->n_values - 1) * dvalue)].value);
182 else if (dvalue <= -1)
183 g_value_set_flags (value, fspec->flags_class->values[g_test_rand_int_range (0, fspec->flags_class->n_values)].value);
186 * G_IS_PARAM_SPEC_PARAM
187 * G_IS_PARAM_SPEC_BOXED
188 * G_IS_PARAM_SPEC_POINTER
189 * G_IS_PARAM_SPEC_VALUE_ARRAY
190 * G_IS_PARAM_SPEC_OBJECT
195 value_as_pointer (GValue *value)
197 if (g_value_fits_pointer (value))
198 return g_value_peek_pointer (value);
199 if (G_VALUE_HOLDS_BOOLEAN (value))
200 return (void*) g_value_get_boolean (value);
201 if (G_VALUE_HOLDS_CHAR (value))
202 return (void*) (gssize) g_value_get_char (value);
203 if (G_VALUE_HOLDS_UCHAR (value))
204 return (void*) (gsize) g_value_get_uchar (value);
205 if (G_VALUE_HOLDS_INT (value))
206 return (void*) g_value_get_int (value);
207 if (G_VALUE_HOLDS_UINT (value))
208 return (void*) g_value_get_uint (value);
209 if (G_VALUE_HOLDS_LONG (value))
210 return (void*) g_value_get_long (value);
211 if (G_VALUE_HOLDS_ULONG (value))
212 return (void*) g_value_get_ulong (value);
213 if (G_VALUE_HOLDS_FLOAT (value))
214 return (void*) (gssize) g_value_get_float (value);
215 if (G_VALUE_HOLDS_DOUBLE (value))
216 return (void*) (gssize) g_value_get_double (value);
217 if (G_VALUE_HOLDS_ENUM (value))
218 return (void*) (gssize) g_value_get_enum (value);
219 if (G_VALUE_HOLDS_FLAGS (value))
220 return (void*) (gsize) g_value_get_flags (value);
221 return (void*) 0x1373babe;
225 object_test_property (GObject *object,
229 /* test setting of a normal writable property */
230 if (pspec->flags & G_PARAM_WRITABLE &&
231 !(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
233 GValue value = { 0, };
235 const IgnoreProperty *ignore_properties;
236 /* select value to set */
237 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
238 pspec_select_value (pspec, &value, dvalue);
239 /* ignore untestable properties */
240 ignore_properties = list_ignore_properties (FALSE);
241 for (i = 0; ignore_properties[i].name; i++)
242 if (g_type_is_a (G_OBJECT_TYPE (object), g_type_from_name (ignore_properties[i].type_name)) &&
243 strcmp (pspec->name, ignore_properties[i].name) == 0 &&
244 (MATCH_ANY_VALUE == ignore_properties[i].value ||
245 value_as_pointer (&value) == ignore_properties[i].value ||
246 (G_VALUE_HOLDS_STRING (&value) &&
247 strcmp (g_value_get_string (&value), ignore_properties[i].value) == 0)))
249 /* ignore known property bugs if not testing thoroughly */
250 if (ignore_properties[i].name == NULL && !g_test_thorough())
252 ignore_properties = list_ignore_properties (TRUE);
253 for (i = 0; ignore_properties[i].name; i++)
254 if (g_type_is_a (G_OBJECT_TYPE (object), g_type_from_name (ignore_properties[i].type_name)) &&
255 strcmp (pspec->name, ignore_properties[i].name) == 0 &&
256 (MATCH_ANY_VALUE == ignore_properties[i].value ||
257 value_as_pointer (&value) == ignore_properties[i].value ||
258 (G_VALUE_HOLDS_STRING (&value) &&
259 strcmp (g_value_get_string (&value), ignore_properties[i].value) == 0)))
262 /* assign unignored properties */
263 if (ignore_properties[i].name == NULL)
265 if (g_test_verbose())
266 g_print ("PropertyTest: %s::%s := (%s value (%s): %p)\n",
267 g_type_name (G_OBJECT_TYPE (object)), pspec->name,
268 SELECT_NAME (dvalue), g_type_name (G_VALUE_TYPE (&value)),
269 value_as_pointer (&value));
270 g_object_set_property (object, pspec->name, &value);
273 g_value_unset (&value);
278 widget_test_properties (GtkWidget *widget,
281 /* try setting all possible properties, according to dvalue */
282 guint i, n_pspecs = 0;
283 GParamSpec **pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (widget), &n_pspecs);
284 for (i = 0; i < n_pspecs; i++)
286 GParamSpec *pspec = pspecs[i];
287 if (pspec->flags & G_PARAM_WRITABLE &&
288 !(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
289 object_test_property (G_OBJECT (widget), pspecs[i], dvalue);
295 widget_fixups (GtkWidget *widget)
297 /* post-constructor for widgets that need additional settings to work correctly */
298 if (GTK_IS_COMBO_BOX_ENTRY (widget))
300 GtkListStore *store = gtk_list_store_new (1, G_TYPE_STRING);
301 g_object_set (widget, "model", store, "text-column", 0, NULL);
302 g_object_unref (store);
303 gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "test text");
305 else if (GTK_IS_COMBO_BOX (widget))
307 GtkListStore *store = gtk_list_store_new (1, G_TYPE_STRING);
308 g_object_set (widget, "model", store, NULL);
309 g_object_unref (store);
310 gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "test text");
315 widget_property_tests (gconstpointer test_data)
317 GType wtype = (GType) test_data;
319 GtkWidget *widget = gtk_widget_new (wtype, NULL);
320 g_object_ref_sink (widget);
321 widget_fixups (widget);
322 /* test property values */
323 widget_test_properties (widget, +2); /* test default_value */
324 widget_test_properties (widget, 0); /* test minimum */
325 widget_test_properties (widget, 0.5); /* test medium */
326 widget_test_properties (widget, 1); /* test maximum */
327 widget_test_properties (widget, -1); /* test random value */
329 gtk_widget_destroy (widget);
330 g_object_unref (widget);
333 /* --- main test program --- */
340 /* initialize test program */
341 gtk_test_init (&argc, &argv);
342 gtk_test_register_all_types();
343 /* install a property test for each widget type */
344 otypes = gtk_test_list_all_types (NULL);
345 for (i = 0; otypes[i]; i++)
346 if (g_type_is_a (otypes[i], GTK_TYPE_WIDGET) &&
347 G_TYPE_IS_OBJECT (otypes[i]) &&
348 !G_TYPE_IS_ABSTRACT (otypes[i]))
350 gchar *testpath = g_strdup_printf ("/properties/%s", g_type_name (otypes[i]));
351 g_test_add_data_func (testpath, (void*) otypes[i], widget_property_tests);