4 #include "gdbus-sessionbus.h"
7 activate (GApplication *application)
9 g_application_hold (application);
10 g_print ("activated\n");
11 g_application_release (application);
15 open (GApplication *application,
22 g_application_hold (application);
25 for (i = 0; i < n_files; i++)
27 gchar *uri = g_file_get_uri (files[i]);
31 g_application_release (application);
37 command_line (GApplication *application,
38 GApplicationCommandLine *cmdline)
43 argv = g_application_command_line_get_arguments (cmdline, &argc);
45 g_application_command_line_print (cmdline, "%d + %d = %d\n", 40, 2, 42);
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]);
59 app_main (int argc, char **argv)
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
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);
73 g_application_set_inactivity_timeout (app, 10000);
75 g_application_set_inactivity_timeout (app, 1000);
77 status = g_application_run (app, argc, argv);
85 main (int argc, char **argv)
87 return app_main (argc - 1, argv + 1);
90 static gint outstanding_watches;
91 static GMainLoop *main_loop;
95 const gchar *expected_stdout;
100 child_quit (GPid pid,
104 ChildData *child = data;
105 gssize expected, actual;
108 g_assert_cmpint (status, ==, 0);
110 if (--outstanding_watches == 0)
111 g_main_loop_quit (main_loop);
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);
118 g_assert_cmpint (actual, >=, 0);
120 if (actual != expected ||
121 memcmp (buffer, child->expected_stdout, expected) != 0)
123 buffer[MIN(expected + 100, actual)] = '\0';
125 g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
126 child->expected_stdout,
127 (actual > expected) ? "truncated" : "full", buffer);
130 g_slice_free (ChildData, child);
134 spawn (const gchar *expected_stdout,
135 const gchar *first_arg,
142 /* We assume that the child will not produce enough stdout
143 * to deadlock by filling the pipe.
146 g_assert_cmpint (s, ==, 0);
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);
169 status = app_main (g_strv_length (args), args);
172 g_print ("exit status: %d\n", status);
179 data = g_slice_new (ChildData);
180 data->expected_stdout = expected_stdout;
181 data->stdout_pipe = pipefd[0];
184 g_child_watch_add (pid, child_quit, data);
185 outstanding_watches++;
188 g_assert_not_reached ();
196 main_loop = g_main_loop_new (NULL, 0);
198 /* spawn the master */
200 "open file:///a file:///b\n"
201 "cmdline '40 +' '2'\n"
205 /* make sure it becomes the master */
208 /* send it some files */
209 spawn ("exit status: 0\n",
210 "./app", "/a", "/b", NULL);
212 /* make sure the commandline arrives after the files */
215 spawn ("40 + 2 = 42\n"
217 "./cmd", "40 +", "2", NULL);
219 g_main_loop_run (main_loop);
225 main (int argc, char **argv)
227 g_test_init (&argc, &argv, NULL);
229 g_test_add_func ("/gapplication/basic", basic);
231 return g_test_run ();