kdbus: Fixup signal subscription
[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 #if 0
10 /* These tests are racy -- there is no guarantee about the order of data
11  * arriving over D-Bus.
12  *
13  * They're also a bit ridiculous -- GApplication was never meant to be
14  * abused in this way...
15  *
16  * We need new tests.
17  */
18 static gint outstanding_watches;
19 static GMainLoop *main_loop;
20
21 typedef struct
22 {
23   gchar *expected_stdout;
24   gint stdout_pipe;
25   gchar *expected_stderr;
26   gint stderr_pipe;
27 } ChildData;
28
29 static void
30 check_data (gint fd, const gchar *expected)
31 {
32   gssize len, actual;
33   gchar *buffer;
34   
35   len = strlen (expected);
36   buffer = g_alloca (len + 100);
37   actual = read (fd, buffer, len + 100);
38
39   g_assert_cmpint (actual, >=, 0);
40
41   if (actual != len ||
42       memcmp (buffer, expected, len) != 0)
43     {
44       buffer[MIN(len + 100, actual)] = '\0';
45
46       g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
47                expected,
48                (actual > len) ? "truncated" : "full", buffer);
49     }
50 }
51
52 static void
53 child_quit (GPid     pid,
54             gint     status,
55             gpointer data)
56 {
57   ChildData *child = data;
58
59   g_assert_cmpint (status, ==, 0);
60
61   if (--outstanding_watches == 0)
62     g_main_loop_quit (main_loop);
63
64   check_data (child->stdout_pipe, child->expected_stdout);
65   close (child->stdout_pipe);
66   g_free (child->expected_stdout);
67
68   if (child->expected_stderr)
69     {
70       check_data (child->stderr_pipe, child->expected_stderr);
71       close (child->stderr_pipe);
72       g_free (child->expected_stderr);
73     }
74
75   g_slice_free (ChildData, child);
76 }
77
78 static void
79 spawn (const gchar *expected_stdout,
80        const gchar *expected_stderr,
81        const gchar *first_arg,
82        ...)
83 {
84   GError *error = NULL;
85   const gchar *arg;
86   GPtrArray *array;
87   ChildData *data;
88   gchar **args;
89   va_list ap;
90   GPid pid;
91   GPollFD fd;
92   gchar **env;
93
94   va_start (ap, first_arg);
95   array = g_ptr_array_new ();
96   g_ptr_array_add (array, g_test_build_filename (G_TEST_BUILT, "basic-application", NULL));
97   for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
98     g_ptr_array_add (array, g_strdup (arg));
99   g_ptr_array_add (array, NULL);
100   args = (gchar **) g_ptr_array_free (array, FALSE);
101   va_end (ap);
102
103   env = g_environ_setenv (g_get_environ (), "TEST", "1", TRUE);
104
105   data = g_slice_new (ChildData);
106   data->expected_stdout = g_strdup (expected_stdout);
107   data->expected_stderr = g_strdup (expected_stderr);
108
109   g_spawn_async_with_pipes (NULL, args, env,
110                             G_SPAWN_DO_NOT_REAP_CHILD,
111                             NULL, NULL, &pid, NULL,
112                             &data->stdout_pipe,
113                             expected_stderr ? &data->stderr_pipe : NULL,
114                             &error);
115   g_assert_no_error (error);
116
117   g_strfreev (env);
118
119   g_child_watch_add (pid, child_quit, data);
120   outstanding_watches++;
121
122   /* we block until the children write to stdout to make sure
123    * they have started, as they need to be executed in order;
124    * see https://bugzilla.gnome.org/show_bug.cgi?id=664627
125    */
126   fd.fd = data->stdout_pipe;
127   fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
128   g_poll (&fd, 1, -1);
129 }
130
131 static void
132 basic (void)
133 {
134   GDBusConnection *c;
135
136   g_assert (outstanding_watches == 0);
137
138   session_bus_up ();
139   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
140
141   main_loop = g_main_loop_new (NULL, 0);
142
143   /* spawn the main instance */
144   spawn ("activated\n"
145          "open file:///a file:///b\n"
146          "exit status: 0\n", NULL,
147          "./app", NULL);
148
149   /* send it some files */
150   spawn ("exit status: 0\n", NULL,
151          "./app", "/a", "/b", NULL);
152
153   g_main_loop_run (main_loop);
154
155   g_object_unref (c);
156   session_bus_down ();
157
158   g_main_loop_unref (main_loop);
159 }
160
161 static void
162 test_remote_command_line (void)
163 {
164   GDBusConnection *c;
165   GFile *file;
166   gchar *replies;
167   gchar *cwd;
168
169   g_assert (outstanding_watches == 0);
170
171   session_bus_up ();
172   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
173
174   main_loop = g_main_loop_new (NULL, 0);
175
176   file = g_file_new_for_commandline_arg ("foo");
177   cwd = g_get_current_dir ();
178
179   replies = g_strconcat ("got ./cmd 0\n",
180                          "got ./cmd 1\n",
181                          "cmdline ./cmd echo --abc -d\n",
182                          "environment TEST=1\n",
183                          "getenv TEST=1\n",
184                          "file ", g_file_get_path (file), "\n",
185                          "properties ok\n",
186                          "cwd ", cwd, "\n",
187                          "busy\n",
188                          "idle\n",
189                          "stdin ok\n",        
190                          "exit status: 0\n",
191                          NULL);
192   g_object_unref (file);
193
194   /* spawn the main instance */
195   spawn (replies, NULL,
196          "./cmd", NULL);
197
198   g_free (replies);
199
200   /* send it a few commandlines */
201   spawn ("exit status: 0\n", NULL,
202          "./cmd", NULL);
203
204   spawn ("exit status: 0\n", NULL,
205          "./cmd", "echo", "--abc", "-d", NULL);
206
207   spawn ("exit status: 0\n", NULL,
208          "./cmd", "env", NULL);
209
210   spawn ("exit status: 0\n", NULL,
211          "./cmd", "getenv", NULL);
212
213   spawn ("print test\n"
214          "exit status: 0\n", NULL,
215          "./cmd", "print", "test", NULL);
216
217   spawn ("exit status: 0\n", "printerr test\n",
218          "./cmd", "printerr", "test", NULL);
219
220   spawn ("exit status: 0\n", NULL,
221          "./cmd", "file", "foo", NULL);
222
223   spawn ("exit status: 0\n", NULL,
224          "./cmd", "properties", NULL);
225
226   spawn ("exit status: 0\n", NULL,
227          "./cmd", "cwd", NULL);
228
229   spawn ("exit status: 0\n", NULL,
230          "./cmd", "busy", NULL);
231
232   spawn ("exit status: 0\n", NULL,
233          "./cmd", "idle", NULL);
234
235   spawn ("exit status: 0\n", NULL,
236          "./cmd", "stdin", NULL);
237
238   g_main_loop_run (main_loop);
239
240   g_object_unref (c);
241   session_bus_down ();
242
243   g_main_loop_unref (main_loop);
244 }
245
246 static void
247 test_remote_actions (void)
248 {
249   GDBusConnection *c;
250
251   g_assert (outstanding_watches == 0);
252
253   session_bus_up ();
254   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
255
256   main_loop = g_main_loop_new (NULL, 0);
257
258   /* spawn the main instance */
259   spawn ("got ./cmd 0\n"
260          "activate action1\n"
261          "change action2 1\n"
262          "exit status: 0\n", NULL,
263          "./cmd", NULL);
264
265   spawn ("actions quit new action1 action2\n"
266          "exit status: 0\n", NULL,
267          "./actions", "list", NULL);
268
269   spawn ("exit status: 0\n", NULL,
270          "./actions", "activate", NULL);
271
272   spawn ("exit status: 0\n", NULL,
273          "./actions", "set-state", NULL);
274
275   g_main_loop_run (main_loop);
276
277   g_object_unref (c);
278   session_bus_down ();
279
280   g_main_loop_unref (main_loop);
281 }
282 #endif
283
284 #if 0
285 /* Now that we register non-unique apps on the bus we need to fix the
286  * following test not to assume that it's safe to create multiple instances
287  * of the same app in one process.
288  *
289  * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
290  * introduced this problem.
291  */
292
293 static GApplication *recently_activated;
294 static GMainLoop *loop;
295
296 static void
297 nonunique_activate (GApplication *application)
298 {
299   recently_activated = application;
300
301   if (loop != NULL)
302     g_main_loop_quit (loop);
303 }
304
305 static GApplication *
306 make_app (gboolean non_unique)
307 {
308   GApplication *app;
309   gboolean ok;
310
311   app = g_application_new ("org.gtk.Test-Application",
312                            non_unique ? G_APPLICATION_NON_UNIQUE : 0);
313   g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
314   ok = g_application_register (app, NULL, NULL);
315   if (!ok)
316     {
317       g_object_unref (app);
318       return NULL;
319     }
320
321   g_application_activate (app);
322
323   return app;
324 }
325
326 static void
327 test_nonunique (void)
328 {
329   GApplication *first, *second, *third, *fourth;
330
331   session_bus_up ();
332
333   first = make_app (TRUE);
334   /* non-remote because it is non-unique */
335   g_assert (!g_application_get_is_remote (first));
336   g_assert (recently_activated == first);
337   recently_activated = NULL;
338
339   second = make_app (FALSE);
340   /* non-remote because it is first */
341   g_assert (!g_application_get_is_remote (second));
342   g_assert (recently_activated == second);
343   recently_activated = NULL;
344
345   third = make_app (TRUE);
346   /* non-remote because it is non-unique */
347   g_assert (!g_application_get_is_remote (third));
348   g_assert (recently_activated == third);
349   recently_activated = NULL;
350
351   fourth = make_app (FALSE);
352   /* should have failed to register due to being
353    * unable to register the object paths
354    */
355   g_assert (fourth == NULL);
356   g_assert (recently_activated == NULL);
357
358   g_object_unref (first);
359   g_object_unref (second);
360   g_object_unref (third);
361
362   session_bus_down ();
363 }
364 #endif
365
366 static void
367 properties (void)
368 {
369   GDBusConnection *c;
370   GObject *app;
371   gchar *id;
372   gchar *version;
373   GApplicationFlags flags;
374   gboolean registered;
375   guint timeout;
376   gboolean remote;
377   gboolean ret;
378   GError *error = NULL;
379
380   session_bus_up ();
381   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
382
383   app = g_object_new (G_TYPE_APPLICATION,
384                       "application-id", "org.gtk.TestApplication",
385                       "version", "1.0",
386                       NULL);
387
388   g_object_get (app,
389                 "application-id", &id,
390                 "version", &version,
391                 "flags", &flags,
392                 "is-registered", &registered,
393                 "inactivity-timeout", &timeout,
394                 NULL);
395
396   g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
397   g_assert_cmpstr (version, ==, "1.0");
398   g_assert_cmpint (flags, ==, G_APPLICATION_DEFAULT_FLAGS);
399   g_assert (!registered);
400   g_assert_cmpint (timeout, ==, 0);
401
402   g_clear_pointer (&version, g_free);
403
404   ret = g_application_register (G_APPLICATION (app), NULL, &error);
405   g_assert (ret);
406   g_assert_no_error (error);
407
408   g_object_get (app,
409                 "is-registered", &registered,
410                 "is-remote", &remote,
411                 NULL);
412
413   g_assert (registered);
414   g_assert (!remote);
415
416   g_object_set (app,
417                 "inactivity-timeout", 1000,
418                 NULL);
419
420   g_application_quit (G_APPLICATION (app));
421
422   g_object_unref (c);
423   g_object_unref (app);
424   g_free (id);
425
426   session_bus_down ();
427 }
428
429 static void
430 appid (void)
431 {
432   gchar *id;
433
434   g_assert_false (g_application_id_is_valid (""));
435   g_assert_false (g_application_id_is_valid ("."));
436   g_assert_false (g_application_id_is_valid ("a"));
437   g_assert_false (g_application_id_is_valid ("abc"));
438   g_assert_false (g_application_id_is_valid (".abc"));
439   g_assert_false (g_application_id_is_valid ("abc."));
440   g_assert_false (g_application_id_is_valid ("a..b"));
441   g_assert_false (g_application_id_is_valid ("a/b"));
442   g_assert_false (g_application_id_is_valid ("a\nb"));
443   g_assert_false (g_application_id_is_valid ("a\nb"));
444   g_assert_false (g_application_id_is_valid ("emoji_picker"));
445   g_assert_false (g_application_id_is_valid ("emoji-picker"));
446   g_assert_false (g_application_id_is_valid ("emojipicker"));
447   g_assert_false (g_application_id_is_valid ("my.Terminal.0123"));
448   id = g_new0 (gchar, 261);
449   memset (id, 'a', 260);
450   id[1] = '.';
451   id[260] = 0;
452   g_assert_false (g_application_id_is_valid (id));
453   g_free (id);
454
455   g_assert_true (g_application_id_is_valid ("a.b"));
456   g_assert_true (g_application_id_is_valid ("A.B"));
457   g_assert_true (g_application_id_is_valid ("A-.B"));
458   g_assert_true (g_application_id_is_valid ("a_b.c-d"));
459   g_assert_true (g_application_id_is_valid ("_a.b"));
460   g_assert_true (g_application_id_is_valid ("-a.b"));
461   g_assert_true (g_application_id_is_valid ("org.gnome.SessionManager"));
462   g_assert_true (g_application_id_is_valid ("my.Terminal._0123"));
463   g_assert_true (g_application_id_is_valid ("com.example.MyApp"));
464   g_assert_true (g_application_id_is_valid ("com.example.internal_apps.Calculator"));
465   g_assert_true (g_application_id_is_valid ("org._7_zip.Archiver"));
466 }
467
468 static gboolean nodbus_activated;
469
470 static gboolean
471 release_app (gpointer user_data)
472 {
473   g_application_release (user_data);
474   return G_SOURCE_REMOVE;
475 }
476
477 static void
478 nodbus_activate (GApplication *app)
479 {
480   nodbus_activated = TRUE;
481   g_application_hold (app);
482
483   g_assert (g_application_get_dbus_connection (app) == NULL);
484   g_assert (g_application_get_dbus_object_path (app) == NULL);
485
486   g_idle_add (release_app, app);
487 }
488
489 static void
490 test_nodbus (void)
491 {
492   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
493   gchar *argv[] = { binpath, NULL };
494   GApplication *app;
495
496   app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_DEFAULT_FLAGS);
497   g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
498   g_application_run (app, 1, argv);
499   g_object_unref (app);
500
501   g_assert (nodbus_activated);
502   g_free (binpath);
503 }
504
505 static gboolean noappid_activated;
506
507 static void
508 noappid_activate (GApplication *app)
509 {
510   noappid_activated = TRUE;
511   g_application_hold (app);
512
513   g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
514
515   g_idle_add (release_app, app);
516 }
517
518 /* test that no appid -> non-unique */
519 static void
520 test_noappid (void)
521 {
522   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
523   gchar *argv[] = { binpath, NULL };
524   GApplication *app;
525
526   app = g_application_new (NULL, G_APPLICATION_DEFAULT_FLAGS);
527   g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
528   g_application_run (app, 1, argv);
529   g_object_unref (app);
530
531   g_assert (noappid_activated);
532   g_free (binpath);
533 }
534
535 static gboolean activated;
536 static gboolean quitted;
537
538 static gboolean
539 quit_app (gpointer user_data)
540 {
541   quitted = TRUE;
542   g_application_quit (user_data);
543   return G_SOURCE_REMOVE;
544 }
545
546 static void
547 quit_activate (GApplication *app)
548 {
549   activated = TRUE;
550   g_application_hold (app);
551
552   g_assert (g_application_get_dbus_connection (app) != NULL);
553   g_assert (g_application_get_dbus_object_path (app) != NULL);
554
555   g_idle_add (quit_app, app);
556 }
557
558 static void
559 test_quit (void)
560 {
561   GDBusConnection *c;
562   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
563   gchar *argv[] = { binpath, NULL };
564   GApplication *app;
565
566   session_bus_up ();
567   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
568
569   app = g_application_new ("org.gtk.Unimportant",
570                            G_APPLICATION_DEFAULT_FLAGS);
571   activated = FALSE;
572   quitted = FALSE;
573   g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
574   g_application_run (app, 1, argv);
575   g_object_unref (app);
576   g_object_unref (c);
577
578   g_assert (activated);
579   g_assert (quitted);
580
581   session_bus_down ();
582   g_free (binpath);
583 }
584
585 typedef struct
586 {
587   gboolean shutdown;
588   GParamSpec *notify_spec; /* (owned) (nullable) */
589 } RegisteredData;
590
591 static void
592 on_registered_shutdown (GApplication *app,
593                         gpointer user_data)
594 {
595   RegisteredData *registered_data = user_data;
596
597   registered_data->shutdown = TRUE;
598 }
599
600 static void
601 on_registered_notify (GApplication *app,
602                       GParamSpec *spec,
603                       gpointer user_data)
604 {
605   RegisteredData *registered_data = user_data;
606   registered_data->notify_spec = g_param_spec_ref (spec);
607
608   if (g_application_get_is_registered (app))
609     g_assert_false (registered_data->shutdown);
610   else
611     g_assert_true (registered_data->shutdown);
612 }
613
614 static void
615 test_registered (void)
616 {
617   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
618   gchar *argv[] = { binpath, NULL };
619   RegisteredData registered_data = { FALSE, NULL };
620   GApplication *app;
621
622   app = g_application_new (NULL, G_APPLICATION_DEFAULT_FLAGS);
623   g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
624   g_signal_connect (app, "shutdown", G_CALLBACK (on_registered_shutdown), &registered_data);
625   g_signal_connect (app, "notify::is-registered", G_CALLBACK (on_registered_notify), &registered_data);
626
627   g_assert_null (registered_data.notify_spec);
628
629   g_assert_true (g_application_register (app, NULL, NULL));
630   g_assert_true (g_application_get_is_registered (app));
631
632   g_assert_nonnull (registered_data.notify_spec);
633   g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered");
634   g_clear_pointer (&registered_data.notify_spec, g_param_spec_unref);
635
636   g_assert_false (registered_data.shutdown);
637
638   g_application_run (app, 1, argv);
639
640   g_assert_true (registered_data.shutdown);
641   g_assert_false (g_application_get_is_registered (app));
642   g_assert_nonnull (registered_data.notify_spec);
643   g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered");
644   g_clear_pointer (&registered_data.notify_spec, g_param_spec_unref);
645
646   /* Register it again */
647   registered_data.shutdown = FALSE;
648   g_assert_true (g_application_register (app, NULL, NULL));
649   g_assert_true (g_application_get_is_registered (app));
650   g_assert_nonnull (registered_data.notify_spec);
651   g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered");
652   g_clear_pointer (&registered_data.notify_spec, g_param_spec_unref);
653   g_assert_false (registered_data.shutdown);
654
655   g_object_unref (app);
656
657   g_free (binpath);
658 }
659
660 static void
661 on_activate (GApplication *app)
662 {
663   gchar **actions;
664   GAction *action;
665   GVariant *state;
666
667   g_assert (!g_application_get_is_remote (app));
668
669   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
670   g_assert (g_strv_length (actions) == 0);
671   g_strfreev (actions);
672
673   action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
674   g_action_map_add_action (G_ACTION_MAP (app), action);
675
676   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
677   g_assert (g_strv_length (actions) == 1);
678   g_strfreev (actions);
679
680   g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
681   state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
682   g_assert (g_variant_get_boolean (state) == TRUE);
683
684   action = g_action_map_lookup_action (G_ACTION_MAP (app), "test");
685   g_assert (action != NULL);
686
687   g_action_map_remove_action (G_ACTION_MAP (app), "test");
688
689   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
690   g_assert (g_strv_length (actions) == 0);
691   g_strfreev (actions);
692 }
693
694 static void
695 test_local_actions (void)
696 {
697   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
698   gchar *argv[] = { binpath, NULL };
699   GApplication *app;
700
701   app = g_application_new ("org.gtk.Unimportant",
702                            G_APPLICATION_DEFAULT_FLAGS);
703   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
704   g_application_run (app, 1, argv);
705   g_object_unref (app);
706   g_free (binpath);
707 }
708
709 typedef GApplication TestLocCmdApp;
710 typedef GApplicationClass TestLocCmdAppClass;
711
712 static GType test_loc_cmd_app_get_type (void);
713 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
714
715 static void
716 test_loc_cmd_app_init (TestLocCmdApp *app)
717 {
718 }
719
720 static void
721 test_loc_cmd_app_startup (GApplication *app)
722 {
723   g_assert_not_reached ();
724 }
725
726 static void
727 test_loc_cmd_app_shutdown (GApplication *app)
728 {
729   g_assert_not_reached ();
730 }
731
732 static gboolean
733 test_loc_cmd_app_local_command_line (GApplication   *application,
734                                      gchar        ***arguments,
735                                      gint           *exit_status)
736 {
737   return TRUE;
738 }
739
740 static void
741 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
742 {
743   G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
744   G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
745   G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
746 }
747
748 static void
749 test_local_command_line (void)
750 {
751   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
752   gchar *argv[] = { binpath, "-invalid", NULL };
753   GApplication *app;
754
755   app = g_object_new (test_loc_cmd_app_get_type (),
756                       "application-id", "org.gtk.Unimportant",
757                       "flags", G_APPLICATION_DEFAULT_FLAGS,
758                       NULL);
759   g_application_run (app, 1, argv);
760   g_object_unref (app);
761   g_free (binpath);
762 }
763
764 static void
765 test_resource_path (void)
766 {
767   GApplication *app;
768
769   app = g_application_new ("x.y.z", 0);
770   g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
771
772   /* this should not change anything */
773   g_application_set_application_id (app, "a.b.c");
774   g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
775
776   /* but this should... */
777   g_application_set_resource_base_path (app, "/x");
778   g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x");
779
780   /* ... and this */
781   g_application_set_resource_base_path (app, NULL);
782   g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
783
784   g_object_unref (app);
785
786   /* Make sure that overriding at construction time works properly */
787   app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", "/a", NULL);
788   g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/a");
789   g_object_unref (app);
790
791   /* ... particularly if we override to NULL */
792   app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", NULL, NULL);
793   g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
794   g_object_unref (app);
795 }
796
797 static gint
798 test_help_command_line (GApplication            *app,
799                         GApplicationCommandLine *command_line,
800                         gpointer                 user_data)
801 {
802   gboolean *called = user_data;
803
804   *called = TRUE;
805
806   return 0;
807 }
808
809 /* Test whether --help is handled when HANDLES_COMMND_LINE is set and
810  * options have been added.
811  */
812 static void
813 test_help (void)
814 {
815   if (g_test_subprocess ())
816     {
817       char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
818       gchar *argv[] = { binpath, "--help", NULL };
819       GApplication *app;
820       gboolean called = FALSE;
821       int status;
822
823       app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE);
824       g_application_add_main_option (app, "foo", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
825       g_signal_connect (app, "command-line", G_CALLBACK (test_help_command_line), &called);
826
827       status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
828       g_assert (called == TRUE);
829       g_assert_cmpint (status, ==, 0);
830
831       g_object_unref (app);
832       g_free (binpath);
833       return;
834     }
835
836   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
837   g_test_trap_assert_passed ();
838   g_test_trap_assert_stdout ("*Application options*");
839 }
840
841 static gint
842 command_line_done_callback (GApplication            *app,
843                             GApplicationCommandLine *command_line,
844                             gpointer                 user_data)
845 {
846   gboolean *called = user_data;
847
848   *called = TRUE;
849
850   g_application_command_line_set_exit_status (command_line, 42);
851   g_application_command_line_done (command_line);
852
853   return 0;
854 }
855
856 /* Test whether 'command-line' handler return value is ignored
857  * after g_application_command_line_done()
858  */
859 static void
860 test_command_line_done (void)
861 {
862   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
863   const gchar *const argv[] = { binpath, "arg", NULL };
864   GApplication *app;
865   gboolean called = FALSE;
866   int status;
867   gulong command_line_id;
868
869   app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE);
870   command_line_id = g_signal_connect (app, "command-line", G_CALLBACK (command_line_done_callback), &called);
871
872   status = g_application_run (app, G_N_ELEMENTS (argv) - 1, (gchar **) argv);
873
874   g_signal_handler_disconnect (app, command_line_id);
875   g_object_unref (app);
876   g_free (binpath);
877
878   g_assert_true (called);
879   g_assert_cmpint (status, ==, 42);
880 }
881
882 static void
883 test_busy (void)
884 {
885   GApplication *app;
886
887   /* use GSimpleAction to bind to the busy state, because it's easy to
888    * create and has an easily modifiable boolean property */
889   GSimpleAction *action1;
890   GSimpleAction *action2;
891
892   session_bus_up ();
893
894   app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_NON_UNIQUE);
895   g_assert (g_application_register (app, NULL, NULL));
896
897   g_assert (!g_application_get_is_busy (app));
898   g_application_mark_busy (app);
899   g_assert (g_application_get_is_busy (app));
900   g_application_unmark_busy (app);
901   g_assert (!g_application_get_is_busy (app));
902
903   action1 = g_simple_action_new ("action", NULL);
904   g_application_bind_busy_property (app, action1, "enabled");
905   g_assert (g_application_get_is_busy (app));
906
907   g_simple_action_set_enabled (action1, FALSE);
908   g_assert (!g_application_get_is_busy (app));
909
910   g_application_mark_busy (app);
911   g_assert (g_application_get_is_busy (app));
912
913   action2 = g_simple_action_new ("action", NULL);
914   g_application_bind_busy_property (app, action2, "enabled");
915   g_assert (g_application_get_is_busy (app));
916
917   g_application_unmark_busy (app);
918   g_assert (g_application_get_is_busy (app));
919
920   g_object_unref (action2);
921   g_assert (!g_application_get_is_busy (app));
922
923   g_simple_action_set_enabled (action1, TRUE);
924   g_assert (g_application_get_is_busy (app));
925
926   g_application_mark_busy (app);
927   g_assert (g_application_get_is_busy (app));
928
929   g_application_unbind_busy_property (app, action1, "enabled");
930   g_assert (g_application_get_is_busy (app));
931
932   g_application_unmark_busy (app);
933   g_assert (!g_application_get_is_busy (app));
934
935   g_object_unref (action1);
936   g_object_unref (app);
937
938   session_bus_down ();
939 }
940
941 /*
942  * Test that handle-local-options works as expected
943  */
944
945 static gint
946 test_local_options (GApplication *app,
947                     GVariantDict *options,
948                     gpointer      data)
949 {
950   gboolean *called = data;
951
952   *called = TRUE;
953
954   if (g_variant_dict_contains (options, "success"))
955     return 0;
956   else if (g_variant_dict_contains (options, "failure"))
957     return 1;
958   else
959     return -1;
960 }
961
962 static gint
963 second_handler (GApplication *app,
964                 GVariantDict *options,
965                 gpointer      data)
966 {
967   gboolean *called = data;
968
969   *called = TRUE;
970
971   return 2;
972 }
973
974 static void
975 test_handle_local_options_success (void)
976 {
977   if (g_test_subprocess ())
978     {
979       char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
980       gchar *argv[] = { binpath, "--success", NULL };
981       GApplication *app;
982       gboolean called = FALSE;
983       gboolean called2 = FALSE;
984       int status;
985
986       app = g_application_new ("org.gtk.TestApplication", 0);
987       g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
988       g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
989       g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
990       g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
991
992       status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
993       g_assert (called);
994       g_assert (!called2);
995       g_assert_cmpint (status, ==, 0);
996
997       g_object_unref (app);
998       g_free (binpath);
999       return;
1000     }
1001
1002   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
1003   g_test_trap_assert_passed ();
1004 }
1005
1006 static void
1007 test_handle_local_options_failure (void)
1008 {
1009   if (g_test_subprocess ())
1010     {
1011       char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1012       gchar *argv[] = { binpath, "--failure", NULL };
1013       GApplication *app;
1014       gboolean called = FALSE;
1015       gboolean called2 = FALSE;
1016       int status;
1017
1018       app = g_application_new ("org.gtk.TestApplication", 0);
1019       g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
1020       g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
1021       g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
1022       g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
1023
1024       status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
1025       g_assert (called);
1026       g_assert (!called2);
1027       g_assert_cmpint (status, ==, 1);
1028
1029       g_object_unref (app);
1030       g_free (binpath);
1031       return;
1032     }
1033
1034   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
1035   g_test_trap_assert_passed ();
1036 }
1037
1038 static void
1039 test_handle_local_options_passthrough (void)
1040 {
1041   if (g_test_subprocess ())
1042     {
1043       char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1044       gchar *argv[] = { binpath, NULL };
1045       GApplication *app;
1046       gboolean called = FALSE;
1047       gboolean called2 = FALSE;
1048       int status;
1049
1050       app = g_application_new ("org.gtk.TestApplication", 0);
1051       g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
1052       g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
1053       g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
1054       g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
1055
1056       status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
1057       g_assert (called);
1058       g_assert (called2);
1059       g_assert_cmpint (status, ==, 2);
1060
1061       g_object_unref (app);
1062       g_free (binpath);
1063       return;
1064     }
1065
1066   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
1067   g_test_trap_assert_passed ();
1068 }
1069
1070 static void
1071 test_api (void)
1072 {
1073   GApplication *app;
1074   GSimpleAction *action;
1075
1076   app = g_application_new ("org.gtk.TestApplication", 0);
1077
1078   /* add an action without a name */
1079   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*assertion*failed*");
1080   action = g_simple_action_new (NULL, NULL);
1081   g_assert (action == NULL);
1082   g_test_assert_expected_messages ();
1083
1084   /* also, gapplication shouldn't accept actions without names */
1085   action = g_object_new (G_TYPE_SIMPLE_ACTION, NULL);
1086   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*action has no name*");
1087   g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
1088   g_test_assert_expected_messages ();
1089
1090   g_object_unref (action);
1091   g_object_unref (app);
1092 }
1093
1094 static void
1095 test_version (void)
1096 {
1097   GApplication *app;
1098   gchar *version = NULL;
1099   gchar *version_orig = NULL;
1100
1101   app = g_application_new ("org.gtk.TestApplication", 0);
1102
1103   version_orig = "1.2";
1104   g_object_set (G_OBJECT (app), "version", version_orig, NULL);
1105   g_object_get (app, "version", &version, NULL);
1106   g_assert_cmpstr (version, ==, version_orig);
1107   g_free (version);
1108
1109   /* test attempting to set the same version again */
1110   version_orig = "1.2";
1111   g_object_set (G_OBJECT (app), "version", version_orig, NULL);
1112   g_object_get (app, "version", &version, NULL);
1113   g_assert_cmpstr (version, ==, version_orig);
1114   g_free (version);
1115
1116   version_orig = "2.4";
1117   g_object_set (G_OBJECT (app), "version", version_orig, NULL);
1118   g_object_get (app, "version", &version, NULL);
1119   g_assert_cmpstr (version, ==, version_orig);
1120   g_free (version);
1121
1122   g_object_unref (app);
1123 }
1124
1125 /* Check that G_APPLICATION_ALLOW_REPLACEMENT works. To do so, we launch
1126  * a GApplication in this process that allows replacement, and then
1127  * launch a subprocess with --gapplication-replace. We have to do our
1128  * own async version of g_test_trap_subprocess() here since we need
1129  * the main process to keep spinning its mainloop.
1130  */
1131
1132 static gboolean
1133 name_was_lost (GApplication *app,
1134                gboolean     *called)
1135 {
1136   *called = TRUE;
1137   g_application_quit (app);
1138   return TRUE;
1139 }
1140
1141 static void
1142 startup_in_subprocess (GApplication *app,
1143                        gboolean     *called)
1144 {
1145   *called = TRUE;
1146 }
1147
1148 typedef struct
1149 {
1150   gboolean allow_replacement;
1151   GSubprocess *subprocess;
1152   GApplication *app;  /* (not owned) */
1153   guint timeout_id;
1154 } TestReplaceData;
1155
1156 static void
1157 startup_cb (GApplication *app,
1158             TestReplaceData *data)
1159 {
1160   const char *argv[] = { NULL, "--verbose", "--quiet", "-p", NULL, "--GTestSubprocess", NULL };
1161   GSubprocessLauncher *launcher;
1162   GError *local_error = NULL;
1163
1164   g_application_hold (app);
1165
1166   argv[0] = g_get_prgname ();
1167
1168   if (data->allow_replacement)
1169     argv[4] = "/gapplication/replace";
1170   else
1171     argv[4] = "/gapplication/no-replace";
1172
1173   /* Now that we are the primary instance, launch our replacement.
1174    * We inherit the environment to share the test session bus.
1175    */
1176   g_test_message ("launching subprocess");
1177
1178   launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
1179   g_subprocess_launcher_set_environ (launcher, NULL);
1180   data->subprocess = g_subprocess_launcher_spawnv (launcher, argv, &local_error);
1181   g_assert_no_error (local_error);
1182   g_object_unref (launcher);
1183
1184   if (!data->allow_replacement)
1185     {
1186       /* make sure we exit after a bit, if the subprocess is not replacing us */
1187       g_application_set_inactivity_timeout (app, 500);
1188       g_application_release (app);
1189     }
1190 }
1191
1192 static void
1193 activate (gpointer data)
1194 {
1195   /* GApplication complains if we don't connect to ::activate */
1196 }
1197
1198 static void
1199 quit_already (gpointer user_data)
1200 {
1201   TestReplaceData *data = user_data;
1202
1203   g_application_quit (data->app);
1204   data->timeout_id = 0;
1205 }
1206
1207 static void
1208 test_replace (gconstpointer data)
1209 {
1210   gboolean allow = GPOINTER_TO_INT (data);
1211
1212   if (g_test_subprocess ())
1213     {
1214       char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1215       char *argv[] = { binpath, "--gapplication-replace", NULL };
1216       GApplication *app;
1217       gboolean startup = FALSE;
1218
1219       app = g_application_new ("org.gtk.TestApplication.Replace", G_APPLICATION_ALLOW_REPLACEMENT);
1220       g_signal_connect (app, "startup", G_CALLBACK (startup_in_subprocess), &startup);
1221       g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
1222
1223       g_application_run (app, G_N_ELEMENTS (argv) - 1, argv);
1224
1225       if (allow)
1226         g_assert_true (startup);
1227       else
1228         g_assert_false (startup);
1229
1230       g_object_unref (app);
1231       g_free (binpath);
1232     }
1233   else
1234     {
1235       char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1236       gchar *argv[] = { binpath, NULL };
1237       GApplication *app;
1238       gboolean name_lost = FALSE;
1239       TestReplaceData data;
1240       GTestDBus *bus;
1241
1242       data.allow_replacement = allow;
1243       data.subprocess = NULL;
1244       data.timeout_id = 0;
1245
1246       bus = g_test_dbus_new (0);
1247       g_test_dbus_up (bus);
1248
1249       app = data.app = g_application_new ("org.gtk.TestApplication.Replace", allow ? G_APPLICATION_ALLOW_REPLACEMENT : G_APPLICATION_DEFAULT_FLAGS);
1250       g_application_set_inactivity_timeout (app, 500);
1251       g_signal_connect (app, "name-lost", G_CALLBACK (name_was_lost), &name_lost);
1252       g_signal_connect (app, "startup", G_CALLBACK (startup_cb), &data);
1253       g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
1254
1255       if (!allow)
1256         data.timeout_id = g_timeout_add_seconds_once (1, quit_already, &data);
1257
1258       g_application_run (app, G_N_ELEMENTS (argv) - 1, argv);
1259
1260       g_assert_nonnull (data.subprocess);
1261       if (allow)
1262         g_assert_true (name_lost);
1263       else
1264         g_assert_false (name_lost);
1265
1266       g_clear_handle_id (&data.timeout_id, g_source_remove);
1267       g_object_unref (app);
1268       g_free (binpath);
1269
1270       g_subprocess_wait (data.subprocess, NULL, NULL);
1271       g_clear_object (&data.subprocess);
1272
1273       g_test_dbus_down (bus);
1274       g_object_unref (bus);
1275     }
1276 }
1277
1278 static void
1279 dbus_activate_cb (GApplication *app,
1280                   gpointer      user_data)
1281 {
1282   guint *n_activations = user_data;
1283
1284   *n_activations = *n_activations + 1;
1285   g_main_context_wakeup (NULL);
1286 }
1287
1288 static void dbus_startup_reply_cb (GObject      *source_object,
1289                                    GAsyncResult *result,
1290                                    gpointer      user_data);
1291 static gboolean dbus_startup_reply_idle_cb (gpointer user_data);
1292
1293 static void
1294 dbus_startup_cb (GApplication *app,
1295                  gpointer      user_data)
1296 {
1297   GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1298   GDBusMessage *message = G_DBUS_MESSAGE (user_data);
1299
1300   g_assert_nonnull (connection);
1301
1302   g_dbus_connection_send_message_with_reply (connection, message,
1303                                              G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1,
1304                                              NULL, NULL,
1305                                              dbus_startup_reply_cb, g_object_ref (app));
1306
1307   g_clear_object (&connection);
1308 }
1309
1310 static void
1311 dbus_startup_reply_cb (GObject      *source_object,
1312                        GAsyncResult *result,
1313                        gpointer      user_data)
1314 {
1315   GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
1316   GApplication *app = G_APPLICATION (user_data);
1317   GDBusMessage *reply = NULL;
1318   GError *local_error = NULL;
1319
1320   reply = g_dbus_connection_send_message_with_reply_finish (connection, result, &local_error);
1321   g_assert_no_error (local_error);
1322
1323   g_object_set_data_full (G_OBJECT (app), "dbus-command-line-reply", g_steal_pointer (&reply), g_object_unref);
1324
1325   /* Release the app in an idle callback, so there’s time to process other
1326    * pending sources first. */
1327   g_idle_add_full (G_PRIORITY_LOW, dbus_startup_reply_idle_cb, g_steal_pointer (&app), g_object_unref);
1328 }
1329
1330 static gboolean
1331 dbus_startup_reply_idle_cb (gpointer user_data)
1332 {
1333   GApplication *app = G_APPLICATION (user_data);
1334
1335   g_application_release (app);
1336
1337   return G_SOURCE_REMOVE;
1338 }
1339
1340 static void
1341 test_dbus_activate (void)
1342 {
1343   GTestDBus *bus = NULL;
1344   GVariantBuilder builder;
1345   GDBusMessage *message = NULL;
1346   GPtrArray *messages = NULL;  /* (element-type GDBusMessage) (owned) */
1347   gsize i;
1348
1349   g_test_summary ("Test that calling the Activate D-Bus method works");
1350
1351   /* Try various different messages */
1352   messages = g_ptr_array_new_with_free_func (g_object_unref);
1353
1354   /* Via org.gtk.Application */
1355   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Activate",
1356                                             "/org/gtk/TestApplication/Activate",
1357                                             "org.gtk.Application",
1358                                             "Activate");
1359   g_dbus_message_set_body (message, g_variant_new ("(a{sv})", NULL));
1360   g_ptr_array_add (messages, g_steal_pointer (&message));
1361
1362   /* Via org.freedesktop.Application */
1363   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Activate",
1364                                             "/org/gtk/TestApplication/Activate",
1365                                             "org.freedesktop.Application",
1366                                             "Activate");
1367   g_dbus_message_set_body (message, g_variant_new ("(a{sv})", NULL));
1368   g_ptr_array_add (messages, g_steal_pointer (&message));
1369
1370   /* With some platform data */
1371   g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1372   g_variant_builder_add (&builder, "{sv}", "cwd", g_variant_new_bytestring ("/home/henry"));
1373
1374   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Activate",
1375                                             "/org/gtk/TestApplication/Activate",
1376                                             "org.gtk.Application",
1377                                             "Activate");
1378   g_dbus_message_set_body (message, g_variant_new ("(a{sv})", &builder));
1379   g_ptr_array_add (messages, g_steal_pointer (&message));
1380
1381   /* Try each message */
1382   bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1383   g_test_dbus_up (bus);
1384
1385   for (i = 0; i < messages->len; i++)
1386     {
1387       GApplication *app = NULL;
1388       gulong activate_id, startup_id;
1389       guint n_activations = 0;
1390
1391       g_test_message ("Message %" G_GSIZE_FORMAT, i);
1392
1393       app = g_application_new ("org.gtk.TestApplication.Activate", G_APPLICATION_DEFAULT_FLAGS);
1394       activate_id = g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_cb), &n_activations);
1395       startup_id = g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), messages->pdata[i]);
1396
1397       g_application_hold (app);
1398       g_application_run (app, 0, NULL);
1399
1400       /* It’ll be activated once as normal, and once due to the D-Bus call */
1401       g_assert_cmpuint (n_activations, ==, 2);
1402
1403       g_signal_handler_disconnect (app, startup_id);
1404       g_signal_handler_disconnect (app, activate_id);
1405       g_clear_object (&app);
1406     }
1407
1408   g_ptr_array_unref (messages);
1409
1410   g_test_dbus_down (bus);
1411   g_clear_object (&bus);
1412 }
1413
1414 static void
1415 dbus_activate_noop_cb (GApplication *app,
1416                        gpointer      user_data)
1417 {
1418   /* noop */
1419 }
1420
1421 static void
1422 dbus_open_cb (GApplication *app,
1423               gpointer      files,
1424               int           n_files,
1425               char         *hint,
1426               gpointer      user_data)
1427 {
1428   guint *n_opens = user_data;
1429
1430   *n_opens = *n_opens + 1;
1431   g_main_context_wakeup (NULL);
1432 }
1433
1434 static void
1435 test_dbus_open (void)
1436 {
1437   GTestDBus *bus = NULL;
1438   GVariantBuilder builder, builder2;
1439   GDBusMessage *message = NULL;
1440   GPtrArray *messages = NULL;  /* (element-type GDBusMessage) (owned) */
1441   gsize i;
1442
1443   g_test_summary ("Test that calling the Open D-Bus method works");
1444
1445   /* Try various different messages */
1446   messages = g_ptr_array_new_with_free_func (g_object_unref);
1447
1448   /* Via org.gtk.Application */
1449   g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1450   g_variant_builder_add (&builder, "s", "file:///home/henry/test");
1451
1452   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open",
1453                                             "/org/gtk/TestApplication/Open",
1454                                             "org.gtk.Application",
1455                                             "Open");
1456   g_dbus_message_set_body (message, g_variant_new ("(assa{sv})", &builder, "hint", NULL));
1457   g_ptr_array_add (messages, g_steal_pointer (&message));
1458
1459   /* Via org.freedesktop.Application (which has no hint parameter) */
1460   g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1461   g_variant_builder_add (&builder, "s", "file:///home/henry/test");
1462
1463   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open",
1464                                             "/org/gtk/TestApplication/Open",
1465                                             "org.freedesktop.Application",
1466                                             "Open");
1467   g_dbus_message_set_body (message, g_variant_new ("(asa{sv})", &builder, NULL));
1468   g_ptr_array_add (messages, g_steal_pointer (&message));
1469
1470   /* With some platform data and more than one file */
1471   g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1472   g_variant_builder_add (&builder, "s", "file:///home/henry/test");
1473   g_variant_builder_add (&builder, "s", "file:///home/henry/test2");
1474
1475   g_variant_builder_init (&builder2, G_VARIANT_TYPE ("a{sv}"));
1476   g_variant_builder_add (&builder2, "{sv}", "cwd", g_variant_new_bytestring ("/home/henry"));
1477
1478   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open",
1479                                             "/org/gtk/TestApplication/Open",
1480                                             "org.gtk.Application",
1481                                             "Open");
1482   g_dbus_message_set_body (message, g_variant_new ("(assa{sv})", &builder, "", &builder2));
1483   g_ptr_array_add (messages, g_steal_pointer (&message));
1484
1485   /* No files */
1486   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open",
1487                                             "/org/gtk/TestApplication/Open",
1488                                             "org.gtk.Application",
1489                                             "Open");
1490   g_dbus_message_set_body (message, g_variant_new ("(assa{sv})", NULL, "", NULL));
1491   g_ptr_array_add (messages, g_steal_pointer (&message));
1492
1493   /* Try each message */
1494   bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1495   g_test_dbus_up (bus);
1496
1497   for (i = 0; i < messages->len; i++)
1498     {
1499       GApplication *app = NULL;
1500       gulong activate_id, open_id, startup_id;
1501       guint n_opens = 0;
1502
1503       g_test_message ("Message %" G_GSIZE_FORMAT, i);
1504
1505       app = g_application_new ("org.gtk.TestApplication.Open", G_APPLICATION_HANDLES_OPEN);
1506       activate_id = g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_noop_cb), NULL);
1507       open_id = g_signal_connect (app, "open", G_CALLBACK (dbus_open_cb), &n_opens);
1508       startup_id = g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), messages->pdata[i]);
1509
1510       g_application_hold (app);
1511       g_application_run (app, 0, NULL);
1512
1513       g_assert_cmpuint (n_opens, ==, 1);
1514
1515       g_signal_handler_disconnect (app, startup_id);
1516       g_signal_handler_disconnect (app, open_id);
1517       g_signal_handler_disconnect (app, activate_id);
1518       g_clear_object (&app);
1519     }
1520
1521   g_ptr_array_unref (messages);
1522
1523   g_test_dbus_down (bus);
1524   g_clear_object (&bus);
1525 }
1526
1527 static void
1528 dbus_command_line_cb (GApplication            *app,
1529                       GApplicationCommandLine *command_line,
1530                       gpointer                 user_data)
1531 {
1532   guint *n_command_lines = user_data;
1533
1534   *n_command_lines = *n_command_lines + 1;
1535   g_main_context_wakeup (NULL);
1536 }
1537
1538 static void
1539 test_dbus_command_line (void)
1540 {
1541   GTestDBus *bus = NULL;
1542   GVariantBuilder builder, builder2;
1543   GDBusMessage *message = NULL;
1544   GPtrArray *messages = NULL;  /* (element-type GDBusMessage) (owned) */
1545   gsize i;
1546
1547   g_test_summary ("Test that calling the CommandLine D-Bus method works");
1548
1549   /* Try various different messages */
1550   messages = g_ptr_array_new_with_free_func (g_object_unref);
1551
1552   /* Via org.gtk.Application */
1553   g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay"));
1554   g_variant_builder_add (&builder, "^ay", "test-program");
1555   g_variant_builder_add (&builder, "^ay", "--open");
1556   g_variant_builder_add (&builder, "^ay", "/path/to/something");
1557
1558   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine",
1559                                             "/org/gtk/TestApplication/CommandLine",
1560                                             "org.gtk.Application",
1561                                             "CommandLine");
1562   g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})",
1563                                                    "/my/org/gtk/private/CommandLine",
1564                                                    &builder, NULL));
1565   g_ptr_array_add (messages, g_steal_pointer (&message));
1566
1567   /* With platform data */
1568   g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay"));
1569   g_variant_builder_add (&builder, "^ay", "test-program");
1570   g_variant_builder_add (&builder, "^ay", "--open");
1571   g_variant_builder_add (&builder, "^ay", "/path/to/something");
1572
1573   g_variant_builder_init (&builder2, G_VARIANT_TYPE ("a{sv}"));
1574   g_variant_builder_add (&builder2, "{sv}", "cwd", g_variant_new_bytestring ("/home"));
1575   g_variant_builder_add_parsed (&builder2, "{'environ', <@aay [ b'HOME=/home/bloop', b'PATH=/blah']>}");
1576   g_variant_builder_add_parsed (&builder2, "{'options', <{'a': <@u 32>, 'b': <'bloop'>}>}");
1577
1578   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine",
1579                                             "/org/gtk/TestApplication/CommandLine",
1580                                             "org.gtk.Application",
1581                                             "CommandLine");
1582   g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})",
1583                                                    "/my/org/gtk/private/CommandLine",
1584                                                    &builder, &builder2));
1585   g_ptr_array_add (messages, g_steal_pointer (&message));
1586
1587   /* With invalid typed platform data */
1588   g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay"));
1589   g_variant_builder_add (&builder, "^ay", "test-program");
1590   g_variant_builder_add (&builder, "^ay", "--open");
1591   g_variant_builder_add (&builder, "^ay", "/path/to/something");
1592
1593   g_variant_builder_init (&builder2, G_VARIANT_TYPE ("a{sv}"));
1594   g_variant_builder_add (&builder2, "{sv}", "cwd", g_variant_new_string ("/home should be a bytestring"));
1595   g_variant_builder_add_parsed (&builder2, "{'environ', <['HOME=should be a bytestring', 'PATH=this also']>}");
1596   g_variant_builder_add_parsed (&builder2, "{'options', <['should be a', 'dict']>}");
1597
1598   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine",
1599                                             "/org/gtk/TestApplication/CommandLine",
1600                                             "org.gtk.Application",
1601                                             "CommandLine");
1602   g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})",
1603                                                    "/my/org/gtk/private/CommandLine",
1604                                                    &builder, &builder2));
1605   g_ptr_array_add (messages, g_steal_pointer (&message));
1606
1607   /* Try each message */
1608   bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1609   g_test_dbus_up (bus);
1610
1611   for (i = 0; i < messages->len; i++)
1612     {
1613       GApplication *app = NULL;
1614       gulong activate_id, command_line_id, startup_id;
1615       guint n_command_lines = 0;
1616
1617       g_test_message ("Message %" G_GSIZE_FORMAT, i);
1618
1619       app = g_application_new ("org.gtk.TestApplication.CommandLine", G_APPLICATION_HANDLES_COMMAND_LINE);
1620       activate_id = g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_noop_cb), NULL);
1621       command_line_id = g_signal_connect (app, "command-line", G_CALLBACK (dbus_command_line_cb), &n_command_lines);
1622       startup_id = g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), messages->pdata[i]);
1623
1624       g_application_hold (app);
1625       g_application_run (app, 0, NULL);
1626
1627       /* It’s called once for handling the local command line on startup, and again
1628        * for the D-Bus call */
1629       g_assert_cmpuint (n_command_lines, ==, 2);
1630
1631       g_signal_handler_disconnect (app, startup_id);
1632       g_signal_handler_disconnect (app, command_line_id);
1633       g_signal_handler_disconnect (app, activate_id);
1634       g_clear_object (&app);
1635     }
1636
1637   g_ptr_array_unref (messages);
1638
1639   g_test_dbus_down (bus);
1640   g_clear_object (&bus);
1641 }
1642
1643 static gint
1644 dbus_command_line_done_cb (GApplication            *app,
1645                            GApplicationCommandLine *command_line,
1646                            gpointer                 user_data)
1647 {
1648   guint *n_command_lines = user_data;
1649
1650   *n_command_lines = *n_command_lines + 1;
1651
1652   if (*n_command_lines == 1)
1653     return 0;
1654
1655   g_object_set_data_full (G_OBJECT (app), "command-line", g_object_ref (command_line), g_object_unref);
1656
1657   g_application_command_line_set_exit_status (command_line, 42);
1658   g_application_command_line_done (command_line);
1659
1660   return 1; /* ignored - after g_application_command_line_done () */
1661 }
1662
1663 static void
1664 test_dbus_command_line_done (void)
1665 {
1666   GTestDBus *bus = NULL;
1667   GVariantBuilder builder;
1668   GDBusMessage *message = NULL;
1669   GDBusMessage *reply = NULL;
1670   GApplication *app = NULL;
1671   guint n_command_lines = 0;
1672   gint exit_status;
1673
1674   g_test_summary ("Test that GDBusCommandLine.done() works");
1675
1676   g_variant_builder_init (&builder, G_VARIANT_TYPE ("aay"));
1677   g_variant_builder_add (&builder, "^ay", "test-program");
1678   g_variant_builder_add (&builder, "^ay", "/path/to/something");
1679
1680   message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine",
1681                                             "/org/gtk/TestApplication/CommandLine",
1682                                             "org.gtk.Application",
1683                                             "CommandLine");
1684   g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})",
1685                                                    "/my/org/gtk/private/CommandLine",
1686                                                    &builder, NULL));
1687
1688   bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1689   g_test_dbus_up (bus);
1690
1691   app = g_application_new ("org.gtk.TestApplication.CommandLine", G_APPLICATION_HANDLES_COMMAND_LINE);
1692   g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_noop_cb), NULL);
1693   g_signal_connect (app, "command-line", G_CALLBACK (dbus_command_line_done_cb), &n_command_lines);
1694   g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), message);
1695
1696   g_application_hold (app);
1697   exit_status = g_application_run (app, 0, NULL);
1698
1699   g_assert_cmpuint (n_command_lines, ==, 2);
1700   g_assert_cmpint (exit_status, ==, 0);
1701
1702   reply = g_object_get_data (G_OBJECT (app), "dbus-command-line-reply");
1703   g_variant_get (g_dbus_message_get_body (reply), "(i)", &exit_status);
1704   g_assert_cmpint (exit_status, ==, 42);
1705
1706   g_signal_handlers_disconnect_by_func (app, G_CALLBACK (dbus_activate_noop_cb), NULL);
1707   g_signal_handlers_disconnect_by_func (app, G_CALLBACK (dbus_command_line_done_cb), &n_command_lines);
1708   g_signal_handlers_disconnect_by_func (app, G_CALLBACK (dbus_startup_cb), message);
1709
1710   g_clear_object (&app);
1711   g_clear_object (&message);
1712
1713   g_test_dbus_down (bus);
1714   g_clear_object (&bus);
1715 }
1716
1717 static void
1718 dbus_activate_action_cb (GSimpleAction *action,
1719                          GVariant      *parameter,
1720                          gpointer       user_data)
1721 {
1722   guint *n_activations = user_data;
1723
1724   *n_activations = *n_activations + 1;
1725   g_main_context_wakeup (NULL);
1726 }
1727
1728 static void
1729 test_dbus_activate_action (void)
1730 {
1731   GTestDBus *bus = NULL;
1732   GVariantBuilder builder;
1733   struct
1734     {
1735       GDBusMessage *message;  /* (not nullable) (owned) */
1736       guint n_expected_activations;
1737     } messages[6];
1738   gsize i;
1739
1740   g_test_summary ("Test that calling the ActivateAction D-Bus method works");
1741
1742   /* Action without parameter */
1743   messages[0].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1744                                                         "/org/gtk/TestApplication/ActivateAction",
1745                                                         "org.freedesktop.Application",
1746                                                         "ActivateAction");
1747   g_dbus_message_set_body (messages[0].message, g_variant_new ("(sava{sv})", "undo", NULL, NULL));
1748   messages[0].n_expected_activations = 1;
1749
1750   /* Action with parameter */
1751   g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
1752   g_variant_builder_add (&builder, "v", g_variant_new_string ("spanish"));
1753
1754   messages[1].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1755                                                         "/org/gtk/TestApplication/ActivateAction",
1756                                                         "org.freedesktop.Application",
1757                                                         "ActivateAction");
1758   g_dbus_message_set_body (messages[1].message, g_variant_new ("(sava{sv})", "lang", &builder, NULL));
1759   messages[1].n_expected_activations = 1;
1760
1761   /* Action with unexpected parameter */
1762   g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
1763   g_variant_builder_add (&builder, "v", g_variant_new_string ("should not be passed"));
1764
1765   messages[2].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1766                                                         "/org/gtk/TestApplication/ActivateAction",
1767                                                         "org.freedesktop.Application",
1768                                                         "ActivateAction");
1769   g_dbus_message_set_body (messages[2].message, g_variant_new ("(sava{sv})", "undo", &builder, NULL));
1770   messages[2].n_expected_activations = 0;
1771
1772   /* Action without required parameter */
1773   messages[3].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1774                                                         "/org/gtk/TestApplication/ActivateAction",
1775                                                         "org.freedesktop.Application",
1776                                                         "ActivateAction");
1777   g_dbus_message_set_body (messages[3].message, g_variant_new ("(sava{sv})", "lang", NULL, NULL));
1778   messages[3].n_expected_activations = 0;
1779
1780   /* Action with wrong parameter type */
1781   g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
1782   g_variant_builder_add (&builder, "v", g_variant_new_uint32 (42));
1783
1784   messages[4].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1785                                                         "/org/gtk/TestApplication/ActivateAction",
1786                                                         "org.freedesktop.Application",
1787                                                         "ActivateAction");
1788   g_dbus_message_set_body (messages[4].message, g_variant_new ("(sava{sv})", "lang", &builder, NULL));
1789   messages[4].n_expected_activations = 0;
1790
1791   /* Nonexistent action */
1792   messages[5].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1793                                                         "/org/gtk/TestApplication/ActivateAction",
1794                                                         "org.freedesktop.Application",
1795                                                         "ActivateAction");
1796   g_dbus_message_set_body (messages[5].message, g_variant_new ("(sava{sv})", "nonexistent", NULL, NULL));
1797   messages[5].n_expected_activations = 0;
1798
1799   /* Try each message */
1800   bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1801   g_test_dbus_up (bus);
1802
1803   for (i = 0; i < G_N_ELEMENTS (messages); i++)
1804     {
1805       GApplication *app = NULL;
1806       gulong activate_id, startup_id;
1807       const GActionEntry entries[] =
1808         {
1809           { "undo", dbus_activate_action_cb, NULL, NULL,      NULL, { 0 } },
1810           { "lang", dbus_activate_action_cb,  "s",  "'latin'", NULL, { 0 } },
1811         };
1812       guint n_activations = 0;
1813
1814       g_test_message ("Message %" G_GSIZE_FORMAT, i);
1815
1816       app = g_application_new ("org.gtk.TestApplication.ActivateAction", G_APPLICATION_DEFAULT_FLAGS);
1817       activate_id = g_signal_connect (app, "activate", G_CALLBACK (dbus_activate_noop_cb), NULL);
1818       startup_id = g_signal_connect (app, "startup", G_CALLBACK (dbus_startup_cb), messages[i].message);
1819
1820       /* Export some actions. */
1821       g_action_map_add_action_entries (G_ACTION_MAP (app), entries, G_N_ELEMENTS (entries), &n_activations);
1822
1823       g_application_hold (app);
1824       g_application_run (app, 0, NULL);
1825
1826       g_assert_cmpuint (n_activations, ==, messages[i].n_expected_activations);
1827
1828       g_signal_handler_disconnect (app, startup_id);
1829       g_signal_handler_disconnect (app, activate_id);
1830       g_clear_object (&app);
1831       g_clear_object (&messages[i].message);
1832     }
1833
1834   g_test_dbus_down (bus);
1835   g_clear_object (&bus);
1836 }
1837
1838 int
1839 main (int argc, char **argv)
1840 {
1841   g_setenv ("LC_ALL", "C", TRUE);
1842
1843   g_log_writer_default_set_use_stderr (TRUE);
1844
1845   g_test_init (&argc, &argv, NULL);
1846
1847   if (!g_test_subprocess ())
1848     g_test_dbus_unset ();
1849
1850   g_test_add_func ("/gapplication/no-dbus", test_nodbus);
1851 /*  g_test_add_func ("/gapplication/basic", basic); */
1852   g_test_add_func ("/gapplication/no-appid", test_noappid);
1853 /*  g_test_add_func ("/gapplication/non-unique", test_nonunique); */
1854   g_test_add_func ("/gapplication/properties", properties);
1855   g_test_add_func ("/gapplication/app-id", appid);
1856   g_test_add_func ("/gapplication/quit", test_quit);
1857   g_test_add_func ("/gapplication/registered", test_registered);
1858   g_test_add_func ("/gapplication/local-actions", test_local_actions);
1859 /*  g_test_add_func ("/gapplication/remote-actions", test_remote_actions); */
1860   g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
1861 /*  g_test_add_func ("/gapplication/remote-command-line", test_remote_command_line); */
1862   g_test_add_func ("/gapplication/resource-path", test_resource_path);
1863   g_test_add_func ("/gapplication/test-help", test_help);
1864   g_test_add_func ("/gapplication/command-line-done", test_command_line_done);
1865   g_test_add_func ("/gapplication/test-busy", test_busy);
1866   g_test_add_func ("/gapplication/test-handle-local-options1", test_handle_local_options_success);
1867   g_test_add_func ("/gapplication/test-handle-local-options2", test_handle_local_options_failure);
1868   g_test_add_func ("/gapplication/test-handle-local-options3", test_handle_local_options_passthrough);
1869   g_test_add_func ("/gapplication/api", test_api);
1870   g_test_add_func ("/gapplication/version", test_version);
1871   g_test_add_data_func ("/gapplication/replace", GINT_TO_POINTER (TRUE), test_replace);
1872   g_test_add_data_func ("/gapplication/no-replace", GINT_TO_POINTER (FALSE), test_replace);
1873   g_test_add_func ("/gapplication/dbus/activate", test_dbus_activate);
1874   g_test_add_func ("/gapplication/dbus/open", test_dbus_open);
1875   g_test_add_func ("/gapplication/dbus/command-line", test_dbus_command_line);
1876   g_test_add_func ("/gapplication/dbus/command-line-done", test_dbus_command_line_done);
1877   g_test_add_func ("/gapplication/dbus/activate-action", test_dbus_activate_action);
1878
1879   return g_test_run ();
1880 }