]> Pileus Git - ~andy/gtk/blob - tests/defaultvaluetest.c
2.13.0
[~andy/gtk] / tests / defaultvaluetest.c
1 /* Gtk+ default value tests
2  * Copyright (C) 2007 Christian Persch
3  *               2007 Johan Dahlin
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library 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  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library 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 #undef GTK_DISABLE_DEPRECATED
22 #define GTK_ENABLE_BROKEN
23 #include <string.h>
24 #include <gtk/gtk.h>
25 #include <gtk/gtkprintunixdialog.h>
26
27 static void
28 check_property (const char *output,
29                 GParamSpec *pspec,
30                 GValue *value)
31 {
32   GValue default_value = { 0, };
33   char *v, *dv, *msg;
34
35   if (g_param_value_defaults (pspec, value))
36       return;
37
38   g_value_init (&default_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
39   g_param_value_set_default (pspec, &default_value);
40       
41   v = g_strdup_value_contents (value);
42   dv = g_strdup_value_contents (&default_value);
43   
44   msg = g_strdup_printf ("%s %s.%s: %s != %s\n",
45                          output,
46                          g_type_name (pspec->owner_type),
47                          pspec->name,
48                          dv, v);
49   g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__,
50                        G_STRFUNC, msg);
51   g_free (msg);
52   
53   g_free (v);
54   g_free (dv);
55   g_value_unset (&default_value);
56 }
57
58 static void
59 test_type (gconstpointer data)
60 {
61   GObjectClass *klass;
62   GObject *instance;
63   GParamSpec **pspecs;
64   guint n_pspecs, i;
65   GType type;
66
67   type = * (GType *) data;
68
69   if (!G_TYPE_IS_CLASSED (type))
70     return;
71
72   if (G_TYPE_IS_ABSTRACT (type))
73     return;
74
75   if (!g_type_is_a (type, G_TYPE_OBJECT))
76     return;
77
78   /* These can't be freely constructed/destroyed */
79   if (g_type_is_a (type, GTK_TYPE_PRINT_JOB) ||
80       g_type_is_a (type, GDK_TYPE_PIXBUF_LOADER) ||
81       g_type_is_a (type, gdk_pixbuf_simple_anim_iter_get_type ()))
82     return;
83
84   /* The gtk_arg compat wrappers can't set up default values */
85   if (g_type_is_a (type, GTK_TYPE_CLIST) ||
86       g_type_is_a (type, GTK_TYPE_CTREE) ||
87       g_type_is_a (type, GTK_TYPE_LIST) ||
88       g_type_is_a (type, GTK_TYPE_TIPS_QUERY)) 
89     return;
90
91   klass = g_type_class_ref (type);
92   
93   if (g_type_is_a (type, GTK_TYPE_SETTINGS))
94     instance = g_object_ref (gtk_settings_get_default ());
95   else if (g_type_is_a (type, GDK_TYPE_PANGO_RENDERER))
96     instance = g_object_ref (gdk_pango_renderer_get_default (gdk_screen_get_default ()));
97   else if (g_type_is_a (type, GDK_TYPE_PIXMAP))
98     instance = g_object_ref (gdk_pixmap_new (NULL, 1, 1, 1));
99   else if (g_type_is_a (type, GDK_TYPE_COLORMAP))
100     instance = g_object_ref (gdk_colormap_new (gdk_visual_get_best (), TRUE));
101   else if (g_type_is_a (type, GDK_TYPE_WINDOW))
102     {
103       GdkWindowAttr attributes;
104       attributes.window_type = GDK_WINDOW_TEMP;
105       instance = g_object_ref (gdk_window_new (NULL, &attributes, 0));
106     }
107   else
108     instance = g_object_new (type, NULL);
109
110   if (g_type_is_a (type, G_TYPE_INITIALLY_UNOWNED))
111     g_object_ref_sink (instance);
112
113   pspecs = g_object_class_list_properties (klass, &n_pspecs);
114   for (i = 0; i < n_pspecs; ++i)
115     {
116       GParamSpec *pspec = pspecs[i];
117       GValue value = { 0, };
118       
119       if (pspec->owner_type != type)
120         continue;
121
122       if ((pspec->flags & G_PARAM_READABLE) == 0)
123         continue;
124
125       if (g_type_is_a (type, GDK_TYPE_DISPLAY_MANAGER) &&
126           (strcmp (pspec->name, "default-display") == 0))
127         continue;
128
129       if (g_type_is_a (type, GDK_TYPE_PANGO_RENDERER) &&
130           (strcmp (pspec->name, "screen") == 0))
131         continue;
132
133       if (g_type_is_a (type, GTK_TYPE_ABOUT_DIALOG) &&
134           (strcmp (pspec->name, "program-name") == 0))
135         continue;
136       
137       /* These are set to the current date */
138       if (g_type_is_a (type, GTK_TYPE_CALENDAR) &&
139           (strcmp (pspec->name, "year") == 0 ||
140            strcmp (pspec->name, "month") == 0 ||
141            strcmp (pspec->name, "day") == 0))
142         continue;
143
144       if (g_type_is_a (type, GTK_TYPE_CELL_RENDERER_TEXT) &&
145           (strcmp (pspec->name, "background-gdk") == 0 ||
146            strcmp (pspec->name, "foreground-gdk") == 0 ||
147            strcmp (pspec->name, "font") == 0 ||
148            strcmp (pspec->name, "font-desc") == 0))
149         continue;
150
151       if (g_type_is_a (type, GTK_TYPE_CELL_VIEW) &&
152           (strcmp (pspec->name, "background-gdk") == 0 ||
153            strcmp (pspec->name, "foreground-gdk") == 0))
154         continue;
155
156       if (g_type_is_a (type, GTK_TYPE_COLOR_BUTTON) &&
157           strcmp (pspec->name, "color") == 0)
158         continue;
159
160       if (g_type_is_a (type, GTK_TYPE_COLOR_SELECTION) &&
161           strcmp (pspec->name, "current-color") == 0)
162         continue;
163
164       /* Gets set to the cwd */
165       if (g_type_is_a (type, GTK_TYPE_FILE_SELECTION) &&
166           strcmp (pspec->name, "filename") == 0)
167         continue;
168
169       if (g_type_is_a (type, GTK_TYPE_FONT_SELECTION) &&
170           strcmp (pspec->name, "font") == 0)
171         continue;
172
173       if (g_type_is_a (type, GTK_TYPE_LAYOUT) &&
174           (strcmp (pspec->name, "hadjustment") == 0 ||
175            strcmp (pspec->name, "vadjustment") == 0))
176         continue;
177
178       if (g_type_is_a (type, GTK_TYPE_MESSAGE_DIALOG) &&
179           strcmp (pspec->name, "image") == 0)
180         continue;
181
182       if (g_type_is_a (type, GTK_TYPE_PRINT_OPERATION) &&
183           strcmp (pspec->name, "job-name") == 0)
184         continue;
185
186       if (g_type_is_a (type, GTK_TYPE_PRINT_UNIX_DIALOG) &&
187           (strcmp (pspec->name, "page-setup") == 0 ||
188            strcmp (pspec->name, "print-settings") == 0))
189         continue;
190
191       if (g_type_is_a (type, GTK_TYPE_PROGRESS_BAR) &&
192           strcmp (pspec->name, "adjustment") == 0)
193         continue;
194
195       /* filename value depends on $HOME */
196       if (g_type_is_a (type, GTK_TYPE_RECENT_MANAGER) &&
197           (strcmp (pspec->name, "filename") == 0 ||
198            strcmp (pspec->name, "size") == 0))
199         continue;
200
201       if (g_type_is_a (type, GTK_TYPE_SCALE_BUTTON) &&
202           strcmp (pspec->name, "adjustment") == 0)
203         continue;
204
205       if (g_type_is_a (type, GTK_TYPE_SCROLLED_WINDOW) &&
206           (strcmp (pspec->name, "hadjustment") == 0 ||
207            strcmp (pspec->name, "vadjustment") == 0))
208         continue;
209
210       /* these defaults come from XResources */
211       if (g_type_is_a (type, GTK_TYPE_SETTINGS) &&
212           strncmp (pspec->name, "gtk-xft-", 8) == 0)
213         continue;
214
215       if (g_type_is_a (type, GTK_TYPE_SETTINGS) &&
216           (strcmp (pspec->name, "color-hash") == 0 ||
217            strcmp (pspec->name, "gtk-cursor-theme-name") == 0 ||
218            strcmp (pspec->name, "gtk-cursor-theme-size") == 0 ||
219            strcmp (pspec->name, "gtk-double-click-time") == 0 ||
220            strcmp (pspec->name, "gtk-file-chooser-backend") == 0 ||
221            strcmp (pspec->name, "gtk-icon-theme-name") == 0 ||
222            strcmp (pspec->name, "gtk-fallback-icon-theme") == 0 ||
223            strcmp (pspec->name, "gtk-key-theme-name") == 0 ||
224            strcmp (pspec->name, "gtk-theme-name") == 0))
225         continue;
226
227       if (g_type_is_a (type, GTK_TYPE_SPIN_BUTTON) &&
228           (strcmp (pspec->name, "adjustment") == 0))
229         continue;
230
231       if (g_type_is_a (type, GTK_TYPE_STATUS_ICON) &&
232           (strcmp (pspec->name, "size") == 0 ||
233            strcmp (pspec->name, "screen") == 0))
234         continue;
235
236       if (g_type_is_a (type, GTK_TYPE_TEXT_BUFFER) &&
237           (strcmp (pspec->name, "tag-table") == 0 ||
238            strcmp (pspec->name, "copy-target-list") == 0 ||
239            strcmp (pspec->name, "paste-target-list") == 0))
240         continue;
241
242       /* language depends on the current locale */
243       if (g_type_is_a (type, GTK_TYPE_TEXT_TAG) &&
244           (strcmp (pspec->name, "background-gdk") == 0 ||
245            strcmp (pspec->name, "foreground-gdk") == 0 ||
246            strcmp (pspec->name, "language") == 0 ||
247            strcmp (pspec->name, "font") == 0 ||
248            strcmp (pspec->name, "font-desc") == 0))
249         continue;
250
251       if (g_type_is_a (type, GTK_TYPE_TEXT) &&
252           (strcmp (pspec->name, "hadjustment") == 0 ||
253            strcmp (pspec->name, "vadjustment") == 0))
254         continue;
255
256       if (g_type_is_a (type, GTK_TYPE_TEXT_VIEW) &&
257           strcmp (pspec->name, "buffer") == 0)
258         continue;
259
260       if (g_type_is_a (type, GTK_TYPE_TREE_VIEW) &&
261           (strcmp (pspec->name, "hadjustment") == 0 ||
262            strcmp (pspec->name, "vadjustment") == 0))
263         continue;
264
265       if (g_type_is_a (type, GTK_TYPE_VIEWPORT) &&
266           (strcmp (pspec->name, "hadjustment") == 0 ||
267            strcmp (pspec->name, "vadjustment") == 0))
268         continue;
269
270       if (g_type_is_a (type, GTK_TYPE_WIDGET) &&
271           (strcmp (pspec->name, "name") == 0 ||
272            strcmp (pspec->name, "screen") == 0 ||
273            strcmp (pspec->name, "style") == 0))
274         continue;
275
276       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
277       g_object_get_property (instance, pspec->name, &value);
278       check_property ("Property", pspec, &value);
279       g_value_unset (&value);
280     }
281   g_free (pspecs);
282
283   if (g_type_is_a (type, GTK_TYPE_WIDGET))
284     {
285       pspecs = gtk_widget_class_list_style_properties (GTK_WIDGET_CLASS (klass), &n_pspecs);
286       
287       for (i = 0; i < n_pspecs; ++i)
288         {
289           GParamSpec *pspec = pspecs[i];
290           GValue value = { 0, };
291           
292           if (pspec->owner_type != type)
293             continue;
294
295           if ((pspec->flags & G_PARAM_READABLE) == 0)
296             continue;
297           
298           g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
299           gtk_widget_style_get_property (GTK_WIDGET (instance), pspec->name, &value);
300           check_property ("Style property", pspec, &value);
301           g_value_unset (&value);
302         }
303
304       g_free (pspecs);
305     }
306   
307   if (g_type_is_a (type, GDK_TYPE_WINDOW))
308     gdk_window_destroy (GDK_WINDOW (instance));
309   else
310     g_object_unref (instance);
311   
312   g_type_class_unref (klass);
313 }
314
315 extern void pixbuf_init (void);
316
317 int
318 main (int argc, char **argv)
319 {
320   const GType *otypes;
321   guint i;
322
323   gtk_test_init (&argc, &argv);
324   pixbuf_init ();
325   gtk_test_register_all_types();
326   
327   otypes = gtk_test_list_all_types (NULL);
328   for (i = 0; otypes[i]; i++)
329     {
330       gchar *testname;
331       
332       testname = g_strdup_printf ("/Default Values/%s",
333                                   g_type_name (otypes[i]));
334       g_test_add_data_func (testname,
335                             &otypes[i],
336                             test_type);
337       g_free (testname);
338     }
339   
340   return g_test_run();
341 }