6 #include "gdbus-tests.h"
7 #include "gdbus-sessionbus.h"
10 /* These tests are racy -- there is no guarantee about the order of data
11 * arriving over D-Bus.
13 * They're also a bit ridiculous -- GApplication was never meant to be
14 * abused in this way...
18 static gint outstanding_watches;
19 static GMainLoop *main_loop;
23 gchar *expected_stdout;
25 gchar *expected_stderr;
30 check_data (gint fd, const gchar *expected)
35 len = strlen (expected);
36 buffer = g_alloca (len + 100);
37 actual = read (fd, buffer, len + 100);
39 g_assert_cmpint (actual, >=, 0);
42 memcmp (buffer, expected, len) != 0)
44 buffer[MIN(len + 100, actual)] = '\0';
46 g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
48 (actual > len) ? "truncated" : "full", buffer);
57 ChildData *child = data;
59 g_assert_cmpint (status, ==, 0);
61 if (--outstanding_watches == 0)
62 g_main_loop_quit (main_loop);
64 check_data (child->stdout_pipe, child->expected_stdout);
65 close (child->stdout_pipe);
66 g_free (child->expected_stdout);
68 if (child->expected_stderr)
70 check_data (child->stderr_pipe, child->expected_stderr);
71 close (child->stderr_pipe);
72 g_free (child->expected_stderr);
75 g_slice_free (ChildData, child);
79 spawn (const gchar *expected_stdout,
80 const gchar *expected_stderr,
81 const gchar *first_arg,
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);
103 env = g_environ_setenv (g_get_environ (), "TEST", "1", TRUE);
105 data = g_slice_new (ChildData);
106 data->expected_stdout = g_strdup (expected_stdout);
107 data->expected_stderr = g_strdup (expected_stderr);
109 g_spawn_async_with_pipes (NULL, args, env,
110 G_SPAWN_DO_NOT_REAP_CHILD,
111 NULL, NULL, &pid, NULL,
113 expected_stderr ? &data->stderr_pipe : NULL,
115 g_assert_no_error (error);
119 g_child_watch_add (pid, child_quit, data);
120 outstanding_watches++;
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
126 fd.fd = data->stdout_pipe;
127 fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
136 g_assert (outstanding_watches == 0);
139 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
141 main_loop = g_main_loop_new (NULL, 0);
143 /* spawn the main instance */
145 "open file:///a file:///b\n"
146 "exit status: 0\n", NULL,
149 /* send it some files */
150 spawn ("exit status: 0\n", NULL,
151 "./app", "/a", "/b", NULL);
153 g_main_loop_run (main_loop);
158 g_main_loop_unref (main_loop);
162 test_remote_command_line (void)
169 g_assert (outstanding_watches == 0);
172 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
174 main_loop = g_main_loop_new (NULL, 0);
176 file = g_file_new_for_commandline_arg ("foo");
177 cwd = g_get_current_dir ();
179 replies = g_strconcat ("got ./cmd 0\n",
181 "cmdline ./cmd echo --abc -d\n",
182 "environment TEST=1\n",
184 "file ", g_file_get_path (file), "\n",
192 g_object_unref (file);
194 /* spawn the main instance */
195 spawn (replies, NULL,
200 /* send it a few commandlines */
201 spawn ("exit status: 0\n", NULL,
204 spawn ("exit status: 0\n", NULL,
205 "./cmd", "echo", "--abc", "-d", NULL);
207 spawn ("exit status: 0\n", NULL,
208 "./cmd", "env", NULL);
210 spawn ("exit status: 0\n", NULL,
211 "./cmd", "getenv", NULL);
213 spawn ("print test\n"
214 "exit status: 0\n", NULL,
215 "./cmd", "print", "test", NULL);
217 spawn ("exit status: 0\n", "printerr test\n",
218 "./cmd", "printerr", "test", NULL);
220 spawn ("exit status: 0\n", NULL,
221 "./cmd", "file", "foo", NULL);
223 spawn ("exit status: 0\n", NULL,
224 "./cmd", "properties", NULL);
226 spawn ("exit status: 0\n", NULL,
227 "./cmd", "cwd", NULL);
229 spawn ("exit status: 0\n", NULL,
230 "./cmd", "busy", NULL);
232 spawn ("exit status: 0\n", NULL,
233 "./cmd", "idle", NULL);
235 spawn ("exit status: 0\n", NULL,
236 "./cmd", "stdin", NULL);
238 g_main_loop_run (main_loop);
243 g_main_loop_unref (main_loop);
247 test_remote_actions (void)
251 g_assert (outstanding_watches == 0);
254 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
256 main_loop = g_main_loop_new (NULL, 0);
258 /* spawn the main instance */
259 spawn ("got ./cmd 0\n"
262 "exit status: 0\n", NULL,
265 spawn ("actions quit new action1 action2\n"
266 "exit status: 0\n", NULL,
267 "./actions", "list", NULL);
269 spawn ("exit status: 0\n", NULL,
270 "./actions", "activate", NULL);
272 spawn ("exit status: 0\n", NULL,
273 "./actions", "set-state", NULL);
275 g_main_loop_run (main_loop);
280 g_main_loop_unref (main_loop);
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.
289 * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
290 * introduced this problem.
293 static GApplication *recently_activated;
294 static GMainLoop *loop;
297 nonunique_activate (GApplication *application)
299 recently_activated = application;
302 g_main_loop_quit (loop);
305 static GApplication *
306 make_app (gboolean non_unique)
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);
317 g_object_unref (app);
321 g_application_activate (app);
327 test_nonunique (void)
329 GApplication *first, *second, *third, *fourth;
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;
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;
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;
351 fourth = make_app (FALSE);
352 /* should have failed to register due to being
353 * unable to register the object paths
355 g_assert (fourth == NULL);
356 g_assert (recently_activated == NULL);
358 g_object_unref (first);
359 g_object_unref (second);
360 g_object_unref (third);
373 GApplicationFlags flags;
378 GError *error = NULL;
381 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
383 app = g_object_new (G_TYPE_APPLICATION,
384 "application-id", "org.gtk.TestApplication",
389 "application-id", &id,
392 "is-registered", ®istered,
393 "inactivity-timeout", &timeout,
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);
402 g_clear_pointer (&version, g_free);
404 ret = g_application_register (G_APPLICATION (app), NULL, &error);
406 g_assert_no_error (error);
409 "is-registered", ®istered,
410 "is-remote", &remote,
413 g_assert (registered);
417 "inactivity-timeout", 1000,
420 g_application_quit (G_APPLICATION (app));
423 g_object_unref (app);
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);
452 g_assert_false (g_application_id_is_valid (id));
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"));
468 static gboolean nodbus_activated;
471 release_app (gpointer user_data)
473 g_application_release (user_data);
474 return G_SOURCE_REMOVE;
478 nodbus_activate (GApplication *app)
480 nodbus_activated = TRUE;
481 g_application_hold (app);
483 g_assert (g_application_get_dbus_connection (app) == NULL);
484 g_assert (g_application_get_dbus_object_path (app) == NULL);
486 g_idle_add (release_app, app);
492 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
493 gchar *argv[] = { binpath, NULL };
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);
501 g_assert (nodbus_activated);
505 static gboolean noappid_activated;
508 noappid_activate (GApplication *app)
510 noappid_activated = TRUE;
511 g_application_hold (app);
513 g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
515 g_idle_add (release_app, app);
518 /* test that no appid -> non-unique */
522 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
523 gchar *argv[] = { binpath, NULL };
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);
531 g_assert (noappid_activated);
535 static gboolean activated;
536 static gboolean quitted;
539 quit_app (gpointer user_data)
542 g_application_quit (user_data);
543 return G_SOURCE_REMOVE;
547 quit_activate (GApplication *app)
550 g_application_hold (app);
552 g_assert (g_application_get_dbus_connection (app) != NULL);
553 g_assert (g_application_get_dbus_object_path (app) != NULL);
555 g_idle_add (quit_app, app);
562 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
563 gchar *argv[] = { binpath, NULL };
567 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
569 app = g_application_new ("org.gtk.Unimportant",
570 G_APPLICATION_DEFAULT_FLAGS);
573 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
574 g_application_run (app, 1, argv);
575 g_object_unref (app);
578 g_assert (activated);
588 GParamSpec *notify_spec; /* (owned) (nullable) */
592 on_registered_shutdown (GApplication *app,
595 RegisteredData *registered_data = user_data;
597 registered_data->shutdown = TRUE;
601 on_registered_notify (GApplication *app,
605 RegisteredData *registered_data = user_data;
606 registered_data->notify_spec = g_param_spec_ref (spec);
608 if (g_application_get_is_registered (app))
609 g_assert_false (registered_data->shutdown);
611 g_assert_true (registered_data->shutdown);
615 test_registered (void)
617 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
618 gchar *argv[] = { binpath, NULL };
619 RegisteredData registered_data = { FALSE, NULL };
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), ®istered_data);
625 g_signal_connect (app, "notify::is-registered", G_CALLBACK (on_registered_notify), ®istered_data);
627 g_assert_null (registered_data.notify_spec);
629 g_assert_true (g_application_register (app, NULL, NULL));
630 g_assert_true (g_application_get_is_registered (app));
632 g_assert_nonnull (registered_data.notify_spec);
633 g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered");
634 g_clear_pointer (®istered_data.notify_spec, g_param_spec_unref);
636 g_assert_false (registered_data.shutdown);
638 g_application_run (app, 1, argv);
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 (®istered_data.notify_spec, g_param_spec_unref);
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 (®istered_data.notify_spec, g_param_spec_unref);
653 g_assert_false (registered_data.shutdown);
655 g_object_unref (app);
661 on_activate (GApplication *app)
667 g_assert (!g_application_get_is_remote (app));
669 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
670 g_assert (g_strv_length (actions) == 0);
671 g_strfreev (actions);
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);
676 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
677 g_assert (g_strv_length (actions) == 1);
678 g_strfreev (actions);
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);
684 action = g_action_map_lookup_action (G_ACTION_MAP (app), "test");
685 g_assert (action != NULL);
687 g_action_map_remove_action (G_ACTION_MAP (app), "test");
689 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
690 g_assert (g_strv_length (actions) == 0);
691 g_strfreev (actions);
695 test_local_actions (void)
697 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
698 gchar *argv[] = { binpath, NULL };
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);
709 typedef GApplication TestLocCmdApp;
710 typedef GApplicationClass TestLocCmdAppClass;
712 static GType test_loc_cmd_app_get_type (void);
713 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
716 test_loc_cmd_app_init (TestLocCmdApp *app)
721 test_loc_cmd_app_startup (GApplication *app)
723 g_assert_not_reached ();
727 test_loc_cmd_app_shutdown (GApplication *app)
729 g_assert_not_reached ();
733 test_loc_cmd_app_local_command_line (GApplication *application,
741 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
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;
749 test_local_command_line (void)
751 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
752 gchar *argv[] = { binpath, "-invalid", NULL };
755 app = g_object_new (test_loc_cmd_app_get_type (),
756 "application-id", "org.gtk.Unimportant",
757 "flags", G_APPLICATION_DEFAULT_FLAGS,
759 g_application_run (app, 1, argv);
760 g_object_unref (app);
765 test_resource_path (void)
769 app = g_application_new ("x.y.z", 0);
770 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
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");
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");
781 g_application_set_resource_base_path (app, NULL);
782 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
784 g_object_unref (app);
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);
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);
798 test_help_command_line (GApplication *app,
799 GApplicationCommandLine *command_line,
802 gboolean *called = user_data;
809 /* Test whether --help is handled when HANDLES_COMMND_LINE is set and
810 * options have been added.
815 if (g_test_subprocess ())
817 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
818 gchar *argv[] = { binpath, "--help", NULL };
820 gboolean called = FALSE;
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);
827 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
828 g_assert (called == TRUE);
829 g_assert_cmpint (status, ==, 0);
831 g_object_unref (app);
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*");
842 command_line_done_callback (GApplication *app,
843 GApplicationCommandLine *command_line,
846 gboolean *called = user_data;
850 g_application_command_line_set_exit_status (command_line, 42);
851 g_application_command_line_done (command_line);
856 /* Test whether 'command-line' handler return value is ignored
857 * after g_application_command_line_done()
860 test_command_line_done (void)
862 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
863 const gchar *const argv[] = { binpath, "arg", NULL };
865 gboolean called = FALSE;
867 gulong command_line_id;
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);
872 status = g_application_run (app, G_N_ELEMENTS (argv) - 1, (gchar **) argv);
874 g_signal_handler_disconnect (app, command_line_id);
875 g_object_unref (app);
878 g_assert_true (called);
879 g_assert_cmpint (status, ==, 42);
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;
894 app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_NON_UNIQUE);
895 g_assert (g_application_register (app, NULL, NULL));
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));
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));
907 g_simple_action_set_enabled (action1, FALSE);
908 g_assert (!g_application_get_is_busy (app));
910 g_application_mark_busy (app);
911 g_assert (g_application_get_is_busy (app));
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));
917 g_application_unmark_busy (app);
918 g_assert (g_application_get_is_busy (app));
920 g_object_unref (action2);
921 g_assert (!g_application_get_is_busy (app));
923 g_simple_action_set_enabled (action1, TRUE);
924 g_assert (g_application_get_is_busy (app));
926 g_application_mark_busy (app);
927 g_assert (g_application_get_is_busy (app));
929 g_application_unbind_busy_property (app, action1, "enabled");
930 g_assert (g_application_get_is_busy (app));
932 g_application_unmark_busy (app);
933 g_assert (!g_application_get_is_busy (app));
935 g_object_unref (action1);
936 g_object_unref (app);
942 * Test that handle-local-options works as expected
946 test_local_options (GApplication *app,
947 GVariantDict *options,
950 gboolean *called = data;
954 if (g_variant_dict_contains (options, "success"))
956 else if (g_variant_dict_contains (options, "failure"))
963 second_handler (GApplication *app,
964 GVariantDict *options,
967 gboolean *called = data;
975 test_handle_local_options_success (void)
977 if (g_test_subprocess ())
979 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
980 gchar *argv[] = { binpath, "--success", NULL };
982 gboolean called = FALSE;
983 gboolean called2 = FALSE;
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);
992 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
995 g_assert_cmpint (status, ==, 0);
997 g_object_unref (app);
1002 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
1003 g_test_trap_assert_passed ();
1007 test_handle_local_options_failure (void)
1009 if (g_test_subprocess ())
1011 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1012 gchar *argv[] = { binpath, "--failure", NULL };
1014 gboolean called = FALSE;
1015 gboolean called2 = FALSE;
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);
1024 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
1026 g_assert (!called2);
1027 g_assert_cmpint (status, ==, 1);
1029 g_object_unref (app);
1034 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
1035 g_test_trap_assert_passed ();
1039 test_handle_local_options_passthrough (void)
1041 if (g_test_subprocess ())
1043 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1044 gchar *argv[] = { binpath, NULL };
1046 gboolean called = FALSE;
1047 gboolean called2 = FALSE;
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);
1056 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
1059 g_assert_cmpint (status, ==, 2);
1061 g_object_unref (app);
1066 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
1067 g_test_trap_assert_passed ();
1074 GSimpleAction *action;
1076 app = g_application_new ("org.gtk.TestApplication", 0);
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 ();
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 ();
1090 g_object_unref (action);
1091 g_object_unref (app);
1098 gchar *version = NULL;
1099 gchar *version_orig = NULL;
1101 app = g_application_new ("org.gtk.TestApplication", 0);
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);
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);
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);
1122 g_object_unref (app);
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.
1133 name_was_lost (GApplication *app,
1137 g_application_quit (app);
1142 startup_in_subprocess (GApplication *app,
1150 gboolean allow_replacement;
1151 GSubprocess *subprocess;
1152 GApplication *app; /* (not owned) */
1157 startup_cb (GApplication *app,
1158 TestReplaceData *data)
1160 const char *argv[] = { NULL, "--verbose", "--quiet", "-p", NULL, "--GTestSubprocess", NULL };
1161 GSubprocessLauncher *launcher;
1162 GError *local_error = NULL;
1164 g_application_hold (app);
1166 argv[0] = g_get_prgname ();
1168 if (data->allow_replacement)
1169 argv[4] = "/gapplication/replace";
1171 argv[4] = "/gapplication/no-replace";
1173 /* Now that we are the primary instance, launch our replacement.
1174 * We inherit the environment to share the test session bus.
1176 g_test_message ("launching subprocess");
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);
1184 if (!data->allow_replacement)
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);
1193 activate (gpointer data)
1195 /* GApplication complains if we don't connect to ::activate */
1199 quit_already (gpointer user_data)
1201 TestReplaceData *data = user_data;
1203 g_application_quit (data->app);
1204 data->timeout_id = 0;
1208 test_replace (gconstpointer data)
1210 gboolean allow = GPOINTER_TO_INT (data);
1212 if (g_test_subprocess ())
1214 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1215 char *argv[] = { binpath, "--gapplication-replace", NULL };
1217 gboolean startup = FALSE;
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);
1223 g_application_run (app, G_N_ELEMENTS (argv) - 1, argv);
1226 g_assert_true (startup);
1228 g_assert_false (startup);
1230 g_object_unref (app);
1235 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1236 gchar *argv[] = { binpath, NULL };
1238 gboolean name_lost = FALSE;
1239 TestReplaceData data;
1242 data.allow_replacement = allow;
1243 data.subprocess = NULL;
1244 data.timeout_id = 0;
1246 bus = g_test_dbus_new (0);
1247 g_test_dbus_up (bus);
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);
1256 data.timeout_id = g_timeout_add_seconds_once (1, quit_already, &data);
1258 g_application_run (app, G_N_ELEMENTS (argv) - 1, argv);
1260 g_assert_nonnull (data.subprocess);
1262 g_assert_true (name_lost);
1264 g_assert_false (name_lost);
1266 g_clear_handle_id (&data.timeout_id, g_source_remove);
1267 g_object_unref (app);
1270 g_subprocess_wait (data.subprocess, NULL, NULL);
1271 g_clear_object (&data.subprocess);
1273 g_test_dbus_down (bus);
1274 g_object_unref (bus);
1279 dbus_activate_cb (GApplication *app,
1282 guint *n_activations = user_data;
1284 *n_activations = *n_activations + 1;
1285 g_main_context_wakeup (NULL);
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);
1294 dbus_startup_cb (GApplication *app,
1297 GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1298 GDBusMessage *message = G_DBUS_MESSAGE (user_data);
1300 g_assert_nonnull (connection);
1302 g_dbus_connection_send_message_with_reply (connection, message,
1303 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1,
1305 dbus_startup_reply_cb, g_object_ref (app));
1307 g_clear_object (&connection);
1311 dbus_startup_reply_cb (GObject *source_object,
1312 GAsyncResult *result,
1315 GDBusConnection *connection = G_DBUS_CONNECTION (source_object);
1316 GApplication *app = G_APPLICATION (user_data);
1317 GDBusMessage *reply = NULL;
1318 GError *local_error = NULL;
1320 reply = g_dbus_connection_send_message_with_reply_finish (connection, result, &local_error);
1321 g_assert_no_error (local_error);
1323 g_object_set_data_full (G_OBJECT (app), "dbus-command-line-reply", g_steal_pointer (&reply), g_object_unref);
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);
1331 dbus_startup_reply_idle_cb (gpointer user_data)
1333 GApplication *app = G_APPLICATION (user_data);
1335 g_application_release (app);
1337 return G_SOURCE_REMOVE;
1341 test_dbus_activate (void)
1343 GTestDBus *bus = NULL;
1344 GVariantBuilder builder;
1345 GDBusMessage *message = NULL;
1346 GPtrArray *messages = NULL; /* (element-type GDBusMessage) (owned) */
1349 g_test_summary ("Test that calling the Activate D-Bus method works");
1351 /* Try various different messages */
1352 messages = g_ptr_array_new_with_free_func (g_object_unref);
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",
1359 g_dbus_message_set_body (message, g_variant_new ("(a{sv})", NULL));
1360 g_ptr_array_add (messages, g_steal_pointer (&message));
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",
1367 g_dbus_message_set_body (message, g_variant_new ("(a{sv})", NULL));
1368 g_ptr_array_add (messages, g_steal_pointer (&message));
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"));
1374 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Activate",
1375 "/org/gtk/TestApplication/Activate",
1376 "org.gtk.Application",
1378 g_dbus_message_set_body (message, g_variant_new ("(a{sv})", &builder));
1379 g_ptr_array_add (messages, g_steal_pointer (&message));
1381 /* Try each message */
1382 bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1383 g_test_dbus_up (bus);
1385 for (i = 0; i < messages->len; i++)
1387 GApplication *app = NULL;
1388 gulong activate_id, startup_id;
1389 guint n_activations = 0;
1391 g_test_message ("Message %" G_GSIZE_FORMAT, i);
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]);
1397 g_application_hold (app);
1398 g_application_run (app, 0, NULL);
1400 /* It’ll be activated once as normal, and once due to the D-Bus call */
1401 g_assert_cmpuint (n_activations, ==, 2);
1403 g_signal_handler_disconnect (app, startup_id);
1404 g_signal_handler_disconnect (app, activate_id);
1405 g_clear_object (&app);
1408 g_ptr_array_unref (messages);
1410 g_test_dbus_down (bus);
1411 g_clear_object (&bus);
1415 dbus_activate_noop_cb (GApplication *app,
1422 dbus_open_cb (GApplication *app,
1428 guint *n_opens = user_data;
1430 *n_opens = *n_opens + 1;
1431 g_main_context_wakeup (NULL);
1435 test_dbus_open (void)
1437 GTestDBus *bus = NULL;
1438 GVariantBuilder builder, builder2;
1439 GDBusMessage *message = NULL;
1440 GPtrArray *messages = NULL; /* (element-type GDBusMessage) (owned) */
1443 g_test_summary ("Test that calling the Open D-Bus method works");
1445 /* Try various different messages */
1446 messages = g_ptr_array_new_with_free_func (g_object_unref);
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");
1452 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open",
1453 "/org/gtk/TestApplication/Open",
1454 "org.gtk.Application",
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));
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");
1463 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open",
1464 "/org/gtk/TestApplication/Open",
1465 "org.freedesktop.Application",
1467 g_dbus_message_set_body (message, g_variant_new ("(asa{sv})", &builder, NULL));
1468 g_ptr_array_add (messages, g_steal_pointer (&message));
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");
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"));
1478 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open",
1479 "/org/gtk/TestApplication/Open",
1480 "org.gtk.Application",
1482 g_dbus_message_set_body (message, g_variant_new ("(assa{sv})", &builder, "", &builder2));
1483 g_ptr_array_add (messages, g_steal_pointer (&message));
1486 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.Open",
1487 "/org/gtk/TestApplication/Open",
1488 "org.gtk.Application",
1490 g_dbus_message_set_body (message, g_variant_new ("(assa{sv})", NULL, "", NULL));
1491 g_ptr_array_add (messages, g_steal_pointer (&message));
1493 /* Try each message */
1494 bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1495 g_test_dbus_up (bus);
1497 for (i = 0; i < messages->len; i++)
1499 GApplication *app = NULL;
1500 gulong activate_id, open_id, startup_id;
1503 g_test_message ("Message %" G_GSIZE_FORMAT, i);
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]);
1510 g_application_hold (app);
1511 g_application_run (app, 0, NULL);
1513 g_assert_cmpuint (n_opens, ==, 1);
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);
1521 g_ptr_array_unref (messages);
1523 g_test_dbus_down (bus);
1524 g_clear_object (&bus);
1528 dbus_command_line_cb (GApplication *app,
1529 GApplicationCommandLine *command_line,
1532 guint *n_command_lines = user_data;
1534 *n_command_lines = *n_command_lines + 1;
1535 g_main_context_wakeup (NULL);
1539 test_dbus_command_line (void)
1541 GTestDBus *bus = NULL;
1542 GVariantBuilder builder, builder2;
1543 GDBusMessage *message = NULL;
1544 GPtrArray *messages = NULL; /* (element-type GDBusMessage) (owned) */
1547 g_test_summary ("Test that calling the CommandLine D-Bus method works");
1549 /* Try various different messages */
1550 messages = g_ptr_array_new_with_free_func (g_object_unref);
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");
1558 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine",
1559 "/org/gtk/TestApplication/CommandLine",
1560 "org.gtk.Application",
1562 g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})",
1563 "/my/org/gtk/private/CommandLine",
1565 g_ptr_array_add (messages, g_steal_pointer (&message));
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");
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'>}>}");
1578 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine",
1579 "/org/gtk/TestApplication/CommandLine",
1580 "org.gtk.Application",
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));
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");
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']>}");
1598 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine",
1599 "/org/gtk/TestApplication/CommandLine",
1600 "org.gtk.Application",
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));
1607 /* Try each message */
1608 bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1609 g_test_dbus_up (bus);
1611 for (i = 0; i < messages->len; i++)
1613 GApplication *app = NULL;
1614 gulong activate_id, command_line_id, startup_id;
1615 guint n_command_lines = 0;
1617 g_test_message ("Message %" G_GSIZE_FORMAT, i);
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]);
1624 g_application_hold (app);
1625 g_application_run (app, 0, NULL);
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);
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);
1637 g_ptr_array_unref (messages);
1639 g_test_dbus_down (bus);
1640 g_clear_object (&bus);
1644 dbus_command_line_done_cb (GApplication *app,
1645 GApplicationCommandLine *command_line,
1648 guint *n_command_lines = user_data;
1650 *n_command_lines = *n_command_lines + 1;
1652 if (*n_command_lines == 1)
1655 g_object_set_data_full (G_OBJECT (app), "command-line", g_object_ref (command_line), g_object_unref);
1657 g_application_command_line_set_exit_status (command_line, 42);
1658 g_application_command_line_done (command_line);
1660 return 1; /* ignored - after g_application_command_line_done () */
1664 test_dbus_command_line_done (void)
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;
1674 g_test_summary ("Test that GDBusCommandLine.done() works");
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");
1680 message = g_dbus_message_new_method_call ("org.gtk.TestApplication.CommandLine",
1681 "/org/gtk/TestApplication/CommandLine",
1682 "org.gtk.Application",
1684 g_dbus_message_set_body (message, g_variant_new ("(oaaya{sv})",
1685 "/my/org/gtk/private/CommandLine",
1688 bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1689 g_test_dbus_up (bus);
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);
1696 g_application_hold (app);
1697 exit_status = g_application_run (app, 0, NULL);
1699 g_assert_cmpuint (n_command_lines, ==, 2);
1700 g_assert_cmpint (exit_status, ==, 0);
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);
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);
1710 g_clear_object (&app);
1711 g_clear_object (&message);
1713 g_test_dbus_down (bus);
1714 g_clear_object (&bus);
1718 dbus_activate_action_cb (GSimpleAction *action,
1719 GVariant *parameter,
1722 guint *n_activations = user_data;
1724 *n_activations = *n_activations + 1;
1725 g_main_context_wakeup (NULL);
1729 test_dbus_activate_action (void)
1731 GTestDBus *bus = NULL;
1732 GVariantBuilder builder;
1735 GDBusMessage *message; /* (not nullable) (owned) */
1736 guint n_expected_activations;
1740 g_test_summary ("Test that calling the ActivateAction D-Bus method works");
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",
1747 g_dbus_message_set_body (messages[0].message, g_variant_new ("(sava{sv})", "undo", NULL, NULL));
1748 messages[0].n_expected_activations = 1;
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"));
1754 messages[1].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1755 "/org/gtk/TestApplication/ActivateAction",
1756 "org.freedesktop.Application",
1758 g_dbus_message_set_body (messages[1].message, g_variant_new ("(sava{sv})", "lang", &builder, NULL));
1759 messages[1].n_expected_activations = 1;
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"));
1765 messages[2].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1766 "/org/gtk/TestApplication/ActivateAction",
1767 "org.freedesktop.Application",
1769 g_dbus_message_set_body (messages[2].message, g_variant_new ("(sava{sv})", "undo", &builder, NULL));
1770 messages[2].n_expected_activations = 0;
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",
1777 g_dbus_message_set_body (messages[3].message, g_variant_new ("(sava{sv})", "lang", NULL, NULL));
1778 messages[3].n_expected_activations = 0;
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));
1784 messages[4].message = g_dbus_message_new_method_call ("org.gtk.TestApplication.ActivateAction",
1785 "/org/gtk/TestApplication/ActivateAction",
1786 "org.freedesktop.Application",
1788 g_dbus_message_set_body (messages[4].message, g_variant_new ("(sava{sv})", "lang", &builder, NULL));
1789 messages[4].n_expected_activations = 0;
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",
1796 g_dbus_message_set_body (messages[5].message, g_variant_new ("(sava{sv})", "nonexistent", NULL, NULL));
1797 messages[5].n_expected_activations = 0;
1799 /* Try each message */
1800 bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1801 g_test_dbus_up (bus);
1803 for (i = 0; i < G_N_ELEMENTS (messages); i++)
1805 GApplication *app = NULL;
1806 gulong activate_id, startup_id;
1807 const GActionEntry entries[] =
1809 { "undo", dbus_activate_action_cb, NULL, NULL, NULL, { 0 } },
1810 { "lang", dbus_activate_action_cb, "s", "'latin'", NULL, { 0 } },
1812 guint n_activations = 0;
1814 g_test_message ("Message %" G_GSIZE_FORMAT, i);
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);
1820 /* Export some actions. */
1821 g_action_map_add_action_entries (G_ACTION_MAP (app), entries, G_N_ELEMENTS (entries), &n_activations);
1823 g_application_hold (app);
1824 g_application_run (app, 0, NULL);
1826 g_assert_cmpuint (n_activations, ==, messages[i].n_expected_activations);
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);
1834 g_test_dbus_down (bus);
1835 g_clear_object (&bus);
1839 main (int argc, char **argv)
1841 g_setenv ("LC_ALL", "C", TRUE);
1843 g_log_writer_default_set_use_stderr (TRUE);
1845 g_test_init (&argc, &argv, NULL);
1847 if (!g_test_subprocess ())
1848 g_test_dbus_unset ();
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);
1879 return g_test_run ();