Improve GApplication tests
[platform/upstream/glib.git] / gio / tests / gapplication.c
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5
6 #include "gdbus-tests.h"
7 #include "gdbus-sessionbus.h"
8
9 static gint outstanding_watches;
10 static GMainLoop *main_loop;
11
12 typedef struct
13 {
14   const gchar *expected_stdout;
15   gint stdout_pipe;
16 } ChildData;
17
18 static void
19 child_quit (GPid     pid,
20             gint     status,
21             gpointer data)
22 {
23   ChildData *child = data;
24   gssize expected, actual;
25   gchar *buffer;
26
27   g_assert_cmpint (status, ==, 0);
28
29   if (--outstanding_watches == 0)
30     g_main_loop_quit (main_loop);
31
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);
36
37   g_assert_cmpint (actual, >=, 0);
38
39   if (actual != expected ||
40       memcmp (buffer, child->expected_stdout, expected) != 0)
41     {
42       buffer[MIN(expected + 100, actual)] = '\0';
43
44       g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
45                child->expected_stdout,
46                (actual > expected) ? "truncated" : "full", buffer);
47     }
48
49   g_slice_free (ChildData, child);
50 }
51
52 static void
53 spawn (const gchar *expected_stdout,
54        const gchar *first_arg,
55        ...)
56 {
57   GError *error = NULL;
58   const gchar *arg;
59   GPtrArray *array;
60   ChildData *data;
61   gchar **args;
62   va_list ap;
63   GPid pid;
64
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);
72
73   data = g_slice_new (ChildData);
74   data->expected_stdout = expected_stdout;
75
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);
81
82   g_child_watch_add (pid, child_quit, data);
83   outstanding_watches++;
84 }
85
86 static void
87 basic (void)
88 {
89   GDBusConnection *c;
90
91   session_bus_up ();
92   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
93
94   main_loop = g_main_loop_new (NULL, 0);
95
96   /* spawn the master */
97   spawn ("activated\n"
98          "open file:///a file:///b\n"
99          "cmdline '40 +' '2'\n"
100          "exit status: 0\n",
101          "./app", NULL);
102
103   /* make sure it becomes the master */
104   g_usleep (100000);
105
106   /* send it some files */
107   spawn ("exit status: 0\n",
108          "./app", "/a", "/b", NULL);
109
110   /* make sure the commandline arrives after the files */
111   g_usleep (100000);
112
113   spawn ("40 + 2 = 42\n"
114          "exit status: 42\n",
115          "./cmd", "40 +", "2", NULL);
116
117   g_main_loop_run (main_loop);
118
119   session_bus_down ();
120   _g_object_wait_for_single_ref_do (c);
121   g_object_unref (c);
122
123   g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
124 }
125
126
127 #if 0
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.
131  *
132  * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
133  * introduced this problem.
134  */
135
136 static GApplication *recently_activated;
137 static GMainLoop *loop;
138
139 static void
140 nonunique_activate (GApplication *application)
141 {
142   recently_activated = application;
143
144   if (loop != NULL)
145     g_main_loop_quit (loop);
146 }
147
148 static GApplication *
149 make_app (gboolean non_unique)
150 {
151   GApplication *app;
152   gboolean ok;
153
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);
158   if (!ok)
159     {
160       g_object_unref (app);
161       return NULL;
162     }
163
164   g_application_activate (app);
165
166   return app;
167 }
168
169 static void
170 test_nonunique (void)
171 {
172   GApplication *first, *second, *third, *fourth;
173
174   session_bus_up ();
175
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;
181
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;
187
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;
193
194   fourth = make_app (FALSE);
195   /* should have failed to register due to being
196    * unable to register the object paths
197    */
198   g_assert (fourth == NULL);
199   g_assert (recently_activated == NULL);
200
201   g_object_unref (first);
202   g_object_unref (second);
203   g_object_unref (third);
204
205   session_bus_down ();
206 }
207 #endif
208
209 static void
210 properties (void)
211 {
212   GDBusConnection *c;
213   GObject *app;
214   gchar *id;
215   GApplicationFlags flags;
216   gboolean registered;
217   guint timeout;
218   gboolean remote;
219   gboolean ret;
220   GError *error = NULL;
221
222   session_bus_up ();
223   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
224
225   app = g_object_new (G_TYPE_APPLICATION,
226                       "application-id", "org.gtk.TestApplication",
227                       NULL);
228
229   g_object_get (app,
230                 "application-id", &id,
231                 "flags", &flags,
232                 "is-registered", &registered,
233                 "inactivity-timeout", &timeout,
234                 NULL);
235
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);
240
241   ret = g_application_register (G_APPLICATION (app), NULL, &error);
242   g_assert (ret);
243   g_assert_no_error (error);
244
245   g_object_get (app,
246                 "is-registered", &registered,
247                 "is-remote", &remote,
248                 NULL);
249
250   g_assert (registered);
251   g_assert (!remote);
252
253   g_application_quit (G_APPLICATION (app));
254
255   g_object_unref (app);
256   g_free (id);
257
258   session_bus_down ();
259   _g_object_wait_for_single_ref (c);
260   g_object_unref (c);
261
262   g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
263 }
264
265 static void
266 appid (void)
267 {
268   gchar *id;
269
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);
284   id[1] = '.';
285   id[260] = 0;
286   g_assert (!g_application_id_is_valid (id));
287   g_free (id);
288
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"));
294 }
295
296 static gboolean nodbus_activated;
297
298 static gboolean
299 release_app (gpointer user_data)
300 {
301   g_application_release (user_data);
302   return G_SOURCE_REMOVE;
303 }
304
305 static void
306 nodbus_activate (GApplication *app)
307 {
308   nodbus_activated = TRUE;
309   g_application_hold (app);
310   g_idle_add (release_app, app);
311 }
312
313 static void
314 test_nodbus (void)
315 {
316   gchar *argv[] = { "./unimportant", NULL };
317   GApplication *app;
318
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);
324
325   g_assert (nodbus_activated);
326 }
327
328 static gboolean
329 quit_app (gpointer user_data)
330 {
331   g_application_quit (user_data);
332   return G_SOURCE_REMOVE;
333 }
334
335 static gboolean quit_activated;
336
337 static void
338 quit_activate (GApplication *app)
339 {
340   quit_activated = TRUE;
341   g_application_hold (app);
342   g_idle_add (quit_app, app);
343 }
344
345 static void
346 test_quit (void)
347 {
348   GDBusConnection *c;
349   gchar *argv[] = { "./unimportant", NULL };
350   GApplication *app;
351
352   session_bus_up ();
353   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
354
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);
360
361   g_assert (quit_activated);
362
363   session_bus_down ();
364
365   _g_object_wait_for_single_ref (c);
366   g_object_unref (c);
367
368   g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
369 }
370
371 static void
372 on_activate (GApplication *app)
373 {
374   gchar **actions;
375   GAction *action;
376   GVariant *state;
377
378   g_assert (!g_application_get_is_remote (app));
379
380   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
381   g_assert (g_strv_length (actions) == 0);
382   g_strfreev (actions);
383
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);
386
387   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
388   g_assert (g_strv_length (actions) == 1);
389   g_strfreev (actions);
390
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);
394
395   g_action_map_remove_action (G_ACTION_MAP (app), "test");
396
397   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
398   g_assert (g_strv_length (actions) == 0);
399   g_strfreev (actions);
400
401   g_idle_add (quit_app, app);
402 }
403
404 static void
405 test_actions (void)
406 {
407   gchar *argv[] = { "./unimportant", NULL };
408   GApplication *app;
409
410   g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
411
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);
417 }
418
419 int
420 main (int argc, char **argv)
421 {
422   g_type_init ();
423
424   g_test_init (&argc, &argv, NULL);
425
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().
429    */
430   g_unsetenv ("DISPLAY");
431   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
432
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);
440
441   return g_test_run ();
442 }