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