Use GTestDBus in all GDBus unit tests
[platform/upstream/glib.git] / gio / tests / gapplication.c
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 #include "gdbus-tests.h"
7 #include "gdbus-sessionbus.h"
8
9 static gint outstanding_watches;
10 static GMainLoop *main_loop;
11
12 typedef struct
13 {
14   const gchar *expected_stdout;
15   gint stdout_pipe;
16 } ChildData;
17
18 static void
19 child_quit (GPid     pid,
20             gint     status,
21             gpointer data)
22 {
23   ChildData *child = data;
24   gssize expected, actual;
25   gchar *buffer;
26
27   g_assert_cmpint (status, ==, 0);
28
29   if (--outstanding_watches == 0)
30     g_main_loop_quit (main_loop);
31
32   expected = strlen (child->expected_stdout);
33   buffer = g_alloca (expected + 100);
34   actual = read (child->stdout_pipe, buffer, expected + 100);
35   close (child->stdout_pipe);
36
37   g_assert_cmpint (actual, >=, 0);
38
39   if (actual != expected ||
40       memcmp (buffer, child->expected_stdout, expected) != 0)
41     {
42       buffer[MIN(expected + 100, actual)] = '\0';
43
44       g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
45                child->expected_stdout,
46                (actual > expected) ? "truncated" : "full", buffer);
47     }
48
49   g_slice_free (ChildData, child);
50 }
51
52 static void
53 spawn (const gchar *expected_stdout,
54        const gchar *first_arg,
55        ...)
56 {
57   GError *error = NULL;
58   const gchar *arg;
59   GPtrArray *array;
60   ChildData *data;
61   gchar **args;
62   va_list ap;
63   GPid pid;
64
65   va_start (ap, first_arg);
66   array = g_ptr_array_new ();
67   g_ptr_array_add (array, g_strdup ("./basic-application"));
68   for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
69     g_ptr_array_add (array, g_strdup (arg));
70   g_ptr_array_add (array, NULL);
71   args = (gchar **) g_ptr_array_free (array, FALSE);
72
73   data = g_slice_new (ChildData);
74   data->expected_stdout = expected_stdout;
75
76   g_spawn_async_with_pipes (NULL, args, NULL,
77                             G_SPAWN_DO_NOT_REAP_CHILD,
78                             NULL, NULL, &pid, NULL,
79                             &data->stdout_pipe, NULL, &error);
80   g_assert_no_error (error);
81
82   g_child_watch_add (pid, child_quit, data);
83   outstanding_watches++;
84 }
85
86 static void
87 basic (void)
88 {
89   GDBusConnection *c;
90
91   session_bus_up ();
92   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
93
94   main_loop = g_main_loop_new (NULL, 0);
95
96   /* spawn the master */
97   spawn ("activated\n"
98          "open file:///a file:///b\n"
99          "cmdline '40 +' '2'\n"
100          "exit status: 0\n",
101          "./app", NULL);
102
103   /* make sure it becomes the master */
104   g_usleep (100000);
105
106   /* send it some files */
107   spawn ("exit status: 0\n",
108          "./app", "/a", "/b", NULL);
109
110   /* make sure the commandline arrives after the files */
111   g_usleep (100000);
112
113   spawn ("40 + 2 = 42\n"
114          "exit status: 42\n",
115          "./cmd", "40 +", "2", NULL);
116
117   g_main_loop_run (main_loop);
118
119   g_object_unref (c);
120   session_bus_down ();
121 }
122
123
124 #if 0
125 /* Now that we register non-unique apps on the bus we need to fix the
126  * following test not to assume that it's safe to create multiple instances
127  * of the same app in one process.
128  *
129  * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
130  * introduced this problem.
131  */
132
133 static GApplication *recently_activated;
134 static GMainLoop *loop;
135
136 static void
137 nonunique_activate (GApplication *application)
138 {
139   recently_activated = application;
140
141   if (loop != NULL)
142     g_main_loop_quit (loop);
143 }
144
145 static GApplication *
146 make_app (gboolean non_unique)
147 {
148   GApplication *app;
149   gboolean ok;
150
151   app = g_application_new ("org.gtk.Test-Application",
152                            non_unique ? G_APPLICATION_NON_UNIQUE : 0);
153   g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
154   ok = g_application_register (app, NULL, NULL);
155   if (!ok)
156     {
157       g_object_unref (app);
158       return NULL;
159     }
160
161   g_application_activate (app);
162
163   return app;
164 }
165
166 static void
167 test_nonunique (void)
168 {
169   GApplication *first, *second, *third, *fourth;
170
171   session_bus_up ();
172
173   first = make_app (TRUE);
174   /* non-remote because it is non-unique */
175   g_assert (!g_application_get_is_remote (first));
176   g_assert (recently_activated == first);
177   recently_activated = NULL;
178
179   second = make_app (FALSE);
180   /* non-remote because it is first */
181   g_assert (!g_application_get_is_remote (second));
182   g_assert (recently_activated == second);
183   recently_activated = NULL;
184
185   third = make_app (TRUE);
186   /* non-remote because it is non-unique */
187   g_assert (!g_application_get_is_remote (third));
188   g_assert (recently_activated == third);
189   recently_activated = NULL;
190
191   fourth = make_app (FALSE);
192   /* should have failed to register due to being
193    * unable to register the object paths
194    */
195   g_assert (fourth == NULL);
196   g_assert (recently_activated == NULL);
197
198   g_object_unref (first);
199   g_object_unref (second);
200   g_object_unref (third);
201
202   session_bus_down ();
203 }
204 #endif
205
206 static void
207 properties (void)
208 {
209   GDBusConnection *c;
210   GObject *app;
211   gchar *id;
212   GApplicationFlags flags;
213   gboolean registered;
214   guint timeout;
215   gboolean remote;
216   gboolean ret;
217   GError *error = NULL;
218
219   session_bus_up ();
220   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
221
222   app = g_object_new (G_TYPE_APPLICATION,
223                       "application-id", "org.gtk.TestApplication",
224                       NULL);
225
226   g_object_get (app,
227                 "application-id", &id,
228                 "flags", &flags,
229                 "is-registered", &registered,
230                 "inactivity-timeout", &timeout,
231                 NULL);
232
233   g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
234   g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
235   g_assert (!registered);
236   g_assert_cmpint (timeout, ==, 0);
237
238   ret = g_application_register (G_APPLICATION (app), NULL, &error);
239   g_assert (ret);
240   g_assert_no_error (error);
241
242   g_object_get (app,
243                 "is-registered", &registered,
244                 "is-remote", &remote,
245                 NULL);
246
247   g_assert (registered);
248   g_assert (!remote);
249
250   g_application_quit (G_APPLICATION (app));
251
252   g_object_unref (c);
253   g_object_unref (app);
254   g_free (id);
255
256   session_bus_down ();
257 }
258
259 static void
260 appid (void)
261 {
262   gchar *id;
263
264   g_assert (!g_application_id_is_valid (""));
265   g_assert (!g_application_id_is_valid ("."));
266   g_assert (!g_application_id_is_valid ("a"));
267   g_assert (!g_application_id_is_valid ("abc"));
268   g_assert (!g_application_id_is_valid (".abc"));
269   g_assert (!g_application_id_is_valid ("abc."));
270   g_assert (!g_application_id_is_valid ("a..b"));
271   g_assert (!g_application_id_is_valid ("a/b"));
272   g_assert (!g_application_id_is_valid ("a\nb"));
273   g_assert (!g_application_id_is_valid ("a\nb"));
274   g_assert (!g_application_id_is_valid ("_a.b"));
275   g_assert (!g_application_id_is_valid ("-a.b"));
276   id = g_new0 (gchar, 261);
277   memset (id, 'a', 260);
278   id[1] = '.';
279   id[260] = 0;
280   g_assert (!g_application_id_is_valid (id));
281   g_free (id);
282
283   g_assert (g_application_id_is_valid ("a.b"));
284   g_assert (g_application_id_is_valid ("A.B"));
285   g_assert (g_application_id_is_valid ("A-.B"));
286   g_assert (g_application_id_is_valid ("a_b.c-d"));
287   g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
288 }
289
290 static gboolean nodbus_activated;
291
292 static gboolean
293 release_app (gpointer user_data)
294 {
295   g_application_release (user_data);
296   return G_SOURCE_REMOVE;
297 }
298
299 static void
300 nodbus_activate (GApplication *app)
301 {
302   nodbus_activated = TRUE;
303   g_application_hold (app);
304   g_idle_add (release_app, app);
305 }
306
307 static void
308 test_nodbus (void)
309 {
310   gchar *argv[] = { "./unimportant", NULL };
311   GApplication *app;
312
313   app = g_application_new ("org.gtk.Unimportant",
314                            G_APPLICATION_FLAGS_NONE);
315   g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
316   g_application_run (app, 1, argv);
317   g_object_unref (app);
318
319   g_assert (nodbus_activated);
320 }
321
322 static gboolean
323 quit_app (gpointer user_data)
324 {
325   g_application_quit (user_data);
326   return G_SOURCE_REMOVE;
327 }
328
329 static gboolean quit_activated;
330
331 static void
332 quit_activate (GApplication *app)
333 {
334   quit_activated = TRUE;
335   g_application_hold (app);
336   g_idle_add (quit_app, app);
337 }
338
339 static void
340 test_quit (void)
341 {
342   GDBusConnection *c;
343   gchar *argv[] = { "./unimportant", NULL };
344   GApplication *app;
345
346   session_bus_up ();
347   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
348
349   app = g_application_new ("org.gtk.Unimportant",
350                            G_APPLICATION_FLAGS_NONE);
351   g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
352   g_application_run (app, 1, argv);
353   g_object_unref (app);
354   g_object_unref (c);
355
356   g_assert (quit_activated);
357
358   session_bus_down ();
359 }
360
361 static void
362 on_activate (GApplication *app)
363 {
364   gchar **actions;
365   GAction *action;
366   GVariant *state;
367
368   g_assert (!g_application_get_is_remote (app));
369
370   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
371   g_assert (g_strv_length (actions) == 0);
372   g_strfreev (actions);
373
374   action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
375   g_action_map_add_action (G_ACTION_MAP (app), action);
376
377   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
378   g_assert (g_strv_length (actions) == 1);
379   g_strfreev (actions);
380
381   g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
382   state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
383   g_assert (g_variant_get_boolean (state) == TRUE);
384
385   g_action_map_remove_action (G_ACTION_MAP (app), "test");
386
387   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
388   g_assert (g_strv_length (actions) == 0);
389   g_strfreev (actions);
390
391   g_idle_add (quit_app, app);
392 }
393
394 static void
395 test_actions (void)
396 {
397   gchar *argv[] = { "./unimportant", NULL };
398   GApplication *app;
399
400   g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
401
402   app = g_application_new ("org.gtk.Unimportant",
403                            G_APPLICATION_FLAGS_NONE);
404   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
405   g_application_run (app, 1, argv);
406   g_object_unref (app);
407 }
408
409 int
410 main (int argc, char **argv)
411 {
412   g_type_init ();
413
414   g_test_init (&argc, &argv, NULL);
415
416   g_test_dbus_unset ();
417
418   g_test_add_func ("/gapplication/no-dbus", test_nodbus);
419   g_test_add_func ("/gapplication/basic", basic);
420 /*  g_test_add_func ("/gapplication/non-unique", test_nonunique); */
421   g_test_add_func ("/gapplication/properties", properties);
422   g_test_add_func ("/gapplication/app-id", appid);
423   g_test_add_func ("/gapplication/quit", test_quit);
424   g_test_add_func ("/gapplication/actions", test_actions);
425
426   return g_test_run ();
427 }