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"
113 /* send it some files */
114 spawn ("exit status: 0\n",
115 "./app", "/a", "/b", NULL);
117 g_main_loop_run (main_loop);
125 /* Now that we register non-unique apps on the bus we need to fix the
126 * following test not to assume that it's safe to create multiple instances
127 * of the same app in one process.
129 * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
130 * introduced this problem.
133 static GApplication *recently_activated;
134 static GMainLoop *loop;
137 nonunique_activate (GApplication *application)
139 recently_activated = application;
142 g_main_loop_quit (loop);
145 static GApplication *
146 make_app (gboolean non_unique)
151 app = g_application_new ("org.gtk.Test-Application",
152 non_unique ? G_APPLICATION_NON_UNIQUE : 0);
153 g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
154 ok = g_application_register (app, NULL, NULL);
157 g_object_unref (app);
161 g_application_activate (app);
167 test_nonunique (void)
169 GApplication *first, *second, *third, *fourth;
173 first = make_app (TRUE);
174 /* non-remote because it is non-unique */
175 g_assert (!g_application_get_is_remote (first));
176 g_assert (recently_activated == first);
177 recently_activated = NULL;
179 second = make_app (FALSE);
180 /* non-remote because it is first */
181 g_assert (!g_application_get_is_remote (second));
182 g_assert (recently_activated == second);
183 recently_activated = NULL;
185 third = make_app (TRUE);
186 /* non-remote because it is non-unique */
187 g_assert (!g_application_get_is_remote (third));
188 g_assert (recently_activated == third);
189 recently_activated = NULL;
191 fourth = make_app (FALSE);
192 /* should have failed to register due to being
193 * unable to register the object paths
195 g_assert (fourth == NULL);
196 g_assert (recently_activated == NULL);
198 g_object_unref (first);
199 g_object_unref (second);
200 g_object_unref (third);
212 GApplicationFlags flags;
217 GError *error = NULL;
220 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
222 app = g_object_new (G_TYPE_APPLICATION,
223 "application-id", "org.gtk.TestApplication",
227 "application-id", &id,
229 "is-registered", ®istered,
230 "inactivity-timeout", &timeout,
233 g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
234 g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
235 g_assert (!registered);
236 g_assert_cmpint (timeout, ==, 0);
238 ret = g_application_register (G_APPLICATION (app), NULL, &error);
240 g_assert_no_error (error);
243 "is-registered", ®istered,
244 "is-remote", &remote,
247 g_assert (registered);
251 "inactivity-timeout", 1000,
254 g_application_quit (G_APPLICATION (app));
257 g_object_unref (app);
268 g_assert (!g_application_id_is_valid (""));
269 g_assert (!g_application_id_is_valid ("."));
270 g_assert (!g_application_id_is_valid ("a"));
271 g_assert (!g_application_id_is_valid ("abc"));
272 g_assert (!g_application_id_is_valid (".abc"));
273 g_assert (!g_application_id_is_valid ("abc."));
274 g_assert (!g_application_id_is_valid ("a..b"));
275 g_assert (!g_application_id_is_valid ("a/b"));
276 g_assert (!g_application_id_is_valid ("a\nb"));
277 g_assert (!g_application_id_is_valid ("a\nb"));
278 g_assert (!g_application_id_is_valid ("_a.b"));
279 g_assert (!g_application_id_is_valid ("-a.b"));
280 id = g_new0 (gchar, 261);
281 memset (id, 'a', 260);
284 g_assert (!g_application_id_is_valid (id));
287 g_assert (g_application_id_is_valid ("a.b"));
288 g_assert (g_application_id_is_valid ("A.B"));
289 g_assert (g_application_id_is_valid ("A-.B"));
290 g_assert (g_application_id_is_valid ("a_b.c-d"));
291 g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
294 static gboolean nodbus_activated;
297 release_app (gpointer user_data)
299 g_application_release (user_data);
300 return G_SOURCE_REMOVE;
304 nodbus_activate (GApplication *app)
306 nodbus_activated = TRUE;
307 g_application_hold (app);
309 g_assert (g_application_get_dbus_connection (app) == NULL);
310 g_assert (g_application_get_dbus_object_path (app) == NULL);
312 g_idle_add (release_app, app);
318 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
319 gchar *argv[] = { binpath, NULL };
322 app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
323 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
324 g_application_run (app, 1, argv);
325 g_object_unref (app);
327 g_assert (nodbus_activated);
331 static gboolean noappid_activated;
334 noappid_activate (GApplication *app)
336 noappid_activated = TRUE;
337 g_application_hold (app);
339 g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
341 g_idle_add (release_app, app);
344 /* test that no appid -> non-unique */
348 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
349 gchar *argv[] = { binpath, NULL };
352 app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
353 g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
354 g_application_run (app, 1, argv);
355 g_object_unref (app);
357 g_assert (noappid_activated);
363 quit_app (gpointer user_data)
365 g_application_quit (user_data);
366 return G_SOURCE_REMOVE;
369 static gboolean quit_activated;
372 quit_activate (GApplication *app)
374 quit_activated = TRUE;
375 g_application_hold (app);
377 g_assert (g_application_get_dbus_connection (app) != NULL);
378 g_assert (g_application_get_dbus_object_path (app) != NULL);
380 g_idle_add (quit_app, app);
387 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
388 gchar *argv[] = { binpath, NULL };
392 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
394 app = g_application_new ("org.gtk.Unimportant",
395 G_APPLICATION_FLAGS_NONE);
396 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
397 g_application_run (app, 1, argv);
398 g_object_unref (app);
401 g_assert (quit_activated);
408 on_activate (GApplication *app)
414 g_assert (!g_application_get_is_remote (app));
416 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
417 g_assert (g_strv_length (actions) == 0);
418 g_strfreev (actions);
420 action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
421 g_action_map_add_action (G_ACTION_MAP (app), action);
423 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
424 g_assert (g_strv_length (actions) == 1);
425 g_strfreev (actions);
427 g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
428 state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
429 g_assert (g_variant_get_boolean (state) == TRUE);
431 g_action_map_remove_action (G_ACTION_MAP (app), "test");
433 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
434 g_assert (g_strv_length (actions) == 0);
435 g_strfreev (actions);
437 g_idle_add (quit_app, app);
443 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
444 gchar *argv[] = { binpath, NULL };
447 app = g_application_new ("org.gtk.Unimportant",
448 G_APPLICATION_FLAGS_NONE);
449 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
450 g_application_run (app, 1, argv);
451 g_object_unref (app);
455 typedef GApplication TestLocCmdApp;
456 typedef GApplicationClass TestLocCmdAppClass;
458 static GType test_loc_cmd_app_get_type (void);
459 G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
462 test_loc_cmd_app_init (TestLocCmdApp *app)
467 test_loc_cmd_app_startup (GApplication *app)
469 g_assert_not_reached ();
473 test_loc_cmd_app_shutdown (GApplication *app)
475 g_assert_not_reached ();
479 test_loc_cmd_app_local_command_line (GApplication *application,
487 test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
489 G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
490 G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
491 G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
495 test_local_command_line (void)
497 char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
498 gchar *argv[] = { binpath, "-invalid", NULL };
501 app = g_object_new (test_loc_cmd_app_get_type (),
502 "application-id", "org.gtk.Unimportant",
503 "flags", G_APPLICATION_FLAGS_NONE,
505 g_application_run (app, 1, argv);
506 g_object_unref (app);
511 main (int argc, char **argv)
513 g_test_init (&argc, &argv, NULL);
515 g_test_dbus_unset ();
517 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
518 g_test_add_func ("/gapplication/basic", basic);
519 g_test_add_func ("/gapplication/no-appid", test_noappid);
520 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
521 g_test_add_func ("/gapplication/properties", properties);
522 g_test_add_func ("/gapplication/app-id", appid);
523 g_test_add_func ("/gapplication/quit", test_quit);
524 g_test_add_func ("/gapplication/actions", test_actions);
525 g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
527 return g_test_run ();