5 #include "gdbus-sessionbus.h"
7 static gint outstanding_watches;
8 static GMainLoop *main_loop;
12 const gchar *expected_stdout;
21 ChildData *child = data;
22 gssize expected, actual;
25 g_assert_cmpint (status, ==, 0);
27 if (--outstanding_watches == 0)
28 g_main_loop_quit (main_loop);
30 expected = strlen (child->expected_stdout);
31 buffer = g_alloca (expected + 100);
32 actual = read (child->stdout_pipe, buffer, expected + 100);
33 close (child->stdout_pipe);
35 g_assert_cmpint (actual, >=, 0);
37 if (actual != expected ||
38 memcmp (buffer, child->expected_stdout, expected) != 0)
40 buffer[MIN(expected + 100, actual)] = '\0';
42 g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
43 child->expected_stdout,
44 (actual > expected) ? "truncated" : "full", buffer);
47 g_slice_free (ChildData, child);
51 spawn (const gchar *expected_stdout,
52 const gchar *first_arg,
63 va_start (ap, first_arg);
64 array = g_ptr_array_new ();
65 g_ptr_array_add (array, g_strdup ("./basic-application"));
66 for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
67 g_ptr_array_add (array, g_strdup (arg));
68 g_ptr_array_add (array, NULL);
69 args = (gchar **) g_ptr_array_free (array, FALSE);
71 data = g_slice_new (ChildData);
72 data->expected_stdout = expected_stdout;
74 g_spawn_async_with_pipes (NULL, args, NULL,
75 G_SPAWN_DO_NOT_REAP_CHILD,
76 NULL, NULL, &pid, NULL,
77 &data->stdout_pipe, NULL, &error);
78 g_assert_no_error (error);
80 g_child_watch_add (pid, child_quit, data);
81 outstanding_watches++;
89 main_loop = g_main_loop_new (NULL, 0);
91 /* spawn the master */
93 "open file:///a file:///b\n"
94 "cmdline '40 +' '2'\n"
98 /* make sure it becomes the master */
101 /* send it some files */
102 spawn ("exit status: 0\n",
103 "./app", "/a", "/b", NULL);
105 /* make sure the commandline arrives after the files */
108 spawn ("40 + 2 = 42\n"
110 "./cmd", "40 +", "2", NULL);
112 g_main_loop_run (main_loop);
118 static GApplication *recently_activated;
119 static GMainLoop *loop;
122 nonunique_activate (GApplication *application)
124 recently_activated = application;
127 g_main_loop_quit (loop);
130 static GApplication *
131 make_app (gboolean non_unique)
136 app = g_application_new ("org.gtk.Test-Application",
137 non_unique ? G_APPLICATION_NON_UNIQUE : 0);
138 g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
139 ok = g_application_register (app, NULL, NULL);
142 g_object_unref (app);
146 g_application_activate (app);
152 Now that we register non-unique apps on the bus we need to fix the
153 following test not to assume that it's safe to create multiple instances
154 of the same app in one process.
156 See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
157 introduced this problem.
160 test_nonunique (void)
162 GApplication *first, *second, *third, *fourth;
166 first = make_app (TRUE);
167 /* non-remote because it is non-unique */
168 g_assert (!g_application_get_is_remote (first));
169 g_assert (recently_activated == first);
170 recently_activated = NULL;
172 second = make_app (FALSE);
173 /* non-remote because it is first */
174 g_assert (!g_application_get_is_remote (second));
175 g_assert (recently_activated == second);
176 recently_activated = NULL;
178 third = make_app (TRUE);
179 /* non-remote because it is non-unique */
180 g_assert (!g_application_get_is_remote (third));
181 g_assert (recently_activated == third);
182 recently_activated = NULL;
184 fourth = make_app (FALSE);
185 /* should have failed to register due to being
186 * unable to register the object paths
188 g_assert (fourth == NULL);
189 g_assert (recently_activated == NULL);
191 g_object_unref (first);
192 g_object_unref (second);
193 g_object_unref (third);
204 GApplicationFlags flags;
208 app = g_object_new (G_TYPE_APPLICATION,
209 "application-id", "org.gtk.TestApplication",
213 "application-id", &id,
215 "is-registered", ®istered,
216 "inactivity-timeout", &timeout,
219 g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
220 g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
221 g_assert (!registered);
222 g_assert_cmpint (timeout, ==, 0);
224 g_object_unref (app);
233 g_assert (!g_application_id_is_valid (""));
234 g_assert (!g_application_id_is_valid ("."));
235 g_assert (!g_application_id_is_valid ("a"));
236 g_assert (!g_application_id_is_valid ("abc"));
237 g_assert (!g_application_id_is_valid (".abc"));
238 g_assert (!g_application_id_is_valid ("abc."));
239 g_assert (!g_application_id_is_valid ("a..b"));
240 g_assert (!g_application_id_is_valid ("a/b"));
241 g_assert (!g_application_id_is_valid ("a\nb"));
242 g_assert (!g_application_id_is_valid ("a\nb"));
243 g_assert (!g_application_id_is_valid ("_a.b"));
244 g_assert (!g_application_id_is_valid ("-a.b"));
245 id = g_new0 (gchar, 261);
246 memset (id, 'a', 260);
249 g_assert (!g_application_id_is_valid (id));
252 g_assert (g_application_id_is_valid ("a.b"));
253 g_assert (g_application_id_is_valid ("A.B"));
254 g_assert (g_application_id_is_valid ("A-.B"));
255 g_assert (g_application_id_is_valid ("a_b.c-d"));
256 g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
259 static gboolean nodbus_activated;
262 release_app (gpointer user_data)
264 g_application_release (user_data);
265 return G_SOURCE_REMOVE;
269 nodbus_activate (GApplication *app)
271 nodbus_activated = TRUE;
272 g_application_hold (app);
273 g_idle_add (release_app, app);
279 gchar *argv[] = { "./unimportant", NULL };
280 GDBusConnection *session;
283 session = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
284 g_assert (session == NULL);
286 app = g_application_new ("org.gtk.Unimportant",
287 G_APPLICATION_FLAGS_NONE);
288 g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
289 g_application_run (app, 1, argv);
290 g_object_unref (app);
292 g_assert (nodbus_activated);
296 main (int argc, char **argv)
300 g_test_init (&argc, &argv, NULL);
302 /* all the tests use a session bus with a well-known address
303 * that we can bring up and down using session_bus_up() and
304 * session_bus_down().
306 g_unsetenv ("DISPLAY");
307 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
309 g_test_add_func ("/gapplication/no-dbus", test_nodbus);
310 g_test_add_func ("/gapplication/basic", basic);
311 /* g_test_add_func ("/gapplication/non-unique", test_nonunique); */
312 g_test_add_func ("/gapplication/properties", properties);
313 g_test_add_func ("/gapplication/app-id", appid);
315 return g_test_run ();