GApplication 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 "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_print ("open");
23
24   for (i = 0; i < n_files; i++)
25     {
26       gchar *uri = g_file_get_uri (files[i]);
27       g_print (" %s", uri);
28       g_free (uri);
29     }
30
31   g_print ("\n");
32 }
33
34 static int
35 app_main (int argc, char **argv)
36 {
37   GApplication *app;
38   int status;
39
40   app = g_application_new ("org.gtk.TestApplication",
41                            G_APPLICATION_FLAGS_HANDLES_OPEN);
42   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
43   g_signal_connect (app, "open", G_CALLBACK (open), NULL);
44   g_application_set_inactivity_timeout (app, 1000);
45   status = g_application_run (app, argc, argv);
46   g_object_unref (app);
47
48   return status;
49 }
50
51 static gint outstanding_watches;
52 static GMainLoop *main_loop;
53
54 typedef struct
55 {
56   const gchar *expected_stdout;
57   gint stdout_pipe;
58 } ChildData;
59
60 static void
61 child_quit (GPid     pid,
62             gint     status,
63             gpointer data)
64 {
65   ChildData *child = data;
66   gssize expected, actual;
67   gchar *buffer;
68
69   g_assert_cmpint (status, ==, 0);
70
71   if (--outstanding_watches == 0)
72     g_main_loop_quit (main_loop);
73
74   expected = strlen (child->expected_stdout);
75   buffer = g_alloca (expected + 100);
76   actual = read (child->stdout_pipe, buffer, expected + 100);
77   close (child->stdout_pipe);
78
79   g_assert_cmpint (actual, >=, 0);
80
81   if (actual != expected ||
82       memcmp (buffer, child->expected_stdout, expected) != 0)
83     {
84       buffer[MIN(expected + 100, actual)] = '\0';
85
86       g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
87                child->expected_stdout,
88                (actual > expected) ? "truncated" : "full", buffer);
89     }
90
91   g_slice_free (ChildData, child);
92 }
93
94 static void
95 spawn (const gchar *expected_stdout,
96        const gchar *first_arg,
97        ...)
98 {
99   gint pipefd[2];
100   GPid pid;
101
102   /* We assume that the child will not produce enough stdout
103    * to deadlock by filling the pipe.
104    */
105   pipe (pipefd);
106   pid = fork ();
107
108   if (pid == 0)
109     {
110       const gchar *arg;
111       GPtrArray *array;
112       gchar **args;
113       int status;
114       va_list ap;
115
116       dup2 (pipefd[1], 1);
117       close (pipefd[0]);
118       close (pipefd[1]);
119
120       va_start (ap, first_arg);
121       array = g_ptr_array_new ();
122       g_ptr_array_add (array, g_strdup ("./app"));
123       for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
124         g_ptr_array_add (array, g_strdup (arg));
125       g_ptr_array_add (array, NULL);
126       args = (gchar **) g_ptr_array_free (array, FALSE);
127
128       status = app_main (g_strv_length (args), args);
129       g_strfreev (args);
130
131       g_print ("exit status: %d\n", status);
132       exit (0);
133     }
134   else
135     {
136       ChildData *data;
137
138       data = g_slice_new (ChildData);
139       data->expected_stdout = expected_stdout;
140       data->stdout_pipe = pipefd[0];
141       close (pipefd[1]);
142
143       g_child_watch_add (pid, child_quit, data);
144       outstanding_watches++;
145     }
146 }
147
148 static void
149 basic (void)
150 {
151   session_bus_up ();
152
153   main_loop = g_main_loop_new (NULL, 0);
154
155   /* spawn the master */
156   spawn ("activated\nopen file:///a file:///b\nexit status: 0\n", NULL);
157
158   /* make sure it becomes the master */
159   g_usleep (100000);
160
161   /* send it some files */
162   spawn ("exit status: 0\n", "/a", "/b", NULL);
163
164   g_main_loop_run (main_loop);
165
166   session_bus_down ();
167 }
168
169 int
170 main (int argc, char **argv)
171 {
172   g_test_init (&argc, &argv, NULL);
173
174   g_test_add_func ("/gapplication/basic", basic);
175
176   return g_test_run ();
177 }