Add a test for the GAppLaunchContext::launched signal
[platform/upstream/glib.git] / gio / tests / appinfo.c
1
2 #include <locale.h>
3
4 #include <gio/gio.h>
5 #include <gio/gdesktopappinfo.h>
6
7 static void
8 test_launch (void)
9 {
10   GAppInfo *appinfo;
11   GError *error;
12   GFile *file;
13   GList *l;
14
15   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
16   g_assert (appinfo != NULL);
17
18   error = NULL;
19   g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
20   g_assert_no_error (error);
21
22   g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
23   g_assert_no_error (error);
24
25   file = g_file_new_for_path (SRCDIR "/appinfo-test.desktop");
26   l = NULL;
27   l = g_list_append (l, file);
28
29   g_assert (g_app_info_launch (appinfo, l, NULL, &error));
30   g_assert_no_error (error);
31   g_list_free (l);
32   g_object_unref (file);
33
34   l = NULL;
35   l = g_list_append (l, "file://" SRCDIR "/appinfo-test.desktop");
36   l = g_list_append (l, "file:///etc/group#adm");
37
38   g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
39   g_assert_no_error (error);
40   g_list_free (l);
41
42   g_object_unref (appinfo);
43 }
44
45 static void
46 test_locale (const char *locale)
47 {
48   GAppInfo *appinfo;
49   const gchar *orig;
50
51   orig = setlocale (LC_ALL, NULL);
52   g_setenv ("LANGUAGE", locale, TRUE);
53   setlocale (LC_ALL, "");
54
55   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
56
57   if (g_strcmp0 (locale, "C") == 0)
58     {
59       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
60       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
61       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
62     }
63   else if (g_str_has_prefix (locale, "en"))
64     {
65       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
66       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
67       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
68     }
69   else if (g_str_has_prefix (locale, "de"))
70     {
71       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
72       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
73       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
74     }
75
76   g_object_unref (appinfo);
77
78   g_setenv ("LANGUAGE", orig, TRUE);
79   setlocale (LC_ALL, "");
80 }
81
82 static void
83 test_text (void)
84 {
85   test_locale ("C");
86   test_locale ("en_US");
87   test_locale ("de");
88   test_locale ("de_DE.UTF-8");
89 }
90
91 static void
92 test_basic (void)
93 {
94   GAppInfo *appinfo;
95   GAppInfo *appinfo2;
96   GIcon *icon, *icon2;
97
98   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
99
100   g_assert (g_app_info_get_id (appinfo) == NULL);
101   g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
102
103   icon = g_app_info_get_icon (appinfo);
104   g_assert (G_IS_THEMED_ICON (icon));
105   icon2 = g_themed_icon_new ("testicon");
106   g_assert (g_icon_equal (icon, icon2));
107   g_object_unref (icon2);
108
109   appinfo2 = g_app_info_dup (appinfo);
110   g_assert (g_app_info_get_id (appinfo) == g_app_info_get_id (appinfo2));
111   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
112
113   g_object_unref (appinfo);
114   g_object_unref (appinfo2);
115 }
116
117 static void
118 test_show_in (void)
119 {
120   GAppInfo *appinfo;
121
122   g_desktop_app_info_set_desktop_env ("GNOME");
123
124   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
125   g_assert (g_app_info_should_show (appinfo));
126   g_object_unref (appinfo);
127
128   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-gnome.desktop");
129   g_assert (g_app_info_should_show (appinfo));
130   g_object_unref (appinfo);
131
132   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-notgnome.desktop");
133   g_assert (!g_app_info_should_show (appinfo));
134   g_object_unref (appinfo);
135 }
136
137 static void
138 test_commandline (void)
139 {
140   GAppInfo *appinfo;
141   GError *error;
142
143   error = NULL;
144   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
145                                                 "cmdline-app-test",
146                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
147                                                 &error);
148   g_assert (appinfo != NULL);
149   g_assert_no_error (error);
150   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
151   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %u");
152   g_assert (g_app_info_supports_uris (appinfo));
153   g_assert (!g_app_info_supports_files (appinfo));
154
155   g_object_unref (appinfo);
156
157   error = NULL;
158   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
159                                                 "cmdline-app-test",
160                                                 G_APP_INFO_CREATE_NONE,
161                                                 &error);
162   g_assert (appinfo != NULL);
163   g_assert_no_error (error);
164   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
165   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %f");
166   g_assert (!g_app_info_supports_uris (appinfo));
167   g_assert (g_app_info_supports_files (appinfo));
168
169   g_object_unref (appinfo);
170 }
171
172 static void
173 test_launch_context (void)
174 {
175   GAppLaunchContext *context;
176   GAppInfo *appinfo;
177   gchar *str;
178
179   context = g_app_launch_context_new ();
180   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
181                                                 "cmdline-app-test",
182                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
183                                                 NULL);
184
185   str = g_app_launch_context_get_display (context, appinfo, NULL);
186   g_assert (str == NULL);
187
188   str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
189   g_assert (str == NULL);
190
191   g_object_unref (appinfo);
192   g_object_unref (context);
193 }
194
195 static gboolean launched_reached;
196
197 static void
198 launched (GAppLaunchContext *context,
199           GAppInfo          *info,
200           GVariant          *platform_data,
201           gpointer           user_data)
202 {
203   gint pid;
204
205   pid = 0;
206   g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
207   g_assert (pid != 0);
208
209   launched_reached = TRUE;
210 }
211
212 static void
213 launch_failed (GAppLaunchContext *context,
214                const gchar       *startup_notify_id)
215 {
216   g_assert_not_reached ();
217 }
218
219 static void
220 test_launch_context_signals (void)
221 {
222   GAppLaunchContext *context;
223   GAppInfo *appinfo;
224   GError *error = NULL;
225
226   context = g_app_launch_context_new ();
227   g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
228   g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
229   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
230                                                 "cmdline-app-test",
231                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
232                                                 NULL);
233
234   error = NULL;
235   g_assert (g_app_info_launch (appinfo, NULL, context, &error));
236   g_assert_no_error (error);
237
238   g_assert (launched_reached);
239
240   g_object_unref (appinfo);
241   g_object_unref (context);
242 }
243
244 static void
245 test_tryexec (void)
246 {
247   GAppInfo *appinfo;
248
249   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test2.desktop");
250
251   g_assert (appinfo == NULL);
252 }
253
254 /* Test that we can set an appinfo as default for a mime type or
255  * file extension, and also add and remove handled mime types.
256  */
257 static void
258 test_associations (void)
259 {
260   GAppInfo *appinfo;
261   GAppInfo *appinfo2;
262   GError *error;
263   gboolean result;
264   GList *list;
265
266   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
267                                                 "cmdline-app-test",
268                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
269                                                 NULL);
270
271   error = NULL;
272   result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
273
274   g_assert (result);
275   g_assert_no_error (error);
276
277   appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
278
279   g_assert (appinfo2);
280   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
281
282   g_object_unref (appinfo2);
283
284   result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
285   g_assert (result);
286   g_assert_no_error (error);
287
288   appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
289
290   g_assert (appinfo2);
291   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
292
293   g_object_unref (appinfo2);
294
295   result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
296   g_assert (result);
297   g_assert_no_error (error);
298
299   list = g_app_info_get_all_for_type ("application/x-gio-test");
300   g_assert_cmpint (g_list_length (list), ==, 1);
301   appinfo2 = list->data;
302   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
303   g_object_unref (appinfo2);
304   g_list_free (list);
305
306   g_assert (g_app_info_can_remove_supports_type (appinfo));
307   g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
308   g_assert_no_error (error);
309
310   g_assert (g_app_info_can_delete (appinfo));
311   g_assert (g_app_info_delete (appinfo));
312   g_object_unref (appinfo);
313 }
314
315 static void
316 test_environment (void)
317 {
318   GAppLaunchContext *ctx;
319   gchar **env;
320   const gchar *path;
321
322   g_unsetenv ("FOO");
323   g_unsetenv ("BLA");
324   path = g_getenv ("PATH");
325
326   ctx = g_app_launch_context_new ();
327
328   env = g_app_launch_context_get_environment (ctx);
329
330   g_assert (g_environ_getenv (env, "FOO") == NULL);
331   g_assert (g_environ_getenv (env, "BAR") == NULL);
332   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
333
334   g_strfreev (env);
335
336   g_app_launch_context_setenv (ctx, "FOO", "bar");
337   g_app_launch_context_setenv (ctx, "BLA", "bla");
338
339   env = g_app_launch_context_get_environment (ctx);
340
341   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
342   g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
343   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
344
345   g_strfreev (env);
346
347   g_app_launch_context_setenv (ctx, "FOO", "baz");
348   g_app_launch_context_unsetenv (ctx, "BLA");
349
350   env = g_app_launch_context_get_environment (ctx);
351
352   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
353   g_assert (g_environ_getenv (env, "BLA") == NULL);
354
355   g_strfreev (env);
356
357   g_object_unref (ctx);
358 }
359
360 static void
361 test_startup_wm_class (void)
362 {
363   GDesktopAppInfo *appinfo;
364   const char *wm_class;
365
366   appinfo = g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
367   wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
368
369   g_assert_cmpstr (wm_class, ==, "appinfo-class");
370
371   g_object_unref (appinfo);
372 }
373
374 static void
375 test_supported_types (void)
376 {
377   GAppInfo *appinfo;
378   const char * const *content_types;
379
380   appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop"));
381   content_types = g_app_info_get_supported_types (appinfo);
382
383   g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
384   g_assert_cmpstr (content_types[0], ==, "image/png");
385
386   g_object_unref (appinfo);
387 }
388
389 static void
390 test_from_keyfile (void)
391 {
392   GDesktopAppInfo *info;
393   GKeyFile *kf;
394   GError *error = NULL;
395   const gchar *categories;
396   gchar **keywords;
397   const gchar *file;
398   const gchar *name;
399
400   kf = g_key_file_new ();
401   g_key_file_load_from_file (kf,
402                              SRCDIR "/appinfo-test.desktop",
403                              G_KEY_FILE_NONE,
404                              &error);
405   g_assert_no_error (error);
406   info = g_desktop_app_info_new_from_keyfile (kf);
407   g_key_file_free (kf);
408   g_assert (info != NULL);
409
410   g_object_get (info, "filename", &file, NULL);
411   g_assert (file == NULL);
412
413   file = g_desktop_app_info_get_filename (info);
414   g_assert (file == NULL);
415   categories = g_desktop_app_info_get_categories (info);
416   g_assert_cmpstr (categories, ==, "GNOME;GTK;");
417   keywords = (gchar **)g_desktop_app_info_get_keywords (info);
418   g_assert_cmpint (g_strv_length (keywords), ==, 2);
419   g_assert_cmpstr (keywords[0], ==, "keyword1");
420   g_assert_cmpstr (keywords[1], ==, "test keyword");
421   name = g_desktop_app_info_get_generic_name (info);
422   g_assert_cmpstr (name, ==, "generic-appinfo-test");
423   g_assert (!g_desktop_app_info_get_nodisplay (info));
424
425   g_object_unref (info);
426 }
427
428 int
429 main (int argc, char *argv[])
430 {
431   g_test_init (&argc, &argv, NULL);
432
433   g_test_add_func ("/appinfo/basic", test_basic);
434   g_test_add_func ("/appinfo/text", test_text);
435   g_test_add_func ("/appinfo/launch", test_launch);
436   g_test_add_func ("/appinfo/show-in", test_show_in);
437   g_test_add_func ("/appinfo/commandline", test_commandline);
438   g_test_add_func ("/appinfo/launch-context", test_launch_context);
439   g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
440   g_test_add_func ("/appinfo/tryexec", test_tryexec);
441   g_test_add_func ("/appinfo/associations", test_associations);
442   g_test_add_func ("/appinfo/environment", test_environment);
443   g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
444   g_test_add_func ("/appinfo/supported-types", test_supported_types);
445   g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
446
447   return g_test_run ();
448 }
449