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);
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);
250 g_application_quit (G_APPLICATION (app));
253 g_object_unref (app);
264 g_assert (!g_application_id_is_valid (""));
265 g_assert (!g_application_id_is_valid ("."));
266 g_assert (!g_application_id_is_valid ("a"));
267 g_assert (!g_application_id_is_valid ("abc"));
268 g_assert (!g_application_id_is_valid (".abc"));
269 g_assert (!g_application_id_is_valid ("abc."));
270 g_assert (!g_application_id_is_valid ("a..b"));
271 g_assert (!g_application_id_is_valid ("a/b"));
272 g_assert (!g_application_id_is_valid ("a\nb"));
273 g_assert (!g_application_id_is_valid ("a\nb"));
274 g_assert (!g_application_id_is_valid ("_a.b"));
275 g_assert (!g_application_id_is_valid ("-a.b"));
276 id = g_new0 (gchar, 261);
277 memset (id, 'a', 260);
280 g_assert (!g_application_id_is_valid (id));
283 g_assert (g_application_id_is_valid ("a.b"));
284 g_assert (g_application_id_is_valid ("A.B"));
285 g_assert (g_application_id_is_valid ("A-.B"));
286 g_assert (g_application_id_is_valid ("a_b.c-d"));
287 g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
290 static gboolean nodbus_activated;
293 release_app (gpointer user_data)
295 g_application_release (user_data);
296 return G_SOURCE_REMOVE;
300 nodbus_activate (GApplication *app)
302 nodbus_activated = TRUE;
303 g_application_hold (app);
304 g_idle_add (release_app, app);
310 gchar *argv[] = { "./unimportant", NULL };
313 app = g_application_new ("org.gtk.Unimportant",
314 G_APPLICATION_FLAGS_NONE);
315 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
316 g_application_run (app, 1, argv);
317 g_object_unref (app);
319 g_assert (nodbus_activated);
323 quit_app (gpointer user_data)
325 g_application_quit (user_data);
326 return G_SOURCE_REMOVE;
329 static gboolean quit_activated;
332 quit_activate (GApplication *app)
334 quit_activated = TRUE;
335 g_application_hold (app);
336 g_idle_add (quit_app, app);
343 gchar *argv[] = { "./unimportant", NULL };
347 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
349 app = g_application_new ("org.gtk.Unimportant",
350 G_APPLICATION_FLAGS_NONE);
351 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
352 g_application_run (app, 1, argv);
353 g_object_unref (app);
356 g_assert (quit_activated);
362 on_activate (GApplication *app)
368 g_assert (!g_application_get_is_remote (app));
370 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
371 g_assert (g_strv_length (actions) == 0);
372 g_strfreev (actions);
374 action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
375 g_action_map_add_action (G_ACTION_MAP (app), action);
377 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
378 g_assert (g_strv_length (actions) == 1);
379 g_strfreev (actions);
381 g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
382 state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
383 g_assert (g_variant_get_boolean (state) == TRUE);
385 g_action_map_remove_action (G_ACTION_MAP (app), "test");
387 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
388 g_assert (g_strv_length (actions) == 0);
389 g_strfreev (actions);
391 g_idle_add (quit_app, app);
397 gchar *argv[] = { "./unimportant", NULL };
400 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
402 app = g_application_new ("org.gtk.Unimportant",
403 G_APPLICATION_FLAGS_NONE);
404 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
405 g_application_run (app, 1, argv);
406 g_object_unref (app);
410 main (int argc, char **argv)
414 g_test_init (&argc, &argv, NULL);
416 g_test_dbus_unset ();
418 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
419 g_test_add_func ("/gapplication/basic", basic);
420 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
421 g_test_add_func ("/gapplication/properties", properties);
422 g_test_add_func ("/gapplication/app-id", appid);
423 g_test_add_func ("/gapplication/quit", test_quit);
424 g_test_add_func ("/gapplication/actions", test_actions);
426 return g_test_run ();