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;
67 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
68 g_assert_no_error (error);
72 p = g_dbus_proxy_new_sync (c,
73 G_DBUS_PROXY_FLAGS_NONE,
74 NULL, /* GDBusInterfaceInfo* */
75 "com.example.TestService", /* name */
76 "/com/example/TestObject", /* object path */
77 "com.example.Frob", /* interface name */
78 NULL, /* GCancellable */
80 g_assert_no_error (error);
82 /* we shouldn't have a name owner nor any cached properties */
83 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
84 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
86 /* also for async: we shouldn't have a name owner nor any cached properties */
88 G_DBUS_PROXY_FLAGS_NONE,
89 NULL, /* GDBusInterfaceInfo* */
90 "com.example.TestService", /* name */
91 "/com/example/TestObject", /* object path */
92 "com.example.Frob", /* interface name */
93 NULL, /* GCancellable */
94 (GAsyncReadyCallback) proxy_new_cb,
96 g_main_loop_run (loop);
97 g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
98 g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);
100 /* this is safe; testserver will exit once the bus goes away */
101 g_assert (g_spawn_command_line_async ("./gdbus-testserver", NULL));
103 /* check that we get the notify::g-name-owner signal */
104 _g_assert_property_notify (p, "g-name-owner");
106 /* Now we should have a name owner as well as properties */
107 name_owner = g_dbus_proxy_get_name_owner (p);
108 property_names = g_dbus_proxy_get_cached_property_names (p);
109 g_assert (g_dbus_is_unique_name (name_owner));
110 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
112 g_strfreev (property_names);
114 /* if we create another proxy with the service being available, check that
115 * it has a name owner and properties
118 p2 = g_dbus_proxy_new_sync (c,
119 G_DBUS_PROXY_FLAGS_NONE,
120 NULL, /* GDBusInterfaceInfo* */
121 "com.example.TestService", /* name */
122 "/com/example/TestObject", /* object path */
123 "com.example.Frob", /* interface name */
124 NULL, /* GCancellable */
126 g_assert_no_error (error);
127 name_owner = g_dbus_proxy_get_name_owner (p2);
128 property_names = g_dbus_proxy_get_cached_property_names (p2);
129 g_assert (g_dbus_is_unique_name (name_owner));
130 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
132 g_strfreev (property_names);
134 /* also for async: we should have a name owner and cached properties */
136 G_DBUS_PROXY_FLAGS_NONE,
137 NULL, /* GDBusInterfaceInfo* */
138 "com.example.TestService", /* name */
139 "/com/example/TestObject", /* object path */
140 "com.example.Frob", /* interface name */
141 NULL, /* GCancellable */
142 (GAsyncReadyCallback) proxy_new_cb,
144 g_main_loop_run (loop);
145 name_owner = g_dbus_proxy_get_name_owner (ap2);
146 property_names = g_dbus_proxy_get_cached_property_names (ap2);
147 g_assert (g_dbus_is_unique_name (name_owner));
148 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
150 g_strfreev (property_names);
152 /* Check property value is the initial value */
153 variant = g_dbus_proxy_get_cached_property (p, "y");
154 g_assert (variant != NULL);
155 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
156 g_variant_unref (variant);
157 variant = g_dbus_proxy_get_cached_property (p2, "y");
158 g_assert (variant != NULL);
159 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
160 g_variant_unref (variant);
161 variant = g_dbus_proxy_get_cached_property (ap, "y");
162 g_assert (variant != NULL);
163 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
164 g_variant_unref (variant);
165 variant = g_dbus_proxy_get_cached_property (ap2, "y");
166 g_assert (variant != NULL);
167 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
168 g_variant_unref (variant);
170 /* Check that properties are updated on both p and p2 */
171 result = g_dbus_proxy_call_sync (p,
173 g_variant_new ("(sv)",
175 g_variant_new_byte (42)),
176 G_DBUS_CALL_FLAGS_NONE,
180 g_assert_no_error (error);
181 g_assert (result != NULL);
182 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
183 g_variant_unref (result);
184 _g_assert_signal_received (p, "g-properties-changed");
185 variant = g_dbus_proxy_get_cached_property (p, "y");
186 g_assert (variant != NULL);
187 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
188 g_variant_unref (variant);
189 variant = g_dbus_proxy_get_cached_property (p2, "y");
190 g_assert (variant != NULL);
191 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
192 g_variant_unref (variant);
193 variant = g_dbus_proxy_get_cached_property (ap, "y");
194 g_assert (variant != NULL);
195 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
196 g_variant_unref (variant);
197 variant = g_dbus_proxy_get_cached_property (ap2, "y");
198 g_assert (variant != NULL);
199 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
200 g_variant_unref (variant);
202 /* Nuke the service and check that we get the signal and then don't
203 * have a name owner nor any cached properties
205 result = g_dbus_proxy_call_sync (p,
208 G_DBUS_CALL_FLAGS_NONE,
212 g_assert_no_error (error);
213 g_assert (result != NULL);
214 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
215 g_variant_unref (result);
217 _g_assert_property_notify (p, "g-name-owner");
218 /* now we shouldn't have a name owner nor any cached properties */
219 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
220 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
221 g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);
223 /* now bring back the server and wait for the proxy to be updated.. now
224 * the 'y' property should be back at 1...
226 /* this is safe; testserver will exit once the bus goes away */
227 g_assert (g_spawn_command_line_async ("./gdbus-testserver", NULL));
228 /* check that we get the notify::g-name-owner signal */
229 _g_assert_property_notify (p, "g-name-owner");
230 /* Now we should have a name owner as well as properties */
231 name_owner = g_dbus_proxy_get_name_owner (p);
232 property_names = g_dbus_proxy_get_cached_property_names (p);
233 g_assert (g_dbus_is_unique_name (name_owner));
234 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
236 g_strfreev (property_names);
237 /* and finally check the 'y' property */
238 variant = g_dbus_proxy_get_cached_property (p, "y");
239 g_assert (variant != NULL);
240 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
241 g_variant_unref (variant);
245 g_object_unref (ap2);
254 /* ---------------------------------------------------------------------------------------------------- */
262 g_test_init (&argc, &argv, NULL);
264 /* all the tests rely on a shared main loop */
265 loop = g_main_loop_new (NULL, FALSE);
267 g_test_dbus_unset ();
269 g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);