1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include <glib-object.h>
22 #include "testcommon.h"
23 #include "testmodule.h"
25 /* This test tests the macros for defining dynamic types */
27 static gboolean loaded = FALSE;
29 struct _TestIfaceClass
31 GTypeInterface base_iface;
35 static GType test_iface_get_type (void);
37 #define TEST_TYPE_IFACE (test_iface_get_type ())
38 #define TEST_IFACE_GET_CLASS(obj) \
39 (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
41 typedef struct _TestIface TestIface;
42 typedef struct _TestIfaceClass TestIfaceClass;
44 static void test_iface_base_init (TestIfaceClass *iface);
45 static void test_iface_default_init (TestIfaceClass *iface, gpointer class_data);
47 static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, test_iface_default_init)
50 test_iface_default_init (TestIfaceClass *iface,
56 test_iface_base_init (TestIfaceClass *iface)
60 GType dynamic_object_get_type (void);
61 #define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
63 typedef GObject DynamicObject;
64 typedef struct _DynamicObjectClass DynamicObjectClass;
66 struct _DynamicObjectClass
68 GObjectClass parent_class;
72 static void dynamic_object_iface_init (TestIface *iface);
74 G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynamicObject, dynamic_object, G_TYPE_OBJECT, 0,
75 G_IMPLEMENT_INTERFACE_DYNAMIC (TEST_TYPE_IFACE,
76 dynamic_object_iface_init));
79 dynamic_object_class_init (DynamicObjectClass *class)
86 dynamic_object_class_finalize (DynamicObjectClass *class)
92 dynamic_object_iface_init (TestIface *iface)
97 dynamic_object_init (DynamicObject *dynamic_object)
102 module_register (GTypeModule *module)
104 dynamic_object_register_type (module);
108 test_dynamic_type (void)
110 DynamicObjectClass *class;
112 /* Not loaded until we call ref for the first time */
113 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
114 g_assert_null (class);
115 g_assert_false (loaded);
117 /* Make sure interfaces work */
118 g_assert_true (g_type_is_a (DYNAMIC_OBJECT_TYPE,
122 class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
123 g_assert_nonnull (class);
124 g_assert_cmpint (class->val, ==, 42);
125 g_assert_true (loaded);
127 /* Peek then works */
128 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
129 g_assert_nonnull (class);
130 g_assert_cmpint (class->val, ==, 42);
131 g_assert_true (loaded);
133 /* Make sure interfaces still work */
134 g_assert_true (g_type_is_a (DYNAMIC_OBJECT_TYPE,
137 /* Unref causes finalize */
138 g_type_class_unref (class);
140 /* Peek returns NULL */
141 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
143 /* Disabled as unloading dynamic types is disabled.
144 * See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
145 g_assert_false (class);
146 g_assert_false (loaded);
150 class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
151 g_assert_nonnull (class);
152 g_assert_cmpint (class->val, ==, 42);
153 g_assert_true (loaded);
155 /* And Unref causes finalize once more*/
156 g_type_class_unref (class);
157 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
159 /* Disabled as unloading dynamic types is disabled.
160 * See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
161 g_assert_null (class);
162 g_assert_false (loaded);
167 test_dynamic_type_query (void)
169 DynamicObjectClass *class;
170 GTypeQuery query_result;
172 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/623");
174 class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
175 g_assert_nonnull (class);
177 g_type_query (DYNAMIC_OBJECT_TYPE, &query_result);
179 g_assert_cmpuint (query_result.type, !=, 0);
180 g_assert_cmpstr (query_result.type_name, ==, "DynamicObject");
181 g_assert_cmpuint (query_result.class_size, >=, sizeof (DynamicObjectClass));
182 g_assert_cmpuint (query_result.instance_size, >=, sizeof (DynamicObject));
184 g_type_class_unref (class);
191 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
192 G_LOG_LEVEL_WARNING |
193 G_LOG_LEVEL_CRITICAL);
195 g_test_init (&argc, &argv, NULL);
197 test_module_new (module_register);
199 g_test_add_func ("/gobject/dynamic-type", test_dynamic_type);
200 g_test_add_func ("/gobject/dynamic-type/query", test_dynamic_type_query);
202 return g_test_run ();