c9610c0bfc26ae124ded8ce25f115bc38e022d09
[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   GPollFD fd;
65
66   va_start (ap, first_arg);
67   array = g_ptr_array_new ();
68   g_ptr_array_add (array, g_test_build_filename (G_TEST_BUILT, "basic-application", NULL));
69   for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
70     g_ptr_array_add (array, g_strdup (arg));
71   g_ptr_array_add (array, NULL);
72   args = (gchar **) g_ptr_array_free (array, FALSE);
73
74   va_end (ap);
75
76   data = g_slice_new (ChildData);
77   data->expected_stdout = expected_stdout;
78
79   g_spawn_async_with_pipes (NULL, args, NULL,
80                             G_SPAWN_DO_NOT_REAP_CHILD,
81                             NULL, NULL, &pid, NULL,
82                             &data->stdout_pipe, NULL, &error);
83   g_assert_no_error (error);
84
85   g_child_watch_add (pid, child_quit, data);
86   outstanding_watches++;
87
88   /* we block until the children write to stdout to make sure
89    * they have started, as they need to be executed in order;
90    * see https://bugzilla.gnome.org/show_bug.cgi?id=664627
91    */
92   fd.fd = data->stdout_pipe;
93   fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
94   g_poll (&fd, 1, -1);
95 }
96
97 static void
98 basic (void)
99 {
100   GDBusConnection *c;
101
102   session_bus_up ();
103   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
104
105   main_loop = g_main_loop_new (NULL, 0);
106
107   /* spawn the master */
108   spawn ("activated\n"
109          "open file:///a file:///b\n"
110          "cmdline '40 +' '2'\n"
111          "exit status: 0\n",
112          "./app", NULL);
113
114   /* send it some files */
115   spawn ("exit status: 0\n",
116          "./app", "/a", "/b", NULL);
117
118   spawn ("40 + 2 = 42\n"
119          "exit status: 42\n",
120          "./cmd", "40 +", "2", NULL);
121
122   g_main_loop_run (main_loop);
123
124   g_object_unref (c);
125   session_bus_down ();
126 }
127
128
129 #if 0
130 /* Now that we register non-unique apps on the bus we need to fix the
131  * following test not to assume that it's safe to create multiple instances
132  * of the same app in one process.
133  *
134  * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
135  * introduced this problem.
136  */
137
138 static GApplication *recently_activated;
139 static GMainLoop *loop;
140
141 static void
142 nonunique_activate (GApplication *application)
143 {
144   recently_activated = application;
145
146   if (loop != NULL)
147     g_main_loop_quit (loop);
148 }
149
150 static GApplication *
151 make_app (gboolean non_unique)
152 {
153   GApplication *app;
154   gboolean ok;
155
156   app = g_application_new ("org.gtk.Test-Application",
157                            non_unique ? G_APPLICATION_NON_UNIQUE : 0);
158   g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
159   ok = g_application_register (app, NULL, NULL);
160   if (!ok)
161     {
162       g_object_unref (app);
163       return NULL;
164     }
165
166   g_application_activate (app);
167
168   return app;
169 }
170
171 static void
172 test_nonunique (void)
173 {
174   GApplication *first, *second, *third, *fourth;
175
176   session_bus_up ();
177
178   first = make_app (TRUE);
179   /* non-remote because it is non-unique */
180   g_assert (!g_application_get_is_remote (first));
181   g_assert (recently_activated == first);
182   recently_activated = NULL;
183
184   second = make_app (FALSE);
185   /* non-remote because it is first */
186   g_assert (!g_application_get_is_remote (second));
187   g_assert (recently_activated == second);
188   recently_activated = NULL;
189
190   third = make_app (TRUE);
191   /* non-remote because it is non-unique */
192   g_assert (!g_application_get_is_remote (third));
193   g_assert (recently_activated == third);
194   recently_activated = NULL;
195
196   fourth = make_app (FALSE);
197   /* should have failed to register due to being
198    * unable to register the object paths
199    */
200   g_assert (fourth == NULL);
201   g_assert (recently_activated == NULL);
202
203   g_object_unref (first);
204   g_object_unref (second);
205   g_object_unref (third);
206
207   session_bus_down ();
208 }
209 #endif
210
211 static void
212 properties (void)
213 {
214   GDBusConnection *c;
215   GObject *app;
216   gchar *id;
217   GApplicationFlags flags;
218   gboolean registered;
219   guint timeout;
220   gboolean remote;
221   gboolean ret;
222   GError *error = NULL;
223
224   session_bus_up ();
225   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
226
227   app = g_object_new (G_TYPE_APPLICATION,
228                       "application-id", "org.gtk.TestApplication",
229                       NULL);
230
231   g_object_get (app,
232                 "application-id", &id,
233                 "flags", &flags,
234                 "is-registered", &registered,
235                 "inactivity-timeout", &timeout,
236                 NULL);
237
238   g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
239   g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
240   g_assert (!registered);
241   g_assert_cmpint (timeout, ==, 0);
242
243   ret = g_application_register (G_APPLICATION (app), NULL, &error);
244   g_assert (ret);
245   g_assert_no_error (error);
246
247   g_object_get (app,
248                 "is-registered", &registered,
249                 "is-remote", &remote,
250                 NULL);
251
252   g_assert (registered);
253   g_assert (!remote);
254
255   g_object_set (app,
256                 "inactivity-timeout", 1000,
257                 NULL);
258
259   g_application_quit (G_APPLICATION (app));
260
261   g_object_unref (c);
262   g_object_unref (app);
263   g_free (id);
264
265   session_bus_down ();
266 }
267
268 static void
269 appid (void)
270 {
271   gchar *id;
272
273   g_assert (!g_application_id_is_valid (""));
274   g_assert (!g_application_id_is_valid ("."));
275   g_assert (!g_application_id_is_valid ("a"));
276   g_assert (!g_application_id_is_valid ("abc"));
277   g_assert (!g_application_id_is_valid (".abc"));
278   g_assert (!g_application_id_is_valid ("abc."));
279   g_assert (!g_application_id_is_valid ("a..b"));
280   g_assert (!g_application_id_is_valid ("a/b"));
281   g_assert (!g_application_id_is_valid ("a\nb"));
282   g_assert (!g_application_id_is_valid ("a\nb"));
283   g_assert (!g_application_id_is_valid ("_a.b"));
284   g_assert (!g_application_id_is_valid ("-a.b"));
285   id = g_new0 (gchar, 261);
286   memset (id, 'a', 260);
287   id[1] = '.';
288   id[260] = 0;
289   g_assert (!g_application_id_is_valid (id));
290   g_free (id);
291
292   g_assert (g_application_id_is_valid ("a.b"));
293   g_assert (g_application_id_is_valid ("A.B"));
294   g_assert (g_application_id_is_valid ("A-.B"));
295   g_assert (g_application_id_is_valid ("a_b.c-d"));
296   g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
297 }
298
299 static gboolean nodbus_activated;
300
301 static gboolean
302 release_app (gpointer user_data)
303 {
304   g_application_release (user_data);
305   return G_SOURCE_REMOVE;
306 }
307
308 static void
309 nodbus_activate (GApplication *app)
310 {
311   nodbus_activated = TRUE;
312   g_application_hold (app);
313
314   g_assert (g_application_get_dbus_connection (app) == NULL);
315   g_assert (g_application_get_dbus_object_path (app) == NULL);
316
317   g_idle_add (release_app, app);
318 }
319
320 static void
321 test_nodbus (void)
322 {
323   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
324   gchar *argv[] = { binpath, NULL };
325   GApplication *app;
326
327   app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
328   g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
329   g_application_run (app, 1, argv);
330   g_object_unref (app);
331
332   g_assert (nodbus_activated);
333   g_free (binpath);
334 }
335
336 static gboolean noappid_activated;
337
338 static void
339 noappid_activate (GApplication *app)
340 {
341   noappid_activated = TRUE;
342   g_application_hold (app);
343
344   g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
345
346   g_idle_add (release_app, app);
347 }
348
349 /* test that no appid -> non-unique */
350 static void
351 test_noappid (void)
352 {
353   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
354   gchar *argv[] = { binpath, NULL };
355   GApplication *app;
356
357   app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
358   g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
359   g_application_run (app, 1, argv);
360   g_object_unref (app);
361
362   g_assert (noappid_activated);
363   g_free (binpath);
364 }
365
366
367 static gboolean
368 quit_app (gpointer user_data)
369 {
370   g_application_quit (user_data);
371   return G_SOURCE_REMOVE;
372 }
373
374 static gboolean quit_activated;
375
376 static void
377 quit_activate (GApplication *app)
378 {
379   quit_activated = TRUE;
380   g_application_hold (app);
381
382   g_assert (g_application_get_dbus_connection (app) != NULL);
383   g_assert (g_application_get_dbus_object_path (app) != NULL);
384
385   g_idle_add (quit_app, app);
386 }
387
388 static void
389 test_quit (void)
390 {
391   GDBusConnection *c;
392   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
393   gchar *argv[] = { binpath, NULL };
394   GApplication *app;
395
396   session_bus_up ();
397   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
398
399   app = g_application_new ("org.gtk.Unimportant",
400                            G_APPLICATION_FLAGS_NONE);
401   g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
402   g_application_run (app, 1, argv);
403   g_object_unref (app);
404   g_object_unref (c);
405
406   g_assert (quit_activated);
407
408   session_bus_down ();
409   g_free (binpath);
410 }
411
412 static void
413 on_activate (GApplication *app)
414 {
415   gchar **actions;
416   GAction *action;
417   GVariant *state;
418
419   g_assert (!g_application_get_is_remote (app));
420
421   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
422   g_assert (g_strv_length (actions) == 0);
423   g_strfreev (actions);
424
425   action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
426   g_action_map_add_action (G_ACTION_MAP (app), action);
427
428   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
429   g_assert (g_strv_length (actions) == 1);
430   g_strfreev (actions);
431
432   g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
433   state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
434   g_assert (g_variant_get_boolean (state) == TRUE);
435
436   g_action_map_remove_action (G_ACTION_MAP (app), "test");
437
438   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
439   g_assert (g_strv_length (actions) == 0);
440   g_strfreev (actions);
441
442   g_idle_add (quit_app, app);
443 }
444
445 static void
446 test_actions (void)
447 {
448   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
449   gchar *argv[] = { binpath, NULL };
450   GApplication *app;
451
452   app = g_application_new ("org.gtk.Unimportant",
453                            G_APPLICATION_FLAGS_NONE);
454   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
455   g_application_run (app, 1, argv);
456   g_object_unref (app);
457   g_free (binpath);
458 }
459
460 typedef GApplication TestLocCmdApp;
461 typedef GApplicationClass TestLocCmdAppClass;
462
463 static GType test_loc_cmd_app_get_type (void);
464 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
465
466 static void
467 test_loc_cmd_app_init (TestLocCmdApp *app)
468 {
469 }
470
471 static void
472 test_loc_cmd_app_startup (GApplication *app)
473 {
474   g_assert_not_reached ();
475 }
476
477 static void
478 test_loc_cmd_app_shutdown (GApplication *app)
479 {
480   g_assert_not_reached ();
481 }
482
483 static gboolean
484 test_loc_cmd_app_local_command_line (GApplication   *application,
485                                      gchar        ***arguments,
486                                      gint           *exit_status)
487 {
488   return TRUE;
489 }
490
491 static void
492 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
493 {
494   G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
495   G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
496   G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
497 }
498
499 static void
500 test_local_command_line (void)
501 {
502   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
503   gchar *argv[] = { binpath, "-invalid", NULL };
504   GApplication *app;
505
506   app = g_object_new (test_loc_cmd_app_get_type (),
507                       "application-id", "org.gtk.Unimportant",
508                       "flags", G_APPLICATION_FLAGS_NONE,
509                       NULL);
510   g_application_run (app, 1, argv);
511   g_object_unref (app);
512   g_free (binpath);
513 }
514
515 int
516 main (int argc, char **argv)
517 {
518   g_test_init (&argc, &argv, NULL);
519
520   g_test_dbus_unset ();
521
522   g_test_add_func ("/gapplication/no-dbus", test_nodbus);
523   g_test_add_func ("/gapplication/basic", basic);
524   g_test_add_func ("/gapplication/no-appid", test_noappid);
525 /*  g_test_add_func ("/gapplication/non-unique", test_nonunique); */
526   g_test_add_func ("/gapplication/properties", properties);
527   g_test_add_func ("/gapplication/app-id", appid);
528   g_test_add_func ("/gapplication/quit", test_quit);
529   g_test_add_func ("/gapplication/actions", test_actions);
530   g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
531
532   return g_test_run ();
533 }