Remove g_type_init() calls
[platform/upstream/glib.git] / gobject / tests / properties.c
1 #include <stdlib.h>
2 #include <gstdio.h>
3 #include <glib-object.h>
4
5 typedef struct _TestObject {
6   GObject parent_instance;
7   gint foo;
8   gboolean bar;
9   gchar *baz;
10 } TestObject;
11
12 typedef struct _TestObjectClass {
13   GObjectClass parent_class;
14 } TestObjectClass;
15
16 enum { PROP_0, PROP_FOO, PROP_BAR, PROP_BAZ, N_PROPERTIES };
17
18 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
19
20 static GType test_object_get_type (void);
21 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
22
23 static void
24 test_object_set_foo (TestObject *obj,
25                      gint        foo)
26 {
27   if (obj->foo != foo)
28     {
29       obj->foo = foo;
30
31       g_assert (properties[PROP_FOO] != NULL);
32       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
33     }
34 }
35
36 static void
37 test_object_set_bar (TestObject *obj,
38                      gboolean    bar)
39 {
40   bar = !!bar;
41
42   if (obj->bar != bar)
43     {
44       obj->bar = bar;
45
46       g_assert (properties[PROP_BAR] != NULL);
47       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAR]);
48     }
49 }
50
51 static void
52 test_object_set_baz (TestObject  *obj,
53                      const gchar *baz)
54 {
55   if (g_strcmp0 (obj->baz, baz) != 0)
56     {
57       g_free (obj->baz);
58       obj->baz = g_strdup (baz);
59
60       g_assert (properties[PROP_BAZ] != NULL);
61       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAZ]);
62     }
63 }
64
65 static void
66 test_object_finalize (GObject *gobject)
67 {
68   g_free (((TestObject *) gobject)->baz);
69
70   G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
71 }
72
73 static void
74 test_object_set_property (GObject *gobject,
75                           guint prop_id,
76                           const GValue *value,
77                           GParamSpec *pspec)
78 {
79   TestObject *tobj = (TestObject *) gobject;
80
81   g_assert_cmpint (prop_id, !=, 0);
82   g_assert_cmpint (prop_id, !=, N_PROPERTIES);
83   g_assert (pspec == properties[prop_id]);
84
85   switch (prop_id)
86     {
87     case PROP_FOO:
88       test_object_set_foo (tobj, g_value_get_int (value));
89       break;
90
91     case PROP_BAR:
92       test_object_set_bar (tobj, g_value_get_boolean (value));
93       break;
94
95     case PROP_BAZ:
96       test_object_set_baz (tobj, g_value_get_string (value));
97       break;
98
99     default:
100       g_assert_not_reached ();
101     }
102 }
103
104 static void
105 test_object_get_property (GObject *gobject,
106                           guint prop_id,
107                           GValue *value,
108                           GParamSpec *pspec)
109 {
110   TestObject *tobj = (TestObject *) gobject;
111
112   g_assert_cmpint (prop_id, !=, 0);
113   g_assert_cmpint (prop_id, !=, N_PROPERTIES);
114   g_assert (pspec == properties[prop_id]);
115
116   switch (prop_id)
117     {
118     case PROP_FOO:
119       g_value_set_int (value, tobj->foo);
120       break;
121
122     case PROP_BAR:
123       g_value_set_boolean (value, tobj->bar);
124       break;
125
126     case PROP_BAZ:
127       g_value_set_string (value, tobj->baz);
128       break;
129
130     default:
131       g_assert_not_reached ();
132     }
133 }
134
135 static void
136 test_object_class_init (TestObjectClass *klass)
137 {
138   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139
140   properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
141                                            -1, G_MAXINT,
142                                            0,
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);
150
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;
154
155   g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
156 }
157
158 static void
159 test_object_init (TestObject *self)
160 {
161   self->foo = 42;
162   self->bar = TRUE;
163   self->baz = g_strdup ("Hello");
164 }
165
166 static void
167 properties_install (void)
168 {
169   TestObject *obj = g_object_new (test_object_get_type (), NULL);
170   GParamSpec *pspec;
171
172   g_assert (properties[PROP_FOO] != NULL);
173
174   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "foo");
175   g_assert (properties[PROP_FOO] == pspec);
176
177   g_object_unref (obj);
178 }
179
180 typedef struct {
181   const gchar *name;
182   GParamSpec *pspec;
183 } TestNotifyClosure;
184
185 static void
186 on_notify (GObject           *gobject,
187            GParamSpec        *pspec,
188            TestNotifyClosure *clos)
189 {
190   g_assert (clos->pspec == pspec);
191   g_assert_cmpstr (clos->name, ==, pspec->name);
192 }
193
194 static void
195 properties_notify (void)
196 {
197   TestObject *obj = g_object_new (test_object_get_type (), NULL);
198   TestNotifyClosure clos;
199
200   g_assert (properties[PROP_FOO] != NULL);
201
202   clos.name = "foo";
203   clos.pspec = properties[PROP_FOO];
204
205   g_signal_connect (obj, "notify", G_CALLBACK (on_notify), &clos);
206   g_object_set (obj, "foo", 47, NULL);
207
208   g_object_unref (obj);
209 }
210
211 static void
212 properties_construct (void)
213 {
214   TestObject *obj;
215   gint val;
216   gboolean b;
217   gchar *s;
218
219   g_test_bug ("630357");
220
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);
244
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);
252
253   g_object_unref (obj);
254 }
255
256 int
257 main (int argc, char *argv[])
258 {
259   g_test_init (&argc, &argv, NULL);
260
261   g_test_bug_base ("http://bugzilla.gnome.org/");
262
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);
266
267   return g_test_run ();
268 }