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