e G_SOURCE_CONTINUE/REMOVE internally
[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 static GApplication *recently_activated;
119 static GMainLoop *loop;
120
121 static void
122 nonunique_activate (GApplication *application)
123 {
124   recently_activated = application;
125
126   if (loop != NULL)
127     g_main_loop_quit (loop);
128 }
129
130 static GApplication *
131 make_app (gboolean non_unique)
132 {
133   GApplication *app;
134   gboolean ok;
135
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);
140   if (!ok)
141     {
142       g_object_unref (app);
143       return NULL;
144     }
145
146   g_application_activate (app);
147
148   return app;
149 }
150
151 static void
152 test_nonunique (void)
153 {
154   GApplication *first, *second, *third, *fourth;
155
156   session_bus_up ();
157
158   first = make_app (TRUE);
159   /* non-remote because it is non-unique */
160   g_assert (!g_application_get_is_remote (first));
161   g_assert (recently_activated == first);
162   recently_activated = NULL;
163
164   second = make_app (FALSE);
165   /* non-remote because it is first */
166   g_assert (!g_application_get_is_remote (second));
167   g_assert (recently_activated == second);
168   recently_activated = NULL;
169
170   third = make_app (TRUE);
171   /* non-remote because it is non-unique */
172   g_assert (!g_application_get_is_remote (third));
173   g_assert (recently_activated == third);
174   recently_activated = NULL;
175
176   fourth = make_app (FALSE);
177   /* should have failed to register due to being
178    * unable to register the object paths
179    */
180   g_assert (fourth == NULL);
181   g_assert (recently_activated == NULL);
182
183   g_object_unref (first);
184   g_object_unref (second);
185   g_object_unref (third);
186
187   session_bus_down ();
188 }
189
190 static void
191 properties (void)
192 {
193   GObject *app;
194   gchar *id;
195   GApplicationFlags flags;
196   gboolean registered;
197   guint timeout;
198
199   app = g_object_new (G_TYPE_APPLICATION,
200                       "application-id", "org.gtk.TestApplication",
201                       NULL);
202
203   g_object_get (app,
204                 "application-id", &id,
205                 "flags", &flags,
206                 "is-registered", &registered,
207                 "inactivity-timeout", &timeout,
208                 NULL);
209
210   g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
211   g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
212   g_assert (!registered);
213   g_assert_cmpint (timeout, ==, 0);
214
215   g_object_unref (app);
216   g_free (id);
217 }
218
219 static void
220 appid (void)
221 {
222   gchar *id;
223
224   g_assert (!g_application_id_is_valid (""));
225   g_assert (!g_application_id_is_valid ("."));
226   g_assert (!g_application_id_is_valid ("a"));
227   g_assert (!g_application_id_is_valid ("abc"));
228   g_assert (!g_application_id_is_valid (".abc"));
229   g_assert (!g_application_id_is_valid ("abc."));
230   g_assert (!g_application_id_is_valid ("a..b"));
231   g_assert (!g_application_id_is_valid ("a/b"));
232   g_assert (!g_application_id_is_valid ("a\nb"));
233   g_assert (!g_application_id_is_valid ("a\nb"));
234   g_assert (!g_application_id_is_valid ("_a.b"));
235   g_assert (!g_application_id_is_valid ("-a.b"));
236   id = g_new0 (gchar, 261);
237   memset (id, 'a', 260);
238   id[1] = '.';
239   id[260] = 0;
240   g_assert (!g_application_id_is_valid (id));
241   g_free (id);
242
243   g_assert (g_application_id_is_valid ("a.b"));
244   g_assert (g_application_id_is_valid ("A.B"));
245   g_assert (g_application_id_is_valid ("A-.B"));
246   g_assert (g_application_id_is_valid ("a_b.c-d"));
247   g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
248 }
249
250 static gboolean nodbus_activated;
251
252 static gboolean
253 release_app (gpointer user_data)
254 {
255   g_application_release (user_data);
256   return G_SOURCE_REMOVE;
257 }
258
259 static void
260 nodbus_activate (GApplication *app)
261 {
262   nodbus_activated = TRUE;
263   g_application_hold (app);
264   g_idle_add (release_app, app);
265 }
266
267 static void
268 test_nodbus (void)
269 {
270   gchar *argv[] = { "./unimportant", NULL };
271   GDBusConnection *session;
272   GApplication *app;
273
274   session = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
275   g_assert (session == NULL);
276
277   app = g_application_new ("org.gtk.Unimportant",
278                            G_APPLICATION_FLAGS_NONE);
279   g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
280   g_application_run (app, 1, argv);
281   g_object_unref (app);
282
283   g_assert (nodbus_activated);
284 }
285
286 int
287 main (int argc, char **argv)
288 {
289   g_type_init ();
290
291   g_test_init (&argc, &argv, NULL);
292
293   /* all the tests use a session bus with a well-known address
294    * that we can bring up and down using session_bus_up() and
295    * session_bus_down().
296    */
297   g_unsetenv ("DISPLAY");
298   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
299
300   g_test_add_func ("/gapplication/no-dbus", test_nodbus);
301   g_test_add_func ("/gapplication/basic", basic);
302   g_test_add_func ("/gapplication/non-unique", test_nonunique);
303   g_test_add_func ("/gapplication/properties", properties);
304   g_test_add_func ("/gapplication/app-id", appid);
305
306   return g_test_run ();
307 }