Fix warnings caused by an ifdeffed-out test case
[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 #include "gdbus-sessionbus.h"
6
7 static gint outstanding_watches;
8 static GMainLoop *main_loop;
9
10 typedef struct
11 {
12   const gchar *expected_stdout;
13   gint stdout_pipe;
14 } ChildData;
15
16 static void
17 child_quit (GPid     pid,
18             gint     status,
19             gpointer data)
20 {
21   ChildData *child = data;
22   gssize expected, actual;
23   gchar *buffer;
24
25   g_assert_cmpint (status, ==, 0);
26
27   if (--outstanding_watches == 0)
28     g_main_loop_quit (main_loop);
29
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);
34
35   g_assert_cmpint (actual, >=, 0);
36
37   if (actual != expected ||
38       memcmp (buffer, child->expected_stdout, expected) != 0)
39     {
40       buffer[MIN(expected + 100, actual)] = '\0';
41
42       g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
43                child->expected_stdout,
44                (actual > expected) ? "truncated" : "full", buffer);
45     }
46
47   g_slice_free (ChildData, child);
48 }
49
50 static void
51 spawn (const gchar *expected_stdout,
52        const gchar *first_arg,
53        ...)
54 {
55   GError *error = NULL;
56   const gchar *arg;
57   GPtrArray *array;
58   ChildData *data;
59   gchar **args;
60   va_list ap;
61   GPid pid;
62
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);
70
71   data = g_slice_new (ChildData);
72   data->expected_stdout = expected_stdout;
73
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);
79
80   g_child_watch_add (pid, child_quit, data);
81   outstanding_watches++;
82 }
83
84 static void
85 basic (void)
86 {
87   session_bus_up ();
88
89   main_loop = g_main_loop_new (NULL, 0);
90
91   /* spawn the master */
92   spawn ("activated\n"
93          "open file:///a file:///b\n"
94          "cmdline '40 +' '2'\n"
95          "exit status: 0\n",
96          "./app", NULL);
97
98   /* make sure it becomes the master */
99   g_usleep (100000);
100
101   /* send it some files */
102   spawn ("exit status: 0\n",
103          "./app", "/a", "/b", NULL);
104
105   /* make sure the commandline arrives after the files */
106   g_usleep (100000);
107
108   spawn ("40 + 2 = 42\n"
109          "exit status: 42\n",
110          "./cmd", "40 +", "2", NULL);
111
112   g_main_loop_run (main_loop);
113
114   session_bus_down ();
115 }
116
117
118 #if 0
119 /* Now that we register non-unique apps on the bus we need to fix the
120  * following test not to assume that it's safe to create multiple instances
121  * of the same app in one process.
122  *
123  * See https://bugzilla.gnome.org/show_bug.cgi?id=647986 for the patch that
124  * introduced this problem.
125  */
126
127 static GApplication *recently_activated;
128 static GMainLoop *loop;
129
130 static void
131 nonunique_activate (GApplication *application)
132 {
133   recently_activated = application;
134
135   if (loop != NULL)
136     g_main_loop_quit (loop);
137 }
138
139 static GApplication *
140 make_app (gboolean non_unique)
141 {
142   GApplication *app;
143   gboolean ok;
144
145   app = g_application_new ("org.gtk.Test-Application",
146                            non_unique ? G_APPLICATION_NON_UNIQUE : 0);
147   g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
148   ok = g_application_register (app, NULL, NULL);
149   if (!ok)
150     {
151       g_object_unref (app);
152       return NULL;
153     }
154
155   g_application_activate (app);
156
157   return app;
158 }
159
160 static void
161 test_nonunique (void)
162 {
163   GApplication *first, *second, *third, *fourth;
164
165   session_bus_up ();
166
167   first = make_app (TRUE);
168   /* non-remote because it is non-unique */
169   g_assert (!g_application_get_is_remote (first));
170   g_assert (recently_activated == first);
171   recently_activated = NULL;
172
173   second = make_app (FALSE);
174   /* non-remote because it is first */
175   g_assert (!g_application_get_is_remote (second));
176   g_assert (recently_activated == second);
177   recently_activated = NULL;
178
179   third = make_app (TRUE);
180   /* non-remote because it is non-unique */
181   g_assert (!g_application_get_is_remote (third));
182   g_assert (recently_activated == third);
183   recently_activated = NULL;
184
185   fourth = make_app (FALSE);
186   /* should have failed to register due to being
187    * unable to register the object paths
188    */
189   g_assert (fourth == NULL);
190   g_assert (recently_activated == NULL);
191
192   g_object_unref (first);
193   g_object_unref (second);
194   g_object_unref (third);
195
196   session_bus_down ();
197 }
198 #endif
199
200 static void
201 properties (void)
202 {
203   GObject *app;
204   gchar *id;
205   GApplicationFlags flags;
206   gboolean registered;
207   guint timeout;
208
209   app = g_object_new (G_TYPE_APPLICATION,
210                       "application-id", "org.gtk.TestApplication",
211                       NULL);
212
213   g_object_get (app,
214                 "application-id", &id,
215                 "flags", &flags,
216                 "is-registered", &registered,
217                 "inactivity-timeout", &timeout,
218                 NULL);
219
220   g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
221   g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
222   g_assert (!registered);
223   g_assert_cmpint (timeout, ==, 0);
224
225   g_object_unref (app);
226   g_free (id);
227 }
228
229 static void
230 appid (void)
231 {
232   gchar *id;
233
234   g_assert (!g_application_id_is_valid (""));
235   g_assert (!g_application_id_is_valid ("."));
236   g_assert (!g_application_id_is_valid ("a"));
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 ("abc."));
240   g_assert (!g_application_id_is_valid ("a..b"));
241   g_assert (!g_application_id_is_valid ("a/b"));
242   g_assert (!g_application_id_is_valid ("a\nb"));
243   g_assert (!g_application_id_is_valid ("a\nb"));
244   g_assert (!g_application_id_is_valid ("_a.b"));
245   g_assert (!g_application_id_is_valid ("-a.b"));
246   id = g_new0 (gchar, 261);
247   memset (id, 'a', 260);
248   id[1] = '.';
249   id[260] = 0;
250   g_assert (!g_application_id_is_valid (id));
251   g_free (id);
252
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"));
256   g_assert (g_application_id_is_valid ("a_b.c-d"));
257   g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
258 }
259
260 static gboolean nodbus_activated;
261
262 static gboolean
263 release_app (gpointer user_data)
264 {
265   g_application_release (user_data);
266   return G_SOURCE_REMOVE;
267 }
268
269 static void
270 nodbus_activate (GApplication *app)
271 {
272   nodbus_activated = TRUE;
273   g_application_hold (app);
274   g_idle_add (release_app, app);
275 }
276
277 static void
278 test_nodbus (void)
279 {
280   gchar *argv[] = { "./unimportant", NULL };
281   GDBusConnection *session;
282   GApplication *app;
283
284   session = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
285   g_assert (session == NULL);
286
287   app = g_application_new ("org.gtk.Unimportant",
288                            G_APPLICATION_FLAGS_NONE);
289   g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
290   g_application_run (app, 1, argv);
291   g_object_unref (app);
292
293   g_assert (nodbus_activated);
294 }
295
296 int
297 main (int argc, char **argv)
298 {
299   g_type_init ();
300
301   g_test_init (&argc, &argv, NULL);
302
303   /* all the tests use a session bus with a well-known address
304    * that we can bring up and down using session_bus_up() and
305    * session_bus_down().
306    */
307   g_unsetenv ("DISPLAY");
308   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
309
310   g_test_add_func ("/gapplication/no-dbus", test_nodbus);
311   g_test_add_func ("/gapplication/basic", basic);
312 /*  g_test_add_func ("/gapplication/non-unique", test_nonunique); */
313   g_test_add_func ("/gapplication/properties", properties);
314   g_test_add_func ("/gapplication/app-id", appid);
315
316   return g_test_run ();
317 }