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>
24 #include "gdbus-tests.h"
26 /* ---------------------------------------------------------------------------------------------------- */
35 on_property_notify (GObject
*object
,
39 PropertyNotifyData
*data
= user_data
;
40 g_main_loop_quit (data
->loop
);
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
);
53 _g_assert_property_notify_run (gpointer object
,
54 const gchar
*property_name
)
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
),
69 timeout_id
= g_timeout_add_seconds (30,
70 on_property_notify_timeout
,
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
;
81 _give_up (gpointer data
)
83 g_error ("%s", (const gchar
*) data
);
84 g_return_val_if_reached (TRUE
);
88 ensure_gdbus_testserver_up (void)
92 GDBusConnection
*connection
;
96 connection
= g_bus_get_sync (G_BUS_TYPE_SESSION
,
100 g_assert_no_error (error
);
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 */
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");
118 name_owner
= g_dbus_proxy_get_name_owner (proxy
);
120 if (name_owner
!= NULL
)
123 g_main_context_iteration (NULL
, TRUE
);
126 g_source_remove (id
);
128 g_object_unref (proxy
);
129 g_object_unref (connection
);
132 /* ---------------------------------------------------------------------------------------------------- */
138 } SignalReceivedData
;
141 on_signal_received (gpointer user_data
)
143 SignalReceivedData
*data
= user_data
;
144 g_main_loop_quit (data
->loop
);
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
);
157 _g_assert_signal_received_run (gpointer object
,
158 const gchar
*signal_name
)
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
,
168 G_CALLBACK (on_signal_received
),
170 timeout_id
= g_timeout_add_seconds (30,
171 on_signal_received_timeout
,
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 /* ---------------------------------------------------------------------------------------------------- */
184 _g_bus_get_priv (GBusType bus_type
,
185 GCancellable
*cancellable
,
189 GDBusConnection
*ret
;
193 address
= g_dbus_address_get_for_bus_sync (bus_type
, cancellable
, error
);
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 */
209 /* ---------------------------------------------------------------------------------------------------- */