1 /* custom-dispatch.c: Test GObjectClass.dispatch_properties_changed
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.
24 #include <glib-object.h>
27 GObject parent_instance;
32 GObjectClass parent_class;
40 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
42 static GType test_object_get_type (void);
43 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
46 test_object_set_foo (TestObject *obj,
53 g_assert (properties[PROP_FOO] != NULL);
54 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
59 test_object_set_property (GObject *gobject,
64 TestObject *tobj = (TestObject *) gobject;
66 switch ((TestObjectProperty)prop_id)
69 test_object_set_foo (tobj, g_value_get_int (value));
73 g_assert_not_reached ();
78 test_object_get_property (GObject *gobject,
83 TestObject *tobj = (TestObject *) gobject;
85 switch ((TestObjectProperty)prop_id)
88 g_value_set_int (value, tobj->foo);
92 g_assert_not_reached ();
96 static int dispatch_properties_called;
99 test_object_dispatch_properties_changed (GObject *object,
103 dispatch_properties_called++;
105 G_OBJECT_CLASS (test_object_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
110 test_object_class_init (TestObjectClass *klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
118 G_PARAM_STATIC_STRINGS |
119 G_PARAM_EXPLICIT_NOTIFY);
121 gobject_class->set_property = test_object_set_property;
122 gobject_class->get_property = test_object_get_property;
123 gobject_class->dispatch_properties_changed = test_object_dispatch_properties_changed;
125 g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
129 test_object_init (TestObject *self)
135 object_has_notify_signal_handlers (gpointer instance)
137 guint signal_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
139 return g_signal_handler_find (instance, G_SIGNAL_MATCH_ID, signal_id, 0, NULL, NULL, NULL) != 0;
143 test_custom_dispatch_init (void)
147 g_test_summary ("Test that custom dispatch_properties_changed is called "
148 "on initialization");
150 dispatch_properties_called = 0;
151 obj = g_object_new (test_object_get_type (), "foo", 5, NULL);
153 g_assert_false (object_has_notify_signal_handlers (obj));
155 g_assert_cmpint (dispatch_properties_called, ==, 1);
156 g_object_set (obj, "foo", 11, NULL);
157 g_assert_cmpint (dispatch_properties_called, ==, 2);
159 g_object_unref (obj);
162 /* This instance init behavior is the thing we are testing:
164 * 1. Don't connect any notify handlers
165 * 2. Change the the foo property
166 * 3. Verify that our custom dispatch_properties_changed is called
169 test_custom_dispatch_set (void)
173 g_test_summary ("Test that custom dispatch_properties_changed is called regardless of connected notify handlers");
175 dispatch_properties_called = 0;
176 obj = g_object_new (test_object_get_type (), NULL);
178 g_assert_false (object_has_notify_signal_handlers (obj));
180 g_assert_cmpint (dispatch_properties_called, ==, 0);
181 g_object_set (obj, "foo", 11, NULL);
182 g_assert_cmpint (dispatch_properties_called, ==, 1);
183 g_object_set (obj, "foo", 11, NULL);
184 g_assert_cmpint (dispatch_properties_called, ==, 1);
186 g_object_unref (obj);
190 main (int argc, char *argv[])
192 g_test_init (&argc, &argv, NULL);
194 g_test_add_func ("/properties/custom-dispatch/init", test_custom_dispatch_init);
195 g_test_add_func ("/properties/custom-dispatch/set", test_custom_dispatch_set);
197 return g_test_run ();