tests: use new g_test_build_filename() API
[platform/upstream/glib.git] / gio / tests / gdbus-proxy-well-known-name.c
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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include "gdbus-tests.h"
28
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
31
32 /* ---------------------------------------------------------------------------------------------------- */
33
34 static void
35 proxy_new_cb (GObject       *source_object,
36               GAsyncResult  *res,
37               gpointer       user_data)
38 {
39   GDBusProxy **ret = user_data;
40   GError *error;
41
42   error = NULL;
43   *ret = g_dbus_proxy_new_finish (res, &error);
44   g_assert_no_error (error);
45   g_assert (ret != NULL);
46
47   g_main_loop_quit (loop);
48 }
49
50 static void
51 test_proxy_well_known_name (void)
52 {
53   GDBusProxy *p;
54   GDBusProxy *p2;
55   GDBusProxy *ap;
56   GDBusProxy *ap2;
57   GDBusConnection *c;
58   GError *error;
59   gchar *name_owner;
60   gchar **property_names;
61   GVariant *variant;
62   GVariant *result;
63   gchar *path;
64
65   session_bus_up ();
66
67   error = NULL;
68   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
69   g_assert_no_error (error);
70   g_assert (c != NULL);
71
72   error = NULL;
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 */
80                              &error);
81   g_assert_no_error (error);
82
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);
86
87   /* also for async: we shouldn't have a name owner nor any cached properties */
88   g_dbus_proxy_new (c,
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,
96                     &ap);
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);
100
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));
104   g_free (path);
105
106   /* check that we get the notify::g-name-owner signal */
107   _g_assert_property_notify (p, "g-name-owner");
108
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);
114   g_free (name_owner);
115   g_strfreev (property_names);
116
117   /* if we create another proxy with the service being available, check that
118    * it has a name owner and properties
119    */
120   error = NULL;
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 */
128                               &error);
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);
134   g_free (name_owner);
135   g_strfreev (property_names);
136
137   /* also for async: we should have a name owner and cached properties */
138   g_dbus_proxy_new (c,
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,
146                     &ap2);
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);
152   g_free (name_owner);
153   g_strfreev (property_names);
154
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);
172
173   /* Check that properties are updated on both p and p2 */
174   result = g_dbus_proxy_call_sync (p,
175                                    "FrobSetProperty",
176                                    g_variant_new ("(sv)",
177                                                   "y",
178                                                   g_variant_new_byte (42)),
179                                    G_DBUS_CALL_FLAGS_NONE,
180                                    -1,
181                                    NULL,
182                                    &error);
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);
204
205   /* Nuke the service and check that we get the signal and then don't
206    * have a name owner nor any cached properties
207    */
208   result = g_dbus_proxy_call_sync (p,
209                                    "Quit",
210                                    NULL,
211                                    G_DBUS_CALL_FLAGS_NONE,
212                                    -1,
213                                    NULL,
214                                    &error);
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);
219   /* and wait... */
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);
225
226   /* now bring back the server and wait for the proxy to be updated.. now
227    * the 'y' property should be back at 1...
228    */
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));
232   g_free (path);
233
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);
241   g_free (name_owner);
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);
248
249   g_object_unref (p2);
250   g_object_unref (p);
251   g_object_unref (ap2);
252   g_object_unref (ap);
253
254   g_object_unref (c);
255
256   /* tear down bus */
257   session_bus_down ();
258 }
259
260 /* ---------------------------------------------------------------------------------------------------- */
261
262 int
263 main (int   argc,
264       char *argv[])
265 {
266   gint ret;
267
268   g_test_init (&argc, &argv, NULL);
269
270   /* all the tests rely on a shared main loop */
271   loop = g_main_loop_new (NULL, FALSE);
272
273   g_test_dbus_unset ();
274
275   g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);
276
277   ret = g_test_run();
278   return ret;
279 }