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,
65 va_start (ap, first_arg);
66 array = g_ptr_array_new ();
67 g_ptr_array_add (array, g_strdup ("./basic-application"));
68 for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
69 g_ptr_array_add (array, g_strdup (arg));
70 g_ptr_array_add (array, NULL);
71 args = (gchar **) g_ptr_array_free (array, FALSE);
73 data = g_slice_new (ChildData);
74 data->expected_stdout = expected_stdout;
76 g_spawn_async_with_pipes (NULL, args, NULL,
77 G_SPAWN_DO_NOT_REAP_CHILD,
78 NULL, NULL, &pid, NULL,
79 &data->stdout_pipe, NULL, &error);
80 g_assert_no_error (error);
82 g_child_watch_add (pid, child_quit, data);
83 outstanding_watches++;
92 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
94 main_loop = g_main_loop_new (NULL, 0);
96 /* spawn the master */
98 "open file:///a file:///b\n"
99 "cmdline '40 +' '2'\n"
103 /* make sure it becomes the master */
106 /* send it some files */
107 spawn ("exit status: 0\n",
108 "./app", "/a", "/b", NULL);
110 /* make sure the commandline arrives after the files */
113 spawn ("40 + 2 = 42\n"
115 "./cmd", "40 +", "2", NULL);
117 g_main_loop_run (main_loop);
120 _g_object_wait_for_single_ref_do (c);
123 g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
128 /* Now that we register non-unique apps on the bus we need to fix the
129 * following test not to assume that it's safe to create multiple instances
130 * of the same app in one process.
132 * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
133 * introduced this problem.
136 static GApplication *recently_activated;
137 static GMainLoop *loop;
140 nonunique_activate (GApplication *application)
142 recently_activated = application;
145 g_main_loop_quit (loop);
148 static GApplication *
149 make_app (gboolean non_unique)
154 app = g_application_new ("org.gtk.Test-Application",
155 non_unique ? G_APPLICATION_NON_UNIQUE : 0);
156 g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
157 ok = g_application_register (app, NULL, NULL);
160 g_object_unref (app);
164 g_application_activate (app);
170 test_nonunique (void)
172 GApplication *first, *second, *third, *fourth;
176 first = make_app (TRUE);
177 /* non-remote because it is non-unique */
178 g_assert (!g_application_get_is_remote (first));
179 g_assert (recently_activated == first);
180 recently_activated = NULL;
182 second = make_app (FALSE);
183 /* non-remote because it is first */
184 g_assert (!g_application_get_is_remote (second));
185 g_assert (recently_activated == second);
186 recently_activated = NULL;
188 third = make_app (TRUE);
189 /* non-remote because it is non-unique */
190 g_assert (!g_application_get_is_remote (third));
191 g_assert (recently_activated == third);
192 recently_activated = NULL;
194 fourth = make_app (FALSE);
195 /* should have failed to register due to being
196 * unable to register the object paths
198 g_assert (fourth == NULL);
199 g_assert (recently_activated == NULL);
201 g_object_unref (first);
202 g_object_unref (second);
203 g_object_unref (third);
215 GApplicationFlags flags;
220 GError *error = NULL;
223 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
225 app = g_object_new (G_TYPE_APPLICATION,
226 "application-id", "org.gtk.TestApplication",
230 "application-id", &id,
232 "is-registered", ®istered,
233 "inactivity-timeout", &timeout,
236 g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
237 g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
238 g_assert (!registered);
239 g_assert_cmpint (timeout, ==, 0);
241 ret = g_application_register (G_APPLICATION (app), NULL, &error);
243 g_assert_no_error (error);
246 "is-registered", ®istered,
247 "is-remote", &remote,
250 g_assert (registered);
253 g_application_quit (G_APPLICATION (app));
255 g_object_unref (app);
259 _g_object_wait_for_single_ref (c);
262 g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
270 g_assert (!g_application_id_is_valid (""));
271 g_assert (!g_application_id_is_valid ("."));
272 g_assert (!g_application_id_is_valid ("a"));
273 g_assert (!g_application_id_is_valid ("abc"));
274 g_assert (!g_application_id_is_valid (".abc"));
275 g_assert (!g_application_id_is_valid ("abc."));
276 g_assert (!g_application_id_is_valid ("a..b"));
277 g_assert (!g_application_id_is_valid ("a/b"));
278 g_assert (!g_application_id_is_valid ("a\nb"));
279 g_assert (!g_application_id_is_valid ("a\nb"));
280 g_assert (!g_application_id_is_valid ("_a.b"));
281 g_assert (!g_application_id_is_valid ("-a.b"));
282 id = g_new0 (gchar, 261);
283 memset (id, 'a', 260);
286 g_assert (!g_application_id_is_valid (id));
289 g_assert (g_application_id_is_valid ("a.b"));
290 g_assert (g_application_id_is_valid ("A.B"));
291 g_assert (g_application_id_is_valid ("A-.B"));
292 g_assert (g_application_id_is_valid ("a_b.c-d"));
293 g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
296 static gboolean nodbus_activated;
299 release_app (gpointer user_data)
301 g_application_release (user_data);
302 return G_SOURCE_REMOVE;
306 nodbus_activate (GApplication *app)
308 nodbus_activated = TRUE;
309 g_application_hold (app);
310 g_idle_add (release_app, app);
316 gchar *argv[] = { "./unimportant", NULL };
319 app = g_application_new ("org.gtk.Unimportant",
320 G_APPLICATION_FLAGS_NONE);
321 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
322 g_application_run (app, 1, argv);
323 g_object_unref (app);
325 g_assert (nodbus_activated);
329 quit_app (gpointer user_data)
331 g_application_quit (user_data);
332 return G_SOURCE_REMOVE;
335 static gboolean quit_activated;
338 quit_activate (GApplication *app)
340 quit_activated = TRUE;
341 g_application_hold (app);
342 g_idle_add (quit_app, app);
349 gchar *argv[] = { "./unimportant", NULL };
353 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
355 app = g_application_new ("org.gtk.Unimportant",
356 G_APPLICATION_FLAGS_NONE);
357 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
358 g_application_run (app, 1, argv);
359 g_object_unref (app);
361 g_assert (quit_activated);
365 _g_object_wait_for_single_ref (c);
368 g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
372 on_activate (GApplication *app)
378 g_assert (!g_application_get_is_remote (app));
380 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
381 g_assert (g_strv_length (actions) == 0);
382 g_strfreev (actions);
384 action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
385 g_action_map_add_action (G_ACTION_MAP (app), action);
387 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
388 g_assert (g_strv_length (actions) == 1);
389 g_strfreev (actions);
391 g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
392 state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
393 g_assert (g_variant_get_boolean (state) == TRUE);
395 g_action_map_remove_action (G_ACTION_MAP (app), "test");
397 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
398 g_assert (g_strv_length (actions) == 0);
399 g_strfreev (actions);
401 g_idle_add (quit_app, app);
407 gchar *argv[] = { "./unimportant", NULL };
410 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
412 app = g_application_new ("org.gtk.Unimportant",
413 G_APPLICATION_FLAGS_NONE);
414 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
415 g_application_run (app, 1, argv);
416 g_object_unref (app);
420 main (int argc, char **argv)
424 g_test_init (&argc, &argv, NULL);
426 /* all the tests use a session bus with a well-known address
427 * that we can bring up and down using session_bus_up() and
428 * session_bus_down().
430 g_unsetenv ("DISPLAY");
431 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
433 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
434 g_test_add_func ("/gapplication/basic", basic);
435 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
436 g_test_add_func ("/gapplication/properties", properties);
437 g_test_add_func ("/gapplication/app-id", appid);
438 g_test_add_func ("/gapplication/quit", test_quit);
439 g_test_add_func ("/gapplication/actions", test_actions);
441 return g_test_run ();