aboutsummaryrefslogtreecommitdiff
path: root/tests/backend-dbus/mock-object.cc
blob: af9330b8cf412e2eda41b4048e6ea6f181c1359c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright 2013 Canonical Ltd.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <gio/gio.h>

#include "mock-object.h"

namespace
{
  const int TIMEOUT_SECONDS = 5;

  gboolean on_timeout_reached (gpointer loop)
  {
    g_main_loop_quit (static_cast<GMainLoop*>(loop));
    return G_SOURCE_REMOVE;
  }

  void on_name_acquired (GDBusConnection * connection G_GNUC_UNUSED,
                         const char      * name       G_GNUC_UNUSED,
                         gpointer          loop)
  {
    //g_debug ("name '%s' acquired", name);
    g_main_loop_quit (static_cast<GMainLoop*>(loop));
  }

  void on_name_lost (GDBusConnection * connection   G_GNUC_UNUSED,
                     const char      * name         G_GNUC_UNUSED,
                     gpointer          loop)
  {
    //g_debug ("name '%s' lost", name);
    g_main_loop_quit (static_cast<GMainLoop*>(loop));
  }
}

void
MockObject :: set_skeleton (GDBusInterfaceSkeleton * skeleton)
{
  g_assert (skeleton != NULL);
  g_assert (my_skeleton == NULL);
  g_assert (g_variant_is_object_path (my_object_path.c_str()));
  g_assert (my_owner_id == 0);

  my_skeleton = G_DBUS_INTERFACE_SKELETON (g_object_ref (skeleton));

  GError * err = NULL;
  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON(my_skeleton),
                                    my_bus_connection,
                                    my_object_path.c_str(),
                                    &err);
  g_assert_no_error (err);

  my_owner_id = g_bus_own_name_on_connection (my_bus_connection,
                                              my_object_name.c_str(),
                                              G_BUS_NAME_OWNER_FLAGS_NONE,
                                              on_name_acquired,
                                              on_name_lost,
                                              my_loop,
                                              NULL);

  // wait for the name to be acquired or timeout, whichever comes first
  const guint timeout_id = g_timeout_add_seconds (TIMEOUT_SECONDS,
                                                  on_timeout_reached,
                                                  my_loop);
  g_main_loop_run (my_loop);
  g_assert (g_main_context_find_source_by_id (NULL, timeout_id) != NULL);
  g_source_remove (timeout_id);
}

/***
****
***/

MockObject :: MockObject (GMainLoop          * loop,
                          GDBusConnection    * bus_connection,
                          const std::string  & object_name,
                          const std::string  & object_path):
  my_owner_id (0),
  my_loop (g_main_loop_ref (loop)),
  my_bus_connection (G_DBUS_CONNECTION (g_object_ref (bus_connection))),
  my_object_name (object_name),
  my_object_path (object_path),
  my_skeleton (0)
{
}

MockObject :: ~MockObject ()
{
  g_main_loop_unref (my_loop);

  if (my_owner_id != 0)
    {
      g_bus_unown_name (my_owner_id);

      my_owner_id = 0;
    }

  if (my_skeleton)
    {
      g_dbus_interface_skeleton_unexport (my_skeleton);

      g_clear_object (&my_skeleton);
    }

  g_clear_object (&my_bus_connection);
}