tests: Add missing va_end()
[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   va_end (ap);
74
75   data = g_slice_new (ChildData);
76   data->expected_stdout = expected_stdout;
77
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);
83
84   g_child_watch_add (pid, child_quit, data);
85   outstanding_watches++;
86 }
87
88 static void
89 basic (void)
90 {
91   GDBusConnection *c;
92
93   session_bus_up ();
94   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
95
96   main_loop = g_main_loop_new (NULL, 0);
97
98   /* spawn the master */
99   spawn ("activated\n"
100          "open file:///a file:///b\n"
101          "cmdline '40 +' '2'\n"
102          "exit status: 0\n",
103          "./app", NULL);
104
105   /* make sure it becomes the master */
106   g_usleep (100000);
107
108   /* send it some files */
109   spawn ("exit status: 0\n",
110          "./app", "/a", "/b", NULL);
111
112   /* make sure the commandline arrives after the files */
113   g_usleep (100000);
114
115   spawn ("40 + 2 = 42\n"
116          "exit status: 42\n",
117          "./cmd", "40 +", "2", NULL);
118
119   g_main_loop_run (main_loop);
120
121   g_object_unref (c);
122   session_bus_down ();
123 }
124
125
126 #if 0
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.
130  *
131  * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
132  * introduced this problem.
133  */
134
135 static GApplication *recently_activated;
136 static GMainLoop *loop;
137
138 static void
139 nonunique_activate (GApplication *application)
140 {
141   recently_activated = application;
142
143   if (loop != NULL)
144     g_main_loop_quit (loop);
145 }
146
147 static GApplication *
148 make_app (gboolean non_unique)
149 {
150   GApplication *app;
151   gboolean ok;
152
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);
157   if (!ok)
158     {
159       g_object_unref (app);
160       return NULL;
161     }
162
163   g_application_activate (app);
164
165   return app;
166 }
167
168 static void
169 test_nonunique (void)
170 {
171   GApplication *first, *second, *third, *fourth;
172
173   session_bus_up ();
174
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;
180
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;
186
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;
192
193   fourth = make_app (FALSE);
194   /* should have failed to register due to being
195    * unable to register the object paths
196    */
197   g_assert (fourth == NULL);
198   g_assert (recently_activated == NULL);
199
200   g_object_unref (first);
201   g_object_unref (second);
202   g_object_unref (third);
203
204   session_bus_down ();
205 }
206 #endif
207
208 static void
209 properties (void)
210 {
211   GDBusConnection *c;
212   GObject *app;
213   gchar *id;
214   GApplicationFlags flags;
215   gboolean registered;
216   guint timeout;
217   gboolean remote;
218   gboolean ret;
219   GError *error = NULL;
220
221   session_bus_up ();
222   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
223
224   app = g_object_new (G_TYPE_APPLICATION,
225                       "application-id", "org.gtk.TestApplication",
226                       NULL);
227
228   g_object_get (app,
229                 "application-id", &id,
230                 "flags", &flags,
231                 "is-registered", &registered,
232                 "inactivity-timeout", &timeout,
233                 NULL);
234
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);
239
240   ret = g_application_register (G_APPLICATION (app), NULL, &error);
241   g_assert (ret);
242   g_assert_no_error (error);
243
244   g_object_get (app,
245                 "is-registered", &registered,
246                 "is-remote", &remote,
247                 NULL);
248
249   g_assert (registered);
250   g_assert (!remote);
251
252   g_object_set (app,
253                 "inactivity-timeout", 1000,
254                 NULL);
255
256   g_application_quit (G_APPLICATION (app));
257
258   g_object_unref (c);
259   g_object_unref (app);
260   g_free (id);
261
262   session_bus_down ();
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
311   g_assert (g_application_get_dbus_connection (app) == NULL);
312   g_assert (g_application_get_dbus_object_path (app) == NULL);
313
314   g_idle_add (release_app, app);
315 }
316
317 static void
318 test_nodbus (void)
319 {
320   gchar *argv[] = { "./unimportant", NULL };
321   GApplication *app;
322
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);
327
328   g_assert (nodbus_activated);
329 }
330
331 static gboolean noappid_activated;
332
333 static void
334 noappid_activate (GApplication *app)
335 {
336   noappid_activated = TRUE;
337   g_application_hold (app);
338
339   g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
340
341   g_idle_add (release_app, app);
342 }
343
344 /* test that no appid -> non-unique */
345 static void
346 test_noappid (void)
347 {
348   gchar *argv[] = { "./unimportant", NULL };
349   GApplication *app;
350
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);
355
356   g_assert (noappid_activated);
357 }
358
359
360 static gboolean
361 quit_app (gpointer user_data)
362 {
363   g_application_quit (user_data);
364   return G_SOURCE_REMOVE;
365 }
366
367 static gboolean quit_activated;
368
369 static void
370 quit_activate (GApplication *app)
371 {
372   quit_activated = TRUE;
373   g_application_hold (app);
374
375   g_assert (g_application_get_dbus_connection (app) != NULL);
376   g_assert (g_application_get_dbus_object_path (app) != NULL);
377
378   g_idle_add (quit_app, app);
379 }
380
381 static void
382 test_quit (void)
383 {
384   GDBusConnection *c;
385   gchar *argv[] = { "./unimportant", NULL };
386   GApplication *app;
387
388   session_bus_up ();
389   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
390
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);
396   g_object_unref (c);
397
398   g_assert (quit_activated);
399
400   session_bus_down ();
401 }
402
403 static void
404 on_activate (GApplication *app)
405 {
406   gchar **actions;
407   GAction *action;
408   GVariant *state;
409
410   g_assert (!g_application_get_is_remote (app));
411
412   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
413   g_assert (g_strv_length (actions) == 0);
414   g_strfreev (actions);
415
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);
418
419   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
420   g_assert (g_strv_length (actions) == 1);
421   g_strfreev (actions);
422
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);
426
427   g_action_map_remove_action (G_ACTION_MAP (app), "test");
428
429   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
430   g_assert (g_strv_length (actions) == 0);
431   g_strfreev (actions);
432
433   g_idle_add (quit_app, app);
434 }
435
436 static void
437 test_actions (void)
438 {
439   gchar *argv[] = { "./unimportant", NULL };
440   GApplication *app;
441
442   g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
443
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);
449 }
450
451 int
452 main (int argc, char **argv)
453 {
454   g_type_init ();
455
456   g_test_init (&argc, &argv, NULL);
457
458   g_test_dbus_unset ();
459
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);
468
469   return g_test_run ();
470 }