Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / gdbus-tests.c
blob0655f1be4999d2891bf8eb7ea22c275522925cf4
1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.1 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include <gio/gio.h>
22 #include <unistd.h>
24 #include "gdbus-tests.h"
26 /* ---------------------------------------------------------------------------------------------------- */
28 typedef struct
30 GMainLoop *loop;
31 gboolean timed_out;
32 } PropertyNotifyData;
34 static void
35 on_property_notify (GObject *object,
36 GParamSpec *pspec,
37 gpointer user_data)
39 PropertyNotifyData *data = user_data;
40 g_main_loop_quit (data->loop);
43 static gboolean
44 on_property_notify_timeout (gpointer user_data)
46 PropertyNotifyData *data = user_data;
47 data->timed_out = TRUE;
48 g_main_loop_quit (data->loop);
49 return TRUE;
52 gboolean
53 _g_assert_property_notify_run (gpointer object,
54 const gchar *property_name)
56 gchar *s;
57 gulong handler_id;
58 guint timeout_id;
59 PropertyNotifyData data;
61 data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
62 data.timed_out = FALSE;
63 s = g_strdup_printf ("notify::%s", property_name);
64 handler_id = g_signal_connect (object,
66 G_CALLBACK (on_property_notify),
67 &data);
68 g_free (s);
69 timeout_id = g_timeout_add_seconds (30,
70 on_property_notify_timeout,
71 &data);
72 g_main_loop_run (data.loop);
73 g_signal_handler_disconnect (object, handler_id);
74 g_source_remove (timeout_id);
75 g_main_loop_unref (data.loop);
77 return data.timed_out;
80 static gboolean
81 _give_up (gpointer data)
83 g_error ("%s", (const gchar *) data);
84 g_return_val_if_reached (TRUE);
87 void
88 ensure_gdbus_testserver_up (void)
90 guint id;
91 gchar *name_owner;
92 GDBusConnection *connection;
93 GDBusProxy *proxy;
94 GError *error = NULL;
96 connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
97 NULL,
98 &error);
100 g_assert_no_error (error);
101 error = NULL;
103 proxy = g_dbus_proxy_new_sync (connection,
104 G_DBUS_PROXY_FLAGS_NONE,
105 NULL, /* GDBusInterfaceInfo */
106 "com.example.TestService", /* name */
107 "/com/example/TestObject", /* object path */
108 "com.example.Frob", /* interface */
109 NULL, /* GCancellable */
110 &error);
111 g_assert_no_error (error);
113 id = g_timeout_add_seconds (60, _give_up,
114 "waited more than ~ 60s for gdbus-testserver to take its bus name");
116 while (TRUE)
118 name_owner = g_dbus_proxy_get_name_owner (proxy);
120 if (name_owner != NULL)
121 break;
123 g_main_context_iteration (NULL, TRUE);
126 g_source_remove (id);
127 g_free (name_owner);
128 g_object_unref (proxy);
129 g_object_unref (connection);
132 /* ---------------------------------------------------------------------------------------------------- */
134 typedef struct
136 GMainLoop *loop;
137 gboolean timed_out;
138 } SignalReceivedData;
140 static void
141 on_signal_received (gpointer user_data)
143 SignalReceivedData *data = user_data;
144 g_main_loop_quit (data->loop);
147 static gboolean
148 on_signal_received_timeout (gpointer user_data)
150 SignalReceivedData *data = user_data;
151 data->timed_out = TRUE;
152 g_main_loop_quit (data->loop);
153 return TRUE;
156 gboolean
157 _g_assert_signal_received_run (gpointer object,
158 const gchar *signal_name)
160 gulong handler_id;
161 guint timeout_id;
162 SignalReceivedData data;
164 data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
165 data.timed_out = FALSE;
166 handler_id = g_signal_connect_swapped (object,
167 signal_name,
168 G_CALLBACK (on_signal_received),
169 &data);
170 timeout_id = g_timeout_add_seconds (30,
171 on_signal_received_timeout,
172 &data);
173 g_main_loop_run (data.loop);
174 g_signal_handler_disconnect (object, handler_id);
175 g_source_remove (timeout_id);
176 g_main_loop_unref (data.loop);
178 return data.timed_out;
181 /* ---------------------------------------------------------------------------------------------------- */
183 GDBusConnection *
184 _g_bus_get_priv (GBusType bus_type,
185 GCancellable *cancellable,
186 GError **error)
188 gchar *address;
189 GDBusConnection *ret;
191 ret = NULL;
193 address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
194 if (address == NULL)
195 goto out;
197 ret = g_dbus_connection_new_for_address_sync (address,
198 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
199 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
200 NULL, /* GDBusAuthObserver */
201 cancellable,
202 error);
203 g_free (address);
205 out:
206 return ret;
209 /* ---------------------------------------------------------------------------------------------------- */