6 #include "gdbus-tests.h"
7 #include "gdbus-sessionbus.h"
9 static const gchar *datapath;
10 static const gchar *binpath;
12 static gint outstanding_watches;
13 static GMainLoop *main_loop;
17 const gchar *expected_stdout;
26 ChildData *child = data;
27 gssize expected, actual;
30 g_assert_cmpint (status, ==, 0);
32 if (--outstanding_watches == 0)
33 g_main_loop_quit (main_loop);
35 expected = strlen (child->expected_stdout);
36 buffer = g_alloca (expected + 100);
37 actual = read (child->stdout_pipe, buffer, expected + 100);
38 close (child->stdout_pipe);
40 g_assert_cmpint (actual, >=, 0);
42 if (actual != expected ||
43 memcmp (buffer, child->expected_stdout, expected) != 0)
45 buffer[MIN(expected + 100, actual)] = '\0';
47 g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
48 child->expected_stdout,
49 (actual > expected) ? "truncated" : "full", buffer);
52 g_slice_free (ChildData, child);
56 spawn (const gchar *expected_stdout,
57 const gchar *first_arg,
69 va_start (ap, first_arg);
70 array = g_ptr_array_new ();
71 g_ptr_array_add (array, g_build_filename (binpath, "basic-application", NULL));
72 for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
73 g_ptr_array_add (array, g_strdup (arg));
74 g_ptr_array_add (array, NULL);
75 args = (gchar **) g_ptr_array_free (array, FALSE);
79 data = g_slice_new (ChildData);
80 data->expected_stdout = expected_stdout;
82 g_spawn_async_with_pipes (NULL, args, NULL,
83 G_SPAWN_DO_NOT_REAP_CHILD,
84 NULL, NULL, &pid, NULL,
85 &data->stdout_pipe, NULL, &error);
86 g_assert_no_error (error);
88 g_child_watch_add (pid, child_quit, data);
89 outstanding_watches++;
91 /* we block until the children write to stdout to make sure
92 * they have started, as they need to be executed in order;
93 * see https://bugzilla.gnome.org/show_bug.cgi?id=664627
95 fd.fd = data->stdout_pipe;
96 fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
106 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
108 main_loop = g_main_loop_new (NULL, 0);
110 /* spawn the master */
112 "open file:///a file:///b\n"
113 "cmdline '40 +' '2'\n"
117 /* send it some files */
118 spawn ("exit status: 0\n",
119 "./app", "/a", "/b", NULL);
121 spawn ("40 + 2 = 42\n"
123 "./cmd", "40 +", "2", NULL);
125 g_main_loop_run (main_loop);
133 /* Now that we register non-unique apps on the bus we need to fix the
134 * following test not to assume that it's safe to create multiple instances
135 * of the same app in one process.
137 * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
138 * introduced this problem.
141 static GApplication *recently_activated;
142 static GMainLoop *loop;
145 nonunique_activate (GApplication *application)
147 recently_activated = application;
150 g_main_loop_quit (loop);
153 static GApplication *
154 make_app (gboolean non_unique)
159 app = g_application_new ("org.gtk.Test-Application",
160 non_unique ? G_APPLICATION_NON_UNIQUE : 0);
161 g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
162 ok = g_application_register (app, NULL, NULL);
165 g_object_unref (app);
169 g_application_activate (app);
175 test_nonunique (void)
177 GApplication *first, *second, *third, *fourth;
181 first = make_app (TRUE);
182 /* non-remote because it is non-unique */
183 g_assert (!g_application_get_is_remote (first));
184 g_assert (recently_activated == first);
185 recently_activated = NULL;
187 second = make_app (FALSE);
188 /* non-remote because it is first */
189 g_assert (!g_application_get_is_remote (second));
190 g_assert (recently_activated == second);
191 recently_activated = NULL;
193 third = make_app (TRUE);
194 /* non-remote because it is non-unique */
195 g_assert (!g_application_get_is_remote (third));
196 g_assert (recently_activated == third);
197 recently_activated = NULL;
199 fourth = make_app (FALSE);
200 /* should have failed to register due to being
201 * unable to register the object paths
203 g_assert (fourth == NULL);
204 g_assert (recently_activated == NULL);
206 g_object_unref (first);
207 g_object_unref (second);
208 g_object_unref (third);
220 GApplicationFlags flags;
225 GError *error = NULL;
228 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
230 app = g_object_new (G_TYPE_APPLICATION,
231 "application-id", "org.gtk.TestApplication",
235 "application-id", &id,
237 "is-registered", ®istered,
238 "inactivity-timeout", &timeout,
241 g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
242 g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
243 g_assert (!registered);
244 g_assert_cmpint (timeout, ==, 0);
246 ret = g_application_register (G_APPLICATION (app), NULL, &error);
248 g_assert_no_error (error);
251 "is-registered", ®istered,
252 "is-remote", &remote,
255 g_assert (registered);
259 "inactivity-timeout", 1000,
262 g_application_quit (G_APPLICATION (app));
265 g_object_unref (app);
276 g_assert (!g_application_id_is_valid (""));
277 g_assert (!g_application_id_is_valid ("."));
278 g_assert (!g_application_id_is_valid ("a"));
279 g_assert (!g_application_id_is_valid ("abc"));
280 g_assert (!g_application_id_is_valid (".abc"));
281 g_assert (!g_application_id_is_valid ("abc."));
282 g_assert (!g_application_id_is_valid ("a..b"));
283 g_assert (!g_application_id_is_valid ("a/b"));
284 g_assert (!g_application_id_is_valid ("a\nb"));
285 g_assert (!g_application_id_is_valid ("a\nb"));
286 g_assert (!g_application_id_is_valid ("_a.b"));
287 g_assert (!g_application_id_is_valid ("-a.b"));
288 id = g_new0 (gchar, 261);
289 memset (id, 'a', 260);
292 g_assert (!g_application_id_is_valid (id));
295 g_assert (g_application_id_is_valid ("a.b"));
296 g_assert (g_application_id_is_valid ("A.B"));
297 g_assert (g_application_id_is_valid ("A-.B"));
298 g_assert (g_application_id_is_valid ("a_b.c-d"));
299 g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
302 static gboolean nodbus_activated;
305 release_app (gpointer user_data)
307 g_application_release (user_data);
308 return G_SOURCE_REMOVE;
312 nodbus_activate (GApplication *app)
314 nodbus_activated = TRUE;
315 g_application_hold (app);
317 g_assert (g_application_get_dbus_connection (app) == NULL);
318 g_assert (g_application_get_dbus_object_path (app) == NULL);
320 g_idle_add (release_app, app);
326 char *binpath = g_build_filename (datapath, "unimportant", NULL);
327 gchar *argv[] = { binpath, NULL };
330 app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
331 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
332 g_application_run (app, 1, argv);
333 g_object_unref (app);
335 g_assert (nodbus_activated);
339 static gboolean noappid_activated;
342 noappid_activate (GApplication *app)
344 noappid_activated = TRUE;
345 g_application_hold (app);
347 g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
349 g_idle_add (release_app, app);
352 /* test that no appid -> non-unique */
356 char *binpath = g_build_filename (datapath, "unimportant", NULL);
357 gchar *argv[] = { binpath, NULL };
360 app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
361 g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
362 g_application_run (app, 1, argv);
363 g_object_unref (app);
365 g_assert (noappid_activated);
371 quit_app (gpointer user_data)
373 g_application_quit (user_data);
374 return G_SOURCE_REMOVE;
377 static gboolean quit_activated;
380 quit_activate (GApplication *app)
382 quit_activated = TRUE;
383 g_application_hold (app);
385 g_assert (g_application_get_dbus_connection (app) != NULL);
386 g_assert (g_application_get_dbus_object_path (app) != NULL);
388 g_idle_add (quit_app, app);
395 char *binpath = g_build_filename (datapath, "unimportant", NULL);
396 gchar *argv[] = { binpath, NULL };
400 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
402 app = g_application_new ("org.gtk.Unimportant",
403 G_APPLICATION_FLAGS_NONE);
404 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
405 g_application_run (app, 1, argv);
406 g_object_unref (app);
409 g_assert (quit_activated);
416 on_activate (GApplication *app)
422 g_assert (!g_application_get_is_remote (app));
424 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
425 g_assert (g_strv_length (actions) == 0);
426 g_strfreev (actions);
428 action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
429 g_action_map_add_action (G_ACTION_MAP (app), action);
431 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
432 g_assert (g_strv_length (actions) == 1);
433 g_strfreev (actions);
435 g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
436 state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
437 g_assert (g_variant_get_boolean (state) == TRUE);
439 g_action_map_remove_action (G_ACTION_MAP (app), "test");
441 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
442 g_assert (g_strv_length (actions) == 0);
443 g_strfreev (actions);
445 g_idle_add (quit_app, app);
451 char *binpath = g_build_filename (datapath, "unimportant", NULL);
452 gchar *argv[] = { binpath, NULL };
455 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
457 app = g_application_new ("org.gtk.Unimportant",
458 G_APPLICATION_FLAGS_NONE);
459 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
460 g_application_run (app, 1, argv);
461 g_object_unref (app);
465 typedef GApplication TestLocCmdApp;
466 typedef GApplicationClass TestLocCmdAppClass;
468 static GType test_loc_cmd_app_get_type (void);
469 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
472 test_loc_cmd_app_init (TestLocCmdApp *app)
477 test_loc_cmd_app_startup (GApplication *app)
479 g_assert_not_reached ();
483 test_loc_cmd_app_shutdown (GApplication *app)
485 g_assert_not_reached ();
489 test_loc_cmd_app_local_command_line (GApplication *application,
497 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
499 G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
500 G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
501 G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
505 test_local_command_line (void)
507 char *binpath = g_build_filename (datapath, "unimportant", NULL);
508 gchar *argv[] = { binpath, "-invalid", NULL };
511 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
513 app = g_object_new (test_loc_cmd_app_get_type (),
514 "application-id", "org.gtk.Unimportant",
515 "flags", G_APPLICATION_FLAGS_NONE,
517 g_application_run (app, 1, argv);
518 g_object_unref (app);
523 main (int argc, char **argv)
525 if (g_getenv ("G_TEST_DATA"))
526 datapath = binpath = g_getenv ("G_TEST_DATA");
533 g_test_init (&argc, &argv, NULL);
535 g_test_dbus_unset ();
537 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
538 g_test_add_func ("/gapplication/basic", basic);
539 g_test_add_func ("/gapplication/no-appid", test_noappid);
540 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
541 g_test_add_func ("/gapplication/properties", properties);
542 g_test_add_func ("/gapplication/app-id", appid);
543 g_test_add_func ("/gapplication/quit", test_quit);
544 g_test_add_func ("/gapplication/actions", test_actions);
545 g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
547 return g_test_run ();