Merge remote branch 'gvdb/master'
[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 int
225 main (int argc, char **argv)
226 {
227   g_test_init (&argc, &argv, NULL);
228
229   g_test_add_func ("/gapplication/basic", basic);
230
231   return g_test_run ();
232 }
233 #endif