1 /* GLib testing framework examples and tests
2 * Copyright (C) 2022 Red Hat, Inc.
4 * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
10 * This work 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.
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
26 #include <glib-object.h>
29 GObject parent_instance;
37 GObjectClass parent_class;
48 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
50 static GType test_object_get_type (void);
51 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
54 test_object_set_foo (TestObject *obj,
61 g_assert (properties[PROP_FOO] != NULL);
62 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
67 test_object_set_bar (TestObject *obj,
76 g_assert (properties[PROP_BAR] != NULL);
77 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAR]);
82 test_object_set_baz (TestObject *obj,
85 if (g_strcmp0 (obj->baz, baz) != 0)
88 obj->baz = g_strdup (baz);
90 g_assert (properties[PROP_BAZ] != NULL);
91 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAZ]);
96 test_object_set_quux (TestObject *obj,
99 if (g_strcmp0 (obj->quux, quux) != 0)
102 obj->quux = g_strdup (quux);
104 g_assert (properties[PROP_QUUX] != NULL);
105 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_QUUX]);
110 test_object_finalize (GObject *gobject)
112 TestObject *self = (TestObject *) gobject;
117 G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
121 test_object_constructor (GType type,
122 guint n_construct_properties,
123 GObjectConstructParam *construct_properties)
125 return G_OBJECT_CLASS (test_object_parent_class)->constructor (type, n_construct_properties, construct_properties);
129 test_object_set_property (GObject *gobject,
134 TestObject *tobj = (TestObject *) gobject;
136 g_assert_cmpint (prop_id, !=, 0);
137 g_assert_true (prop_id < N_PROPERTIES && pspec == properties[prop_id]);
139 switch ((TestObjectProperty)prop_id)
142 test_object_set_foo (tobj, g_value_get_int (value));
146 test_object_set_bar (tobj, g_value_get_boolean (value));
150 test_object_set_baz (tobj, g_value_get_string (value));
154 test_object_set_quux (tobj, g_value_get_string (value));
158 g_assert_not_reached ();
163 test_object_get_property (GObject *gobject,
168 TestObject *tobj = (TestObject *) gobject;
170 g_assert_cmpint (prop_id, !=, 0);
171 g_assert_true (prop_id < N_PROPERTIES && pspec == properties[prop_id]);
173 switch ((TestObjectProperty)prop_id)
176 g_value_set_int (value, tobj->foo);
180 g_value_set_boolean (value, tobj->bar);
184 g_value_set_string (value, tobj->baz);
188 g_value_set_string (value, tobj->quux);
192 g_assert_not_reached ();
197 test_object_class_init (TestObjectClass *klass)
199 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
201 properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
204 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
205 properties[PROP_BAR] = g_param_spec_boolean ("bar", "Bar", "Bar",
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
208 properties[PROP_BAZ] = g_param_spec_string ("baz", "Baz", "Baz",
210 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
212 properties[PROP_QUUX] = g_param_spec_string ("quux", "quux", "quux",
214 G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
216 gobject_class->constructor = test_object_constructor;
217 gobject_class->set_property = test_object_set_property;
218 gobject_class->get_property = test_object_get_property;
219 gobject_class->finalize = test_object_finalize;
221 g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
225 test_object_init (TestObject *self)
229 self->baz = g_strdup ("Hello");
233 test_notify_in_init (void)
237 g_test_summary ("Test that notify freezing during construction of objects with custom constructor works");
238 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2665");
240 obj = g_object_new (test_object_get_type (), "bar", FALSE, NULL);
242 g_object_unref (obj);
246 main (int argc, char *argv[])
248 g_test_init (&argc, &argv, NULL);
250 g_test_add_func ("/properties/notify-in-init2", test_notify_in_init);
252 return g_test_run ();