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