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