Improve GApplication test coverage
[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          "exit status: 0\n",
111          "./app", NULL);
112
113   /* send it some files */
114   spawn ("exit status: 0\n",
115          "./app", "/a", "/b", 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_object_set (app,
251                 "inactivity-timeout", 1000,
252                 NULL);
253
254   g_application_quit (G_APPLICATION (app));
255
256   g_object_unref (c);
257   g_object_unref (app);
258   g_free (id);
259
260   session_bus_down ();
261 }
262
263 static void
264 appid (void)
265 {
266   gchar *id;
267
268   g_assert (!g_application_id_is_valid (""));
269   g_assert (!g_application_id_is_valid ("."));
270   g_assert (!g_application_id_is_valid ("a"));
271   g_assert (!g_application_id_is_valid ("abc"));
272   g_assert (!g_application_id_is_valid (".abc"));
273   g_assert (!g_application_id_is_valid ("abc."));
274   g_assert (!g_application_id_is_valid ("a..b"));
275   g_assert (!g_application_id_is_valid ("a/b"));
276   g_assert (!g_application_id_is_valid ("a\nb"));
277   g_assert (!g_application_id_is_valid ("a\nb"));
278   g_assert (!g_application_id_is_valid ("_a.b"));
279   g_assert (!g_application_id_is_valid ("-a.b"));
280   id = g_new0 (gchar, 261);
281   memset (id, 'a', 260);
282   id[1] = '.';
283   id[260] = 0;
284   g_assert (!g_application_id_is_valid (id));
285   g_free (id);
286
287   g_assert (g_application_id_is_valid ("a.b"));
288   g_assert (g_application_id_is_valid ("A.B"));
289   g_assert (g_application_id_is_valid ("A-.B"));
290   g_assert (g_application_id_is_valid ("a_b.c-d"));
291   g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
292 }
293
294 static gboolean nodbus_activated;
295
296 static gboolean
297 release_app (gpointer user_data)
298 {
299   g_application_release (user_data);
300   return G_SOURCE_REMOVE;
301 }
302
303 static void
304 nodbus_activate (GApplication *app)
305 {
306   nodbus_activated = TRUE;
307   g_application_hold (app);
308
309   g_assert (g_application_get_dbus_connection (app) == NULL);
310   g_assert (g_application_get_dbus_object_path (app) == NULL);
311
312   g_idle_add (release_app, app);
313 }
314
315 static void
316 test_nodbus (void)
317 {
318   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
319   gchar *argv[] = { binpath, NULL };
320   GApplication *app;
321
322   app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
323   g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
324   g_application_run (app, 1, argv);
325   g_object_unref (app);
326
327   g_assert (nodbus_activated);
328   g_free (binpath);
329 }
330
331 static gboolean noappid_activated;
332
333 static void
334 noappid_activate (GApplication *app)
335 {
336   noappid_activated = TRUE;
337   g_application_hold (app);
338
339   g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
340
341   g_idle_add (release_app, app);
342 }
343
344 /* test that no appid -> non-unique */
345 static void
346 test_noappid (void)
347 {
348   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
349   gchar *argv[] = { binpath, NULL };
350   GApplication *app;
351
352   app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
353   g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
354   g_application_run (app, 1, argv);
355   g_object_unref (app);
356
357   g_assert (noappid_activated);
358   g_free (binpath);
359 }
360
361
362 static gboolean
363 quit_app (gpointer user_data)
364 {
365   g_application_quit (user_data);
366   return G_SOURCE_REMOVE;
367 }
368
369 static gboolean quit_activated;
370
371 static void
372 quit_activate (GApplication *app)
373 {
374   quit_activated = TRUE;
375   g_application_hold (app);
376
377   g_assert (g_application_get_dbus_connection (app) != NULL);
378   g_assert (g_application_get_dbus_object_path (app) != NULL);
379
380   g_idle_add (quit_app, app);
381 }
382
383 static void
384 test_quit (void)
385 {
386   GDBusConnection *c;
387   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
388   gchar *argv[] = { binpath, NULL };
389   GApplication *app;
390
391   session_bus_up ();
392   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
393
394   app = g_application_new ("org.gtk.Unimportant",
395                            G_APPLICATION_FLAGS_NONE);
396   g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
397   g_application_run (app, 1, argv);
398   g_object_unref (app);
399   g_object_unref (c);
400
401   g_assert (quit_activated);
402
403   session_bus_down ();
404   g_free (binpath);
405 }
406
407 static void
408 on_activate (GApplication *app)
409 {
410   gchar **actions;
411   GAction *action;
412   GVariant *state;
413
414   g_assert (!g_application_get_is_remote (app));
415
416   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
417   g_assert (g_strv_length (actions) == 0);
418   g_strfreev (actions);
419
420   action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
421   g_action_map_add_action (G_ACTION_MAP (app), action);
422
423   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
424   g_assert (g_strv_length (actions) == 1);
425   g_strfreev (actions);
426
427   g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
428   state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
429   g_assert (g_variant_get_boolean (state) == TRUE);
430
431   action = g_action_map_lookup_action (G_ACTION_MAP (app), "test");
432   g_assert (action != NULL);
433
434   g_action_map_remove_action (G_ACTION_MAP (app), "test");
435
436   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
437   g_assert (g_strv_length (actions) == 0);
438   g_strfreev (actions);
439
440   g_idle_add (quit_app, app);
441 }
442
443 static void
444 test_actions (void)
445 {
446   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
447   gchar *argv[] = { binpath, NULL };
448   GApplication *app;
449
450   app = g_application_new ("org.gtk.Unimportant",
451                            G_APPLICATION_FLAGS_NONE);
452   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
453   g_application_run (app, 1, argv);
454   g_object_unref (app);
455   g_free (binpath);
456 }
457
458 typedef GApplication TestLocCmdApp;
459 typedef GApplicationClass TestLocCmdAppClass;
460
461 static GType test_loc_cmd_app_get_type (void);
462 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
463
464 static void
465 test_loc_cmd_app_init (TestLocCmdApp *app)
466 {
467 }
468
469 static void
470 test_loc_cmd_app_startup (GApplication *app)
471 {
472   g_assert_not_reached ();
473 }
474
475 static void
476 test_loc_cmd_app_shutdown (GApplication *app)
477 {
478   g_assert_not_reached ();
479 }
480
481 static gboolean
482 test_loc_cmd_app_local_command_line (GApplication   *application,
483                                      gchar        ***arguments,
484                                      gint           *exit_status)
485 {
486   return TRUE;
487 }
488
489 static void
490 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
491 {
492   G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
493   G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
494   G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
495 }
496
497 static void
498 test_local_command_line (void)
499 {
500   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
501   gchar *argv[] = { binpath, "-invalid", NULL };
502   GApplication *app;
503
504   app = g_object_new (test_loc_cmd_app_get_type (),
505                       "application-id", "org.gtk.Unimportant",
506                       "flags", G_APPLICATION_FLAGS_NONE,
507                       NULL);
508   g_application_run (app, 1, argv);
509   g_object_unref (app);
510   g_free (binpath);
511 }
512
513 int
514 main (int argc, char **argv)
515 {
516   g_test_init (&argc, &argv, NULL);
517
518   g_test_dbus_unset ();
519
520   g_test_add_func ("/gapplication/no-dbus", test_nodbus);
521   g_test_add_func ("/gapplication/basic", basic);
522   g_test_add_func ("/gapplication/no-appid", test_noappid);
523 /*  g_test_add_func ("/gapplication/non-unique", test_nonunique); */
524   g_test_add_func ("/gapplication/properties", properties);
525   g_test_add_func ("/gapplication/app-id", appid);
526   g_test_add_func ("/gapplication/quit", test_quit);
527   g_test_add_func ("/gapplication/actions", test_actions);
528   g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
529
530   return g_test_run ();
531 }