LCOV - code coverage report
Current view: top level - glib/gobject/tests - properties.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 379 412 92.0 %
Date: 2024-04-30 05:17:35 Functions: 34 36 94.4 %
Branches: 49 68 72.1 %

           Branch data     Line data    Source code
       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                 :            :   GVariant *var;  /* (nullable) (owned) */
      11                 :            :   gchar *quux;
      12                 :            : } TestObject;
      13                 :            : 
      14                 :            : typedef struct _TestObjectClass {
      15                 :            :   GObjectClass parent_class;
      16                 :            : } TestObjectClass;
      17                 :            : 
      18                 :            : enum { PROP_0, PROP_FOO, PROP_BAR, PROP_BAZ, PROP_VAR, PROP_QUUX, N_PROPERTIES };
      19                 :            : 
      20                 :            : static GParamSpec *properties[N_PROPERTIES] = { NULL, };
      21                 :            : 
      22                 :            : static GType test_object_get_type (void);
      23   [ +  +  +  -  :         12 : G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
                   +  + ]
      24                 :            : 
      25                 :            : static void
      26                 :         25 : test_object_set_foo (TestObject *obj,
      27                 :            :                      gint        foo)
      28                 :            : {
      29         [ +  + ]:         25 :   if (obj->foo != foo)
      30                 :            :     {
      31                 :         23 :       obj->foo = foo;
      32                 :            : 
      33                 :         23 :       g_assert (properties[PROP_FOO] != NULL);
      34                 :         23 :       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
      35                 :            :     }
      36                 :         25 : }
      37                 :            : 
      38                 :            : static void
      39                 :          5 : test_object_set_bar (TestObject *obj,
      40                 :            :                      gboolean    bar)
      41                 :            : {
      42                 :          5 :   bar = !!bar;
      43                 :            : 
      44         [ +  + ]:          5 :   if (obj->bar != bar)
      45                 :            :     {
      46                 :          1 :       obj->bar = bar;
      47                 :            : 
      48                 :          1 :       g_assert (properties[PROP_BAR] != NULL);
      49                 :          1 :       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAR]);
      50                 :            :     }
      51                 :          5 : }
      52                 :            : 
      53                 :            : static void
      54                 :          7 : test_object_set_baz (TestObject  *obj,
      55                 :            :                      const gchar *baz)
      56                 :            : {
      57         [ +  + ]:          7 :   if (g_strcmp0 (obj->baz, baz) != 0)
      58                 :            :     {
      59                 :          6 :       g_free (obj->baz);
      60                 :          6 :       obj->baz = g_strdup (baz);
      61                 :            : 
      62                 :          6 :       g_assert (properties[PROP_BAZ] != NULL);
      63                 :          6 :       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_BAZ]);
      64                 :            :     }
      65                 :          7 : }
      66                 :            : 
      67                 :            : static void
      68                 :          1 : test_object_set_var (TestObject *obj,
      69                 :            :                      GVariant   *var)
      70                 :            : {
      71                 :          1 :   GVariant *new_var = NULL;
      72                 :            : 
      73   [ +  -  -  +  :          1 :   if (var == NULL || obj->var == NULL ||
                   -  - ]
      74                 :          0 :       !g_variant_equal (var, obj->var))
      75                 :            :     {
      76                 :            :       /* Note: We deliberately don’t sink @var here, to make sure that
      77                 :            :        * properties_set_property_variant_floating() is testing that GObject
      78                 :            :        * internally sinks variants. */
      79                 :          1 :       new_var = g_variant_ref (var);
      80                 :          1 :       g_clear_pointer (&obj->var, g_variant_unref);
      81                 :          1 :       obj->var = g_steal_pointer (&new_var);
      82                 :            : 
      83                 :          1 :       g_assert (properties[PROP_VAR] != NULL);
      84                 :          1 :       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_VAR]);
      85                 :            :     }
      86                 :          1 : }
      87                 :            : 
      88                 :            : static void
      89                 :          4 : test_object_set_quux (TestObject  *obj,
      90                 :            :                       const gchar *quux)
      91                 :            : {
      92         [ +  + ]:          4 :   if (g_strcmp0 (obj->quux, quux) != 0)
      93                 :            :     {
      94                 :          3 :       g_free (obj->quux);
      95                 :          3 :       obj->quux = g_strdup (quux);
      96                 :            : 
      97                 :          3 :       g_assert (properties[PROP_QUUX] != NULL);
      98                 :          3 :       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_QUUX]);
      99                 :            :     }
     100                 :          4 : }
     101                 :            : 
     102                 :            : static void
     103                 :         10 : test_object_finalize (GObject *gobject)
     104                 :            : {
     105                 :         10 :   TestObject *self = (TestObject *) gobject;
     106                 :            : 
     107                 :         10 :   g_free (self->baz);
     108                 :         10 :   g_clear_pointer (&self->var, g_variant_unref);
     109                 :         10 :   g_free (self->quux);
     110                 :            : 
     111                 :            :   /* When the ref_count of an object is zero it is still
     112                 :            :    * possible to notify the property, but it should do
     113                 :            :    * nothing and silently quit (bug #705570)
     114                 :            :    */
     115                 :         10 :   g_object_notify (gobject, "foo");
     116                 :         10 :   g_object_notify_by_pspec (gobject, properties[PROP_BAR]);
     117                 :            : 
     118                 :         10 :   G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
     119                 :         10 : }
     120                 :            : 
     121                 :            : static void
     122                 :         42 : test_object_set_property (GObject *gobject,
     123                 :            :                           guint prop_id,
     124                 :            :                           const GValue *value,
     125                 :            :                           GParamSpec *pspec)
     126                 :            : {
     127                 :         42 :   TestObject *tobj = (TestObject *) gobject;
     128                 :            : 
     129                 :         42 :   g_assert_cmpint (prop_id, !=, 0);
     130                 :         42 :   g_assert_true (prop_id < N_PROPERTIES && pspec == properties[prop_id]);
     131                 :            : 
     132   [ +  +  +  +  :         42 :   switch (prop_id)
                   +  - ]
     133                 :            :     {
     134                 :         25 :     case PROP_FOO:
     135                 :         25 :       test_object_set_foo (tobj, g_value_get_int (value));
     136                 :         25 :       break;
     137                 :            : 
     138                 :          5 :     case PROP_BAR:
     139                 :          5 :       test_object_set_bar (tobj, g_value_get_boolean (value));
     140                 :          5 :       break;
     141                 :            : 
     142                 :          7 :     case PROP_BAZ:
     143                 :          7 :       test_object_set_baz (tobj, g_value_get_string (value));
     144                 :          7 :       break;
     145                 :            : 
     146                 :          1 :     case PROP_VAR:
     147                 :          1 :       test_object_set_var (tobj, g_value_get_variant (value));
     148                 :          1 :       break;
     149                 :            : 
     150                 :          4 :     case PROP_QUUX:
     151                 :          4 :       test_object_set_quux (tobj, g_value_get_string (value));
     152                 :          4 :       break;
     153                 :            : 
     154                 :          0 :     default:
     155                 :            :       g_assert_not_reached ();
     156                 :            :     }
     157                 :         42 : }
     158                 :            : 
     159                 :            : static void
     160                 :         27 : test_object_get_property (GObject *gobject,
     161                 :            :                           guint prop_id,
     162                 :            :                           GValue *value,
     163                 :            :                           GParamSpec *pspec)
     164                 :            : {
     165                 :         27 :   TestObject *tobj = (TestObject *) gobject;
     166                 :            : 
     167                 :         27 :   g_assert_cmpint (prop_id, !=, 0);
     168                 :         27 :   g_assert_true (prop_id < N_PROPERTIES && pspec == properties[prop_id]);
     169                 :            : 
     170   [ +  +  +  +  :         27 :   switch (prop_id)
                   +  - ]
     171                 :            :     {
     172                 :          7 :     case PROP_FOO:
     173                 :          7 :       g_value_set_int (value, tobj->foo);
     174                 :          7 :       break;
     175                 :            : 
     176                 :          8 :     case PROP_BAR:
     177                 :          8 :       g_value_set_boolean (value, tobj->bar);
     178                 :          8 :       break;
     179                 :            : 
     180                 :          6 :     case PROP_BAZ:
     181                 :          6 :       g_value_set_string (value, tobj->baz);
     182                 :          6 :       break;
     183                 :            : 
     184                 :          1 :     case PROP_VAR:
     185                 :          1 :       g_value_set_variant (value, tobj->var);
     186                 :          1 :       break;
     187                 :            : 
     188                 :          5 :     case PROP_QUUX:
     189                 :          5 :       g_value_set_string (value, tobj->quux);
     190                 :          5 :       break;
     191                 :            : 
     192                 :          0 :     default:
     193                 :            :       g_assert_not_reached ();
     194                 :            :     }
     195                 :         27 : }
     196                 :            : 
     197                 :            : static void
     198                 :          1 : test_object_class_init (TestObjectClass *klass)
     199                 :            : {
     200                 :          1 :   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
     201                 :            : 
     202                 :          1 :   properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
     203                 :            :                                            -1, G_MAXINT,
     204                 :            :                                            0,
     205                 :            :                                            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     206                 :          1 :   properties[PROP_BAR] = g_param_spec_boolean ("bar", "Bar", "Bar",
     207                 :            :                                                FALSE,
     208                 :            :                                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     209                 :          1 :   properties[PROP_BAZ] = g_param_spec_string ("baz", "Baz", "Baz",
     210                 :            :                                               NULL,
     211                 :            :                                               G_PARAM_READWRITE);
     212                 :          1 :   properties[PROP_VAR] = g_param_spec_variant ("var", "Var", "Var",
     213                 :            :                                                G_VARIANT_TYPE_STRING, NULL,
     214                 :            :                                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     215                 :            : 
     216                 :          1 :   gobject_class->set_property = test_object_set_property;
     217                 :          1 :   gobject_class->get_property = test_object_get_property;
     218                 :          1 :   gobject_class->finalize = test_object_finalize;
     219                 :            : 
     220                 :          1 :   g_object_class_install_properties (gobject_class, N_PROPERTIES - 1, properties);
     221                 :            : 
     222                 :            :   /* We intentionally install this property separately, to test
     223                 :            :    * that that works, and that property lookup works regardless
     224                 :            :    * how the property was installed.
     225                 :            :    */
     226                 :          1 :   properties[PROP_QUUX] = g_param_spec_string ("quux", "quux", "quux",
     227                 :            :                                                NULL,
     228                 :            :                                                G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
     229                 :            : 
     230                 :          1 :   g_object_class_install_property (gobject_class, PROP_QUUX, properties[PROP_QUUX]);
     231                 :          1 : }
     232                 :            : 
     233                 :            : static void
     234                 :         10 : test_object_init (TestObject *self)
     235                 :            : {
     236                 :         10 :   self->foo = 42;
     237                 :         10 :   self->bar = TRUE;
     238                 :         10 :   self->baz = g_strdup ("Hello");
     239                 :         10 :   self->quux = NULL;
     240                 :         10 : }
     241                 :            : 
     242                 :            : static void
     243                 :          1 : properties_install (void)
     244                 :            : {
     245                 :          1 :   TestObject *obj = g_object_new (test_object_get_type (), NULL);
     246                 :            :   GParamSpec *pspec;
     247                 :            :   char *name;
     248                 :            : 
     249                 :          1 :   g_assert (properties[PROP_FOO] != NULL);
     250                 :            : 
     251                 :          1 :   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "foo");
     252                 :          1 :   g_assert (properties[PROP_FOO] == pspec);
     253                 :            : 
     254                 :          1 :   name = g_strdup ("bar");
     255                 :          1 :   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), name);
     256                 :          1 :   g_assert (properties[PROP_BAR] == pspec);
     257                 :          1 :   g_free (name);
     258                 :            : 
     259                 :          1 :   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "baz");
     260                 :          1 :   g_assert (properties[PROP_BAZ] == pspec);
     261                 :            : 
     262                 :          1 :   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "var");
     263                 :          1 :   g_assert (properties[PROP_VAR] == pspec);
     264                 :            : 
     265                 :          1 :   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "quux");
     266                 :          1 :   g_assert (properties[PROP_QUUX] == pspec);
     267                 :            : 
     268                 :          1 :   g_object_unref (obj);
     269                 :          1 : }
     270                 :            : 
     271                 :            : typedef struct {
     272                 :            :   GObject parent_instance;
     273                 :            :   int value[16];
     274                 :            : } ManyProps;
     275                 :            : 
     276                 :            : typedef GObjectClass ManyPropsClass;
     277                 :            : 
     278                 :            : static GParamSpec *props[16];
     279                 :            : 
     280                 :            : GType many_props_get_type (void) G_GNUC_CONST;
     281                 :            : 
     282   [ +  +  +  -  :          3 : G_DEFINE_TYPE(ManyProps, many_props, G_TYPE_OBJECT)
                   +  - ]
     283                 :            : 
     284                 :            : static void
     285                 :          1 : many_props_init (ManyProps *self)
     286                 :            : {
     287                 :          1 : }
     288                 :            : 
     289                 :            : static void
     290                 :          0 : get_prop (GObject    *object,
     291                 :            :           guint       prop_id,
     292                 :            :           GValue     *value,
     293                 :            :           GParamSpec *pspec)
     294                 :            : {
     295                 :          0 :   ManyProps *mp = (ManyProps *) object;
     296                 :            : 
     297   [ #  #  #  # ]:          0 :   if (prop_id > 0 && prop_id < 13)
     298                 :          0 :     g_value_set_int (value, mp->value[prop_id]);
     299                 :            :   else
     300                 :          0 :     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     301                 :          0 : }
     302                 :            : 
     303                 :            : static void
     304                 :          0 : set_prop (GObject      *object,
     305                 :            :           guint         prop_id,
     306                 :            :           const GValue *value,
     307                 :            :           GParamSpec   *pspec)
     308                 :            : {
     309                 :          0 :   ManyProps *mp = (ManyProps *) object;
     310                 :            : 
     311   [ #  #  #  # ]:          0 :   if (prop_id > 0 && prop_id < 13)
     312                 :          0 :     mp->value[prop_id] = g_value_get_int (value);
     313                 :            :   else
     314                 :          0 :     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     315                 :          0 : }
     316                 :            : 
     317                 :            : static void
     318                 :          1 : many_props_class_init (ManyPropsClass *class)
     319                 :            : {
     320                 :          1 :   G_OBJECT_CLASS (class)->get_property = get_prop;
     321                 :          1 :   G_OBJECT_CLASS (class)->set_property = set_prop;
     322                 :            : 
     323                 :          1 :   props[1] = g_param_spec_int ("one", NULL, NULL,
     324                 :            :                                0, G_MAXINT, 0,
     325                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     326                 :          1 :   props[2] = g_param_spec_int ("two", NULL, NULL,
     327                 :            :                                0, G_MAXINT, 0,
     328                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     329                 :          1 :   props[3] = g_param_spec_int ("three", NULL, NULL,
     330                 :            :                                0, G_MAXINT, 0,
     331                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     332                 :          1 :   props[4] = g_param_spec_int ("four", NULL, NULL,
     333                 :            :                                0, G_MAXINT, 0,
     334                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     335                 :          1 :   props[5] = g_param_spec_int ("five", NULL, NULL,
     336                 :            :                                0, G_MAXINT, 0,
     337                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     338                 :          1 :   props[6] = g_param_spec_int ("six", NULL, NULL,
     339                 :            :                                0, G_MAXINT, 0,
     340                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     341                 :          1 :   props[7] = g_param_spec_int ("seven", NULL, NULL,
     342                 :            :                                0, G_MAXINT, 0,
     343                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     344                 :          1 :   props[8] = g_param_spec_int ("eight", NULL, NULL,
     345                 :            :                                0, G_MAXINT, 0,
     346                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     347                 :          1 :   props[9] = g_param_spec_int ("nine", NULL, NULL,
     348                 :            :                                0, G_MAXINT, 0,
     349                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     350                 :          1 :   props[10] = g_param_spec_int ("ten", NULL, NULL,
     351                 :            :                                0, G_MAXINT, 0,
     352                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     353                 :          1 :   props[11] = g_param_spec_int ("eleven", NULL, NULL,
     354                 :            :                                0, G_MAXINT, 0,
     355                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     356                 :          1 :   props[12] = g_param_spec_int ("twelve", NULL, NULL,
     357                 :            :                                0, G_MAXINT, 0,
     358                 :            :                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
     359                 :          1 :   g_object_class_install_properties (G_OBJECT_CLASS (class), 12, props);
     360                 :          1 : }
     361                 :            : 
     362                 :            : static void
     363                 :          1 : properties_install_many (void)
     364                 :            : {
     365                 :          1 :   ManyProps *obj = g_object_new (many_props_get_type (), NULL);
     366                 :            :   GParamSpec *pspec;
     367                 :            : 
     368                 :          1 :   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "one");
     369                 :          1 :   g_assert (props[1] == pspec);
     370                 :            : 
     371                 :          1 :   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (obj), "ten");
     372                 :          1 :   g_assert (props[10] == pspec);
     373                 :            : 
     374                 :          1 :   g_object_unref (obj);
     375                 :          1 : }
     376                 :            : 
     377                 :            : typedef struct {
     378                 :            :   const gchar *name;
     379                 :            :   GParamSpec *pspec;
     380                 :            :   gboolean    fired;
     381                 :            : } TestNotifyClosure;
     382                 :            : 
     383                 :            : static void
     384                 :          4 : on_notify (GObject           *gobject,
     385                 :            :            GParamSpec        *pspec,
     386                 :            :            TestNotifyClosure *closure)
     387                 :            : {
     388                 :          4 :   g_assert (closure->pspec == pspec);
     389                 :          4 :   g_assert_cmpstr (closure->name, ==, pspec->name);
     390                 :          4 :   closure->fired = TRUE;
     391                 :          4 : }
     392                 :            : 
     393                 :            : static void
     394                 :          1 : properties_notify (void)
     395                 :            : {
     396                 :          1 :   TestObject *obj = g_object_new (test_object_get_type (), NULL);
     397                 :            :   TestNotifyClosure closure;
     398                 :            : 
     399                 :          1 :   g_assert (properties[PROP_FOO] != NULL);
     400                 :          1 :   g_assert (properties[PROP_QUUX] != NULL);
     401                 :          1 :   g_signal_connect (obj, "notify", G_CALLBACK (on_notify), &closure);
     402                 :            : 
     403                 :          1 :   closure.name = "foo";
     404                 :          1 :   closure.pspec = properties[PROP_FOO];
     405                 :            : 
     406                 :          1 :   closure.fired = FALSE;
     407                 :          1 :   g_object_set (obj, "foo", 47, NULL);
     408                 :          1 :   g_assert (closure.fired);
     409                 :            : 
     410                 :          1 :   closure.name = "baz";
     411                 :          1 :   closure.pspec = properties[PROP_BAZ];
     412                 :            : 
     413                 :          1 :   closure.fired = FALSE;
     414                 :          1 :   g_object_set (obj, "baz", "something new", NULL);
     415                 :          1 :   g_assert (closure.fired);
     416                 :            : 
     417                 :            :   /* baz lacks explicit notify, so we will see this twice */
     418                 :          1 :   closure.fired = FALSE;
     419                 :          1 :   g_object_set (obj, "baz", "something new", NULL);
     420                 :          1 :   g_assert (closure.fired);
     421                 :            : 
     422                 :            :   /* quux on the other hand, ... */
     423                 :          1 :   closure.name = "quux";
     424                 :          1 :   closure.pspec = properties[PROP_QUUX];
     425                 :            : 
     426                 :          1 :   closure.fired = FALSE;
     427                 :          1 :   g_object_set (obj, "quux", "something new", NULL);
     428                 :          1 :   g_assert (closure.fired);
     429                 :            : 
     430                 :            :   /* no change; no notify */
     431                 :          1 :   closure.fired = FALSE;
     432                 :          1 :   g_object_set (obj, "quux", "something new", NULL);
     433                 :          1 :   g_assert (!closure.fired);
     434                 :            : 
     435                 :            : 
     436                 :          1 :   g_object_unref (obj);
     437                 :          1 : }
     438                 :            : 
     439                 :            : typedef struct {
     440                 :            :   GParamSpec *pspec[3];
     441                 :            :   gint pos;
     442                 :            : } Notifys;
     443                 :            : 
     444                 :            : static void
     445                 :          6 : on_notify2 (GObject    *gobject,
     446                 :            :             GParamSpec *pspec,
     447                 :            :             Notifys    *n)
     448                 :            : {
     449                 :          6 :   g_assert (n->pspec[n->pos] == pspec);
     450                 :          6 :   n->pos++;
     451                 :          6 : }
     452                 :            : 
     453                 :            : static void
     454                 :          1 : properties_notify_queue (void)
     455                 :            : {
     456                 :          1 :   TestObject *obj = g_object_new (test_object_get_type (), NULL);
     457                 :            :   Notifys n;
     458                 :            : 
     459                 :          1 :   g_assert (properties[PROP_FOO] != NULL);
     460                 :            : 
     461                 :          1 :   n.pspec[0] = properties[PROP_BAZ];
     462                 :          1 :   n.pspec[1] = properties[PROP_BAR];
     463                 :          1 :   n.pspec[2] = properties[PROP_FOO];
     464                 :          1 :   n.pos = 0;
     465                 :            : 
     466                 :          1 :   g_signal_connect (obj, "notify", G_CALLBACK (on_notify2), &n);
     467                 :            : 
     468                 :          1 :   g_object_freeze_notify (G_OBJECT (obj));
     469                 :          1 :   g_object_set (obj, "foo", 47, NULL);
     470                 :          1 :   g_object_set (obj, "bar", TRUE, "foo", 42, "baz", "abc", NULL);
     471                 :          1 :   g_object_thaw_notify (G_OBJECT (obj));
     472                 :          1 :   g_assert (n.pos == 3);
     473                 :            : 
     474                 :          1 :   g_object_unref (obj);
     475                 :          1 : }
     476                 :            : 
     477                 :            : static void
     478                 :          1 : properties_construct (void)
     479                 :            : {
     480                 :            :   TestObject *obj;
     481                 :            :   gint val;
     482                 :            :   gboolean b;
     483                 :            :   gchar *s;
     484                 :            : 
     485                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=630357");
     486                 :            : 
     487                 :            :   /* more than 16 args triggers a realloc in g_object_new_valist() */
     488                 :          1 :   obj = g_object_new (test_object_get_type (),
     489                 :            :                       "foo", 1,
     490                 :            :                       "foo", 2,
     491                 :            :                       "foo", 3,
     492                 :            :                       "foo", 4,
     493                 :            :                       "foo", 5,
     494                 :            :                       "bar", FALSE,
     495                 :            :                       "foo", 6,
     496                 :            :                       "foo", 7,
     497                 :            :                       "foo", 8,
     498                 :            :                       "foo", 9,
     499                 :            :                       "foo", 10,
     500                 :            :                       "baz", "boo",
     501                 :            :                       "foo", 11,
     502                 :            :                       "foo", 12,
     503                 :            :                       "foo", 13,
     504                 :            :                       "foo", 14,
     505                 :            :                       "foo", 15,
     506                 :            :                       "foo", 16,
     507                 :            :                       "foo", 17,
     508                 :            :                       "foo", 18,
     509                 :            :                       NULL);
     510                 :            : 
     511                 :          1 :   g_object_get (obj, "foo", &val, NULL);
     512                 :          1 :   g_assert (val == 18);
     513                 :          1 :   g_object_get (obj, "bar", &b, NULL);
     514                 :          1 :   g_assert (!b);
     515                 :          1 :   g_object_get (obj, "baz", &s, NULL);
     516                 :          1 :   g_assert_cmpstr (s, ==, "boo");
     517                 :          1 :   g_free (s);
     518                 :            : 
     519                 :          1 :   g_object_unref (obj);
     520                 :          1 : }
     521                 :            : 
     522                 :            : static void
     523                 :          1 : properties_testv_with_no_properties (void)
     524                 :            : {
     525                 :            :   TestObject *test_obj;
     526                 :          1 :   const char *prop_names[4] = { "foo", "bar", "baz", "quux" };
     527                 :          1 :   GValue values_out[4] = { G_VALUE_INIT };
     528                 :            :   guint i;
     529                 :            : 
     530                 :            :   /* Test newv_with_properties && getv */
     531                 :          1 :   test_obj = (TestObject *) g_object_new_with_properties (
     532                 :            :       test_object_get_type (), 0, NULL, NULL);
     533                 :          1 :   g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out);
     534                 :            : 
     535                 :            :   /* It should have init values */
     536                 :          1 :   g_assert_cmpint (g_value_get_int (&values_out[0]), ==, 42);
     537                 :          1 :   g_assert_true (g_value_get_boolean (&values_out[1]));
     538                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out[2]), ==, "Hello");
     539                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out[3]), ==, NULL);
     540                 :            : 
     541         [ +  + ]:          5 :   for (i = 0; i < 4; i++)
     542                 :          4 :     g_value_unset (&values_out[i]);
     543                 :          1 :   g_object_unref (test_obj);
     544                 :          1 : }
     545                 :            : 
     546                 :            : static void
     547                 :          1 : properties_testv_with_valid_properties (void)
     548                 :            : {
     549                 :            :   TestObject *test_obj;
     550                 :          1 :   const char *prop_names[4] = { "foo", "bar", "baz", "quux" };
     551                 :            : 
     552                 :          1 :   GValue values_in[4] = { G_VALUE_INIT };
     553                 :          1 :   GValue values_out[4] = { G_VALUE_INIT };
     554                 :            :   guint i;
     555                 :            : 
     556                 :          1 :   g_value_init (&(values_in[0]), G_TYPE_INT);
     557                 :          1 :   g_value_set_int (&(values_in[0]), 100);
     558                 :            : 
     559                 :          1 :   g_value_init (&(values_in[1]), G_TYPE_BOOLEAN);
     560                 :          1 :   g_value_set_boolean (&(values_in[1]), TRUE);
     561                 :            : 
     562                 :          1 :   g_value_init (&(values_in[2]), G_TYPE_STRING);
     563                 :          1 :   g_value_set_string (&(values_in[2]), "pigs");
     564                 :            : 
     565                 :          1 :   g_value_init (&(values_in[3]), G_TYPE_STRING);
     566                 :          1 :   g_value_set_string (&(values_in[3]), "fly");
     567                 :            : 
     568                 :            :   /* Test newv_with_properties && getv */
     569                 :          1 :   test_obj = (TestObject *) g_object_new_with_properties (
     570                 :            :       test_object_get_type (), 4, prop_names, values_in);
     571                 :          1 :   g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out);
     572                 :            : 
     573                 :          1 :   g_assert_cmpint (g_value_get_int (&values_out[0]), ==, 100);
     574                 :          1 :   g_assert_true (g_value_get_boolean (&values_out[1]));
     575                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out[2]), ==, "pigs");
     576                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out[3]), ==, "fly");
     577                 :            : 
     578         [ +  + ]:          5 :   for (i = 0; i < G_N_ELEMENTS (values_out); i++)
     579                 :          4 :     g_value_unset (&values_out[i]);
     580                 :            : 
     581                 :            :   /* Test newv2 && getv */
     582                 :          1 :   g_value_set_string (&(values_in[2]), "Elmo knows");
     583                 :          1 :   g_value_set_string (&(values_in[3]), "where you live");
     584                 :          1 :   g_object_setv (G_OBJECT (test_obj), 4, prop_names, values_in);
     585                 :            : 
     586                 :          1 :   g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out);
     587                 :            : 
     588                 :          1 :   g_assert_cmpint (g_value_get_int (&values_out[0]), ==, 100);
     589                 :          1 :   g_assert_true (g_value_get_boolean (&values_out[1]));
     590                 :            : 
     591                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out[2]), ==, "Elmo knows");
     592                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out[3]), ==, "where you live");
     593                 :            : 
     594         [ +  + ]:          5 :   for (i = 0; i < G_N_ELEMENTS (values_in); i++)
     595                 :          4 :     g_value_unset (&values_in[i]);
     596         [ +  + ]:          5 :   for (i = 0; i < G_N_ELEMENTS (values_out); i++)
     597                 :          4 :     g_value_unset (&values_out[i]);
     598                 :            : 
     599                 :          1 :   g_object_unref (test_obj);
     600                 :          1 : }
     601                 :            : 
     602                 :            : static void
     603                 :          1 : properties_testv_with_invalid_property_type (void)
     604                 :            : {
     605         [ -  + ]:          1 :   if (g_test_subprocess ())
     606                 :            :     {
     607                 :            :       TestObject *test_obj;
     608                 :          0 :       const char *invalid_prop_names[1] = { "foo" };
     609                 :          0 :       GValue values_in[1] = { G_VALUE_INIT };
     610                 :            : 
     611                 :          0 :       g_value_init (&(values_in[0]), G_TYPE_STRING);
     612                 :          0 :       g_value_set_string (&(values_in[0]), "fly");
     613                 :            : 
     614                 :          0 :       test_obj = (TestObject *) g_object_new_with_properties (
     615                 :            :           test_object_get_type (), 1, invalid_prop_names, values_in);
     616                 :            :       /* should give a warning */
     617                 :            : 
     618                 :          0 :       g_object_unref (test_obj);
     619                 :            :     }
     620                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     621                 :          1 :   g_test_trap_assert_failed ();
     622                 :          1 :   g_test_trap_assert_stderr ("*CRITICAL*foo*gint*gchararray*");
     623                 :          1 : }
     624                 :            : 
     625                 :            : 
     626                 :            : static void
     627                 :          1 : properties_testv_with_invalid_property_names (void)
     628                 :            : {
     629         [ -  + ]:          1 :   if (g_test_subprocess ())
     630                 :            :     {
     631                 :            :       TestObject *test_obj;
     632                 :          0 :       const char *invalid_prop_names[4] = { "foo", "boo", "moo", "poo" };
     633                 :          0 :       GValue values_in[4] = { G_VALUE_INIT };
     634                 :            : 
     635                 :          0 :       g_value_init (&(values_in[0]), G_TYPE_INT);
     636                 :          0 :       g_value_set_int (&(values_in[0]), 100);
     637                 :            : 
     638                 :          0 :       g_value_init (&(values_in[1]), G_TYPE_BOOLEAN);
     639                 :          0 :       g_value_set_boolean (&(values_in[1]), TRUE);
     640                 :            : 
     641                 :          0 :       g_value_init (&(values_in[2]), G_TYPE_STRING);
     642                 :          0 :       g_value_set_string (&(values_in[2]), "pigs");
     643                 :            : 
     644                 :          0 :       g_value_init (&(values_in[3]), G_TYPE_STRING);
     645                 :          0 :       g_value_set_string (&(values_in[3]), "fly");
     646                 :            : 
     647                 :          0 :       test_obj = (TestObject *) g_object_new_with_properties (
     648                 :            :           test_object_get_type (), 4, invalid_prop_names, values_in);
     649                 :            :       /* This call should give 3 Critical warnings. Actually, a critical warning
     650                 :            :        * shouldn't make g_object_new_with_properties to fail when a bad named
     651                 :            :        * property is given, because, it will just ignore that property. However,
     652                 :            :        * for test purposes, it is considered that the test doesn't pass.
     653                 :            :        */
     654                 :            : 
     655                 :          0 :       g_object_unref (test_obj);
     656                 :            :     }
     657                 :            : 
     658                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     659                 :          1 :   g_test_trap_assert_failed ();
     660                 :          1 :   g_test_trap_assert_stderr ("*CRITICAL*g_object_new_is_valid_property*boo*");
     661                 :          1 : }
     662                 :            : 
     663                 :            : static void
     664                 :          1 : properties_testv_getv (void)
     665                 :            : {
     666                 :            :   TestObject *test_obj;
     667                 :          1 :   const char *prop_names[4] = { "foo", "bar", "baz", "quux" };
     668                 :          1 :   GValue values_out_initialized[4] = { G_VALUE_INIT };
     669                 :          1 :   GValue values_out_uninitialized[4] = { G_VALUE_INIT };
     670                 :            :   guint i;
     671                 :            : 
     672                 :          1 :   g_value_init (&(values_out_initialized[0]), G_TYPE_INT);
     673                 :          1 :   g_value_init (&(values_out_initialized[1]), G_TYPE_BOOLEAN);
     674                 :          1 :   g_value_init (&(values_out_initialized[2]), G_TYPE_STRING);
     675                 :          1 :   g_value_init (&(values_out_initialized[3]), G_TYPE_STRING);
     676                 :            : 
     677                 :          1 :   test_obj = (TestObject *) g_object_new_with_properties (
     678                 :            :       test_object_get_type (), 0, NULL, NULL);
     679                 :            : 
     680                 :            :   /* Test g_object_getv for an initialized values array */
     681                 :          1 :   g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out_initialized);
     682                 :            :   /* It should have init values */
     683                 :          1 :   g_assert_cmpint (g_value_get_int (&values_out_initialized[0]), ==, 42);
     684                 :          1 :   g_assert_true (g_value_get_boolean (&values_out_initialized[1]));
     685                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out_initialized[2]), ==, "Hello");
     686                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out_initialized[3]), ==, NULL);
     687                 :            : 
     688                 :            :   /* Test g_object_getv for an uninitialized values array */
     689                 :          1 :   g_object_getv (G_OBJECT (test_obj), 4, prop_names, values_out_uninitialized);
     690                 :            :   /* It should have init values */
     691                 :          1 :   g_assert_cmpint (g_value_get_int (&values_out_uninitialized[0]), ==, 42);
     692                 :          1 :   g_assert_true (g_value_get_boolean (&values_out_uninitialized[1]));
     693                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out_uninitialized[2]), ==, "Hello");
     694                 :          1 :   g_assert_cmpstr (g_value_get_string (&values_out_uninitialized[3]), ==, NULL);
     695                 :            : 
     696         [ +  + ]:          5 :   for (i = 0; i < 4; i++)
     697                 :            :     {
     698                 :          4 :       g_value_unset (&values_out_initialized[i]);
     699                 :          4 :       g_value_unset (&values_out_uninitialized[i]);
     700                 :            :     }
     701                 :          1 :   g_object_unref (test_obj);
     702                 :          1 : }
     703                 :            : 
     704                 :            : static void
     705                 :          1 : properties_get_property (void)
     706                 :            : {
     707                 :            :   TestObject *test_obj;
     708                 :            :   struct {
     709                 :            :     const char *name;
     710                 :            :     GType gtype;
     711                 :            :     GValue value;
     712                 :          1 :   } test_props[] = {
     713                 :            :     { "foo", G_TYPE_INT, G_VALUE_INIT },
     714                 :            :     { "bar", G_TYPE_INVALID, G_VALUE_INIT },
     715                 :            :     { "bar", G_TYPE_STRING, G_VALUE_INIT },
     716                 :            :   };
     717                 :            :   gsize i;
     718                 :            : 
     719                 :          1 :   g_test_summary ("g_object_get_property() accepts uninitialized, "
     720                 :            :                   "initialized, and transformable values");
     721                 :            : 
     722         [ +  + ]:          4 :   for (i = 0; i < G_N_ELEMENTS (test_props); i++)
     723                 :            :     {
     724         [ +  + ]:          3 :       if (test_props[i].gtype != G_TYPE_INVALID)
     725                 :          2 :         g_value_init (&(test_props[i].value), test_props[i].gtype);
     726                 :            :     }
     727                 :            : 
     728                 :          1 :   test_obj = (TestObject *) g_object_new_with_properties (test_object_get_type (), 0, NULL, NULL);
     729                 :            : 
     730                 :          1 :   g_test_message ("Test g_object_get_property with an initialized value");
     731                 :          1 :   g_object_get_property (G_OBJECT (test_obj), test_props[0].name, &(test_props[0].value));
     732                 :          1 :   g_assert_cmpint (g_value_get_int (&(test_props[0].value)), ==, 42);
     733                 :            : 
     734                 :          1 :   g_test_message ("Test g_object_get_property with an uninitialized value");
     735                 :          1 :   g_object_get_property (G_OBJECT (test_obj), test_props[1].name, &(test_props[1].value));
     736                 :          1 :   g_assert_true (g_value_get_boolean (&(test_props[1].value)));
     737                 :            : 
     738                 :          1 :   g_test_message ("Test g_object_get_property with a transformable value");
     739                 :          1 :   g_object_get_property (G_OBJECT (test_obj), test_props[2].name, &(test_props[2].value));
     740                 :          1 :   g_assert_true (G_VALUE_HOLDS_STRING (&(test_props[2].value)));
     741                 :          1 :   g_assert_cmpstr (g_value_get_string (&(test_props[2].value)), ==, "TRUE");
     742                 :            : 
     743         [ +  + ]:          4 :   for (i = 0; i < G_N_ELEMENTS (test_props); i++)
     744                 :          3 :     g_value_unset (&(test_props[i].value));
     745                 :            : 
     746                 :          1 :   g_object_unref (test_obj);
     747                 :          1 : }
     748                 :            : 
     749                 :            : static void
     750                 :          1 : properties_set_property_variant_floating (void)
     751                 :            : {
     752                 :          1 :   TestObject *test_obj = NULL;
     753                 :          1 :   GVariant *owned_floating_variant = NULL;
     754                 :          1 :   GVariant *floating_variant_ptr = NULL;
     755                 :          1 :   GVariant *got_variant = NULL;
     756                 :            : 
     757                 :          1 :   g_test_summary ("Test that setting a property to a floating variant consumes the reference");
     758                 :            : 
     759                 :          1 :   test_obj = (TestObject *) g_object_new (test_object_get_type (), NULL);
     760                 :            : 
     761                 :          1 :   owned_floating_variant = floating_variant_ptr = g_variant_new_string ("this variant has only one floating ref");
     762                 :          1 :   g_assert_true (g_variant_is_floating (floating_variant_ptr));
     763                 :            : 
     764                 :          1 :   g_object_set (test_obj, "var", g_steal_pointer (&owned_floating_variant), NULL);
     765                 :            : 
     766                 :            :   /* This assumes that the GObject implementation refs, rather than copies and destroys, the incoming variant */
     767                 :          1 :   g_assert_false (g_variant_is_floating (floating_variant_ptr));
     768                 :            : 
     769                 :          1 :   g_object_get (test_obj, "var", &got_variant, NULL);
     770                 :          1 :   g_assert_false (g_variant_is_floating (got_variant));
     771                 :          1 :   g_assert_cmpvariant (got_variant, floating_variant_ptr);
     772                 :            : 
     773                 :          1 :   g_variant_unref (got_variant);
     774                 :          1 :   g_object_unref (test_obj);
     775                 :          1 : }
     776                 :            : 
     777                 :            : static void
     778                 :          1 : properties_testv_notify_queue (void)
     779                 :            : {
     780                 :            :   TestObject *test_obj;
     781                 :          1 :   const char *prop_names[3] = { "foo", "bar", "baz" };
     782                 :          1 :   GValue values_in[3] = { G_VALUE_INIT };
     783                 :            :   Notifys n;
     784                 :            :   guint i;
     785                 :            : 
     786                 :          1 :   g_value_init (&(values_in[0]), G_TYPE_INT);
     787                 :          1 :   g_value_set_int (&(values_in[0]), 100);
     788                 :            : 
     789                 :          1 :   g_value_init (&(values_in[1]), G_TYPE_BOOLEAN);
     790                 :          1 :   g_value_set_boolean (&(values_in[1]), TRUE);
     791                 :            : 
     792                 :          1 :   g_value_init (&(values_in[2]), G_TYPE_STRING);
     793                 :          1 :   g_value_set_string (&(values_in[2]), "");
     794                 :            : 
     795                 :            :   /* Test newv_with_properties && getv */
     796                 :          1 :   test_obj = (TestObject *) g_object_new_with_properties (
     797                 :            :       test_object_get_type (), 0, NULL, NULL);
     798                 :            : 
     799                 :          1 :   g_assert_nonnull (properties[PROP_FOO]);
     800                 :            : 
     801                 :          1 :   n.pspec[0] = properties[PROP_BAZ];
     802                 :          1 :   n.pspec[1] = properties[PROP_BAR];
     803                 :          1 :   n.pspec[2] = properties[PROP_FOO];
     804                 :          1 :   n.pos = 0;
     805                 :            : 
     806                 :          1 :   g_signal_connect (test_obj, "notify", G_CALLBACK (on_notify2), &n);
     807                 :            : 
     808                 :          1 :   g_object_freeze_notify (G_OBJECT (test_obj));
     809                 :            :   {
     810                 :          1 :     g_object_setv (G_OBJECT (test_obj), 3, prop_names, values_in);
     811                 :            : 
     812                 :            :     /* Set "foo" to 70 */
     813                 :          1 :     g_value_set_int (&(values_in[0]), 100);
     814                 :          1 :     g_object_setv (G_OBJECT (test_obj), 1, prop_names, values_in);
     815                 :            :   }
     816                 :          1 :   g_object_thaw_notify (G_OBJECT (test_obj));
     817                 :          1 :   g_assert_cmpint (n.pos, ==, 3);
     818                 :            : 
     819         [ +  + ]:          4 :   for (i = 0; i < 3; i++)
     820                 :          3 :     g_value_unset (&values_in[i]);
     821                 :          1 :   g_object_unref (test_obj);
     822                 :          1 : }
     823                 :            : 
     824                 :            : int
     825                 :          1 : main (int argc, char *argv[])
     826                 :            : {
     827                 :          1 :   g_test_init (&argc, &argv, NULL);
     828                 :            : 
     829                 :          1 :   g_test_add_func ("/properties/install", properties_install);
     830                 :          1 :   g_test_add_func ("/properties/install-many", properties_install_many);
     831                 :          1 :   g_test_add_func ("/properties/notify", properties_notify);
     832                 :          1 :   g_test_add_func ("/properties/notify-queue", properties_notify_queue);
     833                 :          1 :   g_test_add_func ("/properties/construct", properties_construct);
     834                 :          1 :   g_test_add_func ("/properties/get-property", properties_get_property);
     835                 :          1 :   g_test_add_func ("/properties/set-property/variant/floating", properties_set_property_variant_floating);
     836                 :            : 
     837                 :          1 :   g_test_add_func ("/properties/testv_with_no_properties",
     838                 :            :       properties_testv_with_no_properties);
     839                 :          1 :   g_test_add_func ("/properties/testv_with_valid_properties",
     840                 :            :       properties_testv_with_valid_properties);
     841                 :          1 :   g_test_add_func ("/properties/testv_with_invalid_property_type",
     842                 :            :       properties_testv_with_invalid_property_type);
     843                 :          1 :   g_test_add_func ("/properties/testv_with_invalid_property_names",
     844                 :            :       properties_testv_with_invalid_property_names);
     845                 :          1 :   g_test_add_func ("/properties/testv_getv", properties_testv_getv);
     846                 :          1 :   g_test_add_func ("/properties/testv_notify_queue",
     847                 :            :       properties_testv_notify_queue);
     848                 :            : 
     849                 :          1 :   return g_test_run ();
     850                 :            : }

Generated by: LCOV version 1.14