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