~codygarver/+junk/ind-sess

« back to all changes in this revision

Viewing changes to tests/backend-dbus/mock-object.cc

  • Committer: Cody Garver
  • Date: 2014-04-03 17:08:08 UTC
  • Revision ID: cody@elementaryos.org-20140403170808-z56s93rorb1dzvmk
Initial import, version 12.10.5+14.04.20140324-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *   Charles Kerr <charles.kerr@canonical.com>
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License version 3, as published
 
9
 * by the Free Software Foundation.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
 * PURPOSE.  See the GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License along
 
17
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <glib.h>
 
21
#include <gio/gio.h>
 
22
 
 
23
#include "mock-object.h"
 
24
 
 
25
namespace
 
26
{
 
27
  const int TIMEOUT_SECONDS = 5;
 
28
 
 
29
  gboolean on_timeout_reached (gpointer loop)
 
30
  {
 
31
    g_main_loop_quit (static_cast<GMainLoop*>(loop));
 
32
    return G_SOURCE_REMOVE;
 
33
  }
 
34
 
 
35
  void on_name_acquired (GDBusConnection * connection G_GNUC_UNUSED,
 
36
                         const char      * name       G_GNUC_UNUSED,
 
37
                         gpointer          loop)
 
38
  {
 
39
    //g_debug ("name '%s' acquired", name);
 
40
    g_main_loop_quit (static_cast<GMainLoop*>(loop));
 
41
  }
 
42
 
 
43
  void on_name_lost (GDBusConnection * connection   G_GNUC_UNUSED,
 
44
                     const char      * name         G_GNUC_UNUSED,
 
45
                     gpointer          loop)
 
46
  {
 
47
    //g_debug ("name '%s' lost", name);
 
48
    g_main_loop_quit (static_cast<GMainLoop*>(loop));
 
49
  }
 
50
}
 
51
 
 
52
void
 
53
MockObject :: set_skeleton (GDBusInterfaceSkeleton * skeleton)
 
54
{
 
55
  g_assert (skeleton != NULL);
 
56
  g_assert (my_skeleton == NULL);
 
57
  g_assert (g_variant_is_object_path (my_object_path.c_str()));
 
58
  g_assert (my_owner_id == 0);
 
59
 
 
60
  my_skeleton = G_DBUS_INTERFACE_SKELETON (g_object_ref (skeleton));
 
61
 
 
62
  GError * err = NULL;
 
63
  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON(my_skeleton),
 
64
                                    my_bus_connection,
 
65
                                    my_object_path.c_str(),
 
66
                                    &err);
 
67
  g_assert_no_error (err);
 
68
 
 
69
  my_owner_id = g_bus_own_name_on_connection (my_bus_connection,
 
70
                                              my_object_name.c_str(),
 
71
                                              G_BUS_NAME_OWNER_FLAGS_NONE,
 
72
                                              on_name_acquired,
 
73
                                              on_name_lost,
 
74
                                              my_loop,
 
75
                                              NULL);
 
76
 
 
77
  // wait for the name to be acquired or timeout, whichever comes first
 
78
  const guint timeout_id = g_timeout_add_seconds (TIMEOUT_SECONDS,
 
79
                                                  on_timeout_reached,
 
80
                                                  my_loop);
 
81
  g_main_loop_run (my_loop);
 
82
  g_assert (g_main_context_find_source_by_id (NULL, timeout_id) != NULL);
 
83
  g_source_remove (timeout_id);
 
84
}
 
85
 
 
86
/***
 
87
****
 
88
***/
 
89
 
 
90
MockObject :: MockObject (GMainLoop          * loop,
 
91
                          GDBusConnection    * bus_connection,
 
92
                          const std::string  & object_name,
 
93
                          const std::string  & object_path):
 
94
  my_owner_id (0),
 
95
  my_loop (g_main_loop_ref (loop)),
 
96
  my_bus_connection (G_DBUS_CONNECTION (g_object_ref (bus_connection))),
 
97
  my_object_name (object_name),
 
98
  my_object_path (object_path),
 
99
  my_skeleton (0)
 
100
{
 
101
}
 
102
 
 
103
MockObject :: ~MockObject ()
 
104
{
 
105
  g_main_loop_unref (my_loop);
 
106
 
 
107
  if (my_owner_id != 0)
 
108
    {
 
109
      g_bus_unown_name (my_owner_id);
 
110
 
 
111
      my_owner_id = 0;
 
112
    }
 
113
 
 
114
  if (my_skeleton)
 
115
    {
 
116
      g_dbus_interface_skeleton_unexport (my_skeleton);
 
117
 
 
118
      g_clear_object (&my_skeleton);
 
119
    }
 
120
 
 
121
  g_clear_object (&my_bus_connection);
 
122
}