Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gapplication.c
blob55d0c0d2a8256fb63a8373272faf0cbc99789754
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
6 #include "gdbus-tests.h"
7 #include "gdbus-sessionbus.h"
9 #if 0
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...
16 * We need new tests.
18 static gint outstanding_watches;
19 static GMainLoop *main_loop;
21 typedef struct
23 gchar *expected_stdout;
24 gint stdout_pipe;
25 gchar *expected_stderr;
26 gint stderr_pipe;
27 } ChildData;
29 static void
30 check_data (gint fd, const gchar *expected)
32 gssize len, actual;
33 gchar *buffer;
35 len = strlen (expected);
36 buffer = g_alloca (len + 100);
37 actual = read (fd, buffer, len + 100);
39 g_assert_cmpint (actual, >=, 0);
41 if (actual != len ||
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",
47 expected,
48 (actual > len) ? "truncated" : "full", buffer);
52 static void
53 child_quit (GPid pid,
54 gint status,
55 gpointer data)
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);
78 static void
79 spawn (const gchar *expected_stdout,
80 const gchar *expected_stderr,
81 const gchar *first_arg,
82 ...)
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;
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);
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,
112 &data->stdout_pipe,
113 expected_stderr ? &data->stderr_pipe : NULL,
114 &error);
115 g_assert_no_error (error);
117 g_strfreev (env);
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;
128 g_poll (&fd, 1, -1);
131 static void
132 basic (void)
134 GDBusConnection *c;
136 g_assert (outstanding_watches == 0);
138 session_bus_up ();
139 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
141 main_loop = g_main_loop_new (NULL, 0);
143 /* spawn the master */
144 spawn ("activated\n"
145 "open file:///a file:///b\n"
146 "exit status: 0\n", NULL,
147 "./app", 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);
155 g_object_unref (c);
156 session_bus_down ();
158 g_main_loop_unref (main_loop);
161 static void
162 test_remote_command_line (void)
164 GDBusConnection *c;
165 GFile *file;
166 gchar *replies;
167 gchar *cwd;
169 g_assert (outstanding_watches == 0);
171 session_bus_up ();
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",
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);
194 /* spawn the master */
195 spawn (replies, NULL,
196 "./cmd", NULL);
198 g_free (replies);
200 /* send it a few commandlines */
201 spawn ("exit status: 0\n", NULL,
202 "./cmd", 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);
240 g_object_unref (c);
241 session_bus_down ();
243 g_main_loop_unref (main_loop);
246 static void
247 test_remote_actions (void)
249 GDBusConnection *c;
251 g_assert (outstanding_watches == 0);
253 session_bus_up ();
254 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
256 main_loop = g_main_loop_new (NULL, 0);
258 /* spawn the master */
259 spawn ("got ./cmd 0\n"
260 "activate action1\n"
261 "change action2 1\n"
262 "exit status: 0\n", NULL,
263 "./cmd", 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);
277 g_object_unref (c);
278 session_bus_down ();
280 g_main_loop_unref (main_loop);
282 #endif
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.
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;
296 static void
297 nonunique_activate (GApplication *application)
299 recently_activated = application;
301 if (loop != NULL)
302 g_main_loop_quit (loop);
305 static GApplication *
306 make_app (gboolean non_unique)
308 GApplication *app;
309 gboolean ok;
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)
317 g_object_unref (app);
318 return NULL;
321 g_application_activate (app);
323 return app;
326 static void
327 test_nonunique (void)
329 GApplication *first, *second, *third, *fourth;
331 session_bus_up ();
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);
362 session_bus_down ();
364 #endif
366 static void
367 properties (void)
369 GDBusConnection *c;
370 GObject *app;
371 gchar *id;
372 GApplicationFlags flags;
373 gboolean registered;
374 guint timeout;
375 gboolean remote;
376 gboolean ret;
377 GError *error = NULL;
379 session_bus_up ();
380 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
382 app = g_object_new (G_TYPE_APPLICATION,
383 "application-id", "org.gtk.TestApplication",
384 NULL);
386 g_object_get (app,
387 "application-id", &id,
388 "flags", &flags,
389 "is-registered", &registered,
390 "inactivity-timeout", &timeout,
391 NULL);
393 g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
394 g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
395 g_assert (!registered);
396 g_assert_cmpint (timeout, ==, 0);
398 ret = g_application_register (G_APPLICATION (app), NULL, &error);
399 g_assert (ret);
400 g_assert_no_error (error);
402 g_object_get (app,
403 "is-registered", &registered,
404 "is-remote", &remote,
405 NULL);
407 g_assert (registered);
408 g_assert (!remote);
410 g_object_set (app,
411 "inactivity-timeout", 1000,
412 NULL);
414 g_application_quit (G_APPLICATION (app));
416 g_object_unref (c);
417 g_object_unref (app);
418 g_free (id);
420 session_bus_down ();
423 static void
424 appid (void)
426 gchar *id;
428 g_assert (!g_application_id_is_valid (""));
429 g_assert (!g_application_id_is_valid ("."));
430 g_assert (!g_application_id_is_valid ("a"));
431 g_assert (!g_application_id_is_valid ("abc"));
432 g_assert (!g_application_id_is_valid (".abc"));
433 g_assert (!g_application_id_is_valid ("abc."));
434 g_assert (!g_application_id_is_valid ("a..b"));
435 g_assert (!g_application_id_is_valid ("a/b"));
436 g_assert (!g_application_id_is_valid ("a\nb"));
437 g_assert (!g_application_id_is_valid ("a\nb"));
438 g_assert (!g_application_id_is_valid ("_a.b"));
439 g_assert (!g_application_id_is_valid ("-a.b"));
440 g_assert (!g_application_id_is_valid ("emoji_picker"));
441 g_assert (!g_application_id_is_valid ("emoji-picker"));
442 g_assert (!g_application_id_is_valid ("emojipicker"));
443 id = g_new0 (gchar, 261);
444 memset (id, 'a', 260);
445 id[1] = '.';
446 id[260] = 0;
447 g_assert (!g_application_id_is_valid (id));
448 g_free (id);
450 g_assert (g_application_id_is_valid ("a.b"));
451 g_assert (g_application_id_is_valid ("A.B"));
452 g_assert (g_application_id_is_valid ("A-.B"));
453 g_assert (g_application_id_is_valid ("a_b.c-d"));
454 g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
457 static gboolean nodbus_activated;
459 static gboolean
460 release_app (gpointer user_data)
462 g_application_release (user_data);
463 return G_SOURCE_REMOVE;
466 static void
467 nodbus_activate (GApplication *app)
469 nodbus_activated = TRUE;
470 g_application_hold (app);
472 g_assert (g_application_get_dbus_connection (app) == NULL);
473 g_assert (g_application_get_dbus_object_path (app) == NULL);
475 g_idle_add (release_app, app);
478 static void
479 test_nodbus (void)
481 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
482 gchar *argv[] = { binpath, NULL };
483 GApplication *app;
485 app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
486 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
487 g_application_run (app, 1, argv);
488 g_object_unref (app);
490 g_assert (nodbus_activated);
491 g_free (binpath);
494 static gboolean noappid_activated;
496 static void
497 noappid_activate (GApplication *app)
499 noappid_activated = TRUE;
500 g_application_hold (app);
502 g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
504 g_idle_add (release_app, app);
507 /* test that no appid -> non-unique */
508 static void
509 test_noappid (void)
511 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
512 gchar *argv[] = { binpath, NULL };
513 GApplication *app;
515 app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
516 g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
517 g_application_run (app, 1, argv);
518 g_object_unref (app);
520 g_assert (noappid_activated);
521 g_free (binpath);
524 static gboolean activated;
525 static gboolean quitted;
527 static gboolean
528 quit_app (gpointer user_data)
530 quitted = TRUE;
531 g_application_quit (user_data);
532 return G_SOURCE_REMOVE;
535 static void
536 quit_activate (GApplication *app)
538 activated = TRUE;
539 g_application_hold (app);
541 g_assert (g_application_get_dbus_connection (app) != NULL);
542 g_assert (g_application_get_dbus_object_path (app) != NULL);
544 g_idle_add (quit_app, app);
547 static void
548 test_quit (void)
550 GDBusConnection *c;
551 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
552 gchar *argv[] = { binpath, NULL };
553 GApplication *app;
555 session_bus_up ();
556 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
558 app = g_application_new ("org.gtk.Unimportant",
559 G_APPLICATION_FLAGS_NONE);
560 activated = FALSE;
561 quitted = FALSE;
562 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
563 g_application_run (app, 1, argv);
564 g_object_unref (app);
565 g_object_unref (c);
567 g_assert (activated);
568 g_assert (quitted);
570 session_bus_down ();
571 g_free (binpath);
574 static void
575 on_activate (GApplication *app)
577 gchar **actions;
578 GAction *action;
579 GVariant *state;
581 g_assert (!g_application_get_is_remote (app));
583 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
584 g_assert (g_strv_length (actions) == 0);
585 g_strfreev (actions);
587 action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
588 g_action_map_add_action (G_ACTION_MAP (app), action);
590 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
591 g_assert (g_strv_length (actions) == 1);
592 g_strfreev (actions);
594 g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
595 state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
596 g_assert (g_variant_get_boolean (state) == TRUE);
598 action = g_action_map_lookup_action (G_ACTION_MAP (app), "test");
599 g_assert (action != NULL);
601 g_action_map_remove_action (G_ACTION_MAP (app), "test");
603 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
604 g_assert (g_strv_length (actions) == 0);
605 g_strfreev (actions);
608 static void
609 test_local_actions (void)
611 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
612 gchar *argv[] = { binpath, NULL };
613 GApplication *app;
615 app = g_application_new ("org.gtk.Unimportant",
616 G_APPLICATION_FLAGS_NONE);
617 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
618 g_application_run (app, 1, argv);
619 g_object_unref (app);
620 g_free (binpath);
623 typedef GApplication TestLocCmdApp;
624 typedef GApplicationClass TestLocCmdAppClass;
626 static GType test_loc_cmd_app_get_type (void);
627 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
629 static void
630 test_loc_cmd_app_init (TestLocCmdApp *app)
634 static void
635 test_loc_cmd_app_startup (GApplication *app)
637 g_assert_not_reached ();
640 static void
641 test_loc_cmd_app_shutdown (GApplication *app)
643 g_assert_not_reached ();
646 static gboolean
647 test_loc_cmd_app_local_command_line (GApplication *application,
648 gchar ***arguments,
649 gint *exit_status)
651 return TRUE;
654 static void
655 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
657 G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
658 G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
659 G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
662 static void
663 test_local_command_line (void)
665 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
666 gchar *argv[] = { binpath, "-invalid", NULL };
667 GApplication *app;
669 app = g_object_new (test_loc_cmd_app_get_type (),
670 "application-id", "org.gtk.Unimportant",
671 "flags", G_APPLICATION_FLAGS_NONE,
672 NULL);
673 g_application_run (app, 1, argv);
674 g_object_unref (app);
675 g_free (binpath);
678 static void
679 test_resource_path (void)
681 GApplication *app;
683 app = g_application_new ("x.y.z", 0);
684 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
686 /* this should not change anything */
687 g_application_set_application_id (app, "a.b.c");
688 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
690 /* but this should... */
691 g_application_set_resource_base_path (app, "/x");
692 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x");
694 /* ... and this */
695 g_application_set_resource_base_path (app, NULL);
696 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
698 g_object_unref (app);
700 /* Make sure that overriding at construction time works properly */
701 app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", "/a", NULL);
702 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/a");
703 g_object_unref (app);
705 /* ... particularly if we override to NULL */
706 app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", NULL, NULL);
707 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
708 g_object_unref (app);
711 static gint
712 test_help_command_line (GApplication *app,
713 GApplicationCommandLine *command_line,
714 gpointer user_data)
716 gboolean *called = user_data;
718 *called = TRUE;
720 return 0;
723 /* Test whether --help is handled when HANDLES_COMMND_LINE is set and
724 * options have been added.
726 static void
727 test_help (void)
729 if (g_test_subprocess ())
731 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
732 gchar *argv[] = { binpath, "--help", NULL };
733 GApplication *app;
734 gboolean called = FALSE;
735 int status;
737 app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE);
738 g_application_add_main_option (app, "foo", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
739 g_signal_connect (app, "command-line", G_CALLBACK (test_help_command_line), &called);
741 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
742 g_assert (called == TRUE);
743 g_assert_cmpint (status, ==, 0);
745 g_object_unref (app);
746 g_free (binpath);
747 return;
750 g_test_trap_subprocess (NULL, 0, 0);
751 g_test_trap_assert_passed ();
752 g_test_trap_assert_stdout ("*Application options*");
755 static void
756 test_busy (void)
758 GApplication *app;
760 /* use GSimpleAction to bind to the busy state, because it's easy to
761 * create and has an easily modifiable boolean property */
762 GSimpleAction *action1;
763 GSimpleAction *action2;
765 session_bus_up ();
767 app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_NON_UNIQUE);
768 g_assert (g_application_register (app, NULL, NULL));
770 g_assert (!g_application_get_is_busy (app));
771 g_application_mark_busy (app);
772 g_assert (g_application_get_is_busy (app));
773 g_application_unmark_busy (app);
774 g_assert (!g_application_get_is_busy (app));
776 action1 = g_simple_action_new ("action", NULL);
777 g_application_bind_busy_property (app, action1, "enabled");
778 g_assert (g_application_get_is_busy (app));
780 g_simple_action_set_enabled (action1, FALSE);
781 g_assert (!g_application_get_is_busy (app));
783 g_application_mark_busy (app);
784 g_assert (g_application_get_is_busy (app));
786 action2 = g_simple_action_new ("action", NULL);
787 g_application_bind_busy_property (app, action2, "enabled");
788 g_assert (g_application_get_is_busy (app));
790 g_application_unmark_busy (app);
791 g_assert (g_application_get_is_busy (app));
793 g_object_unref (action2);
794 g_assert (!g_application_get_is_busy (app));
796 g_simple_action_set_enabled (action1, TRUE);
797 g_assert (g_application_get_is_busy (app));
799 g_application_mark_busy (app);
800 g_assert (g_application_get_is_busy (app));
802 g_application_unbind_busy_property (app, action1, "enabled");
803 g_assert (g_application_get_is_busy (app));
805 g_application_unmark_busy (app);
806 g_assert (!g_application_get_is_busy (app));
808 g_object_unref (action1);
809 g_object_unref (app);
811 session_bus_down ();
815 * Test that handle-local-options works as expected
818 static gint
819 test_local_options (GApplication *app,
820 GVariantDict *options,
821 gpointer data)
823 gboolean *called = data;
825 *called = TRUE;
827 if (g_variant_dict_contains (options, "success"))
828 return 0;
829 else if (g_variant_dict_contains (options, "failure"))
830 return 1;
831 else
832 return -1;
835 static gint
836 second_handler (GApplication *app,
837 GVariantDict *options,
838 gpointer data)
840 gboolean *called = data;
842 *called = TRUE;
844 return 2;
847 static void
848 test_handle_local_options_success (void)
850 if (g_test_subprocess ())
852 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
853 gchar *argv[] = { binpath, "--success", NULL };
854 GApplication *app;
855 gboolean called = FALSE;
856 gboolean called2 = FALSE;
857 int status;
859 app = g_application_new ("org.gtk.TestApplication", 0);
860 g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
861 g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
862 g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
863 g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
865 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
866 g_assert (called);
867 g_assert (!called2);
868 g_assert_cmpint (status, ==, 0);
870 g_object_unref (app);
871 g_free (binpath);
872 return;
875 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
876 g_test_trap_assert_passed ();
879 static void
880 test_handle_local_options_failure (void)
882 if (g_test_subprocess ())
884 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
885 gchar *argv[] = { binpath, "--failure", NULL };
886 GApplication *app;
887 gboolean called = FALSE;
888 gboolean called2 = FALSE;
889 int status;
891 app = g_application_new ("org.gtk.TestApplication", 0);
892 g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
893 g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
894 g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
895 g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
897 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
898 g_assert (called);
899 g_assert (!called2);
900 g_assert_cmpint (status, ==, 1);
902 g_object_unref (app);
903 g_free (binpath);
904 return;
907 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
908 g_test_trap_assert_passed ();
911 static void
912 test_handle_local_options_passthrough (void)
914 if (g_test_subprocess ())
916 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
917 gchar *argv[] = { binpath, NULL };
918 GApplication *app;
919 gboolean called = FALSE;
920 gboolean called2 = FALSE;
921 int status;
923 app = g_application_new ("org.gtk.TestApplication", 0);
924 g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
925 g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
926 g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
927 g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
929 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
930 g_assert (called);
931 g_assert (called2);
932 g_assert_cmpint (status, ==, 2);
934 g_object_unref (app);
935 g_free (binpath);
936 return;
939 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
940 g_test_trap_assert_passed ();
943 static void
944 test_api (void)
946 GApplication *app;
947 GSimpleAction *action;
949 app = g_application_new ("org.gtk.TestApplication", 0);
951 /* add an action without a name */
952 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*assertion*failed*");
953 action = g_simple_action_new (NULL, NULL);
954 g_assert (action == NULL);
955 g_test_assert_expected_messages ();
957 /* also, gapplication shouldn't accept actions without names */
958 action = g_object_new (G_TYPE_SIMPLE_ACTION, NULL);
959 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*action has no name*");
960 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
961 g_test_assert_expected_messages ();
963 g_object_unref (action);
964 g_object_unref (app);
968 main (int argc, char **argv)
970 g_test_init (&argc, &argv, NULL);
972 g_test_dbus_unset ();
974 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
975 /* g_test_add_func ("/gapplication/basic", basic); */
976 g_test_add_func ("/gapplication/no-appid", test_noappid);
977 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
978 g_test_add_func ("/gapplication/properties", properties);
979 g_test_add_func ("/gapplication/app-id", appid);
980 g_test_add_func ("/gapplication/quit", test_quit);
981 g_test_add_func ("/gapplication/local-actions", test_local_actions);
982 /* g_test_add_func ("/gapplication/remote-actions", test_remote_actions); */
983 g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
984 /* g_test_add_func ("/gapplication/remote-command-line", test_remote_command_line); */
985 g_test_add_func ("/gapplication/resource-path", test_resource_path);
986 g_test_add_func ("/gapplication/test-help", test_help);
987 g_test_add_func ("/gapplication/test-busy", test_busy);
988 g_test_add_func ("/gapplication/test-handle-local-options1", test_handle_local_options_success);
989 g_test_add_func ("/gapplication/test-handle-local-options2", test_handle_local_options_failure);
990 g_test_add_func ("/gapplication/test-handle-local-options3", test_handle_local_options_passthrough);
991 g_test_add_func ("/gapplication/api", test_api);
993 return g_test_run ();