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);
75 data = g_slice_new (ChildData);
76 data->expected_stdout = expected_stdout;
78 g_spawn_async_with_pipes (NULL, args, NULL,
79 G_SPAWN_DO_NOT_REAP_CHILD,
80 NULL, NULL, &pid, NULL,
81 &data->stdout_pipe, NULL, &error);
82 g_assert_no_error (error);
84 g_child_watch_add (pid, child_quit, data);
85 outstanding_watches++;
94 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
96 main_loop = g_main_loop_new (NULL, 0);
98 /* spawn the master */
100 "open file:///a file:///b\n"
101 "cmdline '40 +' '2'\n"
105 /* make sure it becomes the master */
108 /* send it some files */
109 spawn ("exit status: 0\n",
110 "./app", "/a", "/b", NULL);
112 /* make sure the commandline arrives after the files */
115 spawn ("40 + 2 = 42\n"
117 "./cmd", "40 +", "2", NULL);
119 g_main_loop_run (main_loop);
127 /* Now that we register non-unique apps on the bus we need to fix the
128 * following test not to assume that it's safe to create multiple instances
129 * of the same app in one process.
131 * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
132 * introduced this problem.
135 static GApplication *recently_activated;
136 static GMainLoop *loop;
139 nonunique_activate (GApplication *application)
141 recently_activated = application;
144 g_main_loop_quit (loop);
147 static GApplication *
148 make_app (gboolean non_unique)
153 app = g_application_new ("org.gtk.Test-Application",
154 non_unique ? G_APPLICATION_NON_UNIQUE : 0);
155 g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
156 ok = g_application_register (app, NULL, NULL);
159 g_object_unref (app);
163 g_application_activate (app);
169 test_nonunique (void)
171 GApplication *first, *second, *third, *fourth;
175 first = make_app (TRUE);
176 /* non-remote because it is non-unique */
177 g_assert (!g_application_get_is_remote (first));
178 g_assert (recently_activated == first);
179 recently_activated = NULL;
181 second = make_app (FALSE);
182 /* non-remote because it is first */
183 g_assert (!g_application_get_is_remote (second));
184 g_assert (recently_activated == second);
185 recently_activated = NULL;
187 third = make_app (TRUE);
188 /* non-remote because it is non-unique */
189 g_assert (!g_application_get_is_remote (third));
190 g_assert (recently_activated == third);
191 recently_activated = NULL;
193 fourth = make_app (FALSE);
194 /* should have failed to register due to being
195 * unable to register the object paths
197 g_assert (fourth == NULL);
198 g_assert (recently_activated == NULL);
200 g_object_unref (first);
201 g_object_unref (second);
202 g_object_unref (third);
214 GApplicationFlags flags;
219 GError *error = NULL;
222 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
224 app = g_object_new (G_TYPE_APPLICATION,
225 "application-id", "org.gtk.TestApplication",
229 "application-id", &id,
231 "is-registered", ®istered,
232 "inactivity-timeout", &timeout,
235 g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
236 g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
237 g_assert (!registered);
238 g_assert_cmpint (timeout, ==, 0);
240 ret = g_application_register (G_APPLICATION (app), NULL, &error);
242 g_assert_no_error (error);
245 "is-registered", ®istered,
246 "is-remote", &remote,
249 g_assert (registered);
253 "inactivity-timeout", 1000,
256 g_application_quit (G_APPLICATION (app));
259 g_object_unref (app);
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);
311 g_assert (g_application_get_dbus_connection (app) == NULL);
312 g_assert (g_application_get_dbus_object_path (app) == NULL);
314 g_idle_add (release_app, app);
320 gchar *argv[] = { "./unimportant", NULL };
323 app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
324 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
325 g_application_run (app, 1, argv);
326 g_object_unref (app);
328 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 gchar *argv[] = { "./unimportant", NULL };
351 app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
352 g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
353 g_application_run (app, 1, argv);
354 g_object_unref (app);
356 g_assert (noappid_activated);
361 quit_app (gpointer user_data)
363 g_application_quit (user_data);
364 return G_SOURCE_REMOVE;
367 static gboolean quit_activated;
370 quit_activate (GApplication *app)
372 quit_activated = TRUE;
373 g_application_hold (app);
375 g_assert (g_application_get_dbus_connection (app) != NULL);
376 g_assert (g_application_get_dbus_object_path (app) != NULL);
378 g_idle_add (quit_app, app);
385 gchar *argv[] = { "./unimportant", NULL };
389 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
391 app = g_application_new ("org.gtk.Unimportant",
392 G_APPLICATION_FLAGS_NONE);
393 g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
394 g_application_run (app, 1, argv);
395 g_object_unref (app);
398 g_assert (quit_activated);
404 on_activate (GApplication *app)
410 g_assert (!g_application_get_is_remote (app));
412 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
413 g_assert (g_strv_length (actions) == 0);
414 g_strfreev (actions);
416 action = (GAction*)g_simple_action_new_stateful ("test", G_VARIANT_TYPE_BOOLEAN, g_variant_new_boolean (FALSE));
417 g_action_map_add_action (G_ACTION_MAP (app), action);
419 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
420 g_assert (g_strv_length (actions) == 1);
421 g_strfreev (actions);
423 g_action_group_change_action_state (G_ACTION_GROUP (app), "test", g_variant_new_boolean (TRUE));
424 state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
425 g_assert (g_variant_get_boolean (state) == TRUE);
427 g_action_map_remove_action (G_ACTION_MAP (app), "test");
429 actions = g_action_group_list_actions (G_ACTION_GROUP (app));
430 g_assert (g_strv_length (actions) == 0);
431 g_strfreev (actions);
433 g_idle_add (quit_app, app);
439 gchar *argv[] = { "./unimportant", NULL };
442 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
444 app = g_application_new ("org.gtk.Unimportant",
445 G_APPLICATION_FLAGS_NONE);
446 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
447 g_application_run (app, 1, argv);
448 g_object_unref (app);
452 main (int argc, char **argv)
456 g_test_init (&argc, &argv, NULL);
458 g_test_dbus_unset ();
460 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
461 g_test_add_func ("/gapplication/basic", basic);
462 g_test_add_func ("/gapplication/no-appid", test_noappid);
463 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
464 g_test_add_func ("/gapplication/properties", properties);
465 g_test_add_func ("/gapplication/app-id", appid);
466 g_test_add_func ("/gapplication/quit", test_quit);
467 g_test_add_func ("/gapplication/actions", test_actions);
469 return g_test_run ();