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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include "gdbus-tests.h"
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
32 /* ---------------------------------------------------------------------------------------------------- */
35 proxy_new_cb (GObject *source_object,
39 GDBusProxy **ret = user_data;
43 *ret = g_dbus_proxy_new_finish (res, &error);
44 g_assert_no_error (error);
45 g_assert (ret != NULL);
47 g_main_loop_quit (loop);
51 test_proxy_well_known_name (void)
60 gchar **property_names;
66 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
67 * until one can connect to the bus but that's not how things work right now
72 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
73 g_assert_no_error (error);
77 p = g_dbus_proxy_new_sync (c,
78 G_DBUS_PROXY_FLAGS_NONE,
79 NULL, /* GDBusInterfaceInfo* */
80 "com.example.TestService", /* name */
81 "/com/example/TestObject", /* object path */
82 "com.example.Frob", /* interface name */
83 NULL, /* GCancellable */
85 g_assert_no_error (error);
87 /* we shouldn't have a name owner nor any cached properties */
88 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
89 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
91 /* also for async: we shouldn't have a name owner nor any cached properties */
93 G_DBUS_PROXY_FLAGS_NONE,
94 NULL, /* GDBusInterfaceInfo* */
95 "com.example.TestService", /* name */
96 "/com/example/TestObject", /* object path */
97 "com.example.Frob", /* interface name */
98 NULL, /* GCancellable */
99 (GAsyncReadyCallback) proxy_new_cb,
101 g_main_loop_run (loop);
102 g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
103 g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);
105 /* this is safe; testserver will exit once the bus goes away */
106 g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
108 /* check that we get the notify::g-name-owner signal */
109 _g_assert_property_notify (p, "g-name-owner");
111 /* Now we should have a name owner as well as properties */
112 name_owner = g_dbus_proxy_get_name_owner (p);
113 property_names = g_dbus_proxy_get_cached_property_names (p);
114 g_assert (g_dbus_is_unique_name (name_owner));
115 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
117 g_strfreev (property_names);
119 /* if we create another proxy with the service being available, check that
120 * it has a name owner and properties
123 p2 = g_dbus_proxy_new_sync (c,
124 G_DBUS_PROXY_FLAGS_NONE,
125 NULL, /* GDBusInterfaceInfo* */
126 "com.example.TestService", /* name */
127 "/com/example/TestObject", /* object path */
128 "com.example.Frob", /* interface name */
129 NULL, /* GCancellable */
131 g_assert_no_error (error);
132 name_owner = g_dbus_proxy_get_name_owner (p2);
133 property_names = g_dbus_proxy_get_cached_property_names (p2);
134 g_assert (g_dbus_is_unique_name (name_owner));
135 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
137 g_strfreev (property_names);
139 /* also for async: we should have a name owner and cached properties */
141 G_DBUS_PROXY_FLAGS_NONE,
142 NULL, /* GDBusInterfaceInfo* */
143 "com.example.TestService", /* name */
144 "/com/example/TestObject", /* object path */
145 "com.example.Frob", /* interface name */
146 NULL, /* GCancellable */
147 (GAsyncReadyCallback) proxy_new_cb,
149 g_main_loop_run (loop);
150 name_owner = g_dbus_proxy_get_name_owner (ap2);
151 property_names = g_dbus_proxy_get_cached_property_names (ap2);
152 g_assert (g_dbus_is_unique_name (name_owner));
153 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
155 g_strfreev (property_names);
157 /* Check property value is the initial value */
158 variant = g_dbus_proxy_get_cached_property (p, "y");
159 g_assert (variant != NULL);
160 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
161 g_variant_unref (variant);
162 variant = g_dbus_proxy_get_cached_property (p2, "y");
163 g_assert (variant != NULL);
164 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
165 g_variant_unref (variant);
166 variant = g_dbus_proxy_get_cached_property (ap, "y");
167 g_assert (variant != NULL);
168 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
169 g_variant_unref (variant);
170 variant = g_dbus_proxy_get_cached_property (ap2, "y");
171 g_assert (variant != NULL);
172 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
173 g_variant_unref (variant);
175 /* Check that properties are updated on both p and p2 */
176 result = g_dbus_proxy_call_sync (p,
178 g_variant_new ("(sv)",
180 g_variant_new_byte (42)),
181 G_DBUS_CALL_FLAGS_NONE,
185 g_assert_no_error (error);
186 g_assert (result != NULL);
187 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
188 g_variant_unref (result);
189 _g_assert_signal_received (p, "g-properties-changed");
190 variant = g_dbus_proxy_get_cached_property (p, "y");
191 g_assert (variant != NULL);
192 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
193 g_variant_unref (variant);
194 variant = g_dbus_proxy_get_cached_property (p2, "y");
195 g_assert (variant != NULL);
196 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
197 g_variant_unref (variant);
198 variant = g_dbus_proxy_get_cached_property (ap, "y");
199 g_assert (variant != NULL);
200 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
201 g_variant_unref (variant);
202 variant = g_dbus_proxy_get_cached_property (ap2, "y");
203 g_assert (variant != NULL);
204 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
205 g_variant_unref (variant);
207 /* Nuke the service and check that we get the signal and then don't
208 * have a name owner nor any cached properties
210 result = g_dbus_proxy_call_sync (p,
213 G_DBUS_CALL_FLAGS_NONE,
217 g_assert_no_error (error);
218 g_assert (result != NULL);
219 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
220 g_variant_unref (result);
222 _g_assert_property_notify (p, "g-name-owner");
223 /* now we shouldn't have a name owner nor any cached properties */
224 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
225 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
226 g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);
228 /* now bring back the server and wait for the proxy to be updated.. now
229 * the 'y' property should be back at 1...
231 /* this is safe; testserver will exit once the bus goes away */
232 g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
233 /* check that we get the notify::g-name-owner signal */
234 _g_assert_property_notify (p, "g-name-owner");
235 /* Now we should have a name owner as well as properties */
236 name_owner = g_dbus_proxy_get_name_owner (p);
237 property_names = g_dbus_proxy_get_cached_property_names (p);
238 g_assert (g_dbus_is_unique_name (name_owner));
239 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
241 g_strfreev (property_names);
242 /* and finally check the 'y' property */
243 variant = g_dbus_proxy_get_cached_property (p, "y");
244 g_assert (variant != NULL);
245 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
246 g_variant_unref (variant);
250 g_object_unref (ap2);
259 /* ---------------------------------------------------------------------------------------------------- */
268 g_test_init (&argc, &argv, NULL);
270 /* all the tests rely on a shared main loop */
271 loop = g_main_loop_new (NULL, FALSE);
273 /* all the tests use a session bus with a well-known address that we can bring up and down
274 * using session_bus_up() and session_bus_down().
276 g_unsetenv ("DISPLAY");
277 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
279 g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);