[glib/freeze-shenanigans: 1/3] Add a testcase for notify-in-instance-init




commit 1e482b28a457d7e60b93a5e3602be750769179bc
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jun 8 11:56:25 2022 -0400

    Add a testcase for notify-in-instance-init
    
    This is reproducing a problem that was observed
    in gtk3's gtk_check_button_init.

 gobject/tests/meson.build   |   1 +
 gobject/tests/notify-init.c | 234 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 235 insertions(+)
---
diff --git a/gobject/tests/meson.build b/gobject/tests/meson.build
index 98f992eff8..4a10daa4d8 100644
--- a/gobject/tests/meson.build
+++ b/gobject/tests/meson.build
@@ -27,6 +27,7 @@ marshalers_c = custom_target('marshalers_c',
 )
 
 gobject_tests = {
+  'notify-init' : {},
   'qdata' : {},
   'accumulator' : {
     'source' : ['accumulator.c', marshalers_h, marshalers_c],
diff --git a/gobject/tests/notify-init.c b/gobject/tests/notify-init.c
new file mode 100644
index 0000000000..e4ec764f68
--- /dev/null
+++ b/gobject/tests/notify-init.c
@@ -0,0 +1,234 @@
+
+#include <stdlib.h>
+#include <gstdio.h>
+#include <glib-object.h>
+
+typedef struct _TestObject {
+  GObject parent_instance;
+  gint foo;
+  gboolean bar;
+  gchar *baz;
+  gchar *quux;
+} TestObject;
+
+typedef struct _TestObjectClass {
+  GObjectClass parent_class;
+} TestObjectClass;
+
+enum { PROP_0, PROP_FOO, PROP_BAR, PROP_BAZ, PROP_QUUX, N_PROPERTIES };
+
+static GParamSpec *properties[N_PROPERTIES] = { NULL, };
+
+static GType test_object_get_type (void);
+G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
+
+static void
+test_object_set_foo (TestObject *obj,
+                     gint        foo)
+{
+  if (obj->foo != foo)
+    {
+      obj->foo = foo;
+
+      g_assert (properties[PROP_FOO] != NULL);
+      g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
+    }
+}
+
+static void
+test_object_set_bar (TestObject *obj,
+                     gboolean    bar)
+{
+  bar = !!bar;
+
+  if (obj->bar != bar)
+    {
+      obj->bar = bar;
+
+      g_assert (properties[PROP_BAR] != NULL);
+      g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAR]);
+    }
+}
+
+static void
+test_object_set_baz (TestObject  *obj,
+                     const gchar *baz)
+{
+  if (g_strcmp0 (obj->baz, baz) != 0)
+    {
+      g_free (obj->baz);
+      obj->baz = g_strdup (baz);
+
+      g_assert (properties[PROP_BAZ] != NULL);
+      g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAZ]);
+    }
+}
+
+static void
+test_object_set_quux (TestObject  *obj,
+                      const gchar *quux)
+{
+  if (g_strcmp0 (obj->quux, quux) != 0)
+    {
+      g_free (obj->quux);
+      obj->quux = g_strdup (quux);
+
+      g_assert (properties[PROP_QUUX] != NULL);
+      g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_QUUX]);
+    }
+}
+
+static void
+test_object_finalize (GObject *gobject)
+{
+  TestObject *self = (TestObject *) gobject;
+
+  g_free (self->baz);
+  g_free (self->quux);
+
+  /* When the ref_count of an object is zero it is still
+   * possible to notify the property, but it should do
+   * nothing and silently quit (bug #705570)
+   */
+  g_object_notify (gobject, "foo");
+  g_object_notify_by_pspec (gobject, properties[PROP_BAR]);
+
+  G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
+}
+
+static void
+test_object_set_property (GObject *gobject,
+                          guint prop_id,
+                          const GValue *value,
+                          GParamSpec *pspec)
+{
+  TestObject *tobj = (TestObject *) gobject;
+
+  g_assert_cmpint (prop_id, !=, 0);
+  g_assert_cmpint (prop_id, !=, N_PROPERTIES);
+  g_assert (pspec == properties[prop_id]);
+
+  switch (prop_id)
+    {
+    case PROP_FOO:
+      test_object_set_foo (tobj, g_value_get_int (value));
+      break;
+
+    case PROP_BAR:
+      test_object_set_bar (tobj, g_value_get_boolean (value));
+      break;
+
+    case PROP_BAZ:
+      test_object_set_baz (tobj, g_value_get_string (value));
+      break;
+
+    case PROP_QUUX:
+      test_object_set_quux (tobj, g_value_get_string (value));
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+}
+
+static void
+test_object_get_property (GObject *gobject,
+                          guint prop_id,
+                          GValue *value,
+                          GParamSpec *pspec)
+{
+  TestObject *tobj = (TestObject *) gobject;
+
+  g_assert_cmpint (prop_id, !=, 0);
+  g_assert_cmpint (prop_id, !=, N_PROPERTIES);
+  g_assert (pspec == properties[prop_id]);
+
+  switch (prop_id)
+    {
+    case PROP_FOO:
+      g_value_set_int (value, tobj->foo);
+      break;
+
+    case PROP_BAR:
+      g_value_set_boolean (value, tobj->bar);
+      break;
+
+    case PROP_BAZ:
+      g_value_set_string (value, tobj->baz);
+      break;
+
+    case PROP_QUUX:
+      g_value_set_string (value, tobj->quux);
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+}
+
+static void
+test_object_class_init (TestObjectClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
+                                           -1, G_MAXINT,
+                                           0,
+                                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+  properties[PROP_BAR] = g_param_spec_boolean ("bar", "Bar", "Bar",
+                                               FALSE,
+                                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+  properties[PROP_BAZ] = g_param_spec_string ("baz", "Baz", "Baz",
+                                              NULL,
+                                              G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  properties[PROP_QUUX] = g_param_spec_string ("quux", "quux", "quux",
+                                               NULL,
+                                               G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | 
G_PARAM_STATIC_STRINGS);
+
+  gobject_class->set_property = test_object_set_property;
+  gobject_class->get_property = test_object_get_property;
+  gobject_class->finalize = test_object_finalize;
+
+  g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
+}
+
+static void
+quux_changed (TestObject *self,
+              GParamSpec *pspec,
+              gpointer    data)
+{
+  g_assert (self->baz != NULL);
+}
+
+static void
+test_object_init (TestObject *self)
+{
+  g_signal_connect (self, "notify::quux", G_CALLBACK (quux_changed), NULL);
+
+  test_object_set_quux (self, "quux");
+
+  self->foo = 42;
+  self->bar = TRUE;
+  self->baz = g_strdup ("Hello");
+}
+
+static void
+test_notify_in_init (void)
+{
+  TestObject *obj;
+
+  obj = g_object_new (test_object_get_type (), NULL);
+
+  g_object_unref (obj);
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/properties/notify-in-init", test_notify_in_init);
+
+  return g_test_run ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]