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