Add test case for G_APPLICATION_NON_UNIQUE
[platform/upstream/glib.git] / gio / tests / gapplication.c
1 #include <gio/gio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "gdbus-sessionbus.h"
5
6 static void
7 activate (GApplication *application)
8 {
9   g_application_hold (application);
10   g_print ("activated\n");
11   g_application_release (application);
12 }
13
14 static void
15 open (GApplication  *application,
16       GFile        **files,
17       gint           n_files,
18       const gchar   *hint)
19 {
20   gint i;
21
22   g_application_hold (application);
23   g_print ("open");
24
25   for (i = 0; i < n_files; i++)
26     {
27       gchar *uri = g_file_get_uri (files[i]);
28       g_print (" %s", uri);
29       g_free (uri);
30     }
31   g_application_release (application);
32
33   g_print ("\n");
34 }
35
36 static int
37 command_line (GApplication            *application,
38               GApplicationCommandLine *cmdline)
39 {
40   gchar **argv;
41   gint argc;
42
43   argv = g_application_command_line_get_arguments (cmdline, &argc);
44
45   g_application_command_line_print (cmdline, "%d + %d = %d\n", 40, 2, 42);
46
47   g_assert_cmpint (argc, ==, 3);
48   g_assert_cmpstr (argv[0], ==, "./cmd");
49   g_assert_cmpstr (argv[1], ==, "40 +");
50   g_assert_cmpstr (argv[2], ==, "2");
51   g_assert (argv[3] == NULL);
52   g_print ("cmdline '%s' '%s'\n", argv[1], argv[2]);
53   g_strfreev (argv);
54
55   return 42;
56 }
57
58 static int
59 app_main (int argc, char **argv)
60 {
61   GApplication *app;
62   int status;
63
64   app = g_application_new ("org.gtk.TestApplication",
65                            G_APPLICATION_HANDLES_OPEN |
66                            (strcmp (argv[0], "./cmd") == 0 ?
67                              G_APPLICATION_HANDLES_COMMAND_LINE
68                            : 0));
69   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
70   g_signal_connect (app, "open", G_CALLBACK (open), NULL);
71   g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
72 #ifdef STANDALONE
73   g_application_set_inactivity_timeout (app, 10000);
74 #else
75   g_application_set_inactivity_timeout (app, 1000);
76 #endif
77   status = g_application_run (app, argc, argv);
78   g_object_unref (app);
79
80   return status;
81 }
82
83 #ifdef STANDALONE
84 int
85 main (int argc, char **argv)
86 {
87   return app_main (argc - 1, argv + 1);
88 }
89 #else
90 static gint outstanding_watches;
91 static GMainLoop *main_loop;
92
93 typedef struct
94 {
95   const gchar *expected_stdout;
96   gint stdout_pipe;
97 } ChildData;
98
99 static void
100 child_quit (GPid     pid,
101             gint     status,
102             gpointer data)
103 {
104   ChildData *child = data;
105   gssize expected, actual;
106   gchar *buffer;
107
108   g_assert_cmpint (status, ==, 0);
109
110   if (--outstanding_watches == 0)
111     g_main_loop_quit (main_loop);
112
113   expected = strlen (child->expected_stdout);
114   buffer = g_alloca (expected + 100);
115   actual = read (child->stdout_pipe, buffer, expected + 100);
116   close (child->stdout_pipe);
117
118   g_assert_cmpint (actual, >=, 0);
119
120   if (actual != expected ||
121       memcmp (buffer, child->expected_stdout, expected) != 0)
122     {
123       buffer[MIN(expected + 100, actual)] = '\0';
124
125       g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
126                child->expected_stdout,
127                (actual > expected) ? "truncated" : "full", buffer);
128     }
129
130   g_slice_free (ChildData, child);
131 }
132
133 static void
134 spawn (const gchar *expected_stdout,
135        const gchar *first_arg,
136        ...)
137 {
138   gint pipefd[2];
139   GPid pid;
140   int s;
141
142   /* We assume that the child will not produce enough stdout
143    * to deadlock by filling the pipe.
144    */
145   s = pipe (pipefd);
146   g_assert_cmpint (s, ==, 0);
147
148   pid = fork ();
149
150   if (pid == 0)
151     {
152       const gchar *arg;
153       GPtrArray *array;
154       gchar **args;
155       int status;
156       va_list ap;
157
158       dup2 (pipefd[1], 1);
159       close (pipefd[0]);
160       close (pipefd[1]);
161
162       va_start (ap, first_arg);
163       array = g_ptr_array_new ();
164       for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
165         g_ptr_array_add (array, g_strdup (arg));
166       g_ptr_array_add (array, NULL);
167       args = (gchar **) g_ptr_array_free (array, FALSE);
168
169       status = app_main (g_strv_length (args), args);
170       g_strfreev (args);
171
172       g_print ("exit status: %d\n", status);
173       exit (0);
174     }
175   else if (pid > 0)
176     {
177       ChildData *data;
178
179       data = g_slice_new (ChildData);
180       data->expected_stdout = expected_stdout;
181       data->stdout_pipe = pipefd[0];
182       close (pipefd[1]);
183
184       g_child_watch_add (pid, child_quit, data);
185       outstanding_watches++;
186     }
187   else
188     g_assert_not_reached ();
189 }
190
191 static void
192 basic (void)
193 {
194   session_bus_up ();
195
196   main_loop = g_main_loop_new (NULL, 0);
197
198   /* spawn the master */
199   spawn ("activated\n"
200          "open file:///a file:///b\n"
201          "cmdline '40 +' '2'\n"
202          "exit status: 0\n",
203          "./app", NULL);
204
205   /* make sure it becomes the master */
206   g_usleep (100000);
207
208   /* send it some files */
209   spawn ("exit status: 0\n",
210          "./app", "/a", "/b", NULL);
211
212   /* make sure the commandline arrives after the files */
213   g_usleep (100000);
214
215   spawn ("40 + 2 = 42\n"
216          "exit status: 42\n",
217          "./cmd", "40 +", "2", NULL);
218
219   g_main_loop_run (main_loop);
220
221   session_bus_down ();
222 }
223
224
225 static GApplication *recently_activated;
226 static GMainLoop *loop;
227
228 static void
229 nonunique_activate (GApplication *application)
230 {
231   recently_activated = application;
232
233   if (loop != NULL)
234     g_main_loop_quit (loop);
235 }
236
237 static GApplication *
238 make_app (gboolean non_unique)
239 {
240   GApplication *app;
241   gboolean ok;
242
243   app = g_application_new ("org.gtk.TestApplication",
244                            non_unique ? G_APPLICATION_NON_UNIQUE : 0);
245   g_signal_connect (app, "activate", G_CALLBACK (nonunique_activate), NULL);
246   ok = g_application_register (app, NULL, NULL);
247   if (!ok)
248     {
249       g_object_unref (app);
250       return NULL;
251     }
252
253   g_application_activate (app);
254
255   return app;
256 }
257
258 static void
259 test_nonunique (void)
260 {
261   GApplication *first, *second, *third, *fourth;
262
263   session_bus_up ();
264
265   first = make_app (TRUE);
266   /* non-remote because it is non-unique */
267   g_assert (!g_application_get_is_remote (first));
268   g_assert (recently_activated == first);
269   recently_activated = NULL;
270
271   second = make_app (FALSE);
272   /* non-remote because it is first */
273   g_assert (!g_application_get_is_remote (second));
274   g_assert (recently_activated == second);
275   recently_activated = NULL;
276
277   third = make_app (TRUE);
278   /* non-remote because it is non-unique */
279   g_assert (!g_application_get_is_remote (third));
280   g_assert (recently_activated == third);
281   recently_activated = NULL;
282
283   fourth = make_app (FALSE);
284   /* should have failed to register due to being
285    * unable to register the object paths
286    */
287   g_assert (fourth == NULL);
288   g_assert (recently_activated == NULL);
289
290   g_object_unref (first);
291   g_object_unref (second);
292   g_object_unref (third);
293
294   session_bus_down ();
295 }
296
297 static void
298 properties (void)
299 {
300   GObject *app;
301   gchar *id;
302   GApplicationFlags flags;
303   gboolean registered;
304   guint timeout;
305
306   app = g_object_new (G_TYPE_APPLICATION,
307                       "application-id", "org.gtk.TestApplication",
308                       NULL);
309
310   g_object_get (app,
311                 "application-id", &id,
312                 "flags", &flags,
313                 "is-registered", &registered,
314                 "inactivity-timeout", &timeout,
315                 NULL);
316
317   g_assert_cmpstr (id, ==, "org.gtk.TestApplication");
318   g_assert_cmpint (flags, ==, G_APPLICATION_FLAGS_NONE);
319   g_assert (!registered);
320   g_assert_cmpint (timeout, ==, 0);
321
322   g_object_unref (app);
323   g_free (id);
324 }
325
326 static void
327 appid (void)
328 {
329   gchar *id;
330
331   g_assert (!g_application_id_is_valid (""));
332   g_assert (!g_application_id_is_valid ("."));
333   g_assert (!g_application_id_is_valid ("a"));
334   g_assert (!g_application_id_is_valid ("abc"));
335   g_assert (!g_application_id_is_valid (".abc"));
336   g_assert (!g_application_id_is_valid ("abc."));
337   g_assert (!g_application_id_is_valid ("a..b"));
338   g_assert (!g_application_id_is_valid ("a/b"));
339   g_assert (!g_application_id_is_valid ("a\nb"));
340   g_assert (!g_application_id_is_valid ("a\nb"));
341   g_assert (!g_application_id_is_valid ("_a.b"));
342   g_assert (!g_application_id_is_valid ("-a.b"));
343   id = g_new0 (gchar, 261);
344   memset (id, 'a', 260);
345   id[1] = '.';
346   id[260] = 0;
347   g_assert (!g_application_id_is_valid (id));
348   g_free (id);
349
350   g_assert (g_application_id_is_valid ("a.b"));
351   g_assert (g_application_id_is_valid ("A.B"));
352   g_assert (g_application_id_is_valid ("A-.B"));
353   g_assert (g_application_id_is_valid ("a_b.c-d"));
354   g_assert (g_application_id_is_valid ("org.gnome.SessionManager"));
355 }
356
357 int
358 main (int argc, char **argv)
359 {
360   g_type_init ();
361
362   g_test_init (&argc, &argv, NULL);
363
364   g_test_add_func ("/gapplication/basic", basic);
365   g_test_add_func ("/gapplication/non-unique", test_nonunique);
366   g_test_add_func ("/gapplication/properties", properties);
367   g_test_add_func ("/gapplication/app-id", appid);
368
369   return g_test_run ();
370 }
371 #endif