LCOV - code coverage report
Current view: top level - glib/gio/tests - fake-service-name.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 27 30 90.0 %
Date: 2024-04-23 05:16:05 Functions: 4 5 80.0 %
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2021 Frederic Martinsons
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5                 :            :  *
       6                 :            :  * This library is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU Lesser General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2.1 of the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This library is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General
      17                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18                 :            :  *
      19                 :            :  * Authors: Frederic Martinsons <frederic.martinsons@gmail.com>
      20                 :            :  */
      21                 :            : 
      22                 :            : /* A dummy service which just own a dbus name and implement a method to quit*/
      23                 :            : 
      24                 :            : #include <gio/gio.h>
      25                 :            : #include <glib.h>
      26                 :            : 
      27                 :            : static GDBusNodeInfo *introspection_data = NULL;
      28                 :            : static GMainLoop *loop = NULL;
      29                 :            : static const gchar introspection_xml[] =
      30                 :            :     "<node>"
      31                 :            :     "    <interface name='org.gtk.GDBus.FakeService'>"
      32                 :            :     "        <method name='Quit'/>"
      33                 :            :     "    </interface>"
      34                 :            :     "</node>";
      35                 :            : 
      36                 :            : static void
      37                 :          1 : incoming_method_call (GDBusConnection       *connection,
      38                 :            :                       const gchar           *sender,
      39                 :            :                       const gchar           *object_path,
      40                 :            :                       const gchar           *interface_name,
      41                 :            :                       const gchar           *method_name,
      42                 :            :                       GVariant              *parameters,
      43                 :            :                       GDBusMethodInvocation *invocation,
      44                 :            :                       gpointer               user_data)
      45                 :            : {
      46         [ +  - ]:          1 :   if (g_strcmp0 (method_name, "Quit") == 0)
      47                 :            :     {
      48                 :          1 :       g_dbus_method_invocation_return_value (invocation, NULL);
      49                 :          1 :       g_main_loop_quit (loop);
      50                 :            :     }
      51                 :          1 : }
      52                 :            : 
      53                 :            : static const GDBusInterfaceVTable interface_vtable = {
      54                 :            :   incoming_method_call,
      55                 :            :   NULL,
      56                 :            :   NULL,
      57                 :            :   { 0 }
      58                 :            : };
      59                 :            : 
      60                 :            : static void
      61                 :          1 : on_bus_acquired (GDBusConnection *connection,
      62                 :            :                  const gchar     *name,
      63                 :            :                  gpointer         user_data)
      64                 :            : {
      65                 :            :   guint registration_id;
      66                 :          1 :   GError *error = NULL;
      67                 :          1 :   g_test_message ("Acquired a message bus connection");
      68                 :            : 
      69                 :          1 :   registration_id = g_dbus_connection_register_object (connection,
      70                 :            :                                                        "/org/gtk/GDBus/FakeService",
      71                 :          1 :                                                        introspection_data->interfaces[0],
      72                 :            :                                                        &interface_vtable,
      73                 :            :                                                        NULL, /* user_data */
      74                 :            :                                                        NULL, /* user_data_free_func */
      75                 :            :                                                        &error);
      76                 :          1 :   g_assert_no_error (error);
      77                 :          1 :   g_assert (registration_id > 0);
      78                 :          1 : }
      79                 :            : 
      80                 :            : static void
      81                 :          1 : on_name_acquired (GDBusConnection *connection,
      82                 :            :                   const gchar     *name,
      83                 :            :                   gpointer         user_data)
      84                 :            : {
      85                 :          1 :   g_test_message ("Acquired the name %s", name);
      86                 :          1 : }
      87                 :            : 
      88                 :            : static void
      89                 :          0 : on_name_lost (GDBusConnection *connection,
      90                 :            :               const gchar     *name,
      91                 :            :               gpointer         user_data)
      92                 :            : {
      93                 :          0 :   g_test_message ("Lost the name %s", name);
      94                 :          0 : }
      95                 :            : 
      96                 :            : gint
      97                 :          1 : main (gint argc, gchar *argv[])
      98                 :            : {
      99                 :            :   guint id;
     100                 :            : 
     101                 :          1 :   g_log_writer_default_set_use_stderr (TRUE);
     102                 :            : 
     103                 :          1 :   loop = g_main_loop_new (NULL, FALSE);
     104                 :          1 :   introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
     105                 :          1 :   g_assert (introspection_data != NULL);
     106                 :            : 
     107                 :          1 :   id = g_bus_own_name (G_BUS_TYPE_SESSION,
     108                 :            :                        "org.gtk.GDBus.FakeService",
     109                 :            :                        G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
     110                 :            :                            G_BUS_NAME_OWNER_FLAGS_REPLACE,
     111                 :            :                        on_bus_acquired,
     112                 :            :                        on_name_acquired,
     113                 :            :                        on_name_lost,
     114                 :            :                        loop,
     115                 :            :                        NULL);
     116                 :            : 
     117                 :          1 :   g_main_loop_run (loop);
     118                 :            : 
     119                 :          1 :   g_bus_unown_name (id);
     120                 :          1 :   g_main_loop_unref (loop);
     121                 :          1 :   g_dbus_node_info_unref (introspection_data);
     122                 :            : 
     123                 :          1 :   return 0;
     124                 :            : }

Generated by: LCOV version 1.14