Debian Codegraph @

testsuite/gtk/objects-finalize.c

raw
1 /* objects-finalize.c
2 * Copyright (C) 2013 Openismus GmbH
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Tristan Van Berkom <[email protected]>
18 */
19 #include <gtk/gtk.h>
20 #include <string.h>
21
22 #ifdef GDK_WINDOWING_X11
23 # include <gdk/gdkx.h>
24 #endif
25
26
27 typedef GType (*GTypeGetFunc) (void);
28
29 static gboolean finalized = FALSE;
30
31 static gboolean
32 main_loop_quit_cb (gpointer data)
33 {
34 gtk_main_quit ();
35
36 g_assert (finalized);
37 return FALSE;
38 }
39
40 static void
41 check_finalized (gpointer data,
42 GObject *where_the_object_was)
43 {
44 gboolean *did_finalize = (gboolean *)data;
45
46 *did_finalize = TRUE;
47 }
48
49 static void
50 test_finalize_object (gconstpointer data)
51 {
52 GType test_type = GPOINTER_TO_SIZE (data);
53 GObject *object;
54
55 object = g_object_new (test_type, NULL);
56 g_assert (G_IS_OBJECT (object));
57
58 /* Make sure we have the only reference */
59 if (g_object_is_floating (object))
60 g_object_ref_sink (object);
61
62 /* Assert that the object finalizes properly */
63 g_object_weak_ref (object, check_finalized, &finalized);
64
65 /* Toplevels are owned by GTK+, just tell GTK+ to destroy it */
66 if (GTK_IS_WINDOW (object) || GTK_IS_INVISIBLE (object))
67 gtk_widget_destroy (GTK_WIDGET (object));
68 else
69 g_object_unref (object);
70
71 /* Even if the object did finalize, it may have left some dangerous stuff in the GMainContext */
72 g_timeout_add (50, main_loop_quit_cb, NULL);
73 gtk_main();
74 }
75
76 int
77 main (int argc, char **argv)
78 {
79 const GType *all_types;
80 guint n_types = 0, i;
81 gchar *schema_dir;
82 int result;
83
84 /* These must be set before before gtk_test_init */
85 g_setenv ("GIO_USE_VFS", "local", TRUE);
86 g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
87
88 /* initialize test program */
89 gtk_test_init (&argc, &argv);
90 gtk_test_register_all_types ();
91
92 /* g_test_build_filename must be called after gtk_test_init */
93 schema_dir = g_test_build_filename (G_TEST_BUILT, "", NULL);
94 if (g_getenv ("GTK_TEST_MESON") == NULL)
95 g_setenv ("GSETTINGS_SCHEMA_DIR", schema_dir, TRUE);
96
97 all_types = gtk_test_list_all_types (&n_types);
98
99 for (i = 0; i < n_types; i++)
100 {
101 if (g_type_is_a (all_types[i], G_TYPE_OBJECT) &&
102 G_TYPE_IS_INSTANTIATABLE (all_types[i]) &&
103 !G_TYPE_IS_ABSTRACT (all_types[i]) &&
104 #ifdef GDK_WINDOWING_X11
105 all_types[i] != GDK_TYPE_X11_WINDOW &&
106 all_types[i] != GDK_TYPE_X11_CURSOR &&
107 all_types[i] != GDK_TYPE_X11_SCREEN &&
108 all_types[i] != GDK_TYPE_X11_DISPLAY &&
109 all_types[i] != GDK_TYPE_X11_DEVICE_MANAGER_CORE &&
110 all_types[i] != GDK_TYPE_X11_DEVICE_MANAGER_XI2 &&
111 all_types[i] != GDK_TYPE_X11_DISPLAY_MANAGER &&
112 all_types[i] != GDK_TYPE_X11_GL_CONTEXT &&
113 #endif
114 /* Not allowed to finalize a GdkPixbufLoader without calling gdk_pixbuf_loader_close() */
115 all_types[i] != GDK_TYPE_PIXBUF_LOADER &&
116 all_types[i] != GDK_TYPE_DRAWING_CONTEXT &&
117 all_types[i] != gdk_pixbuf_simple_anim_iter_get_type())
118 {
119 gchar *test_path = g_strdup_printf ("/FinalizeObject/%s", g_type_name (all_types[i]));
120
121 g_test_add_data_func (test_path, GSIZE_TO_POINTER (all_types[i]), test_finalize_object);
122
123 g_free (test_path);
124 }
125 }
126
127 result = g_test_run();
128
129 g_free (schema_dir);
130
131 return result;
132 }