]> Pileus Git - ~andy/gtk/blob - gtk/tests/defaultvalue.c
Fix memleak in filter model unit test
[~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 #undef GTK_DISABLE_DEPRECATED
22 #define GTK_ENABLE_BROKEN
23 #include <string.h>
24 #include <gtk/gtk.h>
25 #include <gtk/gtkunixprint.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       attributes.event_mask = 0;
106       attributes.width = 100;
107       attributes.height = 100;
108       instance = g_object_ref (gdk_window_new (NULL, &attributes, 0));
109     }
110   else
111     instance = g_object_new (type, NULL);
112
113   if (g_type_is_a (type, G_TYPE_INITIALLY_UNOWNED))
114     g_object_ref_sink (instance);
115
116   pspecs = g_object_class_list_properties (klass, &n_pspecs);
117   for (i = 0; i < n_pspecs; ++i)
118     {
119       GParamSpec *pspec = pspecs[i];
120       GValue value = { 0, };
121       
122       if (pspec->owner_type != type)
123         continue;
124
125       if ((pspec->flags & G_PARAM_READABLE) == 0)
126         continue;
127
128       if (g_type_is_a (type, GDK_TYPE_DISPLAY_MANAGER) &&
129           (strcmp (pspec->name, "default-display") == 0))
130         continue;
131
132       if (g_type_is_a (type, GDK_TYPE_PANGO_RENDERER) &&
133           (strcmp (pspec->name, "screen") == 0))
134         continue;
135
136       if (g_type_is_a (type, GTK_TYPE_ABOUT_DIALOG) &&
137           (strcmp (pspec->name, "program-name") == 0))
138         continue;
139       
140       /* These are set to the current date */
141       if (g_type_is_a (type, GTK_TYPE_CALENDAR) &&
142           (strcmp (pspec->name, "year") == 0 ||
143            strcmp (pspec->name, "month") == 0 ||
144            strcmp (pspec->name, "day") == 0))
145         continue;
146
147       if (g_type_is_a (type, GTK_TYPE_CELL_RENDERER_TEXT) &&
148           (strcmp (pspec->name, "background-gdk") == 0 ||
149            strcmp (pspec->name, "foreground-gdk") == 0 ||
150            strcmp (pspec->name, "font") == 0 ||
151            strcmp (pspec->name, "font-desc") == 0))
152         continue;
153
154       if (g_type_is_a (type, GTK_TYPE_CELL_VIEW) &&
155           (strcmp (pspec->name, "background-gdk") == 0 ||
156            strcmp (pspec->name, "foreground-gdk") == 0))
157         continue;
158
159       if (g_type_is_a (type, GTK_TYPE_COLOR_BUTTON) &&
160           strcmp (pspec->name, "color") == 0)
161         continue;
162
163       if (g_type_is_a (type, GTK_TYPE_COLOR_SELECTION) &&
164           strcmp (pspec->name, "current-color") == 0)
165         continue;
166
167       if (g_type_is_a (type, GTK_TYPE_COLOR_SELECTION_DIALOG) &&
168           (strcmp (pspec->name, "color-selection") == 0 ||
169            strcmp (pspec->name, "ok-button") == 0 ||
170            strcmp (pspec->name, "help-button") == 0 ||
171            strcmp (pspec->name, "cancel-button") == 0))
172         continue;
173
174       /* Default invisible char is determined at runtime */
175       if (g_type_is_a (type, GTK_TYPE_ENTRY) &&
176           strcmp (pspec->name, "invisible-char") == 0 ||
177           strcmp (pspec->name, "buffer") == 0)
178         continue;
179
180       /* Gets set to the cwd */
181       if (g_type_is_a (type, GTK_TYPE_FILE_SELECTION) &&
182           strcmp (pspec->name, "filename") == 0)
183         continue;
184
185       if (g_type_is_a (type, GTK_TYPE_FONT_SELECTION) &&
186           strcmp (pspec->name, "font") == 0)
187         continue;
188
189       if (g_type_is_a (type, GTK_TYPE_LAYOUT) &&
190           (strcmp (pspec->name, "hadjustment") == 0 ||
191            strcmp (pspec->name, "vadjustment") == 0))
192         continue;
193
194       if (g_type_is_a (type, GTK_TYPE_MESSAGE_DIALOG) &&
195           strcmp (pspec->name, "image") == 0)
196         continue;
197
198       if (g_type_is_a (type, GTK_TYPE_PRINT_OPERATION) &&
199           strcmp (pspec->name, "job-name") == 0)
200         continue;
201
202       if (g_type_is_a (type, GTK_TYPE_PRINT_UNIX_DIALOG) &&
203           (strcmp (pspec->name, "page-setup") == 0 ||
204            strcmp (pspec->name, "print-settings") == 0))
205         continue;
206
207       if (g_type_is_a (type, GTK_TYPE_PROGRESS_BAR) &&
208           strcmp (pspec->name, "adjustment") == 0)
209         continue;
210
211       /* filename value depends on $HOME */
212       if (g_type_is_a (type, GTK_TYPE_RECENT_MANAGER) &&
213           (strcmp (pspec->name, "filename") == 0 ||
214            strcmp (pspec->name, "size") == 0))
215         continue;
216
217       if (g_type_is_a (type, GTK_TYPE_SCALE_BUTTON) &&
218           strcmp (pspec->name, "adjustment") == 0)
219         continue;
220
221       if (g_type_is_a (type, GTK_TYPE_SCROLLED_WINDOW) &&
222           (strcmp (pspec->name, "hadjustment") == 0 ||
223            strcmp (pspec->name, "vadjustment") == 0))
224         continue;
225
226       /* these defaults come from XResources */
227       if (g_type_is_a (type, GTK_TYPE_SETTINGS) &&
228           strncmp (pspec->name, "gtk-xft-", 8) == 0)
229         continue;
230
231       if (g_type_is_a (type, GTK_TYPE_SETTINGS) &&
232           (strcmp (pspec->name, "color-hash") == 0 ||
233            strcmp (pspec->name, "gtk-cursor-theme-name") == 0 ||
234            strcmp (pspec->name, "gtk-cursor-theme-size") == 0 ||
235            strcmp (pspec->name, "gtk-dnd-drag-threshold") == 0 ||
236            strcmp (pspec->name, "gtk-double-click-time") == 0 ||
237            strcmp (pspec->name, "gtk-fallback-icon-theme") == 0 ||
238            strcmp (pspec->name, "gtk-file-chooser-backend") == 0 ||
239            strcmp (pspec->name, "gtk-icon-theme-name") == 0 ||
240            strcmp (pspec->name, "gtk-im-module") == 0 ||
241            strcmp (pspec->name, "gtk-key-theme-name") == 0 ||
242            strcmp (pspec->name, "gtk-theme-name") == 0))
243         continue;
244
245       if (g_type_is_a (type, GTK_TYPE_SPIN_BUTTON) &&
246           (strcmp (pspec->name, "adjustment") == 0))
247         continue;
248
249       if (g_type_is_a (type, GTK_TYPE_STATUS_ICON) &&
250           (strcmp (pspec->name, "size") == 0 ||
251            strcmp (pspec->name, "screen") == 0))
252         continue;
253
254       if (g_type_is_a (type, GTK_TYPE_TEXT_BUFFER) &&
255           (strcmp (pspec->name, "tag-table") == 0 ||
256            strcmp (pspec->name, "copy-target-list") == 0 ||
257            strcmp (pspec->name, "paste-target-list") == 0))
258         continue;
259
260       /* language depends on the current locale */
261       if (g_type_is_a (type, GTK_TYPE_TEXT_TAG) &&
262           (strcmp (pspec->name, "background-gdk") == 0 ||
263            strcmp (pspec->name, "foreground-gdk") == 0 ||
264            strcmp (pspec->name, "language") == 0 ||
265            strcmp (pspec->name, "font") == 0 ||
266            strcmp (pspec->name, "font-desc") == 0))
267         continue;
268
269       if (g_type_is_a (type, GTK_TYPE_TEXT) &&
270           (strcmp (pspec->name, "hadjustment") == 0 ||
271            strcmp (pspec->name, "vadjustment") == 0))
272         continue;
273
274       if (g_type_is_a (type, GTK_TYPE_TEXT_VIEW) &&
275           strcmp (pspec->name, "buffer") == 0)
276         continue;
277
278       if (g_type_is_a (type, GTK_TYPE_TREE_VIEW) &&
279           (strcmp (pspec->name, "hadjustment") == 0 ||
280            strcmp (pspec->name, "vadjustment") == 0))
281         continue;
282
283       if (g_type_is_a (type, GTK_TYPE_VIEWPORT) &&
284           (strcmp (pspec->name, "hadjustment") == 0 ||
285            strcmp (pspec->name, "vadjustment") == 0))
286         continue;
287
288       if (g_type_is_a (type, GTK_TYPE_WIDGET) &&
289           (strcmp (pspec->name, "name") == 0 ||
290            strcmp (pspec->name, "screen") == 0 ||
291            strcmp (pspec->name, "style") == 0))
292         continue;
293
294       if (g_test_verbose ())
295       g_print ("Property %s.%s\n", 
296              g_type_name (pspec->owner_type),
297              pspec->name);
298       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
299       g_object_get_property (instance, pspec->name, &value);
300       check_property ("Property", pspec, &value);
301       g_value_unset (&value);
302     }
303   g_free (pspecs);
304
305   if (g_type_is_a (type, GTK_TYPE_WIDGET))
306     {
307       pspecs = gtk_widget_class_list_style_properties (GTK_WIDGET_CLASS (klass), &n_pspecs);
308       
309       for (i = 0; i < n_pspecs; ++i)
310         {
311           GParamSpec *pspec = pspecs[i];
312           GValue value = { 0, };
313           
314           if (pspec->owner_type != type)
315             continue;
316
317           if ((pspec->flags & G_PARAM_READABLE) == 0)
318             continue;
319           
320           g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
321           gtk_widget_style_get_property (GTK_WIDGET (instance), pspec->name, &value);
322           check_property ("Style property", pspec, &value);
323           g_value_unset (&value);
324         }
325
326       g_free (pspecs);
327     }
328   
329   if (g_type_is_a (type, GDK_TYPE_WINDOW))
330     gdk_window_destroy (GDK_WINDOW (instance));
331   else
332     g_object_unref (instance);
333   
334   g_type_class_unref (klass);
335 }
336
337 extern void pixbuf_init (void);
338
339 int
340 main (int argc, char **argv)
341 {
342   const GType *otypes;
343   guint i;
344
345   gtk_test_init (&argc, &argv);
346   pixbuf_init ();
347   gtk_test_register_all_types();
348   
349   otypes = gtk_test_list_all_types (NULL);
350   for (i = 0; otypes[i]; i++)
351     {
352       gchar *testname;
353       
354       testname = g_strdup_printf ("/Default Values/%s",
355                                   g_type_name (otypes[i]));
356       g_test_add_data_func (testname,
357                             &otypes[i],
358                             test_type);
359       g_free (testname);
360     }
361   
362   return g_test_run();
363 }