Debian Codegraph @

testsuite/gtk/object.c

raw
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, see <http://www.gnu.org/licenses/>.
17 */
18 #include <gtk/gtk.h>
19 #include <string.h>
20
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
27 */
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)); \
31 } while (0)
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) : \
35 __default_value)
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" : \
42 "random")
43 #define MATCH_ANY_VALUE ((void*) 0xf1874c23)
44
45 /* --- property blacklists --- */
46 typedef struct {
47 const char *type_name;
48 const char *name;
49 gconstpointer value;
50 } IgnoreProperty;
51 static const IgnoreProperty*
52 list_ignore_properties (gboolean buglist)
53 {
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 { "GtkInvisible", "screen", (void*) MATCH_ANY_VALUE }, /* cannot create GdkScreen */
67 { "GtkMessageDialog", "has-separator", (void*) MATCH_ANY_VALUE, }, /* property disabled */
68 { "GtkFontSelectionDialog", "has-separator", (void*) MATCH_ANY_VALUE, }, /* property disabled */
69 { "GtkColorSelectionDialog","has-separator", (void*) MATCH_ANY_VALUE, }, /* property disabled */
70 { "GtkColorSelection", "child", NULL, },
71 { "GtkColorSelection", "current-color", (void*) NULL, }, /* not a valid boxed color */
72 { "GtkComboBox", "row-span-column", (void*) MATCH_ANY_VALUE }, /* GtkComboBoxEntry needs a tree model for this */
73 { "GtkComboBox", "column-span-column", (void*) MATCH_ANY_VALUE }, /* GtkComboBoxEntry needs a tree model for this */
74 { "GtkFileChooserButton", "select-multiple", (void*) MATCH_ANY_VALUE }, /* property disabled */
75 { "GtkFileChooserButton", "action", (void*) GTK_FILE_CHOOSER_ACTION_SAVE },
76 { "GtkFileChooserButton", "action", (void*) GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER },
77 { "GtkFileChooserWidget", "select-multiple", (void*) 0x1 }, /* property conflicts */
78 { "GtkFileChooserDialog", "select-multiple", (void*) MATCH_ANY_VALUE }, /* property disabled */
79 { "GtkMenu", "accel-path", (void*) MATCH_ANY_VALUE }, /* has odd restrictions in the setter */
80 { "GtkMenuItem", "accel-path", (void*) MATCH_ANY_VALUE }, /* has odd restrictions in the setter */
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 },
84 { "GtkTreeView", "expander-column", (void*) MATCH_ANY_VALUE }, /* assertion list != NULL */
85 { "GtkWindow", "screen", (void*) MATCH_ANY_VALUE }, /* cannot create GdkScreen */
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
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_schar, 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 else if (G_IS_PARAM_SPEC_OBJECT (pspec))
169 {
170 gpointer object = NULL;
171 if (!G_TYPE_IS_ABSTRACT (pspec->value_type) &&
172 !G_TYPE_IS_INTERFACE (pspec->value_type))
173 {
174 if (g_type_is_a (pspec->value_type, GDK_TYPE_PIXBUF))
175 object = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
176 else if (g_type_is_a (pspec->value_type, GDK_TYPE_PIXBUF_ANIMATION))
177 object = gdk_pixbuf_simple_anim_new (32, 32, 15);
178 else
179 object = g_object_new (pspec->value_type, NULL);
180 g_object_ref_sink (object);
181 g_value_take_object (value, object);
182 }
183 }
184 /* unimplemented:
185 * G_IS_PARAM_SPEC_PARAM
186 * G_IS_PARAM_SPEC_BOXED
187 * G_IS_PARAM_SPEC_POINTER
188 * G_IS_PARAM_SPEC_VALUE_ARRAY
189 */
190 }
191
192 static gpointer
193 value_as_pointer (GValue *value)
194 {
195 if (g_value_fits_pointer (value))
196 return g_value_peek_pointer (value);
197 if (G_VALUE_HOLDS_BOOLEAN (value))
198 return GINT_TO_POINTER(g_value_get_boolean (value));
199 if (G_VALUE_HOLDS_CHAR (value))
200 return (void*) (gssize) g_value_get_schar (value);
201 if (G_VALUE_HOLDS_UCHAR (value))
202 return (void*) (gsize) g_value_get_uchar (value);
203 if (G_VALUE_HOLDS_INT (value))
204 return GINT_TO_POINTER(g_value_get_int (value));
205 if (G_VALUE_HOLDS_UINT (value))
206 return GUINT_TO_POINTER(g_value_get_uint (value));
207 if (G_VALUE_HOLDS_LONG (value))
208 return GSIZE_TO_POINTER ((gssize) g_value_get_long (value));
209 if (G_VALUE_HOLDS_ULONG (value))
210 return GSIZE_TO_POINTER (g_value_get_ulong (value));
211 if (G_VALUE_HOLDS_FLOAT (value))
212 return (void*) (gssize) g_value_get_float (value);
213 if (G_VALUE_HOLDS_DOUBLE (value))
214 return (void*) (gssize) g_value_get_double (value);
215 if (G_VALUE_HOLDS_ENUM (value))
216 return (void*) (gssize) g_value_get_enum (value);
217 if (G_VALUE_HOLDS_FLAGS (value))
218 return (void*) (gsize) g_value_get_flags (value);
219 return (void*) 0x1373babe;
220 }
221
222 static void
223 object_test_property (GObject *object,
224 GParamSpec *pspec,
225 double dvalue)
226 {
227 /* test setting of a normal writable property */
228 if (pspec->flags & G_PARAM_WRITABLE &&
229 !(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
230 {
231 GValue value = G_VALUE_INIT;
232 guint i;
233 const IgnoreProperty *ignore_properties;
234 /* select value to set */
235 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
236 pspec_select_value (pspec, &value, dvalue);
237 /* ignore untestable properties */
238 ignore_properties = list_ignore_properties (FALSE);
239 for (i = 0; ignore_properties[i].name; i++)
240 if (g_strcmp0 ("", ignore_properties[i].name) ||
241 (g_type_is_a (G_OBJECT_TYPE (object), g_type_from_name (ignore_properties[i].type_name)) &&
242 strcmp (pspec->name, ignore_properties[i].name) == 0 &&
243 (MATCH_ANY_VALUE == ignore_properties[i].value ||
244 value_as_pointer (&value) == ignore_properties[i].value ||
245 (G_VALUE_HOLDS_STRING (&value) &&
246 g_strcmp0 (g_value_get_string (&value), ignore_properties[i].value) == 0))))
247 break;
248 /* ignore known property bugs if not testing thoroughly */
249 if (ignore_properties[i].name == NULL && !g_test_thorough ())
250 {
251 ignore_properties = list_ignore_properties (TRUE);
252 for (i = 0; ignore_properties[i].name; i++)
253 if (g_type_is_a (G_OBJECT_TYPE (object), g_type_from_name (ignore_properties[i].type_name)) &&
254 strcmp (pspec->name, ignore_properties[i].name) == 0 &&
255 (MATCH_ANY_VALUE == ignore_properties[i].value ||
256 value_as_pointer (&value) == ignore_properties[i].value ||
257 (G_VALUE_HOLDS_STRING (&value) &&
258 g_strcmp0 (g_value_get_string (&value), ignore_properties[i].value) == 0)))
259 break;
260 }
261 /* assign unignored properties */
262 if (ignore_properties[i].name == NULL)
263 {
264 if (g_test_verbose ())
265 g_print ("PropertyTest: %s::%s := (%s value (%s): %p)\n",
266 g_type_name (G_OBJECT_TYPE (object)), pspec->name,
267 SELECT_NAME (dvalue), g_type_name (G_VALUE_TYPE (&value)),
268 value_as_pointer (&value));
269 g_object_set_property (object, pspec->name, &value);
270 }
271 /* cleanups */
272 g_value_unset (&value);
273 }
274 }
275
276 static void
277 widget_test_properties (GtkWidget *widget,
278 double dvalue)
279 {
280 /* try setting all possible properties, according to dvalue */
281 guint i, n_pspecs = 0;
282 GParamSpec **pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (widget), &n_pspecs);
283 for (i = 0; i < n_pspecs; i++)
284 {
285 GParamSpec *pspec = pspecs[i];
286 if (pspec->flags & G_PARAM_WRITABLE &&
287 !(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
288 object_test_property (G_OBJECT (widget), pspecs[i], dvalue);
289 }
290 g_free (pspecs);
291 }
292
293 static void
294 widget_fixups (GtkWidget *widget)
295 {
296 /* post-constructor for widgets that need additional settings to work correctly */
297 if (GTK_IS_COMBO_BOX_TEXT (widget))
298 {
299 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (widget), "test text");
300 }
301 }
302
303 static void
304 widget_property_tests (gconstpointer test_data)
305 {
306 GType wtype = (GType) test_data;
307 /* create widget */
308 GtkWidget *widget = gtk_widget_new (wtype, NULL);
309 g_object_ref_sink (widget);
310 widget_fixups (widget);
311 /* test property values */
312 widget_test_properties (widget, +2); /* test default_value */
313 widget_test_properties (widget, 0); /* test minimum */
314 widget_test_properties (widget, 0.5); /* test medium */
315 widget_test_properties (widget, 1); /* test maximum */
316 widget_test_properties (widget, -1); /* test random value */
317 /* cleanup */
318 gtk_widget_destroy (widget);
319 g_object_unref (widget);
320 }
321
322 /* --- main test program --- */
323 int
324 main (int argc,
325 char *argv[])
326 {
327 const GType *otypes;
328 guint i;
329 gchar *schema_dir;
330
331 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
332
333 /* initialize test program */
334 gtk_test_init (&argc, &argv);
335 gtk_test_register_all_types ();
336
337 /* g_test_build_filename must be called after gtk_test_init */
338 schema_dir = g_test_build_filename (G_TEST_BUILT, "", NULL);
339 if (g_getenv ("GTK_TEST_MESON") == NULL)
340 g_setenv ("GSETTINGS_SCHEMA_DIR", schema_dir, TRUE);
341
342 /* install a property test for each widget type */
343 otypes = gtk_test_list_all_types (NULL);
344 for (i = 0; otypes[i]; i++)
345 if (g_type_is_a (otypes[i], GTK_TYPE_WIDGET) &&
346 G_TYPE_IS_OBJECT (otypes[i]) &&
347 !G_TYPE_IS_ABSTRACT (otypes[i]))
348 {
349 gchar *testpath = g_strdup_printf ("/properties/%s", g_type_name (otypes[i]));
350 g_test_add_data_func (testpath, (void*) otypes[i], widget_property_tests);
351 g_free (testpath);
352 }
353
354 g_free (schema_dir);
355 return g_test_run ();
356 }