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
properties(void)367 properties (void)
368 {
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;
378
379 session_bus_up ();
380 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
381
382 app = g_object_new (G_TYPE_APPLICATION,
383 "application-id", "org.gtk.TestApplication",
384 NULL);
385
386 g_object_get (app,
387 "application-id", &id,
388 "flags", &flags,
389 "is-registered", ®istered,
390 "inactivity-timeout", &timeout,
391 NULL);
392
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);
397
398 ret = g_application_register (G_APPLICATION (app), NULL, &error);
399 g_assert (ret);
400 g_assert_no_error (error);
401
402 g_object_get (app,
403 "is-registered", ®istered,
404 "is-remote", &remote,
405 NULL);
406
407 g_assert (registered);
408 g_assert (!remote);
409
410 g_object_set (app,
411 "inactivity-timeout", 1000,
412 NULL);
413
414 g_application_quit (G_APPLICATION (app));
415
416 g_object_unref (c);
417 g_object_unref (app);
418 g_free (id);
419
420 session_bus_down ();
421 }
422
423 static void
appid(void)424 appid (void)
425 {
426 gchar *id;
427
428 g_assert_false (g_application_id_is_valid (""));
429 g_assert_false (g_application_id_is_valid ("."));
430 g_assert_false (g_application_id_is_valid ("a"));
431 g_assert_false (g_application_id_is_valid ("abc"));
432 g_assert_false (g_application_id_is_valid (".abc"));
433 g_assert_false (g_application_id_is_valid ("abc."));
434 g_assert_false (g_application_id_is_valid ("a..b"));
435 g_assert_false (g_application_id_is_valid ("a/b"));
436 g_assert_false (g_application_id_is_valid ("a\nb"));
437 g_assert_false (g_application_id_is_valid ("a\nb"));
438 g_assert_false (g_application_id_is_valid ("emoji_picker"));
439 g_assert_false (g_application_id_is_valid ("emoji-picker"));
440 g_assert_false (g_application_id_is_valid ("emojipicker"));
441 g_assert_false (g_application_id_is_valid ("my.Terminal.0123"));
442 id = g_new0 (gchar, 261);
443 memset (id, 'a', 260);
444 id[1] = '.';
445 id[260] = 0;
446 g_assert_false (g_application_id_is_valid (id));
447 g_free (id);
448
449 g_assert_true (g_application_id_is_valid ("a.b"));
450 g_assert_true (g_application_id_is_valid ("A.B"));
451 g_assert_true (g_application_id_is_valid ("A-.B"));
452 g_assert_true (g_application_id_is_valid ("a_b.c-d"));
453 g_assert_true (g_application_id_is_valid ("_a.b"));
454 g_assert_true (g_application_id_is_valid ("-a.b"));
455 g_assert_true (g_application_id_is_valid ("org.gnome.SessionManager"));
456 g_assert_true (g_application_id_is_valid ("my.Terminal._0123"));
457 g_assert_true (g_application_id_is_valid ("com.example.MyApp"));
458 g_assert_true (g_application_id_is_valid ("com.example.internal_apps.Calculator"));
459 g_assert_true (g_application_id_is_valid ("org._7_zip.Archiver"));
460 }
461
462 static gboolean nodbus_activated;
463
464 static gboolean
release_app(gpointer user_data)465 release_app (gpointer user_data)
466 {
467 g_application_release (user_data);
468 return G_SOURCE_REMOVE;
469 }
470
471 static void
nodbus_activate(GApplication * app)472 nodbus_activate (GApplication *app)
473 {
474 nodbus_activated = TRUE;
475 g_application_hold (app);
476
477 g_assert (g_application_get_dbus_connection (app) == NULL);
478 g_assert (g_application_get_dbus_object_path (app) == NULL);
479
480 g_idle_add (release_app, app);
481 }
482
483 static void
test_nodbus(void)484 test_nodbus (void)
485 {
486 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
487 gchar *argv[] = { binpath, NULL };
488 GApplication *app;
489
490 app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
491 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
492 g_application_run (app, 1, argv);
493 g_object_unref (app);
494
495 g_assert (nodbus_activated);
496 g_free (binpath);
497 }
498
499 static gboolean noappid_activated;
500
501 static void
noappid_activate(GApplication * app)502 noappid_activate (GApplication *app)
503 {
504 noappid_activated = TRUE;
505 g_application_hold (app);
506
507 g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
508
509 g_idle_add (release_app, app);
510 }
511
512 /* test that no appid -> non-unique */
513 static void
test_noappid(void)514 test_noappid (void)
515 {
516 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
517 gchar *argv[] = { binpath, NULL };
518 GApplication *app;
519
520 app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
521 g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
522 g_application_run (app, 1, argv);
523 g_object_unref (app);
524
525 g_assert (noappid_activated);
526 g_free (binpath);
527 }
528
529 static gboolean activated;
530 static gboolean quitted;
531
532 static gboolean
quit_app(gpointer user_data)533 quit_app (gpointer user_data)
534 {
535 quitted = TRUE;
536 g_application_quit (user_data);
537 return G_SOURCE_REMOVE;
538 }
539
540 static void
quit_activate(GApplication * app)541 quit_activate (GApplication *app)
542 {
543 activated = TRUE;
544 g_application_hold (app);
545
546 g_assert (g_application_get_dbus_connection (app) != NULL);
547 g_assert (g_application_get_dbus_object_path (app) != NULL);
548
549 g_idle_add (quit_app, app);
550 }
551
552 static void
test_quit(void)553 test_quit (void)
554 {
555 GDBusConnection *c;
556 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
557 gchar *argv[] = { binpath, NULL };
558 GApplication *app;
559
560 session_bus_up ();
561 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
562
563 app = g_application_new ("org.gtk.Unimportant",
564 G_APPLICATION_FLAGS_NONE);
565 activated = FALSE;
566 quitted = FALSE;
567 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
568 g_application_run (app, 1, argv);
569 g_object_unref (app);
570 g_object_unref (c);
571
572 g_assert (activated);
573 g_assert (quitted);
574
575 session_bus_down ();
576 g_free (binpath);
577 }
578
579 typedef struct
580 {
581 gboolean shutdown;
582 GParamSpec *notify_spec; /* (owned) (nullable) */
583 } RegisteredData;
584
585 static void
on_registered_shutdown(GApplication * app,gpointer user_data)586 on_registered_shutdown (GApplication *app,
587 gpointer user_data)
588 {
589 RegisteredData *registered_data = user_data;
590
591 registered_data->shutdown = TRUE;
592 }
593
594 static void
on_registered_notify(GApplication * app,GParamSpec * spec,gpointer user_data)595 on_registered_notify (GApplication *app,
596 GParamSpec *spec,
597 gpointer user_data)
598 {
599 RegisteredData *registered_data = user_data;
600 registered_data->notify_spec = g_param_spec_ref (spec);
601
602 if (g_application_get_is_registered (app))
603 g_assert_false (registered_data->shutdown);
604 else
605 g_assert_true (registered_data->shutdown);
606 }
607
608 static void
test_registered(void)609 test_registered (void)
610 {
611 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
612 gchar *argv[] = { binpath, NULL };
613 RegisteredData registered_data = { FALSE, NULL };
614 GApplication *app;
615
616 app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
617 g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
618 g_signal_connect (app, "shutdown", G_CALLBACK (on_registered_shutdown), ®istered_data);
619 g_signal_connect (app, "notify::is-registered", G_CALLBACK (on_registered_notify), ®istered_data);
620
621 g_assert_null (registered_data.notify_spec);
622
623 g_assert_true (g_application_register (app, NULL, NULL));
624 g_assert_true (g_application_get_is_registered (app));
625
626 g_assert_nonnull (registered_data.notify_spec);
627 g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered");
628 g_clear_pointer (®istered_data.notify_spec, g_param_spec_unref);
629
630 g_assert_false (registered_data.shutdown);
631
632 g_application_run (app, 1, argv);
633
634 g_assert_true (registered_data.shutdown);
635 g_assert_false (g_application_get_is_registered (app));
636 g_assert_nonnull (registered_data.notify_spec);
637 g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered");
638 g_clear_pointer (®istered_data.notify_spec, g_param_spec_unref);
639
640 /* Register it again */
641 registered_data.shutdown = FALSE;
642 g_assert_true (g_application_register (app, NULL, NULL));
643 g_assert_true (g_application_get_is_registered (app));
644 g_assert_nonnull (registered_data.notify_spec);
645 g_assert_cmpstr (registered_data.notify_spec->name, ==, "is-registered");
646 g_clear_pointer (®istered_data.notify_spec, g_param_spec_unref);
647 g_assert_false (registered_data.shutdown);
648
649 g_object_unref (app);
650
651 g_free (binpath);
652 }
653
654 static void
on_activate(GApplication * app)655 on_activate (GApplication *app)
656 {
657 gchar **actions;
658 GAction *action;
659 GVariant *state;
660
661 g_assert (!g_application_get_is_remote (app));
662
663 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
664 g_assert (g_strv_length (actions) == 0);
665 g_strfreev (actions);
666
667 action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
668 g_action_map_add_action (G_ACTION_MAP (app), action);
669
670 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
671 g_assert (g_strv_length (actions) == 1);
672 g_strfreev (actions);
673
674 g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
675 state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
676 g_assert (g_variant_get_boolean (state) == TRUE);
677
678 action = g_action_map_lookup_action (G_ACTION_MAP (app), "test");
679 g_assert (action != NULL);
680
681 g_action_map_remove_action (G_ACTION_MAP (app), "test");
682
683 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
684 g_assert (g_strv_length (actions) == 0);
685 g_strfreev (actions);
686 }
687
688 static void
test_local_actions(void)689 test_local_actions (void)
690 {
691 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
692 gchar *argv[] = { binpath, NULL };
693 GApplication *app;
694
695 app = g_application_new ("org.gtk.Unimportant",
696 G_APPLICATION_FLAGS_NONE);
697 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
698 g_application_run (app, 1, argv);
699 g_object_unref (app);
700 g_free (binpath);
701 }
702
703 typedef GApplication TestLocCmdApp;
704 typedef GApplicationClass TestLocCmdAppClass;
705
706 static GType test_loc_cmd_app_get_type (void);
G_DEFINE_TYPE(TestLocCmdApp,test_loc_cmd_app,G_TYPE_APPLICATION)707 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
708
709 static void
710 test_loc_cmd_app_init (TestLocCmdApp *app)
711 {
712 }
713
714 static void
test_loc_cmd_app_startup(GApplication * app)715 test_loc_cmd_app_startup (GApplication *app)
716 {
717 g_assert_not_reached ();
718 }
719
720 static void
test_loc_cmd_app_shutdown(GApplication * app)721 test_loc_cmd_app_shutdown (GApplication *app)
722 {
723 g_assert_not_reached ();
724 }
725
726 static gboolean
test_loc_cmd_app_local_command_line(GApplication * application,gchar *** arguments,gint * exit_status)727 test_loc_cmd_app_local_command_line (GApplication *application,
728 gchar ***arguments,
729 gint *exit_status)
730 {
731 return TRUE;
732 }
733
734 static void
test_loc_cmd_app_class_init(TestLocCmdAppClass * klass)735 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
736 {
737 G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
738 G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
739 G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
740 }
741
742 static void
test_local_command_line(void)743 test_local_command_line (void)
744 {
745 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
746 gchar *argv[] = { binpath, "-invalid", NULL };
747 GApplication *app;
748
749 app = g_object_new (test_loc_cmd_app_get_type (),
750 "application-id", "org.gtk.Unimportant",
751 "flags", G_APPLICATION_FLAGS_NONE,
752 NULL);
753 g_application_run (app, 1, argv);
754 g_object_unref (app);
755 g_free (binpath);
756 }
757
758 static void
test_resource_path(void)759 test_resource_path (void)
760 {
761 GApplication *app;
762
763 app = g_application_new ("x.y.z", 0);
764 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
765
766 /* this should not change anything */
767 g_application_set_application_id (app, "a.b.c");
768 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
769
770 /* but this should... */
771 g_application_set_resource_base_path (app, "/x");
772 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x");
773
774 /* ... and this */
775 g_application_set_resource_base_path (app, NULL);
776 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
777
778 g_object_unref (app);
779
780 /* Make sure that overriding at construction time works properly */
781 app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", "/a", NULL);
782 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/a");
783 g_object_unref (app);
784
785 /* ... particularly if we override to NULL */
786 app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", NULL, NULL);
787 g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
788 g_object_unref (app);
789 }
790
791 static gint
test_help_command_line(GApplication * app,GApplicationCommandLine * command_line,gpointer user_data)792 test_help_command_line (GApplication *app,
793 GApplicationCommandLine *command_line,
794 gpointer user_data)
795 {
796 gboolean *called = user_data;
797
798 *called = TRUE;
799
800 return 0;
801 }
802
803 /* Test whether --help is handled when HANDLES_COMMND_LINE is set and
804 * options have been added.
805 */
806 static void
test_help(void)807 test_help (void)
808 {
809 if (g_test_subprocess ())
810 {
811 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
812 gchar *argv[] = { binpath, "--help", NULL };
813 GApplication *app;
814 gboolean called = FALSE;
815 int status;
816
817 app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE);
818 g_application_add_main_option (app, "foo", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
819 g_signal_connect (app, "command-line", G_CALLBACK (test_help_command_line), &called);
820
821 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
822 g_assert (called == TRUE);
823 g_assert_cmpint (status, ==, 0);
824
825 g_object_unref (app);
826 g_free (binpath);
827 return;
828 }
829
830 g_test_trap_subprocess (NULL, 0, 0);
831 g_test_trap_assert_passed ();
832 g_test_trap_assert_stdout ("*Application options*");
833 }
834
835 static void
test_busy(void)836 test_busy (void)
837 {
838 GApplication *app;
839
840 /* use GSimpleAction to bind to the busy state, because it's easy to
841 * create and has an easily modifiable boolean property */
842 GSimpleAction *action1;
843 GSimpleAction *action2;
844
845 session_bus_up ();
846
847 app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_NON_UNIQUE);
848 g_assert (g_application_register (app, NULL, NULL));
849
850 g_assert (!g_application_get_is_busy (app));
851 g_application_mark_busy (app);
852 g_assert (g_application_get_is_busy (app));
853 g_application_unmark_busy (app);
854 g_assert (!g_application_get_is_busy (app));
855
856 action1 = g_simple_action_new ("action", NULL);
857 g_application_bind_busy_property (app, action1, "enabled");
858 g_assert (g_application_get_is_busy (app));
859
860 g_simple_action_set_enabled (action1, FALSE);
861 g_assert (!g_application_get_is_busy (app));
862
863 g_application_mark_busy (app);
864 g_assert (g_application_get_is_busy (app));
865
866 action2 = g_simple_action_new ("action", NULL);
867 g_application_bind_busy_property (app, action2, "enabled");
868 g_assert (g_application_get_is_busy (app));
869
870 g_application_unmark_busy (app);
871 g_assert (g_application_get_is_busy (app));
872
873 g_object_unref (action2);
874 g_assert (!g_application_get_is_busy (app));
875
876 g_simple_action_set_enabled (action1, TRUE);
877 g_assert (g_application_get_is_busy (app));
878
879 g_application_mark_busy (app);
880 g_assert (g_application_get_is_busy (app));
881
882 g_application_unbind_busy_property (app, action1, "enabled");
883 g_assert (g_application_get_is_busy (app));
884
885 g_application_unmark_busy (app);
886 g_assert (!g_application_get_is_busy (app));
887
888 g_object_unref (action1);
889 g_object_unref (app);
890
891 session_bus_down ();
892 }
893
894 /*
895 * Test that handle-local-options works as expected
896 */
897
898 static gint
test_local_options(GApplication * app,GVariantDict * options,gpointer data)899 test_local_options (GApplication *app,
900 GVariantDict *options,
901 gpointer data)
902 {
903 gboolean *called = data;
904
905 *called = TRUE;
906
907 if (g_variant_dict_contains (options, "success"))
908 return 0;
909 else if (g_variant_dict_contains (options, "failure"))
910 return 1;
911 else
912 return -1;
913 }
914
915 static gint
second_handler(GApplication * app,GVariantDict * options,gpointer data)916 second_handler (GApplication *app,
917 GVariantDict *options,
918 gpointer data)
919 {
920 gboolean *called = data;
921
922 *called = TRUE;
923
924 return 2;
925 }
926
927 static void
test_handle_local_options_success(void)928 test_handle_local_options_success (void)
929 {
930 if (g_test_subprocess ())
931 {
932 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
933 gchar *argv[] = { binpath, "--success", NULL };
934 GApplication *app;
935 gboolean called = FALSE;
936 gboolean called2 = FALSE;
937 int status;
938
939 app = g_application_new ("org.gtk.TestApplication", 0);
940 g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
941 g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
942 g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
943 g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
944
945 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
946 g_assert (called);
947 g_assert (!called2);
948 g_assert_cmpint (status, ==, 0);
949
950 g_object_unref (app);
951 g_free (binpath);
952 return;
953 }
954
955 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
956 g_test_trap_assert_passed ();
957 }
958
959 static void
test_handle_local_options_failure(void)960 test_handle_local_options_failure (void)
961 {
962 if (g_test_subprocess ())
963 {
964 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
965 gchar *argv[] = { binpath, "--failure", NULL };
966 GApplication *app;
967 gboolean called = FALSE;
968 gboolean called2 = FALSE;
969 int status;
970
971 app = g_application_new ("org.gtk.TestApplication", 0);
972 g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
973 g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
974 g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
975 g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
976
977 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
978 g_assert (called);
979 g_assert (!called2);
980 g_assert_cmpint (status, ==, 1);
981
982 g_object_unref (app);
983 g_free (binpath);
984 return;
985 }
986
987 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
988 g_test_trap_assert_passed ();
989 }
990
991 static void
test_handle_local_options_passthrough(void)992 test_handle_local_options_passthrough (void)
993 {
994 if (g_test_subprocess ())
995 {
996 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
997 gchar *argv[] = { binpath, NULL };
998 GApplication *app;
999 gboolean called = FALSE;
1000 gboolean called2 = FALSE;
1001 int status;
1002
1003 app = g_application_new ("org.gtk.TestApplication", 0);
1004 g_application_add_main_option (app, "success", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
1005 g_application_add_main_option (app, "failure", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
1006 g_signal_connect (app, "handle-local-options", G_CALLBACK (test_local_options), &called);
1007 g_signal_connect (app, "handle-local-options", G_CALLBACK (second_handler), &called2);
1008
1009 status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
1010 g_assert (called);
1011 g_assert (called2);
1012 g_assert_cmpint (status, ==, 2);
1013
1014 g_object_unref (app);
1015 g_free (binpath);
1016 return;
1017 }
1018
1019 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDOUT | G_TEST_SUBPROCESS_INHERIT_STDERR);
1020 g_test_trap_assert_passed ();
1021 }
1022
1023 static void
test_api(void)1024 test_api (void)
1025 {
1026 GApplication *app;
1027 GSimpleAction *action;
1028
1029 app = g_application_new ("org.gtk.TestApplication", 0);
1030
1031 /* add an action without a name */
1032 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*assertion*failed*");
1033 action = g_simple_action_new (NULL, NULL);
1034 g_assert (action == NULL);
1035 g_test_assert_expected_messages ();
1036
1037 /* also, gapplication shouldn't accept actions without names */
1038 action = g_object_new (G_TYPE_SIMPLE_ACTION, NULL);
1039 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*action has no name*");
1040 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
1041 g_test_assert_expected_messages ();
1042
1043 g_object_unref (action);
1044 g_object_unref (app);
1045 }
1046
1047 /* Check that G_APPLICATION_ALLOW_REPLACEMENT works. To do so, we launch
1048 * a GApplication in this process that allows replacement, and then
1049 * launch a subprocess with --gapplication-replace. We have to do our
1050 * own async version of g_test_trap_subprocess() here since we need
1051 * the main process to keep spinning its mainloop.
1052 */
1053
1054 static gboolean
name_was_lost(GApplication * app,gboolean * called)1055 name_was_lost (GApplication *app,
1056 gboolean *called)
1057 {
1058 *called = TRUE;
1059 g_application_quit (app);
1060 return TRUE;
1061 }
1062
1063 static void
startup_in_subprocess(GApplication * app,gboolean * called)1064 startup_in_subprocess (GApplication *app,
1065 gboolean *called)
1066 {
1067 *called = TRUE;
1068 }
1069
1070 typedef struct
1071 {
1072 gboolean allow_replacement;
1073 GSubprocess *subprocess;
1074 } TestReplaceData;
1075
1076 static void
startup_cb(GApplication * app,TestReplaceData * data)1077 startup_cb (GApplication *app,
1078 TestReplaceData *data)
1079 {
1080 const char *argv[] = { NULL, "--verbose", "--quiet", "-p", NULL, "--GTestSubprocess", NULL };
1081 GSubprocessLauncher *launcher;
1082 GError *local_error = NULL;
1083
1084 g_application_hold (app);
1085
1086 argv[0] = g_get_prgname ();
1087
1088 if (data->allow_replacement)
1089 argv[4] = "/gapplication/replace";
1090 else
1091 argv[4] = "/gapplication/no-replace";
1092
1093 /* Now that we are the primary instance, launch our replacement.
1094 * We inherit the environment to share the test session bus.
1095 */
1096 g_test_message ("launching subprocess");
1097
1098 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
1099 g_subprocess_launcher_set_environ (launcher, NULL);
1100 data->subprocess = g_subprocess_launcher_spawnv (launcher, argv, &local_error);
1101 g_assert_no_error (local_error);
1102 g_object_unref (launcher);
1103
1104 if (!data->allow_replacement)
1105 {
1106 /* make sure we exit after a bit, if the subprocess is not replacing us */
1107 g_application_set_inactivity_timeout (app, 500);
1108 g_application_release (app);
1109 }
1110 }
1111
1112 static void
activate(gpointer data)1113 activate (gpointer data)
1114 {
1115 /* GApplication complains if we don't connect to ::activate */
1116 }
1117
1118 static gboolean
quit_already(gpointer data)1119 quit_already (gpointer data)
1120 {
1121 GApplication *app = data;
1122
1123 g_application_quit (app);
1124
1125 return G_SOURCE_REMOVE;
1126 }
1127
1128 static void
test_replace(gconstpointer data)1129 test_replace (gconstpointer data)
1130 {
1131 gboolean allow = GPOINTER_TO_INT (data);
1132
1133 if (g_test_subprocess ())
1134 {
1135 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1136 char *argv[] = { binpath, "--gapplication-replace", NULL };
1137 GApplication *app;
1138 gboolean startup = FALSE;
1139
1140 app = g_application_new ("org.gtk.TestApplication.Replace", G_APPLICATION_ALLOW_REPLACEMENT);
1141 g_signal_connect (app, "startup", G_CALLBACK (startup_in_subprocess), &startup);
1142 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
1143
1144 g_application_run (app, G_N_ELEMENTS (argv) - 1, argv);
1145
1146 if (allow)
1147 g_assert_true (startup);
1148 else
1149 g_assert_false (startup);
1150
1151 g_object_unref (app);
1152 g_free (binpath);
1153 }
1154 else
1155 {
1156 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
1157 gchar *argv[] = { binpath, NULL };
1158 GApplication *app;
1159 gboolean name_lost = FALSE;
1160 TestReplaceData data;
1161 GTestDBus *bus;
1162
1163 data.allow_replacement = allow;
1164 data.subprocess = NULL;
1165
1166 bus = g_test_dbus_new (0);
1167 g_test_dbus_up (bus);
1168
1169 app = g_application_new ("org.gtk.TestApplication.Replace", allow ? G_APPLICATION_ALLOW_REPLACEMENT : G_APPLICATION_FLAGS_NONE);
1170 g_application_set_inactivity_timeout (app, 500);
1171 g_signal_connect (app, "name-lost", G_CALLBACK (name_was_lost), &name_lost);
1172 g_signal_connect (app, "startup", G_CALLBACK (startup_cb), &data);
1173 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
1174
1175 if (!allow)
1176 g_timeout_add_seconds (1, quit_already, app);
1177
1178 g_application_run (app, G_N_ELEMENTS (argv) - 1, argv);
1179
1180 g_assert_nonnull (data.subprocess);
1181 if (allow)
1182 g_assert_true (name_lost);
1183 else
1184 g_assert_false (name_lost);
1185
1186 g_object_unref (app);
1187 g_free (binpath);
1188
1189 g_subprocess_wait (data.subprocess, NULL, NULL);
1190 g_clear_object (&data.subprocess);
1191
1192 g_test_dbus_down (bus);
1193 g_object_unref (bus);
1194 }
1195 }
1196
1197 int
main(int argc,char ** argv)1198 main (int argc, char **argv)
1199 {
1200 g_setenv ("LC_ALL", "C", TRUE);
1201
1202 g_test_init (&argc, &argv, NULL);
1203
1204 if (!g_test_subprocess ())
1205 g_test_dbus_unset ();
1206
1207 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
1208 /* g_test_add_func ("/gapplication/basic", basic); */
1209 g_test_add_func ("/gapplication/no-appid", test_noappid);
1210 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
1211 g_test_add_func ("/gapplication/properties", properties);
1212 g_test_add_func ("/gapplication/app-id", appid);
1213 g_test_add_func ("/gapplication/quit", test_quit);
1214 g_test_add_func ("/gapplication/registered", test_registered);
1215 g_test_add_func ("/gapplication/local-actions", test_local_actions);
1216 /* g_test_add_func ("/gapplication/remote-actions", test_remote_actions); */
1217 g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
1218 /* g_test_add_func ("/gapplication/remote-command-line", test_remote_command_line); */
1219 g_test_add_func ("/gapplication/resource-path", test_resource_path);
1220 g_test_add_func ("/gapplication/test-help", test_help);
1221 g_test_add_func ("/gapplication/test-busy", test_busy);
1222 g_test_add_func ("/gapplication/test-handle-local-options1", test_handle_local_options_success);
1223 g_test_add_func ("/gapplication/test-handle-local-options2", test_handle_local_options_failure);
1224 g_test_add_func ("/gapplication/test-handle-local-options3", test_handle_local_options_passthrough);
1225 g_test_add_func ("/gapplication/api", test_api);
1226 g_test_add_data_func ("/gapplication/replace", GINT_TO_POINTER (TRUE), test_replace);
1227 g_test_add_data_func ("/gapplication/no-replace", GINT_TO_POINTER (FALSE), test_replace);
1228
1229 return g_test_run ();
1230 }
1231