tests/protocol: redo a bit
[glib.git] / gobject / tests / properties.c
blob27c2a2258715ede320f0eb0c323b8791acb86b2a
1 #include <stdlib.h>
2 #include <gstdio.h>
3 #include <glib-object.h>
5 typedef struct _TestObject {
6 GObject parent_instance;
7 gint foo;
8 gboolean bar;
9 gchar *baz;
10 } TestObject;
12 typedef struct _TestObjectClass {
13 GObjectClass parent_class;
14 } TestObjectClass;
16 enum { PROP_0, PROP_FOO, PROP_BAR, PROP_BAZ, N_PROPERTIES };
18 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
20 static GType test_object_get_type (void);
21 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
23 static void
24 test_object_set_foo (TestObject *obj,
25 gint foo)
27 if (obj->foo != foo)
29 obj->foo = foo;
31 g_assert (properties[PROP_FOO] != NULL);
32 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
36 static void
37 test_object_set_bar (TestObject *obj,
38 gboolean bar)
40 bar = !!bar;
42 if (obj->bar != bar)
44 obj->bar = bar;
46 g_assert (properties[PROP_BAR] != NULL);
47 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAR]);
51 static void
52 test_object_set_baz (TestObject *obj,
53 const gchar *baz)
55 if (g_strcmp0 (obj->baz, baz) != 0)
57 g_free (obj->baz);
58 obj->baz = g_strdup (baz);
60 g_assert (properties[PROP_BAZ] != NULL);
61 g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAZ]);
65 static void
66 test_object_finalize (GObject *gobject)
68 g_free (((TestObject *) gobject)->baz);
70 G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
73 static void
74 test_object_set_property (GObject *gobject,
75 guint prop_id,
76 const GValue *value,
77 GParamSpec *pspec)
79 TestObject *tobj = (TestObject *) gobject;
81 g_assert_cmpint (prop_id, !=, 0);
82 g_assert_cmpint (prop_id, !=, N_PROPERTIES);
83 g_assert (pspec == properties[prop_id]);
85 switch (prop_id)
87 case PROP_FOO:
88 test_object_set_foo (tobj, g_value_get_int (value));
89 break;
91 case PROP_BAR:
92 test_object_set_bar (tobj, g_value_get_boolean (value));
93 break;
95 case PROP_BAZ:
96 test_object_set_baz (tobj, g_value_get_string (value));
97 break;
99 default:
100 g_assert_not_reached ();
104 static void
105 test_object_get_property (GObject *gobject,
106 guint prop_id,
107 GValue *value,
108 GParamSpec *pspec)
110 TestObject *tobj = (TestObject *) gobject;
112 g_assert_cmpint (prop_id, !=, 0);
113 g_assert_cmpint (prop_id, !=, N_PROPERTIES);
114 g_assert (pspec == properties[prop_id]);
116 switch (prop_id)
118 case PROP_FOO:
119 g_value_set_int (value, tobj->foo);
120 break;
122 case PROP_BAR:
123 g_value_set_boolean (value, tobj->bar);
124 break;
126 case PROP_BAZ:
127 g_value_set_string (value, tobj->baz);
128 break;
130 default:
131 g_assert_not_reached ();
135 static void
136 test_object_class_init (TestObjectClass *klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140 properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
141 -1, G_MAXINT,
143 G_PARAM_READWRITE);
144 properties[PROP_BAR] = g_param_spec_boolean ("bar", "Bar", "Bar",
145 FALSE,
146 G_PARAM_READWRITE);
147 properties[PROP_BAZ] = g_param_spec_string ("baz", "Baz", "Baz",
148 NULL,
149 G_PARAM_READWRITE);
151 gobject_class->set_property = test_object_set_property;
152 gobject_class->get_property = test_object_get_property;
153 gobject_class->finalize = test_object_finalize;
155 g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
158 static void
159 test_object_init (TestObject *self)
161 self->foo = 42;
162 self->bar = TRUE;
163 self->baz = g_strdup ("Hello");
166 static void
167 properties_install (void)
169 TestObject *obj = g_object_new (test_object_get_type (), NULL);
170 GParamSpec *pspec;
172 g_assert (properties[PROP_FOO] != NULL);
174 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "foo");
175 g_assert (properties[PROP_FOO] == pspec);
177 g_object_unref (obj);
180 typedef struct {
181 const gchar *name;
182 GParamSpec *pspec;
183 } TestNotifyClosure;
185 static void
186 on_notify (GObject *gobject,
187 GParamSpec *pspec,
188 TestNotifyClosure *clos)
190 g_assert (clos->pspec == pspec);
191 g_assert_cmpstr (clos->name, ==, pspec->name);
194 static void
195 properties_notify (void)
197 TestObject *obj = g_object_new (test_object_get_type (), NULL);
198 TestNotifyClosure clos;
200 g_assert (properties[PROP_FOO] != NULL);
202 clos.name = "foo";
203 clos.pspec = properties[PROP_FOO];
205 g_signal_connect (obj, "notify", G_CALLBACK (on_notify), &clos);
206 g_object_set (obj, "foo", 47, NULL);
208 g_object_unref (obj);
211 static void
212 properties_construct (void)
214 TestObject *obj;
215 gint val;
216 gboolean b;
217 gchar *s;
219 g_test_bug ("630357");
221 /* more than 16 args triggers a realloc in g_object_new_valist() */
222 obj = g_object_new (test_object_get_type (),
223 "foo", 1,
224 "foo", 2,
225 "foo", 3,
226 "foo", 4,
227 "foo", 5,
228 "bar", FALSE,
229 "foo", 6,
230 "foo", 7,
231 "foo", 8,
232 "foo", 9,
233 "foo", 10,
234 "baz", "boo",
235 "foo", 11,
236 "foo", 12,
237 "foo", 13,
238 "foo", 14,
239 "foo", 15,
240 "foo", 16,
241 "foo", 17,
242 "foo", 18,
243 NULL);
245 g_object_get (obj, "foo", &val, NULL);
246 g_assert (val == 18);
247 g_object_get (obj, "bar", &b, NULL);
248 g_assert (!b);
249 g_object_get (obj, "baz", &s, NULL);
250 g_assert_cmpstr (s, ==, "boo");
251 g_free (s);
253 g_object_unref (obj);
257 main (int argc, char *argv[])
259 g_test_init (&argc, &argv, NULL);
261 g_test_bug_base ("http://bugzilla.gnome.org/");
263 g_test_add_func ("/properties/install", properties_install);
264 g_test_add_func ("/properties/notify", properties_notify);
265 g_test_add_func ("/properties/construct", properties_construct);
267 return g_test_run ();