6 #include "gdbus-tests.h"
7 #include "gdbus-sessionbus.h"
9 static gint outstanding_watches;
10 static GMainLoop *main_loop;
14 const gchar *expected_stdout;
23 ChildData *child = data;
24 gssize expected, actual;
27 g_assert_cmpint (status, ==, 0);
29 if (--outstanding_watches == 0)
30 g_main_loop_quit (main_loop);
32 expected = strlen (child->expected_stdout);
33 buffer = g_alloca (expected + 100);
34 actual = read (child->stdout_pipe, buffer, expected + 100);
35 close (child->stdout_pipe);
37 g_assert_cmpint (actual, >=, 0);
39 if (actual != expected ||
40 memcmp (buffer, child->expected_stdout, expected) != 0)
42 buffer[MIN(expected + 100, actual)] = '\0';
44 g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
45 child->expected_stdout,
46 (actual > expected) ? "truncated" : "full", buffer);
49 g_slice_free (ChildData, child);
53 spawn (const gchar *expected_stdout,
54 const gchar *first_arg,
66 va_start (ap, first_arg);
67 array = g_ptr_array_new ();
68 g_ptr_array_add (array, g_test_build_filename (G_TEST_BUILT, "basic-application", NULL));
69 for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
70 g_ptr_array_add (array, g_strdup (arg));
71 g_ptr_array_add (array, NULL);
72 args = (gchar **) g_ptr_array_free (array, FALSE);
76 data = g_slice_new (ChildData);
77 data->expected_stdout = expected_stdout;
79 g_spawn_async_with_pipes (NULL, args, NULL,
80 G_SPAWN_DO_NOT_REAP_CHILD,
81 NULL, NULL, &pid, NULL,
82 &data->stdout_pipe, NULL, &error);
83 g_assert_no_error (error);
85 g_child_watch_add (pid, child_quit, data);
86 outstanding_watches++;
88 /* we block until the children write to stdout to make sure
89 * they have started, as they need to be executed in order;
90 * see https://bugzilla.gnome.org/show_bug.cgi?id=664627
92 fd.fd = data->stdout_pipe;
93 fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
103 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
105 main_loop = g_main_loop_new (NULL, 0);
107 /* spawn the master */
109 "open file:///a file:///b\n"
110 "cmdline '40 +' '2'\n"
114 /* send it some files */
115 spawn ("exit status: 0\n",
116 "./app", "/a", "/b", NULL);
118 spawn ("40 + 2 = 42\n"
120 "./cmd", "40 +", "2", NULL);
122 g_main_loop_run (main_loop);
130 /* Now that we register non-unique apps on the bus we need to fix the
131 * following test not to assume that it's safe to create multiple instances
132 * of the same app in one process.
134 * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
135 * introduced this problem.
138 static GApplication *recently_activated;
139 static GMainLoop *loop;
142 nonunique_activate (GApplication *application)
144 recently_activated = application;
147 g_main_loop_quit (loop);
150 static GApplication *
151 make_app (gboolean non_unique)
156 app = g_application_new ("org.gtk.Test-Application",
157 non_unique ? G_APPLICATION_NON_UNIQUE : 0);
158 g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
159 ok = g_application_register (app, NULL, NULL);
162 g_object_unref (app);
166 g_application_activate (app);
172 test_nonunique (void)
174 GApplication *first, *second, *third, *fourth;
178 first = make_app (TRUE);
179 /* non-remote because it is non-unique */
180 g_assert (!g_application_get_is_remote (first));
181 g_assert (recently_activated == first);
182 recently_activated = NULL;
184 second = make_app (FALSE);
185 /* non-remote because it is first */
186 g_assert (!g_application_get_is_remote (second));
187 g_assert (recently_activated == second);
188 recently_activated = NULL;
190 third = make_app (TRUE);
191 /* non-remote because it is non-unique */
192 g_assert (!g_application_get_is_remote (third));
193 g_assert (recently_activated == third);
194 recently_activated = NULL;
196 fourth = make_app (FALSE);
197 /* should have failed to register due to being
198 * unable to register the object paths
200 g_assert (fourth == NULL);
201 g_assert (recently_activated == NULL);
203 g_object_unref (first);
204 g_object_unref (second);
205 g_object_unref (third);
217 GApplicationFlags flags;
222 GError *error = NULL;
225 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
227 app = g_object_new (G_TYPE_APPLICATION,
228 "application-id", "org.gtk.TestApplication",
232 "application-id", &id,
234 "is-registered", ®istered,
235 "inactivity-timeout", &timeout,
238 g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
239 g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
240 g_assert (!registered);
241 g_assert_cmpint (timeout, ==, 0);
243 ret = g_application_register (G_APPLICATION (app), NULL, &error);
245 g_assert_no_error (error);
248 "is-registered", ®istered,
249 "is-remote", &remote,
252 g_assert (registered);
256 "inactivity-timeout", 1000,
259 g_application_quit (G_APPLICATION (app));
262 g_object_unref (app);
273 g_assert (!g_application_id_is_valid (""));
274 g_assert (!g_application_id_is_valid ("."));
275 g_assert (!g_application_id_is_valid ("a"));
276 g_assert (!g_application_id_is_valid ("abc"));
277 g_assert (!g_application_id_is_valid (".abc"));
278 g_assert (!g_application_id_is_valid ("abc."));
279 g_assert (!g_application_id_is_valid ("a..b"));
280 g_assert (!g_application_id_is_valid ("a/b"));
281 g_assert (!g_application_id_is_valid ("a\nb"));
282 g_assert (!g_application_id_is_valid ("a\nb"));
283 g_assert (!g_application_id_is_valid ("_a.b"));
284 g_assert (!g_application_id_is_valid ("-a.b"));
285 id = g_new0 (gchar, 261);
286 memset (id, 'a', 260);
289 g_assert (!g_application_id_is_valid (id));
292 g_assert (g_application_id_is_valid ("a.b"));
293 g_assert (g_application_id_is_valid ("A.B"));
294 g_assert (g_application_id_is_valid ("A-.B"));
295 g_assert (g_application_id_is_valid ("a_b.c-d"));
296 g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
299 static gboolean nodbus_activated;
302 release_app (gpointer user_data)
304 g_application_release (user_data);
305 return G_SOURCE_REMOVE;
309 nodbus_activate (GApplication *app)
311 nodbus_activated = TRUE;
312 g_application_hold (app);
314 g_assert (g_application_get_dbus_connection (app) == NULL);
315 g_assert (g_application_get_dbus_object_path (app) == NULL);
317 g_idle_add (release_app, app);
323 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
324 gchar *argv[] = { binpath, NULL };
327 app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
328 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
329 g_application_run (app, 1, argv);
330 g_object_unref (app);
332 g_assert (nodbus_activated);
336 static gboolean noappid_activated;
339 noappid_activate (GApplication *app)
341 noappid_activated = TRUE;
342 g_application_hold (app);
344 g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
346 g_idle_add (release_app, app);
349 /* test that no appid -> non-unique */
353 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
354 gchar *argv[] = { binpath, NULL };
357 app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
358 g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
359 g_application_run (app, 1, argv);
360 g_object_unref (app);
362 g_assert (noappid_activated);
368 quit_app (gpointer user_data)
370 g_application_quit (user_data);
371 return G_SOURCE_REMOVE;
374 static gboolean quit_activated;
377 quit_activate (GApplication *app)
379 quit_activated = TRUE;
380 g_application_hold (app);
382 g_assert (g_application_get_dbus_connection (app) != NULL);
383 g_assert (g_application_get_dbus_object_path (app) != NULL);
385 g_idle_add (quit_app, app);
392 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
393 gchar *argv[] = { binpath, NULL };
397 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
399 app = g_application_new ("org.gtk.Unimportant",
400 G_APPLICATION_FLAGS_NONE);
401 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
402 g_application_run (app, 1, argv);
403 g_object_unref (app);
406 g_assert (quit_activated);
413 on_activate (GApplication *app)
419 g_assert (!g_application_get_is_remote (app));
421 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
422 g_assert (g_strv_length (actions) == 0);
423 g_strfreev (actions);
425 action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
426 g_action_map_add_action (G_ACTION_MAP (app), action);
428 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
429 g_assert (g_strv_length (actions) == 1);
430 g_strfreev (actions);
432 g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
433 state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
434 g_assert (g_variant_get_boolean (state) == TRUE);
436 g_action_map_remove_action (G_ACTION_MAP (app), "test");
438 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
439 g_assert (g_strv_length (actions) == 0);
440 g_strfreev (actions);
442 g_idle_add (quit_app, app);
448 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
449 gchar *argv[] = { binpath, NULL };
452 app = g_application_new ("org.gtk.Unimportant",
453 G_APPLICATION_FLAGS_NONE);
454 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
455 g_application_run (app, 1, argv);
456 g_object_unref (app);
460 typedef GApplication TestLocCmdApp;
461 typedef GApplicationClass TestLocCmdAppClass;
463 static GType test_loc_cmd_app_get_type (void);
464 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
467 test_loc_cmd_app_init (TestLocCmdApp *app)
472 test_loc_cmd_app_startup (GApplication *app)
474 g_assert_not_reached ();
478 test_loc_cmd_app_shutdown (GApplication *app)
480 g_assert_not_reached ();
484 test_loc_cmd_app_local_command_line (GApplication *application,
492 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
494 G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
495 G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
496 G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
500 test_local_command_line (void)
502 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
503 gchar *argv[] = { binpath, "-invalid", NULL };
506 app = g_object_new (test_loc_cmd_app_get_type (),
507 "application-id", "org.gtk.Unimportant",
508 "flags", G_APPLICATION_FLAGS_NONE,
510 g_application_run (app, 1, argv);
511 g_object_unref (app);
516 main (int argc, char **argv)
518 g_test_init (&argc, &argv, NULL);
520 g_test_dbus_unset ();
522 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
523 g_test_add_func ("/gapplication/basic", basic);
524 g_test_add_func ("/gapplication/no-appid", test_noappid);
525 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
526 g_test_add_func ("/gapplication/properties", properties);
527 g_test_add_func ("/gapplication/app-id", appid);
528 g_test_add_func ("/gapplication/quit", test_quit);
529 g_test_add_func ("/gapplication/actions", test_actions);
530 g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
532 return g_test_run ();