Initial GDBus code-drop from GDBus-standalone repo
[platform/upstream/glib.git] / gio / tests / gdbus-tests.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2009 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25
26 #include "gdbus-tests.h"
27
28 /* ---------------------------------------------------------------------------------------------------- */
29
30 typedef struct
31 {
32   GMainLoop *loop;
33   gboolean   timed_out;
34 } PropertyNotifyData;
35
36 static void
37 on_property_notify (GObject    *object,
38                     GParamSpec *pspec,
39                     gpointer    user_data)
40 {
41   PropertyNotifyData *data = user_data;
42   g_main_loop_quit (data->loop);
43 }
44
45 static gboolean
46 on_property_notify_timeout (gpointer user_data)
47 {
48   PropertyNotifyData *data = user_data;
49   data->timed_out = TRUE;
50   g_main_loop_quit (data->loop);
51   return TRUE;
52 }
53
54 gboolean
55 _g_assert_property_notify_run (gpointer     object,
56                                const gchar *property_name)
57 {
58   gchar *s;
59   gulong handler_id;
60   guint timeout_id;
61   PropertyNotifyData data;
62
63   data.loop = g_main_loop_new (NULL, FALSE);
64   data.timed_out = FALSE;
65   s = g_strdup_printf ("notify::%s", property_name);
66   handler_id = g_signal_connect (object,
67                                  s,
68                                  G_CALLBACK (on_property_notify),
69                                  &data);
70   g_free (s);
71   timeout_id = g_timeout_add (5 * 1000,
72                               on_property_notify_timeout,
73                               &data);
74   g_main_loop_run (data.loop);
75   g_signal_handler_disconnect (object, handler_id);
76   g_source_remove (timeout_id);
77   g_main_loop_unref (data.loop);
78
79   return data.timed_out;
80 }
81
82 /* ---------------------------------------------------------------------------------------------------- */
83
84 typedef struct
85 {
86   GMainLoop *loop;
87   gboolean   timed_out;
88 } SignalReceivedData;
89
90 static void
91 on_signal_received (gpointer user_data)
92 {
93   SignalReceivedData *data = user_data;
94   g_main_loop_quit (data->loop);
95 }
96
97 static gboolean
98 on_signal_received_timeout (gpointer user_data)
99 {
100   SignalReceivedData *data = user_data;
101   data->timed_out = TRUE;
102   g_main_loop_quit (data->loop);
103   return TRUE;
104 }
105
106 gboolean
107 _g_assert_signal_received_run (gpointer     object,
108                                const gchar *signal_name)
109 {
110   gulong handler_id;
111   guint timeout_id;
112   SignalReceivedData data;
113
114   data.loop = g_main_loop_new (NULL, FALSE);
115   data.timed_out = FALSE;
116   handler_id = g_signal_connect_swapped (object,
117                                          signal_name,
118                                          G_CALLBACK (on_signal_received),
119                                          &data);
120   timeout_id = g_timeout_add (5 * 1000,
121                               on_signal_received_timeout,
122                               &data);
123   g_main_loop_run (data.loop);
124   g_signal_handler_disconnect (object, handler_id);
125   g_source_remove (timeout_id);
126   g_main_loop_unref (data.loop);
127
128   return data.timed_out;
129 }
130
131 /* ---------------------------------------------------------------------------------------------------- */
132
133 GDBusConnection *
134 _g_bus_get_priv (GBusType            bus_type,
135                  GCancellable       *cancellable,
136                  GError            **error)
137 {
138   gchar *address;
139   GDBusConnection *ret;
140
141   ret = NULL;
142
143   address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
144   if (address == NULL)
145     goto out;
146
147   ret = g_dbus_connection_new_for_address_sync (address,
148                                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
149                                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
150                                                 cancellable,
151                                                 error);
152   g_free (address);
153
154  out:
155   return ret;
156 }
157
158 /* ---------------------------------------------------------------------------------------------------- */
159
160 typedef struct
161 {
162   GMainLoop *loop;
163   gboolean   timed_out;
164 } WaitSingleRefData;
165
166 static gboolean
167 on_wait_single_ref_timeout (gpointer user_data)
168 {
169   WaitSingleRefData *data = user_data;
170   data->timed_out = TRUE;
171   g_main_loop_quit (data->loop);
172   return TRUE;
173 }
174
175 static void
176 on_wait_for_single_ref_toggled (gpointer   user_data,
177                                 GObject   *object,
178                                 gboolean   is_last_ref)
179 {
180   WaitSingleRefData *data = user_data;
181   g_main_loop_quit (data->loop);
182 }
183
184 gboolean
185 _g_object_wait_for_single_ref_do (gpointer object)
186 {
187   WaitSingleRefData data;
188   guint timeout_id;
189
190   data.timed_out = FALSE;
191
192   if (G_OBJECT (object)->ref_count == 1)
193     goto out;
194
195   data.loop = g_main_loop_new (NULL, FALSE);
196   timeout_id = g_timeout_add (5 * 1000,
197                               on_wait_single_ref_timeout,
198                               &data);
199
200   g_object_add_toggle_ref (G_OBJECT (object),
201                            on_wait_for_single_ref_toggled,
202                            &data);
203   g_object_unref (object);
204
205   g_main_loop_run (data.loop);
206
207   g_object_ref (object);
208   g_object_remove_toggle_ref (object,
209                               on_wait_for_single_ref_toggled,
210                               &data);
211
212   g_source_remove (timeout_id);
213   g_main_loop_unref (data.loop);
214  out:
215   return data.timed_out;
216 }
217
218 /* ---------------------------------------------------------------------------------------------------- */