LCOV - code coverage report
Current view: top level - glib/gobject/tests - custom-dispatch.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 59 68 86.8 %
Date: 2024-04-30 05:17:35 Functions: 12 13 92.3 %
Branches: 8 12 66.7 %

           Branch data     Line data    Source code
       1                 :            : /* custom-dispatch.c: Test GObjectClass.dispatch_properties_changed
       2                 :            :  * Copyright (C) 2022 Red Hat, Inc.
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       5                 :            :  *
       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.
       9                 :            :  *
      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.
      13                 :            :  *
      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.
      22                 :            :  */
      23                 :            : 
      24                 :            : #include <glib-object.h>
      25                 :            : 
      26                 :            : typedef struct {
      27                 :            :   GObject parent_instance;
      28                 :            :   int foo;
      29                 :            : } TestObject;
      30                 :            : 
      31                 :            : typedef struct {
      32                 :            :   GObjectClass parent_class;
      33                 :            : } TestObjectClass;
      34                 :            : 
      35                 :            : typedef enum {
      36                 :            :   PROP_FOO = 1,
      37                 :            :   N_PROPERTIES,
      38                 :            : } TestObjectProperty;
      39                 :            : 
      40                 :            : static GParamSpec *properties[N_PROPERTIES] = { NULL, };
      41                 :            : 
      42                 :            : static GType test_object_get_type (void);
      43   [ +  +  +  -  :          4 : G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT)
                   +  + ]
      44                 :            : 
      45                 :            : static void
      46                 :          4 : test_object_set_foo (TestObject *obj,
      47                 :            :                      gint        foo)
      48                 :            : {
      49         [ +  + ]:          4 :   if (obj->foo != foo)
      50                 :            :     {
      51                 :          3 :       obj->foo = foo;
      52                 :            : 
      53                 :          3 :       g_assert (properties[PROP_FOO] != NULL);
      54                 :          3 :       g_object_notify_by_pspec (G_OBJECT (obj), properties[PROP_FOO]);
      55                 :            :     }
      56                 :          4 : }
      57                 :            : 
      58                 :            : static void
      59                 :          4 : test_object_set_property (GObject *gobject,
      60                 :            :                           guint prop_id,
      61                 :            :                           const GValue *value,
      62                 :            :                           GParamSpec *pspec)
      63                 :            : {
      64                 :          4 :   TestObject *tobj = (TestObject *) gobject;
      65                 :            : 
      66         [ +  - ]:          4 :   switch ((TestObjectProperty)prop_id)
      67                 :            :     {
      68                 :          4 :     case PROP_FOO:
      69                 :          4 :       test_object_set_foo (tobj, g_value_get_int (value));
      70                 :          4 :       break;
      71                 :            : 
      72                 :          0 :     default:
      73                 :            :       g_assert_not_reached ();
      74                 :            :     }
      75                 :          4 : }
      76                 :            : 
      77                 :            : static void
      78                 :          0 : test_object_get_property (GObject *gobject,
      79                 :            :                           guint prop_id,
      80                 :            :                           GValue *value,
      81                 :            :                           GParamSpec *pspec)
      82                 :            : {
      83                 :          0 :   TestObject *tobj = (TestObject *) gobject;
      84                 :            : 
      85         [ #  # ]:          0 :   switch ((TestObjectProperty)prop_id)
      86                 :            :     {
      87                 :          0 :     case PROP_FOO:
      88                 :          0 :       g_value_set_int (value, tobj->foo);
      89                 :          0 :       break;
      90                 :            : 
      91                 :          0 :     default:
      92                 :            :       g_assert_not_reached ();
      93                 :            :     }
      94                 :          0 : }
      95                 :            : 
      96                 :            : static int dispatch_properties_called;
      97                 :            : 
      98                 :            : static void
      99                 :          3 : test_object_dispatch_properties_changed (GObject     *object,
     100                 :            :                                          guint        n_pspecs,
     101                 :            :                                          GParamSpec **pspecs)
     102                 :            : {
     103                 :          3 :   dispatch_properties_called++;
     104                 :            : 
     105                 :          3 :   G_OBJECT_CLASS (test_object_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
     106                 :          3 : }
     107                 :            : 
     108                 :            : 
     109                 :            : static void
     110                 :          1 : test_object_class_init (TestObjectClass *klass)
     111                 :            : {
     112                 :          1 :   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
     113                 :            : 
     114                 :          1 :   properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "Foo",
     115                 :            :                                            -1, G_MAXINT,
     116                 :            :                                            0,
     117                 :            :                                            G_PARAM_READWRITE |
     118                 :            :                                            G_PARAM_STATIC_STRINGS |
     119                 :            :                                            G_PARAM_EXPLICIT_NOTIFY);
     120                 :            : 
     121                 :          1 :   gobject_class->set_property = test_object_set_property;
     122                 :          1 :   gobject_class->get_property = test_object_get_property;
     123                 :          1 :   gobject_class->dispatch_properties_changed = test_object_dispatch_properties_changed;
     124                 :            : 
     125                 :          1 :   g_object_class_install_properties (gobject_class, N_PROPERTIES, properties);
     126                 :          1 : }
     127                 :            : 
     128                 :            : static void
     129                 :          2 : test_object_init (TestObject *self)
     130                 :            : {
     131                 :          2 :   self->foo = 42;
     132                 :          2 : }
     133                 :            : 
     134                 :            : static gboolean
     135                 :          2 : object_has_notify_signal_handlers (gpointer instance)
     136                 :            : {
     137                 :          2 :   guint signal_id = g_signal_lookup ("notify", G_TYPE_OBJECT);
     138                 :            : 
     139                 :          2 :   return g_signal_handler_find (instance, G_SIGNAL_MATCH_ID, signal_id, 0, NULL, NULL, NULL) != 0;
     140                 :            : }
     141                 :            : 
     142                 :            : static void
     143                 :          1 : test_custom_dispatch_init (void)
     144                 :            : {
     145                 :            :   TestObject *obj;
     146                 :            : 
     147                 :          1 :   g_test_summary ("Test that custom dispatch_properties_changed is called "
     148                 :            :                   "on initialization");
     149                 :            : 
     150                 :          1 :   dispatch_properties_called = 0;
     151                 :          1 :   obj = g_object_new (test_object_get_type (), "foo", 5, NULL);
     152                 :            : 
     153                 :          1 :   g_assert_false (object_has_notify_signal_handlers (obj));
     154                 :            : 
     155                 :          1 :   g_assert_cmpint (dispatch_properties_called, ==, 1);
     156                 :          1 :   g_object_set (obj, "foo", 11, NULL);
     157                 :          1 :   g_assert_cmpint (dispatch_properties_called, ==, 2);
     158                 :            : 
     159                 :          1 :   g_object_unref (obj);
     160                 :          1 : }
     161                 :            : 
     162                 :            : /* This instance init behavior is the thing we are testing:
     163                 :            :  *
     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
     167                 :            :  */
     168                 :            : static void
     169                 :          1 : test_custom_dispatch_set (void)
     170                 :            : {
     171                 :            :   TestObject *obj;
     172                 :            : 
     173                 :          1 :   g_test_summary ("Test that custom dispatch_properties_changed is called regardless of connected notify handlers");
     174                 :            : 
     175                 :          1 :   dispatch_properties_called = 0;
     176                 :          1 :   obj = g_object_new (test_object_get_type (), NULL);
     177                 :            : 
     178                 :          1 :   g_assert_false (object_has_notify_signal_handlers (obj));
     179                 :            : 
     180                 :          1 :   g_assert_cmpint (dispatch_properties_called, ==, 0);
     181                 :          1 :   g_object_set (obj, "foo", 11, NULL);
     182                 :          1 :   g_assert_cmpint (dispatch_properties_called, ==, 1);
     183                 :          1 :   g_object_set (obj, "foo", 11, NULL);
     184                 :          1 :   g_assert_cmpint (dispatch_properties_called, ==, 1);
     185                 :            : 
     186                 :          1 :   g_object_unref (obj);
     187                 :          1 : }
     188                 :            : 
     189                 :            : int
     190                 :          1 : main (int argc, char *argv[])
     191                 :            : {
     192                 :          1 :   g_test_init (&argc, &argv, NULL);
     193                 :            : 
     194                 :          1 :   g_test_add_func ("/properties/custom-dispatch/init", test_custom_dispatch_init);
     195                 :          1 :   g_test_add_func ("/properties/custom-dispatch/set", test_custom_dispatch_set);
     196                 :            : 
     197                 :          1 :   return g_test_run ();
     198                 :            : }

Generated by: LCOV version 1.14