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;
68 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
69 g_assert_no_error (error);
73 p = g_dbus_proxy_new_sync (c,
74 G_DBUS_PROXY_FLAGS_NONE,
75 NULL, /* GDBusInterfaceInfo* */
76 "com.example.TestService", /* name */
77 "/com/example/TestObject", /* object path */
78 "com.example.Frob", /* interface name */
79 NULL, /* GCancellable */
81 g_assert_no_error (error);
83 /* we shouldn't have a name owner nor any cached properties */
84 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
85 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
87 /* also for async: we shouldn't have a name owner nor any cached properties */
89 G_DBUS_PROXY_FLAGS_NONE,
90 NULL, /* GDBusInterfaceInfo* */
91 "com.example.TestService", /* name */
92 "/com/example/TestObject", /* object path */
93 "com.example.Frob", /* interface name */
94 NULL, /* GCancellable */
95 (GAsyncReadyCallback) proxy_new_cb,
97 g_main_loop_run (loop);
98 g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
99 g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);
101 /* this is safe; testserver will exit once the bus goes away */
102 path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
103 g_assert (g_spawn_command_line_async (path, NULL));
106 /* check that we get the notify::g-name-owner signal */
107 _g_assert_property_notify (p, "g-name-owner");
109 /* Now we should have a name owner as well as properties */
110 name_owner = g_dbus_proxy_get_name_owner (p);
111 property_names = g_dbus_proxy_get_cached_property_names (p);
112 g_assert (g_dbus_is_unique_name (name_owner));
113 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
115 g_strfreev (property_names);
117 /* if we create another proxy with the service being available, check that
118 * it has a name owner and properties
121 p2 = g_dbus_proxy_new_sync (c,
122 G_DBUS_PROXY_FLAGS_NONE,
123 NULL, /* GDBusInterfaceInfo* */
124 "com.example.TestService", /* name */
125 "/com/example/TestObject", /* object path */
126 "com.example.Frob", /* interface name */
127 NULL, /* GCancellable */
129 g_assert_no_error (error);
130 name_owner = g_dbus_proxy_get_name_owner (p2);
131 property_names = g_dbus_proxy_get_cached_property_names (p2);
132 g_assert (g_dbus_is_unique_name (name_owner));
133 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
135 g_strfreev (property_names);
137 /* also for async: we should have a name owner and cached properties */
139 G_DBUS_PROXY_FLAGS_NONE,
140 NULL, /* GDBusInterfaceInfo* */
141 "com.example.TestService", /* name */
142 "/com/example/TestObject", /* object path */
143 "com.example.Frob", /* interface name */
144 NULL, /* GCancellable */
145 (GAsyncReadyCallback) proxy_new_cb,
147 g_main_loop_run (loop);
148 name_owner = g_dbus_proxy_get_name_owner (ap2);
149 property_names = g_dbus_proxy_get_cached_property_names (ap2);
150 g_assert (g_dbus_is_unique_name (name_owner));
151 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
153 g_strfreev (property_names);
155 /* Check property value is the initial value */
156 variant = g_dbus_proxy_get_cached_property (p, "y");
157 g_assert (variant != NULL);
158 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
159 g_variant_unref (variant);
160 variant = g_dbus_proxy_get_cached_property (p2, "y");
161 g_assert (variant != NULL);
162 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
163 g_variant_unref (variant);
164 variant = g_dbus_proxy_get_cached_property (ap, "y");
165 g_assert (variant != NULL);
166 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
167 g_variant_unref (variant);
168 variant = g_dbus_proxy_get_cached_property (ap2, "y");
169 g_assert (variant != NULL);
170 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
171 g_variant_unref (variant);
173 /* Check that properties are updated on both p and p2 */
174 result = g_dbus_proxy_call_sync (p,
176 g_variant_new ("(sv)",
178 g_variant_new_byte (42)),
179 G_DBUS_CALL_FLAGS_NONE,
183 g_assert_no_error (error);
184 g_assert (result != NULL);
185 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
186 g_variant_unref (result);
187 _g_assert_signal_received (p, "g-properties-changed");
188 variant = g_dbus_proxy_get_cached_property (p, "y");
189 g_assert (variant != NULL);
190 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
191 g_variant_unref (variant);
192 variant = g_dbus_proxy_get_cached_property (p2, "y");
193 g_assert (variant != NULL);
194 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
195 g_variant_unref (variant);
196 variant = g_dbus_proxy_get_cached_property (ap, "y");
197 g_assert (variant != NULL);
198 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
199 g_variant_unref (variant);
200 variant = g_dbus_proxy_get_cached_property (ap2, "y");
201 g_assert (variant != NULL);
202 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
203 g_variant_unref (variant);
205 /* Nuke the service and check that we get the signal and then don't
206 * have a name owner nor any cached properties
208 result = g_dbus_proxy_call_sync (p,
211 G_DBUS_CALL_FLAGS_NONE,
215 g_assert_no_error (error);
216 g_assert (result != NULL);
217 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
218 g_variant_unref (result);
220 _g_assert_property_notify (p, "g-name-owner");
221 /* now we shouldn't have a name owner nor any cached properties */
222 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
223 g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
224 g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);
226 /* now bring back the server and wait for the proxy to be updated.. now
227 * the 'y' property should be back at 1...
229 /* this is safe; testserver will exit once the bus goes away */
230 path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
231 g_assert (g_spawn_command_line_async (path, NULL));
234 /* check that we get the notify::g-name-owner signal */
235 _g_assert_property_notify (p, "g-name-owner");
236 /* Now we should have a name owner as well as properties */
237 name_owner = g_dbus_proxy_get_name_owner (p);
238 property_names = g_dbus_proxy_get_cached_property_names (p);
239 g_assert (g_dbus_is_unique_name (name_owner));
240 g_assert (property_names != NULL && g_strv_length (property_names) > 0);
242 g_strfreev (property_names);
243 /* and finally check the 'y' property */
244 variant = g_dbus_proxy_get_cached_property (p, "y");
245 g_assert (variant != NULL);
246 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
247 g_variant_unref (variant);
251 g_object_unref (ap2);
260 /* ---------------------------------------------------------------------------------------------------- */
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 g_test_dbus_unset ();
275 g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);