nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* GLib testing framework examples and tests |
2 | * |
||
3 | * Copyright (C) 2008-2010 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, see <http://www.gnu.org/licenses/>. |
||
17 | * |
||
18 | * Author: David Zeuthen <davidz@redhat.com> |
||
19 | */ |
||
20 | |||
21 | #include <gio/gio.h> |
||
22 | #include <unistd.h> |
||
23 | |||
24 | #include "gdbus-tests.h" |
||
25 | |||
26 | /* ---------------------------------------------------------------------------------------------------- */ |
||
27 | |||
28 | typedef struct |
||
29 | { |
||
30 | GMainLoop *loop; |
||
31 | gboolean timed_out; |
||
32 | } PropertyNotifyData; |
||
33 | |||
34 | static void |
||
35 | on_property_notify (GObject *object, |
||
36 | GParamSpec *pspec, |
||
37 | gpointer user_data) |
||
38 | { |
||
39 | PropertyNotifyData *data = user_data; |
||
40 | g_main_loop_quit (data->loop); |
||
41 | } |
||
42 | |||
43 | static gboolean |
||
44 | on_property_notify_timeout (gpointer user_data) |
||
45 | { |
||
46 | PropertyNotifyData *data = user_data; |
||
47 | data->timed_out = TRUE; |
||
48 | g_main_loop_quit (data->loop); |
||
49 | return TRUE; |
||
50 | } |
||
51 | |||
52 | gboolean |
||
53 | _g_assert_property_notify_run (gpointer object, |
||
54 | const gchar *property_name) |
||
55 | { |
||
56 | gchar *s; |
||
57 | gulong handler_id; |
||
58 | guint timeout_id; |
||
59 | PropertyNotifyData data; |
||
60 | |||
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, |
||
65 | s, |
||
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); |
||
76 | |||
77 | return data.timed_out; |
||
78 | } |
||
79 | |||
80 | static gboolean |
||
81 | _give_up (gpointer data) |
||
82 | { |
||
83 | g_error ("%s", (const gchar *) data); |
||
84 | g_return_val_if_reached (TRUE); |
||
85 | } |
||
86 | |||
87 | void |
||
88 | ensure_gdbus_testserver_up (void) |
||
89 | { |
||
90 | guint id; |
||
91 | gchar *name_owner; |
||
92 | GDBusConnection *connection; |
||
93 | GDBusProxy *proxy; |
||
94 | GError *error = NULL; |
||
95 | |||
96 | connection = g_bus_get_sync (G_BUS_TYPE_SESSION, |
||
97 | NULL, |
||
98 | &error); |
||
99 | |||
100 | g_assert_no_error (error); |
||
101 | error = NULL; |
||
102 | |||
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); |
||
112 | |||
113 | id = g_timeout_add_seconds (60, _give_up, |
||
114 | "waited more than ~ 60s for gdbus-testserver to take its bus name"); |
||
115 | |||
116 | while (TRUE) |
||
117 | { |
||
118 | name_owner = g_dbus_proxy_get_name_owner (proxy); |
||
119 | |||
120 | if (name_owner != NULL) |
||
121 | break; |
||
122 | |||
123 | g_main_context_iteration (NULL, TRUE); |
||
124 | } |
||
125 | |||
126 | g_source_remove (id); |
||
127 | g_free (name_owner); |
||
128 | g_object_unref (proxy); |
||
129 | g_object_unref (connection); |
||
130 | } |
||
131 | |||
132 | /* ---------------------------------------------------------------------------------------------------- */ |
||
133 | |||
134 | typedef struct |
||
135 | { |
||
136 | GMainLoop *loop; |
||
137 | gboolean timed_out; |
||
138 | } SignalReceivedData; |
||
139 | |||
140 | static void |
||
141 | on_signal_received (gpointer user_data) |
||
142 | { |
||
143 | SignalReceivedData *data = user_data; |
||
144 | g_main_loop_quit (data->loop); |
||
145 | } |
||
146 | |||
147 | static gboolean |
||
148 | on_signal_received_timeout (gpointer user_data) |
||
149 | { |
||
150 | SignalReceivedData *data = user_data; |
||
151 | data->timed_out = TRUE; |
||
152 | g_main_loop_quit (data->loop); |
||
153 | return TRUE; |
||
154 | } |
||
155 | |||
156 | gboolean |
||
157 | _g_assert_signal_received_run (gpointer object, |
||
158 | const gchar *signal_name) |
||
159 | { |
||
160 | gulong handler_id; |
||
161 | guint timeout_id; |
||
162 | SignalReceivedData data; |
||
163 | |||
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); |
||
177 | |||
178 | return data.timed_out; |
||
179 | } |
||
180 | |||
181 | /* ---------------------------------------------------------------------------------------------------- */ |
||
182 | |||
183 | GDBusConnection * |
||
184 | _g_bus_get_priv (GBusType bus_type, |
||
185 | GCancellable *cancellable, |
||
186 | GError **error) |
||
187 | { |
||
188 | gchar *address; |
||
189 | GDBusConnection *ret; |
||
190 | |||
191 | ret = NULL; |
||
192 | |||
193 | address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error); |
||
194 | if (address == NULL) |
||
195 | goto out; |
||
196 | |||
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); |
||
204 | |||
205 | out: |
||
206 | return ret; |
||
207 | } |
||
208 | |||
209 | /* ---------------------------------------------------------------------------------------------------- */ |